KEMBAR78
[Quant] [PT2] Enable batchnorm in _move_exported_model_to_eval by leslie-fang-intel · Pull Request #114547 · pytorch/pytorch · GitHub
Skip to content

Conversation

@leslie-fang-intel
Copy link
Collaborator

@leslie-fang-intel leslie-fang-intel commented Nov 26, 2023

Stack from ghstack (oldest at bottom):

Summary
Add standalone batchnorm into _move_exported_model_to_eval to move it from training mode into eval mode

Test Plan

python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
python -u -m pytest -s -v test_quantize_pt2e.py -k test_bn_move_exported_model_to_eval

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @aakhundov @ColinPeppler

Differential Revision: D51853407

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 26, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/114547

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 1248a8f with merge base 7bbc19a (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
```

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
@leslie-fang-intel leslie-fang-intel changed the title Enable batchnorm in _move_exported_model_to_eval [Quant] [PT2] Enable batchnorm in _move_exported_model_to_eval Nov 27, 2023
…eval"


**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
```

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
leslie-fang-intel added a commit to leslie-fang-intel/pytorch that referenced this pull request Nov 27, 2023
@jerryzh168
Copy link
Contributor

cc @andrewor14 please take a look

…eval"


**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
```

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
leslie-fang-intel added a commit that referenced this pull request Nov 28, 2023
ghstack-source-id: 67d7b9e
Pull Request resolved: #114547
Copy link
Contributor

@andrewor14 andrewor14 left a comment

Choose a reason for hiding this comment

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

Hi @leslie-fang-intel, thanks for working on this. Could you also add a check for batchnorm in the existing test?

@andrewor14
Copy link
Contributor

Actually, can you paste here what the graphs look like before and after the switch? I would expect to see aten._native_batch_norm_legit(... train=True) before, and aten._native_batch_norm_legit_no_training after the call to move_exported_model_to_eval. Is this what you observe in the tests?

@leslie-fang-intel
Copy link
Collaborator Author

leslie-fang-intel commented Nov 29, 2023

Actually, can you paste here what the graphs look like before and after the switch? I would expect to see aten._native_batch_norm_legit(... train=True) before, and aten._native_batch_norm_legit_no_training after the call to move_exported_model_to_eval. Is this what you observe in the tests?

UT added as python -u -m pytest -s -v test_quantize_pt2e.py -k test_bn_move_exported_model_to_eval.
Yes, that's what we see and take this UT as example:

  • Before the graph looks like
def forward(self, x):
    arg0, = fx_pytree.tree_flatten_spec(([x], {}), self._in_spec)
    arg0_1 = arg0
    _tensor_constant0 = self._tensor_constant0
    add_ = torch.ops.aten.add_.Tensor(_tensor_constant0, 1);  _tensor_constant0 = None
    empty = torch.ops.aten.empty.memory_format([0], dtype = torch.uint8, layout = torch.strided, device = device(type='cpu'))
    _param_constant0 = self._param_constant0
    _param_constant1 = self._param_constant1
    _tensor_constant1 = self._tensor_constant1
    _tensor_constant2 = self._tensor_constant2
    _native_batch_norm_legit = torch.ops.aten._native_batch_norm_legit.default(arg0_1, _param_constant0, _param_constant1, _tensor_constant1, _tensor_constant2, True, 0.1, 1e-05);  arg0_1 = _param_constant0 = _param_constant1 = _tensor_constant1 = _tensor_constant2 = None
    getitem = _native_batch_norm_legit[0]
    getitem_1 = _native_batch_norm_legit[1]
    getitem_2 = _native_batch_norm_legit[2];  _native_batch_norm_legit = None
    _param_constant2 = self._param_constant2
    _param_constant3 = self._param_constant3
    conv2d = torch.ops.aten.conv2d.default(getitem, _param_constant2, _param_constant3);  getitem = _param_constant2 = _param_constant3 = None
    _tensor_constant3 = self._tensor_constant3
    add__1 = torch.ops.aten.add_.Tensor(_tensor_constant3, 1);  _tensor_constant3 = None
    empty_1 = torch.ops.aten.empty.memory_format([0], dtype = torch.uint8, layout = torch.strided, device = device(type='cpu'))
    _param_constant4 = self._param_constant4
    _param_constant5 = self._param_constant5
    _tensor_constant4 = self._tensor_constant4
    _tensor_constant5 = self._tensor_constant5
    _native_batch_norm_legit_1 = torch.ops.aten._native_batch_norm_legit.default(conv2d, _param_constant4, _param_constant5, _tensor_constant4, _tensor_constant5, True, 0.1, 1e-05);  conv2d = _param_constant4 = _param_constant5 = _tensor_constant4 = _tensor_constant5 = None
    getitem_3 = _native_batch_norm_legit_1[0]
    getitem_4 = _native_batch_norm_legit_1[1]
    getitem_5 = _native_batch_norm_legit_1[2];  _native_batch_norm_legit_1 = None
    return pytree.tree_unflatten([getitem_3], self._out_spec)
  • After the graph looks like
def forward(self, x):
    arg0, = fx_pytree.tree_flatten_spec(([x], {}), self._in_spec)
    arg0_1 = arg0
    _param_constant0 = self._param_constant0
    _param_constant1 = self._param_constant1
    _tensor_constant1 = self._tensor_constant1
    _tensor_constant2 = self._tensor_constant2
    _param_constant2 = self._param_constant2
    _param_constant3 = self._param_constant3
    _native_batch_norm_legit_no_training = torch.ops.aten._native_batch_norm_legit_no_training.default(arg0_1, _param_constant0, _param_constant1, _tensor_constant1, _tensor_constant2, 0.1, 1e-05);  arg0_1 = _param_constant0 = _param_constant1 = _tensor_constant1 = _tensor_constant2 = None
    getitem_6 = _native_batch_norm_legit_no_training[0];  _native_batch_norm_legit_no_training = None
    conv2d = torch.ops.aten.conv2d.default(getitem_6, _param_constant2, _param_constant3);  getitem_6 = _param_constant2 = _param_constant3 = None
    _param_constant4 = self._param_constant4
    _param_constant5 = self._param_constant5
    _tensor_constant4 = self._tensor_constant4
    _tensor_constant5 = self._tensor_constant5
    _native_batch_norm_legit_no_training_1 = torch.ops.aten._native_batch_norm_legit_no_training.default(conv2d, _param_constant4, _param_constant5, _tensor_constant4, _tensor_constant5, 0.1, 1e-05);  conv2d = _param_constant4 = _param_constant5 = _tensor_constant4 = _tensor_constant5 = None
    getitem_7 = _native_batch_norm_legit_no_training_1[0];  _native_batch_norm_legit_no_training_1 = None
    return pytree.tree_unflatten([getitem_7], self._out_spec)

…eval"


**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
python -u -m pytest -s -v test_quantize_pt2e.py -k test_bn_move_exported_model_to_eval
```

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
pytorchmergebot added a commit that referenced this pull request Dec 1, 2023
…eval"


**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
python -u -m pytest -s -v test_quantize_pt2e.py -k test_bn_move_exported_model_to_eval
```

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
…eval"


**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
python -u -m pytest -s -v test_quantize_pt2e.py -k test_bn_move_exported_model_to_eval
```

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler

[ghstack-poisoned]
leslie-fang-intel added a commit that referenced this pull request Dec 2, 2023
ghstack-source-id: b75131f
Pull Request resolved: #114547
@leslie-fang-intel
Copy link
Collaborator Author

Thanks @andrewor14, reopen this PR with additional changes in #114927. Could you help to check if it fixes the internal failures?

@leslie-fang-intel
Copy link
Collaborator Author

Hi @andrewor14, could you help to check if it fixes the internal failure?

@andrewor14
Copy link
Contributor

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

@andrewor14
Copy link
Contributor

Hi @leslie-fang-intel, we're running the tests now. Please don't merge this one yet. I will merge it once all the internal tests pass / once we figure out all the test failures.

@andrewor14
Copy link
Contributor

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@leslie-fang-intel
Copy link
Collaborator Author

Hi @leslie-fang-intel, we're running the tests now. Please don't merge this one yet. I will merge it once all the internal tests pass / once we figure out all the test failures.

Thanks @andrewor14, BTW: we also plan to port it back to 2.2 branch, does it sound good to you?

@facebook-github-bot facebook-github-bot deleted the gh/leslie-fang-intel/48/head branch December 10, 2023 15:27
@kit1980 kit1980 removed the Reverted label Dec 21, 2023
dmenig pushed a commit to dmenig/pytorch that referenced this pull request Dec 21, 2023
…ch#114547)

**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
python -u -m pytest -s -v test_quantize_pt2e.py -k test_bn_move_exported_model_to_eval
```

Pull Request resolved: pytorch#114547
Approved by: https://github.com/jgong5, https://github.com/andrewor14
dmenig pushed a commit to dmenig/pytorch that referenced this pull request Dec 21, 2023
…ytorch#114725)

**Summary**
Enable Inplace Dropout replacement in `_move_exported_model_to_eval`

**Test Plan**
```
python -u -m pytest -s -v test_quantize_pt2e.py -k test_move_exported_model_to_eval
```

Pull Request resolved: pytorch#114725
Approved by: https://github.com/andrewor14, https://github.com/jgong5
ghstack dependencies: pytorch#114547
dmenig pushed a commit to dmenig/pytorch that referenced this pull request Dec 21, 2023
dmenig pushed a commit to dmenig/pytorch that referenced this pull request Dec 21, 2023
…ch#114547)

**Summary**
Add standalone batchnorm into `_move_exported_model_to_eval` to move it from training mode into eval mode

**Test Plan**
```
python -m pytest test_mkldnn_pattern_matcher.py -k test_qat_bn_conv2d
python -u -m pytest -s -v test_quantize_pt2e.py -k test_bn_move_exported_model_to_eval
```

Differential Revision: [D51853407](https://our.internmc.facebook.com/intern/diff/D51853407)
Pull Request resolved: pytorch#114547
Approved by: https://github.com/jgong5, https://github.com/andrewor14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants