Skip to content

[Encoding] Add convertType interface to generalize type conversion - #20700

Merged
jtuyls merged 1 commit into
iree-org:mainfrom
jtuyls:materialize-encoding-into-padding-convert-type-2
May 5, 2025
Merged

jtuyls merged 1 commit into
iree-org:mainfrom
jtuyls:materialize-encoding-into-padding-convert-type-2

Conversation

@jtuyls

@jtuyls jtuyls commented May 1, 2025 •

Copy link
Copy Markdown
Contributor

Adds the convertType interface method to SerializableEncodingAttrInterface. This is the first stage for collapsing MaterializeEncodingIntoPadding into MaterializeEncoding, see #20160.

Note that this removes the need for MaterializePadEncodingTypeConverter if the convertType method is implemented for PadEncodingLayoutAttr, however, this is intentionally left out of the PR as @MaheshRavishankar is looking at adjusting the MaterializeEncodingIntoPadding for dynamic padding. To check what it looks like with MaterializePadEncodingTypeConverter collapsed into MaterializeEncodingTypeConverter you can view this branch instead: https://github.com/iree-org/iree/compare/main...jtuyls:materialize-encoding-into-padding-convert-type?expand=1.

Second note that while the type conversion for EncodingNopLayoutAttr was previously combined with the common implementation for CPUEncodingLayoutAttr, VMVXEncodingLayoutAttr and GPUEncodingLayoutAttr, this PR provides a minimal standalone implementation for clarity on what's going on. CPUEncodingLayoutAttr, VMVXEncodingLayoutAttr and GPUEncodingLayoutAttr still share the same implementation as they did previously through the convertType implementation in HostSerializableEncodingAttrInterfaceExternalModelBase. EncodingNopLayoutAttr now implements both IREE::Encoding::EncodingLayoutResolverAttrInterface and IREE::Encoding::SerializableEncodingAttrInterface to go down the same path as the above attributes, but provides its own implementation.

@jtuyls
jtuyls force-pushed the materialize-encoding-into-padding-convert-type-2 branch from 92adf73 to ca5f6d0 Compare May 1, 2025 21:42
}

Attribute getLayout(Attribute attr, RankedTensorType type) const {
return EncodingNopLayoutAttr::get(attr.getContext());

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.

Is it correct? I thought that it should be something like identity pad encoding attribute.

  MLIRContext *ctx = getContext();
  SmallVector<int64_t> zeros(type.getRank(), 0);
  return IREE::Encoding::PadEncodingLayoutAttr::get(
      ctx, DenseI64ArrayAttr::get(ctx, zeros));

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 don't get this, why does it need to become a PadEncodingLayoutAttr? I guess it could become any attribute that implements SerializableEncodingAttrInterface and is an identity layout, but then it would go down a different route in the type converter. What I did here is make EncodingNopLayoutAttr a SerializableEncodingAttrInterface as well, so that it can just return itself and handle its own type conversion (which is likely indeed the same as the type conversion for any other encoding with identity layout).

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 good question, and I've been thinking it whole today. You are right that it can be any attribute that implements SerializableEncodingAttrInterface. I over-index on the point that I want to separate "tensor encoding attribute" and "encoding resolvers" when possible. And I made a mistake that ties SerializableEncodingAttrInterface to tensor encoding attribute. Because returning a resolver smelled weird to me. Now I realized that also what we've been doing.

Suggested change
return EncodingNopLayoutAttr::get(attr.getContext());
return attr;

Let me put my thoughts here for visibility, and I'll convert them to issues if I figure action items.

IMO, there are two types of encodings. One is the encodings that will be attached to tensor types, and the other is the encoding resolvers. E.g., iree_encoding.encoding and iree_encoding.layout are tensor encoding attributes, iree_gpu.pad_layout_attr is an encoding resolver. I want to keep a clear line between these two categories. It might not be feasible as they evolve, but so far I haven't seen an example that could break my mental model.

My mistake was that I thought the return value of getLayout() falls into the former category. E.g., SpecializedEncodingAttr is not an encoding resolver to me, but it implements the interface for getLayout method. I implemented before we have today's understanding, and I'll fix it.

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.

#20718 fixes the issue that leads me to the wrong direction.

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.

IMO, there are two types of encodings. One is the encodings that will be attached to tensor types, and the other is the encoding resolvers. E.g., iree_encoding.encoding and iree_encoding.layout are tensor encoding attributes, iree_gpu.pad_layout_attr is an encoding resolver. I want to keep a clear line between these two categories.

That makes sense to me. I got guided by CPUEncodingLayoutAttr, VMVXEncodingLayoutAttr and GPUEncodingLayoutAttr implementing both the resolver and serializable encoding interfaces. On the other hand, GPUPadLayoutAttr only implements resolver interface and its getLayout returns a serializable PadEncodingLayoutAttr. We should probably standardize this? And based on your comment, I think we would ideally want separate attributes for the encoding resolvers and encodings?

Also, it might help making a clear distinction if we had some visual clue on whether an attribute is a resolver or not, like renaming GPUPadLayoutAttr to GPUPadLayoutResolverAttr?

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.

Yes, let me try to collect what we have for the encodings, and we will get a clearer picture. I think we are close to a point that we can have concrete definition, and I can document them in EncodingBase.td, like the other dialects. E.g., FlowBased.td:

let description = [{
The flow dialect is used to model regions of dense computation and the data
flow between them. MLIR value-semantic tensors are used as the primary data
type to allow SSA use-def to provide a bulk of the infrastructure required
to perform the computation partitioning and outlining.
The dialect is designed to ingest relatively high-level linear algebra via
XLA HLO ops (that also operate on the value-semantic tensor types) and
optionally MLIR standard ops for control flow and other actions. After
conversion of any higher-level ops that have special semantics in the flow
dialect, such as global variables, the rest are partitioned into regions
containing simple and compatible computations. Finally, outlining moves the
computations into executables and leaves only the execution flow encoded via
dispatch operations.
The primary unit of interest is a "dispatch region" containing compatible
computations that can be scheduled together efficiently (and safely).
"Compatible" here is specified as similarly shaped workloads that indicate
how many invocations a computation can be parallelized across when running
in a SPMD execution model. Though it depends on the particular runtime
backends this more concretely means things like the untiled workload
(or tiled workgroups) used in GPU dispatches or similar thread pool
executors.
After identification of the dispatchable regions a set of transformations
performs folding and simplification to reduce the total number of
dispatches. Heuristics are used in certain cases to more efficiently
schedule special ops (such as GEMM) and the design is amenable to profile-
guided analysis that can be added in the future.
The resulting outlined executable modules containing the dispatchable code
can be translated to one or more backends (such as SPIR-V for Vulkan, or
LLVM IR for running on the CPU, etc). The IR that is outlined is untouched
and in the input format (such as XLA HLO ops) allowing conversion using any
MLIR target that supports ingesting such input. A few special ops are used
to communicate statically available information such as the expected
workload size, shapes of inputs and outputs, etc.
}];

RE renaming, I'll create an issue and get back to this. I might need to tweak UnspcecializedEncodingAttr, or have a new resolver (e.g., UnspecializedEncodingResolver) that converts unspecialized_encoding to specialized encoding. I'd also like to discuss the naming after I create the issue. E.g., could we drop Encoding from the naming, since they are already within Encoding namespace? I'll propose the new names in the issue.

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'd also like to discuss the naming after I create the issue. E.g., could we drop Encoding from the naming, since they are already within Encoding namespace? I'll propose the new names in the issue.

+1 on that, I was thinking the same.

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 created an identity layout resolver when I worked on encoding specialization. Can you help evaluate if we can retire the Codegen one and switch it to IREE::Encoding::IdentityEncoding in a follow-up? I think it will have to implement lowerOp method, then it can work.

https://github.com/iree-org/iree/blob/main/compiler/src/iree/compiler/Dialect/Encoding/IR/EncodingAttrs.td#L297-L312

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.

Let me take it over because I need to revisit the old encoding attributes.

@jtuyls
jtuyls force-pushed the materialize-encoding-into-padding-convert-type-2 branch 3 times, most recently from 67fab2f to 458a3ad Compare May 2, 2025 10: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.

LGTM % 2 minor nits.

I made a mistake in the previous review, and I left how I made the mistake to the comment. I'll fix it, and sorry about that.

}

Attribute getLayout(Attribute attr, RankedTensorType type) const {
return EncodingNopLayoutAttr::get(attr.getContext());

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 good question, and I've been thinking it whole today. You are right that it can be any attribute that implements SerializableEncodingAttrInterface. I over-index on the point that I want to separate "tensor encoding attribute" and "encoding resolvers" when possible. And I made a mistake that ties SerializableEncodingAttrInterface to tensor encoding attribute. Because returning a resolver smelled weird to me. Now I realized that also what we've been doing.

Suggested change
return EncodingNopLayoutAttr::get(attr.getContext());
return attr;

Let me put my thoughts here for visibility, and I'll convert them to issues if I figure action items.

IMO, there are two types of encodings. One is the encodings that will be attached to tensor types, and the other is the encoding resolvers. E.g., iree_encoding.encoding and iree_encoding.layout are tensor encoding attributes, iree_gpu.pad_layout_attr is an encoding resolver. I want to keep a clear line between these two categories. It might not be feasible as they evolve, but so far I haven't seen an example that could break my mental model.

My mistake was that I thought the return value of getLayout() falls into the former category. E.g., SpecializedEncodingAttr is not an encoding resolver to me, but it implements the interface for getLayout method. I implemented before we have today's understanding, and I'll fix it.

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.

Let me take it over because I need to revisit the old encoding attributes.

@jtuyls
jtuyls force-pushed the materialize-encoding-into-padding-convert-type-2 branch from 458a3ad to 9a2c5aa Compare May 5, 2025 06:42
@jtuyls
jtuyls merged commit aac0760 into iree-org:main May 5, 2025
@jtuyls
jtuyls deleted the materialize-encoding-into-padding-convert-type-2 branch May 5, 2025 14:21
KyleHerndon pushed a commit to KyleHerndon/iree that referenced this pull request May 7, 2025
…ree-org#20700)

Adds the `convertType` interface method to
`SerializableEncodingAttrInterface`. This is the first stage for
collapsing MaterializeEncodingIntoPadding into MaterializeEncoding, see
iree-org#20160.

Note that this removes the need for MaterializePadEncodingTypeConverter
if the `convertType` method is implemented for `PadEncodingLayoutAttr`,
however, this is intentionally left out of the PR as @MaheshRavishankar
is looking at adjusting the MaterializeEncodingIntoPadding for dynamic
padding. To check what it looks like with
MaterializePadEncodingTypeConverter collapsed into
MaterializeEncodingTypeConverter you can view this branch instead:
https://github.com/iree-org/iree/compare/main...jtuyls:materialize-encoding-into-padding-convert-type?expand=1.

Second note that while the type conversion for `EncodingNopLayoutAttr`
was previously combined with the common implementation for
`CPUEncodingLayoutAttr`, `VMVXEncodingLayoutAttr` and
`GPUEncodingLayoutAttr`, this PR provides a minimal standalone
implementation for clarity on what's going on. `CPUEncodingLayoutAttr`,
`VMVXEncodingLayoutAttr` and `GPUEncodingLayoutAttr` still share the
same implementation as they did previously through the `convertType`
implementation in
HostSerializableEncodingAttrInterfaceExternalModelBase.
`EncodingNopLayoutAttr` now implements both
IREE::Encoding::EncodingLayoutResolverAttrInterface and
IREE::Encoding::SerializableEncodingAttrInterface to go down the same
path as the above attributes, but provides its own implementation.

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

Adds the `convertType` interface method to
`SerializableEncodingAttrInterface`. This is the first stage for
collapsing MaterializeEncodingIntoPadding into MaterializeEncoding, see
#20160.

Note that this removes the need for MaterializePadEncodingTypeConverter
if the `convertType` method is implemented for `PadEncodingLayoutAttr`,
however, this is intentionally left out of the PR as @MaheshRavishankar
is looking at adjusting the MaterializeEncodingIntoPadding for dynamic
padding. To check what it looks like with
MaterializePadEncodingTypeConverter collapsed into
MaterializeEncodingTypeConverter you can view this branch instead:
https://github.com/iree-org/iree/compare/main...jtuyls:materialize-encoding-into-padding-convert-type?expand=1.

Second note that while the type conversion for `EncodingNopLayoutAttr`
was previously combined with the common implementation for
`CPUEncodingLayoutAttr`, `VMVXEncodingLayoutAttr` and
`GPUEncodingLayoutAttr`, this PR provides a minimal standalone
implementation for clarity on what's going on. `CPUEncodingLayoutAttr`,
`VMVXEncodingLayoutAttr` and `GPUEncodingLayoutAttr` still share the
same implementation as they did previously through the `convertType`
implementation in
HostSerializableEncodingAttrInterfaceExternalModelBase.
`EncodingNopLayoutAttr` now implements both
IREE::Encoding::EncodingLayoutResolverAttrInterface and
IREE::Encoding::SerializableEncodingAttrInterface to go down the same
path as the above attributes, but provides its own implementation.

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