1 /* Copyright 2018 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/kernel_util.h" 19 #include "tensorflow/lite/kernels/op_macros.h" 20 #include "tensorflow/lite/string_util.h" 21 22 namespace tflite { 23 namespace ops { 24 namespace builtin { 25 namespace select { 26 27 constexpr int kInputTensorCondition = 0; 28 constexpr int kInputTensorX = 1; 29 constexpr int kInputTensorY = 2; 30 constexpr int kOutputTensor = 0; 31 32 TfLiteStatus SelectPrepare(TfLiteContext* context, TfLiteNode* node) { 33 TF_LITE_ENSURE_EQ(context, NumInputs(node), 3); 34 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); 35 36 const TfLiteTensor* input_condition = 37 GetInput(context, node, kInputTensorCondition); 38 const TfLiteTensor* input_x = GetInput(context, node, kInputTensorX); 39 const TfLiteTensor* input_y = GetInput(context, node, kInputTensorY); 40 TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 41 42 // Input must be bool. 43 TF_LITE_ENSURE(context, input_condition->type == kTfLiteBool); 44 45 // Input tensors must have the same type and size 46 TF_LITE_ENSURE_EQ(context, input_x->type, input_y->type); 47 TF_LITE_ENSURE(context, HaveSameShapes(input_x, input_y)); 48 output->type = input_x->type; 49 50 // Either the same shape, or input_condition must be Rank 1 and match over the 51 // first dimension. 52 bool same_shape = HaveSameShapes(input_condition, input_x); 53 if (!same_shape && NumDimensions(input_condition) == 1) { 54 same_shape = 55 SizeOfDimension(input_condition, 0) == SizeOfDimension(input_x, 0); 56 } 57 58 TF_LITE_ENSURE(context, same_shape); 59 60 TfLiteIntArray* output_size = TfLiteIntArrayCopy(input_x->dims); 61 return context->ResizeTensor(context, output, output_size); 62 } 63 64 TfLiteStatus SelectEval(TfLiteContext* context, TfLiteNode* node) { 65 const TfLiteTensor* input_condition = 66 GetInput(context, node, kInputTensorCondition); 67 const TfLiteTensor* input_x = GetInput(context, node, kInputTensorX); 68 const TfLiteTensor* input_y = GetInput(context, node, kInputTensorY); 69 TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 70 71 bool is_rank_one = !HaveSameShapes(input_condition, input_x); 72 73 #define TF_LITE_SELECT(type, op) \ 74 reference_ops::op(GetTensorShape(input_condition), \ 75 GetTensorData<bool>(input_condition), \ 76 GetTensorShape(input_x), GetTensorData<type>(input_x), \ 77 GetTensorShape(input_y), GetTensorData<type>(input_y), \ 78 GetTensorShape(output), GetTensorData<type>(output)); 79 80 #define TF_LITE_SWITCH(type, op) \ 81 switch (type) { \ 82 break; \ 83 case kTfLiteBool: \ 84 TF_LITE_SELECT(bool, op); \ 85 break; \ 86 case kTfLiteFloat32: \ 87 TF_LITE_SELECT(float, op); \ 88 break; \ 89 case kTfLiteUInt8: \ 90 TF_LITE_SELECT(uint8_t, op); \ 91 break; \ 92 case kTfLiteInt8: \ 93 TF_LITE_SELECT(int8_t, op); \ 94 break; \ 95 case kTfLiteInt16: \ 96 TF_LITE_SELECT(int16_t, op); \ 97 break; \ 98 case kTfLiteInt32: \ 99 TF_LITE_SELECT(int32_t, op); \ 100 break; \ 101 case kTfLiteInt64: \ 102 TF_LITE_SELECT(int64_t, op); \ 103 break; \ 104 default: \ 105 context->ReportError(context, \ 106 "Does not support type other than bool|float|int, " \ 107 "got %d", \ 108 type); \ 109 return kTfLiteError; \ 110 } 111 112 if (is_rank_one) { 113 TF_LITE_SWITCH(input_x->type, RankOneSelect); 114 } else { 115 TF_LITE_SWITCH(input_x->type, Select); 116 } 117 118 #undef TF_LITE_SELECT 119 #undef TF_LITE_SWITCH 120 return kTfLiteOk; 121 } 122 123 } // namespace select 124 125 TfLiteRegistration* Register_SELECT() { 126 static TfLiteRegistration r = {nullptr, nullptr, select::SelectPrepare, 127 select::SelectEval}; 128 return &r; 129 } 130 131 } // namespace builtin 132 } // namespace ops 133 } // namespace tflite 134