diff options
author | James Reed <jamesreed@fb.com> | 2019-03-26 20:47:23 -0700 |
---|---|---|
committer | Facebook Github Bot <facebook-github-bot@users.noreply.github.com> | 2019-03-26 20:52:31 -0700 |
commit | f447b63ed0c7a5fdf531d5b0022fb24349682e17 (patch) | |
tree | 83d71b18c9a689dc3e30e7ffdda273c9d62f3da3 /docs | |
parent | 45ec4920e32d1422342148afedf3254675a7e5d7 (diff) | |
download | pytorch-f447b63ed0c7a5fdf531d5b0022fb24349682e17.tar.gz pytorch-f447b63ed0c7a5fdf531d5b0022fb24349682e17.tar.bz2 pytorch-f447b63ed0c7a5fdf531d5b0022fb24349682e17.zip |
Add section about .code to docs
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/18493
Differential Revision: D14634677
Pulled By: jamesr66a
fbshipit-source-id: 9ee065f6ce4218f725b93deb4c64b4ef55926145
Diffstat (limited to 'docs')
-rw-r--r-- | docs/source/jit.rst | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/docs/source/jit.rst b/docs/source/jit.rst index 6b2880f9c4..ad968a0f8f 100644 --- a/docs/source/jit.rst +++ b/docs/source/jit.rst @@ -694,8 +694,56 @@ Disable JIT for Debugging function. +Inspecting Code +^^^^^^^^^^^^^^^ + + TorchScript provides a code pretty-printer for all ScriptModule instances. This + pretty-printer gives an interpretation of the script method's code as valid + Python syntax. For example:: + + @torch.jit.script + def foo(len): + # type: (int) -> torch.Tensor + rv = torch.zeros(3, 4) + for i in range(len): + if i < 10: + rv = rv - 1.0 + else: + rv = rv + 1.0 + return rv + + print(foo.code) + + A ``ScriptModule`` with a single ``forward`` method will have an attribute + ``code``, which you can use to inspect the ``ScriptModule``'s code. + If the ScriptModule has more than one method, you will need to access + ``.code`` on the method itself and not the module. We can inspect the + code of a method named ``bar`` on a ScriptModule by accessing ``.bar.code``. + + The example script abouve produces the code:: + + def forward(self, + len: int) -> Tensor: + rv = torch.zeros([3, 4], dtype=None, layout=None, device=None) + rv0 = rv + for i in range(len): + if torch.lt(i, 10): + rv1 = torch.sub(rv0, 1., 1) + else: + rv1 = torch.add(rv0, 1., 1) + rv0 = rv1 + return rv0 + + This is TorchScript's interpretation of the code for the ``forward`` method. + You can use this to ensure TorchScript (tracing or scripting) has captured + your model code correctly. + + Interpreting Graphs ^^^^^^^^^^^^^^^^^^^ + TorchScript also has a representation at a lower level than the code pretty- + printer, in the form of IR graphs. + TorchScript uses a static single assignment (SSA) intermediate representation (IR) to represent computation. The instructions in this format consist of ATen (the C++ backend of PyTorch) operators and other primitive operators, @@ -714,11 +762,8 @@ Interpreting Graphs print(foo.graph) - A ``ScriptModule`` with a single ``forward`` method will have an attribute - ``graph``, which you can use to inspect the IR representing the computation. - If the ScriptModule has more than one method, you will need to access - ``.graph`` on the method itself and not the module. We can inspect the - graph of a method named ``bar`` on a ScriptModule by accessing ``.bar.graph``. + ``.graph`` follows the same rules described in the Inspecting Code section + with regard to ``forward`` method lookup. The example script above produces the graph:: |