1 /* Copyright 2019 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 #include "tensorflow/lite/micro/kernels/conv.h"
17
18 #include "tensorflow/lite/c/builtin_op_data.h"
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/kernels/internal/common.h"
21 #include "tensorflow/lite/kernels/internal/quantization_util.h"
22 #include "tensorflow/lite/kernels/internal/reference/conv.h"
23 #include "tensorflow/lite/kernels/internal/reference/integer_ops/conv.h"
24 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
25 #include "tensorflow/lite/kernels/kernel_util.h"
26 #include "tensorflow/lite/kernels/padding.h"
27 #include "tensorflow/lite/micro/kernels/kernel_util.h"
28
29 namespace tflite {
30 namespace {
31
Init(TfLiteContext * context,const char * buffer,size_t length)32 void* Init(TfLiteContext* context, const char* buffer, size_t length) {
33 TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
34 return context->AllocatePersistentBuffer(context, sizeof(OpDataConv));
35 }
36
Eval(TfLiteContext * context,TfLiteNode * node)37 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
38 const TfLiteEvalTensor* input =
39 tflite::micro::GetEvalInput(context, node, kConvInputTensor);
40 const TfLiteEvalTensor* filter =
41 tflite::micro::GetEvalInput(context, node, kConvWeightsTensor);
42 const TfLiteEvalTensor* bias =
43 (NumInputs(node) == 3)
44 ? tflite::micro::GetEvalInput(context, node, kConvBiasTensor)
45 : nullptr;
46 TfLiteEvalTensor* output =
47 tflite::micro::GetEvalOutput(context, node, kConvOutputTensor);
48
49 TFLITE_DCHECK(node->builtin_data != nullptr);
50 const auto& params =
51 *(reinterpret_cast<TfLiteConvParams*>(node->builtin_data));
52 TFLITE_DCHECK(node->user_data != nullptr);
53 const auto& data = *(static_cast<const OpDataConv*>(node->user_data));
54
55 TF_LITE_ENSURE_EQ(context, input->type, output->type);
56 TF_LITE_ENSURE_MSG(
57 context,
58 input->type == filter->type ||
59 (input->type == kTfLiteInt16 && filter->type == kTfLiteInt8),
60 "Hybrid models are not supported on TFLite Micro.");
61
62 switch (input->type) { // Already know in/out types are same.
63 case kTfLiteFloat32: {
64 tflite::reference_ops::Conv(
65 ConvParamsFloat(params, data), tflite::micro::GetTensorShape(input),
66 tflite::micro::GetTensorData<float>(input),
67 tflite::micro::GetTensorShape(filter),
68 tflite::micro::GetTensorData<float>(filter),
69 tflite::micro::GetTensorShape(bias),
70 tflite::micro::GetTensorData<float>(bias),
71 tflite::micro::GetTensorShape(output),
72 tflite::micro::GetTensorData<float>(output),
73 tflite::micro::GetTensorShape(nullptr), nullptr);
74 break;
75 }
76 case kTfLiteInt16: {
77 reference_integer_ops::ConvPerChannel(
78 ConvParamsQuantized(params, data), data.per_channel_output_multiplier,
79 data.per_channel_output_shift, tflite::micro::GetTensorShape(input),
80 tflite::micro::GetTensorData<int16_t>(input),
81 tflite::micro::GetTensorShape(filter),
82 tflite::micro::GetTensorData<int8_t>(filter),
83 tflite::micro::GetTensorShape(bias),
84 tflite::micro::GetTensorData<std::int64_t>(bias),
85 tflite::micro::GetTensorShape(output),
86 tflite::micro::GetTensorData<int16_t>(output));
87 break;
88 }
89 case kTfLiteInt8: {
90 reference_integer_ops::ConvPerChannel(
91 ConvParamsQuantized(params, data), data.per_channel_output_multiplier,
92 data.per_channel_output_shift, tflite::micro::GetTensorShape(input),
93 tflite::micro::GetTensorData<int8_t>(input),
94 tflite::micro::GetTensorShape(filter),
95 tflite::micro::GetTensorData<int8_t>(filter),
96 tflite::micro::GetTensorShape(bias),
97 tflite::micro::GetTensorData<int32_t>(bias),
98 tflite::micro::GetTensorShape(output),
99 tflite::micro::GetTensorData<int8_t>(output));
100 break;
101 }
102 default:
103 TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
104 TfLiteTypeGetName(input->type), input->type);
105 return kTfLiteError;
106 }
107 return kTfLiteOk;
108 }
109
110 } // namespace
111
Register_CONV_2D()112 TfLiteRegistration Register_CONV_2D() {
113 return {/*init=*/Init,
114 /*free=*/nullptr,
115 /*prepare=*/ConvPrepare,
116 /*invoke=*/Eval,
117 /*profiling_string=*/nullptr,
118 /*builtin_code=*/0,
119 /*custom_name=*/nullptr,
120 /*version=*/0};
121 }
122
123 } // namespace tflite
124