Commit Graph

120 Commits

Author SHA1 Message Date
Natalia Gimelshein
3d4a6ff15d Revert D32154788: Move Concat Linear out of Optimize Numerics
Test Plan: revert-hammer

Differential Revision:
D32154788 (ea94dde573)

Original commit changeset: faa6465c89b3

fbshipit-source-id: 0dcaa65268b68ed01e6a5bc7b73ade1f51163b33
2021-11-04 12:20:02 -07:00
Natalia Gimelshein
279af1a668 Revert D32154787: Formatted with Black
Test Plan: revert-hammer

Differential Revision:
D32154787 (08d630b9a6)

Original commit changeset: 6a95691c4ad9

fbshipit-source-id: 2dbcf2395071433731683f685a0351fa8604d620
2021-11-04 12:18:37 -07:00
John Clow
08d630b9a6 Formatted with Black (#67792)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/67792

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D32154787

Pulled By: Gamrix

fbshipit-source-id: 6a95691c4ad9d997071bb4ffc00b5eab30f90b81
2021-11-04 11:32:26 -07:00
John Clow
ea94dde573 Move Concat Linear out of Optimize Numerics (#67196)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/67196

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D32154788

Pulled By: Gamrix

fbshipit-source-id: faa6465c89b3676d6b1ff7c20a677738a7fbdf88
2021-11-04 11:30:39 -07:00
Mike Iovine
dd81fa9027 [JIT] Freeze allows preservation of submodule attributes (#66102)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/66102

This changes allows the `preserved_attributes` parameter of `torch.jit.freeze` to accept attributes of submodules. Previously, only root-level attributes were able to be preserved. Example:

```
class SubModule(nn.Module):
    def __init__(self):
        super(SubModule, self).__init__()
        self.a = 1
        self.b = 2

    def forward(self):
        return self.a + self.b

class Module(nn.Module):
    def __init__(self):
        super(Module, self).__init__()
        self.sub = SubModule()

    def forward(self):
        return self.sub()

mod = torch.jit.script(Module())
mod.eval()
frozen_mod = torch.jit.freeze(mod, preserved_attrs = ['sub.a'])

mod.sub   # OK
mod.sub.a # OK
mod.sub.b # Error, not preserved
mod()     # = 3
mod.sub.a = 0
mod()     # = 2
```

Test Plan: `buck test caffe2/test:jit -- TestFreezing`

Reviewed By: eellison

Differential Revision: D31383868

fbshipit-source-id: 34a05ca9528d4e5f04f71ac2a339fd584a8fa305
2021-10-25 07:56:20 -07:00
Jane Xu
09c7771e9c Set test owners for jit tests (#66808)
Summary:
Action following https://github.com/pytorch/pytorch/issues/66232

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

Reviewed By: mrshenli

Differential Revision: D31761414

Pulled By: janeyx99

fbshipit-source-id: baf8c49ff9c4bcda7b0ea0f6aafd26380586e72d
2021-10-25 07:51:10 -07:00
John Clow
3bad54069b Concatting multiple linear layers with same input Tensor (different weight/bias) (#63198)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/63198

Linear layers using the same input tensor can be concatted together
as long as the weights and biases are compatible.

Test Plan: Imported from OSS

Reviewed By: albanD

Differential Revision: D31240642

fbshipit-source-id: 1e78daa6b89822412ba2513d326ee0e072ceff1e
2021-10-08 10:55:46 -07:00
John Clow
6cdea8239e Precomputing Transposes for frozen linear layers (#65631)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/65631

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D31314248

Pulled By: Gamrix

fbshipit-source-id: 85611f3ccfe7b91a183d5d12f7fb9aca3c51acb0
2021-10-05 20:08:32 -07:00
Vasiliy Kuznetsov
227e37dd39 pytorch quantization ao migration phase 2: caffe2/test (#65832)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/65832

Renames `torch.quantization` to `torch.ao.quantization` in `caffe2/test`
folder.

```
find caffe2/test/ -type f -name "*.py" -print0 | xargs -0 sed -i "s/torch\.quantization/torch.ao.quantization/g"
HG: manually revert the files testing this migration
hg revert caffe2/test/quantization/ao_migration/common.py
hg revert caffe2/test/quantization/ao_migration/test_ao_migration.py
```

Test Plan: CI

Reviewed By: z-a-f

Differential Revision: D31275754

fbshipit-source-id: 4ed54a74525634feb0f47a26d071102e19c30049
2021-10-01 06:26:30 -07:00
Elias Ellison
928a4bbafb [JIT] Fix compilation unit reference link in constant object upon load (#65784)
Summary:
Follow up to https://github.com/pytorch/pytorch/pull/65442, make sure objects inserted into the graph from load do not holding owning reference.

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

Reviewed By: suo

Differential Revision: D31251033

Pulled By: eellison

fbshipit-source-id: 59efe19ce6f70744383de4eebf0f89f79f3eb03a
2021-09-30 09:32:28 -07:00
Philip Meier
57d4c6cf42 replace self.assertTrue(torch.allclose(..)) with self.assertEqual(…) (#63637)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/63565

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

Reviewed By: malfet

Differential Revision: D30541266

Pulled By: mruberry

fbshipit-source-id: ab461949782c6908a589ea098fcfcf5c3e081ee6
2021-08-25 16:47:40 -07:00
Nikolay Korovaiko
5254e3adb8 layernom inplace (#63437)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/63437

Test Plan: Imported from OSS

Reviewed By: ngimel

Differential Revision: D30388824

Pulled By: Krovatkin

fbshipit-source-id: 852d19bf238544c5de177ed5854dcd01c7ae5572
2021-08-18 23:07:25 -07:00
Nikolay Korovaiko
531262fe2e layernorm (#63436)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/63436

use MKLDNN layernorm

use mkldnn version 2

address Elias feedback

fix build CI errors

Test Plan: Imported from OSS

Reviewed By: ngimel

Differential Revision: D30388825

Pulled By: Krovatkin

fbshipit-source-id: fb909bfbf53cb8567a43aac40f51c491daeec908
2021-08-18 23:05:39 -07:00
Elias Ellison
254148ec7d Add tensor-scalar op (#62903)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/62903

Test Plan: Imported from OSS

Reviewed By: pbelevich, SplitInfinity

Differential Revision: D30168338

Pulled By: eellison

fbshipit-source-id: 7dcb34ddd76c6aad4108a4073d3c8a93d974d0ef
2021-08-09 08:54:47 -07:00
Elias Ellison
211bac53ef [JIT] Add optimize_for_inference API (#58193)
Summary:
Freezing exists as a pass which partially evaluates your model and applies generic optimizations which should speed it up. Optimize for inference is a counterpart to these optimizations which runs build & server specific optimizations.  The interaction with existing `optimize_frozen_module` is not great, I guess we could just deprecate the API entirely? it was never officially released but just existed to document the `optimize_numerics` keyword.

Eventually, I would like to add a way of adding example inputs but I didnt add that here because they are not being used at all yet. I also have not yet included a way to blacklist individual optimizations, and would like to wait until we move this to Beta and have a little more clarity on how everything will fit together. I also think blacklisting will be an uncommon use case for the current optimizations.

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

Reviewed By: bertmaher, navahgar

Differential Revision: D28443714

Pulled By: eellison

fbshipit-source-id: b032355bb2585720a6d2f00c89d0d9a7ef60e649
2021-05-15 15:50:14 -07:00
Hsiu-Chi Chang
f4a5730a6b Add LowerSimpleTuples for freeze tuples (#57915)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/57698

Follow the suggestion mentioned in https://github.com/pytorch/pytorch/issues/57698
add a call to LowerSimpleTuples after the call:
https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/passes/freeze_module.cpp#L89.

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

Reviewed By: agolynski

Differential Revision: D28387310

Pulled By: nikithamalgifb

fbshipit-source-id: 5becb608c5352240b30dfdf03a821d0297e9609c
2021-05-12 19:07:20 -07:00
Nikolay Korovaiko
aeaa91bff6 mkldnn gelu (#53615)
Summary:
Fixes #{issue number}

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

Reviewed By: anjali411

Differential Revision: D28154396

Pulled By: Krovatkin

fbshipit-source-id: 7a9d4d37dc06e54e3249c531a034667b5a2afc46
2021-05-05 02:03:52 -07:00
Bin Bao
c1a442248b [JIT] Enable conv-add-relu fusion as a part of frozen graph optimization (#56580)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56580

Turn on conv-add-relu fusion as default for the frozen graph optimization.

Test Plan:
```
python test/test_jit.py -k test_freeze_conv_relu_fusion
```

Reviewed By: nikithamalgifb

Differential Revision: D27915515

Pulled By: desertfire

fbshipit-source-id: 9a68d2a6aba70e697258c02c4fd3f3fbfc9fb8f6
2021-04-30 15:29:38 -07:00
Nikolay Korovaiko
cfbd06d7a1 add all pools, Batchnorm and Tanh (i.e. all ideeped MKLDNN ops) to MKLDNNFuser (#56541)
Summary:
Fixes #{issue number}

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

Reviewed By: pbelevich

Differential Revision: D27930353

Pulled By: Krovatkin

fbshipit-source-id: 4d5b932bad4154e8bdd6e35498354e13b39c87a1
2021-04-27 08:59:30 -07:00
Nikolay Korovaiko
d6a25a58f5 add hardtanh(0,6) to the set of MKLDNN fusible ops for mobilenetv2 (#56203)
Summary:
TODO: post the numbers for mobilenetv2

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

Reviewed By: malfet

Differential Revision: D27917557

Pulled By: Krovatkin

fbshipit-source-id: acea0f933a7e8c7a036a494295f68222c46a36f7
2021-04-23 08:08:17 -07:00
Nikolay Korovaiko
04e7891aab Add adaptive_avgpool2d to the set of fusible ops (#56180)
Summary:
This improves mobilenetv3 perf from ~1300msto 1147ms (~12%)

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

Reviewed By: Chillee

Differential Revision: D27840860

Pulled By: Krovatkin

fbshipit-source-id: 6ce38e93fd2f55e68a69c34b45271743f84a13b8
2021-04-17 02:04:17 -07:00
Nikolay Korovaiko
a43483586d A heuristic to avoid perf incompatible MKLDNN formats for binary ops (#56089)
Summary:
After adding new ops to a set of fusible ops, mobilenetv3 slows down to **9000ms from 1200ms** without this fix.

This happens because one of the inputs was expanded and converted to nchw/nhwc
we might end up in a very bad spot if the second argument
is in a blocked format. In this case, MKLDNN uses its
reference implementation for a binary operation that follows
these broadcasts and it could be up to ~100x slower.
We use a very simple heuristic to convert an arg in nchw
to the blocked format of the other argument.

* MKLDNN_VERBOSE without the issue:
[test_mobilenet_nopool.txt](https://github.com/pytorch/pytorch/files/6319528/test_mobilenet_nopool.txt)
* MKLDNN_VERBOSE with the issue (Note the times for `ref` operations)
[test_mobilenet_pool.txt](https://github.com/pytorch/pytorch/files/6319529/test_mobilenet_pool.txt)

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

Reviewed By: eellison

Differential Revision: D27796688

Pulled By: Krovatkin

fbshipit-source-id: fc34d76358ce899e3b1f2b69efb9b5c38f5af1ad
2021-04-16 20:58:17 -07:00
Nikolay Korovaiko
9d3d169d2d Implement hardswish/hardsigmoid on MKLDNN tensors (#55218)
Summary:
Adding hardwish and hardsigmoid improves mobilenetv3 by ~13%

  | hardswish | base |
-- | -- | -- | --
run 1 | 1305.032 | 1486.013 |
run 2 | 1290.142 | 1491.001 |
run 3 | 1305.51 | 1491.66 |
run 4 | 1308.788 | 1495.577 |
avg | 1302.368 | 1491.063 | 0.873449

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

Reviewed By: albanD

Differential Revision: D27701276

Pulled By: Krovatkin

fbshipit-source-id: cde78da71d327e65461e80fbb6c3bb3429505410
2021-04-15 08:51:30 -07:00
Bin Bao
a0d9776104 [JIT] Include conv3d in the conv-add-relu fusion (#54772)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54772

conv3d-add-relu fusion does not work on some platforms when TF32 is enabled, so set allow_tf32 to false.

Test Plan:
```
python test/test_jit.py -k test_freeze_conv_relu_fusion
```

Imported from OSS

Reviewed By: bertmaher

Differential Revision: D27435560

fbshipit-source-id: e35e2297dce85acfbe988deea97c3f5e68f1e1c7
2021-04-06 12:08:13 -07:00
Elias Ellison
63997db6ec [JIT] fix freezing with mkldnn tensors (#54632)
Summary:
We were accessing their storage which will throw

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

Reviewed By: ezyang

Differential Revision: D27372192

Pulled By: eellison

fbshipit-source-id: 9985e85af7a35a3d6bf1c0be0185699c34877b94
2021-03-29 10:27:33 -07:00
Elias Ellison
9be4c75fa0 [JIT] Add Reinplacing to MKLDNN Subgraphs (#53908)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53908

This adds reinplacing to MKLDNN Subgraphs so that we replace `aten::add` with `aten::add_`. Normally you would have to prove device and dtype, but we know that already, and because we have explicit broadcast nodes for other reasons we dont have to prove that the output shape of add is the same as inputs.

Ive tested correctness on resnet, I'm going to do more extensive testing as well. When I benchmarked the "unsafe" version (always inplace) I saw average speedups of ~16% for both Single threaded and Multithreaded. I dont think the "safe" version will be far beyond; when I looked at resnet for example every `add` and `relu` were reinplaced.

Theres some question of reusing other alias / liveness / inplacing passes in SR. I thought about it, however I didnt want to add a cross-dependency between very different parts of the code base with a bunch of different assumptions. The logic here is also covering a simpler case and does not add much complexity IMO.

Test Plan: Imported from OSS

Reviewed By: Krovatkin

Differential Revision: D27132969

Pulled By: eellison

fbshipit-source-id: 121a38daaedf01363f6b66a814beaaa72a0ab0dc
2021-03-22 19:21:03 -07:00
Elias Ellison
81c6e5fb38 use reshape when possible in broadcasting (#53326)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/53326

Test Plan: Imported from OSS

Reviewed By: Krovatkin

Differential Revision: D26897275

Pulled By: eellison

fbshipit-source-id: 44278633a1e6429db43443ca689b97d5a077a15c
2021-03-22 19:20:59 -07:00
Elias Ellison
18c04a3f0f Avoid dispatch overhead in call to mkldnn convolution (#52614)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52614

This can speed up models by 5% (~.5-1% from the base, but ~5% after they've been sped up with mkldnn).

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D26696693

Pulled By: eellison

fbshipit-source-id: bfed55242524a4c2f1ae5d63e76d6803016d986d
2021-03-22 18:38:39 -07:00
Bin Bao
4626886f21 [JIT] Add CUDNN Conv-Add-Relu fusion for Frozen Model Optimization (#52102)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52102

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D26646100

fbshipit-source-id: 7f7a82cc0b42c958b9e0c854b3b5dc6ea7cfff6c
2021-03-18 15:18:52 -07:00
Meghan Lele
60ed8fb244 [JIT] Enable ModuleList non-literal indexing (#53410)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53410

**Summary**
This commit enables indexing into `ModuleList` using a non-literal
index if the LHS of the assignment statement of which the indexing is
the RHS is annotated with an interface type.

This feature already exists for `ModuleDict`, and this commit builds on
top of that implementation. A `prim::ModuleContainerIndex` operator is
emitted for any statement of the form `lhs: InterfaceType =
module_container[idx]`. The same operator has to be used for both
`ModuleDict` and `ModuleList` because serialization does not preserve
the metadata that indicates whether a `Module` is a `ModuleDict` or
`ModuleList`.

**Testing**
This commit extends the existing unit tests for non-literal `ModuleDict`
indexing to test non-literal `ModuleList` indexing.

**Fixes**
This commit fixes #47496.

Test Plan: Imported from OSS

Reviewed By: gmagogsfm

Differential Revision: D26857597

Pulled By: SplitInfinity

fbshipit-source-id: d56678700a264d79aae3de37ad6b08b080175f7c
2021-03-09 16:11:34 -08:00
Jacob Szwejbka
a947bfaa26 [Pytorch] Remove assumption forward exists in freeze_module (#52918)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52918

Freeze_module seems to operate under the assumption that forward always exists. This isnt true, so the change first checks for existence then retrieves the function.
ghstack-source-id: 123215242

Test Plan: Try freezing something with and without forward.

Reviewed By: dhruvbird

Differential Revision: D26671815

fbshipit-source-id: d4140dad3c59d3d20012143175f9b9268bf23050
2021-03-08 18:29:44 -08:00
Elias Ellison
506f756a0a Include max pool in fusion groups (#52613)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52613

Including MaxPool as part of the MKLDNN fusion group sped up resnet18 by ~20%, and was a win on other models I tested as well. I will post more complete benchmarks.

As mentioned in the diff, in some cases MaxPool can be slower than aten - ideally we'd only include maxpool if it decreased the number of layout transformations that occur. That hasnt actually matttered for all of the torchvision models, I don't think its necessary for this PR.

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D26696704

Pulled By: eellison

fbshipit-source-id: 61a025dbf5e7591c0a0f75def3beb439a138a21e
2021-03-01 21:22:46 -08:00
Elias Ellison
b1284cfbfb Only functionalize ops which we want to include in mkldnn group (#51924)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/51924

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D26696705

Pulled By: eellison

fbshipit-source-id: df2a780f6316d66f0d6ae99bbb54d044947195e5
2021-03-01 21:22:36 -08:00
Elias Ellison
32fed3f375 Handle mkldnn broadcasting in mkldnn fuser (#51736)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/51736

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D26696694

Pulled By: eellison

fbshipit-source-id: 473cc64c8d9f775e9d06340437aff2eb6c0619b9
2021-03-01 21:22:23 -08:00
Elias Ellison
a2f7e929ef Add MKLDNN fuser (#51600)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51600

Looking for notes on implementation first, will post more notes on benchmarks and overall thoughts/implementation and solicit more input soon.

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D26696702

Pulled By: eellison

fbshipit-source-id: cd612f093fe3859e42fb0b77560ebd1b44fccff7
2021-03-01 21:22:19 -08:00
Elias Ellison
4b40141d2c Add support for linear in mkldnn fusion (#51484)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51484

This PR moves the linear weights of a frozen model to MKLDNN. When the weights are already in MKLDNN, just computing a single linear by converting the input and output from/to mkldnn provides large speedups. I benchmark'd the results of the top 200 shapes in predictor [here](https://www.internalfb.com/phabricator/paste/view/P171537854) (taken from aten::matmul), as well as verified that it sped up popular models. .

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D26696698

Pulled By: eellison

fbshipit-source-id: 53d03b9e6956e11b700ee58214e2266e2aa4106a
2021-03-01 21:22:13 -08:00
Elias Ellison
bfae3789ba Move conv to mkldnn (#51483)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51483

This PR moves the conv weights of a frozen model to MKLDNN, and AOT reorders the weights. When the weights are already in MKLDNN, just computing a single conv by converting the input and output from/to mkldnn provides large speedups. I benchmark'd the results of the top 200 shapes in predictor [here](https://www.internalfb.com/phabricator/paste/view/P171537938), as well as verified that it sped up popular models in torchvision.

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D26696703

Pulled By: eellison

fbshipit-source-id: 0b4441bee4f6e0890a4540fbca3bb5e58b8c5adf
2021-03-01 21:19:27 -08:00
Elias Ellison
e1d927e552 [JIT] Update freezing api (#52337)
Summary:
Update freezing api  for 1.8,  and add a corresponding C++ API. The `optimize` flag hasn't been publicly released yet, so we are able to change it without breaking BC. I will submit a PR to branch release as well, there are a few more days to do that

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

Reviewed By: ejguan

Differential Revision: D26491833

Pulled By: eellison

fbshipit-source-id: 6dcd74eb8f76db64ac53183d03dabdd0f101f4b5
2021-02-18 00:17:27 -08:00
Bin Bao
0dd1d60d54 [JIT] Remove Dropout during Frozon Optimization (#51589)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51589

Dropout operators are only needed in training. Remove them for frozen models.

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D26214259

fbshipit-source-id: 3ab05869e1e1f6c57498ba62bf40944f7c2189aa
2021-02-08 08:38:08 -08:00
Meghan Lele
8f5ad00e13 [JIT] Print out CU address in ClassType::repr_str() (#50194)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50194

**Summary**
`ClassType::repr_str()` prints out only the name of a `ClassType`, which
is not always enough to disambiguate it. In some situations, two
`ClassTypes` are compared and do not match despite having identical
names because they are in separate compilation units. In such cases, the
error message can seem nonsensical (e.g. `expected type T but found type
T`). This commit modifies `ClassType::repr_str()` so that it prints out
the address of the type's compilation unit to make these messages less
puzzling (e.g. `expected type T (0x239023) but found type T (0x230223)`).

**Test Plan**
This commit adds a unit test, `ClassTypeTest.IdenticalTypesDifferentCus`
that reproduces this situation.

**Fixes**
This commit fixes #46212.

Test Plan: Imported from OSS

Reviewed By: tugsbayasgalan

Differential Revision: D25933082

Pulled By: SplitInfinity

fbshipit-source-id: ec71b6728be816edd6a9c2b2d5075ead98d8bc88
2021-01-19 23:04:30 -08:00
Elias Ellison
a389b30bfc Add Post Freezing Optimizations, turn on by default in torch.jit.freeze (#50222)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50222

This PR adds a pass which runs a set of optimizations to be done after freezing. Currently this encompasses Conv-BN folding, Conv->Add/Sub/Mul/Div folding and i'm also planning on adding dropout removal.

I would like some feedback on the API. torch.jit.freeze is technically in \~prototype\~ phase so we have some leeway around making changes. I think in the majority of cases, the user is going to want to freeze their model, and then run in inference. I would prefer if the optimization was opt-out instead of opt-in. All internal/framework use cases of freezing all use `freeze_module`, not the python API, so this shouldn't break anything.

I have separated out the optimization pass as a separate API to make things potentially modular, even though I suspect that is an unlikely case. In a future PR i would like to add a `torch::jit::freeze` which follows the same api as `torch.jit.freeze` intended for C++ use, and runs the optimizations.

Test Plan: Imported from OSS

Reviewed By: tugsbayasgalan

Differential Revision: D25856264

Pulled By: eellison

fbshipit-source-id: 56be1f12cfc459b4c4421d4dfdedff8b9ac77112
2021-01-12 11:39:13 -08:00
Elias Ellison
30aeed7c2b Peephole Optimize out conv(x).dim(), which prevents BN fusion (#50221)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/50221

Test Plan: Imported from OSS

Reviewed By: tugsbayasgalan

Differential Revision: D25856266

Pulled By: eellison

fbshipit-source-id: ef7054b3d4ebc59a0dd129116d29273be33fe12c
2021-01-12 11:39:09 -08:00
Elias Ellison
6971149326 [JIT] Add Frozen Conv-> Add/Sub/Mul/Div fusion (#50075)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50075

Adds Conv - Add/Sub/Mul/Div fusion for frozen models. This helps cover models like torchvision maskrcnn, which use a hand-rolled batchnorm implementation: 90645ccd0e/torchvision/ops/misc.py (L45).

I haven't tested results yet but I would expect a somewhat similar speed up as conv-bn fusion (maybe a little less).

Test Plan: Imported from OSS

Reviewed By: tugsbayasgalan

Differential Revision: D25856265

Pulled By: eellison

fbshipit-source-id: 2c36fb831a841936fe4446ed440185f59110bf68
2021-01-12 11:39:02 -08:00
Elias Ellison
035229c945 [JIT] Frozen Graph Conv-BN fusion (#50074)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50074

Adds Conv-BN fusion for models that have been frozen. I haven't explicitly tested perf yet but it should be equivalent to the results from Chillee's PR [here](https://github.com/pytorch/pytorch/pull/476570) and [here](https://github.com/pytorch/pytorch/pull/47657#issuecomment-725752765). Click on the PR for details but it's a good speed up.

 In a later PR in the stack I plan on making this optimization on by default as part of `torch.jit.freeze`. I will also in a later PR add a peephole so that there is not conv->batchnorm2d doesn't generate a conditional checking # dims.

Zino was working on freezing and left the team, so not really sure who should be reviewing this, but I dont care too much so long as I get a review �

Test Plan: Imported from OSS

Reviewed By: tugsbayasgalan

Differential Revision: D25856261

Pulled By: eellison

fbshipit-source-id: da58c4ad97506a09a5c3a15e41aa92bdd7e9a197
2021-01-12 11:37:32 -08:00
Elias Ellison
fc559bd6dc [JIT] Constant prop getattr (#49806)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49806

Fix for https://github.com/pytorch/pytorch/issues/47089

Test Plan: Imported from OSS

Reviewed By: navahgar

Differential Revision: D25696791

Pulled By: eellison

fbshipit-source-id: 914c17b8effef7f4f341775ac2b8150ee4703efd
2020-12-28 10:44:53 -08:00
Nikita Shulga
57145c910f Revert D24711613: [pytorch][PR] Preserve submodule with __set_state__ in freezing
Test Plan: revert-hammer

Differential Revision:
D24711613 (a3e1bd1fb9)

Original commit changeset: 22e51417454a

fbshipit-source-id: c2090b15fdba2d6c9dc1fbd987d32229dd898608
2020-12-10 16:26:38 -08:00
Zino Benaissa
a3e1bd1fb9 Preserve submodule with __set_state__ in freezing (#47308)
Summary:
This PR does the following:

-  fail freezing if input module has __set_state__ method
-  preserves attributes of  submodules with __set_state__ method.

Fixes #{issue number}

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

Reviewed By: eellison

Differential Revision: D24711613

Pulled By: bzinodev

fbshipit-source-id: 22e51417454aaf85cc0ae4acb2dc7fc822f149a2
2020-12-10 13:36:34 -08:00
Zino Benaissa
11710598db Preserve module parameters in freezing (#47094)
Summary:
Added preserveParameters to freezing API that allows to preserve module
parameters.

Fixes #{39613}

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

Reviewed By: eellison

Differential Revision: D24792867

Pulled By: bzinodev

fbshipit-source-id: f0cd980f5aed617b778afe2f231067c7c30a1527
2020-11-13 20:18:32 -08:00
Meghan Lele
19ede75eb9 [JIT] Enable ModuleDict non-literal indexing (#45716)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/45716

**Summary**
This commit enables indexing into `ModuleDict` using a non-literal
index if the `ModuleDict` is annotated with `Dict[str, X]`, where `X` is
a module interface type. These annotations must be expressed using a
class attribute named `__annotations__`, which is a `Dict[str, Type]`
where the keys are the names of module attributes and the values are
their types.

The approach taken by this commit is that these annotations are stored
as "hints" along with the corresponding module attributes in the
`ConcreteSubmoduleTypeBuilder` instance for each module (which might be
a `ModuleDict`). These hints are passed into the `ModuleValue` that is
created for desugaring operations on submodules so that indexing into a
`ModuleDict` can be emitted as a getitem op into a dict emitted into the
graph that represents the `ModuleDict`.

**Test Plan**
This commit adds unit tests to `TestModuleContainers` to test this
feature (`test_typed_module_dict`).

Differential Revision: D24070606

Test Plan: Imported from OSS

Reviewed By: ansley

Pulled By: SplitInfinity

fbshipit-source-id: 6019a7242d53d68fbfc1aa5a49df6cfc0507b992
2020-10-31 21:36:23 -07:00
Brian Hirsh
00c779a92b detect inplace modifications of views earlier (fix #21875) (#46204)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/46204

Test Plan: Imported from OSS

Reviewed By: izdeby

Differential Revision: D24259500

Pulled By: bdhirsh

fbshipit-source-id: 223f8a07da4e4121009fc0a8b6760d90eef089b3
2020-10-19 08:58:33 -07:00
Elias Ellison
908c23579d [JIT] Revert Freezing shared type PR (#46285)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/45902 by reverting https://github.com/pytorch/pytorch/pull/42457

The test case introduced by https://github.com/pytorch/pytorch/pull/42457 was fixed by https://github.com/pytorch/pytorch/pull/46250, which I'm assuming is the real source of the bug.

In the future it would be good to provide repro's for freezing issues without including a quantization dependency; there was another another issue in freezing (see: https://github.com/pytorch/pytorch/pull/46054) who's root cause was the same quantization issue https://github.com/pytorch/pytorch/pull/46250.

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

Reviewed By: bdhirsh

Differential Revision: D24288739

Pulled By: eellison

fbshipit-source-id: b69ee8c713f749cd93d5eba370c3eafed86568bb
2020-10-15 10:57:30 -07:00
James Reed
be45c3401a [JIT] Make objects throw Python AttributeError on nonexistant attr access (#45911)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/45911

Test Plan: Imported from OSS

Reviewed By: robieta

Differential Revision: D24140971

Pulled By: jamesr66a

fbshipit-source-id: 046a2cffff898efad5bcc36a41bf992f36f555f9
2020-10-07 01:57:29 -07:00
Zino Benaissa
13f76f2be4 Fix preserve submodule attribute in freezing (#45143)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/45143

This PR prevents freezing cleaning up a submodule when user requests to
preserve a submodule.

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D23844969

Pulled By: bzinodev

fbshipit-source-id: 80e6db3fc12460d62e634ea0336ae2a3551c2151
2020-09-28 00:05:38 -07:00
Zino Benaissa
4d80c8c648 Fix inlining interface call in fork subgraph (#43790)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43790

Interface calls were not handled properly when they are used in fork
subgraph. This PR fixes this issue.

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D23402039

Pulled By: bzinodev

fbshipit-source-id: 41adc5ee7d942250e732e243ab30e356d78d9bf7
2020-09-23 11:17:19 -07:00
Zino Benaissa
abe878ce96 Allow Freezing of Module containing interface attribute (#41860)
Summary:
This patch allows to freeze model that utilizes interfaces. Freezing works
under the user assumption that the interfase module dones not aliases with
any value used in the model.

To enable freezing of such modules, added an extra pramater:

torch._C._freeze_module(module, ignoreInterfaces = True)

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

Reviewed By: eellison

Differential Revision: D22670566

Pulled By: bzinodev

fbshipit-source-id: 41197a724bc2dca2e8495a0924c224dc569f62a4
2020-08-21 18:57:13 -07:00
Kimish Patel
472f291375 Fix freeze_module pass for sharedtype (#42457)
Summary:
During cleanup phase, calling recordReferencedAttrs would record
the attributes which are referenced and hence kept.
However, if you have two instances of the same type which are preserved
through freezing process, as the added testcase shows, then during
recording the attributes which are referenced, we iterate through the
type INSTANCES that we have seen so far and record those ones.
Thus if we have another instance of the same type, we will just look at
the first instance in the list, and record that instances.
This PR fixes that by traversing the getattr chains and getting the
actual instance of the getattr output.

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

Test Plan:
python test/test_jit.py TestFreezing
Fixes #{issue number}

Reviewed By: gchanan

Differential Revision: D23106921

Pulled By: kimishpatel

fbshipit-source-id: ffff52876938f8a1fedc69b8b24a3872ea66103b
2020-08-17 08:27:31 -07:00
Richard Zou
3d3752d716 Revert D22898051: [pytorch][PR] Fix freeze_module pass for sharedtype
Test Plan: revert-hammer

Differential Revision:
D22898051 (4665f3fc8d)

Original commit changeset: 8b1d80f0eb40

fbshipit-source-id: 4dc0ba274282a157509db16df13269eed6cd5be9
2020-08-12 10:28:03 -07:00
Kimish Patel
4665f3fc8d Fix freeze_module pass for sharedtype (#42457)
Summary:
During cleanup phase, calling recordReferencedAttrs would record
the attributes which are referenced and hence kept.
However, if you have two instances of the same type which are preserved
through freezing process, as the added testcase shows, then during
recording the attributes which are referenced, we iterate through the
type INSTANCES that we have seen so far and record those ones.
Thus if we have another instance of the same type, we will just look at
the first instance in the list, and record that instances.
This PR fixes that by traversing the getattr chains and getting the
actual instance of the getattr output.

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

Test Plan:
python test/test_jit.py TestFreezing
Fixes #{issue number}

Reviewed By: zou3519

Differential Revision: D22898051

Pulled By: kimishpatel

fbshipit-source-id: 8b1d80f0eb40ab99244f931d4a1fdb28290a4683
2020-08-12 08:35:05 -07:00
Elias Ellison
5ff54ff4ff import freeze (#42319)
Summary:
torch.jit.freeze was broken with https://github.com/pytorch/pytorch/pull/41154/files#diff-9084cd464651f7fa1ff030d2edd9eb55R1

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

Reviewed By: ZolotukhinM

Differential Revision: D22845476

Pulled By: eellison

fbshipit-source-id: bc9e50678d0e0ffca4062854ccc71bbef2e1a97b
2020-07-30 13:00:11 -07:00
Zino Benaissa
690946c49d Generalize constant_table from tensor only to ivalue (#40718)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/40718

Currently only constant except tensor must be inlined during serialization.
Tensor are stored in the contant table. This patch generalizes this capability
to any IValue. This is particularly useful for non ASCII string literal that
cannot be inlined.

Test Plan: Imported from OSS

Differential Revision: D22298169

Pulled By: bzinodev

fbshipit-source-id: 88cc59af9cc45e426ca8002175593b9e431f4bac
2020-07-09 09:09:40 -07:00
Zino Benaissa
9111ae7782 Preserve user specified attributes and methods (#38830)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/38830

This patch enables to preserve user specified attributes or non forward
methods. The API:
  _freeze_module(Module, ["a", "version"])

Test Plan: Imported from OSS

Differential Revision: D21957316

Pulled By: bzinodev

fbshipit-source-id: 5c9146ae679791070a9de868c45785725b48a9e6
2020-06-10 01:38:18 -07:00
Zino Benaissa
1b99be9088 Freezing Module containing fork subgraphs (#37044)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/37044

This patch applies freezing to module containing fork subgraphs.

Test Plan: Imported from OSS

Differential Revision: D21956334

Pulled By: bzinodev

fbshipit-source-id: ec272ddea1ed588c35d8ffa4ea9b532d5336667f
2020-06-09 19:10:38 -07:00
Sebastian Messmer
e4657fe194 Revert D21579607: [pytorch][PR] Do not call optimizations within freezing API
Test Plan: revert-hammer

Differential Revision:
D21579607

Original commit changeset: a6231754fea8

fbshipit-source-id: 277011605eedee1c3b44fbaf877233b239adf56b
2020-06-03 14:50:45 -07:00
Zino Benaissa
5cfd1a190e Do not call optimizations within freezing API (#38499)
Summary:
This patch removes call to run optimizations within freezing API.
Only dead code elimination is invoked to clean up the frozen module.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/38499

Reviewed By: eellison

Differential Revision: D21579607

Pulled By: bzinodev

fbshipit-source-id: a6231754fea89296a3dcf07b5e37a1c43cb8d5dd
2020-06-03 13:25:24 -07:00
Jerry Zhang
ff9a809ccd [quant][graphmode][refactor] Remove unused code in quantization.cpp (#37974)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/37974

Differential Revision: D21468498

Pulled By: jerryzh168

fbshipit-source-id: 96f34db9f98474ec8e5d33e9b7c406b1637f5de8
2020-05-08 11:03:03 -07:00
Elias Ellison
cde1350a5d Add support for generic list constants (#36953)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/36953

Add support for generic lists as a constant. generic dicts & tuples are already implemented. This is a pretty common pattern and cuts down on the number of non-tensor nodes executed in interpolate tests.

Test Plan: Imported from OSS

Differential Revision: D21160761

Pulled By: eellison

fbshipit-source-id: 1e6b7b25b7580f09067794772d44e615601c60c4
2020-04-28 23:28:07 -07:00
Zino Benaissa
6c742af235 Remove attributes and method of submodules in frozen module (#34787)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/34787

This is a follow up patch of freezing of TorchScript modules. This patch
enables removal of constant attributes and unused method in submodules.
The clean up logic is generalized to handle  attributes that share their class
type.

Test Plan: Imported from OSS

Differential Revision: D21004990

Pulled By: bzinodev

fbshipit-source-id: 84778aa9ae1a96d23db29c051031f9995ed3ac90
2020-04-14 12:07:12 -07:00
Zino Benaissa
c321f02756 Follow up on freezing (#34786)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/34786

1) Rename 'HashIValue' to 'HashAliasedIValue'
2) Added Object case in getSubValues function
3) Hashes tensors to their storage
4) Added Dict case in orverrideGradient
5) nit clean up

Test Plan: Imported from OSS

Differential Revision: D20585270

Pulled By: bzinodev

fbshipit-source-id: f580f3cb80dd5623088a014efd5f0f5ccc1659c0
2020-03-23 17:25:40 -07:00
Nikolay Korovaiko
0a4a558c2c Dictionary Constants (#32869)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/32869

Differential Revision: D19909339

Pulled By: Krovatkin

fbshipit-source-id: 6fe2a9b470768f84b957c69cdf9af3a1bd9b1ca9
2020-03-09 16:12:36 -07:00
Zino Benaissa
cab8772c6c Freezing Torchscript modules (#32178)
Summary:
This patch enables folding GetAttr nodes with their corresponding
values. _jit_pass_freeze_module API returns a new TorchScipt module
where all function calls and get attributes are inlined.
Usage:

frozen_model = torch._C._freeze_module(scrited_model._c)
frozen_model.forward(...)

This API currently optimizes the forward method. We will follow up to
to preserve and optimize methods and attributes that are annotated as
 torch.jit.interface.

Several future improvements to JIT optimizations are required to maximize
clean up/de-sugar the graph and eliminate redundancies.
Ideally, we want to produce a graph that can easily be lowered to
GLOW and other low-level backends.
__
Pull Request resolved: https://github.com/pytorch/pytorch/pull/32178

Differential Revision: D19419640

Pulled By: bzinodev

fbshipit-source-id: 52baffaba9bca2cd60a8e747baa68d57711ad42b
2020-03-02 11:38:36 -08:00