Skip to content

Commit da3da1e

Browse files
liufengdbtensorflower-gardener
authored andcommitted
Convert all the constant to standard constant before quantization passes
PiperOrigin-RevId: 364807528 Change-Id: I1476265ff711b5d12a73ff34bf1443f8eb03f4a4
1 parent 19d1ff2 commit da3da1e

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎tensorflow/compiler/mlir/lite/quantization/lite/tfl_to_std.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
#include "llvm/Support/Casting.h"
1818
#include "mlir/Dialect/Quant/QuantOps.h" // from @llvm-project
19+
#include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
1920
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
2021
#include "tensorflow/compiler/mlir/lite/quantization/quantization_utils.h"
2122

@@ -36,6 +37,14 @@ void ConvertTFLQuantOpsToMlirQuantOps(FuncOp func) {
3637
q.getLoc(), q.output().getType(), q.input());
3738
q.output().replaceAllUsesWith(qcast);
3839
q.erase();
40+
} else if (auto q = llvm::dyn_cast<ConstOp>(op)) {
41+
auto value = q.value();
42+
auto type = q.getResult().getType();
43+
if (ConstantOp::isBuildableWith(value, type)) {
44+
auto c = b.create<ConstantOp>(q.getLoc(), q.value());
45+
q.output().replaceAllUsesWith(c);
46+
q.erase();
47+
}
3948
}
4049
});
4150
}

‎tensorflow/compiler/mlir/lite/tests/prepare-quantize.mlir‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ func @QuantizeConv2DPerChannel(%arg0: tensor<1x224x224x3x!quant.uniform<u8:f32,
7575
// CHECK-NEXT: return %[[conv]]
7676
}
7777

78+
// CHECK-LABEL: QuantizeConv2DPerChannelConst
79+
func @QuantizeConv2DPerChannelConst(%arg0: tensor<1x224x224x3x!quant.uniform<u8:f32, 1.5>>,
80+
%arg1: tensor<32x3x3x3x!quant.uniform<u8<1:255>:f32:3, {1.0,2.0,3.0}>>) -> tensor<1x112x112x32xf32> {
81+
%bias = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<32xf32>} : () -> tensor<32xf32>
82+
%input = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform<u8:f32, 1.5>>) -> tensor<1x224x224x3xf32>
83+
%weight = "tfl.dequantize"(%arg1) : (tensor<32x3x3x3x!quant.uniform<u8<1:255>:f32:3, {1.0,2.0,3.0}>>) -> tensor<32x3x3x3xf32>
84+
%conv = "tfl.conv_2d"(%input, %weight, %bias) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32,
85+
fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}
86+
: (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32>
87+
return %conv : tensor<1x112x112x32xf32>
88+
89+
// CHECK-NEXT: %[[cst:.*]] = constant dense<1.000000e+00> : tensor<32xf32>
90+
// CHECK-NEXT: %[[qbias:.*]] = "tfl.quantize"(%[[cst]]) {qtype = tensor<32x!quant.uniform<i32:f32:0, {1.500000e+00,3.000000e+00,4.500000e+00}>>, volatile}
91+
// CHECK-NEXT: %[[bias:.*]] = "tfl.dequantize"(%[[qbias]])
92+
// CHECK-NEXT: %[[in:.*]] = "tfl.dequantize"(%arg0)
93+
// CHECK-NEXT: %[[w:.*]] = "tfl.dequantize"(%arg1)
94+
// CHECK-NEXT: %[[conv:.*]] = "tfl.conv_2d"(%[[in]], %[[w]], %[[bias]])
95+
// CHECK-NEXT: return %[[conv]]
96+
}
97+
7898
// CHECK-LABEL: QuantizeConv2DPerChannels
7999
func @QuantizeConv2DPerChannels(%arg0: tensor<1x224x224x3x!quant.uniform<u8:f32:3, {1.0,2.0,3.0}>>,
80100
%arg1: tensor<32x3x3x3x!quant.uniform<u8<1:255>:f32:3, {1.0,2.0,3.0}>>) -> tensor<1x112x112x32xf32> {

‎tensorflow/compiler/mlir/lite/transforms/prepare_quantize.cc‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ void PrepareQuantizePass::runOnFunction() {
374374
patterns_1.insert<PrepareLstmOutputScale<UnidirectionalSequenceLSTMOp>>(
375375
ctx);
376376
}
377-
(void)applyPatternsAndFoldGreedily(func, std::move(patterns_1));
377+
(void)applyPatternsAndFoldGreedily(
378+
func, std::move(patterns_1),
379+
// TODO(fengliuai): Fix the logic to work without this flag
380+
/*useTopDownTraversal=*/false);
378381

379382
// During the legalization, unsigned quantized type is used, so we have to
380383
// convert all of them to signed.
@@ -399,7 +402,8 @@ void PrepareQuantizePass::runOnFunction() {
399402
ctx, quant_specs_);
400403
patterns_2.insert<ConvertSvdfStatsToQDQs>(ctx, quant_specs_);
401404
}
402-
(void)applyPatternsAndFoldGreedily(func, std::move(patterns_2),
405+
(void)applyPatternsAndFoldGreedily(
406+
func, std::move(patterns_2),
403407
// TODO(fengliuai): Fix the logic to work without this flag
404408
/*useTopDownTraversal=*/false);
405409

‎tensorflow/lite/experimental/quantization_debugger/debugger_test.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import csv
1818
import io
19+
import re
1920

2021
from unittest import mock
2122
from absl.testing import parameterized
@@ -143,11 +144,15 @@ def test_quantization_debugger_layer_metrics(self, quantized_io):
143144
'tensor_idx': 7 if quantized_io else 8,
144145
'scales': [0.15686275],
145146
'zero_points': [-128],
146-
'tensor_name': 'Identity' if quantized_io else 'Identity4'
147+
'tensor_name': r'Identity[1-9]?$'
147148
})
148149
for key, value in expected_values.items():
149150
if isinstance(value, str):
150-
self.assertEqual(value, actual_values[key])
151+
self.assertIsNotNone(
152+
re.match(value, actual_values[key]),
153+
'String is different from expected string. Please fix test code if'
154+
" it's being affected by graph manipulation changes."
155+
)
151156
elif isinstance(value, list):
152157
self.assertAlmostEqual(
153158
value[0], float(actual_values[key][1:-1]), places=5)

0 commit comments

Comments
 (0)