Skip to content

[stablehlo] Reduction upgrade #2745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 102 additions & 17 deletions src/Conversion/ONNXToStablehlo/Math/Reduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,78 @@ Value getIdentityValue(
return nullptr;
}

template <>
Value getIdentityValue<ONNXReduceMaxV13Op>(
Value getReduceMaxIdentityValue(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
MathBuilder createMath(rewriter, loc);
return rewriter.create<stablehlo::ConstantOp>(
loc, createMath.negativeInfAttr(elemType));
}

template <>
Value getIdentityValue<ONNXReduceMinV13Op>(
Value getReduceMinIdentityValue(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
MathBuilder createMath(rewriter, loc);
return rewriter.create<stablehlo::ConstantOp>(
loc, createMath.positiveInfAttr(elemType));
}

template <>
Value getIdentityValue<ONNXReduceSumOp>(
Value getReduceSumIdentityValue(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return rewriter.create<stablehlo::ConstantOp>(
loc, rewriter.getZeroAttr(elemType));
}

template <>
Value getIdentityValue<ONNXReduceSumV11Op>(
Value getReduceMeanIdentityValue(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return rewriter.create<stablehlo::ConstantOp>(
loc, rewriter.getZeroAttr(elemType));
}

template <>
Value getIdentityValue<ONNXReduceMaxOp>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return getReduceMaxIdentityValue(rewriter, loc, elemType);
}

template <>
Value getIdentityValue<ONNXReduceMaxV13Op>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return getReduceMaxIdentityValue(rewriter, loc, elemType);
}

template <>
Value getIdentityValue<ONNXReduceMinOp>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return getReduceMinIdentityValue(rewriter, loc, elemType);
}

template <>
Value getIdentityValue<ONNXReduceMinV13Op>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return getReduceMinIdentityValue(rewriter, loc, elemType);
}

template <>
Value getIdentityValue<ONNXReduceSumOp>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return getReduceSumIdentityValue(rewriter, loc, elemType);
}

template <>
Value getIdentityValue<ONNXReduceSumV11Op>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return getReduceSumIdentityValue(rewriter, loc, elemType);
}

template <>
Value getIdentityValue<ONNXReduceMeanOp>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return getReduceMeanIdentityValue(rewriter, loc, elemType);
}

template <>
Value getIdentityValue<ONNXReduceMeanV13Op>(
ConversionPatternRewriter &rewriter, Location loc, Type elemType) {
return rewriter.create<stablehlo::ConstantOp>(
loc, rewriter.getZeroAttr(elemType));
return getReduceMeanIdentityValue(rewriter, loc, elemType);
}

template <typename ONNXReductionOp>
Expand All @@ -78,12 +115,9 @@ llvm::SmallVector<int64_t, 4> getDefinedAxes(Operation *op) {
return definedAxes;
}

template <>
llvm::SmallVector<int64_t, 4> getDefinedAxes<ONNXReduceSumOp>(Operation *op) {
llvm::SmallVector<int64_t, 4> getDefinedAxesFromConstAxes(
Operation *op, Value axesValue, bool keepDims) {
llvm::SmallVector<int64_t, 4> definedAxes;
ONNXReduceSumOp reduceSumOp = cast<ONNXReduceSumOp>(op);
Value axesValue = reduceSumOp.getAxes();

// Assume it is verified that axes are known. Convert DenseElementsAttr to
// ArrayAttr.
if (!isNoneValue(axesValue) && getONNXConstantOp(axesValue)) {
Expand All @@ -104,7 +138,7 @@ llvm::SmallVector<int64_t, 4> getDefinedAxes<ONNXReduceSumOp>(Operation *op) {
assert(inputType != nullptr && outputType != nullptr &&
"not implemented for dynamic axes when either input or output is not "
"ranked");
bool keepDims = reduceSumOp.getKeepdims() == 1;

int64_t inputRank = inputType.getRank();
int64_t outputRank = outputType.getRank();
llvm::ArrayRef<int64_t> inputShape = inputType.getShape();
Expand All @@ -127,22 +161,69 @@ llvm::SmallVector<int64_t, 4> getDefinedAxes<ONNXReduceSumOp>(Operation *op) {
return definedAxes;
}

template <>
llvm::SmallVector<int64_t, 4> getDefinedAxes<ONNXReduceMaxOp>(Operation *op) {
ONNXReduceMaxOp reduceMaxOp = cast<ONNXReduceMaxOp>(op);
Value axesValue = reduceMaxOp.getAxes();
bool keepDims = reduceMaxOp.getKeepdims() == 1;
return getDefinedAxesFromConstAxes(op, axesValue, keepDims);
}

template <>
llvm::SmallVector<int64_t, 4> getDefinedAxes<ONNXReduceMinOp>(Operation *op) {
ONNXReduceMinOp reduceMinOp = cast<ONNXReduceMinOp>(op);
Value axesValue = reduceMinOp.getAxes();
bool keepDims = reduceMinOp.getKeepdims() == 1;
return getDefinedAxesFromConstAxes(op, axesValue, keepDims);
}

template <>
llvm::SmallVector<int64_t, 4> getDefinedAxes<ONNXReduceSumOp>(Operation *op) {
ONNXReduceSumOp reduceSumOp = cast<ONNXReduceSumOp>(op);
Value axesValue = reduceSumOp.getAxes();
bool keepDims = reduceSumOp.getKeepdims() == 1;
return getDefinedAxesFromConstAxes(op, axesValue, keepDims);
}

template <>
llvm::SmallVector<int64_t, 4> getDefinedAxes<ONNXReduceMeanOp>(Operation *op) {
ONNXReduceMeanOp reduceMeanOp = cast<ONNXReduceMeanOp>(op);
Value axesValue = reduceMeanOp.getAxes();
bool keepDims = reduceMeanOp.getKeepdims() == 1;
return getDefinedAxesFromConstAxes(op, axesValue, keepDims);
}

// Block reduce ops
template <typename ReductionOp>
struct BlockReduceOp {
using Op = void;
};

template <>
struct BlockReduceOp<ONNXReduceMaxOp> {
using Op = stablehlo::MaxOp;
};

template <>
struct BlockReduceOp<ONNXReduceMaxV13Op> {
using Op = stablehlo::MaxOp;
};

template <>
struct BlockReduceOp<ONNXReduceMinOp> {
using Op = stablehlo::MinOp;
};

template <>
struct BlockReduceOp<ONNXReduceMinV13Op> {
using Op = stablehlo::MinOp;
};

template <>
struct BlockReduceOp<ONNXReduceMeanOp> {
using Op = stablehlo::AddOp;
};

template <>
struct BlockReduceOp<ONNXReduceMeanV13Op> {
using Op = stablehlo::AddOp;
Expand Down Expand Up @@ -355,10 +436,14 @@ struct ONNXReductionOpLoweringToStablehlo : public ConversionPattern {

void populateLoweringONNXReductionOpToStablehloPattern(
RewritePatternSet &patterns, MLIRContext *ctx) {
patterns.insert<ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceMaxV13Op>,
patterns.insert<ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceMaxOp>,
ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceMaxV13Op>,
ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceMinOp>,
ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceMinV13Op>,
ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceSumOp>,
ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceSumV11Op>>(ctx);
patterns.insert<ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceMeanOp>>(
ctx, /*computeMean=*/true);
patterns
.insert<ONNXReductionOpLoweringToStablehlo<mlir::ONNXReduceMeanV13Op>>(
ctx, /*computeMean=*/true);
Expand Down
54 changes: 54 additions & 0 deletions test/mlir/conversion/onnx_to_stablehlo/Math/Reduction.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,57 @@ func.func @test_reducemean_v13_2(%arg0 : tensor<?x?x?xf32>) -> tensor<?x?xf32> {
// CHECK: [[VAR_6_:%.+]] = stablehlo.divide [[VAR_2_]], [[VAR_5_]] : tensor<?x?xf32>
// CHECK: return [[VAR_6_]] : tensor<?x?xf32>
// CHECK: }

// -----

func.func @reduce_mean(%arg0: tensor<2x5x9x11xf32>) -> tensor<2x5x1x1xf32> {
%0 = "onnx.Constant"() {value = dense<[2, 3]> : tensor<2xi64>} : () -> tensor<2xi64>
%1 = "onnx.ReduceMean"(%arg0, %0) : (tensor<2x5x9x11xf32>, tensor<2xi64>) -> tensor<2x5x1x1xf32>
return %1 : tensor<2x5x1x1xf32>
}

// CHECK-LABEL: func.func @reduce_mean
// CHECK-SAME: ([[PARAM_0_:%.+]]: tensor<2x5x9x11xf32>) -> tensor<2x5x1x1xf32> {
// CHECK-DAG: [[VAR_0_:%.+]] = shape.const_shape [2, 5, 1, 1] : tensor<4xindex>
// CHECK-DAG: [[VAR_1_:%.+]] = stablehlo.constant dense<9.900000e+01> : tensor<2x5x1x1xf32>
// CHECK-DAG: [[VAR_2_:%.+]] = stablehlo.constant dense<0.000000e+00> : tensor<f32>
// CHECK: [[VAR_3_:%.+]] = stablehlo.reduce([[PARAM_0_]] init: [[VAR_2_]]) applies stablehlo.add across dimensions = [2, 3] : (tensor<2x5x9x11xf32>, tensor<f32>) -> tensor<2x5xf32>
// CHECK: [[VAR_4_:%.+]] = stablehlo.dynamic_reshape [[VAR_3_]], [[VAR_0_]] : (tensor<2x5xf32>, tensor<4xindex>) -> tensor<2x5x1x1xf32>
// CHECK: [[VAR_5_:%.+]] = stablehlo.divide [[VAR_4_]], [[VAR_1_]] : tensor<2x5x1x1xf32>
// CHECK: return [[VAR_5_]] : tensor<2x5x1x1xf32>
// CHECK: }

// -----

func.func @reduce_max(%arg0: tensor<2x5x9x11xf32>) -> tensor<2x5x1x1xf32> {
%0 = "onnx.Constant"() {value = dense<[2, 3]> : tensor<2xi64>} : () -> tensor<2xi64>
%1 = "onnx.ReduceMax"(%arg0, %0) : (tensor<2x5x9x11xf32>, tensor<2xi64>) -> tensor<2x5x1x1xf32>
return %1 : tensor<2x5x1x1xf32>
}

// CHECK-LABEL: func.func @reduce_max
// CHECK-SAME: ([[PARAM_0_:%.+]]: tensor<2x5x9x11xf32>) -> tensor<2x5x1x1xf32> {
// CHECK-DAG: [[VAR_0_:%.+]] = shape.const_shape [2, 5, 1, 1] : tensor<4xindex>
// CHECK-DAG: [[VAR_1_:%.+]] = stablehlo.constant dense<0xFF800000> : tensor<f32>
// CHECK: [[VAR_2_:%.+]] = stablehlo.reduce([[PARAM_0_]] init: [[VAR_1_]]) applies stablehlo.maximum across dimensions = [2, 3] : (tensor<2x5x9x11xf32>, tensor<f32>) -> tensor<2x5xf32>
// CHECK: [[VAR_3_:%.+]] = stablehlo.dynamic_reshape [[VAR_2_]], [[VAR_0_]] : (tensor<2x5xf32>, tensor<4xindex>) -> tensor<2x5x1x1xf32>
// CHECK: return [[VAR_3_]] : tensor<2x5x1x1xf32>
// CHECK: }

// -----


func.func @reduce_min(%arg0: tensor<2x5x9x11xf32>) -> tensor<2x5x1x1xf32> {
%0 = "onnx.Constant"() {value = dense<[2, 3]> : tensor<2xi64>} : () -> tensor<2xi64>
%1 = "onnx.ReduceMin"(%arg0, %0) : (tensor<2x5x9x11xf32>, tensor<2xi64>) -> tensor<2x5x1x1xf32>
return %1 : tensor<2x5x1x1xf32>
}

// CHECK-LABEL: func.func @reduce_min
// CHECK-SAME: ([[PARAM_0_:%.+]]: tensor<2x5x9x11xf32>) -> tensor<2x5x1x1xf32> {
// CHECK-DAG: [[VAR_0_:%.+]] = shape.const_shape [2, 5, 1, 1] : tensor<4xindex>
// CHECK-DAG: [[VAR_1_:%.+]] = stablehlo.constant dense<0x7F800000> : tensor<f32>
// CHECK: [[VAR_2_:%.+]] = stablehlo.reduce([[PARAM_0_]] init: [[VAR_1_]]) applies stablehlo.minimum across dimensions = [2, 3] : (tensor<2x5x9x11xf32>, tensor<f32>) -> tensor<2x5xf32>
// CHECK: [[VAR_3_:%.+]] = stablehlo.dynamic_reshape [[VAR_2_]], [[VAR_0_]] : (tensor<2x5xf32>, tensor<4xindex>) -> tensor<2x5x1x1xf32>
// CHECK: return [[VAR_3_]] : tensor<2x5x1x1xf32>
// CHECK: }