Skip to content

Add a Constant Propagation Pass to the JIT#8808

Closed
eellison wants to merge 2 commits intopytorch:masterfrom
eellison:constant_prop
Closed

Add a Constant Propagation Pass to the JIT#8808
eellison wants to merge 2 commits intopytorch:masterfrom
eellison:constant_prop

Conversation

@eellison
Copy link
Contributor

@eellison eellison commented Jun 22, 2018

Adding a constant propagation pass to the JIT. I have added examples to the expect files.

There are a couple of special cases which have not been implemented here. IF nodes with constant conditions can be inlined with the correct block. WHILE nodes can be removed if the condition is false. I have added a test for each case in test_jit.py file as expected failures.

To be consistent with DCE, python ops & CPP ops are treated as not having side-effects.

@yf225
Copy link
Contributor

yf225 commented Jun 22, 2018

There are probably files that we don't want to put in the PR. Can we rebase onto master, run git submodule update, and then push again?

Copy link
Contributor

@apaszke apaszke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM, but has some accidental checkins that will need to be removed

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@eellison
Copy link
Contributor Author

@apaszke Thanks for your comments! I will resubmit with the changes requested

@eellison
Copy link
Contributor Author

@apaszke all the requested changes have been made.

@jamesr66a I ran git submodule init & force pushed, but it looks like third_party/onnx is still in the pull request.

ps: sorry about the commit clutter 😬 First time using git in a while, next pull request will be cleaner.

@zdevito
Copy link
Contributor

zdevito commented Jun 29, 2018

This looks good, but we need to remove the changes to thirdy_party/onnx in the diff.
You want to do:

git diff origin/master third_party/onnx
cd third_party/onnx
git checkout ${onnx_commit_hash_of_master}
cd ../..
git commit -a --amend

@soumith
Copy link
Collaborator

soumith commented Jul 9, 2018

in Convnet models, it's common for the final layer to have a view(batch_size, -1), like this resnet.

This shows up in the trace like this:

 %1871 : Float(4, 2048, 1, 1) = aten::avg_pool2d[kernel_size=[7, 7], stride=[1, 1], padding=[0, 0], ceil_mode=0, count_include_pad=1](%1869)
  %1872 : Long() = aten::size[dim=0](%1871)
  %1873 : Long() = prim::Constant[value={-1}]()
  %1874 : Dynamic = aten::stack[dim=0](%1872, %1873)
  %1875 : Float(4, 2048) = aten::view(%1871, %1874)
  %1876 : Float(2048!, 1000!) = aten::t(%319)
  %1877 : Float(4!, 1000) = aten::expand[size=[4, 1000], implicit=1](%320)

Since traces are already specialized on sizes (i.e. we know the size of %1871), it'll be good to fold these instructions in the Constant Folding pass.

There's a "just enough done" pass on doing this by @asuhan at https://github.com/asuhan/pytorch/blob/master/torch/csrc/jit/passes/constant_folding.cpp

@yf225
Copy link
Contributor

yf225 commented Jul 9, 2018

@eellison Could you rebase this onto current master? It should fix the No submodule mapping found in .gitmodules for path 'third_party/tbb' error.

@zdevito
Copy link
Contributor

zdevito commented Jul 13, 2018

@eellison can we get this merged? It is going to start blocking other PR very soon.

@eellison eellison force-pushed the constant_prop branch 2 times, most recently from 683c3fd to 20b30ff Compare July 17, 2018 16:58
@eellison
Copy link
Contributor Author

@pytorchbot retest this please

1 similar comment
@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@eellison eellison force-pushed the constant_prop branch 2 times, most recently from 64fd978 to 9e5fc58 Compare July 25, 2018 01:51
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eellison has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ezyang
Copy link
Contributor

ezyang commented Jul 25, 2018

@pytorchbot retest this please

6 similar comments
@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@ezyang
Copy link
Contributor

ezyang commented Jul 25, 2018

@pytorchbot retest this please

@ezyang
Copy link
Contributor

ezyang commented Jul 25, 2018

@pytorchbot retest this please

@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@ezyang
Copy link
Contributor

ezyang commented Jul 26, 2018

this error looks real:

02:13:09 ======================================================================
02:13:09 ERROR: test_type_annotations_varargs (__main__.TestScript)
02:13:09 ----------------------------------------------------------------------
02:13:09 Traceback (most recent call last):
02:13:09   File "test_jit.py", line 2941, in test_type_annotations_varargs
02:13:09     self.checkScript(fn1, (x, y, z), optimize=True)
02:13:09   File "test_jit.py", line 1417, in checkScript
02:13:09     self.checkScript(source, inputs, optimize, outputs, script.__name__, capture_output, frames_up=2)
02:13:09   File "test_jit.py", line 1427, in checkScript
02:13:09     outputs_ge = ge(*inputs)
02:13:09 RuntimeError: 
02:13:09 Schema not found for node. File a bug report.
02:13:09 Node: %3 : Dynamic = ^fn_varargs()(%4)
02:13:09 
02:13:09 Input types:Dynamic
02:13:09 candidates were:
02:13:09 :
02:13:09 def fn1(x, y, z):
02:13:09     return fn_varargs(x)
02:13:09            ~~~~~~~~~~ <--- HERE
02:13:09 
02:13:09 

@eellison eellison force-pushed the constant_prop branch 3 times, most recently from 583b149 to f2dbfd5 Compare July 27, 2018 03:50
@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@eellison
Copy link
Contributor Author

@pytorchbot retest this please

@zdevito
Copy link
Contributor

zdevito commented Jul 30, 2018

This is failing because it needs to be rebased and its expect tests need to be --accepted. The removal of attributes has changed how some graphs print.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eellison has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

jramseyer pushed a commit to jramseyer/pytorch that referenced this pull request Jul 31, 2018
Summary:
Adding a constant propagation pass to the JIT. I have added examples to the expect files.

There are a couple of special cases which have not been implemented here. IF nodes with constant conditions can be inlined with the correct block. WHILE nodes can be removed if the condition is false.  I have added a test for each case in test_jit.py file as expected failures.

To be consistent with DCE, python ops & CPP ops are treated as not having side-effects.
Pull Request resolved: pytorch#8808

Reviewed By: wanchaol

Differential Revision: D8906770

Pulled By: eellison

fbshipit-source-id: 10ad796d89f80b843566c9ddad6a0abd1f3dc74c
goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
Summary:
Adding a constant propagation pass to the JIT. I have added examples to the expect files.

There are a couple of special cases which have not been implemented here. IF nodes with constant conditions can be inlined with the correct block. WHILE nodes can be removed if the condition is false.  I have added a test for each case in test_jit.py file as expected failures.

To be consistent with DCE, python ops & CPP ops are treated as not having side-effects.
Pull Request resolved: pytorch#8808

Reviewed By: wanchaol

Differential Revision: D8906770

Pulled By: eellison

fbshipit-source-id: 10ad796d89f80b843566c9ddad6a0abd1f3dc74c
@ezyang ezyang added the merged label Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants