class oneflow.set_grad_enabled(is_train=True)

Context-manager that enabled gradient calculation.

Enables gradient calculation, if it has been disabled via no_grad.

This context manager is thread local; it will not affect computation in other threads.

Also functions as a decorator. (Make sure to instantiate with parenthesis.)


mode (bool) – Flag whether to enable or disable gradient calculation. (default: True)

>>> import oneflow as flow
>>> x = flow.ones(2, 3, requires_grad=True)
>>> with flow.set_grad_enabled(True):
...     y = x * x
>>> y.requires_grad
>>> @flow.set_grad_enabled(False)
... def no_grad_func(x):
...     return x * x
>>> y = no_grad_func(x)
>>> y.requires_grad

Initialize self. See help(type(self)) for accurate signature.



Call self as a function.

__delattr__(name, /)

Implement delattr(self, name).


Default dir() implementation.


__eq__(value, /)

Return self==value.

__exit__(exc_type, exc_val, exc_tb)

__format__(format_spec, /)

Default object formatter.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__gt__(value, /)

Return self>value.


Return hash(self).


Initialize self.


This method is called when a class is subclassed.

__le__(value, /)

Return self<=value.

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.


Create and return a new object.


Helper for pickle.

__reduce_ex__(protocol, /)

Helper for pickle.


Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).


Size of object in memory, in bytes.


Return str(self).


Abstract classes can override this to customize issubclass().