[NFC] Add elementwise trait on MIGraphX dialect ops#2339
Conversation
Mark all MIGraphX elementwise operations (unary, binary, clip, where, convert) with the MLIR Elementwise trait. This enables programmatic querying of elementwise ops via op.hasTrait<OpTrait::Elementwise>() instead of exhaustive isa<> lists. Also add AllShapesMatch<["inA", "output"]> to unary ops and convert to enforce that elementwise unary operations preserve shape. Made-with: Cursor
Reorganize ops.mlir and invalid.mlir so that all tests for a given op are contiguous. Each op now has a section header comment and tests are ordered following the TD file definition order. Previously the first split of invalid.mlir mixed reshape, equal, and quant_dot tests together, and sigmoid was separated from other unary op tests. Made-with: Cursor
Reorganize ops.mlir and invalid.mlir so that all tests for a given op are contiguous. Each op now has a section header comment and tests are ordered following the TD file definition order. Previously the first split of invalid.mlir mixed reshape, equal, and quant_dot tests together, and sigmoid was separated from other unary op tests. Made-with: Cursor
There was a problem hiding this comment.
Pull request overview
This PR cherry-picks changes that mark MIGraphX dialect elementwise operations with MLIR’s Elementwise trait (and adds/strengthens some shape-equality verification for unary/convert ops), while reorganizing and expanding MIGraphX dialect tests to keep per-op coverage grouped together.
Changes:
- Add
Elementwisetrait to MIGraphX elementwise binary/unary ops (and towhere,clip,convert), plus addAllShapesMatchconstraints for unary ops andconvert. - Expand
mlir/test/Dialect/MIGraphX/ops.mlirwith grouped, per-op smoke tests for many elementwise ops. - Reorganize
mlir/test/Dialect/MIGraphX/invalid.mlirand add new negative tests for shaped-type stride/shape mismatches and elementwise op verification.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
mlir/test/Dialect/MIGraphX/ops.mlir |
Reorders and adds per-op CHECK blocks for elementwise ops (plus existing dot/quant_dot coverage). |
mlir/test/Dialect/MIGraphX/invalid.mlir |
Regroups invalid tests by op/type and adds new verifier diagnostics coverage. |
mlir/include/mlir/Dialect/MIGraphX/IR/MIGraphX.td |
Adds Elementwise trait broadly and introduces AllShapesMatch for unary ops and convert. |
Comments suppressed due to low confidence (1)
mlir/include/mlir/Dialect/MIGraphX/IR/MIGraphX.td:141
- The
migraphx.convertop description says it "currently only supports float to float conversions", but the tree contains tests and conversion pipelines that usemigraphx.convertfor integer conversions as well (e.g., MIGraphXToLinalg tests include f32<->i32 cases). Please either update the description to match current supported behavior, or add/enforce a verifier restriction if non-float conversions are truly unsupported.
def MIGraphX_ConvertOp
: MIGraphX_Op<"convert", [Elementwise, AllShapesMatch<["inA", "output"]>]>,
Arguments<(ins AnyMIXRShaped:$inA)>,
Results<(outs AnyMIXRShaped:$output)> {
let summary = "Elementwise type conversion";
let description = [{
Type conversion. Due to impedance mismatches between MIGraphX and Tosa,
currently only supports float to float conversions
}];
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
from #2316 (comment)
why do we need this trait? |
I've not run into cases where preSoftmaxBody has reshapes in it. I can update logic later if needed. This PR is only concerned about marking migraphx ops as elementwise. In general it is good idea to mark them as elementwise so that we can do pattern matching more generically. |
we do have cases like that, for example
please, can you provide more context for this PR? it's difficult to see the usefulness with the code here. |
Yes, i see them. Thanks.
I want to verify that preSoftmaxBody has elementwise ops and reshape ops. If i don't have elementwiseTrait then i'll have to keep a long list in Op verifier for preSoftmaxBody. e.g. i am doing this right now, see how it uses Region &body = getPreSoftmaxBody();
bool hasPreSoftmaxInputs = !getPreSoftmaxElemWiseInputs().empty();
bool hasNonTerminatorOps = false;
for (Block &block : body) {
for (Operation &op : block) {
if (op.hasTrait<OpTrait::IsTerminator>())
continue;
hasNonTerminatorOps = true;
Dialect *dialect = op.getDialect();
bool isMIGraphX = dialect && dialect->getNamespace() == "migraphx";
if (!isMIGraphX || !op.hasTrait<OpTrait::Elementwise>())
return op.emitOpError(
"preSoftmaxBody must only contain elementwise migraphx ops, "
"but found '")
<< op.getName() << "'";
}
}
|
justinrosner
left a comment
There was a problem hiding this comment.
Changes look good to me. Are there any plans on doing something similar for reshape ops? Or is that list short enough where it doesn't matter?
It's most probably going to be short list. If not i can define a trait later |
Cherry picking 6b1119a
Will be useful when doing
migraphx.attentionopAlso rearranges
migraphx/ops.mlirandmigraphx/invalid.mlirtests so that all tests for a operation appear all together.Also adds type contraints on the
migraphx.clipop.