Skip to content

[Codegen] Add pass for specializing executable variants - #20771

Merged
qedawkins merged 4 commits into
iree-org:mainfrom
qedawkins:split_dispatch_on_util_assume
May 27, 2025
Merged

qedawkins merged 4 commits into
iree-org:mainfrom
qedawkins:split_dispatch_on_util_assume

Conversation

@qedawkins

Copy link
Copy Markdown
Contributor

This allows for kernel specialization by way of annotating tilable ops with the preferred iteration ranges and divisibilities to specialize for. For example,

%35 = linalg.matmul ins(%30, %31 : tensor<?x?xf16>, tensor<?x?xf16>) outs(%34 : tensor<?x?xf32>)
  {iree_codegen.specialization_ranges = #util<int.assumption.multi_array[
    [<umin = 128, umax = 4096, udiv = 128>, <umin = 128, umax = 4096, udiv = 128>, <umin = 64, udiv = 64>],
    [<umin = 4096, udiv = 256>, <umin = 4096, udiv = 256>, <udiv = 64>]
]>} -> tensor<?x?xf32>

This matmul will be specialized into up to 3 variants, one default, and two for each list of ranges. More details can be found in the code documentation.

To specialize a dispatch, the pass queries the iteration domain of the tilable op and looks for a producer util.int.assume op. The function is then cloned and the assume is updated to match the requested ranges.

This is just one way to implement kernel specialization, in the future we can implement many more options (e.g. based on estimated TOPs or smallest individual dimension) and that's without considering what device we're targeting. The implementation here supports many common cases of minimum/maximum sizes and alignments. Learnings here will translate to future improvements for other strategies.

This pass runs first thing in the configuration pipelines because it needs to apply to the variant, and should run before passes like BlockDynamicDims which takes advantage of util.assume.int information that this pass refines.

@qedawkins
qedawkins force-pushed the split_dispatch_on_util_assume branch from e9c94c1 to 3e02c28 Compare May 9, 2025 21:19
@benvanik

benvanik commented May 9, 2025

Copy link
Copy Markdown
Collaborator

This is awesome :)

@qedawkins
qedawkins force-pushed the split_dispatch_on_util_assume branch from c25be2d to 4db7b26 Compare May 12, 2025 20:26
@qedawkins

Copy link
Copy Markdown
Contributor Author

Depends on #20801

qedawkins added 4 commits May 27, 2025 16:48
This allows for kernel specialization by way of annotating tilable ops
with the preferred iteration ranges and divisibilities to specialize
for. For example,

```
%35 = linalg.matmul ins(%30, %31 : tensor<?x?xf16>, tensor<?x?xf16>) outs(%34 : tensor<?x?xf32>)
  {iree_codegen.specialization_ranges = #util<int.assumption.multi_array[
    [<umin = 128, umax = 4096, udiv = 128>, <umin = 128, umax = 4096, udiv = 128>, <umin = 64, udiv = 64>],
    [<umin = 4096, udiv = 256>, <umin = 4096, udiv = 256>, <udiv = 64>]
]>} -> tensor<?x?xf32>
```

This matmul will be specialized into up to 3 variants, one default, and
two for each list of ranges. More details can be found in the code
documentation.

To specialize a dispatch, the pass queries the iteration domain of the
tilable op and looks for a producer `util.int.assume` op. The function
is then cloned and the assume is updated to match the requested ranges.

This is just one way to implement kernel specialization, in the future
we can implement many more options (e.g. based on estimated TOPs or
smallest individual dimension) and that's without considering what
device we're targeting. The implementation here supports many common
cases of minimum/maximum sizes and alignments. Learnings here will
translate to future improvements for other strategies.

This pass runs first thing in the configuration pipelines because it
needs to apply to the variant, and should run before passes like
BlockDynamicDims which takes advantage of `util.assume.int` information
that this pass refines.
@qedawkins
qedawkins force-pushed the split_dispatch_on_util_assume branch from 96f1217 to 8b585f6 Compare May 27, 2025 20:50
@qedawkins
qedawkins requested a review from benvanik May 27, 2025 20:51

@benvanik benvanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

5 or 6 years into the project and we finally have specialization based on dispatch sites - so cool :)

@qedawkins
qedawkins merged commit bb906c1 into iree-org:main May 27, 2025
@qedawkins
qedawkins deleted the split_dispatch_on_util_assume branch May 27, 2025 21:30
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
This allows for kernel specialization by way of annotating tilable ops
with the preferred iteration ranges and divisibilities to specialize
for. For example,

```
%35 = linalg.matmul ins(%30, %31 : tensor<?x?xf16>, tensor<?x?xf16>) outs(%34 : tensor<?x?xf32>)
  {iree_codegen.specialization_ranges = #util<int.assumption.multi_array[
    [<umin = 128, umax = 4096, udiv = 128>, <umin = 128, umax = 4096, udiv = 128>, <umin = 64, udiv = 64>],
    [<umin = 4096, udiv = 256>, <umin = 4096, udiv = 256>, <udiv = 64>]
]>} -> tensor<?x?xf32>
```

This matmul will be specialized into up to 3 variants, one default, and
two for each list of ranges. More details can be found in the code
documentation.

To specialize a dispatch, the pass queries the iteration domain of the
tilable op and looks for a producer `util.int.assume` op. The function
is then cloned and the assume is updated to match the requested ranges.

This is just one way to implement kernel specialization, in the future
we can implement many more options (e.g. based on estimated TOPs or
smallest individual dimension) and that's without considering what
device we're targeting. The implementation here supports many common
cases of minimum/maximum sizes and alignments. Learnings here will
translate to future improvements for other strategies.

This pass runs first thing in the configuration pipelines because it
needs to apply to the variant, and should run before passes like
BlockDynamicDims which takes advantage of `util.assume.int` information
that this pass refines.
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