• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/internal/types.h"
20 #include "tensorflow/lite/kernels/kernel_util.h"
21 
22 namespace tflite {
23 namespace ops {
24 namespace builtin {
25 namespace where {
26 
27 constexpr int kInputConditionTensor = 0;
28 constexpr int kOutputTensor = 0;
29 
ResizeOutputTensor(TfLiteContext * context,const TfLiteTensor * cond_tensor,TfLiteTensor * output_tensor)30 TfLiteStatus ResizeOutputTensor(TfLiteContext* context,
31                                 const TfLiteTensor* cond_tensor,
32                                 TfLiteTensor* output_tensor) {
33   // Output tensor should have shape:
34   // (num_true, cond_rank), where num_true denotes the number of true values
35   // in condition.
36   const RuntimeShape& cond_shape = GetTensorShape(cond_tensor);
37   const int size = cond_shape.FlatSize();
38   const int cond_rank = cond_shape.DimensionsCount();
39   const bool* cond_data = GetTensorData<bool>(cond_tensor);
40 
41   int true_count = 0;
42   for (int i = 0; i < size; ++i) {
43     if (cond_data[i]) {
44       true_count++;
45     }
46   }
47   TfLiteIntArray* output_dims = TfLiteIntArrayCreate(2);
48   output_dims->data[0] = true_count;
49   output_dims->data[1] = cond_rank;
50   return context->ResizeTensor(context, output_tensor, output_dims);
51 }
52 
Prepare(TfLiteContext * context,TfLiteNode * node)53 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
54   TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
55   TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
56 
57   const TfLiteTensor* cond_tensor =
58       GetInput(context, node, kInputConditionTensor);
59   TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
60 
61   if (cond_tensor->type != kTfLiteBool) {
62     context->ReportError(context,
63                          "Condition tensor must be of type bool, but saw '%s'.",
64                          TfLiteTypeGetName(cond_tensor->type));
65     return kTfLiteError;
66   }
67 
68   // As output will be a 2D tensor of indices, we use int32 as data type.
69   output->type = kTfLiteInt32;
70 
71   // Exit early if cond is a non-const tensor. Set output tensor to dynamic so
72   // output size can be determined in Eval.
73   if (!IsConstantTensor(cond_tensor)) {
74     SetTensorToDynamic(output);
75     return kTfLiteOk;
76   }
77   return ResizeOutputTensor(context, cond_tensor, output);
78 }
79 
Eval(TfLiteContext * context,TfLiteNode * node)80 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
81   const TfLiteTensor* cond_tensor =
82       GetInput(context, node, kInputConditionTensor);
83   TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
84 
85   if (IsDynamicTensor(output)) {
86     TF_LITE_ENSURE_OK(context,
87                       ResizeOutputTensor(context, cond_tensor, output));
88   }
89 
90   reference_ops::SelectTrueCoords(GetTensorShape(cond_tensor),
91                                   GetTensorData<bool>(cond_tensor),
92                                   GetTensorData<int32_t>(output));
93   return kTfLiteOk;
94 }
95 }  // namespace where
96 
Register_WHERE()97 TfLiteRegistration* Register_WHERE() {
98   static TfLiteRegistration r = {/*init*/ nullptr, /*free*/ nullptr,
99                                  where::Prepare, where::Eval};
100   return &r;
101 }
102 
103 }  // namespace builtin
104 }  // namespace ops
105 }  // namespace tflite
106