Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLVMGPUVectorDistribute] Refactor vector.contract distribute #19631

Merged
merged 2 commits into from
Jan 22, 2025

Conversation

manupak
Copy link
Contributor

@manupak manupak commented Jan 8, 2025

Currently, vector.contract distribution is implemented as a standalone distribution closely following vector.multi_reduce. Therefore, we have to duplicate code/effort when we improve either one.

This commit changes vector.contract just to distribute the "contract" part of it. Then it creates a new vector.multi_reduce to be re-distributed with partial reduction semantics. Thus, allowing the improvements of vector.multi_reduce to be re-used by vector.contract

closes : #19620

@manupak manupak requested a review from antiagainst as a code owner January 8, 2025 11:00
@manupak manupak requested a review from bangtianliu January 8, 2025 11:01
@manupak manupak force-pushed the refactor-contract-distribution branch from 4379322 to 41bb886 Compare January 15, 2025 10:32
@manupak manupak requested a review from Groverkss January 15, 2025 10:37
Comment on lines 891 to 898
Value isoRankLocalReduced = rewriter.create<vector::ShapeCastOp>(
loc, partialReducedDistributedType, localContractValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

can we instead do a broadcast + transpose? broadcast + transpose is prefered for unit dimensions because it is easier to unroll / reason about

Comment on lines 918 to 947
SmallVector<int64_t> subgroupTileLens =
llvm::to_vector(resLayout.getSubgroupTile());
SmallVector<int64_t> batchTileLens =
llvm::to_vector(resLayout.getBatchTile());
SmallVector<int64_t> outerTileLens =
llvm::to_vector(resLayout.getOuterTile());
SmallVector<int64_t> threadTileLens =
llvm::to_vector(resLayout.getThreadTile());
SmallVector<int64_t> elementTileLens =
llvm::to_vector(resLayout.getElementTile());
SmallVector<int64_t> subgroupStrides =
llvm::to_vector(resLayout.getSubgroupStrides());
SmallVector<int64_t> threadStrides =
llvm::to_vector(resLayout.getThreadStrides());
for (int64_t rDim : reducedDims) {
// thread-local reductions have already been carried out.
// What is remaining is reductions across threads and
// subgroups.
subgroupTileLens.push_back(lhsLayout.getSubgroupTile()[rDim]);
batchTileLens.push_back(1);
outerTileLens.push_back(1);
threadTileLens.push_back(lhsLayout.getThreadTile()[rDim]);
elementTileLens.push_back(1);
subgroupStrides.push_back(lhsLayout.getSubgroupStrides()[rDim]);
threadStrides.push_back(lhsLayout.getThreadStrides()[rDim]);
}
reductionLayout = IREE::VectorExt::NestedLayoutAttr::get(
contractOp.getContext(), subgroupTileLens, batchTileLens,
outerTileLens, threadTileLens, elementTileLens, subgroupStrides,
threadStrides);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a "broadcast" function to the layout which takes a dimension and sizes for a single dimension? This would make it easier to write these transformations.

@manupak
Copy link
Contributor Author

manupak commented Jan 21, 2025

@Groverkss spent few cycles here today...
So Im not exactly sure what you meant by "broadcast" as there is no broadcasting happening here.

I ve simplified the implementation a bit by adding a builder for NestedLayoutAttr that can take an existing layout and append few tiling dims. This API is sufficient to manually infer the partial reduction layout. Then rest of rewrite can be derived from it altogether -- when you have some time PTAL.

@manupak manupak force-pushed the refactor-contract-distribution branch from de22597 to abaad34 Compare January 21, 2025 18:24
@manupak manupak force-pushed the refactor-contract-distribution branch 2 times, most recently from 0211656 to 4f75304 Compare January 22, 2025 10:12
Currently, vector.contract distribution is implemented as a standalone
distribution closely following vector.multi_reduce. Therefore, we have
to duplicate code/effort when we improve either one.

This commit changes vector.contract just to distribute the "contract"
part of it. Then it creates a new vector.multi_reduce to be
re-distributed with partial reduction semantics. Thus, allowing
the improvements of vector.multi_reduce to be re-used by
vector.contract

Signed-off-by: Manupa Karunaratne <manupa.karunaratne@amd.com>
* use this to infer partial reduction layout of
  vector.contract

Signed-off-by: Manupa Karunaratne <manupa.karunaratne@amd.com>
@manupak manupak merged commit 03c5a0f into iree-org:main Jan 22, 2025
40 checks passed
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.

[LLVMGPUVectorDistribute] Add support for inter-subgroup distribution of vector.contract reduction.
2 participants