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/Dialect/Quant/QuantTypes.h" // from @llvm-project 26 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project 27 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project 28 #include "mlir/IR/PatternMatch.h" // from @llvm-project 29 #include "mlir/Support/LLVM.h" // from @llvm-project 30 #include "tensorflow/core/framework/kernel_shape_util.h" 31 #include "tensorflow/core/kernels/conv_grad_shape_utils.h" 32 #include "tensorflow/core/util/padding.h" 33 #include "tensorflow/core/util/tensor_format.h" 34 35 namespace mlir { 36 namespace tosa { 37 38 // Create a TOSA rescale op from TFLite scaling, zero points and rounding mode 39 Value buildRescale(PatternRewriter& rewriter, Operation* op, 40 ShapedType output_type, Value input_val, double scale, 41 int64_t input_zp, int64_t output_zp, bool double_round, 42 bool scale32); 43 44 // Creates TOSA rescale op with int32 output 45 Value buildRescaleToInt32(PatternRewriter& rewriter, Operation* op, 46 Value input_val, double input_scale, 47 int64_t input_zp); 48 49 // Creates TOSA rescale op with int32 input 50 Value buildRescaleFromInt32(PatternRewriter& rewriter, Operation* op, 51 ShapedType output_type, Value input_val, 52 double output_scale, int64_t output_zp); 53 54 // Creates a TOSA rescale op based on conv2d parameters. 55 Value buildRescaleOpConvOutput(PatternRewriter& rewriter, Operation* op, 56 Value conv_val, ShapedType input_type, 57 ShapedType weight_type, ShapedType output_type); 58 59 // Create a 8-bit TOSA TABLE constant tensor 60 Value getTosaConst8bitTable(PatternRewriter& rewriter, Operation* op, 61 double input_scale, int32_t input_zp, 62 double output_scale, int32_t output_zp, 63 std::function<double(double)> func); 64 65 // Create a 16-bit TOSA TABLE constant tensor 66 Value getTosaConst16bitTable(PatternRewriter& rewriter, Operation* op, 67 std::function<double(double)> func, double min, 68 double max); 69 70 // Create a 32-bit TOSA TABLE constant tensor 71 // Output is restricted to [-1.0, 1.0] as s0.31 format 72 void getTosaConst32bitTable(PatternRewriter& rewriter, Operation* op, 73 double input_scale, int32_t input_zp, 74 std::function<double(double)> func, 75 Value& upper_const, Value& lower_const); 76 77 // Create a 32-bit float constant operator from a float 78 Value getTosaConstTensorSingleF32(PatternRewriter& rewriter, Operation* op, 79 float val); 80 81 // Create a 32-bit integer constant operator from an int 82 Value getTosaConstTensorSingleI32(PatternRewriter& rewriter, Operation* op, 83 int32_t val); 84 85 // Create a vector from a 32-bit value tensor. Returns vector size on success 86 // or -1 on error. 87 LogicalResult getVectorFromValue32(Value val, SmallVectorImpl<int32_t>& vec); 88 89 // Calculates the TOSA padding values based on TF operators padded with 90 // SAME/VALID. 91 bool getPaddingValuesFromPadType(tensorflow::Padding tf_pad, 92 tensorflow::TensorFormat data_format_tf, 93 uint32_t first_filter_spatial_dim, 94 ShapedType input_type, ShapedType filter_type, 95 ArrayAttr strides, ArrayAttr dilations, 96 PatternRewriter& rewriter, 97 ArrayAttr& explicit_pad); 98 99 // Calculates the TOSA padding values for explicit-padded TF operators. 100 ArrayAttr getPaddingValuesFromExplicitPadAttr( 101 ArrayAttr explicit_pad, tensorflow::TensorFormat data_format_tf, 102 PatternRewriter& rewriter); 103 104 // Calculates the TOSA padding values for transposeConv2d 105 bool getTransposeConv2dPaddingValues( 106 tensorflow::Padding tf_pad, tensorflow::TensorFormat data_format_tf, 107 uint32_t first_filter_spatial_dim, ShapedType input_type, 108 ShapedType filter_type, ShapedType output_type, ArrayAttr strides, 109 ArrayAttr dilations, PatternRewriter& rewriter, ArrayAttr& explicit_pad); 110 111 // Templated function to create a constant op for given type and shape. 112 // T: storage C type. 113 // Default template creates a constant tensor in T. 114 // To create INT48 TOSA constant, need to pass in llvm::APInt instead. 115 template <typename T> 116 llvm::Optional<Value> getConstTensor(PatternRewriter& rewriter, Operation* op, 117 ArrayRef<T> vec, ArrayRef<int64_t> shape); 118 119 // Check if scale32 mode is used for given output_element_type 120 bool isScale32(mlir::quant::UniformQuantizedType output_element_type); 121 122 } // namespace tosa 123 } // namespace mlir 124 125 #endif // TENSORFLOW_COMPILER_MLIR_TOSA_TRANSFORMS_LEGALIZE_UTILS_H 126