Skip to content

[python] Set up binding for preprocessing transform ops#22227

Merged
bangtianliu merged 1 commit into
iree-org:mainfrom
bangtianliu:transform_python_binding
Oct 10, 2025
Merged

[python] Set up binding for preprocessing transform ops#22227
bangtianliu merged 1 commit into
iree-org:mainfrom
bangtianliu:transform_python_binding

Conversation

@bangtianliu

@bangtianliu bangtianliu commented Oct 7, 2025

Copy link
Copy Markdown
Contributor

This PR adds Python bindings for the Preprocessing Transform Extension ops.
It exposes the transform ops defined in that extension, such as transform.iree.match.contraction, as well as other commonly used ops like transform.iree.match.cast_compatible_dag_from_root and transform.iree.match.dim_is_multiple_of, etc.

The bindings are exposed using declare_mlir_dialect_extension_python_bindings. The logic mainly follows this llvm upstream PR: llvm/llvm-project#159450.

@bangtianliu bangtianliu requested a review from benvanik as a code owner October 7, 2025 06:10
@bangtianliu bangtianliu marked this pull request as draft October 7, 2025 06:10
@bangtianliu bangtianliu force-pushed the transform_python_binding branch 10 times, most recently from 3419a6f to 9f3f729 Compare October 7, 2025 22:46
@bangtianliu bangtianliu marked this pull request as ready for review October 7, 2025 23:13
@bangtianliu bangtianliu requested review from Max191 and kuhar October 7, 2025 23:13

@kuhar kuhar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good overall. Maybe reference the mlir PR you used as a reference in the PR description, so that we can track the overall design/mechanics to that original PR?

Comment thread compiler/bindings/python/test/ir/dialects_test.py Outdated
Comment thread compiler/bindings/python/test/ir/dialects_test.py Outdated
@bangtianliu

bangtianliu commented Oct 8, 2025

Copy link
Copy Markdown
Contributor Author

When updating the test, I noticed that the printed operation from python looks like:

 %batch_dims, %m_dims, %n_dims, %k_dims = transform.iree.match.contraction %arg0, lhs_type = f32, rhs_type = f32, output_type = f32 indexing_maps [#map, #map1, #map2] : !transform.any_op -> !transform.param<i64>

determined by assembly format

let assemblyFormat = [{
    $operand_handle
    `,` `lhs_type` `=` $lhs_type
    `,` `rhs_type` `=` $rhs_type
    `,` `output_type` `=` $output_type
    (`indexing_maps` $indexing_maps^)?   // ← Prints WITHOUT braces!
    attr-dict `:` type($operand_handle) `->` type($batch_dims)
}];

It turns out the parser accepts both of the following formats:

// Format 1: Custom format (what the printer outputs)
transform.iree.match.contraction %op, lhs_type = f32, rhs_type = f32, output_type = f32
  indexing_maps [#map1, #map2, #map3]
  : !transform.any_op -> !transform.param<i64>

// Format 2: Attribute dictionary format (also accepted!)
transform.iree.match.contraction %op, lhs_type = f32, rhs_type = f32, output_type = f32
  {indexing_maps = [#map1, #map2, #map3]}
  : !transform.any_op -> !transform.param<i64>

To make the printed and test formats consistent, I updated the assembly format of transform.iree.match.contraction to :

let assemblyFormat = [{
    $operand_handle
    `,` `lhs_type` `=` $lhs_type
    `,` `rhs_type` `=` $rhs_type
    `,` `output_type` `=` $output_type
    attr-dict                              // ← indexing_maps goes here now
    `:` type($operand_handle) `->` type($batch_dims)
}];

This change doesn’t alter semantics or parsing behavior. It only ensures that the Python-printed module matches the format used in tests.

@kuhar

kuhar commented Oct 8, 2025

Copy link
Copy Markdown
Member

Can we print it as:

`(` `,` `indexing_maps` `=` $indexing_maps^)?

with a leading comma and equals? It's weird to have some attributes printed inline and some in the attribute dictionary

@bangtianliu

Copy link
Copy Markdown
Contributor Author

Can we print it as:

`(` `,` `indexing_maps` `=` $indexing_maps^)?

with a leading comma and equals? It's weird to have some attributes printed inline and some in the attribute dictionary

Updated! The assembly format now prints all attributes inline:

%batch_dims, %m_dims, %n_dims, %k_dims = transform.iree.match.contraction %arg0, lhs_type = f32, rhs_type = f32, output_type = f32, indexing_maps = [#map, #map1, #map2] : !transform.any_op -> !transform.param<i64>

Should I update the tests in preprocessing_match_ops.mlir for consistency, if so I prefer to do it in another separate PR.

@bangtianliu bangtianliu requested a review from kuhar October 8, 2025 15:56
`,` `lhs_type` `=` $lhs_type
`,` `rhs_type` `=` $rhs_type
`,` `output_type` `=` $output_type
(`,` `indexing_maps` `=` $indexing_maps^)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we also use this syntax in all tests and add an op example in let description = above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here is my comment: #22227 (comment)

Should we do it in this PR?

@bangtianliu

Copy link
Copy Markdown
Contributor Author

cc @kuhar for another look, since other PRs are also on top of this.

@kuhar

kuhar commented Oct 10, 2025

Copy link
Copy Markdown
Member

cc @kuhar for another look, since other PRs are also on top of this.

Don't stack PRs when not necessary -- AFAICT, the other PRs don't need these python bindings, and we don't need syntax changes in this PR.

@bangtianliu

Copy link
Copy Markdown
Contributor Author

cc @kuhar for another look, since other PRs are also on top of this.

Don't stack PRs when not necessary -- AFAICT, the other PRs don't need these python bindings, and we don't need syntax changes in this PR.

For python testing, we updated the assembly format for match contraction op in this PR.

@bangtianliu bangtianliu changed the title [python] Set up binding for preprocessing transform ops [python] Set up binding for preprocessing transform ops (1/4) Oct 10, 2025
@kuhar

kuhar commented Oct 10, 2025

Copy link
Copy Markdown
Member

You can either update the assembly format first and then come back and update this PR, or land this first and update the assembly format later. We should avoid stacking PRs when not necessary.

@bangtianliu

bangtianliu commented Oct 10, 2025

Copy link
Copy Markdown
Contributor Author

You can either update the assembly format first and then come back and update this PR, or land this first and update the assembly format later. We should avoid stacking PRs when not necessary.

Ok, could I send a new PR that focuses only on updating the assembly format? The existing PR (#22247) should be closed since it depends on the current one.

@kuhar

kuhar commented Oct 10, 2025

Copy link
Copy Markdown
Member

You can force-push to an existing PR branch to avoid closing/opening PRs

@bangtianliu bangtianliu changed the title [python] Set up binding for preprocessing transform ops (1/4) [python] Set up binding for preprocessing transform ops Oct 10, 2025
@bangtianliu

Copy link
Copy Markdown
Contributor Author

You can force-push to an existing PR branch to avoid closing/opening PRs

Ok the PR is closed automatically when reset --hard and push it to no change. I sent a PR which is independent and pure about updating assembly format: #22270.

bangtianliu added a commit that referenced this pull request Oct 10, 2025
…cher ops (#22270)

According to this comment:
#22227 (comment),
this PR updates the assembly format for contraction and convolution
matcher ops. The attention matcher op's indexing map is not optional and
also consistent with what is suggested in the comment, so no need to
update.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
@bangtianliu bangtianliu force-pushed the transform_python_binding branch from 942931b to a3d441c Compare October 10, 2025 03:49
Signed-off-by: Bangtian Liu <liubangtian@gmail.com>

address the comment and also update the assembly format

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>

update assembly format

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
@bangtianliu bangtianliu force-pushed the transform_python_binding branch from a3d441c to 0e595e3 Compare October 10, 2025 05:51
@bangtianliu bangtianliu merged commit aea660c into iree-org:main Oct 10, 2025
45 checks passed
bangtianliu added a commit that referenced this pull request Oct 15, 2025
…op (#22311)

This PR extends the preprocessing transform dialect Python bindings by
adding `MatchConvolutionOp `and `MatchAttentionOp`, completing the
matcher operation API suite started in #22227.

**Motivation** 
Completes the Python binding API for all preprocessing transform matcher
operations, enabling SHARK Tuner to programmatically generate Transform
Dialect specs for convolutions and attention operations in addition to
contractions. This removes the need for string-based spec generation and
improves type safety and maintainability.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
bangtianliu added a commit to nod-ai/amd-shark-ai that referenced this pull request Oct 20, 2025
This PR uses python binding to build td specs for contraction op.
The python binding is exposed from IREE PR
iree-org/iree#22227 (MatchContractionOp
binding).

Right now, the code still contains two paths for building TD specs:
- Contraction ops: Use new Python bindings via
`build_contraction_td_spec()`
- Convolution/Attention ops: Still use old string template approach via
`build_td_spec()`

After this PR, I will also send PRs for convolution and attention ops to
migrate them to Python bindings. Once all operations are supported, some
code relevant to temporary solutions and string-based method will be
fully cleaned up.

---------

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
weidel-p pushed a commit to weidel-p/iree that referenced this pull request Oct 21, 2025
…cher ops (iree-org#22270)

According to this comment:
iree-org#22227 (comment),
this PR updates the assembly format for contraction and convolution
matcher ops. The attention matcher op's indexing map is not optional and
also consistent with what is suggested in the comment, so no need to
update.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
Signed-off-by: Philipp <philipp.weidel@intel.com>
weidel-p pushed a commit to weidel-p/iree that referenced this pull request Oct 21, 2025
This PR adds Python bindings for the Preprocessing Transform Extension
ops.
It exposes the transform ops defined in that extension, such as
`transform.iree.match.contraction`, as well as other commonly used ops
like `transform.iree.match.cast_compatible_dag_from_root `and
`transform.iree.match.dim_is_multiple_of`, etc.

The bindings are exposed using
`declare_mlir_dialect_extension_python_bindings`. The logic mainly
follows this llvm upstream PR:
llvm/llvm-project#159450.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
Signed-off-by: Philipp <philipp.weidel@intel.com>
weidel-p pushed a commit to weidel-p/iree that referenced this pull request Oct 21, 2025
…op (iree-org#22311)

This PR extends the preprocessing transform dialect Python bindings by
adding `MatchConvolutionOp `and `MatchAttentionOp`, completing the
matcher operation API suite started in iree-org#22227.

**Motivation** 
Completes the Python binding API for all preprocessing transform matcher
operations, enabling SHARK Tuner to programmatically generate Transform
Dialect specs for convolutions and attention operations in addition to
contractions. This removes the need for string-based spec generation and
improves type safety and maintainability.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
Signed-off-by: Philipp <philipp.weidel@intel.com>
mischirmer pushed a commit to mischirmer/iree that referenced this pull request Nov 24, 2025
…cher ops (#22270)

According to this comment:
iree-org/iree#22227 (comment),
this PR updates the assembly format for contraction and convolution
matcher ops. The attention matcher op's indexing map is not optional and
also consistent with what is suggested in the comment, so no need to
update.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
pstarkcdpr pushed a commit to pstarkcdpr/iree that referenced this pull request Nov 28, 2025
This PR adds Python bindings for the Preprocessing Transform Extension
ops.
It exposes the transform ops defined in that extension, such as
`transform.iree.match.contraction`, as well as other commonly used ops
like `transform.iree.match.cast_compatible_dag_from_root `and
`transform.iree.match.dim_is_multiple_of`, etc.

The bindings are exposed using
`declare_mlir_dialect_extension_python_bindings`. The logic mainly
follows this llvm upstream PR:
llvm/llvm-project#159450.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
pstarkcdpr pushed a commit to pstarkcdpr/iree that referenced this pull request Nov 28, 2025
…op (iree-org#22311)

This PR extends the preprocessing transform dialect Python bindings by
adding `MatchConvolutionOp `and `MatchAttentionOp`, completing the
matcher operation API suite started in iree-org#22227.

**Motivation** 
Completes the Python binding API for all preprocessing transform matcher
operations, enabling SHARK Tuner to programmatically generate Transform
Dialect specs for convolutions and attention operations in addition to
contractions. This removes the need for string-based spec generation and
improves type safety and maintainability.

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
RattataKing pushed a commit to nod-ai/amd-shark-ai that referenced this pull request Mar 2, 2026
This PR uses python binding to build td specs for contraction op.
The python binding is exposed from IREE PR
iree-org/iree#22227 (MatchContractionOp
binding).

Right now, the code still contains two paths for building TD specs:
- Contraction ops: Use new Python bindings via
`build_contraction_td_spec()`
- Convolution/Attention ops: Still use old string template approach via
`build_td_spec()`

After this PR, I will also send PRs for convolution and attention ops to
migrate them to Python bindings. Once all operations are supported, some
code relevant to temporary solutions and string-based method will be
fully cleaned up.

---------

Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
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