Skip to content

[spirv] Port over cooperative matrix support - #5607

Merged
antiagainst merged 6 commits into
iree-org:mainfrom
antiagainst:migrate-coopmat
Apr 26, 2021
Merged

antiagainst merged 6 commits into
iree-org:mainfrom
antiagainst:migrate-coopmat

Conversation

@antiagainst

Copy link
Copy Markdown
Member

This commit moves cooperative matrix support to the Linalg on
tensor path. A few improvements are done along the way to have
a better pipeline structure:

  • Cooperative matrix support previoulsy were scattered across
    various passes; now they are scoped into the same pass for
    better isolation.
  • Now the handling of cooperative matrix is at a more proper
    layer. Cooperative matrix is at a large vector semantics level.
    We try to lower vector transfer read/write/arthmetic ops when
    we are still at that level with the necessary high-level
    information; not trying to left them "fall through" to the
    final converting to SPIR-V stage.
  • Now the lowering is more progressive. This commit leverages
    new constructs like unrealized_conversion_cast recently added
    in core to bridge the gap and makes SPIR-V lowering more modular
    and progressive: cooperative matrix part is first handled, with
    the rest latter converted and those cast ops cancelled out.
    Upstream SPIR-V support is expected to grow more towards this
    direction.

The majority of the code in the new VectorToCooperativeMatrixPass.cpp
file is moved from ConvertToSPIRVPass.cpp, with modifications to
properly use type conversion integrated into dialect conversion
framework.

This commit moves cooperative matrix support to the Linalg on
tensor path. A few improvements are done along the way to have
a better pipeline structure:

* Cooperative matrix support previoulsy were scattered across
  various passes; now they are scoped into the same pass for
  better isolation.
* Now the handling of cooperative matrix is at a more proper
  layer. Cooperative matrix is at a large vector semantics level.
  We try to lower vector transfer read/write/arthmetic ops when
  we are still at that level with the necessary high-level
  information; not trying to left them "fall through" to the
  final converting to SPIR-V stage.
* Now the lowering is more progressive. This commit leverages
  new constructs like unrealized_conversion_cast recently added
  in core to bridge the gap and makes SPIR-V lowering more modular
  and progressive: cooperative matrix part is first handled, with
  the rest latter converted and those cast ops cancelled out.
  Upstream SPIR-V support is expected to grow more towards this
  direction.

The majority of the code in the new VectorToCooperativeMatrixPass.cpp
file is moved from ConvertToSPIRVPass.cpp, with modifications to
properly use type conversion integrated into dialect conversion
framework.
@antiagainst antiagainst linked an issue Apr 26, 2021 that may be closed by this pull request

@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, just few nits! 👯

@hanhanW

hanhanW commented Apr 26, 2021

Copy link
Copy Markdown
Contributor

There are some TODO assigned to @ThomasRaoux so I think it's better to wait for his LGTM.

@MaheshRavishankar MaheshRavishankar 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.

I am fine with moving this around, but we should just move this to core. The issue here is that the co-operative matrix has its own types and load-store instructions. So the vector transfer-reads and transfer-writes become problematic. THe best way to handle this is to update the Vector to Co-operative matrix lowering in core to not require the pre-analysis pass done here. For this we are first missing a "spv.cast" operation that converts from the default spv.array type to the co-operative matrix type.

  1. Lower the vector.contract using spv.cast -> spv.cooperativeMatrixMulOp -> spv.cast.
  2. Then you have a spv.load -> spv.cast that should be converted to spv.loadcooperativematrix (and same for the store).

This then requires no state and this pass can be dropped. Would be better for SPIR-V lowering in general.

This refactoring by itself is good, but deleting this pass completely is even better (we have code cleanup as part of our OKRs this quarter)

@antiagainst

Copy link
Copy Markdown
Member Author

I am fine with moving this around, but we should just move this to core. The issue here is that the co-operative matrix has its own types and load-store instructions. So the vector transfer-reads and transfer-writes become problematic. THe best way to handle this is to update the Vector to Co-operative matrix lowering in core to not require the pre-analysis pass done here. For this we are first missing a "spv.cast" operation that converts from the default spv.array type to the co-operative matrix type.

  1. Lower the vector.contract using spv.cast -> spv.cooperativeMatrixMulOp -> spv.cast.
  2. Then you have a spv.load -> spv.cast that should be converted to spv.loadcooperativematrix (and same for the store).

This then requires no state and this pass can be dropped. Would be better for SPIR-V lowering in general.

This refactoring by itself is good, but deleting this pass completely is even better (we have code cleanup as part of our OKRs this quarter)

Yes right. The existing analysis is hacky. @ThomasRaoux is pushing for a more generic cooperative matrix ish representation in gpu dialect so that we can share between cuda and spirv land. After that's landed we can go from vector to gpu counterparts and then convert to spirv will be simple. Expected this part to be simplified in the future; right now doing this to unblock deprecating the buffer path.

@ThomasRaoux ThomasRaoux 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 Lei!

@antiagainst
antiagainst merged commit 74e3302 into iree-org:main Apr 26, 2021
@antiagainst
antiagainst deleted the migrate-coopmat branch April 26, 2021 21:21
ThomasRaoux pushed a commit to ThomasRaoux/iree that referenced this pull request Apr 27, 2021
This commit moves cooperative matrix support to the Linalg on
tensor path. A few improvements are done along the way to have
a better pipeline structure:

* Cooperative matrix support previously were scattered across
  various passes; now they are scoped into the same pass for
  better isolation.
* Now the handling of cooperative matrix is at a more proper
  layer. Cooperative matrix is at a large vector semantics level.
  We try to lower vector transfer read/write/arithmetic ops when
  we are still at that level with the necessary high-level
  information; not trying to left them "fall through" to the
  final converting to SPIR-V stage.
* Now the lowering is more progressive. This commit leverages
  new constructs like unrealized_conversion_cast recently added
  in core to bridge the gap and makes SPIR-V lowering more modular
  and progressive: cooperative matrix part is first handled, with
  the rest latter converted and those cast ops cancelled out.
  Upstream SPIR-V support is expected to grow more towards this
  direction.

The majority of the code in the new VectorToCooperativeMatrixPass.cpp
file is moved from ConvertToSPIRVPass.cpp, with modifications to
properly use type conversion integrated into dialect conversion
framework.
This was referenced Apr 27, 2021
simon-camp pushed a commit to simon-camp/iree that referenced this pull request Apr 29, 2021
This commit moves cooperative matrix support to the Linalg on
tensor path. A few improvements are done along the way to have
a better pipeline structure:

* Cooperative matrix support previously were scattered across
  various passes; now they are scoped into the same pass for
  better isolation.
* Now the handling of cooperative matrix is at a more proper
  layer. Cooperative matrix is at a large vector semantics level.
  We try to lower vector transfer read/write/arithmetic ops when
  we are still at that level with the necessary high-level
  information; not trying to left them "fall through" to the
  final converting to SPIR-V stage.
* Now the lowering is more progressive. This commit leverages
  new constructs like unrealized_conversion_cast recently added
  in core to bridge the gap and makes SPIR-V lowering more modular
  and progressive: cooperative matrix part is first handled, with
  the rest latter converted and those cast ops cancelled out.
  Upstream SPIR-V support is expected to grow more towards this
  direction.

The majority of the code in the new VectorToCooperativeMatrixPass.cpp
file is moved from ConvertToSPIRVPass.cpp, with modifications to
properly use type conversion integrated into dialect conversion
framework.
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.

Some lit tests should be fixed when migrating to Linalg on tensors

4 participants