Compare commits
6 Commits
15e8edb9c4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
905fa9f9a7 | ||
|
|
62b0a6e19d | ||
|
|
b605585b1f | ||
|
|
08b0fcd850 | ||
|
|
9dccc2c701 | ||
|
|
5c839e62c1 |
10
.gitignore
vendored
10
.gitignore
vendored
@@ -1,5 +1,15 @@
|
|||||||
|
.zed
|
||||||
.idea
|
.idea
|
||||||
**/.vscode
|
**/.vscode
|
||||||
|
|
||||||
.claude
|
.claude
|
||||||
|
.codex
|
||||||
AGENTS.md
|
AGENTS.md
|
||||||
|
|
||||||
|
CMakeUserPresets.json
|
||||||
|
|
||||||
build
|
build
|
||||||
|
cmake-build-debug
|
||||||
|
cmake-build-release
|
||||||
|
|
||||||
|
**/__pycache__
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ validate.py \
|
|||||||
--raptor-path ../cmake-build-release/Release/bin/onnx-mlir \
|
--raptor-path ../cmake-build-release/Release/bin/onnx-mlir \
|
||||||
--onnx-include-dir ../onnx-mlir/include \
|
--onnx-include-dir ../onnx-mlir/include \
|
||||||
--operations-dir ./networks/yolo11n/depth_04 \
|
--operations-dir ./networks/yolo11n/depth_04 \
|
||||||
--crossbar-size 2048
|
--crossbar-size 2048 --crossbar-count 256
|
||||||
```
|
```
|
||||||
|
|
||||||
Available networks under `validation/networks/`: `vgg16`, `yolo11n`.
|
Available networks under `validation/networks/`: `vgg16`, `yolo11n`.
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "mlir/Dialect/Arith/IR/Arith.h"
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
||||||
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
||||||
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
||||||
#include "mlir/Dialect/SCF/IR/SCF.h"
|
#include "mlir/Dialect/SCF/IR/SCF.h"
|
||||||
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
||||||
@@ -48,7 +49,9 @@ void dumpModule(ModuleOp moduleOp, const std::string& name) {
|
|||||||
|
|
||||||
std::fstream file(dialectsDir + "/" + name + ".mlir", std::ios::out);
|
std::fstream file(dialectsDir + "/" + name + ".mlir", std::ios::out);
|
||||||
llvm::raw_os_ostream os(file);
|
llvm::raw_os_ostream os(file);
|
||||||
os << *moduleOp;
|
OpPrintingFlags flags;
|
||||||
|
flags.elideLargeElementsAttrs();
|
||||||
|
moduleOp.print(os, flags);
|
||||||
os.flush();
|
os.flush();
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
@@ -173,6 +176,13 @@ void walkPimMvmVmmWeightUses(Operation* root, function_ref<void(OpOperand&)> cal
|
|||||||
root->walk([&](pim::PimCoreOp coreOp) {
|
root->walk([&](pim::PimCoreOp coreOp) {
|
||||||
walkMvmVmmWeightUses<pim::PimMVMOp, pim::PimVMMOp>(coreOp, callback);
|
walkMvmVmmWeightUses<pim::PimMVMOp, pim::PimVMMOp>(coreOp, callback);
|
||||||
});
|
});
|
||||||
|
root->walk([&](pim::PimCoreBatchOp coreBatchOp) {
|
||||||
|
auto weights = coreBatchOp.getWeights();
|
||||||
|
for (auto weight : weights)
|
||||||
|
for (OpOperand& use : weight.getUses())
|
||||||
|
if (use.getOwner() == coreBatchOp.getOperation())
|
||||||
|
callback(use);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
memref::GlobalOp lookupGlobalForGetGlobal(ModuleOp moduleOp, memref::GetGlobalOp getGlobalOp) {
|
memref::GlobalOp lookupGlobalForGetGlobal(ModuleOp moduleOp, memref::GetGlobalOp getGlobalOp) {
|
||||||
@@ -181,66 +191,6 @@ memref::GlobalOp lookupGlobalForGetGlobal(ModuleOp moduleOp, memref::GetGlobalOp
|
|||||||
return moduleOp.lookupSymbol<memref::GlobalOp>(getGlobalOp.getName());
|
return moduleOp.lookupSymbol<memref::GlobalOp>(getGlobalOp.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
FailureOr<Operation*> getOtherEndOfChannel(Operation* op, bool opIsReceive, RewriterBase& rewriter) {
|
|
||||||
|
|
||||||
auto channelNewOp = op->getOperand(0).getDefiningOp<spatial::SpatChannelNewOp>();
|
|
||||||
if (!channelNewOp) {
|
|
||||||
op->emitError("User of Channel must have the first operand created by ChannelNewOp.");
|
|
||||||
return failure();
|
|
||||||
}
|
|
||||||
// channelNewOp should have two users: `op` and a
|
|
||||||
// `ChannelSendOp`/`ChannelReceiveOp`
|
|
||||||
auto channelUsers = channelNewOp->getUsers();
|
|
||||||
auto usersIterator = channelUsers.begin();
|
|
||||||
auto firstUser = *usersIterator;
|
|
||||||
usersIterator++;
|
|
||||||
if (usersIterator == channelUsers.end()) {
|
|
||||||
op->emitError("Operand generated by ChannelNewOp must have two users, "
|
|
||||||
"only one found.");
|
|
||||||
channelNewOp->dump();
|
|
||||||
op->dump();
|
|
||||||
channelNewOp->getParentOp()->dump();
|
|
||||||
return failure();
|
|
||||||
}
|
|
||||||
auto secondUser = *usersIterator;
|
|
||||||
usersIterator++;
|
|
||||||
if (usersIterator != channelUsers.end()) {
|
|
||||||
op->emitError("Operand generated by ChannelNewOp must have two users, "
|
|
||||||
"more than two found.");
|
|
||||||
return failure();
|
|
||||||
}
|
|
||||||
Operation* notOpUser;
|
|
||||||
if (firstUser == op) {
|
|
||||||
notOpUser = secondUser;
|
|
||||||
}
|
|
||||||
else if (secondUser == op) {
|
|
||||||
notOpUser = firstUser;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
op->emitError("Operand generated by ChannelNewOp must have two users, "
|
|
||||||
"and one of them must be me, but"
|
|
||||||
"none of them is actually me.");
|
|
||||||
return failure();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opIsReceive) {
|
|
||||||
if (!isa<spatial::SpatChannelSendOp>(notOpUser)) {
|
|
||||||
op->emitError("Operand generated by ChannelNewOp has two user, one is "
|
|
||||||
"me, the other is not a ChannelSendOp.");
|
|
||||||
return failure();
|
|
||||||
}
|
|
||||||
return notOpUser;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!isa<spatial::SpatChannelReceiveOp>(notOpUser)) {
|
|
||||||
op->emitError("Operand generated by ChannelNewOp has two user, one is "
|
|
||||||
"me, the other is not a ChannelReceiveOp.");
|
|
||||||
return failure();
|
|
||||||
}
|
|
||||||
return notOpUser;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SmallVector<int64_t> computeRowMajorStrides(ArrayRef<int64_t> shape) {
|
SmallVector<int64_t> computeRowMajorStrides(ArrayRef<int64_t> shape) {
|
||||||
SmallVector<int64_t> strides(shape.size(), 1);
|
SmallVector<int64_t> strides(shape.size(), 1);
|
||||||
for (int64_t dim = static_cast<int64_t>(shape.size()) - 2; dim >= 0; --dim)
|
for (int64_t dim = static_cast<int64_t>(shape.size()) - 2; dim >= 0; --dim)
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ inline constexpr llvm::StringRef PimWeightAlwaysAttrName = "weightAlways";
|
|||||||
|
|
||||||
namespace onnx_mlir {
|
namespace onnx_mlir {
|
||||||
|
|
||||||
|
inline constexpr llvm::StringLiteral kCoreIdAttrName = "core_id";
|
||||||
|
|
||||||
struct ResolvedContiguousAddress {
|
struct ResolvedContiguousAddress {
|
||||||
mlir::Value base;
|
mlir::Value base;
|
||||||
int64_t byteOffset = 0;
|
int64_t byteOffset = 0;
|
||||||
@@ -48,9 +50,6 @@ void walkPimMvmVmmWeightUses(mlir::Operation* root, llvm::function_ref<void(mlir
|
|||||||
|
|
||||||
mlir::memref::GlobalOp lookupGlobalForGetGlobal(mlir::ModuleOp moduleOp, mlir::memref::GetGlobalOp getGlobalOp);
|
mlir::memref::GlobalOp lookupGlobalForGetGlobal(mlir::ModuleOp moduleOp, mlir::memref::GetGlobalOp getGlobalOp);
|
||||||
|
|
||||||
llvm::FailureOr<mlir::Operation*>
|
|
||||||
getOtherEndOfChannel(mlir::Operation* op, bool opIsReceive, mlir::RewriterBase& rewriter);
|
|
||||||
|
|
||||||
llvm::SmallVector<int64_t> computeRowMajorStrides(llvm::ArrayRef<int64_t> shape);
|
llvm::SmallVector<int64_t> computeRowMajorStrides(llvm::ArrayRef<int64_t> shape);
|
||||||
|
|
||||||
llvm::SmallVector<int64_t>
|
llvm::SmallVector<int64_t>
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
||||||
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
|
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
|
||||||
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
||||||
#include "mlir/IR/Attributes.h"
|
#include "mlir/IR/Attributes.h"
|
||||||
#include "mlir/IR/BuiltinAttributes.h"
|
#include "mlir/IR/BuiltinAttributes.h"
|
||||||
#include "mlir/IR/BuiltinTypes.h"
|
#include "mlir/IR/BuiltinTypes.h"
|
||||||
|
#include "mlir/IR/IRMapping.h"
|
||||||
|
#include "mlir/IR/Value.h"
|
||||||
|
|
||||||
#include "llvm/ADT/DenseMap.h"
|
#include "llvm/ADT/DenseMap.h"
|
||||||
#include "llvm/ADT/SmallPtrSet.h"
|
#include "llvm/ADT/SmallPtrSet.h"
|
||||||
|
#include "llvm/ADT/StringExtras.h"
|
||||||
#include "llvm/Support/FileSystem.h"
|
#include "llvm/Support/FileSystem.h"
|
||||||
#include "llvm/Support/JSON.h"
|
#include "llvm/Support/JSON.h"
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
@@ -53,9 +57,23 @@ void PimMemory::allocateMemoryForValue(mlir::Value value, MemEntry& memEntry) {
|
|||||||
void PimMemory::allocateHost(ModuleOp moduleOp, func::FuncOp funcOp) {
|
void PimMemory::allocateHost(ModuleOp moduleOp, func::FuncOp funcOp) {
|
||||||
SmallDenseMap<memref::GlobalOp, mlir::Value, 8> globalConstants;
|
SmallDenseMap<memref::GlobalOp, mlir::Value, 8> globalConstants;
|
||||||
SmallVector<std::pair<mlir::Value, mlir::Value>, 16> globalAliases;
|
SmallVector<std::pair<mlir::Value, mlir::Value>, 16> globalAliases;
|
||||||
|
SmallVector<mlir::Value> args;
|
||||||
|
|
||||||
|
|
||||||
|
for (mlir::Value arg : funcOp.getArguments()){
|
||||||
|
gatherMemEntry(arg);
|
||||||
|
args.push_back(arg);
|
||||||
|
}
|
||||||
|
|
||||||
funcOp.walk([&](memref::GetGlobalOp getGlobalOp) {
|
funcOp.walk([&](memref::GetGlobalOp getGlobalOp) {
|
||||||
if (!hasWeightAlways(getGlobalOp)) {
|
if (!hasWeightAlways(getGlobalOp)) {
|
||||||
auto globalMemrefOp = lookupGlobalForGetGlobal(moduleOp, getGlobalOp);
|
auto globalMemrefOp = lookupGlobalForGetGlobal(moduleOp, getGlobalOp);
|
||||||
|
if (globalMemrefOp.getName().starts_with("arg")){
|
||||||
|
StringRef indexStr = globalMemrefOp.getName().substr(4);
|
||||||
|
int index = 0;
|
||||||
|
llvm::to_integer(indexStr,index, 10);
|
||||||
|
globalAliases.push_back({getGlobalOp.getResult(), args[index]});
|
||||||
|
}
|
||||||
auto [iter, inserted] = globalConstants.try_emplace(globalMemrefOp, getGlobalOp.getResult());
|
auto [iter, inserted] = globalConstants.try_emplace(globalMemrefOp, getGlobalOp.getResult());
|
||||||
if (inserted)
|
if (inserted)
|
||||||
gatherMemEntry(getGlobalOp.getResult());
|
gatherMemEntry(getGlobalOp.getResult());
|
||||||
@@ -64,8 +82,6 @@ void PimMemory::allocateHost(ModuleOp moduleOp, func::FuncOp funcOp) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
for (mlir::Value arg : funcOp.getArguments())
|
|
||||||
gatherMemEntry(arg);
|
|
||||||
|
|
||||||
funcOp.walk([&](memref::AllocOp allocOp) {
|
funcOp.walk([&](memref::AllocOp allocOp) {
|
||||||
if (!allocOp->getParentOfType<pim::PimCoreOp>())
|
if (!allocOp->getParentOfType<pim::PimCoreOp>())
|
||||||
@@ -131,6 +147,12 @@ json::Object PimCodeGen::createEmptyOffset() {
|
|||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t PimCodeGen::remapCoreId(size_t coreId) const {
|
||||||
|
auto it = emittedCoreIds.find(coreId);
|
||||||
|
assert(it != emittedCoreIds.end() && "Missing emitted core id remapping");
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
static json::Object createRs1OnlyOffset() {
|
static json::Object createRs1OnlyOffset() {
|
||||||
json::Object offset;
|
json::Object offset;
|
||||||
offset["offset_select"] = 1;
|
offset["offset_select"] = 1;
|
||||||
@@ -190,7 +212,7 @@ void PimCodeGen::emitCommunicationOp(StringRef opName, size_t bufferAddr, size_t
|
|||||||
json::Object json;
|
json::Object json;
|
||||||
json["op"] = opName;
|
json["op"] = opName;
|
||||||
json["rd"] = 0;
|
json["rd"] = 0;
|
||||||
json["core"] = coreId;
|
json["core"] = remapCoreId(coreId);
|
||||||
json["size"] = size;
|
json["size"] = size;
|
||||||
json["offset"] = createEmptyOffset();
|
json["offset"] = createEmptyOffset();
|
||||||
emitInstruction(std::move(json));
|
emitInstruction(std::move(json));
|
||||||
@@ -412,6 +434,9 @@ void PimCodeGen::codeGenVSoftmaxOp(pim::PimVSoftmaxOp vsoftmaxOp, const StaticVa
|
|||||||
emitInstruction(std::move(json));
|
emitInstruction(std::move(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PimCodeGen::codeGetGlobalOp(memref::GetGlobalOp getGlobalOp, const StaticValueKnowledge& knowledge) const {
|
||||||
|
}
|
||||||
|
|
||||||
void PimCodeGen::codeGenTransposeOp(pim::PimTransposeOp transposeOp, const StaticValueKnowledge& knowledge) const {
|
void PimCodeGen::codeGenTransposeOp(pim::PimTransposeOp transposeOp, const StaticValueKnowledge& knowledge) const {
|
||||||
auto srcAddr = addressOf(transposeOp.getInput(), knowledge);
|
auto srcAddr = addressOf(transposeOp.getInput(), knowledge);
|
||||||
auto dstAddr = addressOf(transposeOp.getOutputBuffer(), knowledge);
|
auto dstAddr = addressOf(transposeOp.getOutputBuffer(), knowledge);
|
||||||
@@ -474,19 +499,136 @@ std::string getMemorySizeAsString(size_t size) {
|
|||||||
return std::to_string(size) + " Bytes";
|
return std::to_string(size) + " Bytes";
|
||||||
}
|
}
|
||||||
|
|
||||||
static SmallVector<unsigned, 8> getUsedWeightIndices(pim::PimCoreOp coreOp) {
|
static SmallVector<unsigned, 8> getUsedWeightIndices(Block& block) {
|
||||||
SmallVector<unsigned, 8> indices;
|
SmallVector<unsigned, 8> indices;
|
||||||
auto addIndex = [&](unsigned weightIndex) {
|
auto addIndex = [&](unsigned weightIndex) {
|
||||||
if (!llvm::is_contained(indices, weightIndex))
|
if (!llvm::is_contained(indices, weightIndex))
|
||||||
indices.push_back(weightIndex);
|
indices.push_back(weightIndex);
|
||||||
};
|
};
|
||||||
|
|
||||||
coreOp.walk([&](pim::PimMVMOp mvmOp) { addIndex(mvmOp.getWeightIndex()); });
|
block.walk([&](pim::PimMVMOp mvmOp) { addIndex(mvmOp.getWeightIndex()); });
|
||||||
coreOp.walk([&](pim::PimVMMOp vmmOp) { addIndex(vmmOp.getWeightIndex()); });
|
block.walk([&](pim::PimVMMOp vmmOp) { addIndex(vmmOp.getWeightIndex()); });
|
||||||
llvm::sort(indices);
|
llvm::sort(indices);
|
||||||
return indices;
|
return indices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static SmallVector<unsigned, 8> getUsedWeightIndices(pim::PimCoreOp coreOp) {
|
||||||
|
return getUsedWeightIndices(coreOp.getBody().front());
|
||||||
|
}
|
||||||
|
|
||||||
|
static SmallVector<int32_t> getBatchCoreIds(pim::PimCoreBatchOp coreBatchOp) {
|
||||||
|
auto coreIdsAttr = coreBatchOp->getAttrOfType<DenseI32ArrayAttr>(onnx_mlir::kCoreIdAttrName);
|
||||||
|
assert(coreIdsAttr && "pim.core_batch requires core_id array attribute");
|
||||||
|
return SmallVector<int32_t>(coreIdsAttr.asArrayRef().begin(), coreIdsAttr.asArrayRef().end());
|
||||||
|
}
|
||||||
|
|
||||||
|
static SmallVector<Operation*> collectTopLevelCoreLikeOps(func::FuncOp funcOp) {
|
||||||
|
SmallVector<Operation*> coreLikeOps;
|
||||||
|
for (Operation& op : funcOp.getBody().front()) {
|
||||||
|
if (dyn_cast<pim::PimCoreOp>(&op) || dyn_cast<pim::PimCoreBatchOp>(&op))
|
||||||
|
coreLikeOps.push_back(&op);
|
||||||
|
}
|
||||||
|
return coreLikeOps;
|
||||||
|
}
|
||||||
|
|
||||||
|
static pim::PimCoreOp materializeScalarCoreFromBatchLane(pim::PimCoreBatchOp coreBatchOp, unsigned lane) {
|
||||||
|
OpBuilder builder(coreBatchOp);
|
||||||
|
builder.setInsertionPointAfter(coreBatchOp);
|
||||||
|
|
||||||
|
size_t laneCount = static_cast<size_t>(coreBatchOp.getLaneCount());
|
||||||
|
size_t weightsPerLane = coreBatchOp.getWeights().size() / laneCount;
|
||||||
|
SmallVector<mlir::Value> laneWeights;
|
||||||
|
laneWeights.reserve(weightsPerLane);
|
||||||
|
for (size_t weightIndex = 0; weightIndex < weightsPerLane; ++weightIndex)
|
||||||
|
laneWeights.push_back(coreBatchOp.getWeights()[lane * weightsPerLane + weightIndex]);
|
||||||
|
|
||||||
|
auto coreIds = getBatchCoreIds(coreBatchOp);
|
||||||
|
auto scalarCore = pim::PimCoreOp::create(builder,
|
||||||
|
coreBatchOp.getLoc(),
|
||||||
|
ValueRange(laneWeights),
|
||||||
|
builder.getI32IntegerAttr(coreIds[lane]));
|
||||||
|
Block* block = builder.createBlock(&scalarCore.getBody(), scalarCore.getBody().end());
|
||||||
|
IRMapping mapper;
|
||||||
|
if (coreBatchOp.getBody().front().getNumArguments() == 1)
|
||||||
|
mapper.map(coreBatchOp.getBody().front().getArgument(0), coreBatchOp.getInputs()[lane]);
|
||||||
|
|
||||||
|
builder.setInsertionPointToEnd(block);
|
||||||
|
for (Operation& op : coreBatchOp.getBody().front()) {
|
||||||
|
if (isa<pim::PimHaltOp>(op)) {
|
||||||
|
pim::PimHaltOp::create(builder, op.getLoc());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto sendBatchOp = dyn_cast<pim::PimSendBatchOp>(op)) {
|
||||||
|
pim::PimSendOp::create(builder,
|
||||||
|
sendBatchOp.getLoc(),
|
||||||
|
mapper.lookup(sendBatchOp.getInput()),
|
||||||
|
sendBatchOp.getSizeAttr(),
|
||||||
|
builder.getI32IntegerAttr(sendBatchOp.getTargetCoreIds()[lane]));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto receiveBatchOp = dyn_cast<pim::PimReceiveBatchOp>(op)) {
|
||||||
|
auto scalarReceive = pim::PimReceiveOp::create(builder,
|
||||||
|
receiveBatchOp.getLoc(),
|
||||||
|
receiveBatchOp.getOutput().getType(),
|
||||||
|
mapper.lookup(receiveBatchOp.getOutputBuffer()),
|
||||||
|
receiveBatchOp.getSizeAttr(),
|
||||||
|
builder.getI32IntegerAttr(receiveBatchOp.getSourceCoreIds()[lane]));
|
||||||
|
mapper.map(receiveBatchOp.getOutput(), scalarReceive.getOutput());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto memcpBatchOp = dyn_cast<pim::PimMemCopyHostToDevBatchOp>(op)) {
|
||||||
|
mlir::Value hostSource = mapper.lookupOrNull(memcpBatchOp.getHostSource());
|
||||||
|
if (!hostSource)
|
||||||
|
hostSource = memcpBatchOp.getHostSource();
|
||||||
|
|
||||||
|
auto scalarCopy = pim::PimMemCopyHostToDevOp::create(builder,
|
||||||
|
memcpBatchOp.getLoc(),
|
||||||
|
memcpBatchOp.getOutput().getType(),
|
||||||
|
mapper.lookup(memcpBatchOp.getDeviceTarget()),
|
||||||
|
hostSource,
|
||||||
|
memcpBatchOp.getDeviceTargetOffsetAttr(),
|
||||||
|
memcpBatchOp.getHostSourceOffsetAttr(),
|
||||||
|
memcpBatchOp.getSizeAttr());
|
||||||
|
mapper.map(memcpBatchOp.getOutput(), scalarCopy.getOutput());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Operation* cloned = builder.clone(op, mapper);
|
||||||
|
for (auto [originalResult, clonedResult] : llvm::zip(op.getResults(), cloned->getResults()))
|
||||||
|
mapper.map(originalResult, clonedResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block->empty() || !isa<pim::PimHaltOp>(block->back()))
|
||||||
|
pim::PimHaltOp::create(builder, coreBatchOp.getLoc());
|
||||||
|
return scalarCore;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void aliasMaterializedHostGlobals(
|
||||||
|
ModuleOp moduleOp, func::FuncOp funcOp, pim::PimCoreOp coreOp, PimAcceleratorMemory& memory) {
|
||||||
|
coreOp.walk([&](memref::GetGlobalOp getGlobalOp) {
|
||||||
|
if (hasWeightAlways(getGlobalOp) || memory.memEntriesMap.contains(getGlobalOp.getResult()))
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto targetGlobal = lookupGlobalForGetGlobal(moduleOp, getGlobalOp);
|
||||||
|
if (!targetGlobal)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mlir::Value aliasedValue;
|
||||||
|
funcOp.walk([&](memref::GetGlobalOp candidate) {
|
||||||
|
if (aliasedValue || candidate == getGlobalOp || !memory.memEntriesMap.contains(candidate.getResult()))
|
||||||
|
return;
|
||||||
|
if (lookupGlobalForGetGlobal(moduleOp, candidate) == targetGlobal)
|
||||||
|
aliasedValue = candidate.getResult();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (aliasedValue)
|
||||||
|
memory.memEntriesMap[getGlobalOp.getResult()] = memory.memEntriesMap[aliasedValue];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Write global constant data into a binary memory image at their allocated addresses.
|
/// Write global constant data into a binary memory image at their allocated addresses.
|
||||||
static OnnxMlirCompilerErrorCodes
|
static OnnxMlirCompilerErrorCodes
|
||||||
writeMemoryBinary(ModuleOp moduleOp, func::FuncOp funcOp, PimAcceleratorMemory& memory, StringRef outputDirPath) {
|
writeMemoryBinary(ModuleOp moduleOp, func::FuncOp funcOp, PimAcceleratorMemory& memory, StringRef outputDirPath) {
|
||||||
@@ -581,6 +723,8 @@ static int64_t codeGenCoreOps(Block& block, PimCodeGen& coreCodeGen) {
|
|||||||
coreCodeGen.codeGenVSigmOp(vsigmOp, knowledge);
|
coreCodeGen.codeGenVSigmOp(vsigmOp, knowledge);
|
||||||
else if (auto vsoftmaxOp = dyn_cast<pim::PimVSoftmaxOp>(op))
|
else if (auto vsoftmaxOp = dyn_cast<pim::PimVSoftmaxOp>(op))
|
||||||
coreCodeGen.codeGenVSoftmaxOp(vsoftmaxOp, knowledge);
|
coreCodeGen.codeGenVSoftmaxOp(vsoftmaxOp, knowledge);
|
||||||
|
else if (auto getGlobalOp = dyn_cast<memref::GetGlobalOp>(op))
|
||||||
|
coreCodeGen.codeGetGlobalOp(getGlobalOp, knowledge);
|
||||||
else {
|
else {
|
||||||
op.emitError("Unsupported codegen for this operation");
|
op.emitError("Unsupported codegen for this operation");
|
||||||
op.dump();
|
op.dump();
|
||||||
@@ -670,7 +814,7 @@ static OnnxMlirCompilerErrorCodes writeCrossbarWeights(ModuleOp moduleOp,
|
|||||||
return CompilerSuccess;
|
return CompilerSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
llvm::DenseMap<pim::PimCoreOp, llvm::DenseMap<mlir::Value, std::string>>
|
llvm::DenseMap<size_t, llvm::DenseMap<mlir::Value, std::string>>
|
||||||
createAndPopulateWeightFolder(func::FuncOp funcOp, StringRef outputDirPath) {
|
createAndPopulateWeightFolder(func::FuncOp funcOp, StringRef outputDirPath) {
|
||||||
ModuleOp moduleOp = funcOp->getParentOfType<ModuleOp>();
|
ModuleOp moduleOp = funcOp->getParentOfType<ModuleOp>();
|
||||||
auto coreWeightsDirPath = outputDirPath + "/weights";
|
auto coreWeightsDirPath = outputDirPath + "/weights";
|
||||||
@@ -679,10 +823,24 @@ createAndPopulateWeightFolder(func::FuncOp funcOp, StringRef outputDirPath) {
|
|||||||
size_t indexFileName = 0;
|
size_t indexFileName = 0;
|
||||||
|
|
||||||
int64_t xbarSize = crossbarSize.getValue();
|
int64_t xbarSize = crossbarSize.getValue();
|
||||||
llvm::DenseMap<pim::PimCoreOp, llvm::DenseMap<mlir::Value, std::string>> mapCoreWeightToFileName;
|
llvm::DenseMap<size_t, llvm::DenseMap<mlir::Value, std::string>> mapCoreWeightToFileName;
|
||||||
llvm::DenseMap<memref::GlobalOp, std::string> mapGlobalOpToFileName;
|
llvm::DenseMap<memref::GlobalOp, std::string> mapGlobalOpToFileName;
|
||||||
|
|
||||||
for (pim::PimCoreOp coreOp : funcOp.getOps<pim::PimCoreOp>()) {
|
SmallVector<Operation*> coreLikeOps = collectTopLevelCoreLikeOps(funcOp);
|
||||||
|
|
||||||
|
for (Operation* op : coreLikeOps) {
|
||||||
|
SmallVector<pim::PimCoreOp> scalarCores;
|
||||||
|
if (auto coreOp = dyn_cast<pim::PimCoreOp>(op)) {
|
||||||
|
scalarCores.push_back(coreOp);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
auto coreBatchOp = cast<pim::PimCoreBatchOp>(op);
|
||||||
|
for (unsigned lane = 0; lane < static_cast<unsigned>(coreBatchOp.getLaneCount()); ++lane)
|
||||||
|
scalarCores.push_back(materializeScalarCoreFromBatchLane(coreBatchOp, lane));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (pim::PimCoreOp coreOp : scalarCores) {
|
||||||
|
size_t coreId = static_cast<size_t>(coreOp.getCoreId());
|
||||||
for (unsigned index : getUsedWeightIndices(coreOp)) {
|
for (unsigned index : getUsedWeightIndices(coreOp)) {
|
||||||
if (index >= coreOp.getWeights().size()) {
|
if (index >= coreOp.getWeights().size()) {
|
||||||
coreOp.emitWarning("Weight index " + std::to_string(index) + " is out of range");
|
coreOp.emitWarning("Weight index " + std::to_string(index) + " is out of range");
|
||||||
@@ -717,7 +875,7 @@ createAndPopulateWeightFolder(func::FuncOp funcOp, StringRef outputDirPath) {
|
|||||||
if (mapGlobalOpToFileName.contains(globalOp)) {
|
if (mapGlobalOpToFileName.contains(globalOp)) {
|
||||||
auto& fileName = mapGlobalOpToFileName[globalOp];
|
auto& fileName = mapGlobalOpToFileName[globalOp];
|
||||||
std::pair<mlir::Value, std::string> weightToFile = {weight, fileName};
|
std::pair<mlir::Value, std::string> weightToFile = {weight, fileName};
|
||||||
mapCoreWeightToFileName[coreOp].insert(weightToFile);
|
mapCoreWeightToFileName[coreId].insert(weightToFile);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -756,22 +914,28 @@ createAndPopulateWeightFolder(func::FuncOp funcOp, StringRef outputDirPath) {
|
|||||||
|
|
||||||
weightFileStream.close();
|
weightFileStream.close();
|
||||||
mapGlobalOpToFileName.insert({globalOp, newFileName});
|
mapGlobalOpToFileName.insert({globalOp, newFileName});
|
||||||
mapCoreWeightToFileName[coreOp].insert({weight, newFileName});
|
mapCoreWeightToFileName[coreId].insert({weight, newFileName});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (pim::PimCoreOp coreOp : scalarCores)
|
||||||
|
if (coreOp.getOperation() != op)
|
||||||
|
coreOp.erase();
|
||||||
|
}
|
||||||
return mapCoreWeightToFileName;
|
return mapCoreWeightToFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write the top-level PIM configuration JSON (core count, crossbar config, I/O addresses).
|
/// Write the top-level PIM configuration JSON (core count, crossbar config, I/O addresses).
|
||||||
static OnnxMlirCompilerErrorCodes writeConfigJson(func::FuncOp funcOp,
|
static OnnxMlirCompilerErrorCodes writeConfigJson(func::FuncOp funcOp,
|
||||||
PimAcceleratorMemory& memory,
|
PimAcceleratorMemory& memory,
|
||||||
size_t coreCount,
|
size_t maxCoreId,
|
||||||
json::Object xbarsPerArrayGroup,
|
json::Object xbarsPerArrayGroup,
|
||||||
StringRef outputDirPath) {
|
StringRef outputDirPath) {
|
||||||
json::Object configJson;
|
json::Object configJson;
|
||||||
|
|
||||||
// +1 because pimsim-nn also considers the host as a core
|
// pimsim-nn indexes cores directly by their numeric core ID, with the host
|
||||||
configJson["core_cnt"] = coreCount + 1;
|
// occupying core 0.
|
||||||
|
configJson["core_cnt"] = maxCoreId + 1;
|
||||||
|
|
||||||
// TODO: Should this be based on the floating point type used in the model?
|
// TODO: Should this be based on the floating point type used in the model?
|
||||||
// The 2 following values determine the bitwidth of the vectors' elements: bitwidth = adc_count * cell_precision
|
// The 2 following values determine the bitwidth of the vectors' elements: bitwidth = adc_count * cell_precision
|
||||||
@@ -845,14 +1009,47 @@ OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimJson(ModuleOp& moduleOp, std::
|
|||||||
// For each core, specify the number of crossbar per array group.
|
// For each core, specify the number of crossbar per array group.
|
||||||
// This implementation always assigns one crossbar per group.
|
// This implementation always assigns one crossbar per group.
|
||||||
json::Object xbarsPerArrayGroup;
|
json::Object xbarsPerArrayGroup;
|
||||||
size_t coreCount = 0;
|
size_t maxCoreId = 0;
|
||||||
|
|
||||||
// Create Weight Folder
|
// Create Weight Folder
|
||||||
auto mapCoreWeightToFileName = createAndPopulateWeightFolder(funcOp, outputDirPath);
|
auto mapCoreWeightToFileName = createAndPopulateWeightFolder(funcOp, outputDirPath);
|
||||||
|
|
||||||
for (auto coreOp : funcOp.getOps<pim::PimCoreOp>()) {
|
SmallVector<Operation*> coreLikeOps = collectTopLevelCoreLikeOps(funcOp);
|
||||||
auto coreId = coreOp.getCoreId();
|
llvm::DenseMap<size_t, size_t> emittedCoreIds;
|
||||||
coreCount++;
|
size_t nextEmittedCoreId = 1;
|
||||||
|
|
||||||
|
for (Operation* op : coreLikeOps) {
|
||||||
|
if (auto coreOp = dyn_cast<pim::PimCoreOp>(op)) {
|
||||||
|
size_t originalCoreId = static_cast<size_t>(coreOp.getCoreId());
|
||||||
|
if (!emittedCoreIds.contains(originalCoreId))
|
||||||
|
emittedCoreIds[originalCoreId] = nextEmittedCoreId++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto coreBatchOp = cast<pim::PimCoreBatchOp>(op);
|
||||||
|
auto batchCoreIds = getBatchCoreIds(coreBatchOp);
|
||||||
|
for (unsigned lane = 0; lane < static_cast<unsigned>(coreBatchOp.getLaneCount()); ++lane) {
|
||||||
|
size_t originalCoreId = static_cast<size_t>(batchCoreIds[lane]);
|
||||||
|
if (!emittedCoreIds.contains(originalCoreId))
|
||||||
|
emittedCoreIds[originalCoreId] = nextEmittedCoreId++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Operation* op : coreLikeOps) {
|
||||||
|
SmallVector<pim::PimCoreOp> scalarCores;
|
||||||
|
if (auto coreOp = dyn_cast<pim::PimCoreOp>(op)) {
|
||||||
|
scalarCores.push_back(coreOp);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
auto coreBatchOp = cast<pim::PimCoreBatchOp>(op);
|
||||||
|
for (unsigned lane = 0; lane < static_cast<unsigned>(coreBatchOp.getLaneCount()); ++lane)
|
||||||
|
scalarCores.push_back(materializeScalarCoreFromBatchLane(coreBatchOp, lane));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (pim::PimCoreOp coreOp : scalarCores) {
|
||||||
|
size_t originalCoreId = static_cast<size_t>(coreOp.getCoreId());
|
||||||
|
size_t coreId = emittedCoreIds.lookup(originalCoreId);
|
||||||
|
maxCoreId = std::max(maxCoreId, coreId);
|
||||||
|
|
||||||
std::error_code errorCode;
|
std::error_code errorCode;
|
||||||
auto outputCorePath = outputDirPath + "/core_" + std::to_string(coreId) + ".json";
|
auto outputCorePath = outputDirPath + "/core_" + std::to_string(coreId) + ".json";
|
||||||
@@ -863,7 +1060,8 @@ OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimJson(ModuleOp& moduleOp, std::
|
|||||||
}
|
}
|
||||||
coreFileStream << '[';
|
coreFileStream << '[';
|
||||||
|
|
||||||
PimCodeGen coreCodeGen(memory, coreFileStream);
|
PimCodeGen coreCodeGen(memory, coreFileStream, emittedCoreIds);
|
||||||
|
aliasMaterializedHostGlobals(moduleOp, funcOp, coreOp, memory);
|
||||||
memory.getOrCreateDeviceMem(coreId).allocateCore(coreOp);
|
memory.getOrCreateDeviceMem(coreId).allocateCore(coreOp);
|
||||||
|
|
||||||
int64_t processedOperations = codeGenCoreOps(coreOp.getBody().front(), coreCodeGen);
|
int64_t processedOperations = codeGenCoreOps(coreOp.getBody().front(), coreCodeGen);
|
||||||
@@ -871,19 +1069,17 @@ OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimJson(ModuleOp& moduleOp, std::
|
|||||||
return CompilerFailure;
|
return CompilerFailure;
|
||||||
assert(processedOperations > 0);
|
assert(processedOperations > 0);
|
||||||
|
|
||||||
// Remove trailing comma, close JSON array
|
|
||||||
coreFileStream.seek(coreFileStream.tell() - 1);
|
coreFileStream.seek(coreFileStream.tell() - 1);
|
||||||
coreFileStream << ']';
|
coreFileStream << ']';
|
||||||
coreFileStream.close();
|
coreFileStream.close();
|
||||||
|
|
||||||
// Write crossbar weights for this core
|
|
||||||
auto coreWeightsDirPath = outputDirPath + "/core_" + std::to_string(coreId);
|
auto coreWeightsDirPath = outputDirPath + "/core_" + std::to_string(coreId);
|
||||||
if (auto error = sys::fs::create_directory(coreWeightsDirPath)) {
|
if (auto error = sys::fs::create_directory(coreWeightsDirPath)) {
|
||||||
errs() << "Error creating core directory: " << coreWeightsDirPath << ": " << error.message() << '\n';
|
errs() << "Error creating core directory: " << coreWeightsDirPath << ": " << error.message() << '\n';
|
||||||
return InvalidOutputFileAccess;
|
return InvalidOutputFileAccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& mapWeightToFile = mapCoreWeightToFileName[coreOp];
|
auto& mapWeightToFile = mapCoreWeightToFileName[originalCoreId];
|
||||||
json::Array xbarsPerGroup;
|
json::Array xbarsPerGroup;
|
||||||
for (unsigned index : getUsedWeightIndices(coreOp)) {
|
for (unsigned index : getUsedWeightIndices(coreOp)) {
|
||||||
if (index >= coreOp.getWeights().size()) {
|
if (index >= coreOp.getWeights().size()) {
|
||||||
@@ -897,8 +1093,8 @@ OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimJson(ModuleOp& moduleOp, std::
|
|||||||
if (auto error = sys::fs::create_link(outputDirPath + "/weights/" + fileName,
|
if (auto error = sys::fs::create_link(outputDirPath + "/weights/" + fileName,
|
||||||
coreWeightsDirPath + "/crossbar_" + std::to_string(index) + ".bin")) {
|
coreWeightsDirPath + "/crossbar_" + std::to_string(index) + ".bin")) {
|
||||||
errs() << "Error creating link file: " << (outputDirPath + "/weights/" + fileName) << " to "
|
errs() << "Error creating link file: " << (outputDirPath + "/weights/" + fileName) << " to "
|
||||||
<< (coreWeightsDirPath + "/crossbar_" + std::to_string(index) + ".bin") << "\nError:" << error.message()
|
<< (coreWeightsDirPath + "/crossbar_" + std::to_string(index) + ".bin") << "\nError:"
|
||||||
<< '\n';
|
<< error.message() << '\n';
|
||||||
return InvalidOutputFileAccess;
|
return InvalidOutputFileAccess;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -906,5 +1102,10 @@ OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimJson(ModuleOp& moduleOp, std::
|
|||||||
xbarsPerArrayGroup["core" + std::to_string(coreId)] = std::move(xbarsPerGroup);
|
xbarsPerArrayGroup["core" + std::to_string(coreId)] = std::move(xbarsPerGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
return writeConfigJson(funcOp, memory, coreCount, std::move(xbarsPerArrayGroup), outputDirPath);
|
for (pim::PimCoreOp coreOp : scalarCores)
|
||||||
|
if (coreOp.getOperation() != op)
|
||||||
|
coreOp.erase();
|
||||||
|
}
|
||||||
|
|
||||||
|
return writeConfigJson(funcOp, memory, maxCoreId, std::move(xbarsPerArrayGroup), outputDirPath);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "llvm/ADT/DenseMap.h"
|
||||||
#include "llvm-project/clang/include/clang/Basic/LLVM.h"
|
#include "llvm-project/clang/include/clang/Basic/LLVM.h"
|
||||||
#include "llvm/Support/JSON.h"
|
#include "llvm/Support/JSON.h"
|
||||||
|
|
||||||
@@ -58,10 +59,12 @@ public:
|
|||||||
class PimCodeGen {
|
class PimCodeGen {
|
||||||
PimAcceleratorMemory& memory;
|
PimAcceleratorMemory& memory;
|
||||||
llvm::raw_fd_ostream& coreFileStream;
|
llvm::raw_fd_ostream& coreFileStream;
|
||||||
|
const llvm::DenseMap<size_t, size_t>& emittedCoreIds;
|
||||||
|
|
||||||
size_t addressOf(mlir::Value value, const StaticValueKnowledge& knowledge) const {
|
size_t addressOf(mlir::Value value, const StaticValueKnowledge& knowledge) const {
|
||||||
return memory.getValueAddress(value, knowledge);
|
return memory.getValueAddress(value, knowledge);
|
||||||
}
|
}
|
||||||
|
size_t remapCoreId(size_t coreId) const;
|
||||||
|
|
||||||
static llvm::json::Object createEmptyOffset();
|
static llvm::json::Object createEmptyOffset();
|
||||||
void emitInstruction(llvm::json::Object instruction) const;
|
void emitInstruction(llvm::json::Object instruction) const;
|
||||||
@@ -83,8 +86,10 @@ class PimCodeGen {
|
|||||||
void emitMvmOp(size_t groupId, size_t rdAddr, size_t rdOffset, size_t rs1Addr, size_t rs1Offset) const;
|
void emitMvmOp(size_t groupId, size_t rdAddr, size_t rdOffset, size_t rs1Addr, size_t rs1Offset) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PimCodeGen(PimAcceleratorMemory& memory, llvm::raw_fd_ostream& coreJson)
|
PimCodeGen(PimAcceleratorMemory& memory,
|
||||||
: memory(memory), coreFileStream(coreJson) {}
|
llvm::raw_fd_ostream& coreJson,
|
||||||
|
const llvm::DenseMap<size_t, size_t>& emittedCoreIds)
|
||||||
|
: memory(memory), coreFileStream(coreJson), emittedCoreIds(emittedCoreIds) {}
|
||||||
|
|
||||||
void codeGenLoadOp(pim::PimMemCopyHostToDevOp loadOp, const StaticValueKnowledge& knowledge) const;
|
void codeGenLoadOp(pim::PimMemCopyHostToDevOp loadOp, const StaticValueKnowledge& knowledge) const;
|
||||||
void codeGenStoreOp(pim::PimMemCopyDevToHostOp storeOp, const StaticValueKnowledge& knowledge) const;
|
void codeGenStoreOp(pim::PimMemCopyDevToHostOp storeOp, const StaticValueKnowledge& knowledge) const;
|
||||||
@@ -106,6 +111,7 @@ public:
|
|||||||
void codeGenVTanhOp(pim::PimVTanhOp vtanhOp, const StaticValueKnowledge& knowledge) const;
|
void codeGenVTanhOp(pim::PimVTanhOp vtanhOp, const StaticValueKnowledge& knowledge) const;
|
||||||
void codeGenVSigmOp(pim::PimVSigmOp vsigmOp, const StaticValueKnowledge& knowledge) const;
|
void codeGenVSigmOp(pim::PimVSigmOp vsigmOp, const StaticValueKnowledge& knowledge) const;
|
||||||
void codeGenVSoftmaxOp(pim::PimVSoftmaxOp vsoftmaxOp, const StaticValueKnowledge& knowledge) const;
|
void codeGenVSoftmaxOp(pim::PimVSoftmaxOp vsoftmaxOp, const StaticValueKnowledge& knowledge) const;
|
||||||
|
void codeGetGlobalOp(mlir::memref::GetGlobalOp getGlobalOp, const StaticValueKnowledge& knowledge) const;
|
||||||
void codeGenTransposeOp(pim::PimTransposeOp transposeOp, const StaticValueKnowledge& knowledge) const;
|
void codeGenTransposeOp(pim::PimTransposeOp transposeOp, const StaticValueKnowledge& knowledge) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "llvm/ADT/SmallPtrSet.h"
|
#include "llvm/ADT/SmallPtrSet.h"
|
||||||
|
#include "llvm/ADT/STLExtras.h"
|
||||||
|
|
||||||
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
|
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
|
||||||
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
||||||
@@ -174,6 +175,31 @@ using InvokeWithValueRangeResultT = std::invoke_result_t<Fn, mlir::ValueRange>;
|
|||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
template <typename RewriterT>
|
||||||
|
inline mlir::Value createSpatConcat(RewriterT& rewriter, mlir::Location loc, int64_t axis, mlir::ValueRange inputs) {
|
||||||
|
assert(!inputs.empty() && "spat.concat requires at least one input");
|
||||||
|
if (inputs.size() == 1)
|
||||||
|
return inputs.front();
|
||||||
|
|
||||||
|
auto firstType = mlir::cast<mlir::RankedTensorType>(inputs.front().getType());
|
||||||
|
auto outputShape = llvm::to_vector(firstType.getShape());
|
||||||
|
int64_t concatDimSize = 0;
|
||||||
|
bool concatDimDynamic = false;
|
||||||
|
|
||||||
|
for (mlir::Value input : inputs) {
|
||||||
|
auto inputType = mlir::cast<mlir::RankedTensorType>(input.getType());
|
||||||
|
assert(inputType.getRank() == firstType.getRank() && "spat.concat expects same-rank inputs");
|
||||||
|
if (mlir::ShapedType::isDynamic(inputType.getDimSize(axis)))
|
||||||
|
concatDimDynamic = true;
|
||||||
|
else
|
||||||
|
concatDimSize += inputType.getDimSize(axis);
|
||||||
|
}
|
||||||
|
|
||||||
|
outputShape[axis] = concatDimDynamic ? mlir::ShapedType::kDynamic : concatDimSize;
|
||||||
|
auto outputType = mlir::RankedTensorType::get(outputShape, firstType.getElementType(), firstType.getEncoding());
|
||||||
|
return spatial::SpatConcatOp::create(rewriter, loc, outputType, rewriter.getI64IntegerAttr(axis), inputs).getOutput();
|
||||||
|
}
|
||||||
|
|
||||||
template <size_t NumInputs, typename RewriterT, typename BodyFn>
|
template <size_t NumInputs, typename RewriterT, typename BodyFn>
|
||||||
auto createSpatCompute(RewriterT& rewriter,
|
auto createSpatCompute(RewriterT& rewriter,
|
||||||
mlir::Location loc,
|
mlir::Location loc,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
||||||
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
||||||
#include "mlir/Dialect/SCF/IR/SCF.h"
|
#include "mlir/Dialect/SCF/IR/SCF.h"
|
||||||
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
||||||
@@ -11,6 +12,7 @@
|
|||||||
#include "llvm/ADT/SmallVector.h"
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include "llvm/Support/Casting.h"
|
#include "llvm/Support/Casting.h"
|
||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
|
#include "llvm/Support/ErrorHandling.h"
|
||||||
#include "llvm/Support/raw_os_ostream.h"
|
#include "llvm/Support/raw_os_ostream.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -54,6 +56,43 @@ private:
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
static void foldSingleLaneComputeBatches(func::FuncOp funcOp) {
|
||||||
|
IRRewriter rewriter(funcOp.getContext());
|
||||||
|
SmallVector<spatial::SpatComputeBatch> batchOps;
|
||||||
|
funcOp.walk([&](spatial::SpatComputeBatch batchOp) { batchOps.push_back(batchOp); });
|
||||||
|
|
||||||
|
for (auto batchOp : batchOps) {
|
||||||
|
if (batchOp.getLaneCount() != 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto loc = batchOp.getLoc();
|
||||||
|
rewriter.setInsertionPoint(batchOp);
|
||||||
|
auto computeOp = spatial::SpatCompute::create(rewriter, loc, batchOp.getResultTypes(), batchOp.getWeights(), batchOp.getInputs());
|
||||||
|
computeOp.getProperties().setOperandSegmentSizes(
|
||||||
|
{static_cast<int>(batchOp.getWeights().size()), static_cast<int>(batchOp.getInputs().size())});
|
||||||
|
|
||||||
|
Block& templateBlock = batchOp.getBody().front();
|
||||||
|
SmallVector<Type> blockArgTypes;
|
||||||
|
SmallVector<Location> blockArgLocs;
|
||||||
|
for (BlockArgument arg : templateBlock.getArguments()) {
|
||||||
|
blockArgTypes.push_back(arg.getType());
|
||||||
|
blockArgLocs.push_back(loc);
|
||||||
|
}
|
||||||
|
auto* newBlock = rewriter.createBlock(
|
||||||
|
&computeOp.getBody(), computeOp.getBody().end(), TypeRange(blockArgTypes), blockArgLocs);
|
||||||
|
|
||||||
|
IRMapping mapper;
|
||||||
|
for (auto [oldArg, newArg] : llvm::zip(templateBlock.getArguments(), newBlock->getArguments()))
|
||||||
|
mapper.map(oldArg, newArg);
|
||||||
|
rewriter.setInsertionPointToEnd(newBlock);
|
||||||
|
for (Operation& op : templateBlock)
|
||||||
|
rewriter.clone(op, mapper);
|
||||||
|
|
||||||
|
batchOp.replaceAllUsesWith(computeOp.getResults());
|
||||||
|
rewriter.eraseOp(batchOp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ONNXToSpatialPass::runOnOperation() {
|
void ONNXToSpatialPass::runOnOperation() {
|
||||||
ModuleOp moduleOp = getOperation();
|
ModuleOp moduleOp = getOperation();
|
||||||
MLIRContext* ctx = &getContext();
|
MLIRContext* ctx = &getContext();
|
||||||
@@ -124,6 +163,8 @@ void ONNXToSpatialPass::runOnOperation() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foldSingleLaneComputeBatches(*entryFunc);
|
||||||
|
|
||||||
// Count the number of compute ops and check they do not exceed the core count
|
// Count the number of compute ops and check they do not exceed the core count
|
||||||
if (coresCount != -1) {
|
if (coresCount != -1) {
|
||||||
int computeOpsCount = 0;
|
int computeOpsCount = 0;
|
||||||
@@ -144,6 +185,7 @@ void ONNXToSpatialPass::runOnOperation() {
|
|||||||
llvm::dbgs() << "Failed to run canonicalization cleanup, continuing...\n";
|
llvm::dbgs() << "Failed to run canonicalization cleanup, continuing...\n";
|
||||||
|
|
||||||
annotateWeightsConstants(*entryFunc);
|
annotateWeightsConstants(*entryFunc);
|
||||||
|
|
||||||
encapsulateGlobalInstruction(*entryFunc);
|
encapsulateGlobalInstruction(*entryFunc);
|
||||||
|
|
||||||
if (failed(promoteConstantInputsToWeights(*entryFunc))) {
|
if (failed(promoteConstantInputsToWeights(*entryFunc))) {
|
||||||
@@ -160,7 +202,6 @@ bool encapsulator(IRRewriter& rewriter, Location loc, Operation* inst, std::func
|
|||||||
if (T toRemoveOp = llvm::dyn_cast_if_present<T>(inst)) {
|
if (T toRemoveOp = llvm::dyn_cast_if_present<T>(inst)) {
|
||||||
Value source = funcSource(toRemoveOp);
|
Value source = funcSource(toRemoveOp);
|
||||||
rewriter.setInsertionPointAfter(toRemoveOp);
|
rewriter.setInsertionPointAfter(toRemoveOp);
|
||||||
if (isa_and_present<spatial::SpatCompute>(source.getDefiningOp())) {
|
|
||||||
auto newCompute = spatial::SpatCompute::create(rewriter, loc, inst->getResultTypes(), source);
|
auto newCompute = spatial::SpatCompute::create(rewriter, loc, inst->getResultTypes(), source);
|
||||||
auto BB = rewriter.createBlock(&newCompute.getBody(), newCompute.getBody().end(), {source.getType()}, {loc});
|
auto BB = rewriter.createBlock(&newCompute.getBody(), newCompute.getBody().end(), {source.getType()}, {loc});
|
||||||
newCompute.getProperties().setOperandSegmentSizes({(int) 0, (int) 1});
|
newCompute.getProperties().setOperandSegmentSizes({(int) 0, (int) 1});
|
||||||
@@ -173,6 +214,24 @@ bool encapsulator(IRRewriter& rewriter, Location loc, Operation* inst, std::func
|
|||||||
inst->erase();
|
inst->erase();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encapsulateSlice(IRRewriter& rewriter, Location loc, Operation* inst) {
|
||||||
|
if (tensor::ExtractSliceOp toRemoveOp = llvm::dyn_cast_if_present<tensor::ExtractSliceOp>(inst)) {
|
||||||
|
auto source = toRemoveOp.getSource();
|
||||||
|
rewriter.setInsertionPointAfter(toRemoveOp);
|
||||||
|
auto newCompute = spatial::SpatCompute::create(rewriter, loc, inst->getResultTypes(), source);
|
||||||
|
auto BB = rewriter.createBlock(&newCompute.getBody(), newCompute.getBody().end(), {source.getType()}, {loc});
|
||||||
|
newCompute.getProperties().setOperandSegmentSizes({(int) 0, (int) 1});
|
||||||
|
rewriter.setInsertionPointToEnd(BB);
|
||||||
|
IRMapping mapper;
|
||||||
|
mapper.map(source, BB->getArgument(0));
|
||||||
|
auto newInst = rewriter.clone(*inst, mapper);
|
||||||
|
spatial::SpatYieldOp::create(rewriter, loc, newInst->getResults());
|
||||||
|
inst->replaceAllUsesWith(newCompute->getResults());
|
||||||
|
inst->erase();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -194,6 +253,29 @@ bool encapsulateConcat(IRRewriter& rewriter, Location loc, Operation* inst) {
|
|||||||
newCompute.getProperties().setOperandSegmentSizes({(int) 0, (int) sources.size()});
|
newCompute.getProperties().setOperandSegmentSizes({(int) 0, (int) sources.size()});
|
||||||
rewriter.setInsertionPointToEnd(BB);
|
rewriter.setInsertionPointToEnd(BB);
|
||||||
IRMapping mapper;
|
IRMapping mapper;
|
||||||
|
for (auto [source, bbArg] : llvm::zip(sources, BB->getArguments()))
|
||||||
|
mapper.map(source, bbArg);
|
||||||
|
auto newConcat = spatial::SpatConcatOp::create(rewriter,
|
||||||
|
loc,
|
||||||
|
toRemoveOp.getType(),
|
||||||
|
rewriter.getI64IntegerAttr(toRemoveOp.getDim()),
|
||||||
|
ValueRange(BB->getArguments()));
|
||||||
|
spatial::SpatYieldOp::create(rewriter, loc, newConcat.getOutput());
|
||||||
|
inst->replaceAllUsesWith(newCompute->getResults());
|
||||||
|
inst->erase();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
auto newCompute = spatial::SpatCompute::create(rewriter, loc, inst->getResultTypes(), sources);
|
||||||
|
SmallVector<Type> sourceTypes;
|
||||||
|
SmallVector<Location> sourceLoc;
|
||||||
|
for (auto source : sources) {
|
||||||
|
sourceTypes.push_back(source.getType());
|
||||||
|
sourceLoc.push_back(loc);
|
||||||
|
}
|
||||||
|
auto BB = rewriter.createBlock(&newCompute.getBody(), newCompute.getBody().end(), sourceTypes, sourceLoc);
|
||||||
|
newCompute.getProperties().setOperandSegmentSizes({(int) 0, (int) sources.size()});
|
||||||
|
rewriter.setInsertionPointToEnd(BB);
|
||||||
|
IRMapping mapper;
|
||||||
for (auto [source, bbArg] : llvm::zip(sources, BB->getArguments()))
|
for (auto [source, bbArg] : llvm::zip(sources, BB->getArguments()))
|
||||||
mapper.map(source, bbArg);
|
mapper.map(source, bbArg);
|
||||||
auto newConcat = rewriter.clone(*inst, mapper);
|
auto newConcat = rewriter.clone(*inst, mapper);
|
||||||
@@ -202,7 +284,6 @@ bool encapsulateConcat(IRRewriter& rewriter, Location loc, Operation* inst) {
|
|||||||
inst->erase();
|
inst->erase();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,6 +344,89 @@ static FailureOr<Value> materializeWeightLikeValueInBlock(Value value, IRRewrite
|
|||||||
return cast<Value>(mapped);
|
return cast<Value>(mapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool sourceOpernadHasWeightAlways(Operation* op) {
|
||||||
|
if (op == nullptr)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Operation* source = nullptr;
|
||||||
|
do {
|
||||||
|
|
||||||
|
if (isa<spatial::SpatCompute, spatial::SpatComputeBatch>(*op)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (auto extractSliceOp = dyn_cast<tensor::ExtractSliceOp>(*op)) {
|
||||||
|
auto tmpSource = extractSliceOp.getSource();
|
||||||
|
auto definingOp = tmpSource.getDefiningOp();
|
||||||
|
if (definingOp)
|
||||||
|
op = definingOp;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (auto extractRowsOp = dyn_cast<spatial::SpatExtractRowsOp>(*op)) {
|
||||||
|
auto tmpSource = extractRowsOp.getInput();
|
||||||
|
auto definingOp = tmpSource.getDefiningOp();
|
||||||
|
if (definingOp)
|
||||||
|
op = definingOp;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (auto expandShapeOp = dyn_cast<tensor::ExpandShapeOp>(*op)) {
|
||||||
|
auto tmpSource = expandShapeOp.getSrc();
|
||||||
|
auto definingOp = tmpSource.getDefiningOp();
|
||||||
|
if (definingOp)
|
||||||
|
op = definingOp;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (auto transposeOp = dyn_cast<ONNXTransposeOp>(*op)) {
|
||||||
|
auto tmpSource = transposeOp.getData();
|
||||||
|
auto definingOp = tmpSource.getDefiningOp();
|
||||||
|
if (definingOp)
|
||||||
|
op = definingOp;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (auto collapseShapeOp = dyn_cast<tensor::CollapseShapeOp>(*op)) {
|
||||||
|
auto tmpSource = collapseShapeOp.getSrc();
|
||||||
|
auto definingOp = tmpSource.getDefiningOp();
|
||||||
|
if (definingOp)
|
||||||
|
op = definingOp;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (auto constantOp = dyn_cast<arith::ConstantOp>(*op)) {
|
||||||
|
source = constantOp;
|
||||||
|
}
|
||||||
|
else if (auto concatOp = dyn_cast<tensor::ConcatOp>(*op)) {
|
||||||
|
bool res = false;
|
||||||
|
for (auto operand : concatOp.getOperands()) {
|
||||||
|
res |= hasWeightAlways(operand.getDefiningOp());
|
||||||
|
if (res)
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
else if (auto concatOp = dyn_cast<spatial::SpatConcatOp>(*op)) {
|
||||||
|
bool res = false;
|
||||||
|
for (auto operand : concatOp.getOperands()) {
|
||||||
|
res |= hasWeightAlways(operand.getDefiningOp());
|
||||||
|
if (res)
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
op->dump();
|
||||||
|
llvm_unreachable("Global instruction not handle in func");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (source == nullptr);
|
||||||
|
|
||||||
|
if (hasWeightAlways(source))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO what we want to keep in global?
|
// TODO what we want to keep in global?
|
||||||
void ONNXToSpatialPass::encapsulateGlobalInstruction(func::FuncOp funcOp) {
|
void ONNXToSpatialPass::encapsulateGlobalInstruction(func::FuncOp funcOp) {
|
||||||
Location loc = funcOp.getLoc();
|
Location loc = funcOp.getLoc();
|
||||||
@@ -271,8 +435,14 @@ void ONNXToSpatialPass::encapsulateGlobalInstruction(func::FuncOp funcOp) {
|
|||||||
while (keep) {
|
while (keep) {
|
||||||
keep = false;
|
keep = false;
|
||||||
for (auto& instruction : llvm::make_early_inc_range(funcOp.getOps())) {
|
for (auto& instruction : llvm::make_early_inc_range(funcOp.getOps())) {
|
||||||
keep |= encapsulator<tensor::ExtractSliceOp>(
|
|
||||||
rewriter, loc, &instruction, [](tensor::ExtractSliceOp extract) { return extract.getSource(); });
|
if (isa<spatial::SpatCompute, spatial::SpatComputeBatch, spatial::SpatConcatOp, spatial::SpatExtractRowsOp>(
|
||||||
|
instruction)
|
||||||
|
|| isa<func::ReturnOp>(instruction)
|
||||||
|
|| sourceOpernadHasWeightAlways(&instruction))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
keep |= encapsulateSlice(rewriter, loc, &instruction);
|
||||||
|
|
||||||
keep |= encapsulator<tensor::ExpandShapeOp>(
|
keep |= encapsulator<tensor::ExpandShapeOp>(
|
||||||
rewriter, loc, &instruction, [](tensor::ExpandShapeOp expand) { return expand.getSrc(); });
|
rewriter, loc, &instruction, [](tensor::ExpandShapeOp expand) { return expand.getSrc(); });
|
||||||
|
|||||||
@@ -147,11 +147,11 @@ static Value buildPackedBias(bool hasBias,
|
|||||||
return arith::ConstantOp::create(rewriter, loc, packedBiasType, packedBiasAttr).getResult();
|
return arith::ConstantOp::create(rewriter, loc, packedBiasType, packedBiasAttr).getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
static SmallVector<Value> createIm2colRowComputes(Value x,
|
static Value createIm2colRowComputes(Value x,
|
||||||
RankedTensorType xType,
|
RankedTensorType xType,
|
||||||
RankedTensorType im2colType,
|
RankedTensorType im2colType,
|
||||||
RankedTensorType im2colRowType,
|
RankedTensorType im2colRowType,
|
||||||
RankedTensorType gemmInputRowType,
|
RankedTensorType gemmInputRowsType,
|
||||||
int64_t batchSize,
|
int64_t batchSize,
|
||||||
int64_t numChannelsIn,
|
int64_t numChannelsIn,
|
||||||
int64_t xHeight,
|
int64_t xHeight,
|
||||||
@@ -176,8 +176,8 @@ static SmallVector<Value> createIm2colRowComputes(Value x,
|
|||||||
auto elemType = xType.getElementType();
|
auto elemType = xType.getElementType();
|
||||||
constexpr size_t numInputs = 1;
|
constexpr size_t numInputs = 1;
|
||||||
const int64_t packedNumRows = ceilIntegerDivide(numPatches, packFactor);
|
const int64_t packedNumRows = ceilIntegerDivide(numPatches, packFactor);
|
||||||
SmallVector<Type> resultTypes(packedNumRows, gemmInputRowType);
|
auto im2colComputeOp =
|
||||||
auto im2colComputeOp = createSpatCompute<numInputs>(rewriter, loc, resultTypes, {}, x, [&](Value xArg) {
|
createSpatCompute<numInputs>(rewriter, loc, TypeRange {gemmInputRowsType}, {}, x, [&](Value xArg) {
|
||||||
Value paddedInput = xArg;
|
Value paddedInput = xArg;
|
||||||
|
|
||||||
// Pad input with zeros if needed:
|
// Pad input with zeros if needed:
|
||||||
@@ -285,23 +285,10 @@ static SmallVector<Value> createIm2colRowComputes(Value x,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
SmallVector<Value> rowResults;
|
spatial::SpatYieldOp::create(rewriter, loc, gemmInputRows);
|
||||||
rowResults.reserve(packedNumRows);
|
|
||||||
for (int64_t rowIdx = 0; rowIdx < packedNumRows; rowIdx++) {
|
|
||||||
SmallVector<OpFoldResult> offsets = {rewriter.getIndexAttr(rowIdx), rewriter.getIndexAttr(0)};
|
|
||||||
SmallVector<OpFoldResult> sizes = {rewriter.getIndexAttr(1), rewriter.getIndexAttr(packFactor * patchSize)};
|
|
||||||
SmallVector<OpFoldResult> strides = {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)};
|
|
||||||
rowResults.push_back(
|
|
||||||
tensor::ExtractSliceOp::create(rewriter, loc, gemmInputRowType, gemmInputRows, offsets, sizes, strides));
|
|
||||||
}
|
|
||||||
spatial::SpatYieldOp::create(rewriter, loc, rowResults);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
SmallVector<Value> rows;
|
return im2colComputeOp.getResult(0);
|
||||||
rows.reserve(im2colComputeOp.getNumResults());
|
|
||||||
for (Value result : im2colComputeOp.getResults())
|
|
||||||
rows.push_back(result);
|
|
||||||
return rows;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value createCollectedConvOutput(ValueRange gemmRows,
|
static Value createCollectedConvOutput(ValueRange gemmRows,
|
||||||
@@ -319,15 +306,12 @@ static Value createCollectedConvOutput(ValueRange gemmRows,
|
|||||||
auto collectComputeOp = createSpatCompute(rewriter, loc, convType, {}, gemmRows, [&](ValueRange gemmRowArgs) {
|
auto collectComputeOp = createSpatCompute(rewriter, loc, convType, {}, gemmRows, [&](ValueRange gemmRowArgs) {
|
||||||
Value gemmOut;
|
Value gemmOut;
|
||||||
if (packFactor == 1) {
|
if (packFactor == 1) {
|
||||||
gemmOut = gemmRowArgs.size() == 1 ? gemmRowArgs.front()
|
gemmOut = createSpatConcat(rewriter, loc, /*axis=*/0, gemmRowArgs);
|
||||||
: tensor::ConcatOp::create(rewriter, loc, /*axis=*/0, gemmRowArgs).getResult();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
auto expandedType = RankedTensorType::get({packedNumRows, packFactor, numChannelsOut}, outType.getElementType());
|
auto expandedType = RankedTensorType::get({packedNumRows, packFactor, numChannelsOut}, outType.getElementType());
|
||||||
auto paddedType = RankedTensorType::get({paddedNumPatches, numChannelsOut}, outType.getElementType());
|
auto paddedType = RankedTensorType::get({paddedNumPatches, numChannelsOut}, outType.getElementType());
|
||||||
Value packedOutput = gemmRowArgs.size() == 1
|
Value packedOutput = createSpatConcat(rewriter, loc, /*axis=*/0, gemmRowArgs);
|
||||||
? gemmRowArgs.front()
|
|
||||||
: tensor::ConcatOp::create(rewriter, loc, /*axis=*/0, gemmRowArgs).getResult();
|
|
||||||
Value expandedOutput = tensor::ExpandShapeOp::create(rewriter,
|
Value expandedOutput = tensor::ExpandShapeOp::create(rewriter,
|
||||||
loc,
|
loc,
|
||||||
expandedType,
|
expandedType,
|
||||||
@@ -509,14 +493,15 @@ LogicalResult ConvToGemm::matchAndRewrite(ONNXConvOp convOp,
|
|||||||
// A_packed: [ceil(numPatches / N), N * patchSize]
|
// A_packed: [ceil(numPatches / N), N * patchSize]
|
||||||
// B_packed: [N * patchSize, N * cOut]
|
// B_packed: [N * patchSize, N * cOut]
|
||||||
// Y_packed: [ceil(numPatches / N), N * cOut]
|
// Y_packed: [ceil(numPatches / N), N * cOut]
|
||||||
auto gemmInputRowType = RankedTensorType::get({1, effectiveMaxParallelPixels * patchSize}, elemType);
|
const int64_t packedNumRows = ceilIntegerDivide(numPatches, effectiveMaxParallelPixels);
|
||||||
auto gemmOutputRowType =
|
auto gemmInputRowsType = RankedTensorType::get({packedNumRows, effectiveMaxParallelPixels * patchSize}, elemType);
|
||||||
RankedTensorType::get({1, effectiveMaxParallelPixels * numChannelsOut}, outType.getElementType());
|
auto gemmOutputRowsType =
|
||||||
SmallVector<Value> gemmInputRows = createIm2colRowComputes(x,
|
RankedTensorType::get({packedNumRows, effectiveMaxParallelPixels * numChannelsOut}, outType.getElementType());
|
||||||
|
Value gemmInputRows = createIm2colRowComputes(x,
|
||||||
xType,
|
xType,
|
||||||
im2colType,
|
im2colType,
|
||||||
rowType,
|
rowType,
|
||||||
gemmInputRowType,
|
gemmInputRowsType,
|
||||||
batchSize,
|
batchSize,
|
||||||
numChannelsIn,
|
numChannelsIn,
|
||||||
xHeight,
|
xHeight,
|
||||||
@@ -553,13 +538,10 @@ LogicalResult ConvToGemm::matchAndRewrite(ONNXConvOp convOp,
|
|||||||
Value gemmC = buildPackedBias(
|
Value gemmC = buildPackedBias(
|
||||||
hasB, gemmBias, biasMatrix, biasDenseAttr, outType, numChannelsOut, effectiveMaxParallelPixels, rewriter, loc);
|
hasB, gemmBias, biasMatrix, biasDenseAttr, outType, numChannelsOut, effectiveMaxParallelPixels, rewriter, loc);
|
||||||
|
|
||||||
SmallVector<Value> gemmRows;
|
Value gemmRows = ONNXGemmOp::create(rewriter,
|
||||||
gemmRows.reserve(gemmInputRows.size());
|
|
||||||
for (Value gemmInputRow : gemmInputRows) {
|
|
||||||
Value gemmRow = ONNXGemmOp::create(rewriter,
|
|
||||||
loc,
|
loc,
|
||||||
gemmOutputRowType,
|
gemmOutputRowsType,
|
||||||
gemmInputRow,
|
gemmInputRows,
|
||||||
gemmB,
|
gemmB,
|
||||||
gemmC,
|
gemmC,
|
||||||
rewriter.getF32FloatAttr(1.0f),
|
rewriter.getF32FloatAttr(1.0f),
|
||||||
@@ -567,11 +549,9 @@ LogicalResult ConvToGemm::matchAndRewrite(ONNXConvOp convOp,
|
|||||||
rewriter.getBoolAttr(false),
|
rewriter.getBoolAttr(false),
|
||||||
rewriter.getBoolAttr(false))
|
rewriter.getBoolAttr(false))
|
||||||
.getY();
|
.getY();
|
||||||
gemmRows.push_back(gemmRow);
|
|
||||||
}
|
|
||||||
|
|
||||||
rewriter.replaceOp(convOp,
|
rewriter.replaceOp(convOp,
|
||||||
createCollectedConvOutput(gemmRows,
|
createCollectedConvOutput(ValueRange {gemmRows},
|
||||||
convOp.getType(),
|
convOp.getType(),
|
||||||
gemmOutType,
|
gemmOutType,
|
||||||
nhwcType,
|
nhwcType,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
||||||
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
|
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
|
||||||
#include "mlir/IR/BuiltinTypes.h"
|
#include "mlir/IR/BuiltinTypes.h"
|
||||||
|
#include "mlir/IR/IRMapping.h"
|
||||||
#include "mlir/IR/Location.h"
|
#include "mlir/IR/Location.h"
|
||||||
#include "mlir/Support/LogicalResult.h"
|
#include "mlir/Support/LogicalResult.h"
|
||||||
#include "mlir/Transforms/DialectConversion.h"
|
#include "mlir/Transforms/DialectConversion.h"
|
||||||
@@ -65,6 +66,66 @@ struct GemvToSpatialCompute : OpConversionPattern<ONNXGemmOp> {
|
|||||||
ConversionPatternRewriter& rewriter) const override;
|
ConversionPatternRewriter& rewriter) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GemmToSpatialComputeBatch : OpConversionPattern<ONNXGemmOp> {
|
||||||
|
using OpConversionPattern::OpConversionPattern;
|
||||||
|
|
||||||
|
LogicalResult matchAndRewrite(ONNXGemmOp gemmOp,
|
||||||
|
ONNXGemmOpAdaptor gemmOpAdaptor,
|
||||||
|
ConversionPatternRewriter& rewriter) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
static SmallVector<Value> materializeBatchRowSlices(Value matrix,
|
||||||
|
RankedTensorType matrixType,
|
||||||
|
ConversionPatternRewriter& rewriter,
|
||||||
|
Location loc) {
|
||||||
|
const int64_t numRows = matrixType.getDimSize(0);
|
||||||
|
auto rowType = RankedTensorType::get({1, matrixType.getDimSize(1)}, matrixType.getElementType());
|
||||||
|
SmallVector<Type> resultTypes(static_cast<size_t>(numRows), rowType);
|
||||||
|
|
||||||
|
auto buildRowSlices = [&](Value matrixArg) {
|
||||||
|
auto extractRowsOp = spatial::SpatExtractRowsOp::create(rewriter, loc, TypeRange(resultTypes), matrixArg);
|
||||||
|
return SmallVector<Value>(extractRowsOp->result_begin(), extractRowsOp->result_end());
|
||||||
|
};
|
||||||
|
|
||||||
|
auto cloneBatchInputChainIntoSliceCompute =
|
||||||
|
[&](Value rootInput, SmallVector<Operation*> chainOps, Value rootValue) -> SmallVector<Value> {
|
||||||
|
auto sliceCompute =
|
||||||
|
createSpatCompute<1>(rewriter, loc, TypeRange(resultTypes), {}, ValueRange {rootInput}, [&](Value input) {
|
||||||
|
Value transformedMatrix = input;
|
||||||
|
if (!chainOps.empty()) {
|
||||||
|
IRMapping mapper;
|
||||||
|
mapper.map(rootValue, input);
|
||||||
|
for (Operation* chainOp : chainOps)
|
||||||
|
rewriter.clone(*chainOp, mapper);
|
||||||
|
transformedMatrix = cast<Value>(mapper.lookup(matrix));
|
||||||
|
}
|
||||||
|
spatial::SpatYieldOp::create(rewriter, loc, buildRowSlices(transformedMatrix));
|
||||||
|
});
|
||||||
|
SmallVector<Value> rowSlices(sliceCompute->result_begin(), sliceCompute->result_end());
|
||||||
|
return rowSlices;
|
||||||
|
};
|
||||||
|
|
||||||
|
SmallVector<Operation*> chainOps;
|
||||||
|
Value rootValue = matrix;
|
||||||
|
while (Operation* definingOp = rootValue.getDefiningOp()) {
|
||||||
|
if (auto rootCompute = dyn_cast<spatial::SpatCompute>(definingOp)) {
|
||||||
|
SmallVector<Operation*> reversedChainOps(chainOps.rbegin(), chainOps.rend());
|
||||||
|
return cloneBatchInputChainIntoSliceCompute(
|
||||||
|
rootCompute.getResult(cast<OpResult>(rootValue).getResultNumber()), reversedChainOps, rootValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (definingOp->getNumOperands() != 1)
|
||||||
|
break;
|
||||||
|
if (!isa<tensor::ExtractSliceOp, tensor::ExpandShapeOp, tensor::CollapseShapeOp, ONNXTransposeOp>(definingOp))
|
||||||
|
break;
|
||||||
|
|
||||||
|
chainOps.push_back(definingOp);
|
||||||
|
rootValue = definingOp->getOperand(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildRowSlices(matrix);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
LogicalResult GemmToManyGemv::matchAndRewrite(ONNXGemmOp gemmOp,
|
LogicalResult GemmToManyGemv::matchAndRewrite(ONNXGemmOp gemmOp,
|
||||||
@@ -156,8 +217,7 @@ LogicalResult GemmToManyGemv::matchAndRewrite(ONNXGemmOp gemmOp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto concatComputeOp = createSpatCompute(rewriter, loc, gemmOp.getType(), {}, gemvOps, [&](ValueRange gemvOpsArgs) {
|
auto concatComputeOp = createSpatCompute(rewriter, loc, gemmOp.getType(), {}, gemvOps, [&](ValueRange gemvOpsArgs) {
|
||||||
auto concatOp = tensor::ConcatOp::create(rewriter, loc, /*axis=*/0, gemvOpsArgs);
|
spatial::SpatYieldOp::create(rewriter, loc, createSpatConcat(rewriter, loc, /*axis=*/0, gemvOpsArgs));
|
||||||
spatial::SpatYieldOp::create(rewriter, loc, concatOp.getResult());
|
|
||||||
});
|
});
|
||||||
|
|
||||||
rewriter.replaceOp(gemmOp, concatComputeOp);
|
rewriter.replaceOp(gemmOp, concatComputeOp);
|
||||||
@@ -313,8 +373,108 @@ LogicalResult GemvToSpatialCompute::matchAndRewrite(ONNXGemmOp gemmOp,
|
|||||||
|
|
||||||
auto concatComputeOp =
|
auto concatComputeOp =
|
||||||
createSpatCompute(rewriter, gemmLoc, gemmOp.getType(), {}, outHSlices, [&](ValueRange blockArgs) {
|
createSpatCompute(rewriter, gemmLoc, gemmOp.getType(), {}, outHSlices, [&](ValueRange blockArgs) {
|
||||||
auto concatOp = tensor::ConcatOp::create(rewriter, gemmLoc, /*axis=*/1, blockArgs);
|
spatial::SpatYieldOp::create(rewriter, gemmLoc, createSpatConcat(rewriter, gemmLoc, /*axis=*/1, blockArgs));
|
||||||
spatial::SpatYieldOp::create(rewriter, gemmLoc, concatOp.getResult());
|
});
|
||||||
|
|
||||||
|
rewriter.replaceOp(gemmOp, concatComputeOp);
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
|
||||||
|
LogicalResult GemmToSpatialComputeBatch::matchAndRewrite(ONNXGemmOp gemmOp,
|
||||||
|
ONNXGemmOpAdaptor gemmOpAdaptor,
|
||||||
|
ConversionPatternRewriter& rewriter) const {
|
||||||
|
Location loc = gemmOp.getLoc();
|
||||||
|
Value a = gemmOpAdaptor.getA();
|
||||||
|
Value b = gemmOpAdaptor.getB();
|
||||||
|
Value c = gemmOpAdaptor.getC();
|
||||||
|
|
||||||
|
assert("A should have been transposed already" && !gemmOpAdaptor.getTransA());
|
||||||
|
|
||||||
|
bool hasC = !isa<ONNXNoneOp>(c.getDefiningOp());
|
||||||
|
|
||||||
|
auto aType = cast<RankedTensorType>(a.getType());
|
||||||
|
auto bType = cast<RankedTensorType>(b.getType());
|
||||||
|
auto outType = cast<RankedTensorType>(gemmOp.getY().getType());
|
||||||
|
assert("Only support static shapes" && aType.hasStaticShape() && bType.hasStaticShape() && outType.hasStaticShape());
|
||||||
|
|
||||||
|
const int64_t numOutRows = aType.getDimSize(0);
|
||||||
|
if (numOutRows <= 1)
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
// Only handle the single-tile case: K <= crossbarSize and N <= crossbarSize
|
||||||
|
if (aType.getDimSize(1) > static_cast<int64_t>(crossbarSize.getValue())
|
||||||
|
|| outType.getDimSize(1) > static_cast<int64_t>(crossbarSize.getValue()))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
auto scaledB = materializeScaledConstantTensor(b, gemmOpAdaptor.getAlpha().convertToFloat(), rewriter, loc);
|
||||||
|
if (failed(scaledB))
|
||||||
|
return failure();
|
||||||
|
b = *scaledB;
|
||||||
|
bType = cast<RankedTensorType>(b.getType());
|
||||||
|
|
||||||
|
if (gemmOpAdaptor.getTransB()) {
|
||||||
|
auto bShape = bType.getShape();
|
||||||
|
auto transposedType = bType.cloneWith(ArrayRef({bShape[1], bShape[0]}), bType.getElementType());
|
||||||
|
b = ONNXTransposeOp::create(rewriter, loc, transposedType, b, rewriter.getI64ArrayAttr({1, 0}));
|
||||||
|
bType = cast<RankedTensorType>(b.getType());
|
||||||
|
}
|
||||||
|
(void) bType;
|
||||||
|
|
||||||
|
Value sharedBias;
|
||||||
|
if (hasC) {
|
||||||
|
auto scaledC = materializeScaledConstantTensor(c, gemmOpAdaptor.getBeta().convertToFloat(), rewriter, loc);
|
||||||
|
if (failed(scaledC))
|
||||||
|
return failure();
|
||||||
|
c = *scaledC;
|
||||||
|
auto cType = cast<RankedTensorType>(c.getType());
|
||||||
|
if (cType.getRank() == 1) {
|
||||||
|
auto expandedType = RankedTensorType::get({1, cType.getDimSize(0)}, cType.getElementType());
|
||||||
|
c = tensor::ExpandShapeOp::create(rewriter,
|
||||||
|
loc,
|
||||||
|
expandedType,
|
||||||
|
c,
|
||||||
|
SmallVector<ReassociationIndices> {
|
||||||
|
{0, 1}
|
||||||
|
});
|
||||||
|
cType = cast<RankedTensorType>(c.getType());
|
||||||
|
}
|
||||||
|
assert("Only support rank 2 tensor for C" && cType.getRank() == 2);
|
||||||
|
// Row-specific bias can't share a single template body; fall through to GemmToManyGemv
|
||||||
|
if (cType.getDimSize(0) == numOutRows && numOutRows > 1)
|
||||||
|
return failure();
|
||||||
|
if (cType.getDimSize(0) == 1 && cType.getDimSize(1) == 1)
|
||||||
|
c = broadcastToVector(c, outType.getDimSize(1), rewriter, loc);
|
||||||
|
sharedBias = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
SmallVector<Value> aSlices = materializeBatchRowSlices(a, aType, rewriter, loc);
|
||||||
|
auto aSliceType = cast<RankedTensorType>(aSlices.front().getType());
|
||||||
|
|
||||||
|
auto outRowType = RankedTensorType::get({1, outType.getDimSize(1)}, outType.getElementType());
|
||||||
|
SmallVector<Type> resultTypes(static_cast<size_t>(numOutRows), outRowType);
|
||||||
|
SmallVector<Value> weights(static_cast<size_t>(numOutRows), b);
|
||||||
|
|
||||||
|
auto batchOp = spatial::SpatComputeBatch::create(rewriter,
|
||||||
|
loc,
|
||||||
|
TypeRange(resultTypes),
|
||||||
|
rewriter.getI32IntegerAttr(static_cast<int32_t>(numOutRows)),
|
||||||
|
ValueRange(weights),
|
||||||
|
ValueRange(aSlices));
|
||||||
|
|
||||||
|
Block* body = rewriter.createBlock(
|
||||||
|
&batchOp.getBody(), batchOp.getBody().end(), TypeRange {aSliceType}, SmallVector<Location>(1, loc));
|
||||||
|
rewriter.setInsertionPointToEnd(body);
|
||||||
|
|
||||||
|
Value vmmResult = spatial::SpatWeightedVMMOp::create(rewriter, loc, outRowType, 0, body->getArgument(0)).getResult();
|
||||||
|
Value laneResult = vmmResult;
|
||||||
|
if (sharedBias)
|
||||||
|
laneResult = spatial::SpatVAddOp::create(rewriter, loc, outRowType, vmmResult, sharedBias).getResult();
|
||||||
|
spatial::SpatYieldOp::create(rewriter, loc, laneResult);
|
||||||
|
|
||||||
|
rewriter.setInsertionPointAfter(batchOp);
|
||||||
|
SmallVector<Value> laneResults(batchOp->result_begin(), batchOp->result_end());
|
||||||
|
auto concatComputeOp = createSpatCompute(rewriter, loc, gemmOp.getType(), {}, laneResults, [&](ValueRange args) {
|
||||||
|
spatial::SpatYieldOp::create(rewriter, loc, createSpatConcat(rewriter, loc, /*axis=*/0, args));
|
||||||
});
|
});
|
||||||
|
|
||||||
rewriter.replaceOp(gemmOp, concatComputeOp);
|
rewriter.replaceOp(gemmOp, concatComputeOp);
|
||||||
@@ -322,6 +482,7 @@ LogicalResult GemvToSpatialCompute::matchAndRewrite(ONNXGemmOp gemmOp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void populateGemmPatterns(RewritePatternSet& patterns, MLIRContext* ctx) {
|
void populateGemmPatterns(RewritePatternSet& patterns, MLIRContext* ctx) {
|
||||||
|
patterns.insert<GemmToSpatialComputeBatch>(ctx, PatternBenefit(2));
|
||||||
patterns.insert<GemmToManyGemv>(ctx);
|
patterns.insert<GemmToManyGemv>(ctx);
|
||||||
patterns.insert<GemvToSpatialCompute>(ctx);
|
patterns.insert<GemvToSpatialCompute>(ctx);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -232,9 +232,7 @@ struct MatMulToGemm : OpRewritePattern<ONNXMatMulOp> {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
Value result = batchResults.size() == 1
|
Value result = createSpatConcat(rewriter, loc, /*axis=*/0, batchResults);
|
||||||
? batchResults.front()
|
|
||||||
: tensor::ConcatOp::create(rewriter, loc, /*axis=*/0, batchResults).getResult();
|
|
||||||
rewriter.replaceOp(matmulOp, result);
|
rewriter.replaceOp(matmulOp, result);
|
||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,8 +100,7 @@ static Value buildReduceMeanKeepdims(Value input,
|
|||||||
for (Value slice : slices)
|
for (Value slice : slices)
|
||||||
reducedSlices.push_back(buildReduceMeanKeepdims(slice, reducedAxes, axis + 1, leafType, rewriter, loc));
|
reducedSlices.push_back(buildReduceMeanKeepdims(slice, reducedAxes, axis + 1, leafType, rewriter, loc));
|
||||||
|
|
||||||
return reducedSlices.size() == 1 ? reducedSlices.front()
|
return createSpatConcat(rewriter, loc, axis, reducedSlices);
|
||||||
: tensor::ConcatOp::create(rewriter, loc, axis, reducedSlices).getResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value squeezeReducedAxes(Value keepdimsValue,
|
static Value squeezeReducedAxes(Value keepdimsValue,
|
||||||
|
|||||||
@@ -33,9 +33,7 @@ static int64_t getOptionalI64(std::optional<ArrayAttrT> arrayAttr, size_t index,
|
|||||||
|
|
||||||
static Value concatAlongAxis(ConversionPatternRewriter& rewriter, Location loc, int64_t axis, ArrayRef<Value> values) {
|
static Value concatAlongAxis(ConversionPatternRewriter& rewriter, Location loc, int64_t axis, ArrayRef<Value> values) {
|
||||||
assert(!values.empty() && "Expected at least one value to concatenate.");
|
assert(!values.empty() && "Expected at least one value to concatenate.");
|
||||||
if (values.size() == 1)
|
return createSpatConcat(rewriter, loc, axis, values);
|
||||||
return values.front();
|
|
||||||
return tensor::ConcatOp::create(rewriter, loc, axis, values);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value materializeContiguousTile(ConversionPatternRewriter& rewriter, Location loc, Value tile) {
|
static Value materializeContiguousTile(ConversionPatternRewriter& rewriter, Location loc, Value tile) {
|
||||||
|
|||||||
@@ -47,8 +47,7 @@ buildSoftmax(Value input, int64_t softmaxAxis, int64_t axis, ConversionPatternRe
|
|||||||
for (Value slice : slices)
|
for (Value slice : slices)
|
||||||
rebuiltSlices.push_back(buildSoftmax(slice, softmaxAxis, axis + 1, rewriter, loc));
|
rebuiltSlices.push_back(buildSoftmax(slice, softmaxAxis, axis + 1, rewriter, loc));
|
||||||
|
|
||||||
return rebuiltSlices.size() == 1 ? rebuiltSlices.front()
|
return createSpatConcat(rewriter, loc, axis, rebuiltSlices);
|
||||||
: tensor::ConcatOp::create(rewriter, loc, axis, rebuiltSlices).getResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SoftmaxToSpatialCompute : OpConversionPattern<ONNXSoftmaxOp> {
|
struct SoftmaxToSpatialCompute : OpConversionPattern<ONNXSoftmaxOp> {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "mlir/IR/PatternMatch.h"
|
#include "mlir/IR/PatternMatch.h"
|
||||||
|
|
||||||
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common.hpp"
|
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common.hpp"
|
||||||
|
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
||||||
#include "src/Dialect/ONNX/ONNXOps.hpp"
|
#include "src/Dialect/ONNX/ONNXOps.hpp"
|
||||||
|
|
||||||
using namespace mlir;
|
using namespace mlir;
|
||||||
@@ -17,7 +18,7 @@ struct Concat : public OpConversionPattern<ONNXConcatOp> {
|
|||||||
auto inputs = adaptor.getInputs();
|
auto inputs = adaptor.getInputs();
|
||||||
int64_t axis = adaptor.getAxis();
|
int64_t axis = adaptor.getAxis();
|
||||||
|
|
||||||
rewriter.replaceOpWithNewOp<tensor::ConcatOp>(maxpoolOp, axis, inputs);
|
rewriter.replaceOp(maxpoolOp, createSpatConcat(rewriter, maxpoolOp.getLoc(), axis, inputs));
|
||||||
|
|
||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ static Value concatGatherSlices(Value data,
|
|||||||
}
|
}
|
||||||
if (slices.empty())
|
if (slices.empty())
|
||||||
return {};
|
return {};
|
||||||
return slices.size() == 1 ? slices.front() : tensor::ConcatOp::create(rewriter, loc, axis, slices).getResult();
|
return createSpatConcat(rewriter, loc, axis, slices);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value addLeadingGatherDim(Value value, int64_t axis, ConversionPatternRewriter& rewriter, Location loc) {
|
static Value addLeadingGatherDim(Value value, int64_t axis, ConversionPatternRewriter& rewriter, Location loc) {
|
||||||
@@ -130,9 +130,7 @@ struct Gather : OpConversionPattern<ONNXGatherOp> {
|
|||||||
return failure();
|
return failure();
|
||||||
rows.push_back(addLeadingGatherDim(gatheredRow, axis, rewriter, loc));
|
rows.push_back(addLeadingGatherDim(gatheredRow, axis, rewriter, loc));
|
||||||
}
|
}
|
||||||
result = rows.size() == 1
|
result = createSpatConcat(rewriter, loc, /*axis=*/axis, rows);
|
||||||
? rows.front()
|
|
||||||
: tensor::ConcatOp::create(rewriter, loc, /*axis=*/axis, rows).getResult();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return failure();
|
return failure();
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ static Value buildNearestResize(Value input,
|
|||||||
slices.push_back(buildNearestResize(slice, inputShape, outputShape, axis + 1, rewriter, loc));
|
slices.push_back(buildNearestResize(slice, inputShape, outputShape, axis + 1, rewriter, loc));
|
||||||
}
|
}
|
||||||
|
|
||||||
return slices.size() == 1 ? slices.front() : tensor::ConcatOp::create(rewriter, loc, axis, slices).getResult();
|
return createSpatConcat(rewriter, loc, axis, slices);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Resize : OpConversionPattern<ONNXResizeOp> {
|
struct Resize : OpConversionPattern<ONNXResizeOp> {
|
||||||
|
|||||||
@@ -23,7 +23,10 @@ static Value extractSliceAt(
|
|||||||
sizes.push_back(rewriter.getIndexAttr(dim));
|
sizes.push_back(rewriter.getIndexAttr(dim));
|
||||||
offsets[axis] = rewriter.getIndexAttr(offset);
|
offsets[axis] = rewriter.getIndexAttr(offset);
|
||||||
sizes[axis] = rewriter.getIndexAttr(size);
|
sizes[axis] = rewriter.getIndexAttr(size);
|
||||||
return tensor::ExtractSliceOp::create(rewriter, loc, input, offsets, sizes, strides);
|
SmallVector<int64_t> resultShape(inputType.getShape());
|
||||||
|
resultShape[axis] = size;
|
||||||
|
auto resultType = RankedTensorType::get(resultShape, inputType.getElementType());
|
||||||
|
return tensor::ExtractSliceOp::create(rewriter, loc, resultType, input, offsets, sizes, strides);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Split : OpConversionPattern<ONNXSplitOp> {
|
struct Split : OpConversionPattern<ONNXSplitOp> {
|
||||||
@@ -49,12 +52,7 @@ struct Split : OpConversionPattern<ONNXSplitOp> {
|
|||||||
if (!resultType || !resultType.hasStaticShape())
|
if (!resultType || !resultType.hasStaticShape())
|
||||||
return failure();
|
return failure();
|
||||||
int64_t sliceSize = resultType.getShape()[axis];
|
int64_t sliceSize = resultType.getShape()[axis];
|
||||||
auto computeOp =
|
outputs.push_back(extractSliceAt(adaptor.getInput(), axis, offset, sliceSize, rewriter, splitOp.getLoc()));
|
||||||
createSpatCompute<1>(rewriter, splitOp.getLoc(), TypeRange {resultType}, {}, adaptor.getInput(), [&](Value x) {
|
|
||||||
Value output = extractSliceAt(x, axis, offset, sliceSize, rewriter, splitOp.getLoc());
|
|
||||||
spatial::SpatYieldOp::create(rewriter, splitOp.getLoc(), output);
|
|
||||||
});
|
|
||||||
outputs.push_back(computeOp.getResult(0));
|
|
||||||
offset += sliceSize;
|
offset += sliceSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ add_public_tablegen_target(SpatialToPimIncGen)
|
|||||||
add_pim_library(OMSpatialToPim
|
add_pim_library(OMSpatialToPim
|
||||||
SpatialToPimPass.cpp
|
SpatialToPimPass.cpp
|
||||||
Common.cpp
|
Common.cpp
|
||||||
|
Patterns.cpp
|
||||||
|
|
||||||
EXCLUDE_FROM_OM_LIBS
|
EXCLUDE_FROM_OM_LIBS
|
||||||
|
|
||||||
|
|||||||
@@ -7,23 +7,12 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
#include "Common.hpp"
|
#include "Common.hpp"
|
||||||
#include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp"
|
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
using namespace mlir;
|
using namespace mlir;
|
||||||
|
|
||||||
namespace onnx_mlir {
|
namespace onnx_mlir {
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
IntegerAttr getRequiredI32Attr(Builder& builder, Operation* op, llvm::StringRef attrName) {
|
|
||||||
auto attr = op->getAttrOfType<IntegerAttr>(attrName);
|
|
||||||
assert(attr && "required precomputed channel attr is missing");
|
|
||||||
return IntegerAttr::get(builder.getI32Type(), attr.getInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
size_t getSliceActualOffset(tensor::ExtractSliceOp& sliceOp, ShapedType& inputShape) {
|
size_t getSliceActualOffset(tensor::ExtractSliceOp& sliceOp, ShapedType& inputShape) {
|
||||||
/*
|
/*
|
||||||
EXAMPLE RUN:
|
EXAMPLE RUN:
|
||||||
@@ -74,37 +63,6 @@ IntegerAttr getTensorSizeInBytesAttr(Builder& builder, mlir::Value value) {
|
|||||||
return builder.getI32IntegerAttr(static_cast<int32_t>(getShapedTypeSizeInBytes(cast<ShapedType>(value.getType()))));
|
return builder.getI32IntegerAttr(static_cast<int32_t>(getShapedTypeSizeInBytes(cast<ShapedType>(value.getType()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
IntegerAttr getSpatialChannelSourceCoreIdAttr(Builder& builder, mlir::Value channel) {
|
|
||||||
auto channelNewOp = channel.getDefiningOp<spatial::SpatChannelNewOp>();
|
|
||||||
assert(channelNewOp && "spatial channel value must come from spat.channel_new");
|
|
||||||
return getRequiredI32Attr(builder, channelNewOp, kChannelSourceCoreIdAttrName);
|
|
||||||
}
|
|
||||||
|
|
||||||
IntegerAttr getSpatialChannelTargetCoreIdAttr(Builder& builder, mlir::Value channel) {
|
|
||||||
auto channelNewOp = channel.getDefiningOp<spatial::SpatChannelNewOp>();
|
|
||||||
assert(channelNewOp && "spatial channel value must come from spat.channel_new");
|
|
||||||
return getRequiredI32Attr(builder, channelNewOp, kChannelTargetCoreIdAttrName);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool hasSpatialChannelSourceCoreIdAttr(mlir::Value channel) {
|
|
||||||
auto channelNewOp = channel.getDefiningOp<spatial::SpatChannelNewOp>();
|
|
||||||
return channelNewOp && channelNewOp->hasAttr(kChannelSourceCoreIdAttrName);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool hasSpatialChannelTargetCoreIdAttr(mlir::Value channel) {
|
|
||||||
auto channelNewOp = channel.getDefiningOp<spatial::SpatChannelNewOp>();
|
|
||||||
return channelNewOp && channelNewOp->hasAttr(kChannelTargetCoreIdAttrName);
|
|
||||||
}
|
|
||||||
|
|
||||||
mlir::Value
|
|
||||||
createPimReceiveFromSpatialChannel(PatternRewriter& rewriter, Location loc, mlir::Value output, mlir::Value channel) {
|
|
||||||
mlir::Value outputBuffer = getBestOutputTensorFromOperandsOrAllocate(rewriter, output.getDefiningOp());
|
|
||||||
auto sizeAttr = getTensorSizeInBytesAttr(rewriter, output);
|
|
||||||
auto sourceCoreIdAttr = getSpatialChannelSourceCoreIdAttr(rewriter, channel);
|
|
||||||
return pim::PimReceiveOp::create(rewriter, loc, outputBuffer.getType(), outputBuffer, sizeAttr, sourceCoreIdAttr)
|
|
||||||
.getOutput();
|
|
||||||
}
|
|
||||||
|
|
||||||
Operation* getEarliestUserWithinBlock(mlir::Value value) {
|
Operation* getEarliestUserWithinBlock(mlir::Value value) {
|
||||||
auto users = value.getUsers();
|
auto users = value.getUsers();
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,10 @@
|
|||||||
|
|
||||||
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
||||||
|
|
||||||
#include "llvm/ADT/StringRef.h"
|
|
||||||
|
|
||||||
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
|
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
|
||||||
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
|
||||||
|
|
||||||
namespace onnx_mlir {
|
namespace onnx_mlir {
|
||||||
|
|
||||||
inline constexpr llvm::StringLiteral kChannelSourceCoreIdAttrName = "precomp_source_core_id";
|
|
||||||
inline constexpr llvm::StringLiteral kChannelTargetCoreIdAttrName = "precomp_target_core_id";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Get the offset of the ExtractSliceOp based on its static offsets and
|
* \brief Get the offset of the ExtractSliceOp based on its static offsets and
|
||||||
* its static tensor input.
|
* its static tensor input.
|
||||||
@@ -30,17 +24,6 @@ size_t getShapedTypeSizeInBytes(mlir::ShapedType shapedType);
|
|||||||
|
|
||||||
mlir::IntegerAttr getTensorSizeInBytesAttr(mlir::Builder& builder, mlir::Value value);
|
mlir::IntegerAttr getTensorSizeInBytesAttr(mlir::Builder& builder, mlir::Value value);
|
||||||
|
|
||||||
mlir::IntegerAttr getSpatialChannelSourceCoreIdAttr(mlir::Builder& builder, mlir::Value channel);
|
|
||||||
|
|
||||||
mlir::IntegerAttr getSpatialChannelTargetCoreIdAttr(mlir::Builder& builder, mlir::Value channel);
|
|
||||||
|
|
||||||
bool hasSpatialChannelSourceCoreIdAttr(mlir::Value channel);
|
|
||||||
|
|
||||||
bool hasSpatialChannelTargetCoreIdAttr(mlir::Value channel);
|
|
||||||
|
|
||||||
mlir::Value createPimReceiveFromSpatialChannel(
|
|
||||||
mlir::PatternRewriter& rewriter, mlir::Location loc, mlir::Value output, mlir::Value channel);
|
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
size_t rangeLength(const mlir::iterator_range<T> range) {
|
size_t rangeLength(const mlir::iterator_range<T> range) {
|
||||||
return std::distance(range.begin(), range.end());
|
return std::distance(range.begin(), range.end());
|
||||||
|
|||||||
385
src/PIM/Conversion/SpatialToPim/Patterns.cpp
Normal file
385
src/PIM/Conversion/SpatialToPim/Patterns.cpp
Normal file
@@ -0,0 +1,385 @@
|
|||||||
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
||||||
|
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
|
||||||
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
||||||
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
||||||
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
||||||
|
#include "mlir/IR/BuiltinOps.h"
|
||||||
|
#include "mlir/IR/BuiltinTypes.h"
|
||||||
|
#include "mlir/IR/PatternMatch.h"
|
||||||
|
#include "mlir/IR/Value.h"
|
||||||
|
#include "mlir/Support/LLVM.h"
|
||||||
|
|
||||||
|
#include "llvm/ADT/DenseMap.h"
|
||||||
|
#include "llvm/ADT/STLExtras.h"
|
||||||
|
#include "llvm/Support/Casting.h"
|
||||||
|
#include "llvm/Support/ErrorHandling.h"
|
||||||
|
#include "llvm/Support/LogicalResult.h"
|
||||||
|
|
||||||
|
#include "Common/PimCommon.hpp"
|
||||||
|
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
||||||
|
|
||||||
|
using namespace mlir;
|
||||||
|
|
||||||
|
namespace onnx_mlir {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct MoveExtractSliceIntoCompute final : OpRewritePattern<mlir::tensor::ExtractSliceOp> {
|
||||||
|
using OpRewritePattern::OpRewritePattern;
|
||||||
|
|
||||||
|
LogicalResult matchAndRewrite(mlir::tensor::ExtractSliceOp extractSliceOp, PatternRewriter& rewriter) const override {
|
||||||
|
Location loc = extractSliceOp.getLoc();
|
||||||
|
|
||||||
|
if (!isa<func::FuncOp>(extractSliceOp->getParentOp()))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
for (auto& uses : extractSliceOp->getUses()) {
|
||||||
|
if (isa<spatial::SpatCompute>(uses.getOwner())) {
|
||||||
|
auto spatCompute = cast<spatial::SpatCompute>(uses.getOwner());
|
||||||
|
if (spatCompute.getInputs().empty())
|
||||||
|
return failure();
|
||||||
|
if (uses.getOperandNumber() < spatCompute.getInputs().getBeginOperandIndex())
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
else if (isa_and_present<func::FuncOp>(uses.getOwner()->getParentOp())) {
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::DenseMap<Operation*, Value> mapSpatToExtract;
|
||||||
|
|
||||||
|
for (auto& uses : llvm::make_early_inc_range(extractSliceOp->getUses())) {
|
||||||
|
|
||||||
|
if (auto spatCompute = dyn_cast<spatial::SpatCompute>(uses.getOwner())) {
|
||||||
|
auto BBArgIndex = uses.getOperandNumber() - spatCompute.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatCompute.getBody().front().getArgument(BBArgIndex);
|
||||||
|
|
||||||
|
if (BBArgValue.use_empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
rewriter.setInsertionPoint(&spatCompute.getBody().front().front());
|
||||||
|
if (!mapSpatToExtract.contains(spatCompute.getOperation())) {
|
||||||
|
auto newExtractSlice = rewriter.clone(*extractSliceOp.getOperation());
|
||||||
|
mapSpatToExtract.insert({spatCompute.getOperation(), newExtractSlice->getResult(0)});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatCompute.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(mapSpatToExtract[spatCompute.getOperation()]);
|
||||||
|
spatCompute.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatCompute.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatCompute.getOperation());
|
||||||
|
}
|
||||||
|
else if (auto spatComputeBatch = dyn_cast<spatial::SpatComputeBatch>(uses.getOwner())) {
|
||||||
|
auto BBArgIndex = uses.getOperandNumber() - spatComputeBatch.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatComputeBatch.getBody().front().getArgument(BBArgIndex);
|
||||||
|
|
||||||
|
if (BBArgValue.use_empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
rewriter.setInsertionPoint(&spatComputeBatch.getBody().front().front());
|
||||||
|
if (!mapSpatToExtract.contains(spatComputeBatch.getOperation())) {
|
||||||
|
auto newExtractSlice = rewriter.clone(*extractSliceOp.getOperation());
|
||||||
|
mapSpatToExtract.insert({spatComputeBatch.getOperation(), newExtractSlice->getResult(0)});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatComputeBatch.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(mapSpatToExtract[spatComputeBatch.getOperation()]);
|
||||||
|
spatComputeBatch.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatComputeBatch.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatComputeBatch.getOperation());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
{
|
||||||
|
if (auto spatCompute = uses.getOwner()->getParentOfType<spatial::SpatCompute>()) {
|
||||||
|
rewriter.setInsertionPoint(&spatCompute.getBody().front().front());
|
||||||
|
if (!mapSpatToExtract.contains(spatCompute.getOperation())) {
|
||||||
|
auto newExtractSlice = rewriter.clone(*extractSliceOp.getOperation());
|
||||||
|
mapSpatToExtract.insert({spatCompute.getOperation(), newExtractSlice->getResult(0)});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatCompute.getOperation());
|
||||||
|
uses.set(mapSpatToExtract[spatCompute.getOperation()]);
|
||||||
|
rewriter.finalizeOpModification(spatCompute.getOperation());
|
||||||
|
}
|
||||||
|
else if (auto spatComputeBatch = uses.getOwner()->getParentOfType<spatial::SpatComputeBatch>()) {
|
||||||
|
rewriter.setInsertionPoint(&spatComputeBatch.getBody().front().front());
|
||||||
|
if (!mapSpatToExtract.contains(spatComputeBatch.getOperation())) {
|
||||||
|
auto newExtractSlice = rewriter.clone(*extractSliceOp.getOperation());
|
||||||
|
mapSpatToExtract.insert({spatComputeBatch.getOperation(), newExtractSlice->getResult(0)});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatComputeBatch.getOperation());
|
||||||
|
uses.set(mapSpatToExtract[spatComputeBatch.getOperation()]);
|
||||||
|
rewriter.finalizeOpModification(spatComputeBatch.getOperation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.eraseOp(extractSliceOp);
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ArithConstToGlobalMemoryPattern final : OpRewritePattern<mlir::arith::ConstantOp> {
|
||||||
|
using OpRewritePattern::OpRewritePattern;
|
||||||
|
|
||||||
|
LogicalResult matchAndRewrite(mlir::arith::ConstantOp constantOp, PatternRewriter& rewriter) const override {
|
||||||
|
static int i = 0;
|
||||||
|
Location loc = constantOp.getLoc();
|
||||||
|
|
||||||
|
if (hasWeightAlways(constantOp))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
if (!isa<func::FuncOp>(constantOp->getParentOp()))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
if (llvm::all_of(constantOp->getUsers(), [](Operation* op) {
|
||||||
|
if (isa<spatial::SpatCompute>(op))
|
||||||
|
return false;
|
||||||
|
if (isa<func::FuncOp>(op->getParentOp()))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
rewriter.setInsertionPoint(constantOp->getParentOfType<func::FuncOp>());
|
||||||
|
|
||||||
|
auto constRankedTensorType = llvm::dyn_cast<mlir::RankedTensorType>(constantOp.getType());
|
||||||
|
|
||||||
|
if (constRankedTensorType) {
|
||||||
|
mlir::MemRefType memRefType =
|
||||||
|
mlir::MemRefType::get(constRankedTensorType.getShape(), constRankedTensorType.getElementType());
|
||||||
|
std::string argName = "const_" + std::to_string(i++);
|
||||||
|
memref::GlobalOp::create(rewriter,
|
||||||
|
loc,
|
||||||
|
rewriter.getStringAttr(argName),
|
||||||
|
rewriter.getStringAttr("private"),
|
||||||
|
TypeAttr::get(memRefType),
|
||||||
|
constantOp.getValueAttr(),
|
||||||
|
rewriter.getUnitAttr(),
|
||||||
|
{});
|
||||||
|
|
||||||
|
llvm::DenseMap<Operation*, Value> mapSpatComputeToConst;
|
||||||
|
|
||||||
|
for (auto& constUses : llvm::make_early_inc_range(constantOp->getUses())) {
|
||||||
|
auto constUsers = constUses.getOwner();
|
||||||
|
|
||||||
|
if (auto spatCompute = llvm::dyn_cast<spatial::SpatCompute>(constUsers)) {
|
||||||
|
|
||||||
|
auto BBArgIndex = constUses.getOperandNumber() - spatCompute.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatCompute.getBody().front().getArgument(BBArgIndex);
|
||||||
|
rewriter.setInsertionPoint(&spatCompute.getBody().front().front());
|
||||||
|
if (!mapSpatComputeToConst.contains(spatCompute.getOperation())) {
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, loc, memRefType, argName);
|
||||||
|
auto toTensor = bufferization::ToTensorOp::create(
|
||||||
|
rewriter, loc, constRankedTensorType, getGlobalOp, rewriter.getUnitAttr(), rewriter.getUnitAttr());
|
||||||
|
mapSpatComputeToConst.insert({spatCompute.getOperation(), toTensor.getResult()});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatCompute.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(mapSpatComputeToConst[spatCompute.getOperation()]);
|
||||||
|
spatCompute.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatCompute.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatCompute.getOperation());
|
||||||
|
}
|
||||||
|
else if (auto spatComputeBatch = llvm::dyn_cast<spatial::SpatComputeBatch>(constUsers)) {
|
||||||
|
|
||||||
|
auto BBArgIndex = constUses.getOperandNumber() - spatComputeBatch.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatComputeBatch.getBody().front().getArgument(BBArgIndex);
|
||||||
|
rewriter.setInsertionPoint(&spatComputeBatch.getBody().front().front());
|
||||||
|
if (!mapSpatComputeToConst.contains(spatComputeBatch.getOperation())) {
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, loc, memRefType, argName);
|
||||||
|
auto toTensor = bufferization::ToTensorOp::create(
|
||||||
|
rewriter, loc, constRankedTensorType, getGlobalOp, rewriter.getUnitAttr(), rewriter.getUnitAttr());
|
||||||
|
mapSpatComputeToConst.insert({spatComputeBatch.getOperation(), toTensor.getResult()});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatComputeBatch.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(mapSpatComputeToConst[spatComputeBatch.getOperation()]);
|
||||||
|
spatComputeBatch.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatComputeBatch.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatComputeBatch.getOperation());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
{
|
||||||
|
|
||||||
|
if (auto spatCompute = constUses.getOwner()->getParentOfType<spatial::SpatCompute>()) {
|
||||||
|
rewriter.setInsertionPoint(&spatCompute.getBody().front().front());
|
||||||
|
if (!mapSpatComputeToConst.contains(spatCompute.getOperation())) {
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, loc, memRefType, argName);
|
||||||
|
auto toTensor = bufferization::ToTensorOp::create(
|
||||||
|
rewriter, loc, constRankedTensorType, getGlobalOp, rewriter.getUnitAttr(), rewriter.getUnitAttr());
|
||||||
|
mapSpatComputeToConst.insert({spatCompute.getOperation(), toTensor.getResult()});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatCompute.getOperation());
|
||||||
|
constUses.set(mapSpatComputeToConst[spatCompute.getOperation()]);
|
||||||
|
rewriter.finalizeOpModification(spatCompute.getOperation());
|
||||||
|
}
|
||||||
|
else if (auto spatComputeBatch = constUses.getOwner()->getParentOfType<spatial::SpatComputeBatch>()) {
|
||||||
|
rewriter.setInsertionPoint(&spatComputeBatch.getBody().front().front());
|
||||||
|
if (!mapSpatComputeToConst.contains(spatComputeBatch.getOperation())) {
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, loc, memRefType, argName);
|
||||||
|
auto toTensor = bufferization::ToTensorOp::create(
|
||||||
|
rewriter, loc, constRankedTensorType, getGlobalOp, rewriter.getUnitAttr(), rewriter.getUnitAttr());
|
||||||
|
mapSpatComputeToConst.insert({spatComputeBatch.getOperation(), toTensor.getResult()});
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatComputeBatch.getOperation());
|
||||||
|
constUses.set(mapSpatComputeToConst[spatComputeBatch.getOperation()]);
|
||||||
|
rewriter.finalizeOpModification(spatComputeBatch.getOperation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (constantOp.getType().isIntOrIndexOrFloat()) {
|
||||||
|
llvm::DenseMap<Operation*, Value> mapSpatComputeToConst;
|
||||||
|
|
||||||
|
for (auto& constUses : llvm::make_early_inc_range(constantOp->getUses())) {
|
||||||
|
auto constUsers = constUses.getOwner();
|
||||||
|
|
||||||
|
if (auto spatCompute = llvm::dyn_cast<spatial::SpatCompute>(constUsers)) {
|
||||||
|
|
||||||
|
auto BBArgIndex = constUses.getOperandNumber() - spatCompute.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatCompute.getBody().front().getArgument(BBArgIndex);
|
||||||
|
rewriter.setInsertionPoint(&spatCompute.getBody().front().front());
|
||||||
|
auto newConst = rewriter.clone(*constantOp);
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatCompute.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(newConst->getResult(0));
|
||||||
|
spatCompute.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatCompute.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatCompute.getOperation());
|
||||||
|
}
|
||||||
|
else if (auto spatComputeBatch = llvm::dyn_cast<spatial::SpatComputeBatch>(constUsers)) {
|
||||||
|
|
||||||
|
auto BBArgIndex = constUses.getOperandNumber() - spatComputeBatch.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatComputeBatch.getBody().front().getArgument(BBArgIndex);
|
||||||
|
rewriter.setInsertionPoint(&spatComputeBatch.getBody().front().front());
|
||||||
|
auto newConst = rewriter.clone(*constantOp);
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatComputeBatch.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(newConst->getResult(0));
|
||||||
|
spatComputeBatch.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatComputeBatch.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatComputeBatch.getOperation());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (auto parent = constUsers->getParentOfType<spatial::SpatCompute>()) {
|
||||||
|
if (!mapSpatComputeToConst.contains(parent)) {
|
||||||
|
rewriter.setInsertionPoint(&parent.getBody().front().front());
|
||||||
|
auto newConst = rewriter.clone(*constantOp);
|
||||||
|
mapSpatComputeToConst.insert({parent.getOperation(), newConst->getResult(0)});
|
||||||
|
}
|
||||||
|
constUses.set(mapSpatComputeToConst[parent.getOperation()]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
auto batchParent = constUsers->getParentOfType<spatial::SpatComputeBatch>();
|
||||||
|
assert(batchParent && "Global Constant used direcly not within a compute");
|
||||||
|
if (!mapSpatComputeToConst.contains(batchParent.getOperation())) {
|
||||||
|
rewriter.setInsertionPoint(&batchParent.getBody().front().front());
|
||||||
|
auto newConst = rewriter.clone(*constantOp);
|
||||||
|
mapSpatComputeToConst.insert({batchParent.getOperation(), newConst->getResult(0)});
|
||||||
|
}
|
||||||
|
constUses.set(mapSpatComputeToConst[batchParent.getOperation()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto parent = constantOp->getParentOp();
|
||||||
|
rewriter.eraseOp(constantOp);
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FuncOpArgToGlobalMemoryPattern final : OpRewritePattern<mlir::func::FuncOp> {
|
||||||
|
using OpRewritePattern::OpRewritePattern;
|
||||||
|
|
||||||
|
LogicalResult matchAndRewrite(mlir::func::FuncOp funcOp, PatternRewriter& rewriter) const override {
|
||||||
|
|
||||||
|
if (funcOp.getArguments().empty())
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
if (llvm::all_of(funcOp.getArguments(),
|
||||||
|
[](mlir::BlockArgument blockArgument) { return blockArgument.use_empty(); }))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
Location loc = funcOp.getLoc();
|
||||||
|
|
||||||
|
for (auto [index, arg] : llvm::enumerate(funcOp.getArguments())) {
|
||||||
|
if (arg.getUses().empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
rewriter.setInsertionPoint(funcOp.getOperation());
|
||||||
|
|
||||||
|
assert(isa<mlir::RankedTensorType>(arg.getType()));
|
||||||
|
|
||||||
|
auto argRankedTensorType = llvm::dyn_cast<mlir::RankedTensorType>(arg.getType());
|
||||||
|
mlir::MemRefType memRefType =
|
||||||
|
mlir::MemRefType::get(argRankedTensorType.getShape(), argRankedTensorType.getElementType());
|
||||||
|
|
||||||
|
std::string argName = "arg_" + std::to_string(index);
|
||||||
|
|
||||||
|
memref::GlobalOp::create(rewriter,
|
||||||
|
loc,
|
||||||
|
rewriter.getStringAttr(argName),
|
||||||
|
rewriter.getStringAttr("private"),
|
||||||
|
TypeAttr::get(memRefType),
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
{});
|
||||||
|
|
||||||
|
for (auto& argUses : llvm::make_early_inc_range(arg.getUses())) {
|
||||||
|
auto argUser = argUses.getOwner();
|
||||||
|
if (auto spatCompute = dyn_cast<spatial::SpatCompute>(argUser)) {
|
||||||
|
auto BBArgIndex = argUses.getOperandNumber() - spatCompute.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatCompute.getBody().front().getArgument(BBArgIndex);
|
||||||
|
rewriter.setInsertionPoint(&spatCompute.getBody().front().front());
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, loc, memRefType, argName);
|
||||||
|
auto toTensor = bufferization::ToTensorOp::create(
|
||||||
|
rewriter, loc, argRankedTensorType, getGlobalOp, rewriter.getUnitAttr(), rewriter.getUnitAttr());
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatCompute.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(toTensor);
|
||||||
|
spatCompute.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatCompute.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatCompute.getOperation());
|
||||||
|
}
|
||||||
|
else if (auto spatComputeBatch = dyn_cast<spatial::SpatComputeBatch>(argUser)) {
|
||||||
|
auto BBArgIndex = argUses.getOperandNumber() - spatComputeBatch.getInputs().getBeginOperandIndex();
|
||||||
|
auto BBArgValue = spatComputeBatch.getBody().front().getArgument(BBArgIndex);
|
||||||
|
rewriter.setInsertionPoint(&spatComputeBatch.getBody().front().front());
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, loc, memRefType, argName);
|
||||||
|
auto toTensor = bufferization::ToTensorOp::create(
|
||||||
|
rewriter, loc, argRankedTensorType, getGlobalOp, rewriter.getUnitAttr(), rewriter.getUnitAttr());
|
||||||
|
|
||||||
|
rewriter.startOpModification(spatComputeBatch.getOperation());
|
||||||
|
BBArgValue.replaceAllUsesWith(toTensor);
|
||||||
|
spatComputeBatch.getInputsMutable().erase(BBArgIndex);
|
||||||
|
spatComputeBatch.getBody().front().eraseArgument(BBArgIndex);
|
||||||
|
rewriter.finalizeOpModification(spatComputeBatch.getOperation());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rewriter.setInsertionPoint(argUser);
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, loc, memRefType, argName);
|
||||||
|
auto toTensor = bufferization::ToTensorOp::create(
|
||||||
|
rewriter, loc, argRankedTensorType, getGlobalOp, rewriter.getUnitAttr(), rewriter.getUnitAttr());
|
||||||
|
rewriter.startOpModification(argUser);
|
||||||
|
argUses.set(toTensor);
|
||||||
|
rewriter.finalizeOpModification(argUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
void populateGlobalTensorToMemrefPatterns(RewritePatternSet& patterns) {
|
||||||
|
patterns.add<MoveExtractSliceIntoCompute, FuncOpArgToGlobalMemoryPattern, ArithConstToGlobalMemoryPattern>(
|
||||||
|
patterns.getContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace onnx_mlir
|
||||||
10
src/PIM/Conversion/SpatialToPim/Patterns.hpp
Normal file
10
src/PIM/Conversion/SpatialToPim/Patterns.hpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "mlir/IR/PatternMatch.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace onnx_mlir {
|
||||||
|
|
||||||
|
void populateGlobalTensorToMemrefPatterns(mlir::RewritePatternSet& patterns);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,17 +9,6 @@ include "src/Accelerators/PIM/Dialect/Spatial/Spatial.td"
|
|||||||
include "src/Accelerators/PIM/Dialect/Pim/Pim.td"
|
include "src/Accelerators/PIM/Dialect/Pim/Pim.td"
|
||||||
#endif // OP_BASE
|
#endif // OP_BASE
|
||||||
|
|
||||||
def HasSpatialChannelSourceCoreIdAttr: Constraint<
|
|
||||||
CPred<"onnx_mlir::hasSpatialChannelSourceCoreIdAttr($0)">,
|
|
||||||
"spatial channel has precomputed source core id">;
|
|
||||||
|
|
||||||
def HasSpatialChannelTargetCoreIdAttr: Constraint<
|
|
||||||
CPred<"onnx_mlir::hasSpatialChannelTargetCoreIdAttr($0)">,
|
|
||||||
"spatial channel has precomputed target core id">;
|
|
||||||
|
|
||||||
def createPimReceiveFromSpatialChannelValue: NativeCodeCall<
|
|
||||||
"onnx_mlir::createPimReceiveFromSpatialChannel($_builder, $_loc, $0, $1)">;
|
|
||||||
|
|
||||||
def onnxToPimTranspose : Pat<
|
def onnxToPimTranspose : Pat<
|
||||||
(ONNXTransposeOp:$srcOpRes $data, $perms),
|
(ONNXTransposeOp:$srcOpRes $data, $perms),
|
||||||
(PimTransposeOp $data, $perms,
|
(PimTransposeOp $data, $perms,
|
||||||
@@ -80,18 +69,4 @@ def spatToPimVSoftmax : Pat<
|
|||||||
(NativeCodeCall<"onnx_mlir::getBestOutputTensorFromOperandsOrAllocate($_builder, $0.getDefiningOp())"> $srcOpRes))
|
(NativeCodeCall<"onnx_mlir::getBestOutputTensorFromOperandsOrAllocate($_builder, $0.getDefiningOp())"> $srcOpRes))
|
||||||
>;
|
>;
|
||||||
|
|
||||||
def spatChannelSendToPimSend : Pat<
|
|
||||||
(SpatChannelSendOp $channel, $input),
|
|
||||||
(PimSendOp $input,
|
|
||||||
(NativeCodeCall<"onnx_mlir::getTensorSizeInBytesAttr($_builder, $0)"> $input),
|
|
||||||
(NativeCodeCall<"onnx_mlir::getSpatialChannelTargetCoreIdAttr($_builder, $0)"> $channel)),
|
|
||||||
[(HasSpatialChannelTargetCoreIdAttr $channel)]
|
|
||||||
>;
|
|
||||||
|
|
||||||
def spatChannelReceiveToPimReceive : Pat<
|
|
||||||
(SpatChannelReceiveOp:$srcOpRes $channel),
|
|
||||||
(createPimReceiveFromSpatialChannelValue $srcOpRes, $channel),
|
|
||||||
[(HasSpatialChannelSourceCoreIdAttr $channel)]
|
|
||||||
>;
|
|
||||||
|
|
||||||
#endif // SPATIAL_TO_PIM
|
#endif // SPATIAL_TO_PIM
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -24,7 +24,7 @@ def PimTensor :
|
|||||||
// Execution
|
// Execution
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
def PimCoreOp : PimOp<"core", [SingleBlock]> {
|
def PimCoreOp : PimOp<"core", [SingleBlock, IsolatedFromAbove]> {
|
||||||
let summary = "Execute a block on a PIM core";
|
let summary = "Execute a block on a PIM core";
|
||||||
|
|
||||||
let regions = (region SizedRegion<1>:$body);
|
let regions = (region SizedRegion<1>:$body);
|
||||||
@@ -39,6 +39,22 @@ def PimCoreOp : PimOp<"core", [SingleBlock]> {
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def PimCoreBatchOp : PimOp<"core_batch", [SingleBlock, AttrSizedOperandSegments]> {
|
||||||
|
let summary = "Execute equivalent batched core bodies";
|
||||||
|
|
||||||
|
let regions = (region SizedRegion<1>:$body);
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
I32Attr:$laneCount,
|
||||||
|
Variadic<PimTensor>:$weights,
|
||||||
|
Variadic<PimTensor>:$inputs
|
||||||
|
);
|
||||||
|
|
||||||
|
let assemblyFormat = [{
|
||||||
|
`lanes` $laneCount `(` $weights `)` `[` $inputs `]` attr-dict regions `:` type($weights) `[` type($inputs) `]` `->` `(` `)`
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
def PimHaltOp : PimOp<"halt", [Terminator]> {
|
def PimHaltOp : PimOp<"halt", [Terminator]> {
|
||||||
let summary = "Halt execution of the core";
|
let summary = "Halt execution of the core";
|
||||||
|
|
||||||
@@ -65,6 +81,20 @@ def PimSendOp : PimOp<"send", []> {
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def PimSendBatchOp : PimOp<"send_batch", []> {
|
||||||
|
let summary = "Send a per-lane tensor to target cores from a batched core";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
PimTensor:$input,
|
||||||
|
I32Attr:$size,
|
||||||
|
DenseI32ArrayAttr:$targetCoreIds
|
||||||
|
);
|
||||||
|
|
||||||
|
let assemblyFormat = [{
|
||||||
|
`(` $input `)` attr-dict `:` type($input) `->` `(` `)`
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
def PimReceiveOp : PimOp<"receive", [DestinationStyleOpInterface]> {
|
def PimReceiveOp : PimOp<"receive", [DestinationStyleOpInterface]> {
|
||||||
let summary = "Receive a tensor from another core";
|
let summary = "Receive a tensor from another core";
|
||||||
|
|
||||||
@@ -89,6 +119,30 @@ def PimReceiveOp : PimOp<"receive", [DestinationStyleOpInterface]> {
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def PimReceiveBatchOp : PimOp<"receive_batch", [DestinationStyleOpInterface]> {
|
||||||
|
let summary = "Receive per-lane tensors from source cores into a batched core";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
PimTensor:$outputBuffer,
|
||||||
|
I32Attr:$size,
|
||||||
|
DenseI32ArrayAttr:$sourceCoreIds
|
||||||
|
);
|
||||||
|
|
||||||
|
let results = (outs
|
||||||
|
PimTensor:$output
|
||||||
|
);
|
||||||
|
|
||||||
|
let extraClassDeclaration = [{
|
||||||
|
mlir::MutableOperandRange getDpsInitsMutable() {
|
||||||
|
return getOutputBufferMutable();
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
|
||||||
|
let assemblyFormat = [{
|
||||||
|
`(` $outputBuffer `)` attr-dict `:` type($outputBuffer) `->` type($output)
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
def PimMemCopyHostToDevOp : PimOp<"memcp_hd", [DestinationStyleOpInterface]> {
|
def PimMemCopyHostToDevOp : PimOp<"memcp_hd", [DestinationStyleOpInterface]> {
|
||||||
let summary = "Copy a memory region from host memory into device memory";
|
let summary = "Copy a memory region from host memory into device memory";
|
||||||
|
|
||||||
@@ -115,6 +169,32 @@ def PimMemCopyHostToDevOp : PimOp<"memcp_hd", [DestinationStyleOpInterface]> {
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def PimMemCopyHostToDevBatchOp : PimOp<"memcp_hd_batch", [DestinationStyleOpInterface]> {
|
||||||
|
let summary = "Copy a per-lane tensor from host memory into device memory inside a batched core";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
PimTensor:$deviceTarget,
|
||||||
|
PimTensor:$hostSource,
|
||||||
|
I32Attr:$deviceTargetOffset,
|
||||||
|
I32Attr:$hostSourceOffset,
|
||||||
|
I32Attr:$size
|
||||||
|
);
|
||||||
|
|
||||||
|
let results = (outs
|
||||||
|
PimTensor:$output
|
||||||
|
);
|
||||||
|
|
||||||
|
let extraClassDeclaration = [{
|
||||||
|
mlir::MutableOperandRange getDpsInitsMutable() {
|
||||||
|
return getDeviceTargetMutable();
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
|
||||||
|
let assemblyFormat = [{
|
||||||
|
`(` $deviceTarget `,` $hostSource `)` attr-dict `:` `(` type($deviceTarget) `,` type($hostSource) `)` `->` type($output)
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
def PimMemCopyDevToHostOp : PimOp<"memcp_dh", [DestinationStyleOpInterface]> {
|
def PimMemCopyDevToHostOp : PimOp<"memcp_dh", [DestinationStyleOpInterface]> {
|
||||||
let summary = "Copy a memory region from device memory into host memory";
|
let summary = "Copy a memory region from device memory into host memory";
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
|
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
|
||||||
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
|
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
|
||||||
#include "mlir/Dialect/Bufferization/IR/DstBufferizableOpInterfaceImpl.h"
|
#include "mlir/Dialect/Bufferization/IR/DstBufferizableOpInterfaceImpl.h"
|
||||||
|
#include "mlir/Dialect/Bufferization/Transforms/Bufferize.h"
|
||||||
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
||||||
|
|
||||||
#include "OpBufferizationInterfaces.hpp"
|
#include "OpBufferizationInterfaces.hpp"
|
||||||
@@ -65,6 +66,32 @@ struct MemCopyHostToDevOpInterface
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct MemCopyHostToDevBatchOpInterface
|
||||||
|
: DstBufferizableOpInterfaceExternalModel<MemCopyHostToDevBatchOpInterface, PimMemCopyHostToDevBatchOp> {
|
||||||
|
LogicalResult bufferize(Operation* op,
|
||||||
|
RewriterBase& rewriter,
|
||||||
|
const BufferizationOptions& options,
|
||||||
|
BufferizationState& state) const {
|
||||||
|
auto memCopyHostToDevOp = cast<PimMemCopyHostToDevBatchOp>(op);
|
||||||
|
auto deviceTargetOpt = getBuffer(rewriter, memCopyHostToDevOp.getDeviceTarget(), options, state);
|
||||||
|
if (failed(deviceTargetOpt))
|
||||||
|
return failure();
|
||||||
|
auto hostSourceOpt = getBuffer(rewriter, memCopyHostToDevOp.getHostSource(), options, state);
|
||||||
|
if (failed(hostSourceOpt))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
replaceOpWithNewBufferizedOp<PimMemCopyHostToDevBatchOp>(rewriter,
|
||||||
|
memCopyHostToDevOp,
|
||||||
|
deviceTargetOpt->getType(),
|
||||||
|
*deviceTargetOpt,
|
||||||
|
*hostSourceOpt,
|
||||||
|
memCopyHostToDevOp.getDeviceTargetOffsetAttr(),
|
||||||
|
memCopyHostToDevOp.getHostSourceOffsetAttr(),
|
||||||
|
memCopyHostToDevOp.getSizeAttr());
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct MemCopyDevToHostOpInterface
|
struct MemCopyDevToHostOpInterface
|
||||||
: DstBufferizableOpInterfaceExternalModel<MemCopyDevToHostOpInterface, PimMemCopyDevToHostOp> {
|
: DstBufferizableOpInterfaceExternalModel<MemCopyDevToHostOpInterface, PimMemCopyDevToHostOp> {
|
||||||
LogicalResult bufferize(Operation* op,
|
LogicalResult bufferize(Operation* op,
|
||||||
@@ -122,6 +149,127 @@ struct ReceiveOpInterface : DstBufferizableOpInterfaceExternalModel<ReceiveOpInt
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ReceiveBatchOpInterface : DstBufferizableOpInterfaceExternalModel<ReceiveBatchOpInterface, PimReceiveBatchOp> {
|
||||||
|
bool bufferizesToMemoryRead(Operation* op, OpOperand& opOperand, const AnalysisState& state) const {
|
||||||
|
return !cast<DestinationStyleOpInterface>(op).isDpsInit(&opOperand);
|
||||||
|
}
|
||||||
|
|
||||||
|
LogicalResult bufferize(Operation* op,
|
||||||
|
RewriterBase& rewriter,
|
||||||
|
const BufferizationOptions& options,
|
||||||
|
BufferizationState& state) const {
|
||||||
|
auto receiveOp = cast<PimReceiveBatchOp>(op);
|
||||||
|
auto outputBufferOpt = getBuffer(rewriter, receiveOp.getOutputBuffer(), options, state);
|
||||||
|
if (failed(outputBufferOpt))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
replaceOpWithNewBufferizedOp<PimReceiveBatchOp>(rewriter,
|
||||||
|
op,
|
||||||
|
outputBufferOpt->getType(),
|
||||||
|
*outputBufferOpt,
|
||||||
|
receiveOp.getSizeAttr(),
|
||||||
|
receiveOp.getSourceCoreIdsAttr());
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CoreBatchOpInterface : BufferizableOpInterface::ExternalModel<CoreBatchOpInterface, PimCoreBatchOp> {
|
||||||
|
bool bufferizesToMemoryRead(Operation* op, OpOperand& opOperand, const AnalysisState& state) const {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bufferizesToMemoryWrite(Operation* op, OpOperand& opOperand, const AnalysisState& state) const {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
AliasingValueList getAliasingValues(Operation* op, OpOperand& opOperand, const AnalysisState& state) const {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
AliasingOpOperandList getAliasingOpOperands(Operation* op, Value value, const AnalysisState& state) const {
|
||||||
|
auto coreBatchOp = cast<PimCoreBatchOp>(op);
|
||||||
|
auto bbArg = dyn_cast<BlockArgument>(value);
|
||||||
|
if (!bbArg || bbArg.getOwner() != &coreBatchOp.getBody().front())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
unsigned inputOperandIndex = coreBatchOp.getWeights().size() + bbArg.getArgNumber();
|
||||||
|
return {{&coreBatchOp->getOpOperand(inputOperandIndex), BufferRelation::Equivalent}};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isWritable(Operation* op, Value value, const AnalysisState& state) const {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
FailureOr<BufferLikeType>
|
||||||
|
getBufferType(Operation* op,
|
||||||
|
Value value,
|
||||||
|
const BufferizationOptions& options,
|
||||||
|
const BufferizationState& state,
|
||||||
|
SmallVector<Value>& invocationStack) const {
|
||||||
|
auto coreBatchOp = cast<PimCoreBatchOp>(op);
|
||||||
|
auto bbArg = dyn_cast<BlockArgument>(value);
|
||||||
|
if (!bbArg || bbArg.getOwner() != &coreBatchOp.getBody().front())
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
Value tiedInput = coreBatchOp.getInputs()[bbArg.getArgNumber()];
|
||||||
|
if (auto memRefType = dyn_cast<BufferLikeType>(tiedInput.getType()))
|
||||||
|
return memRefType;
|
||||||
|
|
||||||
|
return bufferization::getBufferType(tiedInput, options, state, invocationStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
LogicalResult bufferize(Operation* op,
|
||||||
|
RewriterBase& rewriter,
|
||||||
|
const BufferizationOptions& options,
|
||||||
|
BufferizationState& state) const {
|
||||||
|
auto coreBatchOp = cast<PimCoreBatchOp>(op);
|
||||||
|
|
||||||
|
SmallVector<Value> weights;
|
||||||
|
SmallVector<Value> inputs;
|
||||||
|
weights.reserve(coreBatchOp.getWeights().size());
|
||||||
|
inputs.reserve(coreBatchOp.getInputs().size());
|
||||||
|
|
||||||
|
for (Value weight : coreBatchOp.getWeights()) {
|
||||||
|
if (isa<TensorType>(weight.getType())) {
|
||||||
|
auto weightOpt = getBuffer(rewriter, weight, options, state);
|
||||||
|
if (failed(weightOpt))
|
||||||
|
return failure();
|
||||||
|
weights.push_back(*weightOpt);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
weights.push_back(weight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Value input : coreBatchOp.getInputs()) {
|
||||||
|
if (isa<TensorType>(input.getType())) {
|
||||||
|
auto inputOpt = getBuffer(rewriter, input, options, state);
|
||||||
|
if (failed(inputOpt))
|
||||||
|
return failure();
|
||||||
|
inputs.push_back(*inputOpt);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
inputs.push_back(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rewriter.setInsertionPoint(coreBatchOp);
|
||||||
|
auto newOp = PimCoreBatchOp::create(
|
||||||
|
rewriter, coreBatchOp.getLoc(), coreBatchOp.getLaneCountAttr(), ValueRange(weights), ValueRange(inputs));
|
||||||
|
newOp.getProperties().setOperandSegmentSizes({static_cast<int>(weights.size()), static_cast<int>(inputs.size())});
|
||||||
|
if (auto coreIdsAttr = coreBatchOp->getAttr(onnx_mlir::kCoreIdAttrName))
|
||||||
|
newOp->setAttr(onnx_mlir::kCoreIdAttrName, coreIdsAttr);
|
||||||
|
|
||||||
|
rewriter.inlineRegionBefore(coreBatchOp.getBody(), newOp.getBody(), newOp.getBody().begin());
|
||||||
|
for (Block& block : newOp.getBody())
|
||||||
|
if (failed(bufferization::bufferizeBlockSignature(&block, rewriter, options, state)))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
rewriter.eraseOp(coreBatchOp);
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct TransposeOpInterface : DstBufferizableOpInterfaceExternalModel<TransposeOpInterface, PimTransposeOp> {
|
struct TransposeOpInterface : DstBufferizableOpInterfaceExternalModel<TransposeOpInterface, PimTransposeOp> {
|
||||||
bool bufferizesToMemoryRead(Operation* op, OpOperand& opOperand, const AnalysisState& state) const {
|
bool bufferizesToMemoryRead(Operation* op, OpOperand& opOperand, const AnalysisState& state) const {
|
||||||
return !cast<DestinationStyleOpInterface>(op).isDpsInit(&opOperand);
|
return !cast<DestinationStyleOpInterface>(op).isDpsInit(&opOperand);
|
||||||
@@ -287,8 +435,11 @@ struct UnaryDstOpInterface : DstBufferizableOpInterfaceExternalModel<UnaryDstOpI
|
|||||||
|
|
||||||
void registerOpBufferizationInterfaces(DialectRegistry& registry) {
|
void registerOpBufferizationInterfaces(DialectRegistry& registry) {
|
||||||
registry.addExtension(+[](MLIRContext* ctx, PimDialect* dialect) {
|
registry.addExtension(+[](MLIRContext* ctx, PimDialect* dialect) {
|
||||||
|
PimCoreBatchOp::attachInterface<CoreBatchOpInterface>(*ctx);
|
||||||
PimReceiveOp::attachInterface<ReceiveOpInterface>(*ctx);
|
PimReceiveOp::attachInterface<ReceiveOpInterface>(*ctx);
|
||||||
|
PimReceiveBatchOp::attachInterface<ReceiveBatchOpInterface>(*ctx);
|
||||||
PimMemCopyHostToDevOp::attachInterface<MemCopyHostToDevOpInterface>(*ctx);
|
PimMemCopyHostToDevOp::attachInterface<MemCopyHostToDevOpInterface>(*ctx);
|
||||||
|
PimMemCopyHostToDevBatchOp::attachInterface<MemCopyHostToDevBatchOpInterface>(*ctx);
|
||||||
PimMemCopyDevToHostOp::attachInterface<MemCopyDevToHostOpInterface>(*ctx);
|
PimMemCopyDevToHostOp::attachInterface<MemCopyDevToHostOpInterface>(*ctx);
|
||||||
PimTransposeOp::attachInterface<TransposeOpInterface>(*ctx);
|
PimTransposeOp::attachInterface<TransposeOpInterface>(*ctx);
|
||||||
PimVMMOp::attachInterface<VMMOpInterface>(*ctx);
|
PimVMMOp::attachInterface<VMMOpInterface>(*ctx);
|
||||||
|
|||||||
@@ -3,12 +3,17 @@
|
|||||||
#include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"
|
#include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"
|
||||||
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
||||||
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
||||||
|
#include "mlir/IR/Threading.h"
|
||||||
#include "mlir/Pass/Pass.h"
|
#include "mlir/Pass/Pass.h"
|
||||||
|
|
||||||
|
#include "llvm/Support/Casting.h"
|
||||||
|
#include "llvm/Support/Debug.h"
|
||||||
|
|
||||||
#include "Common/PimCommon.hpp"
|
#include "Common/PimCommon.hpp"
|
||||||
#include "Compiler/PimCodeGen.hpp"
|
#include "Compiler/PimCodeGen.hpp"
|
||||||
#include "Dialect/Pim/PimOps.hpp"
|
#include "Dialect/Pim/PimOps.hpp"
|
||||||
#include "Dialect/Pim/Transforms/Bufferization/Common.hpp"
|
#include "Dialect/Pim/Transforms/Bufferization/Common.hpp"
|
||||||
|
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
||||||
#include "src/Accelerators/PIM/Pass/PIMPasses.h"
|
#include "src/Accelerators/PIM/Pass/PIMPasses.h"
|
||||||
#include "src/Compiler/CompilerOptions.hpp"
|
#include "src/Compiler/CompilerOptions.hpp"
|
||||||
|
|
||||||
@@ -40,15 +45,45 @@ private:
|
|||||||
|
|
||||||
void PimBufferizationPass::runOnOperation() {
|
void PimBufferizationPass::runOnOperation() {
|
||||||
auto moduleOp = getOperation();
|
auto moduleOp = getOperation();
|
||||||
|
// Refactor this into a function
|
||||||
|
{
|
||||||
|
auto funcOp = getPimEntryFunc(moduleOp);
|
||||||
|
|
||||||
|
auto coreOps = llvm::to_vector(funcOp->getOps<pim::PimCoreOp>());
|
||||||
|
MLIRContext* ctx = moduleOp.getContext();
|
||||||
|
// failableParallelForEach will run the lambda in parallel and stop if any thread fails
|
||||||
|
LogicalResult result = mlir::failableParallelForEach(ctx, coreOps, [&](pim::PimCoreOp coreOp) {
|
||||||
|
// Again, allocate state LOCALLY per thread/function
|
||||||
|
bufferization::OneShotBufferizationOptions options;
|
||||||
|
options.allowUnknownOps = true;
|
||||||
|
bufferization::BufferizationState state;
|
||||||
|
if (failed(bufferization::runOneShotBufferize(coreOp, options, state))) {
|
||||||
|
coreOp.emitError("Failed to bufferize PIM and Spatial ops");
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
return success();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (failed(result)) {
|
||||||
|
moduleOp.emitError("Failed to bufferize-parallel PIM and Spatial ops");
|
||||||
|
signalPassFailure();
|
||||||
|
}
|
||||||
|
|
||||||
|
funcOp->walk([&](bufferization::ToTensorOp toTensorOp) {
|
||||||
|
if (llvm::isa_and_present<pim::PimCoreOp>(toTensorOp->getParentOp()))
|
||||||
|
toTensorOp->setAttr("restrict", UnitAttr::get(ctx));
|
||||||
|
});
|
||||||
|
|
||||||
// One-Shot-Bufferization
|
// One-Shot-Bufferization
|
||||||
bufferization::OneShotBufferizationOptions options;
|
bufferization::OneShotBufferizationOptions options;
|
||||||
options.allowUnknownOps = true;
|
options.allowUnknownOps = true;
|
||||||
bufferization::BufferizationState state;
|
bufferization::BufferizationState state;
|
||||||
|
|
||||||
if (failed(bufferization::runOneShotBufferize(moduleOp, options, state))) {
|
if (failed(bufferization::runOneShotBufferize(moduleOp, options, state))) {
|
||||||
moduleOp.emitError("Failed to bufferize PIM and Spatial ops");
|
moduleOp.emitError("Failed to bufferize PIM and Spatial ops");
|
||||||
signalPassFailure();
|
signalPassFailure();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
MLIRContext* ctx = moduleOp.getContext();
|
MLIRContext* ctx = moduleOp.getContext();
|
||||||
ConversionTarget target(*ctx);
|
ConversionTarget target(*ctx);
|
||||||
@@ -57,7 +92,18 @@ void PimBufferizationPass::runOnOperation() {
|
|||||||
RewritePatternSet patterns(ctx);
|
RewritePatternSet patterns(ctx);
|
||||||
populateWithGenerated(patterns);
|
populateWithGenerated(patterns);
|
||||||
|
|
||||||
if (failed(applyPartialConversion(moduleOp, target, std::move(patterns)))) {
|
// Only convert memref.copy → pim.memcp inside pim.core / pim.core_batch bodies.
|
||||||
|
// Host-level copies (e.g. from split/slice ops) must remain as memref.copy for CPU lowering.
|
||||||
|
FrozenRewritePatternSet frozenPatterns(std::move(patterns));
|
||||||
|
bool hasFailed = false;
|
||||||
|
moduleOp.walk<WalkOrder::PreOrder>([&](Operation* op) {
|
||||||
|
if (!isa<pim::PimCoreOp, pim::PimCoreBatchOp>(op))
|
||||||
|
return WalkResult::advance();
|
||||||
|
if (failed(applyPartialConversion(op, target, frozenPatterns)))
|
||||||
|
hasFailed = true;
|
||||||
|
return WalkResult::skip();
|
||||||
|
});
|
||||||
|
if (hasFailed) {
|
||||||
signalPassFailure();
|
signalPassFailure();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -93,8 +139,8 @@ void PimBufferizationPass::runOnOperation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PimBufferizationPass::annotateWeightsMemrefs(ModuleOp moduleOp, func::FuncOp funcOp) const {
|
void PimBufferizationPass::annotateWeightsMemrefs(ModuleOp moduleOp, func::FuncOp funcOp) const {
|
||||||
funcOp.walk([&](PimCoreOp coreOp) {
|
auto markWeights = [&](Operation* op) {
|
||||||
walkPimMvmVmmWeightUses(coreOp, [&](OpOperand& weightUse) {
|
walkPimMvmVmmWeightUses(op, [&](OpOperand& weightUse) {
|
||||||
Value weight = weightUse.get();
|
Value weight = weightUse.get();
|
||||||
auto getGlobalOp = weight.getDefiningOp<memref::GetGlobalOp>();
|
auto getGlobalOp = weight.getDefiningOp<memref::GetGlobalOp>();
|
||||||
if (!getGlobalOp)
|
if (!getGlobalOp)
|
||||||
@@ -104,7 +150,10 @@ void PimBufferizationPass::annotateWeightsMemrefs(ModuleOp moduleOp, func::FuncO
|
|||||||
markWeightAlways(getGlobalOp);
|
markWeightAlways(getGlobalOp);
|
||||||
markWeightAlways(globalMemrefOp);
|
markWeightAlways(globalMemrefOp);
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
|
funcOp.walk([&](PimCoreOp coreOp) { markWeights(coreOp); });
|
||||||
|
funcOp.walk([&](PimCoreBatchOp coreBatchOp) { markWeights(coreBatchOp); });
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Pass> createPimBufferizationPass() { return std::make_unique<PimBufferizationPass>(); }
|
std::unique_ptr<Pass> createPimBufferizationPass() { return std::make_unique<PimBufferizationPass>(); }
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ add_onnx_mlir_dialect(Spatial spat)
|
|||||||
add_onnx_mlir_dialect_doc(spat Spatial.td)
|
add_onnx_mlir_dialect_doc(spat Spatial.td)
|
||||||
|
|
||||||
add_pim_library(SpatialOps
|
add_pim_library(SpatialOps
|
||||||
|
Channels.cpp
|
||||||
SpatialOps.cpp
|
SpatialOps.cpp
|
||||||
Transforms/MergeComputeNodes/MergeComputeNodesPass.cpp
|
Transforms/MergeComputeNodes/MergeComputeNodesPass.cpp
|
||||||
Transforms/MergeComputeNodes/DCPGraph/Graph.cpp
|
Transforms/MergeComputeNodes/DCPGraph/Graph.cpp
|
||||||
|
|||||||
120
src/PIM/Dialect/Spatial/Channels.cpp
Normal file
120
src/PIM/Dialect/Spatial/Channels.cpp
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
#include "src/Accelerators/PIM/Dialect/Spatial/Channels.hpp"
|
||||||
|
|
||||||
|
#include "mlir/IR/BuiltinAttributes.h"
|
||||||
|
#include "mlir/IR/Diagnostics.h"
|
||||||
|
|
||||||
|
using namespace mlir;
|
||||||
|
|
||||||
|
namespace onnx_mlir::spatial {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
static Channels::ChannelId getChannelId(SpatChannelSendOp sendOp) { return sendOp.getChannelId(); }
|
||||||
|
|
||||||
|
static Channels::ChannelId getChannelId(SpatChannelReceiveOp receiveOp) { return receiveOp.getChannelId(); }
|
||||||
|
|
||||||
|
static LogicalResult verifyEndpointPair(ChannelEndpoints endpoints) {
|
||||||
|
if (!endpoints.send || !endpoints.receive)
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
if (endpoints.send.getSourceCoreId() != endpoints.receive.getSourceCoreId()) {
|
||||||
|
endpoints.send.emitOpError("sourceCoreId does not match paired spat.channel_receive");
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
if (endpoints.send.getTargetCoreId() != endpoints.receive.getTargetCoreId()) {
|
||||||
|
endpoints.send.emitOpError("targetCoreId does not match paired spat.channel_receive");
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
if (endpoints.send.getInput().getType() != endpoints.receive.getOutput().getType()) {
|
||||||
|
endpoints.send.emitOpError("input type does not match paired spat.channel_receive result type");
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Channels::Channels(func::FuncOp funcOp) {
|
||||||
|
if (!funcOp)
|
||||||
|
return;
|
||||||
|
|
||||||
|
funcOp.walk([&](SpatChannelSendOp sendOp) { insertSend(sendOp); });
|
||||||
|
funcOp.walk([&](SpatChannelReceiveOp receiveOp) { insertReceive(receiveOp); });
|
||||||
|
}
|
||||||
|
|
||||||
|
Channels::ChannelId Channels::allocate() { return nextChannelId++; }
|
||||||
|
|
||||||
|
void Channels::insertSend(SpatChannelSendOp sendOp) {
|
||||||
|
ChannelId channelId = getChannelId(sendOp);
|
||||||
|
nextChannelId = std::max(nextChannelId, channelId + 1);
|
||||||
|
endpoints[channelId].send = sendOp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channels::insertReceive(SpatChannelReceiveOp receiveOp) {
|
||||||
|
ChannelId channelId = getChannelId(receiveOp);
|
||||||
|
nextChannelId = std::max(nextChannelId, channelId + 1);
|
||||||
|
endpoints[channelId].receive = receiveOp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channels::eraseSend(SpatChannelSendOp sendOp) {
|
||||||
|
ChannelId channelId = getChannelId(sendOp);
|
||||||
|
auto it = endpoints.find(channelId);
|
||||||
|
if (it == endpoints.end())
|
||||||
|
return;
|
||||||
|
it->second.send = {};
|
||||||
|
if (!it->second.receive)
|
||||||
|
endpoints.erase(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channels::eraseReceive(SpatChannelReceiveOp receiveOp) {
|
||||||
|
ChannelId channelId = getChannelId(receiveOp);
|
||||||
|
auto it = endpoints.find(channelId);
|
||||||
|
if (it == endpoints.end())
|
||||||
|
return;
|
||||||
|
it->second.receive = {};
|
||||||
|
if (!it->second.send)
|
||||||
|
endpoints.erase(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
FailureOr<ChannelEndpoints> Channels::lookup(ChannelId id) const {
|
||||||
|
auto it = endpoints.find(id);
|
||||||
|
if (it == endpoints.end())
|
||||||
|
return failure();
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
FailureOr<SpatChannelReceiveOp> Channels::getReceiveFor(SpatChannelSendOp sendOp) const {
|
||||||
|
auto endpointsOr = lookup(getChannelId(sendOp));
|
||||||
|
if (failed(endpointsOr) || !endpointsOr->receive)
|
||||||
|
return failure();
|
||||||
|
return endpointsOr->receive;
|
||||||
|
}
|
||||||
|
|
||||||
|
FailureOr<SpatChannelSendOp> Channels::getSendFor(SpatChannelReceiveOp receiveOp) const {
|
||||||
|
auto endpointsOr = lookup(getChannelId(receiveOp));
|
||||||
|
if (failed(endpointsOr) || !endpointsOr->send)
|
||||||
|
return failure();
|
||||||
|
return endpointsOr->send;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogicalResult Channels::verify() const {
|
||||||
|
for (const auto& [channelId, pair] : endpoints) {
|
||||||
|
if (!pair.send || !pair.receive) {
|
||||||
|
if (pair.send) {
|
||||||
|
auto sendOp = pair.send;
|
||||||
|
sendOp.emitOpError("channel_id ") << channelId << " is missing a paired spat.channel_receive";
|
||||||
|
}
|
||||||
|
else if (pair.receive) {
|
||||||
|
auto receiveOp = pair.receive;
|
||||||
|
receiveOp.emitOpError("channel_id ") << channelId << " is missing a paired spat.channel_send";
|
||||||
|
}
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
if (failed(verifyEndpointPair(pair)))
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace onnx_mlir::spatial
|
||||||
43
src/PIM/Dialect/Spatial/Channels.hpp
Normal file
43
src/PIM/Dialect/Spatial/Channels.hpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
||||||
|
#include "mlir/Support/LogicalResult.h"
|
||||||
|
|
||||||
|
#include "llvm/ADT/DenseMap.h"
|
||||||
|
#include "llvm/ADT/StringRef.h"
|
||||||
|
|
||||||
|
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
|
||||||
|
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
||||||
|
|
||||||
|
namespace onnx_mlir::spatial {
|
||||||
|
|
||||||
|
struct ChannelEndpoints {
|
||||||
|
SpatChannelSendOp send;
|
||||||
|
SpatChannelReceiveOp receive;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Channels {
|
||||||
|
public:
|
||||||
|
using ChannelId = int64_t;
|
||||||
|
|
||||||
|
explicit Channels(mlir::func::FuncOp funcOp);
|
||||||
|
|
||||||
|
ChannelId allocate();
|
||||||
|
|
||||||
|
void insertSend(SpatChannelSendOp sendOp);
|
||||||
|
void insertReceive(SpatChannelReceiveOp receiveOp);
|
||||||
|
void eraseSend(SpatChannelSendOp sendOp);
|
||||||
|
void eraseReceive(SpatChannelReceiveOp receiveOp);
|
||||||
|
|
||||||
|
llvm::FailureOr<ChannelEndpoints> lookup(ChannelId id) const;
|
||||||
|
llvm::FailureOr<SpatChannelReceiveOp> getReceiveFor(SpatChannelSendOp sendOp) const;
|
||||||
|
llvm::FailureOr<SpatChannelSendOp> getSendFor(SpatChannelReceiveOp receiveOp) const;
|
||||||
|
|
||||||
|
mlir::LogicalResult verify() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ChannelId nextChannelId = 0;
|
||||||
|
llvm::DenseMap<ChannelId, ChannelEndpoints> endpoints;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace onnx_mlir::spatial
|
||||||
@@ -9,7 +9,6 @@ def SpatialDialect : Dialect {
|
|||||||
let name = "spat";
|
let name = "spat";
|
||||||
let summary = "Dialect designed for deep learning computation in a spatial architecture";
|
let summary = "Dialect designed for deep learning computation in a spatial architecture";
|
||||||
let cppNamespace = "::onnx_mlir::spatial";
|
let cppNamespace = "::onnx_mlir::spatial";
|
||||||
let useDefaultTypePrinterParser = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class SpatOp<string mnemonic, list<Trait> traits = []> :
|
class SpatOp<string mnemonic, list<Trait> traits = []> :
|
||||||
@@ -19,15 +18,6 @@ class SpatOp<string mnemonic, list<Trait> traits = []> :
|
|||||||
def SpatTensor :
|
def SpatTensor :
|
||||||
AnyTypeOf<[AnyMemRef, AnyRankedTensor], "", "::mlir::ShapedType">;
|
AnyTypeOf<[AnyMemRef, AnyRankedTensor], "", "::mlir::ShapedType">;
|
||||||
|
|
||||||
class SpatType<string name, string typeMnemonic, list<Trait> traits = []>
|
|
||||||
: TypeDef<SpatialDialect, name, traits> {
|
|
||||||
let mnemonic = typeMnemonic;
|
|
||||||
}
|
|
||||||
|
|
||||||
def SpatChannelType : SpatType<"SpatChannel", "ch"> {
|
|
||||||
let summary = "Virtual channel type";
|
|
||||||
}
|
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// Execution
|
// Execution
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
@@ -48,10 +38,27 @@ def SpatCompute : SpatOp<"compute", [SingleBlock, AttrSizedOperandSegments]> {
|
|||||||
|
|
||||||
let hasVerifier = 1;
|
let hasVerifier = 1;
|
||||||
let hasFolder = 1;
|
let hasFolder = 1;
|
||||||
|
let hasCustomAssemblyFormat = 1;
|
||||||
|
}
|
||||||
|
|
||||||
let assemblyFormat = [{
|
def SpatComputeBatch : SpatOp<"compute_batch",
|
||||||
`[` $weights `]` `(` $inputs `)` attr-dict `:` `[` type($weights) `]` `(` type($inputs) `)` `->` type($outputs) $body
|
[SingleBlock, AttrSizedOperandSegments]> {
|
||||||
}];
|
let summary = "Compressed batch of independent equivalent compute lanes";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
I32Attr:$laneCount,
|
||||||
|
Variadic<SpatTensor>:$weights,
|
||||||
|
Variadic<SpatTensor>:$inputs
|
||||||
|
);
|
||||||
|
|
||||||
|
let results = (outs
|
||||||
|
Variadic<SpatTensor>:$outputs
|
||||||
|
);
|
||||||
|
|
||||||
|
let regions = (region SizedRegion<1>:$body);
|
||||||
|
|
||||||
|
let hasVerifier = 1;
|
||||||
|
let hasCustomAssemblyFormat = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
def SpatYieldOp : SpatOp<"yield", [Terminator]> {
|
def SpatYieldOp : SpatOp<"yield", [Terminator]> {
|
||||||
@@ -61,51 +68,66 @@ def SpatYieldOp : SpatOp<"yield", [Terminator]> {
|
|||||||
Variadic<SpatTensor>:$outputs
|
Variadic<SpatTensor>:$outputs
|
||||||
);
|
);
|
||||||
|
|
||||||
let assemblyFormat = [{
|
let hasCustomAssemblyFormat = 1;
|
||||||
$outputs attr-dict `:` type($outputs)
|
}
|
||||||
}];
|
|
||||||
|
def SpatExtractRowsOp : SpatOp<"extract_rows", []> {
|
||||||
|
let summary = "Extract every row of a rank-2 tensor as separate rank-2 row tensors";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
SpatTensor:$input
|
||||||
|
);
|
||||||
|
|
||||||
|
let results = (outs
|
||||||
|
Variadic<SpatTensor>:$outputs
|
||||||
|
);
|
||||||
|
|
||||||
|
let hasVerifier = 1;
|
||||||
|
let hasCustomAssemblyFormat = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
def SpatConcatOp : SpatOp<"concat", []> {
|
||||||
|
let summary = "Concatenate tensors with compact Spatial operand syntax";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
I64Attr:$axis,
|
||||||
|
Variadic<SpatTensor>:$inputs
|
||||||
|
);
|
||||||
|
|
||||||
|
let results = (outs
|
||||||
|
SpatTensor:$output
|
||||||
|
);
|
||||||
|
|
||||||
|
let hasVerifier = 1;
|
||||||
|
let hasCustomAssemblyFormat = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// Communication
|
// Communication
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
def SpatChannelNewOp : SpatOp<"channel_new", []> {
|
|
||||||
let summary = "Create a new virtual channel";
|
|
||||||
|
|
||||||
let results = (outs
|
|
||||||
SpatChannelType:$channel
|
|
||||||
);
|
|
||||||
|
|
||||||
let builders = [
|
|
||||||
OpBuilder<(ins ), [{
|
|
||||||
$_state.addTypes(SpatChannelType());
|
|
||||||
}]>
|
|
||||||
];
|
|
||||||
|
|
||||||
let assemblyFormat = [{
|
|
||||||
attr-dict
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
|
|
||||||
def SpatChannelSendOp : SpatOp<"channel_send", []> {
|
def SpatChannelSendOp : SpatOp<"channel_send", []> {
|
||||||
let summary = "Send a tensor through a channel";
|
let summary = "Send a tensor through a logical channel";
|
||||||
|
|
||||||
let arguments = (ins
|
let arguments = (ins
|
||||||
SpatChannelType:$channel,
|
I64Attr:$channelId,
|
||||||
|
I32Attr:$sourceCoreId,
|
||||||
|
I32Attr:$targetCoreId,
|
||||||
SpatTensor:$input
|
SpatTensor:$input
|
||||||
);
|
);
|
||||||
|
|
||||||
let assemblyFormat = [{
|
let assemblyFormat = [{
|
||||||
$input `to` $channel attr-dict `:` `(` type($input) `->` type($channel) `)`
|
$input attr-dict `:` type($input)
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
def SpatChannelReceiveOp : SpatOp<"channel_receive", []> {
|
def SpatChannelReceiveOp : SpatOp<"channel_receive", []> {
|
||||||
let summary = "Receive a tensor from a channel";
|
let summary = "Receive a tensor from a logical channel";
|
||||||
|
|
||||||
let arguments = (ins
|
let arguments = (ins
|
||||||
SpatChannelType:$channel
|
I64Attr:$channelId,
|
||||||
|
I32Attr:$sourceCoreId,
|
||||||
|
I32Attr:$targetCoreId
|
||||||
);
|
);
|
||||||
|
|
||||||
let results = (outs
|
let results = (outs
|
||||||
@@ -113,37 +135,70 @@ def SpatChannelReceiveOp : SpatOp<"channel_receive", []> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let assemblyFormat = [{
|
let assemblyFormat = [{
|
||||||
$channel attr-dict `:` `(` type($channel) `->` type($output) `)`
|
attr-dict `:` type($output)
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
def SpatChannelBroadcastSendOp : SpatOp<"channel_broadcast_send", []> {
|
def SpatChannelSendManyOp : SpatOp<"channel_send_many", []> {
|
||||||
let summary = "Broadcast a tensor through a shared channel buffer";
|
let summary = "Send multiple tensors through logical channels";
|
||||||
|
|
||||||
let arguments = (ins
|
let arguments = (ins
|
||||||
SpatChannelType:$channel,
|
DenseI64ArrayAttr:$channelIds,
|
||||||
|
DenseI32ArrayAttr:$sourceCoreIds,
|
||||||
|
DenseI32ArrayAttr:$targetCoreIds,
|
||||||
|
Variadic<SpatTensor>:$inputs
|
||||||
|
);
|
||||||
|
|
||||||
|
let hasVerifier = 1;
|
||||||
|
let hasCustomAssemblyFormat = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
def SpatChannelReceiveManyOp : SpatOp<"channel_receive_many", []> {
|
||||||
|
let summary = "Receive multiple tensors from logical channels";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
DenseI64ArrayAttr:$channelIds,
|
||||||
|
DenseI32ArrayAttr:$sourceCoreIds,
|
||||||
|
DenseI32ArrayAttr:$targetCoreIds
|
||||||
|
);
|
||||||
|
|
||||||
|
let results = (outs
|
||||||
|
Variadic<SpatTensor>:$outputs
|
||||||
|
);
|
||||||
|
|
||||||
|
let hasVerifier = 1;
|
||||||
|
let hasCustomAssemblyFormat = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
def SpatChannelSendBatchOp : SpatOp<"channel_send_batch", []> {
|
||||||
|
let summary = "Send per-lane tensors through logical channels in a batch body";
|
||||||
|
|
||||||
|
let arguments = (ins
|
||||||
|
DenseI64ArrayAttr:$channelIds,
|
||||||
|
DenseI32ArrayAttr:$sourceCoreIds,
|
||||||
|
DenseI32ArrayAttr:$targetCoreIds,
|
||||||
SpatTensor:$input
|
SpatTensor:$input
|
||||||
);
|
);
|
||||||
|
|
||||||
let assemblyFormat = [{
|
let hasVerifier = 1;
|
||||||
$input `to` $channel attr-dict `:` `(` type($input) `->` type($channel) `)`
|
let hasCustomAssemblyFormat = 1;
|
||||||
}];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def SpatChannelBroadcastReceiveOp : SpatOp<"channel_broadcast_receive", []> {
|
def SpatChannelReceiveBatchOp : SpatOp<"channel_receive_batch", []> {
|
||||||
let summary = "Receive a tensor from a shared channel buffer";
|
let summary = "Receive a per-lane tensor through logical channels in a batch body";
|
||||||
|
|
||||||
let arguments = (ins
|
let arguments = (ins
|
||||||
SpatChannelType:$channel
|
DenseI64ArrayAttr:$channelIds,
|
||||||
|
DenseI32ArrayAttr:$sourceCoreIds,
|
||||||
|
DenseI32ArrayAttr:$targetCoreIds
|
||||||
);
|
);
|
||||||
|
|
||||||
let results = (outs
|
let results = (outs
|
||||||
SpatTensor:$output
|
SpatTensor:$output
|
||||||
);
|
);
|
||||||
|
|
||||||
let assemblyFormat = [{
|
let hasVerifier = 1;
|
||||||
$channel attr-dict `:` `(` type($channel) `->` type($output) `)`
|
let hasCustomAssemblyFormat = 1;
|
||||||
}];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -28,6 +28,8 @@ namespace spatial {
|
|||||||
using namespace mlir;
|
using namespace mlir;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
using SpatCompute = onnx_mlir::spatial::SpatCompute;
|
||||||
|
using SpatComputeBatch = onnx_mlir::spatial::SpatComputeBatch;
|
||||||
|
|
||||||
struct VirtualNode {
|
struct VirtualNode {
|
||||||
SmallVector<size_t, 4> originalComputeIndices;
|
SmallVector<size_t, 4> originalComputeIndices;
|
||||||
@@ -54,6 +56,45 @@ struct WindowScheduleResult {
|
|||||||
size_t maxMergeGroupSize = 0;
|
size_t maxMergeGroupSize = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constexpr CPU kDefaultMaxCpuCount = 1000;
|
||||||
|
|
||||||
|
size_t getSchedulingCpuBudget() {
|
||||||
|
if (coresCount.getValue() > 0)
|
||||||
|
return static_cast<size_t>(coresCount.getValue());
|
||||||
|
return static_cast<size_t>(kDefaultMaxCpuCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getBatchChunkTargetCount(int32_t laneCount) {
|
||||||
|
assert(laneCount > 0 && "laneCount must be positive");
|
||||||
|
return std::min(static_cast<size_t>(laneCount), std::max<size_t>(1, getSchedulingCpuBudget()));
|
||||||
|
}
|
||||||
|
|
||||||
|
ComputeInstance getBatchChunkForIndex(SpatComputeBatch batch, size_t chunkIndex) {
|
||||||
|
size_t totalLanes = static_cast<size_t>(batch.getLaneCount());
|
||||||
|
size_t chunkCount = getBatchChunkTargetCount(batch.getLaneCount());
|
||||||
|
size_t baseChunkSize = totalLanes / chunkCount;
|
||||||
|
size_t largeChunkCount = totalLanes % chunkCount;
|
||||||
|
|
||||||
|
size_t laneStart = chunkIndex * baseChunkSize + std::min(chunkIndex, largeChunkCount);
|
||||||
|
size_t laneCount = baseChunkSize + (chunkIndex < largeChunkCount ? 1 : 0);
|
||||||
|
return {batch.getOperation(), static_cast<uint32_t>(laneStart), static_cast<uint32_t>(laneCount)};
|
||||||
|
}
|
||||||
|
|
||||||
|
ComputeInstance getBatchChunkForLane(SpatComputeBatch batch, uint32_t lane) {
|
||||||
|
size_t totalLanes = static_cast<size_t>(batch.getLaneCount());
|
||||||
|
size_t chunkCount = getBatchChunkTargetCount(batch.getLaneCount());
|
||||||
|
size_t baseChunkSize = totalLanes / chunkCount;
|
||||||
|
size_t largeChunkCount = totalLanes % chunkCount;
|
||||||
|
size_t largeChunkSpan = largeChunkCount * (baseChunkSize + 1);
|
||||||
|
|
||||||
|
size_t chunkIndex = 0;
|
||||||
|
if (static_cast<size_t>(lane) < largeChunkSpan)
|
||||||
|
chunkIndex = static_cast<size_t>(lane) / (baseChunkSize + 1);
|
||||||
|
else
|
||||||
|
chunkIndex = largeChunkCount + (static_cast<size_t>(lane) - largeChunkSpan) / baseChunkSize;
|
||||||
|
return getBatchChunkForIndex(batch, chunkIndex);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<IndexedEdge> aggregateEdges(ArrayRef<IndexedEdge> edges) {
|
std::vector<IndexedEdge> aggregateEdges(ArrayRef<IndexedEdge> edges) {
|
||||||
llvm::DenseMap<std::pair<size_t, size_t>, Weight> edgeWeights;
|
llvm::DenseMap<std::pair<size_t, size_t>, Weight> edgeWeights;
|
||||||
for (auto [start, end, weight] : edges) {
|
for (auto [start, end, weight] : edges) {
|
||||||
@@ -81,14 +122,96 @@ std::vector<IndexedEdge> aggregateEdges(ArrayRef<IndexedEdge> edges) {
|
|||||||
return aggregatedEdges;
|
return aggregatedEdges;
|
||||||
}
|
}
|
||||||
|
|
||||||
VirtualGraph buildInitialVirtualGraph(ArrayRef<SpatCompute> spatComputes, ArrayRef<IndexedEdge> edges) {
|
Weight getComputeBodyWeight(Region& body) {
|
||||||
|
constexpr Weight kOperationWeight = 100;
|
||||||
|
Weight numOperations = 0;
|
||||||
|
for (auto& block : body)
|
||||||
|
for ([[maybe_unused]] auto& op : block)
|
||||||
|
numOperations = checkedAdd(numOperations, static_cast<Weight>(1));
|
||||||
|
return checkedMultiply(numOperations, kOperationWeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
CrossbarUsage getComputeBodyCrossbarUsage(Region& body) {
|
||||||
|
CrossbarUsage crossbarUsage = 0;
|
||||||
|
for (auto& block : body)
|
||||||
|
for (auto& op : block)
|
||||||
|
if (isa<SpatWeightedVMMOp>(op))
|
||||||
|
crossbarUsage = checkedAdd(crossbarUsage, static_cast<CrossbarUsage>(1));
|
||||||
|
return crossbarUsage;
|
||||||
|
}
|
||||||
|
|
||||||
|
Weight getComputeInstanceWeight(const ComputeInstance& instance) {
|
||||||
|
if (auto spatCompute = dyn_cast<SpatCompute>(instance.op))
|
||||||
|
return getSpatComputeWeight(spatCompute);
|
||||||
|
auto batch = cast<SpatComputeBatch>(instance.op);
|
||||||
|
return checkedMultiply(getComputeBodyWeight(batch.getBody()), static_cast<Weight>(instance.laneCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
CrossbarUsage getComputeInstanceCrossbarUsage(const ComputeInstance& instance) {
|
||||||
|
if (auto spatCompute = dyn_cast<SpatCompute>(instance.op))
|
||||||
|
return getSpatComputeCrossbarUsage(spatCompute);
|
||||||
|
auto batch = cast<SpatComputeBatch>(instance.op);
|
||||||
|
return checkedMultiply(getComputeBodyCrossbarUsage(batch.getBody()), static_cast<CrossbarUsage>(instance.laneCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
SmallVector<Value, 4> getComputeInstanceInputs(const ComputeInstance& instance) {
|
||||||
|
if (auto spatCompute = dyn_cast<SpatCompute>(instance.op))
|
||||||
|
return SmallVector<Value, 4>(spatCompute.getInputs().begin(), spatCompute.getInputs().end());
|
||||||
|
auto batch = cast<SpatComputeBatch>(instance.op);
|
||||||
|
SmallVector<Value, 4> inputs;
|
||||||
|
inputs.reserve(instance.laneCount);
|
||||||
|
for (uint32_t lane = instance.laneStart; lane < instance.laneStart + instance.laneCount; ++lane)
|
||||||
|
inputs.push_back(batch.getInputs()[lane]);
|
||||||
|
return inputs;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<ComputeInstance> getOriginalComputeInstance(Value value) {
|
||||||
|
Operation* op = value.getDefiningOp();
|
||||||
|
if (!op)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
while (auto extract = dyn_cast<tensor::ExtractSliceOp>(op)) {
|
||||||
|
value = extract.getSource();
|
||||||
|
op = value.getDefiningOp();
|
||||||
|
if (!op)
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto spatCompute = dyn_cast<SpatCompute>(op))
|
||||||
|
return ComputeInstance {spatCompute.getOperation(), 0, 1};
|
||||||
|
if (auto batch = dyn_cast<SpatComputeBatch>(op))
|
||||||
|
return getBatchChunkForLane(batch, static_cast<uint32_t>(cast<OpResult>(value).getResultNumber()));
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
SmallVector<ComputeInstance> collectComputeInstances(Operation* entryOp) {
|
||||||
|
SmallVector<ComputeInstance> instances;
|
||||||
|
for (Region& region : entryOp->getRegions()) {
|
||||||
|
for (Block& block : region) {
|
||||||
|
for (Operation& op : block) {
|
||||||
|
if (auto spatCompute = dyn_cast<SpatCompute>(&op)) {
|
||||||
|
instances.push_back({spatCompute.getOperation(), 0, 1});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (auto batch = dyn_cast<SpatComputeBatch>(&op)) {
|
||||||
|
size_t chunkCount = getBatchChunkTargetCount(batch.getLaneCount());
|
||||||
|
for (size_t chunkIndex = 0; chunkIndex < chunkCount; ++chunkIndex)
|
||||||
|
instances.push_back(getBatchChunkForIndex(batch, chunkIndex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
VirtualGraph buildInitialVirtualGraph(ArrayRef<ComputeInstance> computeInstances, ArrayRef<IndexedEdge> edges) {
|
||||||
VirtualGraph graph;
|
VirtualGraph graph;
|
||||||
graph.nodes.reserve(spatComputes.size());
|
graph.nodes.reserve(computeInstances.size());
|
||||||
for (auto [index, spatCompute] : llvm::enumerate(spatComputes)) {
|
for (auto [index, computeInstance] : llvm::enumerate(computeInstances)) {
|
||||||
VirtualNode node;
|
VirtualNode node;
|
||||||
node.originalComputeIndices.push_back(index);
|
node.originalComputeIndices.push_back(index);
|
||||||
node.weight = getSpatComputeWeight(spatCompute);
|
node.weight = getComputeInstanceWeight(computeInstance);
|
||||||
node.crossbarUsage = getSpatComputeCrossbarUsage(spatCompute);
|
node.crossbarUsage = getComputeInstanceCrossbarUsage(computeInstance);
|
||||||
graph.nodes.push_back(std::move(node));
|
graph.nodes.push_back(std::move(node));
|
||||||
}
|
}
|
||||||
graph.edges = aggregateEdges(edges);
|
graph.edges = aggregateEdges(edges);
|
||||||
@@ -116,22 +239,34 @@ TimingInfo computeTiming(const VirtualGraph& graph) {
|
|||||||
incomingEdgeCount[endIndex]++;
|
incomingEdgeCount[endIndex]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<size_t> readyNodes;
|
auto getVirtualNodeOrderKey = [&](size_t nodeIndex) {
|
||||||
readyNodes.reserve(nodeCount);
|
const VirtualNode& node = graph.nodes[nodeIndex];
|
||||||
|
if (!node.originalComputeIndices.empty())
|
||||||
|
return node.originalComputeIndices.front();
|
||||||
|
return nodeIndex;
|
||||||
|
};
|
||||||
|
auto readyNodeGreater = [&](size_t lhs, size_t rhs) {
|
||||||
|
size_t lhsKey = getVirtualNodeOrderKey(lhs);
|
||||||
|
size_t rhsKey = getVirtualNodeOrderKey(rhs);
|
||||||
|
if (lhsKey != rhsKey)
|
||||||
|
return lhsKey > rhsKey;
|
||||||
|
return lhs > rhs;
|
||||||
|
};
|
||||||
|
std::priority_queue<size_t, std::vector<size_t>, decltype(readyNodeGreater)> readyNodes(readyNodeGreater);
|
||||||
for (size_t i = 0; i < nodeCount; ++i)
|
for (size_t i = 0; i < nodeCount; ++i)
|
||||||
if (incomingEdgeCount[i] == 0)
|
if (incomingEdgeCount[i] == 0)
|
||||||
readyNodes.push_back(i);
|
readyNodes.push(i);
|
||||||
|
|
||||||
size_t readyIndex = 0;
|
while (!readyNodes.empty()) {
|
||||||
while (readyIndex != readyNodes.size()) {
|
size_t current = readyNodes.top();
|
||||||
size_t current = readyNodes[readyIndex++];
|
readyNodes.pop();
|
||||||
timing.topologicalOrder.push_back(current);
|
timing.topologicalOrder.push_back(current);
|
||||||
for (auto [child, weight] : children[current]) {
|
for (auto [child, weight] : children[current]) {
|
||||||
(void) weight;
|
(void) weight;
|
||||||
assert(incomingEdgeCount[child] > 0 && "incoming edge count underflow");
|
assert(incomingEdgeCount[child] > 0 && "incoming edge count underflow");
|
||||||
incomingEdgeCount[child]--;
|
incomingEdgeCount[child]--;
|
||||||
if (incomingEdgeCount[child] == 0)
|
if (incomingEdgeCount[child] == 0)
|
||||||
readyNodes.push_back(child);
|
readyNodes.push(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,17 +422,21 @@ std::vector<IndexedEdge> buildWindowEdges(const VirtualGraph& graph, const std::
|
|||||||
WindowScheduleResult scheduleWindow(const VirtualGraph& graph, ArrayRef<size_t> selectedNodes, MLIRContext* context) {
|
WindowScheduleResult scheduleWindow(const VirtualGraph& graph, ArrayRef<size_t> selectedNodes, MLIRContext* context) {
|
||||||
std::vector<Weight> windowWeights;
|
std::vector<Weight> windowWeights;
|
||||||
std::vector<CrossbarUsage> windowCrossbarUsage;
|
std::vector<CrossbarUsage> windowCrossbarUsage;
|
||||||
|
std::vector<int64_t> windowNodeOrderKeys;
|
||||||
std::vector<int64_t> nodeToWindowIndex(graph.nodes.size(), -1);
|
std::vector<int64_t> nodeToWindowIndex(graph.nodes.size(), -1);
|
||||||
windowWeights.reserve(selectedNodes.size());
|
windowWeights.reserve(selectedNodes.size());
|
||||||
windowCrossbarUsage.reserve(selectedNodes.size());
|
windowCrossbarUsage.reserve(selectedNodes.size());
|
||||||
|
windowNodeOrderKeys.reserve(selectedNodes.size());
|
||||||
|
|
||||||
for (auto [windowIndex, nodeIndex] : llvm::enumerate(selectedNodes)) {
|
for (auto [windowIndex, nodeIndex] : llvm::enumerate(selectedNodes)) {
|
||||||
nodeToWindowIndex[nodeIndex] = static_cast<int64_t>(windowIndex);
|
nodeToWindowIndex[nodeIndex] = static_cast<int64_t>(windowIndex);
|
||||||
windowWeights.push_back(graph.nodes[nodeIndex].weight);
|
windowWeights.push_back(graph.nodes[nodeIndex].weight);
|
||||||
windowCrossbarUsage.push_back(graph.nodes[nodeIndex].crossbarUsage);
|
windowCrossbarUsage.push_back(graph.nodes[nodeIndex].crossbarUsage);
|
||||||
|
windowNodeOrderKeys.push_back(static_cast<int64_t>(nodeIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
GraphDCP windowGraph(windowWeights, buildWindowEdges(graph, nodeToWindowIndex), windowCrossbarUsage);
|
GraphDCP windowGraph(
|
||||||
|
windowWeights, buildWindowEdges(graph, nodeToWindowIndex), windowNodeOrderKeys, windowCrossbarUsage);
|
||||||
if (coresCount.getValue() > 0)
|
if (coresCount.getValue() > 0)
|
||||||
windowGraph.setMaxCpuCount(static_cast<int>(coresCount.getValue()));
|
windowGraph.setMaxCpuCount(static_cast<int>(coresCount.getValue()));
|
||||||
windowGraph.setContext(context);
|
windowGraph.setContext(context);
|
||||||
@@ -414,13 +553,7 @@ bool coarsenGraph(const VirtualGraph& graph,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr CPU kDefaultMaxCpuCount = 1000;
|
CPU getVirtualGraphMaxCpuCount() { return static_cast<CPU>(getSchedulingCpuBudget()); }
|
||||||
|
|
||||||
CPU getVirtualGraphMaxCpuCount() {
|
|
||||||
if (coresCount.getValue() > 0)
|
|
||||||
return static_cast<CPU>(coresCount.getValue());
|
|
||||||
return kDefaultMaxCpuCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t getDcpCoarseningWindowSize(size_t nodeCount) {
|
size_t getDcpCoarseningWindowSize(size_t nodeCount) {
|
||||||
size_t windowSize = std::min(dcpCriticalWindowSize.getValue(), nodeCount);
|
size_t windowSize = std::min(dcpCriticalWindowSize.getValue(), nodeCount);
|
||||||
@@ -430,7 +563,7 @@ size_t getDcpCoarseningWindowSize(size_t nodeCount) {
|
|||||||
return windowSize;
|
return windowSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
DCPAnalysisResult buildResultFromVirtualGraph(const VirtualGraph& graph, ArrayRef<SpatCompute> spatComputes) {
|
DCPAnalysisResult buildResultFromVirtualGraph(const VirtualGraph& graph, ArrayRef<ComputeInstance> computeInstances) {
|
||||||
DCPAnalysisResult result;
|
DCPAnalysisResult result;
|
||||||
|
|
||||||
TimingInfo timing = computeTiming(graph);
|
TimingInfo timing = computeTiming(graph);
|
||||||
@@ -443,19 +576,19 @@ DCPAnalysisResult buildResultFromVirtualGraph(const VirtualGraph& graph, ArrayRe
|
|||||||
std::iota(virtualNodeOrder.begin(), virtualNodeOrder.end(), 0);
|
std::iota(virtualNodeOrder.begin(), virtualNodeOrder.end(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<size_t> originalComputeToCpu(spatComputes.size(), 0);
|
std::vector<size_t> originalComputeToCpu(computeInstances.size(), 0);
|
||||||
for (auto [cpu, virtualNodeIndex] : llvm::enumerate(virtualNodeOrder)) {
|
for (auto [cpu, virtualNodeIndex] : llvm::enumerate(virtualNodeOrder)) {
|
||||||
const VirtualNode& virtualNode = graph.nodes[virtualNodeIndex];
|
const VirtualNode& virtualNode = graph.nodes[virtualNodeIndex];
|
||||||
for (size_t originalIndex : virtualNode.originalComputeIndices)
|
for (size_t originalIndex : virtualNode.originalComputeIndices)
|
||||||
originalComputeToCpu[originalIndex] = cpu;
|
originalComputeToCpu[originalIndex] = cpu;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.dominanceOrderCompute.reserve(spatComputes.size());
|
result.dominanceOrderCompute.reserve(computeInstances.size());
|
||||||
for (auto [originalIndex, spatCompute] : llvm::enumerate(spatComputes)) {
|
for (auto [originalIndex, computeInstance] : llvm::enumerate(computeInstances)) {
|
||||||
size_t cpu = originalComputeToCpu[originalIndex];
|
size_t cpu = originalComputeToCpu[originalIndex];
|
||||||
result.dominanceOrderCompute.push_back(spatCompute);
|
result.dominanceOrderCompute.push_back(computeInstance);
|
||||||
result.computeToCpuMap[spatCompute] = cpu;
|
result.computeToCpuMap[computeInstance] = cpu;
|
||||||
result.cpuToLastComputeMap[cpu] = spatCompute;
|
result.cpuToLastComputeMap[cpu] = computeInstance;
|
||||||
}
|
}
|
||||||
for (const auto& [cpu, lastCompute] : result.cpuToLastComputeMap)
|
for (const auto& [cpu, lastCompute] : result.cpuToLastComputeMap)
|
||||||
result.isLastComputeOfCpu.insert(lastCompute);
|
result.isLastComputeOfCpu.insert(lastCompute);
|
||||||
@@ -463,13 +596,44 @@ DCPAnalysisResult buildResultFromVirtualGraph(const VirtualGraph& graph, ArrayRe
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
DCPAnalysisResult runLegacyDcp(ArrayRef<SpatCompute> spatComputes, ArrayRef<IndexedEdge> edges, MLIRContext* context) {
|
DCPAnalysisResult buildResultFromScheduledGraph(GraphDCP& graphDCP, ArrayRef<ComputeInstance> computeInstances) {
|
||||||
GraphDCP graphDCP(spatComputes, edges);
|
DCPAnalysisResult result;
|
||||||
|
result.dominanceOrderCompute.assign(computeInstances.begin(), computeInstances.end());
|
||||||
|
|
||||||
|
for (CPU cpu = 0; cpu < graphDCP.cpuCount(); ++cpu) {
|
||||||
|
auto scheduledTasks = graphDCP.getScheduledTasks(cpu);
|
||||||
|
if (scheduledTasks.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (const auto& task : scheduledTasks)
|
||||||
|
result.computeToCpuMap[computeInstances[task.nodeIndex]] = cpu;
|
||||||
|
result.cpuToLastComputeMap[cpu] = computeInstances[scheduledTasks.back().nodeIndex];
|
||||||
|
result.isLastComputeOfCpu.insert(computeInstances[scheduledTasks.back().nodeIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
DCPAnalysisResult
|
||||||
|
runLegacyDcp(ArrayRef<ComputeInstance> computeInstances, ArrayRef<IndexedEdge> edges, MLIRContext* context) {
|
||||||
|
SmallVector<Weight> nodeWeights;
|
||||||
|
SmallVector<CrossbarUsage> nodeCrossbarUsage;
|
||||||
|
SmallVector<int64_t> nodeOrderKeys;
|
||||||
|
nodeWeights.reserve(computeInstances.size());
|
||||||
|
nodeCrossbarUsage.reserve(computeInstances.size());
|
||||||
|
nodeOrderKeys.reserve(computeInstances.size());
|
||||||
|
for (auto [index, instance] : llvm::enumerate(computeInstances)) {
|
||||||
|
nodeWeights.push_back(getComputeInstanceWeight(instance));
|
||||||
|
nodeCrossbarUsage.push_back(getComputeInstanceCrossbarUsage(instance));
|
||||||
|
nodeOrderKeys.push_back(static_cast<int64_t>(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphDCP graphDCP(nodeWeights, edges, nodeOrderKeys, nodeCrossbarUsage);
|
||||||
if (coresCount.getValue() > 0)
|
if (coresCount.getValue() > 0)
|
||||||
graphDCP.setMaxCpuCount(static_cast<int>(coresCount.getValue()));
|
graphDCP.setMaxCpuCount(static_cast<int>(coresCount.getValue()));
|
||||||
graphDCP.setContext(context);
|
graphDCP.setContext(context);
|
||||||
graphDCP.runDcp();
|
graphDCP.runDcp();
|
||||||
return graphDCP.getResult();
|
return buildResultFromScheduledGraph(graphDCP, computeInstances);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -488,27 +652,31 @@ SpatCompute getOriginalSpatCompute(Operation* op) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DCPAnalysisResult DCPAnalysis::run() {
|
DCPAnalysisResult DCPAnalysis::run() {
|
||||||
SmallVector<SpatCompute, 10> spatComputes;
|
SmallVector<ComputeInstance> computeInstances = collectComputeInstances(entryOp);
|
||||||
SmallVector<IndexedEdge, 10> edges;
|
SmallVector<IndexedEdge, 10> edges;
|
||||||
for (auto& region : entryOp->getRegions())
|
|
||||||
for (SpatCompute spatCompute : region.getOps<SpatCompute>())
|
|
||||||
spatComputes.push_back(spatCompute);
|
|
||||||
|
|
||||||
for (auto [indexEndEdge, spatCompute] : llvm::enumerate(spatComputes)) {
|
llvm::DenseMap<ComputeInstance, size_t> instanceToIndex;
|
||||||
for (Value input : spatCompute.getInputs()) {
|
instanceToIndex.reserve(computeInstances.size());
|
||||||
if (auto producerCompute = getOriginalSpatCompute(input.getDefiningOp())) {
|
for (auto [index, instance] : llvm::enumerate(computeInstances))
|
||||||
auto producerIt = llvm::find(spatComputes, producerCompute);
|
instanceToIndex[instance] = index;
|
||||||
assert(producerIt != spatComputes.end());
|
|
||||||
auto indexStartEdge = std::distance(spatComputes.begin(), producerIt);
|
for (auto [indexEndEdge, computeInstance] : llvm::enumerate(computeInstances)) {
|
||||||
edges.push_back({indexStartEdge, indexEndEdge, getSizeInBytes(cast<ShapedType>(input.getType()))});
|
for (Value input : getComputeInstanceInputs(computeInstance)) {
|
||||||
|
if (auto producerInstance = getOriginalComputeInstance(input)) {
|
||||||
|
auto producerIt = instanceToIndex.find(*producerInstance);
|
||||||
|
assert(producerIt != instanceToIndex.end());
|
||||||
|
auto indexStartEdge = producerIt->second;
|
||||||
|
edges.push_back({static_cast<int64_t>(indexStartEdge),
|
||||||
|
static_cast<int64_t>(indexEndEdge),
|
||||||
|
static_cast<int64_t>(getSizeInBytes(cast<ShapedType>(input.getType())))});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dcpCriticalWindowSize.getValue() == 0)
|
if (dcpCriticalWindowSize.getValue() == 0)
|
||||||
return runLegacyDcp(spatComputes, edges, entryOp->getContext());
|
return runLegacyDcp(computeInstances, edges, entryOp->getContext());
|
||||||
|
|
||||||
VirtualGraph virtualGraph = buildInitialVirtualGraph(spatComputes, edges);
|
VirtualGraph virtualGraph = buildInitialVirtualGraph(computeInstances, edges);
|
||||||
size_t iteration = 0;
|
size_t iteration = 0;
|
||||||
auto tryCoarsenSelectedNodes = [&](ArrayRef<size_t> selectedNodes) {
|
auto tryCoarsenSelectedNodes = [&](ArrayRef<size_t> selectedNodes) {
|
||||||
size_t oldNodeCount = virtualGraph.nodes.size();
|
size_t oldNodeCount = virtualGraph.nodes.size();
|
||||||
@@ -545,6 +713,13 @@ DCPAnalysisResult DCPAnalysis::run() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
while (virtualGraph.nodes.size() > 1) {
|
while (virtualGraph.nodes.size() > 1) {
|
||||||
|
if (virtualGraph.nodes.size() <= getSchedulingCpuBudget()) {
|
||||||
|
if (virtualGraph.nodes.size() >= 200)
|
||||||
|
llvm::errs() << llvm::formatv(
|
||||||
|
"[DCP-COARSEN] iter={0} old={1} stop=cpu-budget\n", iteration, virtualGraph.nodes.size());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
iteration++;
|
iteration++;
|
||||||
TimingInfo timing = computeTiming(virtualGraph);
|
TimingInfo timing = computeTiming(virtualGraph);
|
||||||
if (!timing.valid) {
|
if (!timing.valid) {
|
||||||
@@ -576,7 +751,7 @@ DCPAnalysisResult DCPAnalysis::run() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return buildResultFromVirtualGraph(virtualGraph, spatComputes);
|
return buildResultFromVirtualGraph(virtualGraph, computeInstances);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace spatial
|
} // namespace spatial
|
||||||
|
|||||||
@@ -5,15 +5,28 @@
|
|||||||
#include "llvm/ADT/DenseMap.h"
|
#include "llvm/ADT/DenseMap.h"
|
||||||
#include "llvm/ADT/DenseSet.h"
|
#include "llvm/ADT/DenseSet.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
|
||||||
|
|
||||||
|
// A scheduling identity that covers both spat.compute and scheduled shards of
|
||||||
|
// spat.compute_batch.
|
||||||
|
struct ComputeInstance {
|
||||||
|
mlir::Operation* op = nullptr;
|
||||||
|
uint32_t laneStart = 0;
|
||||||
|
uint32_t laneCount = 1;
|
||||||
|
|
||||||
|
bool operator==(const ComputeInstance& other) const {
|
||||||
|
return op == other.op && laneStart == other.laneStart && laneCount == other.laneCount;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct DCPAnalysisResult {
|
struct DCPAnalysisResult {
|
||||||
std::vector<onnx_mlir::spatial::SpatCompute> dominanceOrderCompute;
|
std::vector<ComputeInstance> dominanceOrderCompute;
|
||||||
llvm::DenseMap<onnx_mlir::spatial::SpatCompute, size_t> computeToCpuMap;
|
llvm::DenseMap<ComputeInstance, size_t> computeToCpuMap;
|
||||||
llvm::DenseSet<onnx_mlir::spatial::SpatCompute> isLastComputeOfCpu;
|
llvm::DenseSet<ComputeInstance> isLastComputeOfCpu;
|
||||||
llvm::DenseMap<size_t, onnx_mlir::spatial::SpatCompute> cpuToLastComputeMap;
|
llvm::DenseMap<size_t, ComputeInstance> cpuToLastComputeMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace onnx_mlir {
|
namespace onnx_mlir {
|
||||||
@@ -34,3 +47,21 @@ public:
|
|||||||
|
|
||||||
} // namespace spatial
|
} // namespace spatial
|
||||||
} // namespace onnx_mlir
|
} // namespace onnx_mlir
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
template <>
|
||||||
|
struct DenseMapInfo<ComputeInstance> {
|
||||||
|
static ComputeInstance getEmptyKey() {
|
||||||
|
return {DenseMapInfo<mlir::Operation*>::getEmptyKey(), UINT32_MAX, UINT32_MAX};
|
||||||
|
}
|
||||||
|
static ComputeInstance getTombstoneKey() {
|
||||||
|
return {DenseMapInfo<mlir::Operation*>::getTombstoneKey(), UINT32_MAX, UINT32_MAX};
|
||||||
|
}
|
||||||
|
static unsigned getHashValue(const ComputeInstance& v) {
|
||||||
|
return llvm::hash_combine(v.op, v.laneStart, v.laneCount);
|
||||||
|
}
|
||||||
|
static bool isEqual(const ComputeInstance& a, const ComputeInstance& b) {
|
||||||
|
return a == b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace llvm
|
||||||
|
|||||||
@@ -1491,18 +1491,21 @@ void GraphDCP::runDcp() {
|
|||||||
struct ReadyEntry {
|
struct ReadyEntry {
|
||||||
Time slack;
|
Time slack;
|
||||||
Time aest;
|
Time aest;
|
||||||
|
int64_t orderKey;
|
||||||
TaskDCP* task;
|
TaskDCP* task;
|
||||||
bool operator>(const ReadyEntry& other) const {
|
bool operator>(const ReadyEntry& other) const {
|
||||||
if (slack != other.slack)
|
if (slack != other.slack)
|
||||||
return slack > other.slack;
|
return slack > other.slack;
|
||||||
|
if (aest != other.aest)
|
||||||
return aest > other.aest;
|
return aest > other.aest;
|
||||||
|
return orderKey > other.orderKey;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::priority_queue<ReadyEntry, std::vector<ReadyEntry>, std::greater<ReadyEntry>> readyQueue;
|
std::priority_queue<ReadyEntry, std::vector<ReadyEntry>, std::greater<ReadyEntry>> readyQueue;
|
||||||
size_t readyCount = 0;
|
size_t readyCount = 0;
|
||||||
|
|
||||||
auto pushReady = [&](TaskDCP* node) {
|
auto pushReady = [&](TaskDCP* node) {
|
||||||
readyQueue.push({slackOrZero(node->getAest(), node->getAlst()), node->getAest(), node});
|
readyQueue.push({slackOrZero(node->getAest(), node->getAlst()), node->getAest(), node->Id(), node});
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto& node : nodes) {
|
for (auto& node : nodes) {
|
||||||
@@ -1528,7 +1531,7 @@ void GraphDCP::runDcp() {
|
|||||||
candidate = entry.task;
|
candidate = entry.task;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
readyQueue.push({curSlack, curAest, entry.task});
|
readyQueue.push({curSlack, curAest, entry.orderKey, entry.task});
|
||||||
}
|
}
|
||||||
assert(candidate != nullptr && "readyCount > 0 but heap exhausted");
|
assert(candidate != nullptr && "readyCount > 0 but heap exhausted");
|
||||||
--readyCount;
|
--readyCount;
|
||||||
@@ -1579,8 +1582,11 @@ DCPAnalysisResult GraphDCP::getResult() {
|
|||||||
|
|
||||||
auto dominanceOrder = dcp_graph::collectDominanceOrder(getRoots(), nodes.size());
|
auto dominanceOrder = dcp_graph::collectDominanceOrder(getRoots(), nodes.size());
|
||||||
ret.dominanceOrderCompute.reserve(dominanceOrder.size());
|
ret.dominanceOrderCompute.reserve(dominanceOrder.size());
|
||||||
for (auto elem : dominanceOrder)
|
for (auto elem : dominanceOrder) {
|
||||||
ret.dominanceOrderCompute.push_back(elem->getSpatCompute());
|
auto spatCompute = elem->getSpatCompute();
|
||||||
|
if (spatCompute)
|
||||||
|
ret.dominanceOrderCompute.push_back({spatCompute.getOperation(), 0});
|
||||||
|
}
|
||||||
|
|
||||||
for (CPU cpu = 0; cpu < getLastCpu(); ++cpu) {
|
for (CPU cpu = 0; cpu < getLastCpu(); ++cpu) {
|
||||||
const CpuTaskList* tasks = findCpuTasks(cpu);
|
const CpuTaskList* tasks = findCpuTasks(cpu);
|
||||||
@@ -1588,10 +1594,14 @@ DCPAnalysisResult GraphDCP::getResult() {
|
|||||||
continue;
|
continue;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (auto node : *tasks) {
|
for (auto node : *tasks) {
|
||||||
ret.computeToCpuMap[node->getSpatCompute()] = cpu;
|
auto spatCompute = node->getSpatCompute();
|
||||||
|
if (!spatCompute)
|
||||||
|
continue;
|
||||||
|
ComputeInstance instance {spatCompute.getOperation(), 0};
|
||||||
|
ret.computeToCpuMap[instance] = cpu;
|
||||||
if (i++ == tasks->size() - 1) {
|
if (i++ == tasks->size() - 1) {
|
||||||
ret.isLastComputeOfCpu.insert(node->getSpatCompute());
|
ret.isLastComputeOfCpu.insert(instance);
|
||||||
ret.cpuToLastComputeMap[cpu] = node->getSpatCompute();
|
ret.cpuToLastComputeMap[cpu] = instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,13 +138,18 @@ public:
|
|||||||
|
|
||||||
GraphDCP(llvm::ArrayRef<Weight> nodeWeights,
|
GraphDCP(llvm::ArrayRef<Weight> nodeWeights,
|
||||||
llvm::ArrayRef<IndexedEdge> edges,
|
llvm::ArrayRef<IndexedEdge> edges,
|
||||||
|
llvm::ArrayRef<int64_t> nodeOrderKeys = {},
|
||||||
llvm::ArrayRef<CrossbarUsage> nodeCrossbarUsage = {})
|
llvm::ArrayRef<CrossbarUsage> nodeCrossbarUsage = {})
|
||||||
: nodes(), cpuTasks(), cpuCrossbarUsage() {
|
: nodes(), cpuTasks(), cpuCrossbarUsage() {
|
||||||
assert((nodeCrossbarUsage.empty() || nodeCrossbarUsage.size() == nodeWeights.size())
|
assert((nodeCrossbarUsage.empty() || nodeCrossbarUsage.size() == nodeWeights.size())
|
||||||
&& "synthetic crossbar usage must match synthetic node weights");
|
&& "synthetic crossbar usage must match synthetic node weights");
|
||||||
|
assert((nodeOrderKeys.empty() || nodeOrderKeys.size() == nodeWeights.size())
|
||||||
|
&& "synthetic node order keys must match synthetic node weights");
|
||||||
nodes.reserve(nodeWeights.size());
|
nodes.reserve(nodeWeights.size());
|
||||||
for (auto [index, weight] : llvm::enumerate(nodeWeights))
|
for (auto [index, weight] : llvm::enumerate(nodeWeights))
|
||||||
nodes.emplace_back(index, weight, nodeCrossbarUsage.empty() ? 0 : nodeCrossbarUsage[index]);
|
nodes.emplace_back(nodeOrderKeys.empty() ? static_cast<int64_t>(index) : nodeOrderKeys[index],
|
||||||
|
weight,
|
||||||
|
nodeCrossbarUsage.empty() ? 0 : nodeCrossbarUsage[index]);
|
||||||
for (auto [start, end, weight] : edges)
|
for (auto [start, end, weight] : edges)
|
||||||
makeEdge(start, end, weight);
|
makeEdge(start, end, weight);
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -116,10 +116,9 @@ struct FoldConstantCoreMapPattern final : OpRewritePattern<linalg::MapOp> {
|
|||||||
auto globalOp = createFoldedGlobal(moduleOp, mapOp.getLoc(), initType, splatAttr, "pim_core_fill");
|
auto globalOp = createFoldedGlobal(moduleOp, mapOp.getLoc(), initType, splatAttr, "pim_core_fill");
|
||||||
|
|
||||||
OpBuilder::InsertionGuard guard(rewriter);
|
OpBuilder::InsertionGuard guard(rewriter);
|
||||||
rewriter.setInsertionPoint(coreOp);
|
|
||||||
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, mapOp.getLoc(), initType, globalOp.getName());
|
|
||||||
|
|
||||||
rewriter.setInsertionPoint(mapOp);
|
rewriter.setInsertionPoint(mapOp);
|
||||||
|
auto getGlobalOp = memref::GetGlobalOp::create(rewriter, mapOp.getLoc(), initType, globalOp.getName());
|
||||||
auto sizeInBytes = initType.getNumElements() * initType.getElementTypeBitWidth() / 8;
|
auto sizeInBytes = initType.getNumElements() * initType.getElementTypeBitWidth() / 8;
|
||||||
pim::PimMemCopyOp::create(rewriter,
|
pim::PimMemCopyOp::create(rewriter,
|
||||||
mapOp.getLoc(),
|
mapOp.getLoc(),
|
||||||
@@ -258,9 +257,18 @@ struct FoldConstantTransposePattern final : OpRewritePattern<pim::PimTransposeOp
|
|||||||
if (!resultType || !resultType.hasStaticShape())
|
if (!resultType || !resultType.hasStaticShape())
|
||||||
return failure();
|
return failure();
|
||||||
|
|
||||||
|
// Look through an optional pim.memcp_hd to find the source get_global.
|
||||||
|
// This occurs when the constant was staged into device memory before transposing.
|
||||||
|
pim::PimMemCopyHostToDevOp memcpHd;
|
||||||
auto sourceGetGlobal = transposeOp.getInput().getDefiningOp<memref::GetGlobalOp>();
|
auto sourceGetGlobal = transposeOp.getInput().getDefiningOp<memref::GetGlobalOp>();
|
||||||
|
if (!sourceGetGlobal) {
|
||||||
|
memcpHd = transposeOp.getInput().getDefiningOp<pim::PimMemCopyHostToDevOp>();
|
||||||
|
if (!memcpHd)
|
||||||
|
return failure();
|
||||||
|
sourceGetGlobal = memcpHd.getHostSource().getDefiningOp<memref::GetGlobalOp>();
|
||||||
if (!sourceGetGlobal)
|
if (!sourceGetGlobal)
|
||||||
return failure();
|
return failure();
|
||||||
|
}
|
||||||
|
|
||||||
auto moduleOp = transposeOp->getParentOfType<ModuleOp>();
|
auto moduleOp = transposeOp->getParentOfType<ModuleOp>();
|
||||||
if (!moduleOp)
|
if (!moduleOp)
|
||||||
@@ -298,13 +306,26 @@ struct FoldConstantTransposePattern final : OpRewritePattern<pim::PimTransposeOp
|
|||||||
|
|
||||||
bool isAlwaysWeight =
|
bool isAlwaysWeight =
|
||||||
!transposeOp->getUsers().empty()
|
!transposeOp->getUsers().empty()
|
||||||
&& llvm::all_of(transposeOp->getUsers(), [](Operation* user) { return isa<pim::PimCoreOp>(user); });
|
&& llvm::all_of(transposeOp->getUsers(), [](Operation* user) {
|
||||||
|
return isa<pim::PimCoreOp, pim::PimCoreBatchOp>(user);
|
||||||
|
});
|
||||||
if (isAlwaysWeight) {
|
if (isAlwaysWeight) {
|
||||||
markWeightAlways(newGlobal);
|
markWeightAlways(newGlobal);
|
||||||
markWeightAlways(newGetGlobal);
|
markWeightAlways(newGetGlobal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto outputAllocOp = transposeOp.getOutputBuffer().getDefiningOp<memref::AllocOp>();
|
||||||
rewriter.replaceOp(transposeOp, newGetGlobal.getResult());
|
rewriter.replaceOp(transposeOp, newGetGlobal.getResult());
|
||||||
|
|
||||||
|
if (memcpHd && memcpHd.use_empty()) {
|
||||||
|
auto deviceAllocOp = memcpHd.getDeviceTarget().getDefiningOp<memref::AllocOp>();
|
||||||
|
rewriter.eraseOp(memcpHd);
|
||||||
|
if (deviceAllocOp && deviceAllocOp->use_empty())
|
||||||
|
rewriter.eraseOp(deviceAllocOp);
|
||||||
|
}
|
||||||
|
if (outputAllocOp && outputAllocOp->use_empty())
|
||||||
|
rewriter.eraseOp(outputAllocOp);
|
||||||
|
|
||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -341,18 +362,25 @@ struct FoldConstantAllocPattern final : OpRewritePattern<memref::AllocOp> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isa<pim::PimCoreOp>(user))
|
if (!isa<pim::PimCoreOp, pim::PimCoreBatchOp>(user))
|
||||||
return failure();
|
return failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!llvm::all_of(castsToReplace, [](memref::CastOp castOp) {
|
if (!llvm::all_of(castsToReplace, [](memref::CastOp castOp) {
|
||||||
return llvm::all_of(castOp->getUsers(), [](Operation* user) { return isa<pim::PimCoreOp>(user); });
|
return llvm::all_of(castOp->getUsers(), [](Operation* user) {
|
||||||
|
return isa<pim::PimCoreOp, pim::PimCoreBatchOp>(user);
|
||||||
|
});
|
||||||
})) {
|
})) {
|
||||||
allLiveUsersAreCoreOps = false;
|
allLiveUsersAreCoreOps = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!llvm::all_of(allocOp->getUsers(), [](Operation* user) {
|
if (!llvm::all_of(allocOp->getUsers(), [](Operation* user) {
|
||||||
return isa<linalg::MapOp, memref::SubViewOp, memref::DeallocOp, memref::CastOp, pim::PimCoreOp>(user);
|
return isa<linalg::MapOp,
|
||||||
|
memref::SubViewOp,
|
||||||
|
memref::DeallocOp,
|
||||||
|
memref::CastOp,
|
||||||
|
pim::PimCoreOp,
|
||||||
|
pim::PimCoreBatchOp>(user);
|
||||||
})) {
|
})) {
|
||||||
return failure();
|
return failure();
|
||||||
}
|
}
|
||||||
@@ -389,6 +417,83 @@ struct FoldConstantAllocPattern final : OpRewritePattern<memref::AllocOp> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct FoldConstantHostCopyPattern final : OpRewritePattern<memref::CopyOp> {
|
||||||
|
using OpRewritePattern::OpRewritePattern;
|
||||||
|
|
||||||
|
LogicalResult matchAndRewrite(memref::CopyOp copyOp, PatternRewriter& rewriter) const override {
|
||||||
|
if (copyOp->getParentOfType<pim::PimCoreOp>())
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
auto allocOp = copyOp.getTarget().getDefiningOp<memref::AllocOp>();
|
||||||
|
if (!allocOp)
|
||||||
|
return failure();
|
||||||
|
auto allocType = dyn_cast<MemRefType>(allocOp.getType());
|
||||||
|
if (!allocType || !allocType.hasStaticShape())
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
auto srcSubview = getStaticSubviewInfo(copyOp.getSource());
|
||||||
|
Value globalSource = succeeded(srcSubview) ? srcSubview->source : stripMemRefCasts(copyOp.getSource());
|
||||||
|
|
||||||
|
auto moduleOp = copyOp->getParentOfType<ModuleOp>();
|
||||||
|
if (!moduleOp)
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
auto denseAttr = getDenseGlobalValue(moduleOp, globalSource);
|
||||||
|
if (failed(denseAttr))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
DenseElementsAttr foldedAttr;
|
||||||
|
if (succeeded(srcSubview)) {
|
||||||
|
if (llvm::any_of(srcSubview->strides, [](int64_t stride) { return stride != 1; }))
|
||||||
|
return failure();
|
||||||
|
auto staticOffsets = getStaticSubviewOffsets(*srcSubview);
|
||||||
|
if (failed(staticOffsets))
|
||||||
|
return failure();
|
||||||
|
|
||||||
|
auto maybeFoldedAttr = foldDenseSubview(*denseAttr, *staticOffsets, allocType.getShape());
|
||||||
|
if (failed(maybeFoldedAttr))
|
||||||
|
return failure();
|
||||||
|
foldedAttr = *maybeFoldedAttr;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
auto resultTensorType = RankedTensorType::get(allocType.getShape(), allocType.getElementType());
|
||||||
|
if (resultTensorType != denseAttr->getType())
|
||||||
|
return failure();
|
||||||
|
foldedAttr = *denseAttr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool allLiveUsersAreCores = true;
|
||||||
|
for (Operation* user : allocOp->getUsers()) {
|
||||||
|
if (user == copyOp)
|
||||||
|
continue;
|
||||||
|
if (isa<memref::DeallocOp>(user))
|
||||||
|
continue;
|
||||||
|
if (isa<pim::PimCoreOp, pim::PimCoreBatchOp>(user))
|
||||||
|
continue;
|
||||||
|
if (isa<memref::SubViewOp>(user)) {
|
||||||
|
allLiveUsersAreCores = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto newGlobal = createFoldedGlobal(moduleOp, allocOp.getLoc(), allocType, foldedAttr, "pim_folded_host_copy");
|
||||||
|
if (allLiveUsersAreCores)
|
||||||
|
markWeightAlways(newGlobal);
|
||||||
|
|
||||||
|
rewriter.setInsertionPoint(allocOp);
|
||||||
|
auto newGetGlobal = memref::GetGlobalOp::create(rewriter, allocOp.getLoc(), allocType, newGlobal.getName());
|
||||||
|
if (allLiveUsersAreCores)
|
||||||
|
markWeightAlways(newGetGlobal);
|
||||||
|
|
||||||
|
rewriter.replaceAllUsesWith(allocOp.getResult(), newGetGlobal.getResult());
|
||||||
|
rewriter.eraseOp(copyOp);
|
||||||
|
if (allocOp.use_empty())
|
||||||
|
rewriter.eraseOp(allocOp);
|
||||||
|
return success();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct FoldConstantMemCpPattern final : OpRewritePattern<pim::PimMemCopyOp> {
|
struct FoldConstantMemCpPattern final : OpRewritePattern<pim::PimMemCopyOp> {
|
||||||
using OpRewritePattern::OpRewritePattern;
|
using OpRewritePattern::OpRewritePattern;
|
||||||
|
|
||||||
@@ -443,7 +548,7 @@ struct FoldConstantMemCpPattern final : OpRewritePattern<pim::PimMemCopyOp> {
|
|||||||
continue;
|
continue;
|
||||||
if (isa<memref::DeallocOp>(user))
|
if (isa<memref::DeallocOp>(user))
|
||||||
continue;
|
continue;
|
||||||
if (isa<pim::PimCoreOp>(user))
|
if (isa<pim::PimCoreOp, pim::PimCoreBatchOp>(user))
|
||||||
continue;
|
continue;
|
||||||
if (isa<memref::SubViewOp>(user)) {
|
if (isa<memref::SubViewOp>(user)) {
|
||||||
allLiveUsersAreCores = false;
|
allLiveUsersAreCores = false;
|
||||||
@@ -473,7 +578,11 @@ struct FoldConstantMemCpPattern final : OpRewritePattern<pim::PimMemCopyOp> {
|
|||||||
|
|
||||||
void populateConstantFoldingConstantPatterns(RewritePatternSet& patterns) {
|
void populateConstantFoldingConstantPatterns(RewritePatternSet& patterns) {
|
||||||
patterns
|
patterns
|
||||||
.add<FoldConstantTransposePattern, FoldConstantAllocPattern, FoldConstantCoreMapPattern, FoldConstantMemCpPattern>(
|
.add<FoldConstantTransposePattern,
|
||||||
|
FoldConstantAllocPattern,
|
||||||
|
FoldConstantCoreMapPattern,
|
||||||
|
FoldConstantHostCopyPattern,
|
||||||
|
FoldConstantMemCpPattern>(
|
||||||
patterns.getContext());
|
patterns.getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,26 @@ static bool isAddressOnlyHostOp(Operation* op) {
|
|||||||
memref::CastOp,
|
memref::CastOp,
|
||||||
memref::CollapseShapeOp,
|
memref::CollapseShapeOp,
|
||||||
memref::ExpandShapeOp,
|
memref::ExpandShapeOp,
|
||||||
spatial::SpatChannelNewOp>(op);
|
memref::CopyOp>(op);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Looser than isCodegenAddressableValue: follows view ops without requiring contiguity.
|
||||||
|
// Used for memref.copy operands which may be non-contiguous subviews.
|
||||||
|
static bool isBaseAddressableValue(Value value) {
|
||||||
|
while (true) {
|
||||||
|
if (isa<BlockArgument>(value))
|
||||||
|
return true;
|
||||||
|
Operation* defOp = value.getDefiningOp();
|
||||||
|
if (!defOp)
|
||||||
|
return false;
|
||||||
|
if (isa<memref::AllocOp, memref::GetGlobalOp>(defOp))
|
||||||
|
return true;
|
||||||
|
if (auto subview = dyn_cast<memref::SubViewOp>(defOp)) { value = subview.getSource(); continue; }
|
||||||
|
if (auto cast = dyn_cast<memref::CastOp>(defOp)) { value = cast.getSource(); continue; }
|
||||||
|
if (auto collapse = dyn_cast<memref::CollapseShapeOp>(defOp)) { value = collapse.getSrc(); continue; }
|
||||||
|
if (auto expand = dyn_cast<memref::ExpandShapeOp>(defOp)) { value = expand.getSrc(); continue; }
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isCodegenAddressableValue(Value value) {
|
static bool isCodegenAddressableValue(Value value) {
|
||||||
@@ -38,6 +57,8 @@ static bool isCodegenAddressableValue(Value value) {
|
|||||||
static bool isExplicitHostOperand(Operation* op, unsigned operandIndex) {
|
static bool isExplicitHostOperand(Operation* op, unsigned operandIndex) {
|
||||||
if (isa<pim::PimMemCopyHostToDevOp>(op))
|
if (isa<pim::PimMemCopyHostToDevOp>(op))
|
||||||
return operandIndex == 1;
|
return operandIndex == 1;
|
||||||
|
if (isa<pim::PimMemCopyHostToDevBatchOp>(op))
|
||||||
|
return operandIndex == 1;
|
||||||
if (isa<pim::PimMemCopyDevToHostOp>(op))
|
if (isa<pim::PimMemCopyDevToHostOp>(op))
|
||||||
return operandIndex == 0;
|
return operandIndex == 0;
|
||||||
return false;
|
return false;
|
||||||
@@ -69,6 +90,12 @@ struct VerificationPass : PassWrapper<VerificationPass, OperationPass<ModuleOp>>
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (auto coreBatchOp = dyn_cast<pim::PimCoreBatchOp>(&op)) {
|
||||||
|
if (failed(verifyCoreWeights(moduleOp, coreBatchOp)) || failed(verifyCoreOperands(coreBatchOp)))
|
||||||
|
hasFailure = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (auto returnOp = dyn_cast<func::ReturnOp>(&op)) {
|
if (auto returnOp = dyn_cast<func::ReturnOp>(&op)) {
|
||||||
if (failed(verifyReturnOp(returnOp)))
|
if (failed(verifyReturnOp(returnOp)))
|
||||||
hasFailure = true;
|
hasFailure = true;
|
||||||
@@ -92,10 +119,11 @@ struct VerificationPass : PassWrapper<VerificationPass, OperationPass<ModuleOp>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static LogicalResult verifyCoreWeights(ModuleOp moduleOp, pim::PimCoreOp coreOp) {
|
template <typename CoreOpTy>
|
||||||
|
static LogicalResult verifyCoreWeights(ModuleOp moduleOp, CoreOpTy coreOp) {
|
||||||
bool hasFailure = false;
|
bool hasFailure = false;
|
||||||
for (auto [weightIndex, weight] : llvm::enumerate(coreOp.getWeights())) {
|
for (auto [weightIndex, weight] : llvm::enumerate(coreOp.getWeights())) {
|
||||||
auto getGlobalOp = weight.getDefiningOp<memref::GetGlobalOp>();
|
auto getGlobalOp = weight.template getDefiningOp<memref::GetGlobalOp>();
|
||||||
if (!getGlobalOp) {
|
if (!getGlobalOp) {
|
||||||
coreOp.emitOpError() << "weight #" << weightIndex
|
coreOp.emitOpError() << "weight #" << weightIndex
|
||||||
<< " must be materialized as memref.get_global before JSON codegen";
|
<< " must be materialized as memref.get_global before JSON codegen";
|
||||||
@@ -131,7 +159,8 @@ private:
|
|||||||
return success(!hasFailure);
|
return success(!hasFailure);
|
||||||
}
|
}
|
||||||
|
|
||||||
static LogicalResult verifyCoreOperands(pim::PimCoreOp coreOp) {
|
template <typename CoreOpTy>
|
||||||
|
static LogicalResult verifyCoreOperands(CoreOpTy coreOp) {
|
||||||
return walkPimCoreBlock(
|
return walkPimCoreBlock(
|
||||||
coreOp.getBody().front(), StaticValueKnowledge {}, [](Operation& op, const StaticValueKnowledge& knowledge) {
|
coreOp.getBody().front(), StaticValueKnowledge {}, [](Operation& op, const StaticValueKnowledge& knowledge) {
|
||||||
bool hasFailure = false;
|
bool hasFailure = false;
|
||||||
@@ -174,6 +203,13 @@ private:
|
|||||||
return verifyAddressOnlySource(op, collapseOp.getSrc());
|
return verifyAddressOnlySource(op, collapseOp.getSrc());
|
||||||
if (auto expandOp = dyn_cast<memref::ExpandShapeOp>(op))
|
if (auto expandOp = dyn_cast<memref::ExpandShapeOp>(op))
|
||||||
return verifyAddressOnlySource(op, expandOp.getSrc());
|
return verifyAddressOnlySource(op, expandOp.getSrc());
|
||||||
|
if (auto copyOp = dyn_cast<memref::CopyOp>(op)) {
|
||||||
|
if (!isBaseAddressableValue(copyOp.getSource()) || !isBaseAddressableValue(copyOp.getTarget())) {
|
||||||
|
op->emitOpError("depends on a value that is not backed by addressable storage");
|
||||||
|
return failure();
|
||||||
|
}
|
||||||
|
return success();
|
||||||
|
}
|
||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class ValidationResult:
|
|||||||
|
|
||||||
|
|
||||||
class ProgressReporter:
|
class ProgressReporter:
|
||||||
def __init__(self, total_models, stages_per_model=STAGE_COUNT):
|
def __init__(self, total_models, stages_per_model=STAGE_COUNT, enabled=None):
|
||||||
self.total_models = total_models
|
self.total_models = total_models
|
||||||
self.stages_per_model = stages_per_model
|
self.stages_per_model = stages_per_model
|
||||||
self.total_steps = max(1, total_models * stages_per_model)
|
self.total_steps = max(1, total_models * stages_per_model)
|
||||||
@@ -45,7 +45,7 @@ class ProgressReporter:
|
|||||||
self.passed_models = 0
|
self.passed_models = 0
|
||||||
self.failed_models = 0
|
self.failed_models = 0
|
||||||
self.current_label = ""
|
self.current_label = ""
|
||||||
self.enabled = True
|
self.enabled = sys.stdout.isatty() if enabled is None else enabled
|
||||||
self.columns = shutil.get_terminal_size((100, 20)).columns
|
self.columns = shutil.get_terminal_size((100, 20)).columns
|
||||||
self.suspended = False
|
self.suspended = False
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user