[Mlir-commits] [mlir] [mlir][TOSA] Fix linalg lowering of depthwise conv2d (PR #130282)
Thomas Preud'homme
llvmlistbot at llvm.org
Fri Mar 7 05:20:48 PST 2025
https://github.com/RoboTux created https://github.com/llvm/llvm-project/pull/130282
Current lowering for tosa.depthwise_conv2d assumes if both zero points
are zero then it's a floating-point operation by hardcoding the use of a
arith.addf in the lowered code. Fix code to check for the element type
to decide what add operation to use.
>From 874f873863b756b212f7ee95037f6a66dab6327b Mon Sep 17 00:00:00 2001
From: Thomas Preud'homme <thomas.preudhomme at arm.com>
Date: Fri, 7 Mar 2025 13:15:12 +0000
Subject: [PATCH] [mlir][TOSA] Fix linalg lowering of depthwise conv2d
Current lowering for tosa.depthwise_conv2d assumes if both zero points
are zero then it's a floating-point operation by hardcoding the use of a
arith.addf in the lowered code. Fix code to check for the element type
to decide what add operation to use.
---
.../TosaToLinalg/TosaToLinalgNamed.cpp | 35 +++++++++----------
.../TosaToLinalg/tosa-to-linalg-named.mlir | 27 +++++++++++++-
2 files changed, 43 insertions(+), 19 deletions(-)
diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamed.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamed.cpp
index 2a2589e19d0ac..c8c1975177b90 100644
--- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamed.cpp
+++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamed.cpp
@@ -477,27 +477,21 @@ class DepthwiseConvConverter
return rewriter.notifyMatchFailure(
op, "weight zero point must be zero for non-int8 integer types");
- bool hasZp = (inputZpVal != 0) || (weightZpVal != 0);
auto weightShape = weightTy.getShape();
auto resultShape = resultTy.getShape();
// Apply padding as necessary.
- TypedAttr zeroAttr = rewriter.getZeroAttr(inputETy);
- if (hasZp) {
- int64_t intMin =
- APInt::getSignedMinValue(inputETy.getIntOrFloatBitWidth())
- .getSExtValue();
- int64_t intMax =
- APInt::getSignedMaxValue(inputETy.getIntOrFloatBitWidth())
- .getSExtValue();
+ int64_t intMin = APInt::getSignedMinValue(inputETy.getIntOrFloatBitWidth())
+ .getSExtValue();
+ int64_t intMax = APInt::getSignedMaxValue(inputETy.getIntOrFloatBitWidth())
+ .getSExtValue();
- if (inputZpVal < intMin || inputZpVal > intMax)
- return rewriter.notifyMatchFailure(
- op, "tosa.depthwise_conv op quantization has zp outside of input "
- "range");
+ if (inputZpVal < intMin || inputZpVal > intMax)
+ return rewriter.notifyMatchFailure(
+ op, "tosa.depthwise_conv op quantization has zp outside of input "
+ "range");
- zeroAttr = rewriter.getIntegerAttr(inputETy, inputZpVal);
- }
+ TypedAttr zeroAttr = rewriter.getIntegerAttr(inputETy, inputZpVal);
llvm::SmallVector<int64_t> pad;
pad.resize(2, 0);
@@ -536,7 +530,7 @@ class DepthwiseConvConverter
indexingMaps.push_back(rewriter.getMultiDimIdentityMap(resultRank));
indexingMaps.push_back(rewriter.getMultiDimIdentityMap(resultRank));
- if (!hasZp) {
+ if (inputZpVal == 0 && weightZpVal == 0) {
Value conv = rewriter
.create<linalg::DepthwiseConv2DNhwcHwcmOp>(
loc, linalgConvTy, ValueRange{input, weight},
@@ -556,8 +550,13 @@ class DepthwiseConvConverter
getNParallelLoopsAttrs(resultRank),
[&](OpBuilder &nestedBuilder, Location nestedLoc,
ValueRange args) {
- Value added = nestedBuilder.create<arith::AddFOp>(
- loc, args[0], args[1]);
+ Value added;
+ if (llvm::isa<FloatType>(inputETy))
+ added = nestedBuilder.create<arith::AddFOp>(loc, args[0],
+ args[1]);
+ else
+ added = nestedBuilder.create<arith::AddIOp>(loc, args[0],
+ args[1]);
nestedBuilder.create<linalg::YieldOp>(nestedLoc, added);
})
.getResult(0);
diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-named.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-named.mlir
index d4afc468eeea4..dab6f3f922347 100644
--- a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-named.mlir
+++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-named.mlir
@@ -798,9 +798,10 @@ func.func @depthwise_conv2d_dyn_w_h(%arg0: tensor<2x?x?x3xf32>, %arg1: tensor<3x
// CHECK: arith.subi
// CHECK: arith.muli
// CHECK: arith.divui
+ // CHECK: [[CST0:%.+]] = arith.constant 0
// CHECK: %[[PADDED:.+]] = tensor.pad %arg0 low[0, 1, 3, 0] high[0, 2, 4, 0] {
// CHECK: ^bb0(%[[ARG3:[0-9a-zA-Z_]+]]: index, %[[ARG4:[0-9a-zA-Z_]+]]: index, %[[ARG5:[0-9a-zA-Z_]+]]: index, %[[ARG6:[0-9a-zA-Z_]+]]: index):
- // CHECK: tensor.yield %cst : f32
+ // CHECK: tensor.yield [[CST0]] : f32
// CHECK: } : tensor<2x?x?x3xf32> to tensor<2x?x?x3xf32>
// CHECK: %[[CONV:.+]] = linalg.depthwise_conv_2d_nhwc_hwcm {dilations = dense<[2, 1]> : tensor<2xi64>, strides = dense<[1, 2]> : tensor<2xi64>} ins(%[[PADDED]], %arg1 : tensor<2x?x?x3xf32>, tensor<3x6x3x5xf32>) outs(%{{.*}} : tensor<2x?x?x3x5xf32>) -> tensor<2x?x?x3x5xf32>
// CHECK: %[[COLLAPSED:.+]] = tensor.collapse_shape %[[CONV]] {{\[}}[0], [1], [2], [3, 4]]
@@ -812,6 +813,30 @@ func.func @depthwise_conv2d_dyn_w_h(%arg0: tensor<2x?x?x3xf32>, %arg1: tensor<3x
// -----
+// CHECK: #[[$MAP0:.*]] = affine_map<(d0, d1, d2, d3) -> (d3)>
+// CHECK: #[[$MAP1:.*]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+
+// CHECK-LABEL: @depthwise_int_conv_zero_zp
+func.func @depthwise_int_conv_zero_zp(%arg0 : tensor<1x7x5x3xi8>, %arg1 : tensor<3x1x3x11xi8>, %arg2 : tensor<33xi32>) -> () {
+ // CHECK: [[INIT:%.+]] = tensor.empty()
+ // CHECK: [[CST0:%.+]] = arith.constant 0
+ // CHECK: [[FILL:%.+]] = linalg.fill ins([[CST0]]{{.*}}outs([[INIT]]
+ // CHECK: [[OUT:%.+]] = tensor.empty()
+ // CHECK: [[DEPTH:%.+]] = linalg.depthwise_conv_2d_nhwc_hwcm {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>} ins(%arg0, %arg1 : tensor<1x7x5x3xi8>, tensor<3x1x3x11xi8>) outs([[FILL]] : tensor<1x5x5x3x11xi32>)
+ // CHECK: [[COLLAPSED:%.+]] = tensor.collapse_shape [[DEPTH]] {{\[}}[0], [1], [2], [3, 4]]
+ // CHECK: [[BIAS:%.+]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]], #[[$MAP1]]], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%arg2, [[COLLAPSED]] : tensor<33xi32>, tensor<1x5x5x33xi32>) outs([[OUT]] : tensor<1x5x5x33xi32>) {
+ // CHECK: ^bb0(%[[ARG3:[0-9a-zA-Z_]+]]: i32, %[[ARG4:[0-9a-zA-Z_]+]]: i32, %[[ARG5:[0-9a-zA-Z_]+]]: i32):
+ // CHECK: [[ADD:%.+]] = arith.addi %[[ARG3]], %[[ARG4]] : i32
+ // CHECK: linalg.yield [[ADD]] : i32
+ // CHECK: } -> tensor<1x5x5x33xi32>
+ %input_zp = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
+ %weight_zp = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
+ %2 = tosa.depthwise_conv2d %arg0, %arg1, %arg2, %input_zp, %weight_zp {acc_type = i32, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 1>, dilation = array<i64: 1, 1> } : (tensor<1x7x5x3xi8>, tensor<3x1x3x11xi8>, tensor<33xi32>, tensor<1xi8>, tensor<1xi8>) -> tensor<1x5x5x33xi32>
+ return
+}
+
+// -----
+
// CHECK: #[[$MAP1:.+]] = affine_map<(d0, d1, d2, d3, d4) -> (d4)>
// CHECK: #[[$MAP2:.+]] = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>
More information about the Mlir-commits
mailing list