Skip to content

Commit

Permalink
Add note on traced module train/eval behavior
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: pytorch#12903

Differential Revision: D10489090

Pulled By: SsnL

fbshipit-source-id: 13ff5587f53706b360dd0905d0ae97fb16ae2bf0
  • Loading branch information
ssnl authored and facebook-github-bot committed Oct 22, 2018
1 parent a022fd2 commit 5e8e199
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions torch/jit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,9 @@ def foo(x, y):
traced_foo = torch.jit.trace(foo, (torch.rand(3), torch.rand(3)))
.. note::
Tracing a *function* will produce a ScriptModule with a single
Tracing a *function* will produce a ``ScriptModule`` with a single
``forward`` method that implements that function, and that contains
no parameteres.
no parameters.
Example::
Expand All @@ -908,13 +908,26 @@ def foo(x, y):
.. note::
Since tracing only records operations on tensors, it will not record any
control-flow operations like if statements or loops. When this control-flow is
constant across your module, this is fine and it often just inlines
configuration decisions. But sometimes the control-flow is actually part of the
model itself. For instance, a beam search in sequence-to-sequence translation is
a loop over the (varying) sequence length of inputs. In this case tracing would
not be appropriate and the beam search should be written using scripting.
Tracing only records operations done when the given function is run on the given
tensors. Therefore, the returned ``ScriptModule`` will always run the same traced
graph on any input. This has some important implications when your module is
expected to run different sets of operations, depending on the input and/or the
module state. For example,
+ Tracing will not record any control-flow like if statements or loops. When
this control-flow is constant across your module, this is fine and it often
just inlines configuration decisions. But sometimes the control-flow is
actually part of the model itself. For instance, a beam search in
sequence-to-sequence translation is a loop over the (varying) sequence
length of inputs.
+ In the returned ``ScriptModule``, operations that have different behaviors
in ``training`` and ``eval`` modes will always behave as if it is in the
mode it was in during tracing, no matter which mode the ``ScriptModule``
is in.
In cases like these, tracing would not be appropriate and scripting is a better
choice.
**Scripting:**
Expand Down

0 comments on commit 5e8e199

Please sign in to comment.