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
16 #include "tensorflow/lite/kernels/internal/reference/space_to_batch_nd.h"
17
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
20 #include "tensorflow/lite/kernels/internal/types.h"
21 #include "tensorflow/lite/kernels/kernel_util.h"
22 #include "tensorflow/lite/micro/kernels/kernel_util.h"
23 #include "tensorflow/lite/micro/micro_utils.h"
24
25 namespace tflite {
26
27 namespace {
28
29 constexpr int kInputTensor = 0;
30 constexpr int kBlockShapeTensor = 1;
31 constexpr int kCropsTensor = 2;
32 constexpr int kOutputTensor = 0;
33
34 // Currently, only 3D NHC and 4D NHWC input/output op_context are supported.
35 // In case of 3D input, it will be extended to 3D NHWC by adding W=1.
36 // The 4D array need to have exactly 2 spatial dimensions.
37 // TODO(b/149952582): Support arbitrary dimension in SpaceToBatchND.
38 const int kInputOutputMinDimensionNum = 3;
39 const int kInputOutputMaxDimensionNum = 4;
40
Init(TfLiteContext * context,const char * buffer,size_t length)41 void* Init(TfLiteContext* context, const char* buffer, size_t length) {
42 TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
43 return context->AllocatePersistentBuffer(context, sizeof(SpaceToBatchParams));
44 }
45
Prepare(TfLiteContext * context,TfLiteNode * node)46 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
47 TF_LITE_ENSURE_EQ(context, NumInputs(node), 3);
48 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
49
50 const TfLiteTensor* input = GetInput(context, node, kInputTensor);
51 TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
52 TF_LITE_ENSURE(context, input != nullptr && output != nullptr);
53
54 TF_LITE_ENSURE(context, NumDimensions(input) >= kInputOutputMinDimensionNum);
55 TF_LITE_ENSURE(context, NumDimensions(output) >= kInputOutputMinDimensionNum);
56 TF_LITE_ENSURE(context, NumDimensions(input) <= kInputOutputMaxDimensionNum);
57 TF_LITE_ENSURE(context, NumDimensions(output) <= kInputOutputMaxDimensionNum);
58 TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
59
60 return kTfLiteOk;
61 }
62
Eval(TfLiteContext * context,TfLiteNode * node)63 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
64 TFLITE_DCHECK(node->user_data != nullptr);
65 const SpaceToBatchParams& params =
66 *(static_cast<const SpaceToBatchParams*>(node->user_data));
67
68 const TfLiteEvalTensor* input =
69 tflite::micro::GetEvalInput(context, node, kInputTensor);
70 const TfLiteEvalTensor* block_shape =
71 tflite::micro::GetEvalInput(context, node, kBlockShapeTensor);
72 const TfLiteEvalTensor* crops =
73 tflite::micro::GetEvalInput(context, node, kCropsTensor);
74 TfLiteEvalTensor* output =
75 tflite::micro::GetEvalOutput(context, node, kOutputTensor);
76
77 switch (input->type) { // Already know in/out types are same.
78 case kTfLiteFloat32:
79 reference_ops::SpaceToBatchND(
80 params, tflite::micro::GetTensorShape(input),
81 tflite::micro::GetTensorData<float>(input),
82 tflite::micro::GetTensorShape(block_shape),
83 tflite::micro::GetTensorData<int32_t>(block_shape),
84 tflite::micro::GetTensorShape(crops),
85 tflite::micro::GetTensorData<int32_t>(crops),
86 tflite::micro::GetTensorShape(output),
87 tflite::micro::GetTensorData<float>(output));
88 break;
89 case kTfLiteInt8:
90 reference_ops::SpaceToBatchND(
91 params, tflite::micro::GetTensorShape(input),
92 tflite::micro::GetTensorData<int8_t>(input),
93 tflite::micro::GetTensorShape(block_shape),
94 tflite::micro::GetTensorData<int32_t>(block_shape),
95 tflite::micro::GetTensorShape(crops),
96 tflite::micro::GetTensorData<int32_t>(crops),
97 tflite::micro::GetTensorShape(output),
98 tflite::micro::GetTensorData<int8_t>(output));
99 break;
100 default:
101 TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
102 TfLiteTypeGetName(input->type), input->type);
103 return kTfLiteError;
104 }
105 return kTfLiteOk;
106 }
107
108 } // namespace.
109
Register_SPACE_TO_BATCH_ND()110 TfLiteRegistration Register_SPACE_TO_BATCH_ND() {
111 return {/*init=*/Init,
112 /*free=*/nullptr,
113 /*prepare=*/Prepare,
114 /*invoke=*/Eval,
115 /*profiling_string=*/nullptr,
116 /*builtin_code=*/0,
117 /*custom_name=*/nullptr,
118 /*version=*/0};
119 }
120
121 } // namespace tflite
122