Skip to content

Commit b858f42

Browse files
ezyangfacebook-github-bot
authored andcommitted
Document that no_grad is thread local. (#21755)
Summary: Pull Request resolved: #21755 ghimport-source-id: dfb5375 Differential Revision: D15811172 Pulled By: ezyang fbshipit-source-id: c8c7c1c15277d8fe8cc513e20af449257d7ff15c
1 parent 3e8dc56 commit b858f42

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

docs/source/torch.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ Locally disabling gradient computation
142142
The context managers :func:`torch.no_grad`, :func:`torch.enable_grad`, and
143143
:func:`torch.set_grad_enabled` are helpful for locally disabling and enabling
144144
gradient computation. See :ref:`locally-disable-grad` for more details on
145-
their usage.
145+
their usage. These context managers are thread local, so they won't
146+
work if you send work to another thread using the :module:`threading`
147+
module, etc.
146148

147149
Examples::
148150

torch/autograd/grad_mode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class no_grad(object):
1010
consumption for computations that would otherwise have `requires_grad=True`.
1111
In this mode, the result of every computation will have
1212
`requires_grad=False`, even when the inputs have `requires_grad=True`.
13+
This context manager is thread local; it will not affect computation
14+
in other threads.
1315
1416
Also functions as a decorator.
1517
@@ -49,6 +51,8 @@ class enable_grad(object):
4951
5052
Enables gradient calculation inside a :class:`~no_grad` context. This has
5153
no effect outside of :class:`~no_grad`.
54+
This context manager is thread local; it will not affect computation
55+
in other threads.
5256
5357
Also functions as a decorator.
5458
@@ -93,6 +97,8 @@ class set_grad_enabled(object):
9397
9498
``set_grad_enabled`` will enable or disable grads based on its argument :attr:`mode`.
9599
It can be used as a context-manager or as a function.
100+
This context manager is thread local; it will not affect computation
101+
in other threads.
96102
97103
Arguments:
98104
mode (bool): Flag whether to enable grad (``True``), or disable

0 commit comments

Comments
 (0)