1 /* Copyright 2017 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/contrib/lite/builtin_op_data.h" 16 #include "tensorflow/contrib/lite/context.h" 17 #include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" 18 #include "tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h" 19 #include "tensorflow/contrib/lite/kernels/internal/tensor.h" 20 #include "tensorflow/contrib/lite/kernels/kernel_util.h" 21 #include "tensorflow/contrib/lite/kernels/op_macros.h" 22 23 namespace tflite { 24 namespace ops { 25 namespace builtin { 26 namespace l2norm { 27 28 // This file has two implementation of L2Norm. 29 enum KernelType { 30 kReference, 31 kGenericOptimized, 32 }; 33 34 constexpr int kInputTensor = 0; 35 constexpr int kOutputTensor = 0; 36 37 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { 38 auto* params = reinterpret_cast<TfLiteL2NormParams*>(node->builtin_data); 39 40 TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); 41 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); 42 43 TfLiteTensor* input = GetInput(context, node, kInputTensor); 44 TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 45 46 TF_LITE_ENSURE(context, NumDimensions(input) <= 4); 47 48 // TODO(ahentz): Our current implementations only support float32. 49 TF_LITE_ENSURE_EQ(context, output->type, kTfLiteFloat32); 50 TF_LITE_ENSURE_EQ(context, input->type, output->type); 51 52 // TODO(ahentz): For some reason our implementations don't support 53 // activations. 54 TF_LITE_ENSURE_EQ(context, params->activation, kTfLiteActNone); 55 56 TfLiteIntArray* output_size = TfLiteIntArrayCopy(input->dims); 57 return context->ResizeTensor(context, output, output_size); 58 } 59 60 template <KernelType kernel_type> 61 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { 62 TfLiteTensor* input = GetInput(context, node, kInputTensor); 63 TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 64 65 if (output->type == kTfLiteFloat32) { 66 #define TF_LITE_L2NORM(type) \ 67 type::L2Normalization<FusedActivationFunctionType::kNone>( \ 68 GetTensorData<float>(input), GetTensorDims(input), \ 69 GetTensorData<float>(output), GetTensorDims(output)) 70 71 if (kernel_type == kReference) { 72 TF_LITE_L2NORM(reference_ops); 73 } 74 if (kernel_type == kGenericOptimized) { 75 TF_LITE_L2NORM(optimized_ops); 76 } 77 #undef TF_LITE_L2NORM 78 } else { 79 context->ReportError(context, "Inputs and outputs not all float types."); 80 return kTfLiteError; 81 } 82 83 return kTfLiteOk; 84 } 85 86 } // namespace l2norm 87 88 TfLiteRegistration* Register_L2NORM_REF() { 89 static TfLiteRegistration r = {nullptr, nullptr, l2norm::Prepare, 90 l2norm::Eval<l2norm::kReference>}; 91 return &r; 92 } 93 94 TfLiteRegistration* Register_L2NORM_GENERIC_OPT() { 95 static TfLiteRegistration r = {nullptr, nullptr, l2norm::Prepare, 96 l2norm::Eval<l2norm::kGenericOptimized>}; 97 return &r; 98 } 99 100 TfLiteRegistration* Register_L2_NORMALIZATION() { 101 return Register_L2NORM_GENERIC_OPT(); 102 } 103 104 } // namespace builtin 105 } // namespace ops 106 } // namespace tflite 107