Skip to content

Commit e141181

Browse files
committed
[mlir][tosa] Fix lowering of tosa.conv2d
The lowering of tosa.conv2d produces an illegal tensor.empty operation where the number of inputs do not match the number of dynamic dimensions in the output type. The fix is to base the generation of tensor.dim operations off the result type of the conv2d operation, rather than the input type. The problem and fix are very similar to this fix #72724 but for convolution.
1 parent 65aab9e commit e141181

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamed.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static SmallVector<Value> inferDynamicDimsForConv(
136136
for (uint32_t i = 0, s = inputSizeDims.size(); i < s; ++i) {
137137
int64_t inputDim = inputSizeDims[i];
138138
int64_t kernelDim = kernelSizeDims[i];
139-
if (inputTy.isDynamicDim(inputDim)) {
139+
if (resultTy.isDynamicDim(inputDim)) {
140140
auto padTop = padAttr[i * 2];
141141
auto padBottom = padAttr[i * 2 + 1];
142142
auto stride = strideAttr[i];
@@ -153,7 +153,7 @@ static SmallVector<Value> inferDynamicDimsForConv(
153153

154154
// Get the batch/channels dimensions.
155155
for (int i = 0; i < inputRank; i++) {
156-
if (inputTy.isDynamicDim(i) && !dynDims[i])
156+
if (resultTy.isDynamicDim(i) && !dynDims[i])
157157
dynDims[i] = rewriter.create<tensor::DimOp>(loc, input, i);
158158
}
159159

mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-named.mlir

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,29 @@ func.func @conv2d_dyn_w_h(%input: tensor<1x?x?x27xf32>, %weights: tensor<28x3x3x
497497

498498
// -----
499499

500+
// CHECK: [[$MAP1:.+]] = affine_map<(d0, d1, d2, d3) -> (d3)>
501+
// CHECK: [[$MAP2:.+]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
502+
503+
func.func @conv2d_dyn_output(%input: tensor<2x6x5x4xf32>, %weights: tensor<4x3x3x4xf32>, %bias: tensor<4xf32>) {
504+
// %[[C0:.+]] = arith.constant 0 : index
505+
// %[[DIM0:.+]] = tensor.dim %input, %[[C0]] : tensor<2x6x5x4xf32>
506+
// %[[INIT_CONV:.+]] = tensor.empty(%[[DIM0]]) : tensor<?x4x3x4xf32>
507+
// %[[ZERO:.+]] = arith.constant 0.000000e+00 : f32
508+
// %[[FILL:.+]] = linalg.fill
509+
// %[[INIT_GENERIC:.+]] = tensor.empty([[DIM0]]) : tensor<?x4x3x4xf32>
510+
511+
// %[[CONV:.+]] = linalg.conv_2d_nhwc_fhwc {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>} ins(%arg0, %arg1 : tensor<2x6x5x4xf32>, tensor<4x3x3x4xf32>) outs(%[[INIT_CONV]] : tensor<?x4x3x4xf32>) -> tensor<?x4x3x4xf32>
512+
// linalg.generic {indexing_maps = [#[[MAP1]], #[[MAP2]], #[[MAP2]]], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%arg2, %[[CONV]] : tensor<4xf32>, tensor<?x4x3x4xf32>) outs(%[[INIT_GENERIC]] : tensor<?x4x3x4xf32>) {
513+
// %[[ADD:.+]] = arith.addf
514+
// linalg.yield %[[ADD]] : f32
515+
// } -> tensor<?x4x3x4xf32>
516+
517+
%0 = tosa.conv2d %input, %weights, %bias {dilation = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 1>} : (tensor<2x6x5x4xf32 >, tensor<4x3x3x4xf32>, tensor<4xf32>) -> tensor<?x4x3x4xf32>
518+
return
519+
}
520+
521+
// -----
522+
500523
// CHECK-LABEL: @conv2d_padded_f32
501524
func.func @conv2d_padded_f32(%input: tensor<1x47x40x28xf32>, %weights: tensor<28x3x3x28xf32>, %bias: tensor<28xf32>) -> () {
502525
// CHECK: %[[C0:.+]] = arith.constant 0

0 commit comments

Comments
 (0)