Skip to content

[Encoding] Add getOffsetSizesStrides interface for load/store materialization - #20741

Merged
jtuyls merged 1 commit into
iree-org:mainfrom
jtuyls:collapse-materialize-load-store
May 15, 2025
Merged

jtuyls merged 1 commit into
iree-org:mainfrom
jtuyls:collapse-materialize-load-store

Conversation

@jtuyls

@jtuyls jtuyls commented May 6, 2025 •

Copy link
Copy Markdown
Contributor

This PR adds the getOffsetSizesStrides interface method as part of the effort to collapse MaterializeEncodingIntoPadding into MaterializeEncoding: #20160.

This should enable the collapsing of the padding materialization patterns for dispatch.tensor.load and dispatch.tensor.store into the generic one, however, this is intentionally left out of the PR as @MaheshRavishankar is looking at adjusting the MaterializeEncodingIntoPadding pass for dynamic padding.

The declaration looks like this:

/// Returns success if materialized `newOffsets`, `newSizes` and
/// `newStrides` can be calculated and set for the slice specified by
/// `offsets`, `sizes` and `strides` on the dispatch tensor `type` with
/// potential `dynamicDims` sizes.
LogicalResult
getOffsetsSizesStrides(OpBuilder &builder, Location loc,
                         IREE::TensorExt::DispatchTensorType type,
                         IREE::Codegen::LayoutAttrInterface layoutAttr,
                         ValueRange dynamicDims,
                         ArrayRef<OpFoldResult> offsets,
                         ArrayRef<OpFoldResult> sizes,
                         ArrayRef<OpFoldResult> strides,
                         SmallVectorImpl<OpFoldResult> &newOffsets,
                         SmallVectorImpl<OpFoldResult> &newSizes,
                         SmallVectorImpl<OpFoldResult> &newStrides);

Two notes on this:

  • In general, the old offsets, sizes and strides should be passed as well, but the current materialization patterns assumes whole loads/stores (isLoadOfWholeSource, isStoreToWholeTarget). Imo this could be left to future work/a follow up. The old offsets, sizes and strides are passed, but internally the implementations check for and only implement whole slices. Support for partial slices if left for future work to avoid changing the functionality (and adding corresponding tests) in this PR.
  • The targetAttr is passed as an Attribute instead of IREE::HAL::ExecutableTargetAttr to avoid a circular dependency between the HAL and Encoding dialects. This might need a better solution though longer term, i.e. avoiding the need to pass this attribute as it's only needed to check for a ukernel in the VMVX backend or by removing the HAL dependency on Encoding? The VMVX ukernel information is retrieved from the layoutAttr (IREE::Codegen::LayoutAttrInterface).

@jtuyls
jtuyls force-pushed the collapse-materialize-load-store branch 3 times, most recently from c9c5ea4 to de074b7 Compare May 6, 2025 20:48

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have a strong concern about passing executable target to the interface method, see the other comment. I think the VMVX ukernel support has been a pain and I can help fix it first. Let me know what you think, thanks!

In general, the old offsets, sizes and strides should be passed as well, but the current materialization patterns assumes whole loads/stores (isLoadOfWholeSource, isStoreToWholeTarget). Imo this could be left to future work/a follow up.

We can decouple them a bit. The whole loads/stores are implementation details in the patterns. The interface provides the transformed sizes/strides/offsets. The patterns make the decision to bail out when it is not a full load/store, and how we define interface methods should not be guided by their implementation details. I don't have a strong opinion, and we can leave them to future work. Just sharing my thought about this comment.

@jtuyls

jtuyls commented May 7, 2025

Copy link
Copy Markdown
Contributor Author

We can decouple them a bit. The whole loads/stores are implementation details in the patterns. The interface provides the transformed sizes/strides/offsets. The patterns make the decision to bail out when it is not a full load/store, and how we define interface methods should not be guided by their implementation details. I don't have a strong opinion, and we can leave them to future work. Just sharing my thought about this comment.

Yeah, that's fair and the reason I didn't do it yet is that I would have to adjust the current implementations of the interface to support a partial load/stores and then I might just update the patterns as well to support it. And my thinking was to do a follow-up as it actually changes the implementation.

@hanhanW

hanhanW commented May 8, 2025

Copy link
Copy Markdown
Contributor

We can decouple them a bit. The whole loads/stores are implementation details in the patterns. The interface provides the transformed sizes/strides/offsets. The patterns make the decision to bail out when it is not a full load/store, and how we define interface methods should not be guided by their implementation details. I don't have a strong opinion, and we can leave them to future work. Just sharing my thought about this comment.

Yeah, that's fair and the reason I didn't do it yet is that I would have to adjust the current implementations of the interface to support a partial load/stores and then I might just update the patterns as well to support it. And my thinking was to do a follow-up as it actually changes the implementation.

I see your point, and it is fair to me. Thanks for your sharing!

@jtuyls
jtuyls force-pushed the collapse-materialize-load-store branch 2 times, most recently from ce02d76 to a242296 Compare May 8, 2025 13:30
@jtuyls
jtuyls requested a review from benvanik as a code owner May 8, 2025 13:30
@jtuyls

jtuyls commented May 8, 2025

Copy link
Copy Markdown
Contributor Author

We can decouple them a bit. The whole loads/stores are implementation details in the patterns. The interface provides the transformed sizes/strides/offsets. The patterns make the decision to bail out when it is not a full load/store, and how we define interface methods should not be guided by their implementation details. I don't have a strong opinion, and we can leave them to future work. Just sharing my thought about this comment.

Yeah, that's fair and the reason I didn't do it yet is that I would have to adjust the current implementations of the interface to support a partial load/stores and then I might just update the patterns as well to support it. And my thinking was to do a follow-up as it actually changes the implementation.

I see your point, and it is fair to me. Thanks for your sharing!

I did think more about this today and I think we can already add the old offsets, sizes and strides to the interface and then just return a failure in case of a partial access. I updated the PR for that, but I did leave the old checks around though for the default implementation (https://github.com/iree-org/iree/pull/20741/files#diff-9b275dec6a3f214c6e32c1d9ebd8b75a50ad5d84be23c9db090f5a1e8e667af4R195) as I would otherwise need to enable partial slices there and add tests for it and I would prefer to do that in a follow up.

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the awesome work, and it helps me define the encodings that we are inventing!

Comment on lines +41 to +48
FailureOr<SmallVector<OpFoldResult>> getPackedDimsForDispatchTensor(
OpBuilder &builder, Location loc,
IREE::TensorExt::DispatchTensorType dispatchTensorType,
ValueRange dynamicDims) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I checked a couple of times, and I think it is not used at all?

I'd expect a cleanup in MaterializeInterfaceBindingEncoding, which calls getPackedDynamicDimsForDispatchTensor that uses a local getPackedDimsForDispatchTensor method. Is it possible to remove the local methods now?

EDIT: RE my second question, I think it is not possible at the moment, because sizes != dims. We'll need a different interface method to get rid of it.

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.

Yes, the idea was to use this method for the remaining use of getPackedDimsForDispatchTensor in MaterializeEncodingPatterns, so removed the local version of that one and getSwizzledShape. This seems perfectly possible, not sure what issue you were expecting with:

EDIT: RE my second question, I think it is not possible at the moment, because sizes != dims

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I probably misread the code previously, but it looks good to me now. Please add a doc to it.

It can be done in a follow-up. IMO, it deserves an interface method like getOffsetsSizesStrides. Maybe we can name it as getMaterializedDynamicDims(). (Any other suggestion is welcome.) We don't have a use case today, because padding only works for static shape. However, I can imagine that different kinds of relayout need something similar to this.

Furthermore, I think I will need it being an interface method for tracing encoding types for host, see the last paragraph in #20450 for more details. I think this is outside the scope of your PR, so let me pick this up when I start looking at the tracing support.

Moving it to the type converter looks okay to me, just wanna point out that we would make it be an interface method in the future. I'll follow up on this after you land the PR.

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.

I probably misread the code previously, but it looks good to me now. Please add a doc to it.

Done.

Moving it to the type converter looks okay to me, just wanna point out that we would make it be an interface method in the future. I'll follow up on this after you land the PR.

Yes, it doesn't have to stay within the type converter, but for now I think it's an improvement to make the functions reusable with relatively clean interfaces (no need to pass the type converter to it, no need to access/pass the layout attr around as it's already a member in the type converter).

I can see it being useful as an interface as well as it's quite general and it could be implemented for padding. I wouldn't have gone for it as long as there was only a single use for it or if it was only being used within getOffsetsSizesStrides, but if you see a need for tracing support as well that makes sense. One thing I am wondering though is whether we can generally implement getOffsetsSizesStrides with getMaterializedDynamicDims because then we potentially only need the latter as an interface.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One thing I am wondering though is whether we can generally implement getOffsetsSizesStrides with getMaterializedDynamicDims because then we potentially only need the latter as an interface.

I think they may be different. The getOffsetsSizesStrides is for materializing a slice, and getMaterializedDynamicDims is for materializing the whole tensor. I'd say that getMaterializedDynamicDims is a syntax sugar version of the other one. Having both looks okay to me atm. Let's see after I have tracing prototype support. :)

":EncodingInterfacesGen",
":EncodingOpsIncGen",
":EncodingTypesGen",
"//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is something we want to avoid in terms of dependency. The rule is Util/Encoding/TensorExt should not depend on other IREE dialects. I see why this happens, and I think we probably want to move the interface to Encoding dialect.

I started the work from Codegen and I wanted to have a clear line between codegen and host. This is why I introduced the interface in codegen dialect.

IMO, it is reasonable to move the interface to Encoding because we still have codegen specific implemented in compiler/Codegen/ExternalInterfaces. And now I also feel that the new interface method can belong to LayoutAttrInterface after we move the interface to the encoding dialect.

Let me share more about my thinking about the interfaces here, and I'll also update the issue #20742

  • EncodingLayoutResolverAttrInterface is an interface that help resolve verbose encodings into serialized/specialized encodings.
  • SerializableEncodingAttrInterface is an interface that describes encoding properties and should have enough information for later transformation.
  • LayoutAttrInterface is an interface that provides a set of interface method to materialize serialized encoding to physical operations/types/etc.

With the definition, there are three states for an encoding. (a) Verbose encoding -> (b) serialized encoding -> (c) physical operations and types.

EncodingLayoutResolverAttrInterface helps (a) -> (b).
SerializableEncodingAttrInterface describes the properties for (b).
LayoutAttrInterface helps (b) -> (c).

Does it sound reasonable to you? What do you think? (more eyes are better, and I'd appreciate your feedback!)

(I was a little struggle when I wrote the issue. I could not figure this out until you connect these together. I'm sorry about that and I really appreciate your work!)

@benvanik we might add this dependency temporarily, and we can drop it soon. I've been fighting with better definition for encodings and the work helps a lot. If you have a concern about this temporary dep, either @jtuyls or I can move the interface first. This work is mainly to put padding encoding to a stable state.

@hanhanW hanhanW May 9, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(Reason that I'm not moving the interface now is that I want to have the better definition first. I'm pretty sure that we can drop the dep in the future. If I really fail on it, it can always be Attribute and we drop the dep)

@jtuyls jtuyls May 9, 2025 •

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.

This is something we want to avoid in terms of dependency. The rule is Util/Encoding/TensorExt should not depend on other IREE dialects. I see why this happens, and I think we probably want to move the interface to Encoding dialect.

Yes, 100%! I was thinking the same when I added it and I was going to propose moving the interface after I had a deeper look. Should have left a note on it.

With the definition, there are three states for an encoding. (a) Verbose encoding -> (b) serialized encoding -> (c) physical operations and types.

EncodingLayoutResolverAttrInterface helps (a) -> (b).
SerializableEncodingAttrInterface describes the properties for (b).
LayoutAttrInterface helps (b) -> (c).

Does it sound reasonable to you? What do you think? (more eyes are better, and I'd appreciate your feedback!)

Yes, I think this makes a lot of sense! There are two 'smells' related to this LayoutAttrInterface parameter in the interface method. One is that it introduces this dependency and the other is that LayoutAttrInterface might be the better home for this method as then LayoutAttrInterface doesn't need to be passed. The above description of the relation between the three interfaces clears up a lot and following that we should move getOffsetsSizesStrides and convertType to that interface. Then it might not be technically necessary to move LayoutAttrInterface to Encoding to avoid the dependency, but I feel like it would still be more logical to have the three interfaces within the same space.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One is that it introduces this dependency and the other is that LayoutAttrInterface might be the better home for this method as then LayoutAttrInterface doesn't need to be passed.

This was something in my mind, and I forgot to note in the comment. Good catch! :)

Okay, I think we have a solid definition for the interfaces and encodings. What is the execution plan? Do you want to land the PR first, or move the interface to the Encoding dialect first? I don't have a strong opinion because now I can see a path to the finial picture we had discussed.

@jtuyls jtuyls May 10, 2025 •

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.

Ok, I am currently thinking the following:

  1. Move LayoutAttrInterface
  2. Move convertType to LayoutAttrInterface
  3. (This PR) Add getOffsetsSizesStrides or getMaterializedDynamicDims (if we wouldn't need getOffsetsSizesStrides)
  4. Enable partial slices as a follow up (added for completeness)

If there is a need to get in this work earlier, we can start with merging this, but any dependent work might be impacted anyway until 1-3 is done as interface methods would be moved around.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SGTM, I think we don't have other dependent work. Just in case if you want to land this, since you already made significant improvements in the PR. Thanks for keeping the codebase state clean and better!

@jtuyls
jtuyls force-pushed the collapse-materialize-load-store branch from a242296 to be8cb22 Compare May 9, 2025 07:22

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me, and I enjoy the discussion in the review! Thanks for making it happen, how we layer encodings is going to be awesome!

Just a couple of nits, and a question about the execution plan. I think it is very close to land.

":EncodingInterfacesGen",
":EncodingOpsIncGen",
":EncodingTypesGen",
"//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One is that it introduces this dependency and the other is that LayoutAttrInterface might be the better home for this method as then LayoutAttrInterface doesn't need to be passed.

This was something in my mind, and I forgot to note in the comment. Good catch! :)

Okay, I think we have a solid definition for the interfaces and encodings. What is the execution plan? Do you want to land the PR first, or move the interface to the Encoding dialect first? I don't have a strong opinion because now I can see a path to the finial picture we had discussed.

Comment on lines +41 to +48
FailureOr<SmallVector<OpFoldResult>> getPackedDimsForDispatchTensor(
OpBuilder &builder, Location loc,
IREE::TensorExt::DispatchTensorType dispatchTensorType,
ValueRange dynamicDims) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I probably misread the code previously, but it looks good to me now. Please add a doc to it.

It can be done in a follow-up. IMO, it deserves an interface method like getOffsetsSizesStrides. Maybe we can name it as getMaterializedDynamicDims(). (Any other suggestion is welcome.) We don't have a use case today, because padding only works for static shape. However, I can imagine that different kinds of relayout need something similar to this.

Furthermore, I think I will need it being an interface method for tracing encoding types for host, see the last paragraph in #20450 for more details. I think this is outside the scope of your PR, so let me pick this up when I start looking at the tracing support.

Moving it to the type converter looks okay to me, just wanna point out that we would make it be an interface method in the future. I'll follow up on this after you land the PR.

@jtuyls
jtuyls force-pushed the collapse-materialize-load-store branch from be8cb22 to 8983b2a Compare May 10, 2025 07:48
jtuyls added a commit that referenced this pull request May 13, 2025
As discussed here:
#20741 (comment), the
`LayoutAttrInterface` belongs in the Encoding and should contain the
interface methods for materializing encodings. This PR moves the
`LayoutAttrInterface` to the Encoding dialect. Other follow-up PRs will
take care of moving `convertType` and `getOffsetsSizesStrides` to it.

---------

Signed-off-by: Jorn Tuyls <[email protected]>
jtuyls added a commit that referenced this pull request May 13, 2025
The `convertType` interface method, which converts a type into its
materialized form, belongs in the Encoding's `LayoutAttrInterface`
instead of the `SerializableEncodingAttrInterface` as the former is
responsible for materializing encodings, while the latter describes
properties of serialized encodings, see
#20741 (comment).

Signed-off-by: Jorn Tuyls <[email protected]>
@jtuyls
jtuyls force-pushed the collapse-materialize-load-store branch from 8983b2a to 99bead3 Compare May 14, 2025 08:18
@jtuyls

jtuyls commented May 14, 2025

Copy link
Copy Markdown
Contributor Author

@hanhanW I rebased this on main and moved the getOffsetsSizesStrides interface method to the LayoutAttrInterface. Could you review again?

Comment on lines +41 to +48
FailureOr<SmallVector<OpFoldResult>> getPackedDimsForDispatchTensor(
OpBuilder &builder, Location loc,
IREE::TensorExt::DispatchTensorType dispatchTensorType,
ValueRange dynamicDims) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One thing I am wondering though is whether we can generally implement getOffsetsSizesStrides with getMaterializedDynamicDims because then we potentially only need the latter as an interface.

I think they may be different. The getOffsetsSizesStrides is for materializing a slice, and getMaterializedDynamicDims is for materializing the whole tensor. I'd say that getMaterializedDynamicDims is a syntax sugar version of the other one. Having both looks okay to me atm. Let's see after I have tracing prototype support. :)

@jtuyls
jtuyls merged commit 626fa14 into iree-org:main May 15, 2025
@jtuyls
jtuyls deleted the collapse-materialize-load-store branch May 15, 2025 07:39
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
As discussed here:
#20741 (comment), the
`LayoutAttrInterface` belongs in the Encoding and should contain the
interface methods for materializing encodings. This PR moves the
`LayoutAttrInterface` to the Encoding dialect. Other follow-up PRs will
take care of moving `convertType` and `getOffsetsSizesStrides` to it.

---------

Signed-off-by: Jorn Tuyls <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
The `convertType` interface method, which converts a type into its
materialized form, belongs in the Encoding's `LayoutAttrInterface`
instead of the `SerializableEncodingAttrInterface` as the former is
responsible for materializing encodings, while the latter describes
properties of serialized encodings, see
#20741 (comment).

Signed-off-by: Jorn Tuyls <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…lization (#20741)

This PR adds the `getOffsetSizesStrides` interface method as part of the
effort to collapse MaterializeEncodingIntoPadding into
MaterializeEncoding: #20160.

This should enable the collapsing of the padding materialization
patterns for `dispatch.tensor.load` and `dispatch.tensor.store` into the
generic one, however, this is intentionally left out of the PR as the
MaterializeEncodingIntoPadding pass is being adjusted for dynamic
padding.

The declaration looks like this:
```cpp
/// Returns success if materialized `newOffsets`, `newSizes` and
/// `newStrides` can be calculated and set for the slice specified by
/// `offsets`, `sizes` and `strides` on the dispatch tensor `type` with
/// potential `dynamicDims` sizes.
LogicalResult
getOffsetsSizesStrides(OpBuilder &builder, Location loc,
                         IREE::TensorExt::DispatchTensorType type,
                         IREE::Codegen::LayoutAttrInterface layoutAttr,
                         ValueRange dynamicDims,
                         ArrayRef<OpFoldResult> offsets,
                         ArrayRef<OpFoldResult> sizes,
                         ArrayRef<OpFoldResult> strides,
                         SmallVectorImpl<OpFoldResult> &newOffsets,
                         SmallVectorImpl<OpFoldResult> &newSizes,
                         SmallVectorImpl<OpFoldResult> &newStrides);
```
Two notes on this:
- The old offsets, sizes and
strides are passed, but internally the implementations check for and
only implement whole slices. Support for partial slices if left for
future work to avoid changing the functionality (and adding
corresponding tests) in this PR.
- The VMVX ukernel information is retrieved from
the layoutAttr (IREE::Encoding::LayoutAttrInterface).

Signed-off-by: Jorn Tuyls <[email protected]>
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