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

[SplitLogicalObjectFifos] Add support for dma tranposed on the target side #850

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,6 @@ static LogicalResult checkWhetherSplitIsPossible(
fetchL3ToL2DmaCpyNdOp(l2ToL1DmaOps[0]);
if (failed(maybeL3ToL2DmaOp)) return failure();
AMDAIE::DmaCpyNdOp l3ToL2DmaOp = maybeL3ToL2DmaOp.value();
if ((l3ToL2DmaOp.getTargetMixedOffsets().size() !=
l3ToL2DmaOp.getSourceMixedOffsets().size()) ||
(l3ToL2DmaOp.getTargetMixedSizes().size() !=
l3ToL2DmaOp.getSourceMixedSizes().size()) ||
(l3ToL2DmaOp.getTargetMixedStrides().size() !=
l3ToL2DmaOp.getSourceMixedStrides().size())) {
LLVM_DEBUG(llvm::dbgs() << "dimensionality of source and target's "
"offset/size/stride found different for "
<< l3ToL2DmaOp << "\n");
return failure();
}

SmallVector<OpFoldResult, 4> staticL2AsTargetSizes =
l3ToL2DmaOp.getTargetMixedSizes();
Expand Down Expand Up @@ -353,39 +342,81 @@ LogicalResult splitLogicalObjectFifos(
toBeErased.insert(sourceAllocOp);
toBeErased.insert(sourceObjectFifo);

SmallVector<OpFoldResult, 4> staticL2AsTargetOffsets =
SmallVector<OpFoldResult> staticL2AsTargetOffsets =
l3ToL2DmaOp.getTargetMixedOffsets();
SmallVector<OpFoldResult, 4> staticL2AsTargetSizes =
SmallVector<OpFoldResult> staticL2AsTargetSizes =
l3ToL2DmaOp.getTargetMixedSizes();
SmallVector<OpFoldResult, 4> staticL3AsSourceOffsets =
SmallVector<OpFoldResult> staticL3AsSourceOffsets =
l3ToL2DmaOp.getSourceMixedOffsets();
SmallVector<OpFoldResult, 4> staticL3AsSourceSizes =
SmallVector<OpFoldResult> staticL3AsSourceSizes =
l3ToL2DmaOp.getSourceMixedSizes();

LogicalObjectFifoFromMemrefOp l2TargetObjectFifo =
l3ToL2DmaOp.getTargetObjectFifo();
ArrayRef<int64_t> l2TargetShape =
l2TargetObjectFifo.getMemrefType().getShape();
if (l2TargetShape.size() != staticL2AsTargetSizes.size()) {
LLVM_DEBUG(llvm::dbgs() << "L2 target size should be the same");
return failure();
}

// Check if the L3->L2 dma is transposed on the target side.
bool dmaTransposeOnSource = true;
for (auto [s1, s2] : llvm::zip_equal(l2TargetShape, staticL2AsTargetSizes)) {
if (s1 != getConstantIntValue(s2)) {
dmaTransposeOnSource = false;
break;
}
}
if (staticL3AsSourceSizes.size() != staticL2AsTargetSizes.size()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also added this check to deal with the case I mentioned [2, 32, 32, 32] transposed to [2, 32, 32, 32].

Copy link
Contributor

Choose a reason for hiding this comment

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

Umm, can you explain how this check is helping the case of [2, 32, 32, 32] transposed to [2, 32, 32, 32] ?

I see the static sizes' ranks being compared for L3 source and L2 target, and unable to understand how the above case is checked here. Perhaps some assumption is being made here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the dma is tranposed on the target side then the source side data is continuous. The dma ops are converted from pack ops, and for matmul cases we know the dimensions of source and target memrefs are not the same. For example, we have [128, 128] on the source side, and [1, 2, 32, 32] on the target side, if we transpose on the target side, then the dma addressing has 2 dimensions on source side and 4 dimensions on target side. While on the side, if we transpose on source side, then both dma addressing have 4 dimensions.

dmaTransposeOnSource = false;
}

OpFoldResult zeroVal = getAsIndexOpFoldResult(context, 0);
OpFoldResult oneVal = getAsIndexOpFoldResult(context, 1);
// Update split dimensions' offset/size for L2 as target and L3 as source. We
// can afford to do this here because it's going to be the same for all L3->L2
// splits. Here we are setting offset = 0 and size = 1.
for (size_t dim : splitDimsForL2) {
staticL2AsTargetOffsets[dim] = zeroVal;
staticL2AsTargetSizes[dim] = oneVal;
staticL3AsSourceOffsets[dim] = zeroVal;
staticL3AsSourceSizes[dim] = oneVal;

if (dmaTransposeOnSource) {
// Update split dimensions' offset/size for L2 as target and L3 as source.
// We can afford to do this here because it's going to be the same for all
// L3->L2 splits. Here we are setting offset = 0 and size = 1.
for (size_t dim : splitDimsForL2) {
staticL2AsTargetOffsets[dim] = zeroVal;
staticL2AsTargetSizes[dim] = oneVal;
staticL3AsSourceOffsets[dim] = zeroVal;
staticL3AsSourceSizes[dim] = oneVal;
}
} else {
// The L2 target side has transposed dimensions, while the L3 source side
// data are continuous and don't have `nonSplitDim`. Then the L3 source
// sizes need to be modified to match the new L2 target sizes.
// Hardcoded the transposed dimensions for now.
const SmallVector<size_t> transposeDim = {0, 2, 1, 3};
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we have added a check earlier : if (l2TargetShape.size() != staticL2AsTargetSizes.size()) , I'd rather we infer even the dimensions where transpose has taken place inside the loop for (auto [s1, s2] : llvm::zip_equal(l2TargetShape, staticL2AsTargetSizes)) itself and get rid of this hardcoding.

Copy link
Contributor Author

@yzhang93 yzhang93 Oct 23, 2024

Choose a reason for hiding this comment

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

Well, now I think there could be issues if we blindly compare values. There could be dimensions that have same values, e.g, [2, 32, 32, 32] -> [2, 32, 32, 32] after transposing dim 1 and 2. Then there's no way to tell which dma dimensions are transposed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh okay. That makes sense. Thanks for the explanation.

for (auto &&[splitDim, nonSplitdim] :
llvm::zip_equal(splitDimsForL2, nonSplitDimsForL2)) {
staticL2AsTargetOffsets[transposeDim[splitDim]] = zeroVal;
staticL2AsTargetSizes[transposeDim[splitDim]] = oneVal;
staticL3AsSourceSizes[splitDim] =
staticL2AsTargetSizes[transposeDim[nonSplitdim]];
}
}

// Traverse each L2->L1 DmaCpyNd op and split them.
for (AMDAIE::DmaCpyNdOp l2ToL1DmaOp : l2ToL1DmaOps) {
SmallVector<OpFoldResult, 6> staticL2AsSourceOffsets =
SmallVector<OpFoldResult> staticL2AsSourceOffsets =
l2ToL1DmaOp.getSourceMixedOffsets();
SmallVector<OpFoldResult, 6> staticL2AsSourceSizes =
SmallVector<OpFoldResult> staticL2AsSourceSizes =
l2ToL1DmaOp.getSourceMixedSizes();

// Now we'll create a new L2 buffer based on the new shape inferred earlier
// via `staticL2AsTargetSizes`.
LogicalObjectFifoFromMemrefOp oldL2ObjectFifo =
l2ToL1DmaOp.getSourceObjectFifo();
AMDAIE::LogicalObjectFifoFromMemrefOp source = createNewLogicalObjectFifo(
rewriter, oldL2ObjectFifo, staticL2AsTargetSizes);
// If the dma transpose is on the source(target) side, then the L2
// target(source) side has the sizes in order.
SmallVector<OpFoldResult> newL2Sizes =
dmaTransposeOnSource ? staticL2AsTargetSizes : staticL2AsSourceSizes;
AMDAIE::LogicalObjectFifoFromMemrefOp source =
createNewLogicalObjectFifo(rewriter, oldL2ObjectFifo, newL2Sizes);

// --------------------------------------------
// ---------- L3 -> L2 splitting --------------
Expand All @@ -404,15 +435,19 @@ LogicalResult splitLogicalObjectFifos(
<< splitDim;
}
std::optional<int64_t> constantSize =
getConstantIntValue(staticL2AsTargetSizes[nonSplitdim]);
getConstantIntValue(newL2Sizes[nonSplitdim]);
if (!constantSize) {
return l3ToL2DmaOp->emitOpError()
<< "found a non-constant value for target size at dim "
<< nonSplitdim;
}
int64_t offsetToAdd = constantOffset.value() * constantSize.value();

// If the dma transpose is on the target side, L3 source side data are
// continuous and don't have `nonSplitDim`.
size_t dim = dmaTransposeOnSource ? nonSplitdim : splitDim;
FailureOr<OpFoldResult> newOffset = updateL3SourceOffset(
rewriter, staticL3AsSourceOffsets[nonSplitdim], offsetToAdd, context);
rewriter, staticL3AsSourceOffsets[dim], offsetToAdd, context);
if (failed(newOffset)) {
// TODO: Ideally we should be able to handle even +, -, *, /, etc.
// But handle this later (if at all!) as such cases might not
Expand All @@ -421,8 +456,9 @@ LogicalResult splitLogicalObjectFifos(
<< "Unhandled expression for source offset at dim "
<< nonSplitdim;
}
staticL3AsSourceOffsets[nonSplitdim] = *newOffset;
staticL3AsSourceOffsets[dim] = *newOffset;
}

// Create new L3 -> L2 Dma Op.
rewriter.setInsertionPoint(l3ToL2DmaOp);
rewriter.create<AMDAIE::DmaCpyNdOp>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,14 @@ void addAMDAIEObjectFifoLoweringPasses(OpPassManager &passManager,
// cause 'aie.dma_bd' error, so for now keep using transpose on source for
// both pack and unpack ops.
// TODO(vivian): explore the other options for conv ops.
AMDAIEConvertToDmaOptions dmaOptions;
dmaOptions.packTransposeOnSource =
(useTilePipeline == TilePassPipeline::ConvDecomposePipeline) ? true
: false;
dmaOptions.unpackTransposeOnSource = true;
passManager.addPass(createAMDAIEConvertToDmaPass(dmaOptions));
{
AMDAIEConvertToDmaOptions dmaOptions;
dmaOptions.packTransposeOnSource =
(useTilePipeline == TilePassPipeline::ConvDecomposePipeline) ? true
: false;
dmaOptions.unpackTransposeOnSource = true;
passManager.addPass(createAMDAIEConvertToDmaPass(dmaOptions));
}

passManager.addPass(createAMDAIENormalizeLoopBoundsPass());
passManager.addPass(createAMDAIEInsertCoresPass());
Expand All @@ -571,6 +573,7 @@ void addAMDAIEObjectFifoLoweringPasses(OpPassManager &passManager,
passManager.addPass(createAMDAIEDistributeCoresAndObjectFifosPass());
passManager.addPass(createCSEPass());
passManager.addPass(createCanonicalizerPass());

passManager.addPass(createAMDAIESplitLogicalObjFifosForConnectionReusePass());
passManager.addPass(createCSEPass());
passManager.addPass(createCanonicalizerPass());
Expand Down
Loading
Loading