Skip to content

Commit dcf27be

Browse files
lhutton1Jerry-Ge
authored andcommitted
[mlir][tosa] Fold 'small' constant 1D concat operations
The commit improves the concat folder to cover operations consisting of all constant inputs where the number of output values does not exceed 6. Keeping the folder restricted to small inputs avoids a large folder runtime or increased memory requirements. This folder is useful in the context of legalizing dynamic models where the input shapes are resolved to static directly before legalization. In this context, constant shape operations are used over tensors of num elements <= 6 (tosa_level_8k MAX_RANK). Change-Id: Ieb522fc1d0d1ec4596ce060aa9ab76439322d6d6 Signed-off-by: Luke Hutton <[email protected]>
1 parent 74c6111 commit dcf27be

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,16 +1226,54 @@ OpFoldResult tosa::AbsOp::fold(FoldAdaptor adaptor) {
12261226
}
12271227

12281228
OpFoldResult ConcatOp::fold(FoldAdaptor adaptor) {
1229+
const auto operands = getOperands();
1230+
const unsigned int numOperands = getNumOperands();
1231+
1232+
// Fold concat when all operands are constant and the output is 'small'
1233+
auto hasAllConstOperands = [](Value v) {
1234+
return llvm::dyn_cast_or_null<tosa::ConstOp>(v.getDefiningOp());
1235+
};
1236+
if (llvm::all_of(operands, hasAllConstOperands)) {
1237+
const ShapedType outputType = dyn_cast<ShapedType>(getOutput().getType());
1238+
if (!outputType || !outputType.hasStaticShape()) {
1239+
return {};
1240+
}
1241+
1242+
// A 'small' output is currently defined as 1D and <= 6 elements
1243+
// (tosa_level_8k MAX_RANK)
1244+
if (outputType.getRank() != 1) {
1245+
return {};
1246+
}
1247+
const int64_t outputNumElements = outputType.getNumElements();
1248+
if (outputNumElements > 6) {
1249+
return {};
1250+
}
1251+
1252+
llvm::SmallVector<Attribute> constOperands;
1253+
constOperands.reserve(outputNumElements);
1254+
for (const Attribute operand : adaptor.getOperands()) {
1255+
const auto elementsAttr =
1256+
llvm::dyn_cast_if_present<DenseElementsAttr>(operand);
1257+
if (!elementsAttr) {
1258+
return {};
1259+
}
1260+
constOperands.append(
1261+
llvm::to_vector(elementsAttr.getValues<Attribute>()));
1262+
}
1263+
1264+
return DenseElementsAttr::get(outputType, constOperands);
1265+
}
1266+
12291267
// Fold consecutive concats on the same axis into a single op.
12301268
// Keep track of the operands so we are able to construct a new concat
12311269
// later. Conservatively assume that we double the number of operands when
12321270
// folding
12331271
SmallVector<Value, 8> concatOperands;
1234-
concatOperands.reserve(2 * getNumOperands());
1272+
concatOperands.reserve(2 * numOperands);
12351273

12361274
// Find all operands that are foldable concats
12371275
bool foundFoldableConcat = false;
1238-
for (Value operand : getOperands()) {
1276+
for (Value operand : operands) {
12391277
concatOperands.emplace_back(operand);
12401278

12411279
auto producer = dyn_cast_or_null<ConcatOp>(operand.getDefiningOp());

mlir/test/Dialect/Tosa/fold_concats.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,16 @@ func.func @partially_foldable(%arg0: tensor<1x1x8x8xf32>, %arg1: tensor<1x2x4x8x
9191
// CHECK: %[[VAL_3:.*]] = tosa.concat %[[VAL_0]], %[[VAL_0]], %[[VAL_2]] {axis = 1 : i32} : (tensor<1x1x8x8xf32>, tensor<1x1x8x8xf32>, tensor<1x2x8x8xf32>) -> tensor<1x4x8x8xf32>
9292
// CHECK: return %[[VAL_3]] : tensor<1x4x8x8xf32>
9393
// CHECK: }
94+
95+
// -----
96+
97+
// CHECK-LABEL: test_fold_small_const_concat
98+
func.func @test_fold_small_const_concat() -> tensor<6xi8> {
99+
// CHECK-DAG: %[[VAL_0:.*]] = "tosa.const"() <{value = dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi8>}> : () -> tensor<6xi8>
100+
// CHECK: return %[[VAL_0]] : tensor<6xi8>
101+
%0 = "tosa.const"() <{value = dense<[1, 2]> : tensor<2xi8>}> : () -> tensor<2xi8>
102+
%1 = "tosa.const"() <{value = dense<[3, 4, 5]> : tensor<3xi8>}> : () -> tensor<3xi8>
103+
%2 = "tosa.const"() <{value = dense<6> : tensor<1xi8>}> : () -> tensor<1xi8>
104+
%3 = "tosa.concat"(%0, %1, %2) <{axis = 0 : i32}> : (tensor<2xi8>, tensor<3xi8>, tensor<1xi8>) -> tensor<6xi8>
105+
func.return %3 : tensor<6xi8>
106+
}

0 commit comments

Comments
 (0)