diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp --- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp +++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp @@ -1052,6 +1052,36 @@ return true; } +// Checks for a dynamic batch dim in any of the passed parameters of an op. +// The batch dimention must be #0 and the rest of the dimensions must be static. +template +static Optional> +checkForDynamicBatchOnly(PatternRewriter &rewriter, Op op, + ArrayRef params) { + SmallVector dynTypes; + SmallVector dynamicDims; + for (const Value ¶m : params) { + auto paramTy = param.getType().cast(); + if (!paramTy.hasStaticShape()) + dynTypes.push_back(paramTy); + } + + if (dynTypes.empty()) + return dynamicDims; + + for (const ShapedType &dynTy : dynTypes) { + if (llvm::any_of(dynTy.getShape().drop_front(), ShapedType::isDynamic)) { + (void)rewriter.notifyMatchFailure( + op, "input can only be dynamic for batch size"); + return llvm::None; + } + } + + dynamicDims.push_back( + rewriter.create(op->getLoc(), params[0], 0)); + return dynamicDims; +} + namespace { template @@ -1089,10 +1119,15 @@ auto dilationTosaAttr = op->getAttr("dilation").cast(); bool isQuantized = op->hasAttr("quantization_info"); - if (!inputTy.hasStaticShape() || !weightTy.hasStaticShape() || - !biasTy.hasStaticShape() || !resultTy.hasStaticShape()) - return rewriter.notifyMatchFailure(op, - "tosa.conv ops require static shapes"); + if (!weightTy.hasStaticShape() || !biasTy.hasStaticShape()) + return rewriter.notifyMatchFailure( + op, "tosa.conv ops require static shapes for weight and bias"); + + auto dynamicDimsOr = + checkForDynamicBatchOnly(rewriter, op, {input, op.output()}); + if (!dynamicDimsOr.hasValue()) + return failure(); + SmallVector dynamicDims = dynamicDimsOr.getValue(); if (inputETy.isUnsignedInteger()) return rewriter.notifyMatchFailure( @@ -1145,7 +1180,7 @@ Attribute resultZeroAttr = rewriter.getZeroAttr(resultETy); Value initTensor = rewriter.create( - loc, resultTy.getShape(), resultETy); + loc, dynamicDims, resultTy.getShape(), resultETy); Value zero = rewriter.create(loc, resultZeroAttr); Value zeroTensor = rewriter.create(loc, zero, initTensor).getResult(0); @@ -1170,7 +1205,7 @@ indexingMaps.push_back(rewriter.getMultiDimIdentityMap(resultTy.getRank())); Value biasInitTensor = rewriter.create( - loc, resultTy.getShape(), resultETy); + loc, dynamicDims, resultTy.getShape(), resultETy); if (isQuantized) { auto quantizationInfo = @@ -1265,10 +1300,15 @@ quantizationInfo.weight_zp().getValue().getSExtValue()); } - if (!inputTy.hasStaticShape() || !weightTy.hasStaticShape() || - !biasTy.hasStaticShape() || !resultTy.hasStaticShape()) - return rewriter.notifyMatchFailure(op, - "tosa.conv ops require static shapes"); + if (!weightTy.hasStaticShape() || !biasTy.hasStaticShape()) + return rewriter.notifyMatchFailure( + op, "tosa.depthwise_conv ops require static shapes"); + + auto dynamicDimsOr = + checkForDynamicBatchOnly(rewriter, op, {input, op.output()}); + if (!dynamicDimsOr.hasValue()) + return failure(); + SmallVector dynamicDims = dynamicDimsOr.getValue(); auto weightShape = weightTy.getShape(); auto resultShape = resultTy.getShape(); @@ -1327,13 +1367,13 @@ Attribute resultZeroAttr = rewriter.getZeroAttr(resultETy); Value initTensor = rewriter.create( - loc, linalgConvTy.getShape(), resultETy); + loc, dynamicDims, linalgConvTy.getShape(), resultETy); Value zero = rewriter.create(loc, resultZeroAttr); Value zeroTensor = rewriter.create(loc, zero, initTensor).getResult(0); Value biasInitTensor = rewriter.create( - loc, resultTy.getShape(), resultETy); + loc, dynamicDims, resultTy.getShape(), resultETy); if (!isQuantized) { Value conv = rewriter .create( @@ -1760,9 +1800,11 @@ return rewriter.notifyMatchFailure( op, "tosa.rescale requires scale32 for double_round to be true"); - if (!outputTy.hasStaticShape()) - return rewriter.notifyMatchFailure( - op, "tosa to linalg conversion expects statically shaped tensors"); + auto dynamicDimsOr = + checkForDynamicBatchOnly(rewriter, op, {input, op.output()}); + if (!dynamicDimsOr.hasValue()) + return failure(); + SmallVector dynamicDims = dynamicDimsOr.getValue(); // The shift and multiplier values. SmallVector multiplierValues; @@ -1830,8 +1872,7 @@ // Construct the indexing maps needed for linalg.generic ops. Value initTensor = rewriter.create( - loc, ArrayRef({}), outputTy.getShape(), - outputTy.getElementType()); + loc, dynamicDims, outputTy.getShape(), outputTy.getElementType()); auto linalgOp = rewriter.create( loc, outputTy, genericInputs, ValueRange{initTensor}, indexingMaps, @@ -1943,15 +1984,20 @@ auto imageH = inputTy.getShape()[1]; auto imageW = inputTy.getShape()[2]; - if (!resultTy.hasStaticShape()) + auto dynamicDimsOr = + checkForDynamicBatchOnly(rewriter, op, {input, op.output()}); + if (!dynamicDimsOr.hasValue()) return failure(); + SmallVector dynamicDims = dynamicDimsOr.getValue(); + ; + if (op.mode() != "NEAREST_NEIGHBOR" && op.mode() != "BILINEAR") return failure(); auto initTensor = rewriter - .create(loc, ArrayRef{}, - resultTy.getShape(), resultElementTy) + .create(loc, dynamicDims, resultTy.getShape(), + resultElementTy) .result(); SmallVector affineMaps = { @@ -2637,13 +2683,13 @@ auto input = adaptor.getOperands()[0]; auto indices = adaptor.getOperands()[1]; - auto inputTy = input.getType().cast(); - auto indicesTy = indices.getType().cast(); auto resultTy = op.getType().cast(); - if (!inputTy.hasStaticShape() || !indicesTy.hasStaticShape()) - return rewriter.notifyMatchFailure( - op, "require input type to have static shape"); + auto dynamicDimsOr = + checkForDynamicBatchOnly(rewriter, op, {input, indices, op.output()}); + if (!dynamicDimsOr.hasValue()) + return failure(); + SmallVector dynamicDims = dynamicDimsOr.getValue(); auto resultElementTy = resultTy.getElementType(); @@ -2651,8 +2697,8 @@ auto initTensor = rewriter - .create(loc, ArrayRef{}, - resultTy.getShape(), resultElementTy) + .create(loc, dynamicDims, resultTy.getShape(), + resultElementTy) .result(); SmallVector affineMaps = { @@ -2816,8 +2862,11 @@ ShapedType resultTy = op.getType().template cast(); Type resultETy = inputTy.getElementType(); - if (!inputTy.hasStaticShape()) + auto dynamicDimsOr = + checkForDynamicBatchOnly(rewriter, op, {input, op.output()}); + if (!dynamicDimsOr.hasValue()) return failure(); + SmallVector dynamicDims = dynamicDimsOr.getValue(); // Determine what the initial value needs to be for the max pool op. Attribute initialAttr; @@ -2854,7 +2903,7 @@ // Create the linalg op that performs pooling. Value initTensor = rewriter.create( - loc, resultTy.getShape(), resultTy.getElementType()); + loc, dynamicDims, resultTy.getShape(), resultTy.getElementType()); Value filledInitTensor = rewriter.create(loc, initialValue, initTensor).result(); @@ -2887,8 +2936,11 @@ inElementTy.isa() ? rewriter.getI32Type() : inElementTy; ShapedType accTy = resultTy.clone(accETy); - if (!inputTy.hasStaticShape()) + auto dynamicDimsOr = + checkForDynamicBatchOnly(rewriter, op, {input, op.output()}); + if (!dynamicDimsOr.hasValue()) return failure(); + SmallVector dynamicDims = dynamicDimsOr.getValue(); // Apply padding as necessary. llvm::SmallVector pad; @@ -2909,8 +2961,8 @@ Attribute dilationAttr = rewriter.getI64VectorAttr({1, 1}); // Create the linalg op that performs pooling. - Value poolInitTensor = - rewriter.create(loc, accTy.getShape(), accETy); + Value poolInitTensor = rewriter.create( + loc, dynamicDims, accTy.getShape(), accETy); Value filledInitTensor = rewriter.create(loc, initialValue, poolInitTensor) @@ -2933,7 +2985,7 @@ auto affineMap = rewriter.getMultiDimIdentityMap(resultTy.getRank()); Value genericInitTensor = rewriter.create( - loc, resultTy.getShape(), resultETy); + loc, dynamicDims, resultTy.getShape(), resultETy); auto genericOp = rewriter.create( loc, ArrayRef({resultTy}), ValueRange{poolingOp}, diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir --- a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir +++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir @@ -897,6 +897,26 @@ // ----- +// CHECK: #[[$MAP0:.*]] = affine_map<(d0, d1) -> (d0, d1)> + +// CHECK-LABEL: @rescale_i8_dyn +func @rescale_i8_dyn(%arg0 : tensor) -> () { + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[INIT:.+]] = linalg.init_tensor [%[[BATCH]], 2] + // CHECK: [[GENERIC:%.+]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP0]]], iterator_types = ["parallel", "parallel"]} ins(%arg0 : tensor) outs(%[[INIT]] : tensor) + %0 = "tosa.rescale"(%arg0) {input_zp = 17 : i32, output_zp = 22 : i32, multiplier = [19689 : i32], shift = [15 : i32], scale32 = false, double_round = false, per_channel = false} : (tensor) -> (tensor) + + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[INIT:.+]] = linalg.init_tensor [%[[BATCH]], 2] + // CHECK: [[GENERIC:%.+]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP0]]], iterator_types = ["parallel", "parallel"]} ins(%arg0 : tensor) outs(%[[INIT]] : tensor) + %1 = "tosa.rescale"(%arg0) {input_zp = 17 : i32, output_zp = 22 : i32, multiplier = [19689 : i32], shift = [15 : i32], scale32 = false, double_round = false, per_channel = false} : (tensor) -> (tensor) + + return +} +// ----- + // CHECK: #[[$MAP0:.*]] = affine_map<(d0) -> (d0)> // CHECK-LABEL: @rescale_ui8 @@ -1332,6 +1352,22 @@ return } +// CHECK-LABEL: @gather_float_dyn +func @gather_float_dyn(%arg0: tensor, %arg1: tensor) -> () { + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[INIT:.+]] = linalg.init_tensor [%[[BATCH]], 3, 2] + // CHECK: %[[GENERIC:.+]] = linalg.generic {indexing_maps = [#map0, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%arg1 : tensor) outs(%[[INIT]] : tensor) + // CHECK: ^bb0(%[[ARG0:.+]]: i32, %[[ARG1:.+]]: f32) + // CHECK: %[[IDX0:.+]] = linalg.index 0 + // CHECK: %[[CAST:.+]] = arith.index_cast %[[ARG0]] + // CHECK: %[[IDX2:.+]] = linalg.index 2 + // CHECK: %[[EXTRACT:.+]] = tensor.extract %arg0[%[[IDX0]], %[[CAST]], %[[IDX2]]] : tensor + // CHECK: linalg.yield %[[EXTRACT]] + %0 = "tosa.gather"(%arg0, %arg1) : (tensor, tensor) -> (tensor) + return +} + // CHECK-LABEL: @gather_int func @gather_int(%arg0: tensor<2x3x2xi32>, %arg1: tensor<2x3xi32>) -> () { // CHECK: %[[INIT:.+]] = linalg.init_tensor [2, 3, 2] @@ -1420,6 +1456,19 @@ return } +// CHECK-LABEL: @max_pool_dyn +func @max_pool_dyn(%arg0: tensor) -> () { + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[CONST:.+]] = arith.constant -3.40282347E+38 + // CHECK: %[[INIT:.+]] = linalg.init_tensor [%[[BATCH]], 4, 32, 62] + // CHECK: %[[FILL:.+]] = linalg.fill(%[[CONST]], %[[INIT]]) + // CHECK: %[[KERNEL:.+]] = linalg.init_tensor [3, 3] + // CHECK: linalg.pooling_nhwc_max {dilations = dense<1> : vector<2xi64>, strides = dense<1> : vector<2xi64>} ins(%arg0, %[[KERNEL]] : tensor, tensor<3x3xf32>) outs(%[[FILL]] : tensor) + %0 = "tosa.max_pool2d"(%arg0) {pad = [0, 0, 0, 0], kernel = [3, 3], stride = [1, 1]} : (tensor) -> (tensor) + return +} + // CHECK-LABEL: @max_pool_i8 func @max_pool_i8(%arg0: tensor<1x6x34x62xi8>) -> () { // CHECK: arith.constant -128 @@ -1504,6 +1553,22 @@ return %0 : tensor<1x5x33x62xf32> } +// CHECK-LABEL: @avg_pool_dyn +func @avg_pool_dyn(%arg0: tensor) -> (tensor) { + // The calculations remain the same as above, only testing for dyn behavior + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[PAD:.+]] = linalg.pad_tensor %arg0 low[0, 1, 1, 0] high[0, 1, 1, 0] + // CHECK: %[[POOLINIT:.+]] = linalg.init_tensor [%[[BATCH]], 5, 33, 62] + // CHECK: %[[FILL:.+]] = linalg.fill + // CHECK: %[[KERNEL:.+]] = linalg.init_tensor [4, 4] + // CHECK: %[[POOL:.+]] = linalg.pooling_nhwc_sum {dilations = dense<1> : vector<2xi64>, strides = dense<1> : vector<2xi64>} ins(%[[PAD]], %[[KERNEL]] : tensor, tensor<4x4xf32>) outs(%[[FILL]] : tensor) + // CHECK: %[[INIT:.+]] = linalg.init_tensor [%[[BATCH]], 5, 33, 62] + // CHECK: %[[GENERIC:.+]] = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%[[POOL]] : tensor) outs(%[[INIT]] : tensor) + %0 = "tosa.avg_pool2d"(%arg0) {pad = [1, 1, 1, 1], kernel = [4, 4], stride = [1, 1]} : (tensor) -> (tensor) + return %0 : tensor +} + // ----- // CHECK-LABEL: @avg_pool_i8 @@ -1587,6 +1652,32 @@ // ----- +// CHECK: #[[$MAP0:.+]] = affine_map<(d0, d1, d2, d3) -> (d3, d0, d1, d2)> +// CHECK: #[[$MAP1:.+]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> +// CHECK: #[[$MAP2:.+]] = affine_map<(d0, d1, d2, d3) -> (d3)> + +// CHECK-LABEL: @conv2d_dyn +func @conv2d_dyn(%input: tensor, %weights: tensor<28x3x3x27xf32>, %bias: tensor<28xf32>) -> () { + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[W_IN:.+]] = linalg.init_tensor [3, 3, 27, 28] + // CHECK: %[[W:.+]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]]], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%arg1 : tensor<28x3x3x27xf32>) outs(%[[W_IN]] : tensor<3x3x27x28xf32>) + // CHECK: linalg.yield %arg3 : f32 + // CHECK: %[[M_IN:.+]] = linalg.init_tensor [%[[BATCH]], 45, 40, 28] + // CHECK: %[[CST:.+]] = arith.constant 0 + // CHECK: %[[FILL:.+]] = linalg.fill + // CHECK: %[[B_IN:.+]] = linalg.init_tensor [%[[BATCH]], 45, 40, 28] + // CHECK: %[[CONV:.+]] = linalg.conv_2d_nhwc_hwcf {dilations = dense<[2, 1]> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>} ins(%arg0, %[[W]] : tensor, tensor<3x3x27x28xf32>) outs(%[[FILL]] : tensor) + // CHECK: %[[B:.+]] = linalg.generic {indexing_maps = [#[[$MAP2]], #[[$MAP1]], #[[$MAP1]]], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%arg2, %[[CONV]] : tensor<28xf32>, tensor) outs(%[[B_IN]] : tensor) + // CHECK: %[[ADD:.+]] = arith.addf + // CHECK: linalg.yield %[[ADD]] : f32 + %0 = "tosa.conv2d"(%input, %weights, %bias) {pad = [0, 0, 0, 0], stride = [1, 1], dilation = [2, 1]} : (tensor, tensor<28x3x3x27xf32>, tensor<28xf32>) -> (tensor) + return +} + +// ----- + + // CHECK-LABEL: @conv2d_padded_f32 func @conv2d_padded_f32(%input: tensor<1x47x40x28xf32>, %weights: tensor<28x3x3x28xf32>, %bias: tensor<28xf32>) -> () { // CHECK: %[[C0:.+]] = arith.constant 0 @@ -1636,6 +1727,31 @@ // CHECK: #[[$MAP0:.*]] = affine_map<(d0, d1, d2, d3) -> (d3)> // CHECK: #[[$MAP1:.*]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> +// CHECK-LABEL: @depthwise_conv_dyn +func @depthwise_conv_dyn(%arg0 : tensor, %arg1 : tensor<3x1x3x11xf32>, %arg2 : tensor<33xf32>) -> () { + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[INIT:.+]] = linalg.init_tensor [%[[BATCH]], 5, 5, 3, 11] + // CHECK: %[[CST0:.+]] = arith.constant 0 + // CHECK: %[[FILL:.+]] = linalg.fill + // CHECK: %[[OUT:.+]] = linalg.init_tensor [%[[BATCH]], 5, 5, 33] + // CHECK: %[[DEPTH:.+]] = linalg.depthwise_conv_2d_nhwc_hwcm {dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>} ins(%arg0, %arg1 : tensor, tensor<3x1x3x11xf32>) outs(%[[FILL]] : tensor) + // CHECK: %[[COLLAPSED:.+]] = tensor.collapse_shape %[[DEPTH]] {{\[}}[0, 1, 2, 3, 4]] + // CHECK: %[[EXPANDED:.+]] = tensor.expand_shape %[[COLLAPSED]] {{\[}}[0, 1, 2, 3]] + // CHECK: %[[BIAS:.+]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]], #[[$MAP1]]], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%arg2, %[[EXPANDED]] : tensor<33xf32>, tensor) outs(%[[OUT]] : tensor) { + // CHECK: ^bb0(%arg3: f32, %arg4: f32, %arg5: f32): // no predecessors + // CHECK: %[[ADD:.+]] = arith.addf %arg3, %arg4 : f32 + // CHECK: linalg.yield %[[ADD]] : f32 + // CHECK: } -> tensor + %2 = "tosa.depthwise_conv2d"(%arg0, %arg1, %arg2) { pad = [0, 0, 0, 0], stride = [1, 1], dilation = [1, 1] } : (tensor, tensor<3x1x3x11xf32>, tensor<33xf32>) -> (tensor) + return +} + +// ----- + +// CHECK: #[[$MAP0:.*]] = affine_map<(d0, d1, d2, d3) -> (d3)> +// CHECK: #[[$MAP1:.*]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> + // CHECK-LABEL: @depthwise_conv_strides func @depthwise_conv_strides(%arg0 : tensor<1x11x9x3xf32>, %arg1 : tensor<3x1x3x11xf32>, %arg2 : tensor<33xf32>) -> () { // CHECK: [[INIT:%.+]] = linalg.init_tensor [1, 5, 5, 3, 11] @@ -2008,3 +2124,15 @@ %output = "tosa.resize"(%input) { output_size = [4, 4], stride = [128, 128], offset = [1, 2], stride_fp = [0. : f32, 0. : f32], offset_fp = [0. : f32, 0. : f32], shift = 8 : i32, mode = "BILINEAR" } : (tensor<1x2x2x1xi8>) -> (tensor<1x4x4x1xi32>) return } + +// ----- + +// CHECK-LABEL: @resize_dyn +func @resize_dyn(%input: tensor) -> () { + // CHECK: %[[C0:.+]] = arith.constant 0 + // CHECK: %[[BATCH:.+]] = tensor.dim %arg0, %[[C0]] + // CHECK: %[[INIT:.+]] = linalg.init_tensor [%[[BATCH]], 4, 4, 1] + // CHECK: %[[GENERIC:.+]] = linalg.generic + %output = "tosa.resize"(%input) { output_size = [4, 4], stride = [128, 128], offset = [1, 2], stride_fp = [0. : f32, 0. : f32], offset_fp = [0. : f32, 0. : f32], shift = 8 : i32, mode = "BILINEAR" } : (tensor) -> (tensor) + return +}