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 // Padding can result in a larger output than input.
61 TF_LITE_ENSURE(context,
62 ElementCount(*output->dims) >= ElementCount(*input->dims));
63
64 return kTfLiteOk;
65 }
66
Eval(TfLiteContext * context,TfLiteNode * node)67 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
68 TFLITE_DCHECK(node->user_data != nullptr);
69 const SpaceToBatchParams& params =
70 *(static_cast<const SpaceToBatchParams*>(node->user_data));
71
72 const TfLiteEvalTensor* input =
73 tflite::micro::GetEvalInput(context, node, kInputTensor);
74 const TfLiteEvalTensor* block_shape =
75 tflite::micro::GetEvalInput(context, node, kBlockShapeTensor);
76 const TfLiteEvalTensor* crops =
77 tflite::micro::GetEvalInput(context, node, kCropsTensor);
78 TfLiteEvalTensor* output =
79 tflite::micro::GetEvalOutput(context, node, kOutputTensor);
80
81 switch (input->type) { // Already know in/out types are same.
82 case kTfLiteFloat32:
83 reference_ops::SpaceToBatchND(
84 params, tflite::micro::GetTensorShape(input),
85 tflite::micro::GetTensorData<float>(input),
86 tflite::micro::GetTensorShape(block_shape),
87 tflite::micro::GetTensorData<int32_t>(block_shape),
88 tflite::micro::GetTensorShape(crops),
89 tflite::micro::GetTensorData<int32_t>(crops),
90 tflite::micro::GetTensorShape(output),
91 tflite::micro::GetTensorData<float>(output));
92 break;
93 case kTfLiteInt8:
94 reference_ops::SpaceToBatchND(
95 params, tflite::micro::GetTensorShape(input),
96 tflite::micro::GetTensorData<int8_t>(input),
97 tflite::micro::GetTensorShape(block_shape),
98 tflite::micro::GetTensorData<int32_t>(block_shape),
99 tflite::micro::GetTensorShape(crops),
100 tflite::micro::GetTensorData<int32_t>(crops),
101 tflite::micro::GetTensorShape(output),
102 tflite::micro::GetTensorData<int8_t>(output));
103 break;
104 default:
105 TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
106 TfLiteTypeGetName(input->type), input->type);
107 return kTfLiteError;
108 }
109 return kTfLiteOk;
110 }
111
112 } // namespace.
113
Register_SPACE_TO_BATCH_ND()114 TfLiteRegistration Register_SPACE_TO_BATCH_ND() {
115 return {/*init=*/Init,
116 /*free=*/nullptr,
117 /*prepare=*/Prepare,
118 /*invoke=*/Eval,
119 /*profiling_string=*/nullptr,
120 /*builtin_code=*/0,
121 /*custom_name=*/nullptr,
122 /*version=*/0};
123 }
124
125 } // namespace tflite
126