Commit Graph

4 Commits

Author SHA1 Message Date
XiaobingSuper
b8679ee1fc fix conv+bn folding issue when bn hasn't running states (#71259)
Summary:
Doing conv+bn folding which bn hasn't a running stats, there have error for JIT and FX path:

```
import torch

import torch.nn as nn

import torch.fx.experimental.optimization as optimization

class M(nn.Module):
    def __init__(self):
        super(M, self).__init__()
        self.conv = nn.Conv2d(32, 64, 3, stride=2)
        self.bn = nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        return x

x = torch.randn([1, 32, 50, 50])

model = M().eval()

'''
# jit path
with torch.no_grad():
    traced = torch.jit.trace(model, x).eval()
    traced = torch.jit.freeze(traced)
'''

# FX path
fused_model = optimization.fuse(model)
```

expected result:
1. JIT path
```
Traceback (most recent call last):
  File "bn_test.py", line 27, in <module>
    traced = torch.jit.freeze(traced)
  File "/home/xiaobinz/miniconda3/envs/pytorch-master/lib/python3.8/site-packages/torch/jit/_freeze.py", line 119, in freeze
    run_frozen_optimizations(out, optimize_numerics, preserved_methods)
  File "/home/xiaobinz/miniconda3/envs/pytorch-master/lib/python3.8/site-packages/torch/jit/_freeze.py", line 167, in run_frozen_optimizations
    torch._C._jit_pass_optimize_frozen_graph(mod.graph, optimize_numerics)
RuntimeError: Expected Tensor but got None
```
2. FX path
```
Traceback (most recent call last):
  File "bn_test.py", line 31, in <module>
    model = optimization.fuse(model, inplace=True)
  File "/home/xiaobinz/miniconda3/envs/pytorch-master/lib/python3.8/site-packages/torch/fx/experimental/optimization.py", line 71, in fuse
    fused_conv = fuse_conv_bn_eval(conv, bn)
  File "/home/xiaobinz/miniconda3/envs/pytorch-master/lib/python3.8/site-packages/torch/nn/utils/fusion.py", line 11, in fuse_conv_bn_eval
    fuse_conv_bn_weights(fused_conv.weight, fused_conv.bias,
  File "/home/xiaobinz/miniconda3/envs/pytorch-master/lib/python3.8/site-packages/torch/nn/utils/fusion.py", line 23, in fuse_conv_bn_weights
    bn_var_rsqrt = torch.rsqrt(bn_rv + bn_eps)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
```

This PR will fix this issue.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/71259

Reviewed By: anjali411

Differential Revision: D33595049

Pulled By: davidberard98

fbshipit-source-id: 0fe56bb2bb25d6d54ebc53789d2ad22458da9012
(cherry picked from commit 5672c08378)
2022-01-18 22:12:41 +00:00
Horace He
84d8e3b0f6 [FX] Finished prepare_for_inference API for release (#58293)
Summary:
Added an ability to configure which passes to run.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/58293

Reviewed By: bdhirsh

Differential Revision: D28435948

Pulled By: Chillee

fbshipit-source-id: dfc7f1ef6b38e6f49c2423a5efe8477a645171d0
2021-05-14 14:10:07 -07:00
Sam Estep
75024e228c Add lint for unqualified type: ignore (#56290)
Summary:
The other half of https://github.com/pytorch/pytorch/issues/56272.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/56290

Test Plan:
CI should pass on the tip of this PR, and we know that the lint works because the following CI runs (before this PR was finished) failed:

- https://github.com/pytorch/pytorch/runs/2384511062
- https://github.com/pytorch/pytorch/actions/runs/765036024

Reviewed By: seemethere

Differential Revision: D27867219

Pulled By: samestep

fbshipit-source-id: e648f07b6822867e70833e23ddafe7fb7eaca235
2021-04-21 08:07:23 -07:00
Horace He
24bfcd537e [FX] Added FX prepare_for_inference for Intel CPUs (#53805)
Summary:
Part of https://github.com/pytorch/pytorch/issues/48209

Taken from the docstring:
 Performs a set of optimization passes to optimize a model for the purposes of inference. Specifically, the passes that are run are:
    1. Conv/BN fusion
    2. Dropout removal
    3. MKL layout optimizations

The third optimization takes a function `use_mkl_heuristic` that's used to determine whether a subgraph should be explicity run in MKL layout.

I implemented 2 heuristics:
1. Does it in MKL if the subgraph is larger than 2.
2. Benchmarks each subgraph with MKL layout and without, and keeps the subgraph if it's faster.

### Batch size of 10 and multi-threaded.

Results with the second heuristic are generally as strong as the "jit.freeze" version, except in `densenet` and `vgg`, where it's faster, likely due to the heuristic being better. With the first heuristic, there are some notable gaps, particularly on `inception_v3` and `alexnet`.

```
model         Eager      FX         FX Auto   jit.mkldnn
------------  ---------  ---------  ---------  ---------  -
custom        0.195614   0.14686    0.15929    0.156442   6
resnet18      0.172012   0.114007   0.119678   0.12945    6
resnet50      0.486463   0.294308   0.299518   0.318121   6
densenet161   0.955309   0.893502   0.882798   1.29315    6
inception_v3  0.38454    0.307076   0.239513   0.233083   6
googlenet     0.229388   0.237486   0.170458   0.174106   6
shufflenet    0.0513613  0.0286739  0.0292908  0.0267209  6
alexnet       0.0709602  0.0768137  0.0660831  0.0650399  6
vgg16         1.053993   0.9013264  0.9360212  1.082820   6
mobilenet     0.12264    0.0970935  0.0936568  0.106314   6
mnasnet       0.0989875  0.0412083  0.0424499  0.0472336  6
resnext       0.476811   0.315428   0.314422   0.343156   6
```

For single-threaded (still running...)
```
model             eager         FX    FX auto        mkl    threads
------------  ---------  ---------  ---------  ---------  ---------
custom        0.0401415  0.259863   0.0263152  0.200667           1
resnet18      0.499931   0.382113   0.383711   0.396335           1
resnet50      1.10353    0.911865   0.923645   0.992125           1
densenet161   2.20158    2.39421    2.08204    2.30124            1
inception_v3  0.79161    0.849207   0.703546   0.724492           1
googlenet     0.66896    0.820965   0.515927   0.529414           1
shufflenet    0.0987308  0.0689343  0.0629298  0.0617193          1
alexnet       0.198795   0.19862    0.19325    0.211934           1
vgg16         3.744      3.2499     3.28503    3.31576            1
mobilenet     0.152725   0.14505    0.135555   0.159754           1
mnasnet       0.141983   0.089406   0.089599   0.0956167          1
resnext       1.13778    0.97016    0.955417   0.965376           1
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/53805

Reviewed By: gmagogsfm

Differential Revision: D27424611

Pulled By: Chillee

fbshipit-source-id: a39137159de962fba7ca15121dfa9e78c1e01223
2021-03-31 10:15:01 -07:00