Home | History | Annotate | Download | only in kernels
      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 #include "tensorflow/lite/c/c_api_internal.h"
     16 #include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
     17 #include "tensorflow/lite/kernels/internal/tensor.h"
     18 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
     19 #include "tensorflow/lite/kernels/kernel_util.h"
     20 
     21 namespace tflite {
     22 namespace ops {
     23 namespace builtin {
     24 namespace add_n {
     25 
     26 constexpr int kInputTensor1 = 0;
     27 constexpr int kOutputTensor = 0;
     28 
     29 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
     30   int num_inputs = NumInputs(node);
     31   TF_LITE_ENSURE(context, num_inputs >= 2);
     32   TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
     33 
     34   const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
     35   TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
     36   output->type = input1->type;
     37 
     38   // Check that all input tensors have the same shape and type.
     39   for (int i = kInputTensor1 + 1; i < num_inputs; ++i) {
     40     const TfLiteTensor* input = GetInput(context, node, i);
     41     TF_LITE_ENSURE(context, HaveSameShapes(input1, input));
     42     TF_LITE_ENSURE_EQ(context, input1->type, input->type);
     43   }
     44 
     45   // Use the first input node's dimension to be the dimension of the output
     46   // node.
     47   TfLiteIntArray* input1_dims = input1->dims;
     48   TfLiteIntArray* output_dims = TfLiteIntArrayCopy(input1_dims);
     49   return context->ResizeTensor(context, output, output_dims);
     50 }
     51 
     52 template <typename T>
     53 void EvalAddN(TfLiteContext* context, TfLiteNode* node) {
     54   // TODO(haoliang): Initialize all_inputs only once during init.
     55   VectorOfTensors<T> all_inputs(*context, *node->inputs);
     56   TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
     57   int num_inputs = NumInputs(node);
     58   const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
     59   reference_ops::AddN<T>(GetTensorShape(input1), num_inputs, all_inputs.data(),
     60                          GetTensorData<T>(output));
     61 }
     62 
     63 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
     64   const TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
     65   if (output->type == kTfLiteFloat32) {
     66     EvalAddN<float>(context, node);
     67   } else if (output->type == kTfLiteInt32) {
     68     EvalAddN<int32_t>(context, node);
     69   } else {
     70     context->ReportError(context,
     71                          "AddN only supports FLOAT32|INT32 now, got %s.",
     72                          TfLiteTypeGetName(output->type));
     73     return kTfLiteError;
     74   }
     75   return kTfLiteOk;
     76 }
     77 
     78 }  // namespace add_n
     79 
     80 TfLiteRegistration* Register_ADD_N() {
     81   static TfLiteRegistration r = {/*init*/ nullptr, /*free*/ nullptr,
     82                                  add_n::Prepare, add_n::Eval};
     83   return &r;
     84 }
     85 
     86 }  // namespace builtin
     87 }  // namespace ops
     88 }  // namespace tflite
     89