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

static shapes propagation: add torch-shape-refinement-pipeline after canonicalization #963

Closed
Shukla-Gaurav opened this issue Jun 22, 2022 · 3 comments

Comments

@Shukla-Gaurav
Copy link
Collaborator

Shukla-Gaurav commented Jun 22, 2022

We can simplify the IR with the help of canonicalization/folding and the result of those simplifications can be used to compute few dynamic shapes at the compile time.
For example:

%int128 = torch.constant.int 128
%int1 = torch.constant.int 1
%185 = torch.vtensor.literal(opaque<"elided_large_const", "0xDEADBEEF"> : tensor<1x512xsi64>) : !torch.vtensor<[1,512],si64>
%186 = torch.vtensor.literal(dense<0> : tensor<si64>) : !torch.vtensor<[],si64>
%195 = torch.prim.NumToTensor.Scalar %int128 : !torch.int -> !torch.vtensor<[],si64>
%196 = torch.aten.add.Tensor %195, %186, %int1 : !torch.vtensor<[],si64>, !torch.vtensor<[],si64>, !torch.int -> !torch.vtensor<[],si64>
%197 = torch.aten.Int.Tensor %196 : !torch.vtensor<[],si64> -> !torch.int
%198 = torch.aten.slice.Tensor %185, %int0, %int0, %int9223372036854775807, %int1 : !torch.vtensor<[1,512],si64>, !torch.int, !torch.int, !torch.int, !torch.int -> !torch.vtensor<[1,512],si64>
%199 = torch.aten.slice.Tensor %198, %int1, %int0, %197, %int1 : !torch.vtensor<[1,512],si64>, !torch.int, !torch.int, !torch.int, !torch.int -> !torch.vtensor<[1,?],si64>

In this IR, it's possible to compute the only dynamic dimension in aten.slice.Tensor op. By adding proper canonicalization to aten.add.Tensor and aten.int.Tensor, we can simplify %197 as %int128 (there is an open patch for the first canonicalization #935). This simplification will not help to get rid of the dynamic dimension in aten.slice.Tensor op until we run the torch-shape-refinement-pipeline again.

Whenever we canonicalize/fold an op, there is a room for shape simplification to its users. How should we achieve that?
Should we add torch-shape-refinement-pipeline after canonicalization in the passes pipeline?
@silvasean @ramiro050 @cathyzhyi

@powderluv
Copy link
Collaborator

@sjarus FYI this is the last dynamic shape that is required for you to lower BERT to TOSA.

@silvasean
Copy link
Contributor

We don't want to randomly run the shape refinment pipeline too many times. Ideally we will have it in one good place in the pass pipeline.

One idea is to put the canonicalize pass here:

pm.addNestedPass<func::FuncOp>(createCSEPass());

The inliner which runs a few lines above that should already be running canonicalizations. So adding the explicit canonicalize pass should only be needed if SimplifyShapeCalculations is exposing this constant folding opportunity.

qedawkins pushed a commit to nod-ai/torch-mlir that referenced this issue Oct 3, 2022
Signed-off-by: Michael Holman <michhol@microsoft.com>

Co-authored-by: Alexandre Eichenberger <alexe@us.ibm.com>
qedawkins pushed a commit to nod-ai/torch-mlir that referenced this issue Oct 3, 2022
… errors by assertion. (llvm#968)

* Conversion support (llvm#956)

* initial

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* works, bool is great, but unsigned create issues

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* added support to remove the UnrealizedConversionCastOp while lowering to llvm

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* last version to support some shaped type in conversion, reduce after to Integer or Float

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* converted to cast scalar only

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* added lit tests

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* update

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* initial

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* works, bool is great, but unsigned create issues

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* added support to remove the UnrealizedConversionCastOp while lowering to llvm

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* last version to support some shaped type in conversion, reduce after to Integer or Float

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* converted to cast scalar only

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* added lit tests

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* update

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* implemented cast with index, before replacing usage

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* update

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* cleanup and use of createMath constant/constantIndex/cast/castIndex

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* make sure that only explicitly unsigned are treated as unsigned; signless and indices are treated as signed

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Add the onnx-mlir logo to the onnxai webpage (llvm#959)

* Add the onnx-mlir logo to the onnxai webpage

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Remove a dump()

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Missing git-add the .png file

Signed-off-by: Tung D. Le <tung@jp.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Several small improvements including adding onnx-mlir-doc to ALL target and building onnx from the submodule (llvm#957)

1. Add onnx-mlir-doc to the ALL target. This way it will be run with the default target
2. On Windows instead of installing onnx from pip, build it from source to match other CIs
3. Remove unnecessary include in Compiler

Signed-off-by: Stella Stamenova <stilis@microsoft.com>

Co-authored-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* fix the mistake of shape in doc-example (llvm#964)

Signed-off-by: david.cheng <david.cheng@enflame-tech.com>

Co-authored-by: david.cheng <david.cheng@enflame-tech.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* install dll to bin (llvm#963)

Signed-off-by: Michael Holman <michhol@microsoft.com>

Co-authored-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Output better error messages for file opening errors.

Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Fix clang-format errors

Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Fix build errors.

Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Fix a build error

Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Onehot update (llvm#967)

* initial commit

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* one hot upgraded, more dyn cases

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* cleanup

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* cleanup

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* initial commit

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* one hot upgraded, more dyn cases

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* cleanup

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* cleanup

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* We currently run a few test sets, e.g., check-onnx-backend, (llvm#958)

check-onnx-backend-dynamic, etc., in serial. While we do
parallelize within each test set, we don't get much benefit
from it. Because within each test set, most of the time was
spent on one or two long running tests. While waiting for
these long running tests, we could have started the other
test sets.

This patch parallelizes across tests sets by starting them
all at once so the long running tests from different test
sets can run in parallel.

CompilerLib tests are disabled because a bug has been uncovered
that it was actually still using onnx-mlir instead of CompilerLibTest
to compile the models. Fix the bug to use CompilerLibTest and
it fails to compile the models.

Use CTEST_PARALLEL_LEVEL for numerical tests to be compatible
with Windows CI

Signed-off-by: Gong Su <gong_su@hotmail.com>

Co-authored-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Use ninja as a build system (llvm#946)

Signed-off-by: mshr-h <mhg00g13@gmail.com>

Co-authored-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Added documentation on using DEBUG according to LLVM (llvm#970)

* added documentation on using DEBUG according to LLVM

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* removed errand printf for debugging

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* CategoryMapper: shape inference support. (llvm#971)

Signed-off-by: Ettore Tiotto <etiotto@ca.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Updating docs to move into the right place (llvm#972)

* Updating docs to move into the right place

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* add link

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* add links

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* add links

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>

* respond to comments on MLIR.cmake

Signed-off-by: Alexandre Eichenberger <alexe@us.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Fix EnableMemoryPool to account for 'memref.cast' operation. (llvm#966)

Signed-off-by: Ettore Tiotto <etiotto@ca.ibm.com>
Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Fix typo.

Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Revert "Fix typo."

This reverts commit f2934faee463557ebc2adb6aca5b9521f5064f12.

Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

* Fix typo

Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>

Co-authored-by: Alexandre Eichenberger <alexe@us.ibm.com>
Co-authored-by: Tung D. Le <tung@jp.ibm.com>
Co-authored-by: Stella Stamenova <stilis@microsoft.com>
Co-authored-by: DavidChan <chengwei0519@163.com>
Co-authored-by: david.cheng <david.cheng@enflame-tech.com>
Co-authored-by: Michael Holman <michhol@microsoft.com>
Co-authored-by: gongsu832 <gong_su@hotmail.com>
Co-authored-by: Masahiro Hiramori <mhg00g13@gmail.com>
Co-authored-by: Ettore Tiotto <etiotto@ca.ibm.com>
@silvasean
Copy link
Contributor

We added canonicalization for aten.add.Tensor and aten.int.Tensor so this shouldn't be an issue anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants