1 /* Copyright 2021 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 #ifndef TENSORFLOW_LITE_TOOLS_VERSIONING_OP_SIGNATURE_H_ 16 #define TENSORFLOW_LITE_TOOLS_VERSIONING_OP_SIGNATURE_H_ 17 18 #include <string> 19 20 #include "tensorflow/lite/c/c_api_types.h" 21 #include "tensorflow/lite/c/common.h" 22 #include "tensorflow/lite/schema/schema_generated.h" 23 24 namespace tflite { 25 26 // OpSignature contains operator parameters for version functions. 27 typedef struct { 28 TfLiteType type; 29 std::vector<int32_t> dims; 30 bool is_const; 31 } OpSignatureTensorSpec; 32 33 typedef struct { 34 BuiltinOperator op; 35 std::vector<OpSignatureTensorSpec> inputs; 36 std::vector<OpSignatureTensorSpec> outputs; 37 void* builtin_data; 38 const void* custom_initial_data; 39 std::string custom_name; 40 union { 41 struct { 42 bool is_per_channel_quantized; 43 } conv_2d; 44 struct { 45 bool is_per_channel_quantized; 46 } depthwise_conv_2d; 47 struct { 48 // TODO(b/156530611): Make this global when more ops support sparse 49 // computation. 50 bool sparse_weight; 51 } fully_connected; 52 struct { 53 float input1_scale; 54 float input2_scale; 55 float output_scale; 56 } mul; 57 struct { 58 int32_t num_dims; 59 } strided_slice; 60 struct { 61 bool input_quantized; 62 } abs; 63 struct { 64 bool is_per_channel_quantized; 65 } dequantize; 66 } ext_options; 67 } OpSignature; 68 69 // Generate OpSignature with the given OperatorCode, Operator and Tensors (from 70 // SubGraph). The OpSignature will be used by GetBuiltinOperatorVersion() and 71 // mostly input and output tensor types are enough to figure out op version. 72 // But some ops (DEPTHWISE_CONV_2D, FULLY_CONNECTED, ...) require to pass their 73 // options to decide op version. 74 // 75 // WARNING: The caller is responsible to free the allocated 76 // OpSignature.builtin_data memory. 77 OpSignature GetOpSignature(const OperatorCode* op_code, const Operator* op, 78 const SubGraph* subgraph, const Model* model); 79 80 // Generate OpSignature with the given TfLiteContext, TfLiteNode and 81 // TfLiteRegistration. 82 // The function can be used by a compatibility checker of a delegate such as 83 // TFLiteOperationParser::IsSupported() in the GPU delegate. 84 OpSignature GetOpSignature(const TfLiteContext* context, const TfLiteNode* node, 85 const TfLiteRegistration* registration); 86 } // namespace tflite 87 #endif // TENSORFLOW_LITE_TOOLS_VERSIONING_OP_SIGNATURE_H_ 88