Skip to content

Conversation

@BowenBao
Copy link
Collaborator

This pass tries to resolve scalar type mismatch issues between input tensors introduced by the implicit type conversions on scalars.

e.g. #23724

@pytorchbot pytorchbot added oncall: jit Add this issue/PR to JIT oncall triage queue module: build Build system issues module: internals Related to internal abstractions in c10 and ATen module: onnx Related to torch.onnx labels Aug 14, 2019
@BowenBao BowenBao requested a review from spandantiwari August 14, 2019 23:54

Choose a reason for hiding this comment

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

This mapping maybe useful beyond this pass. We should consider abstracting it out.

Choose a reason for hiding this comment

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

Maybe this one too.

Choose a reason for hiding this comment

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

This is a method for extending type promotion beyond pairs, to a vector of types. It may be useful beyond this pass. Should we maybe move this as a overload to c10::promoteTypes? @bddppq What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

@smessmer @zdevito do we want to promote this method to c10 level helper function?

@ajaysg-zz
Copy link

ajaysg-zz commented Aug 21, 2019

@BowenBao and @spandantiwari i cloned the following repo mentioned in this issue https://github.com/BowenBao/pytorch/tree/onnx_implicit_cast. Still i am getting the type mismatch error.

@BowenBao
Copy link
Collaborator Author

Hi @ajaysg, on my local machine I could not repro the mismatch error with this branch. Particularly, the exported model now has a cast node after Mul which should resolve this issue.

Screenshot from 2019-08-21 11-34-23

Please also note that this model won't be able to export, or run in ONNX backend, until the spec & impl for Det and support for torch.unfold is in. We are actively working on these two items.

@BowenBao BowenBao force-pushed the onnx_implicit_cast branch 2 times, most recently from ffc18d5 to 0562f98 Compare August 22, 2019 18:10
@BowenBao
Copy link
Collaborator Author

@pytorchbot retest this please

@ajaysg-zz
Copy link

ajaysg-zz commented Aug 23, 2019

hi @BowenBao i cloned this repo using the following command
git clone https://github.com/BowenBao/pytorch/
then i checkout to the branch using this command
cd pytorch
git checkout -b remotes/origin/onnx_implicit_cast
Then i built the binaries from source
After the installation i converted my model to onnx. I could not find the cast node after mul node. When i tried to use the model in onnxruntime i am getting the same error as before
RuntimeError: [ONNXRuntimeError] : 1 : GENERAL ERROR : Type Error: Type parameter (T) bound to different types (tensor(int64) and tensor(float) in node ().
Thanks for your support in advance

@BowenBao
Copy link
Collaborator Author

hi @ajaysg , could you try git checkout -t origin/onnx_implicit_cast? You can use git log to check if your current branch indeed contains patch from this PR.

@ajaysg-zz
Copy link

Hi @BowenBao i tried the same you suggested. Still i am not able to find the cast node after mul node in the generated onnx model. Also while i am using that model in onnxrt i am getting the Type Parameter issue

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@dzhulgakov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@dzhulgakov
Copy link
Collaborator

@pytorchbot retest this please

@dzhulgakov
Copy link
Collaborator

I'm not the best person to review it probably. @nairbv or @ZolotukhinM - do you happen to have more context on type promotion logic?

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@houseroad has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Member

@houseroad houseroad left a comment

Choose a reason for hiding this comment

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

Thanks, this is more fundamental solution for scalar type mismatching solution.

I am not sure whether we have covered all the pytorch ops already or not, but it's good start.

Please address my inline comments.

Copy link
Member

Choose a reason for hiding this comment

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

Not only Analysis, but Propagation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As below, the pass is not doing Propagation at the moment.

Copy link
Member

Choose a reason for hiding this comment

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

Also add some comments about the purpose/motivation of this pass.

Copy link
Member

Choose a reason for hiding this comment

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

It's a bit weird we only have one function call in ScalarTypeAnalysisForONNX, consider merging them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We were wondering if in the future this is a good place to extend the pass to perform complete shape and type inferencing. Especially to support scripting, which unlike tracing, doesn't record operator outputs with complete tensor type.
There is existing shape_analysis.cpp, but is for aten operators, and it is usually more dynamic than ONNX.

Copy link
Member

Choose a reason for hiding this comment

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

@smessmer @zdevito do we want to promote this method to c10 level helper function?

Copy link
Member

Choose a reason for hiding this comment

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

Give some context why handling like this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding it to the comments.

Copy link
Member

Choose a reason for hiding this comment

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

This may incur some duplicate values, but I guess it should be rare.

Copy link

@ZolotukhinM ZolotukhinM left a comment

Choose a reason for hiding this comment

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

The IR pass part looks good!

Choose a reason for hiding this comment

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

I wonder if this works correctly if a node has the same value as input several times, e.g

%a = prim::mul(%b, %b)

I'm not sure that inputs() iterator will stay valid after we call replaceInputWith, which would replace all entries of the current input. Could you please double-check that it works as expected? A safe alternative to achieve the same and avoid iterator issues for certain would be to use replaceInput(index, newValue).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a very interesting case. I will create a test case for that, after fixing the regression #26328.

@BowenBao
Copy link
Collaborator Author

After rebasing, the CI is failing on this test case #26328, which seems to be a regression since it can also repro on master. I'm currently looking at that.

BowenBao added a commit to BowenBao/pytorch that referenced this pull request Jan 5, 2022
* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch#24378

* Fix UTs

ghstack-source-id: 4e796b1
Pull Request resolved: pytorch#69548
BowenBao added a commit that referenced this pull request Jan 7, 2022
…JIT pass (#69227)"

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Differential Revision: [D32994268](https://our.internmc.facebook.com/intern/diff/D32994268)

[ghstack-poisoned]
BowenBao added a commit that referenced this pull request Jan 7, 2022
* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Differential Revision: [D32994268](https://our.internmc.facebook.com/intern/diff/D32994268)

[ghstack-poisoned]
BowenBao added a commit that referenced this pull request Jan 11, 2022
…JIT pass (#69227)"

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Differential Revision: [D32994268](https://our.internmc.facebook.com/intern/diff/D32994268)

[ghstack-poisoned]
BowenBao added a commit that referenced this pull request Jan 11, 2022
* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Differential Revision: [D32994268](https://our.internmc.facebook.com/intern/diff/D32994268)

[ghstack-poisoned]
BowenBao added a commit to BowenBao/pytorch that referenced this pull request Jan 21, 2022
* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch#24378

* Fix UTs

ghstack-source-id: 4e796b1
Pull Request resolved: pytorch#69548
BowenBao added a commit to BowenBao/pytorch that referenced this pull request Jan 21, 2022
* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch#24378

* Fix UTs

ghstack-source-id: 4e796b1
Pull Request resolved: pytorch#69548
BowenBao added a commit to BowenBao/pytorch that referenced this pull request Jan 31, 2022
* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch#24378

* Fix UTs

ghstack-source-id: 4e796b1
Pull Request resolved: pytorch#69548
BowenBao added a commit that referenced this pull request Feb 10, 2022
…JIT pass (#69227)"

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Differential Revision: [D32994268](https://our.internmc.facebook.com/intern/diff/D32994268)

[ghstack-poisoned]
BowenBao added a commit that referenced this pull request Feb 10, 2022
* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Differential Revision: [D32994268](https://our.internmc.facebook.com/intern/diff/D32994268)

[ghstack-poisoned]
facebook-github-bot pushed a commit that referenced this pull request Feb 11, 2022
Summary:
Pull Request resolved: #69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
pytorchmergebot pushed a commit that referenced this pull request Feb 11, 2022
Summary:
Pull Request resolved: #69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR #24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322c)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 15, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 15, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 15, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 16, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 16, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 17, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 17, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 17, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 17, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 17, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 17, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 17, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 20, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 20, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 20, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
cyyever pushed a commit to cyyever/pytorch_private that referenced this pull request Feb 21, 2022
Summary:
Pull Request resolved: pytorch/pytorch#69548

* Add Concat to Scalar type analysis pass

By using scalar type analysis for Concat, the exported model can do
automatic type promotion for Concat nodes, including mixed fp16 and fp32
inputs, for example.

Unit tests based on the original PR pytorch/pytorch#24378

* Fix UTs

Test Plan: Imported from OSS

Reviewed By: msaroufim

Differential Revision: D32994268

Pulled By: malfet

fbshipit-source-id: 0deab88b0bb1e396770690af27730accb64fcf63
(cherry picked from commit a99322cadf7b79a4548266a9d4d3af094b89bac4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: build Build system issues module: internals Related to internal abstractions in c10 and ATen module: onnx Related to torch.onnx oncall: jit Add this issue/PR to JIT oncall triage queue open source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants