[ONNX] Fix an issue that optimizations might adjust graph inputs unexpectedly. (...
authorBowenBao <bowbao@microsoft.com>
Fri, 20 Aug 2021 19:44:29 +0000 (12:44 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Fri, 20 Aug 2021 19:46:52 +0000 (12:46 -0700)
commit87602549112aac84c6f36fa2b2fd18902ec6bef6
tree5746785473d738a7ea94614d9305a21ab4e7704e
parenta65d1ae7cc8c104d041f554d460da0a84c0f2d4e
[ONNX] Fix an issue that optimizations might adjust graph inputs unexpectedly. (#61280) (#62763)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/62763

This PR is to fix the issue that the graph inputs might be updated when we export the model in inference mode.

When a model is export in inference mode, some optimizations will be made. One side effect of these optimizations is: the inputs of graph might be adjusted. Such optimizatiosn include:

1. Conv and BatchNorm op fusion.
2. Do constant folding.

If the user sets export_params=False, or set keep_initializers_as_inputs=True, it's highly possible that the user wants to provide the corresponding parameters or initiliazers as the inputs of the graph.
In such situation, no matter the model is export in inference mode or training mode, exporter needs to prevent above optimizations from adjusting the graph inputs. By this, the inputs of graph could match inputs that users provided.

The changes in this PR, add an additional common judgement to see if the above optimizations needs to be done or not. From the value of export_params and keep_initializers_as_inputs arguments, infer if the graph inputs are allowed to be adjusted.
If no, these optimizations will be ignored, even other requirements are matched.

Besides these code changes, the comments of some parameters below have been updated so that users have more thoughts when they consider how to leverage these parameters for different purposes:

1. export_params
2. training
3. do_constant_folding
4. keep_initializers_as_inputs

Test Plan: Imported from OSS

Reviewed By: SplitInfinity

Differential Revision: D30375183

Pulled By: msaroufim

fbshipit-source-id: 4db8b9695649eb32a3a0fefa950ee2e5651bdba0

Co-authored-by: fatcat-z <jiz@microsoft.com>
test/onnx/expect/TestOperators.test_prelu.expect
test/onnx/expect/TestOperators.test_retain_param_name_disabled.expect
torch/_C/__init__.pyi.in
torch/csrc/jit/passes/onnx/eval_peephole.cpp
torch/csrc/jit/passes/onnx/eval_peephole.h
torch/csrc/jit/python/init.cpp
torch/onnx/__init__.py
torch/onnx/utils.py