Skip to content

Commit 06ef8a6

Browse files
doyeonkim0tensorflower-gardener
authored andcommitted
Convert quantized stablehlo.constant to tfl.pseudo_qconst
PiperOrigin-RevId: 620182445
1 parent a829ac0 commit 06ef8a6

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

tensorflow/compiler/mlir/lite/stablehlo/tests/uniform-quantized-stablehlo-to-tfl.mlir

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,3 +1506,32 @@ func.func @add_i32(%arg0: tensor<1x3x!quant.uniform<i32:f32:1, {1.000000e+0, 1.0
15061506
// CHECK-LABEL: func @add_i32
15071507
// CHECK: stablehlo.add
15081508
// CHECK-NOT: tfl.add
1509+
1510+
// -----
1511+
1512+
// Tests that a quantized `stablehlo.constant` is converted into `tfl.qconst`.
1513+
1514+
// CHECK-LABEL: func @quantized_constant
1515+
func.func @quantized_constant() -> tensor<1x2x4x5x!quant.uniform<i8:f32, 1.000000e+0>> {
1516+
%0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform<i8:f32, 1.000000e+0>>
1517+
return %0 : tensor<1x2x4x5x!quant.uniform<i8:f32, 1.000000e+0>>
1518+
}
1519+
1520+
// CHECK: %[[QCONST:.+]] = "tfl.pseudo_qconst"() {qtype = tensor<1x2x4x5x!quant.uniform<i8:f32, 1.000000e+00>>, value = dense<1> : tensor<1x2x4x5xi8>}
1521+
// CHECK-SAME: () -> tensor<1x2x4x5x!quant.uniform<i8:f32, 1.000000e+00>>
1522+
// CHECK: return %[[QCONST]]
1523+
1524+
// -----
1525+
1526+
// Tests that a float `stablehlo.constant` is not converted into `tfl.qconst`.
1527+
1528+
// CHECK-LABEL: func @float_constant
1529+
func.func @float_constant() -> tensor<1x2x4x5xf32> {
1530+
%0 = stablehlo.constant() {value = dense<1.0> : tensor<1x2x4x5xf32>} : () -> tensor<1x2x4x5xf32>
1531+
return %0 : tensor<1x2x4x5xf32>
1532+
}
1533+
1534+
// CHECK: stablehlo.constant
1535+
// CHECK-NOT: tfl.pseudo_qconst
1536+
// CHECK-NOT: tfl.pseudo_const
1537+
// CHECK-NOT: arith.constant

tensorflow/compiler/mlir/lite/stablehlo/transforms/uniform_quantized_stablehlo_to_tfl_pass.cc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,20 +2102,39 @@ class RewriteQuantizedAddOp : public OpRewritePattern<stablehlo::AddOp> {
21022102
}
21032103
};
21042104

2105+
// Rewrites quantized `stablehlo.constant` to `tfl.pseudo_qconst`.
2106+
class RewriteQuantizedConstantOp
2107+
: public OpRewritePattern<stablehlo::ConstantOp> {
2108+
public:
2109+
using OpRewritePattern<stablehlo::ConstantOp>::OpRewritePattern;
2110+
2111+
LogicalResult match(stablehlo::ConstantOp op) const override {
2112+
return success(IsQuantizedTensorType(op.getOutput().getType()));
2113+
}
2114+
2115+
void rewrite(stablehlo::ConstantOp op,
2116+
PatternRewriter& rewriter) const override {
2117+
rewriter.replaceOpWithNewOp<TFL::QConstOp>(
2118+
op, /*qtype=*/TypeAttr::get(op.getOutput().getType()),
2119+
/*value=*/op.getValue());
2120+
}
2121+
};
2122+
21052123
void UniformQuantizedStableHloToTflPass::runOnOperation() {
21062124
func::FuncOp func_op = getOperation();
21072125
MLIRContext& ctx = getContext();
21082126

21092127
RewritePatternSet patterns(&ctx);
21102128
patterns.add<RewriteUniformDequantizeOp, RewriteUniformQuantizeOp,
2111-
RewriteQuantizedBroadcastInDimOp, RewriteQuantizedConcatenateOp,
2129+
RewriteQuantizedAddOp, RewriteQuantizedBroadcastInDimOp,
2130+
RewriteQuantizedConcatenateOp, RewriteQuantizedConstantOp,
21122131
RewriteQuantizedConvolutionOp,
21132132
RewriteQuantizedDotGeneralOpToTflFullyConnectedOrBatchMatmulOp,
21142133
RewriteQuantizedDynamicReshapeOp, RewriteQuantizedDynamicSliceOp,
21152134
RewriteQuantizedGatherOp, RewriteQuantizedPadOp,
21162135
RewriteQuantizedReduceWindowOpWithMax, RewriteQuantizedReshapeOp,
21172136
RewriteQuantizedSelectOp, RewriteQuantizedSliceOp,
2118-
RewriteQuantizedTransposeOp, RewriteQuantizedAddOp>(&ctx);
2137+
RewriteQuantizedTransposeOp>(&ctx);
21192138

21202139
if (failed(applyPatternsAndFoldGreedily(func_op, std::move(patterns)))) {
21212140
func_op.emitError() << "Failed to convert stablehlo ops with uniform "

0 commit comments

Comments
 (0)