Skip to content

Commit 3c31c84

Browse files
authored
[mlir][tosa] Allow dynamic dims in --tosa-validate pass (llvm#171463)
This commit allows tensor dimensions to be dynamic when the specified target TOSA specification version is `1.1.draft` or higher. This is because this version of the specification supports representation operations that are dynamic until backend compile time.
1 parent 9741a84 commit 3c31c84

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,22 @@ LogicalResult TosaValidation::levelCheckSize(Operation *op,
688688
return op->emitOpError() << "failed level check: unranked tensor";
689689
auto shape = type.getShape();
690690
for (auto dim : shape) {
691-
if (mlir::ShapedType::isDynamic(dim))
691+
const bool dimIsDynamic = mlir::ShapedType::isDynamic(dim);
692+
const TosaSpecificationVersion targetVersion = targetEnv.getSpecVersion();
693+
const TosaSpecificationVersion minRequiredVersion(1, 1);
694+
if (targetVersion.isBackwardsCompatibleWith(minRequiredVersion) &&
695+
dimIsDynamic)
696+
// TOSA 1.1 and above supports dynamic dimensions, however, they must be
697+
// resolved at backend compile time. Runtime dynamism is not currently
698+
// supported. Checking this requirement is met is delegated to backends.
699+
return success();
700+
701+
// When targeting TOSA 1.0 or below, dynamic dims are not supported
702+
if (dimIsDynamic)
692703
return op->emitOpError() << "failed level check: " << operandOrResult
693-
<< " shape dimension cannot be dynamic";
704+
<< " shape dimension cannot be dynamic when"
705+
<< " targeting TOSA specification version 1.0"
706+
<< " or below";
694707
}
695708

696709
int64_t element_bits = tosa::getBitWidth(getElementTypeOrSelf(type));

mlir/test/Dialect/Tosa/tosa-validation-version-1p0-invalid.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ func.func @test_matmul_fp8_input_fp32_acc_type(%arg0: tensor<1x14x19xf8E4M3FN>,
1919
%0 = tosa.matmul %arg0, %arg1, %azp0, %bzp0 : (tensor<1x14x19xf8E4M3FN>, tensor<1x19x28xf8E4M3FN>, tensor<1xf8E4M3FN>, tensor<1xf8E4M3FN>) -> tensor<1x14x28xf32>
2020
return %0 : tensor<1x14x28xf32>
2121
}
22+
23+
// -----
24+
25+
func.func @test_dyanmic_dims(%arg0: tensor<?x8x16xi8>) -> tensor<?x16xi32> {
26+
// expected-error@+1 {{'tosa.argmax' op failed level check: operand shape dimension cannot be dynamic when targeting TOSA specification version 1.0 or below}}
27+
%0 = tosa.argmax %arg0 { axis = 1 : i32 } : (tensor<?x8x16xi8>) -> tensor<?x16xi32>
28+
return %0 : tensor<?x16xi32>
29+
}

mlir/test/Dialect/Tosa/tosa-validation-version-1p1-valid.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,11 @@ func.func @test_scatter_const_indices_int64(%arg0: tensor<2x52x3xf32>, %arg2: te
140140
%0 = tosa.scatter %arg0, %indices, %arg2 : (tensor<2x52x3xf32>, tensor<2x12xi64>, tensor<2x12x3xf32>) -> tensor<2x52x3xf32>
141141
return %0 : tensor<2x52x3xf32>
142142
}
143+
144+
// -----
145+
146+
// CHECK-LABEL: test_dynamic_dims
147+
func.func @test_dynamic_dims(%arg0: tensor<?x8x16xi8>) -> tensor<?x16xi32> {
148+
%0 = tosa.argmax %arg0 { axis = 1 : i32 } : (tensor<?x8x16xi8>) -> tensor<?x16xi32>
149+
return %0 : tensor<?x16xi32>
150+
}

0 commit comments

Comments
 (0)