Skip to content

[mlir][TOSA] Fix shape inference when operand was inferred #66906

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlir/include/mlir/Interfaces/InferTypeOpInterface.td
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def InferTypeOpAdaptorWithIsCompatible : InferTypeOpAdaptorBase<
>;

// Convenient trait to define a wrapper to inferReturnTypeComponents that passes
// in the Op Adaptor directly
// in the Op Adaptor directly. Only uses the current types of the operands.
class InferShapedTypeOpAdaptorBase<list<string> overridenMethods = []> : TraitList<
[
// Op implements infer type op interface.
Expand Down
56 changes: 15 additions & 41 deletions mlir/lib/Dialect/Tosa/Transforms/TosaInferShapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ namespace {

void propagateShapesInRegion(Region &region);

void propagateShapesToTosaIf(
Operation &op, DenseMap<Value, ShapedTypeComponents> &shapesStorage) {
void propagateShapesToTosaIf(Operation &op) {
IfOp ifOp = dyn_cast<IfOp>(op);
if (!ifOp)
return;
Expand All @@ -53,12 +52,12 @@ void propagateShapesToTosaIf(
return;

for (unsigned int i = 1, s = op.getNumOperands(); i < s; i++) {
auto inferredTy = shapesStorage[op.getOperand(i)];
auto inferredTy = cast<ShapedType>(op.getOperand(i).getType());
auto blockArg = frontBlock.getArgument(i - 1);
auto oldType = cast<ShapedType>(blockArg.getType());

if (inferredTy.hasRank()) {
Type newType = oldType.clone(inferredTy.getDims());
Type newType = oldType.clone(inferredTy.getShape());
blockArg.setType(newType);
}
}
Expand All @@ -79,8 +78,7 @@ void propagateShapesToTosaIf(
}
}

void propagateShapesToTosaWhile(
Operation &op, DenseMap<Value, ShapedTypeComponents> &shapesStorage) {
void propagateShapesToTosaWhile(Operation &op) {
WhileOp whileOp = dyn_cast<WhileOp>(op);
if (!whileOp)
return;
Expand All @@ -91,9 +89,8 @@ void propagateShapesToTosaWhile(
llvm::SmallVector<Type> argTypes;
for (auto operand : op.getOperands()) {
auto operandTy = cast<ShapedType>(operand.getType());
auto shapedTypeComponent = shapesStorage[operand];
if (shapedTypeComponent.hasRank()) {
auto newTy = operandTy.clone(shapedTypeComponent.getDims());
if (operandTy.hasRank()) {
auto newTy = operandTy.clone(operandTy.getShape());
argTypes.push_back(newTy);
} else {
argTypes.push_back(operand.getType());
Expand Down Expand Up @@ -187,21 +184,6 @@ void propagateShapesToTosaWhile(
}

void propagateShapesInRegion(Region &region) {
DenseMap<Value, ShapedTypeComponents> shapesStorage;
auto setShapes = [&](Value val, Type t) {
if (auto st = dyn_cast<ShapedType>(t))
shapesStorage[val] = st;
else
shapesStorage[val] = t;
};
auto operandShape = [&](Value val) -> ShapeAdaptor {
// Query the WIP mapping rather than the type if set.
auto it = shapesStorage.find(val);
if (it == shapesStorage.end())
return nullptr;
return it->second;
};

// Check whether this use case is replaceable. We define an op as
// being replaceable if it is used by a ReturnOp, a TosaOp, or an op with a
// type-inference related interface.
Expand All @@ -217,8 +199,8 @@ void propagateShapesInRegion(Region &region) {
if (op.getDialect()->getNamespace() != TosaDialect::getDialectNamespace())
continue;

propagateShapesToTosaIf(op, shapesStorage);
propagateShapesToTosaWhile(op, shapesStorage);
propagateShapesToTosaIf(op);
propagateShapesToTosaWhile(op);

InferShapedTypeOpInterface shapeInterface =
dyn_cast<InferShapedTypeOpInterface>(op);
Expand All @@ -227,12 +209,11 @@ void propagateShapesInRegion(Region &region) {

SmallVector<ShapedTypeComponents> returnedShapes;

ValueShapeRange range(op.getOperands(), operandShape);
if (shapeInterface
.inferReturnTypeComponents(op.getContext(), op.getLoc(), range,
op.getDiscardableAttrDictionary(),
op.getPropertiesStorage(),
op.getRegions(), returnedShapes)
.inferReturnTypeComponents(
op.getContext(), op.getLoc(), op.getOperands(),
op.getDiscardableAttrDictionary(), op.getPropertiesStorage(),
op.getRegions(), returnedShapes)
.succeeded()) {
for (auto it : llvm::zip(op.getResults(), returnedShapes)) {
Value result = std::get<0>(it);
Expand Down Expand Up @@ -262,20 +243,13 @@ void propagateShapesInRegion(Region &region) {
ValueKnowledge::join(currentKnowledge, inferredKnowledge);
if (!newKnowledge)
continue;
setShapes(result, newKnowledge.getType());

// Set new type
result.setType(newKnowledge.getType());
}
}
}
}

// Actually update types with updated shape knowledge.
for (auto it : shapesStorage) {
auto result = it.second;
if (result.hasRank()) {
Type t = cast<ShapedType>(it.first.getType()).clone(result.getDims());
it.first.setType(t);
}
}
}

/// Pass that performs shape propagation across TOSA operations. This includes
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1259,3 +1259,16 @@ func.func @test_non_tosa_consumer_extract(%arg0: tensor<4x4xf32>, %arg1: index)
%1 = tensor.extract %0[%arg1, %arg1] : tensor<?x?xf32>
return %1 : f32
}

// -----

// CHECK-LABEL: test_tosa_use_def_chain
func.func @test_tosa_use_def_chain(%arg0: tensor<1x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>) -> tensor<?x16x16x16xf32> {
// CHECK: [[CONV:%.+]] = tosa.conv2d %arg0, %arg1, %arg2
// CHECK: (tensor<1x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<1x32x32x16xf32>
%0 = tosa.conv2d %arg0, %arg1, %arg2 {dilation = array<i64: 1, 1>, pad = array<i64: 1, 1, 1, 1>, stride = array<i64: 1, 1>} : (tensor<1x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<?x32x32x16xf32>
// CHECK: tosa.max_pool2d [[CONV]]
// CHECK: (tensor<1x32x32x16xf32>) -> tensor<1x16x16x16xf32>
%1 = tosa.max_pool2d %0 {kernel = array<i64: 2, 2>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 2, 2>} : (tensor<?x32x32x16xf32>) -> tensor<?x16x16x16xf32>
return %1 : tensor<?x16x16x16xf32>
}