Commit Graph

829 Commits

Author SHA1 Message Date
Vasiliy Kuznetsov
db6cd42434 fx quant: clean up nit in insert_observer (#57367)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57367

This code is never hit (see insert_observer_for_output_of_the_node
which gates it out), so changing to an assert in order to
have `insert_observer` actually always insert an observer.
This helps code readability.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D28126898

fbshipit-source-id: 411bc37769a6eacbebc463ed6c84cac85871bd5e
2021-05-02 20:12:10 -07:00
Vasiliy Kuznetsov
bb640efa40 ns for fx: add missing add_relu and mul_relu patterns (#56927)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56927

Adds the connection of `torch.add` to `toq.add_relu` and of `torch.mul`
to `toq.mul_relu`.

Test Plan:
CI

Imported from OSS

Reviewed By: supriyar

Differential Revision: D28003475

fbshipit-source-id: a12871feacf84c5afb0e1cc47e708e285695ffeb
2021-05-02 08:34:49 -07:00
Jerry Zhang
ecacb8c78b [quant][graphmode][fx] Fix getitem for unmatched nodes (#57173)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57173

If getitem is followed by an unmatched node, we'll remove the observer after it.

Test Plan:
python test/test_quantization.pyt TestQuantizeFxOps.test_getitem

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D28068805

fbshipit-source-id: e79f8ec3e8fd61d348b8a7069ab0bb434d737c30
2021-04-29 10:16:44 -07:00
Vasiliy Kuznetsov
9fe2673d1c ns for fx: additional bugfix for user defined functions (#57028)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57028

Adds a test case for wrapped sigmoid, and fixes the following issues
to make it pass in NS:
* allows comparing between x.sigmoid() and torch.sigmoid(x), if they are related
* allows dtype cast from FP32_OR_INT8 to FP32, via dequantize (this will be improved later)

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_defined_function
```

Reviewed By: jerryzh168

Differential Revision: D28030089

Pulled By: vkuzo

fbshipit-source-id: b237353e2d564a4476f409df461746a259015a4b
2021-04-27 16:29:03 -07:00
Vasiliy Kuznetsov
da2cef6a40 ns for fx: allow comparing int8 to int8 for functionals (#57027)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57027

Fixes a bug to allow shadowing of linear and conv functionals.
The bug is to only detach tensors, not all objects.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_int8_shadows_int8_fun
```

Reviewed By: jerryzh168

Differential Revision: D28030090

Pulled By: vkuzo

fbshipit-source-id: 0a38c4b232e007d7822eee818b0af99d98335d22
2021-04-27 16:29:01 -07:00
Vasiliy Kuznetsov
a359cfac22 ns for fx: add option to skip matching classes and functions (#57026)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57026

Adds a config option to skip matching classes by class type
and functions by function type.

This is useful when users make custom modules which return
types other than tensors. With the current implementation of
Logger, these are not scriptable.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_module_scriptable
```

Reviewed By: jerryzh168

Differential Revision: D28030093

Pulled By: vkuzo

fbshipit-source-id: 71dc54dd935d2071c4b017260ea2a1e5c2298bfe
2021-04-27 16:29:00 -07:00
Vasiliy Kuznetsov
e8a5490c0a ns for fx: support binary ops when adding unshadowed loggers for inputs (#57025)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57025

Adds the ability to log unshadowed inputs of binary ops such as `add`
and `mul`, when indices 0, 1, or 0 and 1 are tensors.

Note: making shadowing support this is saved for a future PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_add_mul_inputs_activations
```

Reviewed By: jerryzh168

Differential Revision: D28030098

Pulled By: vkuzo

fbshipit-source-id: fd46760faac153975cd7688e70c44991ec1d5dff
2021-04-27 16:28:58 -07:00
Vasiliy Kuznetsov
ddedeab66d ns for fx: bug fix for shadowing fp16 emulation patterns (#57024)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57024

Enables shadow copies of fp16 emulation patterns where weights
are cast to fp16 before being passed to linear.  This previously
did not work because copying of `call_method` nodes was not implemented.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_linear_fp16_vs_linear_fp16_shadow_activations
```

Reviewed By: jerryzh168

Differential Revision: D28030096

Pulled By: vkuzo

fbshipit-source-id: 13a39ea6c106180df6d750246672286b58b4d04c
2021-04-27 16:28:56 -07:00
Vasiliy Kuznetsov
2acc19eca1 ns for fx: add fp16 function shadowing (#57023)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57023

Adds functionality for shadowing user functions with fp16 I/O dtype.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_defined_function
```

Reviewed By: jerryzh168

Differential Revision: D28030092

Pulled By: vkuzo

fbshipit-source-id: 642792398a76bd62593fa439ab14901e9dbdf4f8
2021-04-27 16:28:54 -07:00
Vasiliy Kuznetsov
782a0a1469 ns for fx: allow user functions in shadowing (#57022)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57022

Allows usage of user functions in NS shadow APIs. We expose the
i/o mapping to the user APIs, and thread them throughout the code.

Note: the format of the mapping is currently not the best.  Saving
improving that for a future PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_defined_function
```

Reviewed By: jerryzh168

Differential Revision: D28030095

Pulled By: vkuzo

fbshipit-source-id: 2863312362223ad276437e2aeeec4a3f71b691c7
2021-04-27 16:28:53 -07:00
Vasiliy Kuznetsov
c4bec76bec ns for fx: move node I/O dtype mapping to be local instead of global (#57021)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57021

To support shadows of custom functions, we need to allow user to
specify I/O type of the custom functions.

This PR is a cleanup in preparation for making the above happen.
We make the I/O dtype mappings be generated by a function instead
of a global variable. In the next PR, we will add a hook so user
can modify these mappings.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Reviewed By: jerryzh168

Differential Revision: D28030094

Pulled By: vkuzo

fbshipit-source-id: 3cbb617f034ef385c2875c4ec7fed13ca30bfc57
2021-04-27 16:27:40 -07:00
Mike Ruberry
1145e2c6e2 Revert D27831996: ns for fx: move node I/O dtype mapping to be local instead of global
Test Plan: revert-hammer

Differential Revision:
D27831996 (93de80203d)

Original commit changeset: 782f5e77de0e

fbshipit-source-id: 6637ef4e8ba76fc4f2b3836ad1ed8d37ce040576
2021-04-27 01:01:08 -07:00
Mike Ruberry
45e96b5410 Revert D27833189: ns for fx: allow user functions in shadowing
Test Plan: revert-hammer

Differential Revision:
D27833189 (1917350977)

Original commit changeset: dac418e294d1

fbshipit-source-id: c6f58dac1a35806ea7d1dfb993d67e698196dee1
2021-04-27 01:01:06 -07:00
Mike Ruberry
982c72ac33 Revert D27836064: ns for fx: add fp16 function shadowing
Test Plan: revert-hammer

Differential Revision:
D27836064 (96a9eafcfb)

Original commit changeset: 37a434a04e2b

fbshipit-source-id: e85088f5e301e14a0fc9ac1f7241c2baaf0a957e
2021-04-27 01:01:04 -07:00
Mike Ruberry
90d554bd86 Revert D27857735: ns for fx: bug fix for shadowing fp16 emulation patterns
Test Plan: revert-hammer

Differential Revision:
D27857735 (f35540be38)

Original commit changeset: 7c1a067f035a

fbshipit-source-id: 6816223975b2e7b1f395e8894d17e3358fdb50ed
2021-04-27 01:01:02 -07:00
Mike Ruberry
abb8b6c1c1 Revert D27864296: ns for fx: support binary ops when adding unshadowed loggers for inputs
Test Plan: revert-hammer

Differential Revision:
D27864296 (c004346c88)

Original commit changeset: 3cbeb728297a

fbshipit-source-id: bc87cb707b14a0965452e9a1aa0d4e37ffbe5bf1
2021-04-27 01:01:01 -07:00
Mike Ruberry
cc8c5c1447 Revert D27886107: ns for fx: add option to skip matching classes and functions
Test Plan: revert-hammer

Differential Revision:
D27886107 (92c7aec5f5)

Original commit changeset: ec92c4f7ab71

fbshipit-source-id: 87d3b91c3d601f1706b61a2b2ce287a7b44f3d81
2021-04-27 01:00:59 -07:00
Mike Ruberry
5dc7a6b050 Revert D27960767: ns for fx: allow comparing int8 to int8 for functionals
Test Plan: revert-hammer

Differential Revision:
D27960767 (502c58ad84)

Original commit changeset: abc911ca4b9e

fbshipit-source-id: 9bb1aa9d0e764bfd2dd6745af897d958c054ef3a
2021-04-27 01:00:57 -07:00
Mike Ruberry
5db03b4109 Revert D27960766: ns for fx: additional bugfix for user defined functions
Test Plan: revert-hammer

Differential Revision:
D27960766 (9bd14da6e4)

Original commit changeset: 02935d2f400a

fbshipit-source-id: e7026c8637a591b6ffef288da8ef6306cdb9eb95
2021-04-27 00:59:57 -07:00
Vasiliy Kuznetsov
9bd14da6e4 ns for fx: additional bugfix for user defined functions (#56762)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56762

Adds a test case for wrapped sigmoid, and fixes the following issues
to make it pass in NS:
* allows comparing between x.sigmoid() and torch.sigmoid(x), if they are related
* allows dtype cast from FP32_OR_INT8 to FP32, via dequantize (this will be improved later)

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_defined_function
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27960766

fbshipit-source-id: 02935d2f400aa0b8f3d51bbf664a6c8ca89aa811
2021-04-26 17:03:32 -07:00
Vasiliy Kuznetsov
502c58ad84 ns for fx: allow comparing int8 to int8 for functionals (#56742)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56742

Fixes a bug to allow shadowing of linear and conv functionals.
The bug is to only detach tensors, not all objects.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_int8_shadows_int8_fun
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27960767

fbshipit-source-id: abc911ca4b9edafd1effb9dada7731981538c2df
2021-04-26 17:03:30 -07:00
Vasiliy Kuznetsov
92c7aec5f5 ns for fx: add option to skip matching classes and functions (#56493)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56493

Adds a config option to skip matching classes by class type
and functions by function type.

This is useful when users make custom modules which return
types other than tensors. With the current implementation of
Logger, these are not scriptable.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_module_scriptable
```

needs more testing before land

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27886107

fbshipit-source-id: ec92c4f7ab7141021bc022f07b3b558b42bbb986
2021-04-26 17:03:28 -07:00
Vasiliy Kuznetsov
c004346c88 ns for fx: support binary ops when adding unshadowed loggers for inputs (#56408)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56408

Adds the ability to log unshadowed inputs of binary ops such as `add`
and `mul`, when indices 0, 1, or 0 and 1 are tensors.

Note: making shadowing support this is saved for a future PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_add_mul_inputs_activations
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27864296

fbshipit-source-id: 3cbeb728297aa192d1ea17e815299709fd9db056
2021-04-26 17:03:26 -07:00
Vasiliy Kuznetsov
f35540be38 ns for fx: bug fix for shadowing fp16 emulation patterns (#56384)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56384

Enables shadow copies of fp16 emulation patterns where weights
are cast to fp16 before being passed to linear.  This previously
did not work because copying of `call_method` nodes was not implemented.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_linear_fp16_vs_linear_fp16_shadow_activations
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27857735

fbshipit-source-id: 7c1a067f035acf7322175f8535876d0ead88a86a
2021-04-26 17:03:25 -07:00
Vasiliy Kuznetsov
96a9eafcfb ns for fx: add fp16 function shadowing (#56311)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56311

Adds functionality for shadowing user functions with fp16 I/O dtype.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_defined_function
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27836064

fbshipit-source-id: 37a434a04e2bd2593a892209bbae59f0f1f34319
2021-04-26 17:03:23 -07:00
Vasiliy Kuznetsov
1917350977 ns for fx: allow user functions in shadowing (#56301)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56301

Allows usage of user functions in NS shadow APIs. We expose the
i/o mapping to the user APIs, and thread them throughout the code.

Note: the format of the mapping is currently not the best.  Saving
improving that for a future PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_defined_function
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27833189

fbshipit-source-id: dac418e294d1c9b204efbf4071d5cc12a9e784c0
2021-04-26 17:03:21 -07:00
Vasiliy Kuznetsov
93de80203d ns for fx: move node I/O dtype mapping to be local instead of global (#56296)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56296

To support shadows of custom functions, we need to allow user to
specify I/O type of the custom functions.

This PR is a cleanup in preparation for making the above happen.
We make the I/O dtype mappings be generated by a function instead
of a global variable. In the next PR, we will add a hook so user
can modify these mappings.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27831996

fbshipit-source-id: 782f5e77de0eef3899b9b7def0fdabd8dcafef12
2021-04-26 17:03:19 -07:00
Vasiliy Kuznetsov
8dbf6ae8fa ns for fx: handling for user functions in weight and unshadowed act APIs (#56292)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56292

Adds hooks for specifying user defined functions to NS weight and
unshadowed activation APIs.

Adding it to shadowed activation APIs will be a bit more work, upcoming
in a separate PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_user_defined_function
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27830409

fbshipit-source-id: 6bbddc3062c0b3e412a3147244795319c0785a92
2021-04-26 17:03:18 -07:00
Vasiliy Kuznetsov
d405d41a7c ns for fx: enable user defined functions for graph matching (#56283)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56283

Exposes the `base_name_to_sets_of_related_ops` variable
to the graph matching API, so that users can add relationships
for custom functions. This is needed to enable full support of
external functions for custom backends.

The next PR will extend this to the NS APIs.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher.test_user_defined_function
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27830410

fbshipit-source-id: 8688cf697d388c52e3d18f108765edfca3c3d3aa
2021-04-26 17:02:11 -07:00
Joel Schlosser
febff45900 Support factory kwargs in torch.nn modules (#54508)
Summary:
Continuation of https://github.com/pytorch/pytorch/pull/53144

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

Reviewed By: albanD

Differential Revision: D27939544

Pulled By: jbschlosser

fbshipit-source-id: 4bf517e5f74f093e27ca38a85e732da65e44d805
2021-04-22 16:16:53 -07:00
Jerry Zhang
1719cb82f3 [quant][graphmode][fx] Support preserving attributes in deepcopy of observed/quantized graphmodule (#56550)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56550

Add support for preserving a list of attributes on observed/quantized GraphModule

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_deepcopy_preserve_attributes

Imported from OSS

Reviewed By: vkuzo, kazhang

Differential Revision: D27899317

fbshipit-source-id: ebf21334715e5ab764aaa27eed534cc0cdf9f2b5
2021-04-22 15:02:44 -07:00
Joel Schlosser
12b2bc94d7 Revert D27909732: [pytorch][PR] Support factory kwargs in torch.nn modules
Test Plan: revert-hammer

Differential Revision:
D27909732 (5a09def9b0)

Original commit changeset: d8684b2403ab

fbshipit-source-id: d00d69fae4fa4ed58d9e97e70b27a06a0dcb39e4
2021-04-21 13:44:03 -07:00
Joel Schlosser
5a09def9b0 Support factory kwargs in torch.nn modules (#54508)
Summary:
Continuation of https://github.com/pytorch/pytorch/pull/53144

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

Reviewed By: malfet

Differential Revision: D27909732

Pulled By: jbschlosser

fbshipit-source-id: d8684b2403ab7eb336371d118799146a2520bd76
2021-04-21 13:20:11 -07:00
Jerry Zhang
096089abcb [quant][graphmode][fx] Produce torch.cat instead of torch.ops.quantized.cat (#54924)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54924

Previously we are producing torch.ops.quantize.cat which takes inputs, dequantize them
and requantize with new qparams. This PR changes that to produce torch.cat directly, torch.cat
will assume all inputs are sharing the same qparam, and it will produce a quantized Tensor with
the same qparam as all inputs (because previous PR makes sure all inputs and output of cat are sharing
the same observer/fakequant instance).

Using torch.cat is expected to be more efficient since it does not introduce extra quant/dequant.

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_cat

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27416528

fbshipit-source-id: 896c280abec2903c29d597c655729666583ff0dd
2021-04-21 10:58:09 -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
Charles David Hernandez
6e1fc5cef8 [quant] added dq->op->q quantization patterns for GELU and softmax ops (#56004)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56004

added reference pattern support for GELU, softmax and bmm for int dtypes. For GELU and Softmax, this consisted of adding reference patterns to the default node handler for int dtypes. Note GELU and softmax patterns are not registered since they do not have a proper quantized kernel which means they would either add unnecessary dequant and quant ops to the network, or they would simply error. This can be circumvented with custom qconfig usage as in test_gelu_reference

bmm was added within binary ops along with some significant changes to how that code is structured. Theoretically the reference pattern used for bmm could be applied to other dtypes. This was not enabled because of issues relating to Line 1323 in quantize.py. In essence, the prepare step does not know whether an op will use a reference pattern or not, so for ops that are supported with one dtype in reference and one dtype normally, this has the potential to cause issues. This is difficult to get aorund with the is_reference flag being available in the prepare step or discussed changes around separating

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_gelu_reference
python test/test_quantization.py TestQuantizeFxOps.ttest_gelu_normal
python test/test_quantization.py TestQuantizeFxOps.test_softmax_reference
python test/test_quantization.py TestQuantizeFxOps.test_softmax_normal
python test/test_quantization.py TestQuantizeFxOps.test_silu_reference
python test/test_quantization.py TestQuantizeFxOps.test_bmm_int_reference
python test/test_quantization.py TestQuantizeFxOps
python test/test_quantization.py TestFuseFx
python test/test_quantization.py TestQuantizeFx
python test/test_quantization.py TestQuantizeFxModels

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D27818340

fbshipit-source-id: de65be0797035463cd2d1b0e4677d1a87f69143c
2021-04-20 13:26:15 -07:00
Jerry Zhang
94406f77f6 [quant][graphmode][fx] Add support for keeping output quantized for list and dict (#56391)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56391

Previously we only support keeping output quantized for tensor output, this PR adds support
for list and dict (values) as well

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27860327

fbshipit-source-id: e770160ced47a7173abff5505ec620bd2b1a0b01
2021-04-19 21:37:11 -07:00
Natalia Gimelshein
92d24e3060 Revert D27855386: [pytorch][PR] Support factory kwargs in torch.nn modules
Test Plan: revert-hammer

Differential Revision:
D27855386 (40483acc51)

Original commit changeset: dabd505d2a04

fbshipit-source-id: f5bf3120d87861b30a8e1bf11977ad7d27cd8500
2021-04-19 20:07:20 -07:00
Sam Estep
e3900d2ba5 Add lint for unqualified noqa (#56272)
Summary:
As this diff shows, currently there are a couple hundred instances of raw `noqa` in the codebase, which just ignore all errors on a given line. That isn't great, so this PR changes all existing instances of that antipattern to qualify the `noqa` with respect to a specific error code, and adds a lint to prevent more of this from happening in the future.

Interestingly, some of the examples the `noqa` lint catches are genuine attempts to qualify the `noqa` with a specific error code, such as these two:
```
test/jit/test_misc.py:27:            print(f"{hello + ' ' + test}, I'm a {test}") # noqa E999
test/jit/test_misc.py:28:            print(f"format blank") # noqa F541
```
However, those are still wrong because they are [missing a colon](https://flake8.pycqa.org/en/3.9.1/user/violations.html#in-line-ignoring-errors), which actually causes the error code to be completely ignored:

- If you change them to anything else, the warnings will still be suppressed.
- If you add the necessary colons then it is revealed that `E261` was also being suppressed, unintentionally:
  ```
  test/jit/test_misc.py:27:57: E261 at least two spaces before inline comment
  test/jit/test_misc.py:28:35: E261 at least two spaces before inline comment
  ```

I did try using [flake8-noqa](https://pypi.org/project/flake8-noqa/) instead of a custom `git grep` lint, but it didn't seem to work. This PR is definitely missing some of the functionality that flake8-noqa is supposed to provide, though, so if someone can figure out how to use it, we should do that instead.

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

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

- https://github.com/pytorch/pytorch/runs/2365189927

Reviewed By: janeyx99

Differential Revision: D27830127

Pulled By: samestep

fbshipit-source-id: d6dcf4f945ebd18cd76c46a07f3b408296864fcb
2021-04-19 13:16:18 -07:00
Joel Schlosser
40483acc51 Support factory kwargs in torch.nn modules (#54508)
Summary:
Continuation of https://github.com/pytorch/pytorch/pull/53144

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

Reviewed By: bdhirsh

Differential Revision: D27855386

Pulled By: jbschlosser

fbshipit-source-id: dabd505d2a04208e74b158570fb2859c736eea2c
2021-04-19 12:24:58 -07:00
Sam Estep
d05e7c163f Revert D27600457: [pytorch][PR] Support factory kwargs in torch.nn modules
Test Plan: revert-hammer

Differential Revision:
D27600457 (1077f87269)

Original commit changeset: b58bfee61c39

fbshipit-source-id: 19d5bfc5133a3880383731d0332503ca1f3bce0c
2021-04-19 07:47:24 -07:00
Joel Schlosser
1077f87269 Support factory kwargs in torch.nn modules (#54508)
Summary:
Continuation of https://github.com/pytorch/pytorch/pull/53144

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

Reviewed By: mrshenli

Differential Revision: D27600457

Pulled By: jbschlosser

fbshipit-source-id: b58bfee61c3917524b4622f63ef216c27a588eb1
2021-04-19 06:58:40 -07:00
Vasiliy Kuznetsov
48e675ac75 fx quant: fix subtle bug in BinaryOpQuantizeHanlder logic in matching (#56294)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56294

When matching a pattern to `BinaryOpQuantizeHandler`, we need to make
sure we check for dtype support on the base node, instead of the current
node.  This is important in cases such as `add-relu` and `mul-relu`,
when the current node is `relu`, but the base node is `add|mul`.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx
```

There is no good test case to check this in current logic.  Created an
add-relu model manually, and verified with pdb that the add node was
being used to match against dtypes.

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27831070

fbshipit-source-id: 3697f1328dff9fec3eb910bae49a73793ef36d63
2021-04-16 18:19:22 -07:00
Jerry Zhang
98933866a9 [quant][graphmode][fx] Optimize cat (#54813)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54813

Previously we have a cat that takes a list of Tensors with different qparams and dequantize them
cacatenate them and requantize with the output qparams. This adds some unnecessary overhead in dequantizing
and quantizing Tensors.

This PR adds an optimization for cat operator, we'll make sure inputs and output of cat
uses same observer/fake_quant and produce a cat that does not do rescaling.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27408377

fbshipit-source-id: 6a4bdcfd15e57ea1fe0f7e72d1e1288eb3ece4db
2021-04-16 16:00:43 -07:00
Vasiliy Kuznetsov
9f216b9499 ns for fx: enable shadowing int8 to int8 (#56205)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56205

Allows for int8 modules to shadow int8 modules. This is useful when
comparing quantized models with different qconfigs.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_int8_shadows_int8
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27807405

fbshipit-source-id: 10c3bc7ab9bb1e6808aa1af23a34c7cf380465fd
2021-04-16 10:34:47 -07:00
Vasiliy Kuznetsov
ae0af8bb51 ns for fx: move unmatchable mod/fun/meth mapping to mappings file (#56197)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56197

No logic change, just moving code around.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_op_io_dtype_coverage
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27805332

fbshipit-source-id: 0a63cf6ef7e5c4f655cdd5a18d54cc988424ac80
2021-04-16 10:34:46 -07:00
Vasiliy Kuznetsov
6de5d13e0f ns for fx: make call_method nodes work in NS APIs (#56196)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56196

Enables `call_method` nodes to work in NS APIs for unshadowed
and shadowed activations.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_op_io_dtype_coverage
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_match_activations_meth_ptq
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_add_shadow_loggers_meth_ptq
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27805335

fbshipit-source-id: 39b9c02c5c5faf098f2dd4f36d1ea8296d51a63c
2021-04-16 10:34:44 -07:00
Vasiliy Kuznetsov
07f3eaa716 ns for fx: remove deprecated code (#56195)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56195

This is outdated, removing (forgot to clean up in a previous PR).

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27805334

fbshipit-source-id: 3b035945b4928a3c727e96e0f7fe0efe201f42c0
2021-04-16 10:34:42 -07:00
Vasiliy Kuznetsov
0fbc2be234 ns for fx: enable call_method nodes in graph matching (#56194)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56194

Enables the NS graph matcher to also match `call_method` nodes.
These are useful for ops such as `torch.sigmoid`.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher.test_methods
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27805333

fbshipit-source-id: 509ae283db6b245671f11e3eb6b7fcb3a5735ef5
2021-04-16 10:34:41 -07:00
Vasiliy Kuznetsov
2380cc7d65 ns for fx: fill out coverage for node I/O types (#55918)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55918

Adds coverage for determining I/O dtype for various ops. This will
enable shadowing of these ops.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_op_io_dtype_coverage
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27740661

fbshipit-source-id: c5ce873ec56bffa50ca46d2fe134c70ed677e37e
2021-04-16 10:34:39 -07:00
Vasiliy Kuznetsov
430fc03e3f ns for fx: add category for ops which accept fp32 or int8 input (#55859)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55859

Adds mappings for ops which can accept either fp32 or int8 input,
such as `F.relu`.  A future PR will fill out the op coverage.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_op_with_either_fp32_or_int8_input
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D27740659

fbshipit-source-id: cfc3dd58319b7161ca7f1fe05cd22d9a3ff11141
2021-04-16 10:34:37 -07:00
Vasiliy Kuznetsov
5ec6434945 ns for fx: move op dtype category mapping to separate file (#55858)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55858

Moves the mappings of input and output dtypes of various ops
into its own file, and makes the variable names more clear. No logic
change.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D27740662

fbshipit-source-id: d384e7e542d9cc868d9cee9c53c2ac2f74a15a48
2021-04-16 10:33:05 -07:00
Vasiliy Kuznetsov
f59244ec16 ns for fx: add test for op relationship coverage (#55837)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55837

Adds a test that checks that all of the relevant op pairs defined in
`quantization_mappings.py` are also defined as related by Numerical
Suite.

Note: this does not cover all the ops, just the ones in
`quantization_mappings.py`.  A future PR will fill out the remainder.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher.test_op_relationship_mapping
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27719979

fbshipit-source-id: 9e852ef94da5f7a653ea15ba52c68a89c8e30208
2021-04-15 16:11:26 -07:00
Vasiliy Kuznetsov
c8209a7336 ns for fx: move pattern utils to separate file (#55805)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55805

No logic change, just moving util functions to separate file.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27719982

fbshipit-source-id: c80d5397c1efeb9fc83eacaa532ecbde557cca3f
2021-04-15 16:11:24 -07:00
Vasiliy Kuznetsov
b461104554 ns for fx: make get_reversed_fusions reuse quantization fusions (#55803)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55803

Makes the NS `graph_matcher.get_reversed_fusions` use the fusions
defined the FX quantization code instead of duplicating them.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27719980

fbshipit-source-id: 12e3183405181bb9001f10e765cfb4d2ffdfdd88
2021-04-15 16:11:23 -07:00
Vasiliy Kuznetsov
1cbc4023e9 ns for fx: add qat handling for weight extraction (#55506)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55506

Makes the NS weight extraction tests also test QAT, and fixes
the mappings where necessary to cover all the fusions and make
the tests pass.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_extract_weights_mod_ptq
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_extract_weights_mod_qat
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27650409

fbshipit-source-id: c5bd9268d1bc559afc27d4c5109effd77bf1538a
2021-04-15 16:11:16 -07:00
Vasiliy Kuznetsov
3786c2719d ns for fx: make NSTracer inherit from QuantizationTracer (#55505)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55505

This necessary to add support in NS for QAT modules, to avoid
duplicating logic between NSTracer and QuantizationTracer.

The eng work to expose the custom module and class names to
the user will be in a future PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27650407

fbshipit-source-id: 431f47c5353b41c11371c5efa79657bfd085459a
2021-04-15 16:11:14 -07:00
Vasiliy Kuznetsov
5ad3bc715c ns for fx: change node I/O determination to strict allowlist (#55434)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55434

Before this PR, there was some hacky logic which determined
the input and output types of nodes based on heuristics such
as inspecting `__module__`, or assuming that an op has an
I/O dtype of `torch.float` when the heuristics did not find
any matches.  This is problematic because the heuristics were not exact,
and this could result in non-sensical shadow graphs when the heuristics
would return an incorrect dtype.

This PR switches the dtype determination to an allowlist system,
where we specify exactly what the dtypes are for the nodes or modules
which are in an allowlist, and we add an `UNKNOWN` type for everything
else.  The shadow logic is changed to skip inserting shadows on any
function or module where the I/O dtype is unknown.

The current allowlist only contains functions necessary for the
currently existing tests.  Filling out the allowlist with all necessary
torch functions is left for a future PR.

As a result of this, we can do the following (also implemented in this PR):
1. enable graph matching on nodes with equal types (for example,
F.linear and F.linear). The restriction that only nodes with equal types
was in the code as a placeholder, it's better to allow comparisons of
nodes of equal types. One case where this is useful is unshadowed
activations.
2. enable models with user defined modules to be passed to Numeric Suite
APIs without errors.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher
python test/test_quantization.py TestFXGraphMatcherModels
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27622418

fbshipit-source-id: 40dcba0222c01154c141467640c1eb89725f33a7
2021-04-15 16:09:51 -07:00
Vasiliy Kuznetsov
8188d18f8d ns for fx: add functional conv-relu fusion support (#55433)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55433

Makes `F.conv{n}d -> F.relu` patterns work for NS weight
extraction.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_extract_weights_conv_fun
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D27622417

fbshipit-source-id: d3ee08bd19865874cff3776c3b69e232fdfc5912
2021-04-14 09:04:37 -07:00
Vasiliy Kuznetsov
784ae23d43 ns for fx: fix bug in weight extraction testing (#55431)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55431

Fixes a bug in the test cases, returning early resulted
in some tests not being run.  Adds logic for `nni.LinearReLU`,
which was unmasked by making the tests run

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_extract_weights_mod
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D27622415

fbshipit-source-id: 79d9e3125e5d881d9d13645abbe4bd007a5e1d44
2021-04-14 09:04:32 -07:00
Vasiliy Kuznetsov
8b992ab0e4 ns for fx: add conv1d weight extraction (#55327)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55327

Adds NS functionality for extracting weights from `F.conv1d` nodes.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_extract_weights_conv_fun
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27575425

fbshipit-source-id: 65fa194802ac7a9fb75b7616d962c5c2e71321ff
2021-04-14 09:04:30 -07:00
Vasiliy Kuznetsov
8fc1ca0d22 fx quant: fix prepacking for F.conv1d (#55311)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55311

Before this PR, `F.conv1d` was matched by FX graph mode quant patterns
but the prepacking was happening inline.  There was also a bug with
argument type mismatch.

This PR fixes both issues and adds a test. Thanks jerryzh168 for the
code tip.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx.test_functional_not_reference
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27575422

fbshipit-source-id: 42301e23cb101a9e64e46800813bc771317e233e
2021-04-14 09:04:28 -07:00
Vasiliy Kuznetsov
457fac0a33 ns for fx: move more weight matching logic to weight_utils.py (#55288)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55288

No logic change, just moving util-like code to the utils file.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D27575423

fbshipit-source-id: cd5188a0940bb664be7d0275faa7df8ea18401a8
2021-04-14 09:04:26 -07:00
Vasiliy Kuznetsov
13d7b40ea0 ns for fx: add F.conv2d and F.conv3d weight extraction (#55287)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55287

Adds support for extracting weights from F.conv2d and F.conv3d.
F.conv1d and the fused variants are saved for future PRs.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_extract_weights_conv_fun
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D27575424

fbshipit-source-id: e945912d7d0ab320f47cab30d00d60ddb7497158
2021-04-14 09:04:24 -07:00
Vasiliy Kuznetsov
1fb2abc7ad ns for fx: rename SugraphTypeRelationship to SubgraphTypeRelationship (#55155)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55155

Fixes typo in enum name, no logic change

Test Plan:
CI

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27504625

fbshipit-source-id: 21605dadb48225987f1da5ad5f6c30b0183278f2
2021-04-14 09:04:22 -07:00
Vasiliy Kuznetsov
37a404610f ns for fx: add allowlist for ops with same signature across dtypes (#55154)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55154

Adds functionality to NS to allow matching nodes which have the
same signature across dtypes.  For now, only the skeleton is added,
we can fill out the rest of the ops later.  This is to unblock
the work to change `cat` to have the same signature for fp32 and int8,
and keep the testing we have for `cat` in NS.

For context, the main reason we are not matching nodes with equal types,
for now, is user defined types for which we do not know the signature.
For now, the design is strictly allowlist of everything. In the future,
we may adjust the design to safely match user defined types.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_ops_with_same_fp32_and_int8_signature
python test/test_quantization.py TestFXGraphMatcher.test_nodes_with_equal_types_do_not_get_matched
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D27504624

fbshipit-source-id: 4f8eb4f3258caf6f99aa373ca7ba516ebbcf4779
2021-04-14 09:04:20 -07:00
Vasiliy Kuznetsov
444b318a90 ns for fx: add linear-relu mod weight extraction (#55080)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55080

Adds support for extracting weights of linear-relu module pattern.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D27474701

fbshipit-source-id: 69ceaadc28d7fdcebd16d519367274d348b0dd29
2021-04-14 09:02:51 -07:00
Jerry Zhang
c96b5b2a20 [quant][graphmode][fx][fix] Fix fp16 reference patterns for linear (#55727)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55727

number of dequantize for fp16 reference pattern was incorrect before, this
PR fixes the problem

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27713390

fbshipit-source-id: 72b8d4cda0bdcea74abe27a76f918d1b47819b01
2021-04-13 23:19:45 -07:00
Sam Estep
4753100a3b Un-ignore F403 in .flake8 (#55838)
Summary:
Generally wildcard imports are bad for the reasons described here: https://www.flake8rules.com/rules/F403.html

This PR replaces wildcard imports with an explicit list of imported items where possible, and adds a `# noqa: F403` comment in the other cases (mostly re-exports in `__init__.py` files).

This is a prerequisite for https://github.com/pytorch/pytorch/issues/55816, because currently [`tools/codegen/dest/register_dispatch_key.py` simply fails if you sort its imports](https://github.com/pytorch/pytorch/actions/runs/742505908).

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

Test Plan: CI. You can also run `flake8` locally.

Reviewed By: jbschlosser

Differential Revision: D27724232

Pulled By: samestep

fbshipit-source-id: 269fb09cb4168f8a51fd65bfaacc6cda7fb87c34
2021-04-13 09:24:07 -07:00
Vasiliy Kuznetsov
ec9b20ddc0 fx quant: fix edge case with copynode after user function (#55710)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55710

In the current code, there is an edge case which leads to an error
after the prepare step:

1. have a pattern like this:

```
user_func_unmatched_to_qhandler -> node_matched_to_copy_node_qhandler
```

2. the user function returns a type which is not observable (i.e. not a
Tensor)

3. if this is run through `prepare_fx`, calibrating it with data leads
to a runtime error, because observers cannot observe non-tensor types.

This PR fixes the issue.  If a node matched to `CopyNodeQuantizeHandler`
is after an unmatched node, we delete the observer.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx.test_no_obs_between_unmatched_node_and_copy_node
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27686811

fbshipit-source-id: 320be41b1f383c6352ff89fb39a9f480822a3bb2
2021-04-12 08:47:44 -07:00
Jerry Zhang
3e8ebb17aa [reland][quant][graphmode][fx][refactor] Factor out insert_observers_for_model to a separate function (#54733) (#55307)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/55307

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27567475

fbshipit-source-id: 74b7db63f7e1e795e7ac7ed6027cf786d922e7bf
2021-04-09 17:56:55 -07:00
Jerry Zhang
bbd2b1bd3c [quant][graphmode][fx] Add shape to nontensor op list (#55529)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55529

x.shape outputs a non-Tensor, add this to the all_node_args_have_no_tensors function
to avoid inserting observer for the getattr "shape" node.

Test Plan: Imported from OSS

Reviewed By: wat3rBro

Differential Revision: D27628145

fbshipit-source-id: 4729294ab80c0a1e72440396d31e7e82257b1092
2021-04-08 23:27:05 -07:00
Jerry Zhang
4d449f915f [quant][graphmode][fx] Separate handling Copy operator to a helper function (#54644) (#55429)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55429

Previously we special case copy operator in normal insert observer code, this PR tries to split the
special case logic to a separate function and keep the rest of the code clean.

Test Plan:
Imported from OSS

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27609972

fbshipit-source-id: 378f6aa70f18c0b477b62b6efe236648748aae7e
2021-04-08 22:12:24 -07:00
Nikita Shulga
add49e7e4e Enforce PEP263 for PyTorch python codebase (#55346)
Summary:
All python files containing non-ASCII characters should be correctly annotated with `# -*- coding: utf-8 -*-` comment

Delete number of superfluous UTF-8 characters, most commonly UTF-8 opening closing quotation mark U+2019 (’) instead of ascii apostrophe ', for example `Module’s`->`Module's`

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

Reviewed By: samestep

Differential Revision: D27582044

Pulled By: malfet

fbshipit-source-id: c1cd89655915858ff3a41f675cdfffff795a8e44
2021-04-06 18:31:38 -07:00
Bradley Davis
8eaa4a97b7 Back out "[quant][graphmode][fx] Separate handling Copy operator to a helper function" (#55388)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55388

temporarily revert D27314678 (c57541ce06), it appears to cause a perf regression that makes quantization of some models take too long to complete tests.

Reviewed By: houseroad

Differential Revision: D27583809

fbshipit-source-id: e9c088ccbfd3bfb3a1d4c7eafee3eca29ee7717b
2021-04-06 14:20:36 -07:00
Vasiliy Kuznetsov
8062545c63 ns for fx: weight extaction for conv1d and conv3d (#55079)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55079

Extends weight extraction to conv1d and conv3d.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27474696

fbshipit-source-id: 9d5f892160b1b003aa557cfd099c6834e3f70ded
2021-04-02 09:35:34 -07:00
Vasiliy Kuznetsov
80b1b7e4b1 ns for fx: ensure kwargs are handled when graph matching (#55078)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55078

Fixes a TODO, make sure we iterate through kwargs as well as args
when navigating graphs.  We can use `node.all_input_nodes` convenience
property to accomplish this.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27474699

fbshipit-source-id: 8a6e3db5a73328c4f296ac5fce951e81213b6f58
2021-04-02 09:35:32 -07:00
Vasiliy Kuznetsov
a590fa7af4 ns for fx: clean up debug print statements (#55077)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55077

Deletes debugging prints from the code, no logic change.

Test Plan:
CI

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27474700

fbshipit-source-id: 3d9d73da6615ddffdfdb0df270bcdfd2c4b50be3
2021-04-02 09:35:30 -07:00
Vasiliy Kuznetsov
f6b25e758d ns for fx: move it to top level file (#55060)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55060

Removes the previous iteration of Numeric Suite for FX graph mode
quantization, and moves the current iteration into the top level
file.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
python test/test_quantization.py TestFXGraphMatcher
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27467725

fbshipit-source-id: 4c22b5a3221857231f9f59cf6d2908820e6a7f12
2021-04-02 09:35:27 -07:00
Vasiliy Kuznetsov
c6cb99a6c7 ns for fx: weight extraction for nni.ConvReLU2d (#54335)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54335

Simple fix to enable weight extraction for nni.ConvReLU2d.

Note: this module only appears if the internal GraphModule APIs are
called, so we add testing for this path.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_extract_weights_mod
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D27192844

fbshipit-source-id: 923cf63e29e4638fd77ca42e69aedb15fb20a330
2021-04-02 09:35:25 -07:00
Vasiliy Kuznetsov
5319d17be4 ns for fx: make input logging work for multi node subgraphs (#54327)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54327

Makes input logging work properly for multi-node subgraphs.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_linear_fp16_shadow_activations
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D27190137

fbshipit-source-id: 3f39bfd5112d5ee92c1e66c133e970c28db40d46
2021-04-02 09:35:22 -07:00
Vasiliy Kuznetsov
b8019cee0e ns for fx: make input logging work for multi-node subgraphs (#54326)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54326

Fixes unshadowed activation input logging for subgraphs where start_node does
not equal end_node. In detail:
* instead of passing around a single list of nodes, pass around a list
of nodes to instrument inputs, and a list of nodes to instrument
outputs. This way we can handle multi-node subgraphs properly, and we
also keep the subgraph instance definition out of the public APIs.
* add a test case

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_linear_fp16_activations
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D27190138

fbshipit-source-id: 58e2377c1c128baaf3b760c1ad29098fb21f53d3
2021-04-02 09:35:20 -07:00
Vasiliy Kuznetsov
757e3cbf82 ns for fx: add support for shadowing linear fp16 patterns (#54275)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54275

Adds support for NS shadow activations path for the fp16 emulation
pattern such as

```
... -> dequantize -> linear -> relu -> to(torch.float16) -> ...
```

There are a couple of changes necessary here:

1. removing the restriction on the shadowing graph pass that the B
subgraph is a single node (since this subgraph is four nodes), and
modifying the code to correctly add the relevant inputs versus output
loggers (input loggers and subgraph copy if we are at start_node,
and output logger if we are at end_node)

2. modifying the logic for calculating node input and output type
to work correcty for the `to` and `dequantize` nodes:
2a. make the function return the first input and output, instead of just
the first input
2b. make the function handle `dequantize` correctly by recursively
using the output if its input
2c. make the function handle `to` correctyl by recursively using the
output of its input and the target dtype

3. a bug fix to handle observers in kwargs, while copying subgraphs

Note: input logging for these patterns is not tested yet,
this will be in the next PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_linear_fp16
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27172655

fbshipit-source-id: 3bdc86618b2a5782627fcf303d58af7f47fbc30d
2021-04-02 09:33:36 -07:00
Mike Ruberry
15f04e3466 Revert D27408378: [quant][graphmode][fx][refactor] Factor out insert_observers_for_model to a separate function
Test Plan: revert-hammer

Differential Revision:
D27408378 (c445f4ee93)

Original commit changeset: 9143f0a6f939

fbshipit-source-id: ae65ea798a6d72f2ec724c4c1b492937edddf721
2021-03-31 20:51:42 -07:00
Jerry Zhang
c445f4ee93 [quant][graphmode][fx][refactor] Factor out insert_observers_for_model to a separate function (#54733)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/54733

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27408378

fbshipit-source-id: 9143f0a6f939fa80f1d1d6bae4b2d37aa21cb9b9
2021-03-31 18:50:47 -07:00
Jerry Zhang
c57541ce06 [quant][graphmode][fx] Separate handling Copy operator to a helper function (#54644)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54644

Previously we special case copy operator in normal insert observer code, this PR tries to split the
special case logic to a separate function and keep the rest of the code clean.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27314678

fbshipit-source-id: d36870ceb3717bc01eaeaa6f3f1532ad562cbaf1
2021-03-31 17:50:32 -07:00
Jerry Zhang
c0d6dbdce4 [quant][fx][graphmode][refactor] Change activation_post_process_map to track the observer name instead (#54643)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54643

A refactor needed for future changes.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27314677

fbshipit-source-id: 972fbfb506f86da13f8817b3eaa5e6d0ad16ffe1
2021-03-31 17:50:30 -07:00
Jerry Zhang
c2adedf6fe [quant][graphmode][refactor] Remove reduandent code (#54073)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/54073

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27086067

fbshipit-source-id: b1995138de56f1352c5df03378ebc2832bf35ef7
2021-03-31 17:50:27 -07:00
Jerry Zhang
55544cb13a [quant][graphmode][fx] Add support for one value being quantized with different qconfigs (#53586)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53586

Previously one value can only be quantized to one dtype, this PR adds the support for quantizing one value
in the fx graph with multiple dtypes, e.g. first quantize to int8 and then float16

might do some followup PRs to clean up the hacks and refactor the code.

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_multiple_qconfigs_single_value

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26912676

fbshipit-source-id: ae3653fd67f05870a3a9e808f491871826c555d5
2021-03-31 17:48:50 -07:00
Supriya Rao
6f63126b5c [quant][fx] Add pass in convert to fold quant-dequant sequence (#54860)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54860

Currently we insert a quantize_per_tensor op when we encounter the quantizable input,
so if it has multiple uses and not all are quantizable then we need to add a dequantize op
before these ops.

In this pass - For a sequence of quantize_per_tensor - dequantize, we combine them
since it is a no-op.

[internal only][pyper]

Before this change we had redundant dequantize nodes in the graph
Example 1x inline_cvr graph https://www.internalfb.com/intern/everpaste/?handle=GODBxAlUMzGHD6 (98143776f5)MSACpHKKu9qjorbsIXAAAz
 FC layers -> 37
 quantize_per_tensor -> 30
 dequantize -> 49

After this change
https://www.internalfb.com/intern/everpaste/?handle=GAl0uQnOlDNmpLoSAB-GZqRxu9wMbsIXAAAz
 FC layers -> 37
 quantize_per_tensor -> 30
 dequantize -> 39

We remove extra 10 dequantize nodes in the graph.

Test Plan:
python test/test_quantization.py test_fold_quant_dequant

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D27390506

fbshipit-source-id: 56e6fb8496171246eccf4bd45eb8bebd87fcb740
2021-03-30 08:40:24 -07:00
Supriya Rao
a7dc0ab845 [quant][fx][pyper] Get first linear use of quantize_per_tensor for FQN (#54859)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54859

This is applicable to the case when a call_function linear op is one of the users of quantize op
In order to be able to map the qparams of quantize_per_tensor to the qparams of the linear operator
that consumes it, we need to use the FQN of the module with linear op for the qparmas of quantize_per_tensor.

Test Plan:
python test/test_quantization.py test_qparams_fqn

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27390505

fbshipit-source-id: a47af0e5ac016f2b2df74fbdf45afe99dc04be46
2021-03-30 08:38:51 -07:00
Supriya Rao
a0a7a2d648 [quant][fx] store dtype, axis as literals in the graph (#54624)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54624

previously we were creating setattr nodes for dtype and axis.
The FX convention is that primitive types are embedded as literals in args/kwargs.

With this change we won't see getattr nodes in the graph anymore for dtype/axis

Test Plan:
python test/test_quantization.py TestQuantizeFx

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27306898

fbshipit-source-id: a7c91c7cb21ee96015c7f8830b38d943ada65358
2021-03-28 21:59:49 -07:00
eellison
0e320ddb36 Lazily initialize alias db constant prop (#54640)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54640

If we are running constant propagation on a graph that doesn't have any operators with constant inputs and any mutable inputs/outputs, we do not need to initialize an alias db. This is going to be used to speed up symbolic shape analysis.

Test Plan: Imported from OSS

Reviewed By: nikithamalgifb

Differential Revision: D27340863

Pulled By: eellison

fbshipit-source-id: 087b2a33b42c58fa5dae405d652b056d0f1d72e7
2021-03-26 19:44:29 -07:00
Vasiliy Kuznetsov
0a18211989 ns for fx: add weight matching for linear fp16 emulation (#54257)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54257

Makes the NS weight extraction fuction work correctly with
fp16 emulation patterns for linear.  We navigate to the
weight correctly, and cast it to `torch.float16` before returning.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_linear_fp16
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27159370

fbshipit-source-id: 95f555298e3153e4783c64b3d8c83b9d3fdffa12
2021-03-25 22:35:38 -07:00
Vasiliy Kuznetsov
182d8c375c ns for fx: add partial support for subgraphs with base_op_node (#54254)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54254

In fp16 emulation, we now have patterns such as

```
... -> dequantize -> linear -> relu -> to(torch.float16) -> ...
```

This PR adds support for
* specifying a subgraph's "base_op_node", which is the node with the op
which should be matched to related nodes. In the example above,
"base_op_node" would be the linear node, and it would be the second
node in the matched pattern.
* matching these fusion patterns and properly setting "base_op_node"
based on pattern and index
* using "base_op_node" instead of "start_node" throughout the NS
codebase wherever the intent is to match subgraphs or create names
for subgraphs.

At the end of this PR, matching unshadowed activations with an example
fp16 emulation pattern works e2e.

I'm saving the following work for future PRs (soon), mostly to keep
PR size manageable:
* adding weight matching (will require some changes to function which
extracts weights)
* adding shadowed activation matching (will require some changes to
shadow copying)
* adding input logging for these patterns (will likely require some changes as well)

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_linear_fp16
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27158199

fbshipit-source-id: 49fc445395452fda62e3c7a243544190f9af691c
2021-03-25 22:35:36 -07:00
Vasiliy Kuznetsov
454832e5fa ns for fx: create subgraph type (#54253)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54253

Creates an `NSSubgraph` type for representing a subgraph instance,
and modifies the NS code to use it. This will enable us to add
more information to the subgraph instance definition without
having to change all the callsites.

Test Plan:
```
mypy torch/quantization
python test/test_quantization.py TestFXGraphMatcher
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27158198

fbshipit-source-id: 548785dd90144e2da256c23af990620c778e7cfe
2021-03-25 22:35:34 -07:00
Vasiliy Kuznetsov
9e8e744efe ns for fx: move shadow lstm test to new API (#53828)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53828

Moves LSTM shadow activations test to new API. In order
to enable this, adds support for passing two args instead
of one arg when copying a subgraph from A to B.

Since this was the last test of the old API, deletes
the old test case.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels.test_compare_shadow_activations_lstm_dynamic
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26982733

fbshipit-source-id: 03f580688dd37f3ccd688d9f444e9e79cfa84734
2021-03-25 22:35:31 -07:00
Vasiliy Kuznetsov
cfe7364809 ns for fx: move shadow activations linear test to new API (#53819)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53819

Moves the linear tests for shadow activations to new API.
In order to do so, adds logic for fp32 to fp32 dtype cast,
which is an identity.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels.test_compare_shadow_activations_linear
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26982734

fbshipit-source-id: b6203228abf3cdf74ab0638468a6df77658aa662
2021-03-25 22:35:29 -07:00
Vasiliy Kuznetsov
3dc8ba27a5 ns for fx: move shadow activations conv test to new API (#53818)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53818

Moves testing of conv for shadow activations to new NS API

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels.test_compare_shadow_activations_conv
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26982732

fbshipit-source-id: 9e8709a76363fbcdf84413e5d4a6c8a0889cb97b
2021-03-25 22:35:27 -07:00
Vasiliy Kuznetsov
52a8075f16 ns for fx: add support for lstm activation matching (#53779)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53779

Moves the test case for LSTM activation matching to new NS APIs.

This requires adding the ability to log non-Tensor types.
Since we need Loggers to be scriptable and TorchScript does
not support `Union`, we collect statistics in a separate collector
if we have an RNN.  Note: this can scale to a small N of
return types, but not to a large N.  If the N becomes large in
the future, we will solve it then.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26967110

fbshipit-source-id: afe60b44fdec28a328813b4f342cf4fe04820baa
2021-03-25 22:33:41 -07:00
Vasiliy Kuznetsov
b81e10a291 fx quant: fix bug with fusion patterns and disabling quantization (#54654)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54654

Fixes a bug where disabling quantizaton on potential fusion patterns
would lead to errors in the `convert` function.  For example:
1. have a model with add-relu
2. disable quantization for the part of the model containing add-relu
3. run prepare and convert, the convert step would fail because
intermediate nodes were missing from `env`.

The fix is to add handling for this edge case.  If quantization is
disabled, we manually copy the nodes for multi-node fusion patterns.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx.test_fusion_pattern_unquantized
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D27318454

fbshipit-source-id: 27c1fd1cb7c9711a8e8d338200971c428dae8f98
2021-03-25 22:21:41 -07:00
Yukio Siraichi
27048c1dfa Remove legacy constructor calls from _torch_ folder. (#53889)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/53146
Related to https://github.com/pytorch/pytorch/issues/47112

As mentioned in https://github.com/pytorch/pytorch/issues/47112, the plan is to:

1. Verify that all `torch.Tensor()` scenarios are covered by other functions
2. Scrub internal `torch.Tensor()` uses
3. Update the docs and throw `TORCH_WARN_ONCE` if someone uses `torch.Tensor()`

In this PR, I replaced all occurrences of `torch.Tensor` present in the _torch_ folder.

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

Reviewed By: walterddr, zou3519

Differential Revision: D27190743

Pulled By: jbschlosser

fbshipit-source-id: 7ecc201d57935b8dbb98ae3718b60d95cb55a010
2021-03-19 15:20:19 -07:00
Vasiliy Kuznetsov
01c6e9360e ns for fx: move lstm dynamic weight test case to new API (#53772)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53772

Moves the test case for extracting LSTM dynamic weights to new NS API.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels.test_compare_weights_lstm_dynamic
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26967104

fbshipit-source-id: 0d17e7735ec361167dcf72bcb373bfc1aad84668
2021-03-12 10:02:43 -08:00
Vasiliy Kuznetsov
a71cd135ae ns for fx: move linear dynamic weight test case to new API (#53765)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53765

Moves linear dynamic weight test case to new NS API.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels.test_compare_weights_linear
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26967109

fbshipit-source-id: 2096a88a3005270696d536f2e1bbc87e70c07230
2021-03-12 10:02:38 -08:00
Vasiliy Kuznetsov
9c8f112ada ns for fx: move linear weight test case to new API (#53764)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53764

Moving the linear weight test case to new FX NS APIs.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels.test_compare_weights_linear
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26967111

fbshipit-source-id: f0a90d7863d5d866e391729ec28e0e0dea339900
2021-03-12 10:02:34 -08:00
Vasiliy Kuznetsov
19fe8a529e ns for fx: move conv weight test case to new API (#53761)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53761

Moves the testing of conv weight matching to new NS APIs.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels.test_compare_weights_conv
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26967108

fbshipit-source-id: af3647733f954a657e0868c2c40642018de9ea49
2021-03-12 10:02:30 -08:00
Vasiliy Kuznetsov
7d27eb8068 ns for fx: clean up API naming (#53729)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53729

Aligns the names of the three core APIs to the design doc.
New names:

```
// weights
_extract_weights_one_model
extract_weights

// unshadowed activations
_add_loggers_one_model
add_loggers
_extract_logger_info_one_model
extract_logger_info

// shadowed activations
add_shadow_loggers
extract_shadow_logger_info
```

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26953071

fbshipit-source-id: dda6df1c26afd99dd7779e72e3eed2d3d72c8128
2021-03-12 10:02:21 -08:00
Vasiliy Kuznetsov
421e91dfd2 ns for fx: add support for logging inputs
Summary:
This PR implements the option to log inputs for FX Numeric Suite.  The user facing api looks like

```
def prepare_model_outputs(..., should_log_inputs : bool = False)
def prepare_model_with_stubs(..., should_log_inputs : bool = False)
```

The output data now looks like

```
{
  "layer1": {
    "node_inputs": {
      "model1": [{
        "values": ...,
        ...,
      }],
    },
    "node_outputs": {
      ...,
    }
  },
  ...  // other layers
}
```

One key design decision taken here is that an input logger logs the output of previous nodes, instead of logging the input of the current node.  This matters for a signature such as `cat([x1, x2, x3])`.  We are inserting three input loggers here (for x1, x2, and x3), instead of a single input logger for `[x1, x2, x3]`.  This was chosen in order to preserve the structure of the original graph as much as possible and keep flexibility for future optimizations.

Test Plan:
TODO: fill out

Imported from OSS

Differential Revision: D26931225

Reviewed By: hx89

Pulled By: vkuzo

fbshipit-source-id: dd692bfb5ddaaf5554f80c25e2f40b21762e4fc3
2021-03-12 10:02:17 -08:00
Vasiliy Kuznetsov
cc940f3580 ns for fx: change dtype cast from once per N node to once per node
Summary:
This PR ensures that when we do a dtype cast for a shadow module,
we insert N dtype casts for N nodes, instead of combining N nodes
into a single dtype cast.

An example where this occurs is `cat([x, y], dim=0)`

```
// original graph

[x, y] -> cat_b -> output

// shadow graph with a single dtype cast, before this PR

  dtype_cast -> cat_a_shadow -> output_a_shadow
  /
[x, y] -> cat_b -> output_b

// shadow graph with multiple dtype casts, after this PR

 [dtype_cast_x, dtype_cast_y] -> cat_a_shadow -> output_a_shadow
 /
[x, y] -> cat_b -> output_b
```

The reason things worked before this PR is because `torch.dequantize`
can take either a single tensor or a list of tensors.  We are changing
this to make an upcoming addition of input loggers easier.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_prepare_model_with_stubs_multiple_dtype_casts
```

Imported from OSS

Differential Revision: D26931226

Reviewed By: hx89

Pulled By: vkuzo

fbshipit-source-id: e9c7d4c7942e0f59c952094d2e446b1e2c838396
2021-03-12 10:02:12 -08:00
Vasiliy Kuznetsov
d73e36a44a ns for fx: change API to take nn.Module instead of GraphModule (#53075)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53075

The input and output types should be `nn.Module`, to hide
the implementation detail that the pass is using FX.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26740548

fbshipit-source-id: d5ed445379355bebdd90d377c95fcd7e671371a3
2021-03-12 10:00:35 -08:00
Vasiliy Kuznetsov
4884a6ab51 fx quant: clean up names of quantize handlers (#53614)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53614

Ensures that every subclass of `QuantizeHandler` has a clear name.  This
prevents ambiguous names like `Cat`, which look like a module but are
really a quantize handler.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26914784

fbshipit-source-id: 6dca7e27975c09f422f8e36f1d2b709bf3eaaadf
2021-03-12 07:43:53 -08:00
Vasiliy Kuznetsov
279b5372ab [not for land] fix fx quant for quant_layer -> stack -> sum (#53196)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53196

Before this PR, code patterns like this did not work:

```
x = some_quant_layer(x)
x = torch.stack([x, ...])
x = torch.sum(x, ...)
```

The reason this did not work is because `torch.sum` is treated as
"quantized" because of the newly added fp16 support, even though it is
not actually "quantized" for models where fp16 is not used.  We may
need to adjust the concept of "quantized vs non-quantized" into a
"dtype" for the longer term fix.

The current PR is a hacky fix to unblock.  We need to clean things
up before this is landable

Test Plan:
```
python test/test_quantization.py TestQuantizeFx.test_quant_sum
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26783960

fbshipit-source-id: 3be7c3c1eaa2b8fcb99a105e1b0004c9ffd3a1c1
2021-03-12 07:43:50 -08:00
Vasiliy Kuznetsov
93d5807c1e [not for land yet]fix using size of quant layer in torch._assert (#53187)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53187

Before this diff, if we had code lik

```
x = any_quant_layer(...)
x_size0 = x.size(0)
torch._assert(x_size_0 == 1)
```

The convert code would try to insert a dequantize after `x_size0`,
because it was a descendant of a quantized node and it was needed
for a non-quantized operation.  Since the actual type of the `size`
function output is an integer, this does not make sense.

For now, this is fixed as a one-off to unblock a customer.  In the
future, we may need to think more deeply about all the functions which
can return non-quantized types from quantized tensors and make sure
they are all covered.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx.test_assert_on_size_after_quant_layer
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26780690

fbshipit-source-id: 44cc25c9179d460efb3f110d40b73d854d676af5
2021-03-12 07:43:48 -08:00
Vasiliy Kuznetsov
ccab6680d5 [not for land yet] hacky fix for x.ndim followed by sub (#53120)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53120

Currently there is a pattern which is not handled correctly by
FX graph mode quantization:

```
def forward(self, x):
    ndim = x.ndim
    # or add, mul, div, etc
    x = torch.sub(x, ndim)
    return x
```

The reason this does not work is as follows:
1. x.ndim becomes a getattr node
2. the real world type of x.ndim is an integer, but this is not known from the graph (yet)
3. binary ops such as `torch.sub` require quantization of inputs
4. the framework inserts an observer to observe the output of `ndim`
5. the observer fails because `ndim` is not a Tensor

For now, we hack a bandaid to unblock some teams, none of this is for
land.  We will have to think of a better fix which is landable (TBD).

Test Plan:
```
python test/test_quantization.py TestQuantizeFx.test_getattr_with_nontensor_result
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26756180

fbshipit-source-id: c0e498766b22c23df74fbb5aaeaa237c4c944263
2021-03-12 07:42:12 -08:00
Jerry Zhang
7484c56fa3 [quant][graphmode][fx] Fix a condition check for CopyNode (#53585)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53585

Previously fp16_static CopyNode would be marked as unquantized because of
an incorrect condition check of whether a Node is statically quantized or not.
This PR fixes that.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26912677

fbshipit-source-id: 4ddb538714c5ba2db28430de5e1cf2931baf1993
2021-03-11 09:32:20 -08:00
Jerry Zhang
0584fd9339 [quant][fx][graphmode][fix] Only insert observers for fixed qparam ops (#53330)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53330

Fixed a condition check for fixed qparam ops, previously we were including CopyNodes as well

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_fixed_qparams_ops_fp16

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26836867

fbshipit-source-id: 8c486155244f852e675a938c3f4237f26505671c
2021-03-10 16:51:36 -08:00
hyperfraise
f9185973d1 [quantization] Add some support for 3d operations (#50003)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/50002

The last commit adds tests for 3d conv with the `SubModelFusion` and `SubModelWithoutFusion` classes.

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

Reviewed By: mrshenli

Differential Revision: D26325953

Pulled By: jerryzh168

fbshipit-source-id: 7406dd2721c0c4df477044d1b54a6c5e128a9034
2021-03-10 16:40:35 -08:00
Jerry Zhang
d9fa957ecc [quant][graphmode][fix] Handle the case when observed node has no users (#53210)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/53210

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26791724

fbshipit-source-id: b2a226a22d6aba86dd01cacbb56577048a289b3e
2021-03-10 15:08:48 -08:00
Supriya Rao
7cec4b3d4a [quant][fx] add _remove_qconfig flag to convert_fx (#53166)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53166

Context: For fx modules that consist of scriptmodules, calling
delattr(module, 'qconfig') throws an attribute error. will follow up
with a separate issue/repro to fix this problem

This PR adds a temporary flag to convert_fx API to preserve the qconfig attributes on the converted model
We will remove this flag once we reach a conclusion on calling delattr on scriptmodules

Test Plan:
python test/test_quantization.py test_preserve_qconfig

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26771518

fbshipit-source-id: 9fd72816576856ffb4aa11f8fde08303d1df10a2
2021-03-03 12:58:05 -08:00
Jerry Zhang
46bd76fdec [quant][graphmode][fx][fp16] Add fp16 support for silu (#52865)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52865

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_silu

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26672270

fbshipit-source-id: a6a6ab58c347a56f0ded612b2e0a3e2230a91d9e
2021-03-02 02:11:29 -08:00
Jerry Zhang
d40b501cfc [quant][graphmode][fx][fp16] Add fp16 support for sigmoid (#52863)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52863

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_fixed_qparams_ops_fp16

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26672273

fbshipit-source-id: 30d5befe2a24081ac12ac773df4d2bd26d2d0192
2021-03-02 02:11:21 -08:00
Jerry Zhang
3fb324f05b [quant][graphmode][fx][fp16] Add fp16 support for layer_norm (#52862)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52862

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_layer_norm

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26672272

fbshipit-source-id: 4cfdce986efa98db7dc58bf2a62b650e45a69ed0
2021-03-02 02:11:17 -08:00
Jerry Zhang
fc6fdade9f [quant][graphmode][fx][fp16] Add fp16 support for torch.sum (#52811)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52811

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_sum

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26655619

fbshipit-source-id: 642e0de47d0da7bd1abe1e981819de33e84c32f3
2021-03-02 02:11:13 -08:00
Jerry Zhang
97c51d5d5d [quant][graphmode][fx][fp16] Add fp16 support for div (#52810)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52810

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_div

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26655620

fbshipit-source-id: e46cb895ba456e99e4433bd6037229b8248a1b28
2021-03-02 02:11:08 -08:00
Jerry Zhang
a6af93e921 [quant][graphmode][fx][fp16] Add fp16 support for sub (#52809)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52809

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_sub

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26655618

fbshipit-source-id: b47966ee1b75a2f814b9019d8d16b2da2212f5da
2021-03-02 02:09:07 -08:00
Jerry Zhang
991160ebd9 [quant][graphmode][fx] Add support for fp16 bmm pattern (#52808) (#53021)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53021

Add support for producing fp16 bmm pattern

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_bmm

Imported from OSS

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26725349

fbshipit-source-id: debee718fc33e562aff3f5664757bb52ee85f651
2021-03-01 14:45:55 -08:00
Jerry Zhang
096bea5251 [reland][quant][graphmode][fx][fp16] Add fp16 support for {add|mul}{_relu} (#52714) (#53019)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/53019

Test Plan:
python test/test_quantization.py TestQuantizedOps.test_add
python test/test_quantization.py TestQuantizedOps.test_mul
python test/test_quantization.py TestQuantizedOps.test_add_relu
python test/test_quantization.py TestQuantizedOps.test_mul_relu

Imported from OSS

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26725350

fbshipit-source-id: 2a89f5da6a21908f454f870521d2a4549fdd291e
2021-03-01 13:19:42 -08:00
Vasiliy Kuznetsov
0d46926c63 ns for fx: remove subgraphs from user facing API (#52928)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52928

Changes the user facing API of `prepare_single_model_output` to
require a list of nodes instead of a list of subgraphs. This ensures
that how we define a subgraph is an implementation detail and is
not exposed to the user, keeping the eng cost of updating this
implementation later low.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26693471

fbshipit-source-id: 67c2feb844556225e36f8d6d4023246939bcb445
2021-03-01 08:56:26 -08:00
Vasiliy Kuznetsov
87be8c1d7c ns for fx: clean up duplicate code in get_matching_activations_a_shadows_b (#52927)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52927

Refactor to use an existing util instead of duplicating code, no logic
change.

Test Plan:
CI

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26693474

fbshipit-source-id: 06b7047eb9a762557b7f679347e424c0dd009aad
2021-03-01 08:56:22 -08:00
Vasiliy Kuznetsov
5b93cdace1 ns for fx: remove model_name from get_matching_activations API (#52926)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52926

Model name is already stored in the Loggers in the prepare call.
Removing the need to specify it again in the extract activations
functions, to simplify things.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26693473

fbshipit-source-id: 52511cacc16f79fa09c78ccde78e7f439f4b315c
2021-03-01 08:56:18 -08:00
Vasiliy Kuznetsov
907ee5b290 ns for fx: docblock fixes (#52925)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52925

Cleans up some incorrect comments and docblocks in
`numeric_suite_core_apis.py`.

Test Plan:
CI

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26693472

fbshipit-source-id: 17f3ff464c6ea01374bcc6ac5899da7034627152
2021-03-01 08:53:57 -08:00
Mike Ruberry
312b297b82 Revert D26626092: [quant][graphmode][fx][fp16] Add fp16 support for {add|mul}{_relu}
Test Plan: revert-hammer

Differential Revision:
D26626092 (2962fbb03c)

Original commit changeset: 91d040efa51e

fbshipit-source-id: cc6bcc0f451d6adcd7bf7572451e6e3cd6ad59d1
2021-03-01 04:52:47 -08:00
Mike Ruberry
3a024a7ae2 Revert D26655616: [quant][graphmode][fx] Add support for fp16 bmm pattern
Test Plan: revert-hammer

Differential Revision:
D26655616 (2c44b256d8)

Original commit changeset: 1d0639303e5c

fbshipit-source-id: 403429c706c8a9e6a657669daf8aadf282025f83
2021-03-01 04:50:35 -08:00
Jerry Zhang
2c44b256d8 [quant][graphmode][fx] Add support for fp16 bmm pattern (#52808)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52808

Add support for producing fp16 bmm pattern

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_bmm

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26655616

fbshipit-source-id: 1d0639303e5ca2ca4ceae08d03ebc3b25256de57
2021-02-28 16:48:41 -08:00
Jerry Zhang
2962fbb03c [quant][graphmode][fx][fp16] Add fp16 support for {add|mul}{_relu} (#52714)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52714

Test Plan:
python test/test_quantization.py TestQuantizedOps.test_add
python test/test_quantization.py TestQuantizedOps.test_mul
python test/test_quantization.py TestQuantizedOps.test_add_relu
python test/test_quantization.py TestQuantizedOps.test_mul_relu

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26626092

fbshipit-source-id: 91d040efa51e9c955eb688ec16a30f0c12233958
2021-02-27 22:12:10 -08:00
Jerry Zhang
0818dbf49d [quant][refactor] Merge add and mul handler (#52651)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52651

Merging them for easier extensions to fp16 and more binary ops

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26600118

fbshipit-source-id: a1816e593cf3065afe87d2e6e44cdace13bf6aeb
2021-02-27 19:56:32 -08:00
Jerry Zhang
b685864f50 [quant][graphmode][fx] Add reference option support for linear_static_fp16 (#52650)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52650

linear_dynamic_fp16 has following dtypes for activation, weight, bias, output:
(fp32, fp16, fp32, fp32)

linear_static_fp16 has following dtypes:
(fp16, fp16, fp16, fp16)

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26599803

fbshipit-source-id: b4a8345d355125070be718a227288cc848cc8bbc
2021-02-27 08:25:44 -08:00
Jerry Zhang
177694681e [quant][graphmode][fx] Add reference option support for linear_dynamic_fp16 (#52534)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52534

Currently linear_dynamic_fp16 has a signature that's tied to fbgemm/qnnpack
We'll need to produce a pattern equivalent to linear_dynamic_fp16 to support extensions
to other backends

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_linear_dynamic_fp16

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26557726

fbshipit-source-id: 270c9f781f73c79416a092b7831294cabca84b0c
2021-02-26 21:12:22 -08:00
Jerry Zhang
cb6b65699f [quant][graphmode][fx] Add support for packed params in state_dict (#51639)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/51639

Test Plan: Imported from OSS

Reviewed By: z-a-f

Differential Revision: D26228185

fbshipit-source-id: 6cf8b4106fec9c6900521a2afe0de6f3d29cc896
2021-02-26 15:13:50 -08:00
Vasiliy Kuznetsov
25001a0148 ns for fx: remove ".stats" suffix (#52799)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52799

We agreed that it's better to not add this, removing.
We can make Eager mode NS match this in a future PR.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26652638

fbshipit-source-id: 5baa51a6bf6de5632946417fe9fd3d0f3e78f7fa
2021-02-25 20:45:53 -08:00
Vasiliy Kuznetsov
1d3172130d ns for fx: add node name and type to results (#52798)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52798

Adds the node name and node target type to Numerical Suite outputs.
This is useful to debug which node got matched to which node,
and what is the type of the operation.

```
// before
{
  layer_name: {
    model_name: {
      'type': 'weight',
      'values': [...],
    },
  },
}

// after
{
  layer_name: {
    model_name: {
      'type': 'weight',
      'values': [...],
      'node_name': '0',
      'node_target_type': "<class 'torch.nn.modules.conv.Conv2d'>",
    },
  },
}
```

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26652637

fbshipit-source-id: ba75b110cb91234f17a926ccbc5d0ccee2c3faeb
2021-02-25 20:45:49 -08:00
Vasiliy Kuznetsov
d2e88246d8 ns for fx: make return type of ns APIs future proof (#52789)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52789

Changes the return type of NS APIs from

```
{
  layer_name: {
    model_name: [torch.Tensor(...), ...],
  },
}
```

to

```
{
  layer_name: {
    model_name: {
      'type': 'weight',  # or node_output, etc
      'values': [torch.Tensor(...), ...],
      // future info can be added here, such as node name, etc
  },
}
```

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26652640

fbshipit-source-id: 4b31164e402754141368d5a04d595f2b643af3bb
2021-02-25 20:45:44 -08:00
Vasiliy Kuznetsov
fe068157de ns for fx: unify return types of weight and activation APIs (#52779)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52779

1. makes the return type of the weight comparison APIs match the return
type of the activation comparison APIs:

```
# before
{layer_name: {model_name: weight_tensor}}
{layer_name: {model_name: [activation_tensor]}}

# after
{layer_name: {model_name: [weight_tensor]}}
{layer_name: {model_name: [activation_tensor]}}
```

2. makes a type alias for the type, so future changes are easier

Test Plan:
```
mypy torch/quantization
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26652639

fbshipit-source-id: eb1f04d6913cedf88d628f362468875ae9ced928
2021-02-25 20:45:39 -08:00
Vasiliy Kuznetsov
7094d970d1 ns for fx: decouple subgraph names from node names (#52771)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52771

Before this PR, subgraph names were derived from node names
in model B.  For example, if we had

```
A: linear0 -> relu0 -> ...
B: linear_relu0 -> ...
```

Then the subgraph name would be `linear_relu0`, and the outputs before this
PR would look like

```
{
  'linear_relu0': {
    'model_a': ...,
    'model_b': ...,
  },
}
```

This PR decouples subgraph naming from node names.
The outputs after this PR look like:

```
{
  # guaranteed to match the right subgraphs across different models
  # without needing more than one model during the prepare passes
  'base_op_torch.nn.functional.linear_0': {
    'model_a': ...,
    'model_b': ...,
  },
}
```

There are future requirements for which using node_name as subgraph name does not work well:
a. the need to support N models, without having all of them in memory at the same time
b. the need to support fusions and match subgraphs with related but non-equal types

This PR changes the naming of subgraphs to be based on two things:
1. the name of the underlying set of related ops (i.e. `torch.nn.functional.linear`)
2. the order in which this subgraph was named (i.e. `foo_0`, `foo_1`, ...)

Basically, we can't use a node name because of (a), since there must be
a reference model which node name other models must use, but that
reference model is not guaranteed to be available.  Note: we could add
some state and require the reference model to go through the APIs first,
saving the reference node names, but I'm deliberately not doing that
to minimize the state used throughout.

To support (b), we need a way to determine a name of a subgraph which is
the same for all related subgraphs (i.e. linear-relu vs quantized_linear
vs quantized_linear_relu). In this PR, this is done by using the base
aten op's name.  We use a string name so it looks nice in the output
(I tried `str(underlying_type)`, and it is not easy for humans to read).

Note: after this PR, it's hard to parse the results to see which layer
is related to which node in the graph. This will be fixed in a future PR
where we will store the node name on the logger, and expose it in the
output.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher
python test/test_quantization.py TestFXGraphMatcherModels
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
python test/test_quantization.py TestFXNumericSuiteCoreAPIsModels
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26652641

fbshipit-source-id: ee8dacc2d6e875357c1574cbf426923f9466ea10
2021-02-25 20:43:45 -08:00
Vasiliy Kuznetsov
19a8ada8d5 quant: fix conv transpose with qconfig == None (#52844)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52844

Fixes a crash in qconfig checking which happened if a model had conv transpose
with qconfig set to None.

Test Plan:
```
python test/test_quantization.py TestPostTrainingStatic.test_convtranspose_per_channel_qconfig_none
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26666043

fbshipit-source-id: e1b62840b4e3c67acbb4dbdcd32514b374efce1e
2021-02-25 11:52:30 -08:00
Vasiliy Kuznetsov
1618dc2ac6 ns for fx: update graph matching to handle dicts and tuples in node args (#52681)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52681

Updates the NS graph matching to properly traverse through args of nodes
if args are lists or tuples.  As a side benefit, refactors the code to
make future similar improvements easier.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26611221

fbshipit-source-id: 4ddd9b26338a5a2763b2883967e100f73e207538
2021-02-25 08:53:44 -08:00
Vasiliy Kuznetsov
608f44b24b ns for fx: update graph matching to not match nodes with equal types (#52402)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52402

Before this PR, any pair of subgraphs with base nodes of equal
types matched.

While sometimes this is useful, this should be off by default to
properly handle user defined modules and functions, for which we do not
know how to extract weights or cast to the right input type.

In a future PR, we can add hooks to turn on matching for nodes
of equal types, for the situations where it makes sense.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher.test_nodes_with_equal_types_do_not_get_matched
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26499848

fbshipit-source-id: 5818b88eb7fd8ed36390f60aa1a18228bb50507e
2021-02-25 08:53:39 -08:00
Vasiliy Kuznetsov
4483c48eb1 ns for fx: support linear_relu for weight matching (#52395)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52395

Simple change to add logic to get the weight of a quantized
`linear_relu` node.

More flavors of conv and linear will be added in future PRs.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_compare_weights_fun
```

Imported from OSS

Reviewed By: hx89

Differential Revision: D26497992

fbshipit-source-id: e6d88e92eedd6cdbf9116cbcfc8f6164f8499246
2021-02-25 08:53:35 -08:00
Vasiliy Kuznetsov
64b4e37c26 ns for fx: allow graph matching of parents of cat (#52368)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52368

Before this PR, the graph matching logic only handles node arguments of
type Node. This PR extends it to allow to handle node arguments of type
Tuple, so that the matcher can properly navigate through the arguments
of `cat`.

Test Plan:
```
python test/test_quantization.py TestFXGraphMatcher.test_nodes_before_cat
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26490101

fbshipit-source-id: 2de8d6acc30f237e22bfc3cfa89728b37411aab6
2021-02-25 08:51:48 -08:00
Jerry Zhang
626756ac39 [quant][graphmode][api] debug --> reference (#52179)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52179

Rename debug to reference. We'll use this to produce a reference quantized model
that can be used as a common interface between pytorch quantized model and backends.

Test Plan:
python test/test_quantization.py TestQuantizeFx

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26424656

fbshipit-source-id: a0299b023f6ba7d98f5750724c517b0ecb987b35
2021-02-19 14:20:01 -08:00
Jerry Zhang
0c0de542be [quant][graphmode][fx] Guard the supported quantization type for add/mul (#52413)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52413

TODO: We'll need to add this guard for other ops as well

(Note: this ignores all push blocking failures!)

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_mul_add_fp16_config

Imported from OSS

Reviewed By: supriyar

Differential Revision: D26503348

fbshipit-source-id: 5aaba518742a516cc3521fd5f23f1a264d2973e2
2021-02-19 12:56:22 -08:00
Jerry Zhang
fb9f89507a [quant][graphmode][fx] Fix fp16 dynamic quant for functional linear (#52369)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52369

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D26491425

fbshipit-source-id: d2c2a70bf1bc43ac2b63ac4cf9ae9c07887f12e9
2021-02-18 23:05:30 -08:00
Vasiliy Kuznetsov
ad9746456e ns for fx: make unshadowed activation comparison work for N models (#52357)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52357

Refactor the NS for FX compare unshadowed activations API to be able
to work on N models and do arbitrary matching strategies.

We factor out a util which takes a model and a list of
nodes to extract weights for, with names to give the extracted
weights. The user can then call this util with a set
of nodes and names created in any way they want.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26487270

fbshipit-source-id: 1372ef07b5f3ddc7cebdfb2dee0221a2facd0527
2021-02-18 08:20:14 -08:00
Vasiliy Kuznetsov
a937d1cb16 ns for fx: make weights comparison work on N models (#52356)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52356

Refactor the NS for FX compare weights API to be able to
work on N models and do arbitrary matching strategies.

We factor out a util which takes a model and a list of
nodes to extract weights for, with names to give the extracted
weights.  The user can then call this util with a set
of nodes and names created in any way they want.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26487271

fbshipit-source-id: 0c2172a1b33d47565004a307aff14d205671add7
2021-02-18 08:20:09 -08:00
Vasiliy Kuznetsov
d903106bad [wip] ns for fx: add support for subgraph matching (#52130)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52130

We have patterns like (F.linear, F.relu) which need to match
to (toq.linear_relu).  So, we need to match subgraphs.

This PR does the following:
* defines a "subgraph" as (start_node, end_node). The current assumption
is that subgraphs are simple, there is always a path from start_node to
end_node, and we can ignore any non-input args/kwargs of these nodes
for the purposes of matching and copying things. An example one node
subgraph is (F.linear, F.linear).  An example two node subgraph
is (F.linear, F.relu).
* changes the matching logic to iterate over subgraphs instead of nodes
* changes the NS core APIs to use subgraph pairs instead of node pairs:
1. for weights, we match on the start node
2. for unshadowed activations, we observe the end nodes
3. for shadowed activations, we copy the subgraph of a to graph c

TODO(before review) write up better, not ready for review yet

Test Plan:
TODO before land: better test plan

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26403092

fbshipit-source-id: e49aaad4b02b8d60589435848bee422b8f41937a
2021-02-18 08:20:04 -08:00
Vasiliy Kuznetsov
3978ffb37a NS for FX: add test for a simple sparsenn model (#52092)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52092

Adds a very simple toy sparsenn model, and enables
its inspection with the new NS APIs.

Test Plan:
```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_sparsenn_compare_activations
python test/test_quantization.py TestFXNumericSuiteCoreAPIs.test_sparsenn_shadow
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26403095

fbshipit-source-id: 3c3650aca47186deb32f2b3f1d87a0716d1ad9d1
2021-02-18 08:17:57 -08:00
Zafar Takhirov
a07530e57f [quant] Factoring out the list of no_observers (#50459)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50459

Some of the custom modules cannot have the observers be inserted automatically. This PR factors out that list into a separate function.

Test is not required, as it is covered by the unittests for those modules.

(Note: this ignores all push blocking failures!)

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26092531

fbshipit-source-id: 1f89daf3a13ef31bc4e9058c3443559c65a05812
2021-02-17 12:38:30 -08:00
Zafar Takhirov
b8584b884e [quant] Quantizable MultiheadAttention (#49866)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49866

- Adds the `torch.nn.quantizable.MultiheadAttention`

The quantizable version can serve as a fully equivalent to `torch.nn.MultiheadAttention` module.
The main difference is that it allows for linear units observation after the `prepare` step in the quantization flow.

Note: The `from_observed` method (called during the `convert`) removes the `bias_k` and `bias_v` parameters, and resets them as attributes.
This is done to avoid an error of assigning a quantized tensor to the `torch.nn.Parameter`.

(Note: this ignores all push blocking failures!)

Test Plan:
```
python test/test_quantization.py TestQuantizedOps.test_custom_module_multi_head_attention
```

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25706179

fbshipit-source-id: e27ab641d8d1eccc64cf9e44343459331f89eea4
2021-02-17 12:36:30 -08:00
Vasiliy Kuznetsov
bfc7e28188 reland - ns for fx - stubs of the three APIs (compare weights, activations, activations with shadow) (#52302)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52302

Adds the basic functionality for the three Numeric Suite core APIs to work on FX models:
1. comparing weights
2. comparing activations, with same input fed to both models
3. comparing activations, with nodes of A shadowing nodes of B

Note: there are a lot of TODOs in the code, and some/most of the APIs and implementation details may change as we iterate.  This is just the first PR.

Test Plan:
We have unit test coverage for all of the APIs, for now this is with toy models:

```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Reviewed By: raghuramank100

Differential Revision: D26463013

Pulled By: vkuzo

fbshipit-source-id: e454115099ad18e4037d3c54986951cdffcab367
2021-02-16 19:59:32 -08:00
Natalia Gimelshein
eaddadd4f7 Revert D26403094: ns for fx - stubs of the three APIs (compare weights, activations, activations with shadow)
Test Plan: revert-hammer

Differential Revision:
D26403094 (37622db76a)

Original commit changeset: 9752331d4ae0

fbshipit-source-id: f0a32d443a29b25af33d90420dfd1bada40c917c
2021-02-14 15:09:16 -08:00
Vasiliy Kuznetsov
37622db76a ns for fx - stubs of the three APIs (compare weights, activations, activations with shadow) (#51669)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51669

Adds the basic functionality for the three Numeric Suite core APIs to work on FX models:
1. comparing weights
2. comparing activations, with same input fed to both models
3. comparing activations, with nodes of A shadowing nodes of B

Note: there are a lot of TODOs in the code, and some/most of the APIs and implementation details may change as we iterate.  This is just the first PR.

Test Plan:
We have unit test coverage for all of the APIs, for now this is with toy models:

```
python test/test_quantization.py TestFXNumericSuiteCoreAPIs
```

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D26403094

fbshipit-source-id: 9752331d4ae0105346d3da309b13c895b593b450
2021-02-12 17:52:21 -08:00
Vasiliy Kuznetsov
bfe6e23209 Early version of fx graph matcher for NS (#51588)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51588

Early version of utility to match nodes between graph A and graph B, for Numerical Suite for FX graph mode quantization.

The main goal of this utility is to reliably match the nodes of graph A to the nodes of graph B, and throw an easy to read error message.  This will be used in future PRs to create the APIs for matching activations.  It also could potentially be used to match weights.

Test Plan:
For now, we have bare bones test coverage on some toy models, and a single torchvision model.

```
python test/test_quantization.py TestFXGraphMatcher
```

Future PRs will add more testing.

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26403093

fbshipit-source-id: 60e318d51e6fefe65265488c4967629d946048ef
2021-02-12 17:50:13 -08:00
Vasiliy Kuznetsov
929b91a24d ns_eager: rename Logger I/O var names to logger_cls (#51359)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51359

`Logger` is the name of the base Logger class.  It's confusing that
it is also used as a variable name, which can represent this class
or its subclasses.  Renaming to `logger_cls` to make it clearer.

Test Plan:
```
python test/test_quantization.py TestEagerModeNumericSuite
```

Imported from OSS

Reviewed By: supriyar

Differential Revision: D26149577

fbshipit-source-id: a9c12f9446f66e5c683ab054b2a94aeb0cf9cc8a
2021-02-09 22:30:44 -08:00
Raghuraman Krishnamoorthi
14273126d2 Numeric Suite: Swap with shadow modules only for quantized part of model (#51052)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51052

Ensure that shadow modules are inserted only for quantized modules in a model. Removes redundant module insertion.
ghstack-source-id: 121041113

Test Plan: buck test caffe2/test:quantization --  'test_compare_model_stub_partial \(quantization\.test_numeric_suite\.TestEagerModeNumericSuite\)'

Reviewed By: vkuzo

Differential Revision: D26054016

fbshipit-source-id: 73fc2fd2f0239b0363f358c80e34566d06a0c7cb
2021-02-04 11:40:30 -08:00
Haichuan Yang
0c60922fb0 mem-efficient learnable fake quantization (#49315)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49315

Update the learnable fake-quantization to use c++ and CUDA kernels, and resolve some issues on using it with pytorch DDP.
The updated quantization operator have a different gradient calculation for scale and zero_point when the output is at the endpoints of clamp operation. The updated quantization operator calculates the gradient according to the gradient of the `clamp` function. This behavior is consistent with the gradient calculation for non-learnable fake-quant ops.
ghstack-source-id: 120821868

Test Plan:
# learnable_fake_quantization forward/backward op test
## Unit Test:
`buck test mode/dev-nosan -c fbcode.platform=platform009 //caffe2/test:quantization -- -v TestFakeQuantize`

## Benchmark Test:
`buck run mode/opt //caffe2/benchmarks/operator_benchmark/pt:quantization_test -- --operators FakeQuantizePerTensorOpBenchmark`

`buck run mode/opt //caffe2/benchmarks/operator_benchmark/pt:quantization_test -- --operators FakeQuantizePerChannelOpBenchmark`

### In **microseconds** (`1e-6` second),
References: P171624031
input size: [1, 3, 256, 256]
|                           | C++ Kernel | Non-backprop C++ Kernel |
|---------------------------|---------------|------------|-------------------------|---|
| Per Tensor CPU Forward    | 1372.123                | 1365.981 |
| Per Tensor Cuda Forward   | 84.586                 | 27.205|
| Per Channel CPU Forward   | 2306.668                | 2299.991|
| Per Channel Cuda Forward  | 154.742                 | 135.219 |
| Per Tensor CPU Backward   | 2544.617               | 581.268|
| Per Tensor Cuda Backward   | 304.529                 | 137.335|
| Per Channel CPU Backward   | 3328.188               |582.088 |
| Per Channel Cuda Backward  | 504.176                | 134.082|

input size: [1, 3, 512, 512]

|                           | C++ Kernel | Non-backprop C++ Kernel |
|---------------------------|---------------|------------|-------------------------|---|
| Per Tensor CPU Forward    | 5426.244                | 5726.440 |
| Per Tensor Cuda Forward   | 85.834                 | 26.871|
| Per Channel CPU Forward   | 9125.913                | 9118.152|
| Per Channel Cuda Forward  | 159.599                 | 145.117 |
| Per Tensor CPU Backward   | 14020.830               | 2214.864|
| Per Tensor Cuda Backward  | 285.525                 | 131.302|
| Per Channel CPU Backward  | 16977.141               |2104.345 |
| Per Channel Cuda Backward | 541.511                | 120.222|

# use learnable_fake_quantization in AI-denoising QAT:
f229412681

Reviewed By: raghuramank100

Differential Revision: D24479735

fbshipit-source-id: 5275596f3ce8200525f4d9d07d0c913afdf8b43a
2021-02-03 18:57:47 -08:00
Richard Barnes
b283ac6da4 "whitelist" -> "allowlist" (#51375)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/51375

Test Plan: Sandcastle tests

Reviewed By: iseeyuan

Differential Revision: D26150609

fbshipit-source-id: 1ca17bc8943598a42f028005d1f6d3f362fe2659
2021-02-02 16:20:34 -08:00
Xin (Annie) Guan
74ec9e7ccf compare_model_outputs_fx API implementation (#49266)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49266

compare_model_outputs_fx API implementation
ghstack-source-id: 120828880

Test Plan:
buck test mode/dev caffe2/test:quantization_fx -- 'test_compare_model_outputs_linear_static_fx'
buck test mode/dev caffe2/test:quantization_fx -- 'test_compare_model_outputs_conv_static_fx'
buck test mode/dev caffe2/test:quantization_fx -- 'test_compare_model_stub_linear_static_fx'
buck test mode/dev caffe2/test:quantization_fx -- 'test_compare_model_stub_conv_static_fx'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_outputs_linear_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_outputs_conv_static'

Reviewed By: vkuzo

Differential Revision: D25507933

fbshipit-source-id: 1b502b5eadb0fafbe9e8c2e843410bca03c63fd6
2021-02-02 10:43:25 -08:00
Xin (Annie) Guan
c354888e5d compare_model_stub_fx API implementation (#48951)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48951

compare_model_stub_fx API implementation
ghstack-source-id: 120817825

Test Plan:
buck test mode/dev caffe2/test:quantization_fx -- 'test_compare_model_stub_conv_static_fx'
buck test mode/dev caffe2/test:quantization_fx -- 'test_compare_model_stub_linear_static_fx'

Reviewed By: vkuzo

Differential Revision: D25379000

fbshipit-source-id: f1321d37b60b56b202e7d227e370ce13addb10cc
2021-02-01 22:16:14 -08:00
Supriya Rao
916af892b3 [quant][fx] Update name of packed weight attributes (#51259)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51259

Store the FQN of the module that is using the packed weights (the quantized op)

In the case of fusion we update the scope mapping to store the module path of the fused node.

Test Plan:
python test/test_quantization.py test_packed_weight_fused_op

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26117964

fbshipit-source-id: 9d929997baafb1c91063dd9786a451b0040ae461
2021-01-28 20:31:08 -08:00
Vasiliy Kuznetsov
05c8cd748d memory efficient per-channel fq: use it everywhere, delete old version (#51265)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51265

This PR is the cleanup after #51159. High level, we make the new
definition of fake_quant per channel be the definition used by autograd, but keep the old
function around as a thin wrapper to keep the user facing API the same.

In detail:

1. point fake_quantize_per_channel_affine's implementation to be fake_quantize_per_channel_affine_cachemask
2. delete the fake_quantize_per_channel_affine backward, autograd will automatically use the cachemask backward
3. delete all the fake_quantize_per_channel_affine kernels, since they are no longer used by anything

Test Plan:
```
python test/test_quantization.py TestFakeQuantize
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26120957

fbshipit-source-id: 264426435fabd925decf6d1f0aa79275977ea29b
2021-01-28 19:42:25 -08:00
Vasiliy Kuznetsov
267e243064 fake_quant: more memory efficient per-channel backward (#51255)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51255

This is the same as #50561, but for per-channel fake_quant.

TODO before land write up better

Memory and performance impact (MobileNetV2): TODO

Performance impact (microbenchmarks): https://gist.github.com/vkuzo/fbe1968d2bbb79b3f6dd776309fbcffc
* forward pass on cpu: 512ms -> 750ms (+46%)
* forward pass on cuda: 99ms -> 128ms (+30%)
* note: the overall performance impact to training jobs should be minimal, because this is used for weights, and relative importance of fq is dominated by fq'ing the activations
* note: we can optimize the perf in a future PR by reading once and writing twice

Test Plan:
```
python test/test_quantization.py TestFakeQuantize.test_forward_per_channel_cachemask_cpu
python test/test_quantization.py TestFakeQuantize.test_forward_per_channel_cachemask_cuda
python test/test_quantization.py TestFakeQuantize.test_backward_per_channel_cachemask_cpu
python test/test_quantization.py TestFakeQuantize.test_backward_per_channel_cachemask_cuda
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26117721

fbshipit-source-id: 798b59316dff8188a1d0948e69adf9e5509e414c
2021-01-28 19:39:35 -08:00
Jerry Zhang
7097c0d4f3 [quant][graphmode][fx] Add support for functional conv1d and conv3d (#51155) (#51254)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51254

This PR added support for quantizing functional conv1d, conv3d,  conv1d_relu and conv3d_relu

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_functional_conv

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26116172

fbshipit-source-id: 56e7d799c11963fe59ee3a1b6eb23f52007b91dc
2021-01-28 14:32:32 -08:00
Supriya Rao
288b94a8ee [quant][fx] Make scale, zero_point buffers in the model, use FQN (for quantize_per_tensor ops) (#51171)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51171

Following up on previous PR, this PR makes scale and zero_point for quantize_per_tensor to be
registered as buffers in the module.
Currently the dtype is still stored as attr (not registered as buffer) since we can only register tensor types.

Test Plan:
python test/test_quantization.py test_qparams_buffers

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26092964

fbshipit-source-id: a54d914db7863402f2b5a3ba2c8ce8b27c18b47b
2021-01-28 08:35:46 -08:00
Supriya Rao
4c3f59b70e [quant][fx] Make scale, zero_point buffers in the model and use FQN (for quantized ops) (#51166)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51166

Currently scale and zero_point values are stored as constant values in the graph.
This prevents these values from being updated in the graph and also does not enable saving
these values to state_dict

After this PR we store scale/zero_point values for quantized ops as buffers in the root module
and createe get_attr nodes for them in the graph.

We also use the FQN of the module where the quantized ops are present to name these attributes so
that they can be uniquely  identified and mapped to quantized ops.

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_qparams_buffers

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26092965

fbshipit-source-id: b549b2d3dccb45c5d38415ce95a09c26f5bd590b
2021-01-28 08:35:42 -08:00
Supriya Rao
096adf4b8b [quant][fx] Scope support for call_function in QuantizationTracer (#51086)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51086

Previously we only supported getting scope for call_module and custom qconfig dict for call_module.
This PR extends the Scope class to record the scope for all node types.
For call_function qconfig if module_name is specified it takes precedence over function qconfig.

Test Plan:
python test/test_quantization.py test_qconfig_for_call_func

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26077602

fbshipit-source-id: 99cdcdedde2280e51812db300e17d4e6d8f477d2
2021-01-28 08:32:24 -08:00
Vasiliy Kuznetsov
0335222a4a memory efficient fq: use it everywhere, delete the old version (#51159)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51159

This PR is the cleanup after #50561. High level, we make the new
definition of fake_quant be the definition used by autograd, but keep the old
function around as a thin wrapper to keep the user facing API the same.

In detail:
1. point `fake_quantize_per_tensor_affine`'s implementation to be `fake_quantize_per_tensor_affine_cachemask`
2. delete the `fake_quantize_per_tensor_affine` backward, autograd will automatically use the cachemask backward
3. delete all the `fake_quantize_per_tensor_affine` kernels, since they are no longer used by anything

Test Plan:
```
python test/test_quantization.py TestFakeQuantize
```

performance testing was done in the previous PR.

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26090869

fbshipit-source-id: fda042881f77a993a9d15dafabea7cfaf9dc7c9c
2021-01-27 19:39:05 -08:00
Vasiliy Kuznetsov
983b8e6b62 fake_quant: add a more memory efficient version (#50561)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50561

Not for review yet, a bunch of TODOs need finalizing.

tl;dr; add an alternative implementation of `fake_quantize` which saves
a ask during the forward pass and uses it to calculate the backward.

There are two benefits:

1. the backward function no longer needs the input Tensor, and it can be
gc'ed earlier by autograd.  On MobileNetV2, this reduces QAT overhead
by ~15% (TODO: link, and absolute numbers).  We add an additional mask Tensor
to pass around, but its size is 4x smaller than the input tensor. A
future optimization would be to pack the mask bitwise and unpack in the
backward.

2. the computation of `qval` can be done only once in the forward and
reused in the backward. No perf change observed, TODO verify with better
matrics.

TODO: describe in more detail

Test Plan:
OSS / torchvision / MobileNetV2
```
python references/classification/train_quantization.py
  --print-freq 1
  --data-path /data/local/packages/ai-group.imagenet-256-smallest-side/prod/
  --output-dir ~/nfs/pytorch_vision_tests/
  --backend qnnpack
  --epochs 5
TODO paste results here
```

TODO more

Imported from OSS

Reviewed By: ngimel

Differential Revision: D25918519

fbshipit-source-id: ec544ca063f984de0f765bf833f205c99d6c18b6
2021-01-27 19:36:04 -08:00
Mike Ruberry
f7e90cf311 Revert D26089965: [quant][graphmode][fx] Add support for functional conv1d and conv3d
Test Plan: revert-hammer

Differential Revision:
D26089965 (dd1a97b3ae)

Original commit changeset: 4aea507d05b7

fbshipit-source-id: f54184cafb9dd07858683489d8bd147474e7e4b3
2021-01-27 13:27:10 -08:00
Jerry Zhang
dd1a97b3ae [quant][graphmode][fx] Add support for functional conv1d and conv3d (#51155)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51155

This PR added support for quantizing functional conv1d, conv3d,  conv1d_relu and conv3d_relu

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_functional_conv

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26089965

fbshipit-source-id: 4aea507d05b744807e993f6d3711ab308fb7591b
2021-01-27 12:00:35 -08:00
Jerry Zhang
d3ec204ef2 [quant][graphmode][fx] Add functional conv2d + relu (#51079)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51079

Added support for functional conv2d + relu, will add conv1d and conv3d in future PR

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_functional_conv

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D26089964

fbshipit-source-id: 8703de17de1469f7076651c386c83fb5922a56eb
2021-01-27 11:20:55 -08:00
Vasiliy Kuznetsov
e9ffad088f numeric suite: add types to eager (#51168)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51168

Adds types to function I/O for numeric suite.  This is for readability
and static type checking with mypy.

Test Plan:
```
mypy torch/quantization/
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D26092454

fbshipit-source-id: d37cf61e4d9604f4bc550b392f55fb59165f7624
2021-01-27 10:40:49 -08:00
yanli924
ada916675f update HistogramObserver to be scriptable (#51081)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51081

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

fix tests in TestQuantizeJitOps

Test Plan:
Imported from OSS
python test/test_quantization.py

Reviewed By: raghuramank100

Differential Revision: D26038759

Pulled By: lyoka

fbshipit-source-id: 0977ba7b8b26a9f654f20f5c698a7a20ec078c35
2021-01-27 07:27:03 -08:00
Vasiliy Kuznetsov
f8eefbdf7a fake_quant: fix device affinity and buffer resizing for state_dict (#50868)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50868

Ensures that `FakeQuantize` respects device affinity when loading from
state_dict, and knows how to resize scale and zero_point values
(which is necessary for FQ classes wrapping per channel observers).

This is same as https://github.com/pytorch/pytorch/pull/44537, but for
`FakeQuantize`.

Test Plan:
```
python test/test_quantization.py TestObserver.test_state_dict_respects_device_affinity
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25991570

fbshipit-source-id: 1193a6cd350bddabd625aafa0682e2e101223bb1
2021-01-25 13:50:28 -08:00
Jerry Zhang
28869d5a80 [quant][graphmode][fx] Add support for quantizing functional linear + {functional relu/module relu} (#50975)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/50975

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D26032532

fbshipit-source-id: a084fb4fd711ad52b2da1c6378cbcc2b352976c6
2021-01-25 12:49:58 -08:00
Vasiliy Kuznetsov
ac8e90fa6d quantization: Linear + BatchNorm1d fusion (#50748)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50748

Adds support for Linear + BatchNorm1d fusion to quantization.

This is a redo of dreiss's https://github.com/pytorch/pytorch/pull/37467, faster
to copy-paste it than rebase and deal with conflicts.

Test Plan:
```
python test/test_quantization.py TestFusion.test_fusion_linear_bn_eval
```

Imported from OSS

Reviewed By: supriyar

Differential Revision: D25957432

fbshipit-source-id: 24e5b760f70186aa953ef65ab0182770e89495e4
2021-01-20 12:59:02 -08:00
Zafar
7d28f1c81d [quant][refactor] Minor refactor of some typos (#50304)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50304

Does not include any functional changes -- purely for fixing minor typos in the `fuser_method_mappings.py`

Test Plan: Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25857248

Pulled By: z-a-f

fbshipit-source-id: 3f9b864b18bda8096e7cd52922dc21be64278887
2021-01-12 15:23:13 -08:00
Zafar
39aac65430 [quant][bug] Fixing the mapping getter to return a copy (#50297)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50297

Current implementation has a potential bug: if a user modifies the quantization mappings returned by the getters, the changes will propagate.
For example, the bug will manifest itself if the user does the following:

```
my_mapping = get_default_static_quant_module_mappings()
my_mapping[nn.Linear] = UserLinearImplementation
model_A = convert(model_A, mapping=my_mapping)

default_mapping = get_default_static_quant_module_mappings()
model_B = convert(model_B, mapping=default_mapping)
```

In that case the `model_B` will be quantized with with the modified mapping.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25855753

Pulled By: z-a-f

fbshipit-source-id: 0149a0c07a965024ba7d1084e89157a9c8fa1192
2021-01-12 15:19:39 -08:00
Jerry Zhang
55ac7e53ae [quant][graphmode][fx] Support preserved_attributes in prepare_fx (#50306)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/50306

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D25857747

fbshipit-source-id: fac132fb36ed9cf207aea40429b5bc3f7c72c35d
2021-01-11 12:10:02 -08:00
Jerry Zhang
f10e7aad06 [quant][graphmode][fx] Scope support for call_method in QuantizationTracer (#50173)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50173

Previously we did not set the qconfig for call_method node correctly since it requires us to know
the scope (module path of the module whose forward graph contains the node) of the node. This
PR modifies the QuantizationTracer to record the scope information and build a map from call_method
Node to module path, which will be used when we construct qconfig_map

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_qconfig_for_call_method

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25818132

fbshipit-source-id: ee9c5830f324d24d7cf67e5cd2bf1f6e0e46add8
2021-01-11 10:43:58 -08:00
Zafar
e12008d110 [quant] Mapping for the _LinearWithBias (#49964)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49964

`torch.nn.modules.linear._LinearWithBias` is only used in the transformers, and is completely identical to the `torch.nn.Linear`.
This PR creates a mapping so that this module would be treated the same as the Linear.

Test Plan:
```
python test/test_quantization.py TestDynamicQuantizedModule TestStaticQuantizedModule
```

Differential Revision: D25731589

Reviewed By: jerryzh168

Pulled By: z-a-f

fbshipit-source-id: 1b2697014e250e97d3010cdb542f9d130b71fbc3
2021-01-07 13:57:29 -08:00
Jerry Zhang
f6f0fde841 [reland][quant][graphmode][fx] Standalone module support {input/output}_quantized_idxs (#49754) (#50058)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50058

This PR adds the support for {input/output}_quantized_idxs for standalone module.

if input_quantized_idxs = [] and output_quantized_idxs = [], the standalone module will be expecting float
input and produce float output, and will quantize the input and dequantize output internally

if input_quantized_idxs = [0] and otuput_qiuantized_idxs = [0], the standalone module will be expecting quantized
input and produce quantized output, the input will be quantized in the parent module, and output will be dequantized
in the parent module as well, this is similar to current quantized modules like nn.quantized.Conv2d

For more details, please see the test case

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_standalone_module

Imported from OSS

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25768910

fbshipit-source-id: 96c21a3456cf192c8f1400afa4e86273ee69197b
2021-01-05 20:27:46 -08:00
Vasiliy Kuznetsov
04e86be1a2 eager quant: fix error with removing forward hooks (#49813)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49813

https://github.com/pytorch/pytorch/issues/49739 reports a crash
where removing forward hooks results in a

```
RuntimeError: OrderedDict mutated during iteration
```

Unfortunately I cannot repro this inside the PyTorch module, but the issue
author has a good point and and we should not mutate the dict inside
of the iteration.

Test Plan:
```
// test plan from https://github.com/pytorch/pytorch/pull/46871 which
// originally added this
python test/test_quantization.py TestEagerModeQATOps
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25698725

fbshipit-source-id: 13069d0d5017a84038c8f7be439a3ed537938ac6
2021-01-05 11:00:20 -08:00
Vasiliy Kuznetsov
44c17b28c6 quant: nice error message on convtranspose with per-channel weight (#49899)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49899

Per channel weights observer in conv transpose is not supported yet.  Adding an
error message which fails instantly instead of making the user wait until after
calibration/training finishes.

Test Plan:
```
python test/test_quantization.py TestPostTrainingStatic.test_convtranspose_per_channel_fails_early
python test/test_quantization.py TestQuantizeFx.test_convtranspose_per_channel_fails_early
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25717151

fbshipit-source-id: 093e5979030ec185e3e0d56c45d7ce7338bf94b6
2021-01-05 09:38:57 -08:00
Vasiliy Kuznetsov
72306378b4 quant: ensure observers do not crash for empty Tensors (#49800)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49800

Ensures that having a Tensor with 0 elements does not crash observers.
Note: it's illegal to pass Tensors with 0 elements to reductions such
as min and max, so we gate this out before the logic hits min/max.

This should not be hit often in practice, but it's coming up
during debugging of some RCNN models with test inputs.

Test Plan:
```
python test/test_quantization.py TestObserver.test_zero_numel
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25693230

fbshipit-source-id: d737559697c98bd923356edacba895835060bb38
2021-01-05 09:35:47 -08:00
Zafar
04a8412b86 [quant] Quantizable LSTM (#49671)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49671

- Introduces the `torch.nn.quantizable` namespace
- Adds the `torch.nn.quantizable.LSTM` module

The point of the `quantizable` namespace is to segregate the purely quantized modules with the modules that could be quantized through a normal quantization flow, but are not using the quantized kernels explicitly.
That means the quantizable modules are functionally and numerically equivalent to the FP ones and can be used instead of the FP ones without any loss.

The main difference between the `torch.nn.LSTM` and the `torch.nn.quantizable.LSTM` is that the former one does not support observation for the linear layers, because all the computation is internal to the `aten` namespace.
The `torch.nn.quantizable.LSTM`, however, uses explicit linear layers that can be observed for further quantization.

Test Plan: Imported from OSS

Differential Revision: D25663870

Reviewed By: vkuzo

Pulled By: z-a-f

fbshipit-source-id: 70ff5463bd759b9a7922571a5712d3409dfdfa06
2020-12-30 15:21:38 -08:00
Richard Barnes
14edc726d9 Clean up some type annotations in caffe2/torch/quantization (#49942)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49942

Upgrades type annotations from Python2 to Python3

Test Plan: Sandcastle tests

Reviewed By: vkuzo

Differential Revision: D25717551

fbshipit-source-id: 1b63dc485ecf6641641b05f7ce095ae1d2d87346
2020-12-29 15:43:50 -08:00
Vasiliy Kuznetsov
ea558b2135 fx quant: hook up ConvTranspose{n}d (#49717)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49717

Quantization of `ConvTranpose{n}d` is supported in Eager mode. This PR
adds the support for FX graph mode.

Note: this currenlty only works in `qnnpack` because per-channel weights
are not supported by quantized conv transpose. In a future PR we should throw
an error when someone tries to quantize a ConvTranspose model with per-channel
weight observers until this is fixed.

Test Plan:
```
python test/test_quantization.py TestQuantizeFxOps.test_conv_transpose_1d
python test/test_quantization.py TestQuantizeFxOps.test_conv_transpose_2d
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25674636

fbshipit-source-id: b6948156123ed55db77e6337bea10db956215ae6
2020-12-28 14:27:07 -08:00
Samuel Marks
e6779d4357 [*.py] Rename "Arguments:" to "Args:" (#49736)
Summary:
I've written custom parsers and emitters for everything from docstrings to classes and functions. However, I recently came across an issue when I was parsing/generating from the TensorFlow codebase: inconsistent use of `Args:` and `Arguments:` in its docstrings.

```sh
(pytorch#c348fae)$ for name in 'Args:' 'Arguments:'; do
    printf '%-10s %04d\n' "$name" "$(rg -IFtpy --count-matches "$name" | paste -s -d+ -- | bc)"; done
Args:      1095
Arguments: 0336
```

It is easy enough to extend my parsers to support both variants, however it looks like `Arguments:` is wrong anyway, as per:

  - https://google.github.io/styleguide/pyguide.html#doc-function-args @ [`ddccc0f`](https://github.com/google/styleguide/blob/ddccc0f/pyguide.md)

  - https://chromium.googlesource.com/chromiumos/docs/+/master/styleguide/python.md#describing-arguments-in-docstrings @ [`9fc0fc0`](https://chromium.googlesource.com/chromiumos/docs/+/9fc0fc0/styleguide/python.md)

  - https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html @ [`c0ae8e3`](https://github.com/sphinx-contrib/napoleon/blob/c0ae8e3/docs/source/example_google.rst)

Therefore, only `Args:` is valid. This PR replaces them throughout the codebase.

PS: For related PRs, see tensorflow/tensorflow/pull/45420

PPS: The trackbacks automatically appearing below are sending the same changes to other repositories in the [PyTorch](https://github.com/pytorch) organisation.

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

Reviewed By: albanD

Differential Revision: D25710534

Pulled By: soumith

fbshipit-source-id: 61e8ff01abb433e9f78185c2d1d0cbd7c22c1619
2020-12-28 09:34:47 -08:00
Mike Ruberry
46cf6d332f Revert D25684692: [quant][graphmode][fx] Standalone module support {input/output}_quantized_idxs
Test Plan: revert-hammer

Differential Revision:
D25684692 (89b4899ea5)

Original commit changeset: 900360e01c0e

fbshipit-source-id: 8b65fa8fbc7b364fbddb5f23cc696cd9b7db98cd
2020-12-24 15:50:52 -08:00
Jerry Zhang
89b4899ea5 [quant][graphmode][fx] Standalone module support {input/output}_quantized_idxs (#49754)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49754

This PR adds the support for {input/output}_quantized_idxs for standalone module.

if input_quantized_idxs = [] and output_quantized_idxs = [], the standalone module will be expecting float
input and produce float output, and will quantize the input and dequantize output internally

if input_quantized_idxs = [0] and otuput_qiuantized_idxs = [0], the standalone module will be expecting quantized
input and produce quantized output, the input will be quantized in the parent module, and output will be dequantized
in the parent module as well, this is similar to current quantized modules like nn.quantized.Conv2d

For more details, please see the test case

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_standalone_module

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D25684692

fbshipit-source-id: 900360e01c0e35b26fe85f4a887dc1fd6f7bfb66
2020-12-23 22:36:57 -08:00
Jerry Zhang
f474ffa1a9 [quant][graphmode][fx] Change standalone module api (#49719)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49719

We find there are multiple use cases for standalone module, one use case requires standalone module
to produce a module that takes float Tensor as input and outputs a float Tensor, the other needs to
produce a modulee that takes quantized Tensor as input and outputs a quantized Tensor.

This is similar to `quantized_input_idxs` and `quantized_output_idxs` so we want to nest
prepare_custom_config_dict in the standalone module configuration, for maximum flxibility we also
include qconfig_dict for stand alone module as well in case user needs to have special qconfig_dict for
the standalone module in the future.

Changed from
```python
prepare_custom_config_dict =
{
  "standalone_module_name": ["standalone_module"],
   "standalone_module_class": [StandaloneModule]
 }
```
to
```python
prepare_custom_config_dict =
{
  "standalone_module_name": [("standalone_module", qconfig_dict1, prepare_custom_config_dict1)],
  "standalone_module_class": [(StandaloneModule, qconfig_dict2, prepare_custom_config_dict2)]
 }
```
The entries in the config are:
1. name/module_class
2. optional qconfig_dict, when it is None, we'll use {"": qconfig} where qconfig is the one from parent qconfig_dict
3. optional prepare_custom_config_dict, when it is None, we'll use default value of prepare_custom_config_dict for prepare API (None)

Test Plan:
python test/test_quantization.py TestQuantizeFx.test_standalone_module

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D25675704

fbshipit-source-id: 0889f519a3e55a7a677f0e2db4db9a18d87a93d4
2020-12-22 21:58:40 -08:00
Vasiliy Kuznetsov
de07d07600 fx quant: improve types on convert (#49688)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49688

Adds more types on FX quantize convert, fixing things as they
are uncovered by mypy.

Test Plan:
```
mypy torch/quantization
python test/test_quantization.py TestQuantizeFx
python test/test_quantization.py TestQuantizeFxOps
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25667231

fbshipit-source-id: 262713c6ccb050a05e3119c0457d0335dde82d25
2020-12-22 16:53:23 -08:00
Vasiliy Kuznetsov
19f972b696 fx quant: do not observe bias on F.linear (#49628)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49628

Ensures that linear bias is not observed in a `F.linear` call. This should
be a small speedup in PTQ, and will change numerics (in a good way) for
QAT if someone is using `F.linear`.

Note: the implementation is slightly more verbose compared to conv
because bias is a keyword argument in Linear.

Test Plan:
```
python test/test_quantization.py TestQuantizeFxOps.test_linear_functional_bias_not_observed
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25653532

fbshipit-source-id: c93501bf6b55cbe4a11cfdad6f79313483133a39
2020-12-22 16:53:21 -08:00
Vasiliy Kuznetsov
c3a7591cef fx quant: do not observe bias on F.conv (#49623)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49623

(not ready for review)

Ensures that conv bias is not observed in a `F.conv{n}d` call.

Test Plan: Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25652856

fbshipit-source-id: 884f87be1948d3e049a557d79bec3c90aec34340
2020-12-22 16:49:50 -08:00
Vasiliy Kuznetsov
edce6b138d fx quant: fix types on _find_quants (#49616)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49616

Add types to `_find_quants` I/O and fix resulting errors,
needed for an upcoming bug fix.

Test Plan:
```
mypy torch/quantization
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25645719

fbshipit-source-id: 4bf788b55fd4fd086c83a4438b9c2df22b9cff49
2020-12-21 21:05:57 -08:00
Vasiliy Kuznetsov
7c90b20f38 fx quant: add types to observed_module.py (#49607)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49607

Readability

Test Plan:
```
mypy torch/quantization
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25643895

fbshipit-source-id: b4b8741b07ac4827c3bacd2084df81fbfdd0c2d5
2020-12-21 21:05:53 -08:00
Vasiliy Kuznetsov
9d5d193704 fx quant: types for fusion_patterns.py (#49606)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49606

Adds more types, for readability.

Test Plan:
```
mypy torch/quantization
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25643894

fbshipit-source-id: 4aad52fe4e59ad74b6e0e3acd0f98fba91561a29
2020-12-21 21:05:49 -08:00
Raghuraman Krishnamoorthi
f7a085af98 Dynamic GRU quantization support (#49448)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49448

ghstack-source-id: 118982171

Test Plan:
buck test caffe2/test:quantization --  'test_qlstmGRU \(quantization\.test_quantized_op\.TestDynamicQuantizedRNNOp\)' --print-passing-details
buck test caffe2/test:quantization --  'test_quantized_rnn \(quantization\.test_quantize\.TestPostTrainingDynamic\)' --print-passing-details
buck test caffe2/test:quantization --  'test_qrnncell \(quantization\.test_quantized_op\.TestDynamicQuantizedRNNOp\)' --run-disabled --print-passing-details

Reviewed By: vkuzo

Differential Revision: D25579815

fbshipit-source-id: 413cc8888eb8058230b94c9576d2fa54b0ed1416
2020-12-21 12:36:59 -08:00
Jerry Zhang
5cde23fdd4 [quant][graphmode][fx] Allow user to specify qconfig for call_method (#49621)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49621

This adds support to configure qconfig for a call_method, e.g. x.chunk, this will help workaround
a problem in our internal model.

TODO: since call_method is also a string and we flatten the qconfig, might need to resolve namespace conflict between
call_method and module_name
TODO: Add scope support to set the qconfig for call_method correctly with original qconfig

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25651828

fbshipit-source-id: 82d66b121d37c8274fd481b6a2e9f9b54c5ca73d
2020-12-18 20:21:52 -08:00
Jerry Zhang
4b85239532 [quant][eagermode][fix] Fix quantization for DeQuantStub (#49428)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49428

Previously dequantstub will be swapped with nn.quantized.DeQuantize regardless of qconfig
reason is we skipped attaching qconfig for DeQuantStub to avoid adding fake quantize module to it
but the correct fix is to skip it in insert observers, this PR fixes the issue.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25569991

fbshipit-source-id: d44a08c6e64c7a49509687dc389b57de1cbb878c
2020-12-17 14:42:40 -08:00
Jerry Zhang
ede0b169ea [quant][be] Add typing for quantization_mappings.py (#49179)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/49179

Test Plan: Imported from OSS

Reviewed By: vkuzo, wat3rBro

Differential Revision: D25470520

fbshipit-source-id: 16e35fec9a5f3339860bd2305ae8ffdd8e2dfaf7
2020-12-16 21:36:00 -08:00
Vasiliy Kuznetsov
82ac6c75af fx quant: make sure observer is inserted before a quantized output (#49420)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49420

Before: if an output was marked as quantized, it could actually not
be quantized, if the previous node was not quantized.

After: if an output was marked as quantized, it will be quantized
regardless of the quantization status of the previous node.

Test Plan:
```
python test/test_quantization.py TestQuantizeFxOps.test_quant_output_always_observed
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25566834

fbshipit-source-id: 84755a1605fd3847edd03a7887ab9f635498c05c
2020-12-16 18:53:37 -08:00
Vasiliy Kuznetsov
84506e0316 fx quant: fix fq when input is quantized and node does not need fq (#49382)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49382

Fixes an edge case.  If the input to the graph is quantized and the
first node does not need activation observation, makes sure that
the observer is not inserted.

Test Plan:
```
python test/test_quantization.py TestQuantizeFxOps.test_int8_input_no_unnecessary_fq
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25551041

fbshipit-source-id: a6cba235c63ca7f6856e4128af7c1dc7fa0085ea
2020-12-16 18:53:33 -08:00
Vasiliy Kuznetsov
7542076097 fx quant: do not insert observers at quantized inputs (#49239)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49239

Context: the existing implementation of `quantized_input_idxs` is convert-only.
Therefore, observers are inserted between the input and the first
quantized node.  This is a problem during QAT, because the initial
input is a fake_quant, and it starts with scale=1 and zp=0.  This does
not match the quantization parameters of the graph input, which can
lead to incorrect numerics.

Fix: do not insert observer for a quantized input.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25499486

fbshipit-source-id: 303b49cc9d95a9fd06fef3b0859c08be34e19d8a
2020-12-16 18:53:30 -08:00
Vasiliy Kuznetsov
92df8706a0 fx quant: move {input|output}_quantized_idxs cfg from convert to prepare (#49238)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/49238

Moves the `input_quantized_idxs` and `output_quantized_idxs` options
from the convert config to the prepare config.  This is done because
these operations are related to placing observers, which is numerics
changing during QAT.

The next PR will adjust the behavior of `input_quantized_idxs` in
prepare in QAT to prevent placing a fake_quant at the input if the
input is marked quantized.  Placing a fake_quant there can lead to
numerical inaccuracies during calibration, as it would start with
scale=1 and zp=0, which may be different from the quantization
parameters of the incoming quantized input.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25498762

fbshipit-source-id: 17ace8f803542155652b310e5539e1882ebaadc6
2020-12-16 18:53:27 -08:00
Jerry Zhang
2a3bb1cea0 [quant][graphmode][fx][fix] Fix typo in fusion (#49183)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/49183

Test Plan: Imported from OSS

Reviewed By: hx89

Differential Revision: D25473367

fbshipit-source-id: 0cd5e6769eeea0923dd104ea90b0192e3475b3ad
2020-12-11 12:14:53 -08:00
Jerry Zhang
882eb0f646 [quant][graphmode][fx] Add support for dynamic quant for RNN and RNNCell (#49126)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/49126

Test Plan:
python test/test_quantization.py TestQuantizeFxOps.test_rnn
python test/test_quantization.py TestQuantizeFxOps.test_rnn_cell

Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D25449047

fbshipit-source-id: 532bf9ad2839958dde8c6f2d9399fac96b2b8bd4
2020-12-10 18:11:40 -08:00
Jerry Zhang
993ce4b206 [quant][graphmode][fx] Add MatchAllNode in pattern matching (#48979)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48979

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25385459

fbshipit-source-id: 43adffc9e2242d099cecd38d1902f9900158f51e
2020-12-08 18:53:55 -08:00
Vasiliy Kuznetsov
d033e185ed fx quant: move more functions to utils (#48908)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48908

No logic change, improving readability

Test Plan:
CI

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25363080

fbshipit-source-id: 1d73a875bd7abf671b544ebc835432fea5306dc3
2020-12-08 15:37:04 -08:00
Vasiliy Kuznetsov
2668ea8087 fx quant: move qconfig utils to utils file (#48907)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48907

Improving readability

Test Plan:
CI

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25363078

fbshipit-source-id: 6b0161db14ccf8c3b47edf4fc760ca9a399254b2
2020-12-08 15:37:00 -08:00
Vasiliy Kuznetsov
17e71509a6 fx quant: quick cleanup for model_device (#48906)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48906

As titled, removing some code which is no longer
needed after refactors.

Test Plan:
CI

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25363079

fbshipit-source-id: 9e4bcf63f4f1c2a2d3fb734688ba593d72495349
2020-12-08 15:35:18 -08:00
Vasiliy Kuznetsov
0923d19601 fx quant: add types to quantization_patterns (#48851)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48851

Adding typing to improve readability.

Note: this uncovered a few missing return statements, we should
fix that before landing.

Test Plan:
```
mypy torch/quantization/
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25338644

fbshipit-source-id: 0ac4405db05fdd2737bc3415217bc1937c2db684
2020-12-05 08:47:18 -08:00
Vasiliy Kuznetsov
fa5f7d87bf fx quant: add typing for fuser (#48844)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48844

Add types to function I/O for `Fuser` to improve readability

Test Plan:
```
mypy torch/quantization/
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25337314

fbshipit-source-id: e5074d71c7834f24975169d36bf49357e53650ff
2020-12-05 08:44:32 -08:00
Jerry Zhang
fadec77c30 [quant][fx][graphmode] Renable torchvision test (#48602)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48602

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25224917

fbshipit-source-id: efc73f425253c4eb7ae51064b6760416097f0437
2020-12-04 10:13:38 -08:00
Vasiliy Kuznetsov
f5d94244b2 fx quant: more typehints, part 3 (#48794)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48794

Adds typehints to function I/O in `torch/quantization/quantize_fx.py`,
for readability.

Test Plan:
```
mypy torch/quantization/
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25307084

fbshipit-source-id: 67bdf95b78836dcabc7d829e1854ca5b8ceb8346
2020-12-03 19:28:16 -08:00
Vasiliy Kuznetsov
54da2dadd8 fx quant: more typehints, part 2 (#48792)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48792

Adds some more typehints throughout quantization/fx/quantize.py,
to help with readability.

Test Plan:
```
mypy torch/quantization/fx/quantize.py
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25306683

fbshipit-source-id: fc38b885a2cb5bf2c6d23b6305658704c6eb7811
2020-12-03 19:28:12 -08:00
Vasiliy Kuznetsov
f5bcf45e3b fx quant: add more typehints (#48774)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48774

Adds some more typehints throughout `quantization/fx/quantize.py`.

More are needed, ran out of time for now, we can continue in
future PRs.

Test Plan:
```
mypy torch/quantization/fx/quantize.py
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25295836

fbshipit-source-id: 4029aa8ea5b07ce9a57e4be6a66314d7a4e19585
2020-12-03 19:28:09 -08:00
Vasiliy Kuznetsov
c98c617b44 fx quant: clean up functions in _prepare (#48773)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48773

Makes util functions in `_prepare` have no side effects,
all dependencies are now in arguments.

Note: arg names are added in order as they appeared in function
code. It's not the most readable, but the lowest risk. This can
be cleaned up in future PRs if needed.

```
python test/test_quantization.py TestQuantizeFx
```

Test Plan: Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25295839

fbshipit-source-id: 60c687f6b64924473f969541c8703118e4f7d16e
2020-12-03 19:28:06 -08:00
Vasiliy Kuznetsov
536352e86f fx quant: clean up functions in _generate_qconfig_map (#48772)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48772

Makes util functions in `_generate_qconfig_map` have no side
effects, all dependencies are now in arguments.

Test Plan:
```
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25295837

fbshipit-source-id: 49399abef626234e34bb5ec8c6d870da3c1760e7
2020-12-03 19:25:38 -08:00
Jerry Zhang
ea573ea944 [qunat][graphmode][fx] Standalone module takes float as input and output (#48671)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48671

Standalone module might be called separately so it's better to use float
as interface.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25256184

fbshipit-source-id: e209492a180ce1f81f31c8d6057956a74bad20b1
2020-12-02 20:34:25 -08:00
Lei Mao
0db73460db [quantization] fix run_arg tiny bug (#48537)
Summary:
This fix allows the calibration function to take in more than one positional argument.

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

Reviewed By: zhangguanheng66

Differential Revision: D25255764

Pulled By: jerryzh168

fbshipit-source-id: 3ce20dbed95fd26664a186bd4a992ab406bba827
2020-12-02 10:07:33 -08:00
Vasiliy Kuznetsov
f80aaadbae fx quantization: add option to leave graph inputs and/or outputs quantized (#48624)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48624

Before this PR, there was an assumption that all graph inputs
and outputs are in floating point, with some exceptions for
`standalone_module`.

This PR adds an option to specify either inputs or outputs
as being quantized.

This is useful for incremental migrations of models using Eager mode.

Test Plan: Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25231833

fbshipit-source-id: 9f9da17be72b614c4c334f5c588458b3e726ed17
2020-12-01 10:39:51 -08:00
Vasiliy Kuznetsov
bc2c1d7d59 quant: make each line of fx/quantize.py <=80 chars (#48357)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48357

Cleans up the long lines in `torch/quantization/fx/quantize.py`
to fit the 80 character limit, so it's easier to read and looks
better on FB's tools.

In the future we can consider adding a linter for this.

Test Plan:
CI

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25140833

fbshipit-source-id: 78605d58eda0184eb82f510baec26685a34870e2
2020-11-25 09:04:23 -08:00
Vasiliy Kuznetsov
1d984410fb quant fx: fix typo (#48356)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48356

As titled

Test Plan:
CI

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25140834

fbshipit-source-id: e22f8d1ae77c7eb2ec8275b5fbca7dc5e503a4ca
2020-11-25 09:04:20 -08:00
Vasiliy Kuznetsov
8581c02a3f quant: add type annotations on quantization.fx.Quantizer matches (#48350)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48350

As titled, continuing to incrementally type quantization.fx.Quantizer.

Test Plan:
```
mypy torch/quantization/
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25138947

fbshipit-source-id: fd19bf360077b447ce2272bfd4f6d6b798ae05ac
2020-11-25 08:59:29 -08:00
Vasiliy Kuznetsov
c5e380bfcb quant: add type annotations on quantization.fx.Quantizer class vars (#48343)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48343

Annotates the 4 class variables on `Quantizer` with real types,
fixing the small things uncovered by this along the way.

Test Plan:
```
mypy torch/quantization/
python test/test_quantization.py TestQuantizeFx
```

Imported from OSS

Reviewed By: supriyar

Differential Revision: D25136212

fbshipit-source-id: 6ee556c291c395bd8d8765a99f10793ca738086f
2020-11-21 15:31:00 -08:00
Vasiliy Kuznetsov
6b80b664bb quant: enable mypy on torch/quantization/fx (#48331)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48331

Enables mypy to not ignore type errors in FX quantization files.  Fixes the easy
typing errors inline, and comments out the harder errors to be fixed at a later time.
After this PR, mypy runs without errors on `torch/quantization`.

Test Plan:
```
> mypy torch/quantization/
Success: no issues found in 25 source files
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25133348

fbshipit-source-id: 0568ef9405b292b80b3857eae300450108843e80
2020-11-21 15:29:27 -08:00
Xin Guan
f8722825b5 Compare Weights FX Implementation (#48056)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48056

PyTorch FX Quantization API:  Compare weights
ghstack-source-id: 117255311

Test Plan:
buck test mode/dev caffe2/test:quantization -- 'test_remove_qconfig_observer_fx'
buck test mode/dev caffe2/test:quantization -- 'test_compare_weights_linear_dynamic_fx'
buck test mode/dev caffe2/test:quantization -- 'test_compare_weights_linear_static_fx'
buck test mode/dev caffe2/test:quantization -- 'test_compare_weights_conv_static_fx'

Reviewed By: hx89

Differential Revision: D24940516

fbshipit-source-id: 301c1958c0e64ead9072e0fd002e4b21e8cb5b79
2020-11-20 17:17:19 -08:00
Xin Guan
46d846f5bb T78750158 Support varying size input in numeric suite at 10/30/2020, 3:55:01 PM (#47391)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/47391

Current Numeric Suite will fail if it's collecting for multiple inputs and each input is of not same size.  This fix adds support for varying size input in numeric suite.
ghstack-source-id: 117058862

Test Plan:
buck test mode/dev caffe2/test:quantization -- 'test_shadow_logger'
buck test mode/dev caffe2/test:quantization  -- 'test_output_logger'
buck test mode/dev caffe2/test:quantization -- 'test_compare_weights_lstm_dynamic'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_stub_lstm_dynamic'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_outputs_lstm_dynamic'
buck test mode/dev caffe2/test:quantization -- 'test_compare_weights_conv_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_weights_linear_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_weights_linear_dynamic'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_stub_conv_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_stub_linear_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_stub_submodule_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_stub_functional_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_stub_linear_dynamic'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_outputs_conv_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_outputs_linear_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_outputs_functional_static'
buck test mode/dev caffe2/test:quantization -- 'test_compare_model_outputs_linear_dynami

Reviewed By: hx89

Differential Revision: D24662271

fbshipit-source-id: 6908169ee448cbb8f33beedbd26104633632896a
2020-11-18 23:57:41 -08:00
Jerry Zhang
ed57f804fa [quant][refactor] Move some util functions from torch/quantization/fx/utils.py to torch/quantization/utils.py (#48107)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48107

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D25026495

fbshipit-source-id: 3634b6b95a18670232600874b1e593180ea9f44c
2020-11-18 22:32:19 -08:00
Jerry Zhang
5883e0b0e0 [quant][fix][ez] Fix quant_type classification for fp16, fp16 (#48073)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48073

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D25011799

fbshipit-source-id: a12f645d6be1c607898633225b02617283d37df1
2020-11-18 20:07:54 -08:00
Jerry Zhang
72918e475e [quant] FakeQuantize inherit from FakeQuantizeBase (#48072)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48072

Test Plan: Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D25011074

fbshipit-source-id: 260f4d39299bc148b65c21e67b571dfa1d0fe2ad
2020-11-18 19:14:20 -08:00
Jerry Zhang
576fa09157 [quant][fix] Fix quant type classification for float_qparam qconfig (#48069)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48069

also renamed float_qparam_dynamic_qconfig to float_qparam_weight_only_qconfig
It's not used in user code yet so we only need to update the tests.

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D25010175

fbshipit-source-id: caa3eaa5358a8bc5c808bf5f64e6ebff3e0b61e8
2020-11-18 18:22:08 -08:00
Jerry Zhang
6049653c20 [quant][graphmode][fx] Keep linear op unchanged when qconfig is not supported (#48067)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48067

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D25008463

fbshipit-source-id: d0bfc6bf8d544824d0a55cd4bcd1f9301d75c935
2020-11-17 21:59:55 -08:00
Jerry Zhang
d7e838467a [qunat][graphmode][fx] Embedding/EmbeddingBag works in static quant qconfig (#48062)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48062

When Embedding/EmbeddingBag are configured with static quant we'll skip inserting observer for
them in the graph and keep the op unchanged and print a warning.
This also aligns with eager mode behavior as well.

We'll enforce this behavior for other ops that only supports dynamic/weight_only quant but not static quant as well.

We used a global variable `DEFAULT_NOT_OBSERVED_QUANTIZE_HANDLER`, this is not exposed to user right now,
we can add that later if needed.

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D25007537

fbshipit-source-id: 6ab9e025269b44bbfd0d6dd5bb9f95fe3ca9dead
2020-11-17 15:02:04 -08:00
Jerry Zhang
8aaca4b46a [reland][quant] Remove nn.quantized.ReLU module and nn.quantized.functional.relu (#47415) (#48038)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48038

nn.ReLU works for both float and quantized input, we don't want to define an nn.quantized.ReLU
that does the same thing as nn.ReLU, similarly for nn.quantized.functional.relu

this also removes the numerical inconsistency for models quantizes nn.ReLU independently in qat mode

Test Plan:
Imported from OSS

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D25000462

fbshipit-source-id: e3609a3ae4a3476a42f61276619033054194a0d2
2020-11-17 09:52:21 -08:00
Bram Wasti
43a9d6fb6e [TorchScript] Support user defined classes as constants (#5062)
Summary:
Pull Request resolved: https://github.com/pytorch/glow/pull/5062

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

User defined classes can be used as constants.  This is useful when freezing and removing the module from the graph.

Test Plan: waitforsadcastle

Reviewed By: eellison

Differential Revision: D23994974

fbshipit-source-id: 5b4a5c91158aa7f22df39d71f2658afce1d29317
2020-11-16 20:52:02 -08:00
Vasiliy Kuznetsov
4779553921 Revert "[quant] Remove nn.quantized.ReLU module and nn.quantized.functional.relu (#47415)" (#47949)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/47949

This reverts commit 1478e5ec2a.

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D24966363

Pulled By: vkuzo

fbshipit-source-id: ca1126f699eef84027a15df35962728296c8a790
2020-11-14 08:40:30 -08:00
Jerry Zhang
c0aa863c56 [quant][graphmode][fx][refactor] insert_quantize_node (#47880)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/47880

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24928797

fbshipit-source-id: 9a8b359cabfb800da86da114bf26bb5bd99d3fff
2020-11-13 14:50:42 -08:00
Jerry Zhang
1915ae9510 [quant][graphmode][fx][refactor] is_output_quantized (#47879)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/47879

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24928796

fbshipit-source-id: 55c49243b6a0b4811953cf72af57e5f56be8c419
2020-11-13 11:15:55 -08:00