Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic model export pipeline + torch to ONNX and ONNX to deepsparse refactor #1192

Merged
merged 52 commits into from
Dec 29, 2022

Conversation

dbogunowicz
Copy link
Contributor

@dbogunowicz dbogunowicz commented Dec 1, 2022

Summary

This PR reworks all of the onnx optimization passes contained in src/sparseml/pytorch/sparsification/quantization/quantize_qat_export.py.

The new classes/functions are introduced:

  1. Transforms: class BaseTransform and class OnnxTransform(BaseTransform), which represent abstract transforms that act on onnx graphs
  2. Exporters: class BaseExporter(BaseTransform), class TorchToONNX(BaseExporter), and class ONNXToDeepsparse(BaseExporter), which represent transforms that can also save things to disk

In addition, every transform from quantize_qat_export (and other files), has been added as a OnnxTransform. This includes:

  1. The actual transform implementation
  2. docstring of exactly what the transform does
  3. a unit test for the transform

The get_structural_matches utility function was also added to aid in the implementation of these transforms. Notably it allows callees to match sub-graphs in any onnx graph, which makes implementation details more clear/explicit.

Test Plan

As mentioned in the summary, every transform has a unit test associated with it. Additionally, regression tests were added against the old module exporter logic for the following models:

  1. Resnet50
  2. Yolov5
  3. Bert for QA
    So both the graph structure and numerical output match exactly for both the new exporters and the old exporters.

bogunowicz@arrival.com and others added 13 commits December 1, 2022 11:54
* initial commit

* PR comments

* initial commit

* Delete test_fold_identity_initializers.py

* Delete __init__.py

* Delete __init__.py

* Update src/sparseml/exporters/transforms/base_transform.py

* fix docstrings

* few improvements and tests

Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
* Adding onnx graph structural matching

* Styling

* Adding missing init.py

* Update src/sparseml/exporters/transforms/utils/matching.py

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>

* Updating docstring of structural_matches

* Adding __all__

* Addressing review comments

* Removing extra file from merge

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>
* initial commit

* PR comments

* initial commit

* Delete test_fold_identity_initializers.py

* Delete __init__.py

* Delete __init__.py

* Adding onnx graph structural matching

* Styling

* Update src/sparseml/exporters/transforms/base_transform.py

* fix docstrings

* Adding missing init.py

* Update src/sparseml/exporters/transforms/utils/matching.py

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>

* Updating docstring of structural_matches

* Adding __all__

* ready for review

* Update src/sparseml/exporters/transforms/fold_identity_initializers.py

Co-authored-by: corey-nm <109536191+corey-nm@users.noreply.github.com>

* some nits according to Bens comments

Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
Co-authored-by: Corey Lowman <corey@neuralmagic.com>
Co-authored-by: corey-nm <109536191+corey-nm@users.noreply.github.com>
* Adding InitializersToUint8 transform

* Update src/sparseml/exporters/transforms/initializers_to_uint8.py
* Adding onnx graph structural matching

* Styling

* Adding missing init.py

* Update src/sparseml/exporters/transforms/utils/matching.py

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>

* Updating docstring of structural_matches

* Adding __all__

* initial commit

* Update convert_quantizable_conv_integer.py

Co-authored-by: Corey Lowman <corey@neuralmagic.com>
Co-authored-by: corey-nm <109536191+corey-nm@users.noreply.github.com>
Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
* Adding onnx graph structural matching

* Styling

* Adding missing init.py

* Update src/sparseml/exporters/transforms/utils/matching.py

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>

* Updating docstring of structural_matches

* Adding __all__

* initial commit

* Delete base_exporter.py

* Update src/sparseml/exporters/transforms/convert_quantizable_matmul.py

* beautify

* check for initializers

* add docstring

Co-authored-by: Corey Lowman <corey@neuralmagic.com>
Co-authored-by: corey-nm <109536191+corey-nm@users.noreply.github.com>
Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
corey-nm and others added 16 commits December 12, 2022 15:40
* Adding ConvToQLinearConv transform

* Responding to review comments

* Respond to reviews
* Adding FlattenQParams transform

* Respond to review
* Adding ConvToQLinearConv transform

* Responding to review comments

* Adding GemmToQLinearMatMul

* Styling
* initial commit

* intiial commit

* PR comments

* fix errors

* Apply suggestions from code review

* upadte heleprs

* matching of conv integer pass

* second implementation done, needs some polishing

* Adding match_structure and iter_structural_matches

* Using structural matching for quantizable_conv_integer

* initial commit

* Adding onnx graph structural matching

* Styling

* Adding missing init.py

* Update src/sparseml/exporters/transforms/utils/matching.py

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>

* Updating docstring of structural_matches

* Adding __all__

* initial commit

* ready for PR

* beautify

* Delete test_helpers.py

* Delete base_exporter.py

Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
Co-authored-by: Corey Lowman <corey@neuralmagic.com>
Co-authored-by: corey-nm <109536191+corey-nm@users.noreply.github.com>
* Adding FoldConvDivBn

* Expanding docstring
* initial commit

* get transform into the correct format

* ready for review

* fix naming in test

* Fixing trivial onnx adds

Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
Co-authored-by: corey-nm <109536191+corey-nm@users.noreply.github.com>
Co-authored-by: Corey Lowman <corey@neuralmagic.com>
* Adding QuantizeResiduals transform

* Adding tests
corey-nm and others added 22 commits December 15, 2022 08:58
* Adding DeleteRepeatedQdq transform

* Adding unit test for delete repeated qdq

* Using assert_node_type

* Update src/sparseml/exporters/transforms/delete_repeated_qdq.py
* Adding SkipInputQuantize transform

* add tests

Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
* Fixing matching logic of qlinear transforms

* Adding folding of input/output quants to qlinears
…1249)

* Initial comit of exporters

* Styling

* Fixing SkipInputQuantize

* Adding validation methods

* Clean up ONNXToDeepsparse

* Moving TorchToONNX to pytorch

* Adding inplace and saving pre optimized model to ONNXToDeepsparse

* Adding sketch of tests

* Regression tests against simple models

* resnet50 regression tests passing

* resnet50 exporters are all equivalent

* Moving FoldConvDivBn under initializer folding

* Adding yolov5 tests

* Apply suggestions from code review

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>

* Review response

* Adding notes from review

* uncomment asserts... oops

* yolo & resnet tests passing

Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com>
…tchResult to str (#1262)

* Addin any_of and MatchResult to str

* Fixing docstring of get_structural_matches
* Standardization of some transforms

* Adding logging methods to OnnxTransform class
* Standardizing transforms with node removals

* Using log_match
* Standardizing MatMulToQLinearMatMul

* Using log_match
* Standardizing ConvToConvIntegerAddCastMul

* Using log_match
* Standardizing qlinear transforms

* Using log_match
#1269)

* Standardizing MatMulIntegerAddCastMul transforms

* Using log_match and any_of
* Standardizing QuantizeQATEmbedding

* Add log_match
* initial commit

* Apply suggestions from code review

* Update tests/sparseml/pytorch/test_torch_to_onnx_exporter.py

* Fixing bert exporters

Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
Co-authored-by: Corey Lowman <corey@neuralmagic.com>
* initial commit

* PR edits

* Delete recipe.yaml

* fix onnx problem

* Fixing torch import issue and numpy attr error

* Another attempt at fixing get_numpy_dtype

* Fix numpy.float usage

Co-authored-by: bogunowicz@arrival.com <bogunowicz@arrival.com>
Co-authored-by: Corey Lowman <corey@neuralmagic.com>
@bfineran bfineran changed the title [Exporter Refactor] Feature Branch Generic model export pipeline + torch to ONNX and ONNX to deepsparse refactor Dec 20, 2022
Copy link
Member

@bfineran bfineran left a comment

Choose a reason for hiding this comment

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

🚀

Copy link
Contributor

@KSGulin KSGulin left a comment

Choose a reason for hiding this comment

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

Super clean implementation!

@dbogunowicz
Copy link
Contributor Author

@corey-nm great job! I like the PR message.
Can we override the tests and land it now?

@corey-nm
Copy link
Contributor

I think @bfineran would have to force merge it

@bfineran bfineran merged commit ec37d3e into main Dec 29, 2022
@bfineran bfineran deleted the feature/damian/export_pipeline_refactor branch December 29, 2022 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants