Commit Graph

36 Commits

Author SHA1 Message Date
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
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
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
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
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
1478e5ec2a [quant] Remove nn.quantized.ReLU module and nn.quantized.functional.relu (#47415)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/47415

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

Reviewed By: z-a-f

Differential Revision: D24747035

fbshipit-source-id: b8fdf13e513a0d5f0c4c6c9835635bdf9fdc2769
2020-11-12 10:56:30 -08:00
Jerry Zhang
1239d067ae [quant][graphmode][fx] Support standalone_module_class (#47705)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/47705

Test Plan: Imported from OSS

Reviewed By: z-a-f

Differential Revision: D24872380

fbshipit-source-id: db2ec7ba03da27203033fbebc11666be572622bb
2020-11-11 09:15:14 -08:00
Jerry Zhang
366888a5e2 [quant][graphmode][fx] Remove logging for standalone module api calls (#47032)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/47032

these are not top level apis, not supposed to be called directly by user.

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24610602

fbshipit-source-id: c5510f06b05499387d70f23508470b676aea582c
2020-10-29 18:39:43 -07:00
Jerry Zhang
c2a3951352 [quant][graphmode][fx] Remove inplace option for convert_fx (#46955)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46955

Initially we were thinking of adding a `invalidate_quantized_float_parameters` option to free the memory
of quantized floating parameters, but it turns out we will do module swap just like in eager mode for the modules
that are quantized, so the old floating point module will not be referenced after quantization. therefore this feature
is only needed for functionals, since most people are using quantization with modules we may not need this.

we'll revisit after we find there is a need for this.

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D24579400

fbshipit-source-id: fbb0e567405dc0604a2089fc001573affdade986
2020-10-28 21:07:19 -07:00
Jerry Zhang
cd8ed93287 [quant][graphmode][fx][api] Remove inplace option from prepare_fx (#46954)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/46954

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D24579401

fbshipit-source-id: adce623ce819fa220f7bb08d1ff3beaa69850621
2020-10-28 08:00:12 -07:00
Jerry Zhang
d92bf921db [quant][graphmode][fx] Remove inplace option for fuse_fx (#46953)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/46953

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D24579402

fbshipit-source-id: 5e0b8abf682287ab3c7dd54c2fc2cf309295e147
2020-10-27 22:34:11 -07:00
Jerry Zhang
998b9b9e68 [quant][graphmode][fx] custom_module support static/dynamic/weight_only quant (#46786)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46786

Previously we only support static quant, this PR added support for other types of quantization.

Note qat is actually orthogonal to these quant types, this is referring to the convert step where we
convert the observed module to a quantized module.

for qat, user will provide a CustomModule -> FakeQuantizedCustomModule in prepare_custom_config_dict
and FakeQuantizedCustomModule -> static/dynamic/weight_only quantized CustomModule in convert_custom_config_dict.

Test Plan: Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D24514701

fbshipit-source-id: 2918be422dd76093d67a6df560aaaf949b7f338c
2020-10-27 21:41:33 -07:00
Jerry Zhang
37dbc6117f [quant][eagermode] Add additional_fuser_method_mapping to config (#46355)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/46355

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24319562

fbshipit-source-id: be9800723c0b3e36f26e73c25c0c6ae1d4344f45
2020-10-24 02:18:04 -07:00
Jerry Zhang
343260a1cc [quant][graphmode][fx] Add support for additional_{fusion/quant}_pattern (#46346)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46346

Allow user to provide additional fusion/quant patterns for fx graph mode

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24317437

fbshipit-source-id: 719927cce50c74dffa4f848bd5c98995c944a26a
2020-10-23 15:03:42 -07:00
Jerry Zhang
bd90379df5 [quant][graphmode][fx] Add support for additional_fuse_method_mapping (#46345)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46345

Allow user to add more fusion mappings

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24317439

fbshipit-source-id: 3b144bbc305e41efbdf3e9fb25dbbeaad9e86c6a
2020-10-22 15:15:31 -07:00
Jerry Zhang
23fad9111e [quant][graphmode][fx] Add additional_qat_module_mapping (#46344)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/46344

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24317438

fbshipit-source-id: f9e73aeb4c7a107c8df0bae8319464e7d5d7275b
2020-10-22 13:11:26 -07:00
Jerry Zhang
ab28bd528d [quant][graphmode][fx] Support quantizing FloatFunctional (#46634)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/46634

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24438227

fbshipit-source-id: f33439d51112e13f59ee4292e804495d38fa3899
2020-10-22 01:21:17 -07:00
Jerry Zhang
746febdeac [quant][graphmode][fx] Add additional_object_mapping argument to convert (#46338)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46338

Should we merge quantized module and quantized operator configurations?

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24317435

fbshipit-source-id: 3575251fe9d80a6628b8c3243c2ed92ea5e921e3
2020-10-21 16:39:07 -07:00
Jerry Zhang
a06b95b2ba [quant][graphmode][fx] Support non_traceable_module/module_class (#46298)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46298

Allow user to specify a list of qualified names for non traceable submodule
or type of the non traceable submodule
See quantize_fx.py for api

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24294210

fbshipit-source-id: eb1e309065e3dfbf31e63507aaed73587f0dae29
2020-10-19 18:50:08 -07:00
Jerry Zhang
49903a5cd5 [quant][graphmode][fx] Move custom_module_class config to prepare/convert_custom_config_dict (#46251)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/46251

Test Plan: Imported from OSS

Reviewed By: raghuramank100

Differential Revision: D24290810

fbshipit-source-id: 7a96f04a0f33f0315943ac18ef2d08e4f5a5d1c0
2020-10-14 10:43:48 -07:00
Jerry Zhang
67a0c0af27 [quant][fx][graphmode] Add prepare_custom_config_dict and convert_custom_config_dict (#46223)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46223

Also move standalone module config to the prepare_custom_config_dict

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24266900

fbshipit-source-id: fe3ff5b8c657af3f377041e7881d400938e044f8
2020-10-13 14:19:49 -07:00
Jerry Zhang
7f6a1b2bd5 [quant][fx][graphmode][api] Change API for custom module (#45920)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/45920

See docs for new way of defining custom modules

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D24145856

fbshipit-source-id: 488673fba503e39e8e303ed5a776fe36899ea4e3
2020-10-12 23:42:27 -07:00
Vasiliy Kuznetsov
7094c09ff7 quantizaton: add API usage logging (#46095)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46095

Adds logging on usage of public quantization APIs. This only works in FB codebase
and is a no-op in OSS.

Test Plan: The test plan is fb-only

Reviewed By: raghuramank100

Differential Revision: D24220817

fbshipit-source-id: a2cc957b5a077a70c318242f4a245426e48f75e5
2020-10-09 16:51:27 -07:00
Jerry Zhang
2b204e6db3 [quant][fx][graphmode] Run symbolic_trace in quantization (#45919)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/45919

As discussed with JIT team, we'll run symbolic trace in quantization functions
prepare_fx now takes orginal pytorch model (torch.nn.Module) instead of `GraphModule` as input

Test Plan: Imported from OSS

Reviewed By: supriyar

Differential Revision: D24145857

fbshipit-source-id: 2b7a4ca525a7a8c23a26af54ef594c6a951e4024
2020-10-08 17:26:03 -07:00
Jerry Zhang
4f685ecc25 [reland][quant][graphmode][fx] Merge all quantization mode (#45292) (#45672)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/45672

This PR merges all quantization mode and will only expose the following top level functions:
```
prepare_fx
prepare_qat_fx
convert_fx
```

Test Plan:
Imported from OSS

Imported from OSS

Reviewed By: z-a-f

Differential Revision: D24053439

fbshipit-source-id: 03d545e26a36bc22a73349061b751eeb35171e64
2020-10-01 15:47:11 -07:00
Mike Ruberry
c36b354072 Revert D23913105: [quant][graphmode][fx] Merge all quantization mode
Test Plan: revert-hammer

Differential Revision:
D23913105 (ffcb0989e7)

Original commit changeset: 4e335286d6de

fbshipit-source-id: 5765b4e8ec917423f1745f73a9f3f235fc53423d
2020-10-01 03:12:42 -07:00
Jerry Zhang
ffcb0989e7 [quant][graphmode][fx] Merge all quantization mode (#45292)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/45292

This PR merges all quantization mode and will only expose the following top level functions:
```
prepare_fx
prepare_qat_fx
convert_fx
```

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D23913105

fbshipit-source-id: 4e335286d6de225839daf51d1df54322d52d68e5
2020-09-30 21:20:34 -07:00
Jerry Zhang
5539066d12 [quant][graphmode][fx] Support quantization for custom module (#44074)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/44074

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D23580642

fbshipit-source-id: a80b0b3e5e1f4c4a9647da872239cc0a4d58dd3b
2020-09-30 10:24:54 -07:00
Jerry Zhang
adb2b380ba [quant][graphmode][fx] qconfig_dict support more types of configurations (#44856)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/44856

Support following format of qconfig_dict
```python
qconfig_dict = {
    # optional, global config
    "": qconfig?,

    # optional, used for module and function types
    # could also be split into module_types and function_types if we prefer
    "object_type": [
      (nn.Conv2d, qconfig?),
      (F.add, qconfig?),
      ...,
    ],

    # optional, used for module names
    "module_name": [
      ("foo.bar", qconfig?)
      ...,
    ],

    # optional, matched in order, first match takes precedence
    "module_name_regex": [
      ("foo.*bar.*conv[0-9]+", qconfig?)
      ...,
    ]
    # priority (in increasing order): global, object_type, module_name_regex, module_name
    # qconfig == None means fusion and quantization should be skipped for anything
    # matching the rule
}
```

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D23751304

fbshipit-source-id: 5b98f4f823502b12ae2150c93019c7b229c49c50
2020-09-23 13:59:53 -07:00
Jerry Zhang
6269b6e0f0 [quant][graphmode][fx][api] Call fuse in prepare (#43984)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/43984

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D23459261

fbshipit-source-id: 6b56b0916d76df67b9cc2f4be1fcee905d604019
2020-09-08 18:09:26 -07:00
Jerry Zhang
9f54bcc522 [quant][graphmode][fx] Support inplace option (#43983)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43983

Support inplace option in apis

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D23459260

fbshipit-source-id: 80409c7984f17d1a4e13fb1eece8e18a69ee43b3
2020-09-08 17:39:13 -07:00
Meghan Lele
7816d53798 [JIT] Add mypy type annotations for JIT (#43862)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/43862

Test Plan: Imported from OSS

Reviewed By: eellison

Differential Revision: D23491151

Pulled By: SplitInfinity

fbshipit-source-id: 88367b89896cf409bb9ac3db7490d6779efdc3a4
2020-09-03 15:09:24 -07:00
Vasiliy Kuznetsov
6f5282adc8 add quantization debug util to pretty print FX graphs (#43910)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43910

Adds a debug function to get a representation of all nodes in the
graph, such as

```
name          op      target         args               kwargs
x             plchdr  x              ()                 {}
linear_weight gt_prm  linear.weight  ()                 {}
add_1         cl_fun  <bi_fun add>   (x, linear_weight) {}
linear_1      cl_mod  linear         (add_1,)           {}
relu_1        cl_meth relu           (linear_1,)        {}
sum_1         cl_fun  <bi_meth sum>  (relu_1,)          {'dim': -1}
topk_1        cl_fun  <bi_meth topk> (sum_1, 3)         {}
```

using only Python STL. This is useful for printing internal state of
graphs when working on FX code.

Has some on-by-default logic to shorten things so that node reprs for
toy models and unit tests fit into 80 chars.

Flexible on function name and location, I care more that this is
accessible from both inside PT as well as from debug scripts which
are not checked in.

Test Plan:
see
https://gist.github.com/vkuzo/ed0a50e5d6dc7442668b03bb417bd603 for
example usage

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D23435029

fbshipit-source-id: 1a2df797156a19cedd705e9e700ba7098b5a1376
2020-09-02 09:04:44 -07:00
Jerry Zhang
7db7da7151 [reland][quant][graphmode][fx] Add top level APIs (#43581) (#43901)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43901

Add similar APIs like eager and graph mode on torchscript
- fuse_fx
- quantize_fx (for both post training static and qat)
- quantize_dynamic_fx (for post training dynamic)
- prepare_fx (for both post training static and qat)
- prepare_dynamic_fx (for post training dynamic)
- convert_fx (for all modes)

Test Plan:
Imported from OSS

Imported from OSS

Reviewed By: vkuzo

Differential Revision: D23432430

fbshipit-source-id: fc99eb75cbecd6ee7a3aa6c8ec71cd499ff7e3c1
2020-08-31 18:24:26 -07:00
Alban Desmaison
f7bae5b6b1 Revert D23385091: [quant][graphmode][fx] Add top level APIs
Test Plan: revert-hammer

Differential Revision:
D23385091 (eb4199b0a7)

Original commit changeset: b789e54e1a0f

fbshipit-source-id: dc3dd9169d34beab92488d78d42d7e7d05e771d1
2020-08-31 12:18:29 -07:00
Jerry Zhang
eb4199b0a7 [quant][graphmode][fx] Add top level APIs (#43581)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43581

Add similar APIs like eager and graph mode on torchscript
- fuse_fx
- quantize_fx (for both post training static and qat)
- quantize_dynamic_fx (for post training dynamic)
- prepare_fx (for both post training static and qat)
- prepare_dynamic_fx (for post training dynamic)
- convert_fx (for all modes)

Test Plan: Imported from OSS

Reviewed By: vkuzo

Differential Revision: D23385091

fbshipit-source-id: b789e54e1a0f3af6b026fd568281984e253e0433
2020-08-31 10:12:55 -07:00