• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 
16 #ifndef TENSORFLOW_COMPILER_MLIR_TOSA_TRANSFORMS_LEGALIZE_UTILS_H
17 #define TENSORFLOW_COMPILER_MLIR_TOSA_TRANSFORMS_LEGALIZE_UTILS_H
18 
19 #include <climits>
20 #include <cstddef>
21 #include <cstdint>
22 #include <iterator>
23 #include <numeric>
24 
25 #include "mlir/IR/BuiltinAttributes.h"  // from @llvm-project
26 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
27 #include "mlir/IR/PatternMatch.h"  // from @llvm-project
28 #include "mlir/Support/LLVM.h"  // from @llvm-project
29 #include "tensorflow/core/framework/kernel_shape_util.h"
30 #include "tensorflow/core/kernels/conv_grad_shape_utils.h"
31 #include "tensorflow/core/util/padding.h"
32 #include "tensorflow/core/util/tensor_format.h"
33 
34 namespace mlir {
35 namespace tosa {
36 
37 // Create a TOSA rescale op from TFLite scaling, zero points and rounding mode
38 Value buildRescale(PatternRewriter& rewriter, Operation* op,
39                    RankedTensorType output_type, Value input_val, double scale,
40                    int64_t input_zp, int64_t output_zp,
41                    bool double_round = false);
42 
43 // Creates TOSA rescale op with int32 output
44 Value buildRescaleToInt32(PatternRewriter& rewriter, Operation* op,
45                           Value input_val, double input_scale,
46                           int64_t input_zp);
47 
48 // Creates TOSA rescale op with int32 input
49 Value buildRescaleFromInt32(PatternRewriter& rewriter, Operation* op,
50                             RankedTensorType output_type, Value input_val,
51                             double output_scale, int64_t output_zp);
52 
53 // Creates a TOSA rescale op based on conv2d parameters.
54 Value buildRescaleOpConvOutput(PatternRewriter& rewriter, Operation* op,
55                                Value conv_val, RankedTensorType input_type,
56                                RankedTensorType weight_type,
57                                RankedTensorType output_type);
58 
59 // Create a 513 entry TOSA constant tensor suitable for the Table operator based
60 // on the values from an int32_t func(int32_t) lambda function.
61 Value getTosa1DConstTensorTable(PatternRewriter& rewriter, Operation* op,
62                                 std::function<int32_t(int32_t)> func);
63 
64 // Create a 32-bit float constant operator from a float
65 Value getTosaConstTensorSingleF32(PatternRewriter& rewriter, Operation* op,
66                                   float val);
67 
68 // Create a 32-bit integer constant operator from an int
69 Value getTosaConstTensorSingleI32(PatternRewriter& rewriter, Operation* op,
70                                   int32_t val);
71 
72 // Create a vector from a 32-bit value tensor.  Returns vector size on success
73 // or -1 on error.
74 int getVectorFromValue32(Value val, SmallVector<int32_t, 4>& vec);
75 
76 // Calculates the TOSA padding values based on TF operators padded with
77 // SAME/VALID.
78 bool getPaddingValuesFromPadType(
79     tensorflow::Padding tf_pad, tensorflow::TensorFormat data_format_tf,
80     uint32_t first_filter_spatial_dim, RankedTensorType input_type,
81     RankedTensorType filter_type, ArrayAttr strides, ArrayAttr dilations,
82     PatternRewriter& rewriter, ArrayAttr& explicit_pad);
83 
84 // Calculates the TOSA padding values for explicit-padded TF operators.
85 ArrayAttr getPaddingValuesFromExplicitPadAttr(
86     ArrayAttr explicit_pad, tensorflow::TensorFormat data_format_tf,
87     PatternRewriter& rewriter);
88 
89 // Calculates the TOSA padding values for transposeConv2d
90 bool getTransposeConv2dPaddingValues(
91     tensorflow::Padding tf_pad, tensorflow::TensorFormat data_format_tf,
92     uint32_t first_filter_spatial_dim, RankedTensorType input_type,
93     RankedTensorType filter_type, RankedTensorType output_type,
94     ArrayAttr strides, ArrayAttr dilations, PatternRewriter& rewriter,
95     ArrayAttr& explicit_pad);
96 
97 // Templated function to create a constant op in a given dialect and with a
98 // given type.  Specializations below.
99 
100 // T0: target dialect constant op
101 // T1: native c++ integer type
102 template <typename T0, typename T1>
103 Value get1DConstTensor(PatternRewriter& rewriter, Operation* op,
104                        SmallVector<T1, 8> arr);
105 
106 // Same as get1DConstTensor, but int48 is not native c++ type, needs additional
107 // interface
108 Value get1DConstTensorInt48(PatternRewriter& rewriter, Operation* op,
109                             SmallVector<int64_t, 8> arr);
110 
111 // Strip off quantization information for bias tensor and return a unquantized
112 // bias
113 Value getUnquantizedBias(PatternRewriter& rewriter, Operation* op, Value input);
114 
115 }  // namespace tosa
116 }  // namespace mlir
117 
118 #endif  // TENSORFLOW_COMPILER_MLIR_TOSA_TRANSFORMS_LEGALIZE_UTILS_H
119