-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add V0 tensor layout generation #518
Changes from all commits
9d3c8a4
51684cf
b8b619c
55cbc77
1f1372b
d20c3df
9b91a42
b76714f
2ea7a16
679fd17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,48 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "ttmlir/Dialect/TTIR/Analysis/LegalGridAnalysis.h" | ||
#include "ttmlir/Dialect/TTIR/IR/TTIROps.h" | ||
|
||
namespace mlir::tt::ttir { | ||
|
||
bool mock_is_output_tensor_legal_for_op(Operation *op, LayoutAttr layout) { | ||
// Placeholder, needs to be replaced with a call the the TTNN op interface. | ||
return true; | ||
} | ||
|
||
bool tensor_shape_compatible_with_shard(Operation *op, LayoutAttr layout) { | ||
// These constraints are implemented seperatelly in every TTNN op. | ||
// Almost nothing seems to be shared between EVERY op, so is hard to have any | ||
// logic here without the risk of discarding a valid configuraiton or modeling | ||
// the constraint for each op. This logic may be offloaded to the TTNN op | ||
// interface. | ||
|
||
// For now we will check if the tilised tensor dims are divisible by the grid | ||
// dims. This will definitly discard possible valid configurations, but is a | ||
// start. | ||
RankedTensorType tensorType = | ||
mlir::cast<RankedTensorType>(op->getResult(0).getType()); | ||
llvm::ArrayRef<int64_t> tensorShape = tensorType.getShape(); | ||
|
||
int64_t MTiles = 1; | ||
if (tensorType.getRank() >= 2) { | ||
MTiles = (tensorShape.rbegin()[1] + 31) / 32; | ||
} | ||
|
||
int64_t KTIles = (tensorShape.back() + 31) / 32; | ||
|
||
int64_t gridR = layout.getGrid().getShape()[0]; | ||
int64_t gridC = layout.getGrid().getShape()[1]; | ||
|
||
return (MTiles % gridR == 0) && (KTIles % gridC == 0); | ||
} | ||
|
||
bool LegalGridAnalysis::applyOverrides() { | ||
// Lookup grid size overrides based on location information for current | ||
// operation. | ||
// | ||
|
||
// TODO(odjuricic): Need to override all params, not just grid size. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this for followup change? File an issue under Optmizier if so. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there is an issue filed. |
||
RankedTensorType tensorType = | ||
mlir::cast<RankedTensorType>(op->getResult(0).getType()); | ||
LayoutAttr layout = mlir::cast<LayoutAttr>(tensorType.getEncoding()); | ||
|
@@ -36,17 +70,114 @@ bool LegalGridAnalysis::applyOverrides() { | |
} | ||
|
||
void LegalGridAnalysis::analysisImplementation() { | ||
// Placeholder, needs to be implemented. Go through all the grid sizes and | ||
// check if they are legal based on tensor type and device/chip attributes. | ||
// For now result of analysis is maximum supported grid size. | ||
// | ||
// A first incomplete implementation of the LegalGridAnalysis. | ||
// This implementation is a placeholder and is meant to just enable testing of | ||
// other components. | ||
|
||
// Process only TTIR ops. | ||
if (not llvm::isa<TTIROp>(op)) { | ||
return; | ||
} | ||
// Skip operations that don't have output tensors. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be used instead: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, except for ToLayoutOp which has an output. |
||
if (op->getNumResults() == 0) { | ||
return; | ||
} | ||
if (llvm::isa<ToLayoutOp>(op)) { | ||
return; | ||
} | ||
|
||
// Get output tensor type. | ||
RankedTensorType tensorType = | ||
mlir::cast<RankedTensorType>(op->getResult(0).getType()); | ||
LayoutAttr layout = mlir::cast<LayoutAttr>(tensorType.getEncoding()); | ||
llvm::ArrayRef<int64_t> tensorShape = tensorType.getShape(); | ||
|
||
analysisResult.push_back(layout.withGrid( | ||
op->getContext(), tensorShape, | ||
GridAttr::get(op->getContext(), analysisInput.maxGrid.getShape()))); | ||
// DRAM | ||
// No grid is set since the tensor is not sharded. | ||
// TODO(odjuricic): We need to set grid here since it will be used as the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File an issue, mention special case where grid matches number of DRAM banks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to the 1x1 comment above, this needs to be updated once we know how runtime and TTNN treat this param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filed an issue. |
||
// compute gird. (not implemented in runtime atm) | ||
LayoutAttr dram = | ||
layout.withMemorySpace(op->getContext(), MemorySpace::DeviceDRAM) | ||
.withMemoryLayout(op->getContext(), TensorMemoryLayout::Interleaved) | ||
.withGrid(op->getContext(), tensorType, | ||
GridAttr::get(op->getContext(), | ||
analysisInput.maxGrid.getShape())); | ||
if (mock_is_output_tensor_legal_for_op(op, dram)) { | ||
analysisResult.push_back(dram); | ||
} | ||
|
||
// L1 Interleaved (same as above). | ||
LayoutAttr l1Interleaved = | ||
layout.withMemorySpace(op->getContext(), MemorySpace::DeviceL1) | ||
.withMemoryLayout(op->getContext(), TensorMemoryLayout::Interleaved) | ||
.withGrid(op->getContext(), tensorType, | ||
GridAttr::get(op->getContext(), | ||
analysisInput.maxGrid.getShape())); | ||
if (mock_is_output_tensor_legal_for_op(op, l1Interleaved)) { | ||
analysisResult.push_back(l1Interleaved); | ||
} | ||
|
||
// L1 Sharded | ||
LayoutAttr shardedBase = | ||
layout.withMemorySpace(op->getContext(), MemorySpace::DeviceL1); | ||
std::vector<LayoutAttr> shardedResults; | ||
|
||
// Block Sharded | ||
for (auto width = 1; width <= analysisInput.maxGrid.getShape()[0]; ++width) { | ||
for (auto height = 1; height <= analysisInput.maxGrid.getShape()[1]; | ||
++height) { | ||
shardedResults.push_back( | ||
shardedBase | ||
.withGrid(op->getContext(), tensorType, | ||
GridAttr::get(op->getContext(), {width, height})) | ||
.withMemoryLayout(op->getContext(), | ||
TensorMemoryLayout::BlockSharded)); | ||
} | ||
} | ||
|
||
auto numCores = | ||
analysisInput.maxGrid.getShape()[0] * analysisInput.maxGrid.getShape()[1]; | ||
// Height Sharded | ||
// TODO(odjuricic): Missing affine mapping to actual grid. Need to check with | ||
// runtime implementation on what to produce here. | ||
for (auto height = 2; height <= numCores; ++height) { | ||
shardedResults.push_back( | ||
shardedBase | ||
.withGrid(op->getContext(), tensorType, | ||
GridAttr::get(op->getContext(), {height, 1})) | ||
.withMemoryLayout(op->getContext(), | ||
TensorMemoryLayout::HeightSharded)); | ||
} | ||
|
||
// Width Sharded | ||
for (auto width = 2; width <= numCores; ++width) { | ||
shardedResults.push_back( | ||
shardedBase | ||
.withGrid(op->getContext(), tensorType, | ||
GridAttr::get(op->getContext(), {1, width})) | ||
.withMemoryLayout(op->getContext(), | ||
TensorMemoryLayout::WidthSharded)); | ||
} | ||
|
||
// Filter layouts based on output tensor legality for current op. | ||
shardedResults.erase( | ||
std::remove_if(shardedResults.begin(), shardedResults.end(), | ||
[this](LayoutAttr layout) { | ||
return !tensor_shape_compatible_with_shard(op, layout) || | ||
!mock_is_output_tensor_legal_for_op(op, layout); | ||
}), | ||
shardedResults.end()); | ||
|
||
// Pick top largest sharded grids. | ||
std::sort(shardedResults.begin(), shardedResults.end(), | ||
[](LayoutAttr a, LayoutAttr b) { | ||
return a.getGrid().getShape()[0] * a.getGrid().getShape()[1] > | ||
b.getGrid().getShape()[0] * b.getGrid().getShape()[1]; | ||
}); | ||
|
||
analysisResult.insert( | ||
analysisResult.end(), shardedResults.begin(), | ||
shardedResults.begin() + | ||
std::min(analysisInput.maxShardedGrids, | ||
static_cast<int64_t>(shardedResults.size()))); | ||
} | ||
} // namespace mlir::tt::ttir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constexpr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was intending for this to be a passable param. Will set to constexpr for now and change when needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it should be passed in as param, make it as part of override issue in followup change.