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

Lower shape.cstr_broadcastable op in ShapeLegalizeToHLO #2384

Closed

Conversation

sdasgup3
Copy link
Member

@sdasgup3 sdasgup3 commented Jun 7, 2024

Lower shape.cstr_broadcastable op in ShapeLegalizeToHLO

Lower the op to shape_assertion custom_calls. And replace the result shape.witness with a const_witness of true. This allows the subsequent shape.assuming regions to be removed.

Also add an option to the pass to control whether to lower cstr_xxx ops. This makes the impl easier as we can reuse util functions in the file. And it makes sure existing use cases are not modified.

Why needed

The evaluation of quantized program depends bout lowering the stablehlo quantized types/ops to their integer equivalents. This is accomplished by pass like #2383 which introduces chlo boradcast operations. These chlo operations needs to be lowered to stablehlo ops as well using a pass which the current PR upgrades. Without this upgrade shape.cstr_broadcastable will not be lowered to stablehlo for evaluation.

Inspired from https://github.com/openxla/xla/blob/25f38dd29b8442e45072fc097d0f1ebe3163bf9d/xla/mlir_hlo/mhlo/transforms/mhlo_passes.td#L361

Copy link
Contributor

@mlevesquedion mlevesquedion left a comment

Choose a reason for hiding this comment

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

LGTM

// expected-error@+1 {{failed to legalize operation 'shape.cstr_broadcastable' that was explicitly marked illegal}}
%0 = shape.cstr_broadcastable %arg0, %arg1 : !shape.shape, !shape.shape
shape.assuming %0 {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed? Same comment below, I feel like this was covered in the first test.

stablehlo/transforms/ShapeLegalizeToStablehlo.cpp Outdated Show resolved Hide resolved
stablehlo/transforms/ShapeLegalizeToStablehlo.cpp Outdated Show resolved Hide resolved
Comment on lines +651 to +654
if (this->legalize_constraints_) {
target->addLegalOp<shape::ConstWitnessOp, shape::AssumingOp,
shape::AssumingYieldOp>();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO it would be cleaner to consolidate the handling of this->legalize_constraints_ into a single if. If it's true, add the pattern to the set of patterns and add the legal ops.

Copy link
Member Author

@sdasgup3 sdasgup3 Jun 7, 2024

Choose a reason for hiding this comment

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

I agree. The current choice is driven by how the code is currenty structured: addition of legal ops handled separately from adding the pattern. Using a single if would create an outlier for legalize_contriants_ case. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I think both takes are valid, but IMO in this case it would be more readable to have a single if since that would make it clearer what happens when legalize_constraints_ is true. Up to you though.

@sdasgup3 sdasgup3 force-pushed the support-shape-cstr-broadcastable branch from c6d6244 to 7a44cab Compare June 7, 2024 23:48
Copy link
Member Author

@sdasgup3 sdasgup3 left a comment

Choose a reason for hiding this comment

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

This is a blocker comment for merging this PR.

It seems that the current use-case of lowering statically shaped quantized program to stablehlo math operations can be achieve even without this pass.

With that it might be the case that we do not prioritize this PR for now.
I will get back with the resolution of this PR asap.

@mlevesquedion In anycase thanks for the review. They are exptremly valuable.

@sdasgup3
Copy link
Member Author

GitHub issue #2390 focuses on exploring the removal of chlo-broadcast operation dependencies from stablehlo-legalize-quant-to-int pass. If this removal is successful, we can avoid further changes to shape legalization, as suggested in the pull request. Consequently, let's close the pull request for now.

@sdasgup3 sdasgup3 closed this Jun 11, 2024
sdasgup3 added a commit that referenced this pull request Jun 20, 2024
fixes #2373

The PR is rebased on top on
#2383 and cherry-pick changes
from #2384.

### Direction to reviewer

Please review the commit
4d7dc1a
**excluding** the following files
  - docs/generated/stablehlo_passes.md
  - stablehlo/transforms/Passes.td
  - stablehlo/transforms/ShapeLegalizeToStablehlo.cpp
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

Successfully merging this pull request may close these issues.

2 participants