xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/quantization/tensorflow/ops/tf_op_quant_spec.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/compiler/mlir/quantization/tensorflow/ops/tf_op_quant_spec.h"
16 
17 #include <algorithm>
18 #include <iterator>
19 #include <memory>
20 #include <vector>
21 
22 #include "absl/container/flat_hash_set.h"
23 
24 namespace mlir {
25 namespace quant {
26 
27 // TODO(b/228928859): Improve the getter function to match attributes rather
28 // than function name.
GetTFOpQuantSpec(Operation * op)29 std::unique_ptr<OpQuantSpec> GetTFOpQuantSpec(Operation* op) {
30   auto spec = std::make_unique<OpQuantSpec>();
31   if (auto call_op = dyn_cast<TF::PartitionedCallOp>(op)) {
32     StringRef function_name =
33         call_op.fAttr().cast<FlatSymbolRefAttr>().getValue();
34     if (!function_name.startswith("composite_")) {
35       return spec;
36     }
37     if (function_name.contains("depthwise_conv2d")) {
38       spec->coeff_op_quant_dim[1] = 3;
39       if (function_name.contains("with_bias")) {
40         spec->biases_params[2] = {{0, 1},
41                                   quant::GetUniformQuantizedTypeForBias};
42       }
43     } else if (function_name.contains("conv2d")) {
44       spec->coeff_op_quant_dim[1] = 3;
45       if (function_name.contains("with_bias")) {
46         spec->biases_params[2] = {{0, 1},
47                                   quant::GetUniformQuantizedTypeForBias};
48       }
49     } else if (function_name.contains("matmul")) {
50       spec->coeff_op_quant_dim[1] = -1;
51       if (function_name.contains("with_bias")) {
52         spec->biases_params[2] = {{0, 1},
53                                   quant::GetUniformQuantizedTypeForBias};
54       }
55     }
56     for (auto quantizable_operand : spec->coeff_op_quant_dim) {
57       spec->quantizable_operands.insert(quantizable_operand.first);
58     }
59   }
60   return spec;
61 }
62 
GetTfQuantScaleSpec(Operation * op)63 std::unique_ptr<OpQuantScaleSpec> GetTfQuantScaleSpec(Operation* op) {
64   auto scale_spec = std::make_unique<OpQuantScaleSpec>();
65   if (llvm::isa<
66           // clang-format off
67           // go/keep-sorted start
68           TF::AvgPoolOp,
69           TF::ConcatV2Op,
70           TF::IdentityOp,
71           TF::MaxPoolOp,
72           TF::PadV2Op,
73           TF::ReshapeOp,
74           TF::SqueezeOp
75           // go/keep-sorted end
76           // clang-format on
77           >(op)) {
78     scale_spec->has_same_scale_requirement = true;
79   }
80   return scale_spec;
81 }
82 
83 }  // namespace quant
84 }  // namespace mlir
85