diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2018-05-09 18:45:13 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-05-09 18:47:47 -0700 |
commit | 2e7329d75b1c8da9e12000cb15972f123438623c (patch) | |
tree | d64afe71e7b339d853aba5262558e873fe0755a7 /tensorflow/contrib/lite/kernels/elementwise.cc | |
parent | 901035bbe15d8a20cf619a2dca6c46fa4f6e8a76 (diff) |
Implement sin operator
PiperOrigin-RevId: 196062186
Diffstat (limited to 'tensorflow/contrib/lite/kernels/elementwise.cc')
-rw-r--r-- | tensorflow/contrib/lite/kernels/elementwise.cc | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tensorflow/contrib/lite/kernels/elementwise.cc b/tensorflow/contrib/lite/kernels/elementwise.cc new file mode 100644 index 0000000000..6588256df7 --- /dev/null +++ b/tensorflow/contrib/lite/kernels/elementwise.cc @@ -0,0 +1,67 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include <cmath> +#include "tensorflow/contrib/lite/context.h" +#include "tensorflow/contrib/lite/kernels/internal/tensor.h" +#include "tensorflow/contrib/lite/kernels/kernel_util.h" + +namespace tflite { +namespace ops { +namespace builtin { +namespace elementwise { + +TfLiteStatus SinPrepare(TfLiteContext* context, TfLiteNode* node) { + TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); + TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); + TfLiteTensor* input = GetInput(context, node, 0); + TfLiteTensor* output = GetOutput(context, node, 0); + TF_LITE_ENSURE_EQ(context, input->type, output->type); + // Quantized float is not supported yet. + TF_LITE_ENSURE_EQ(context, input->type, kTfLiteFloat32); + return context->ResizeTensor(context, output, + TfLiteIntArrayCopy(input->dims)); +} + +TfLiteStatus SinEval(TfLiteContext* context, TfLiteNode* node) { + TfLiteTensor* input = GetInput(context, node, 0); + TfLiteTensor* output = GetOutput(context, node, 0); + switch (input->type) { + case kTfLiteFloat32: { + size_t elements = NumElements(input); + float* in = GetTensorData<float>(input); + float* in_end = in + elements; + float* out = output->data.f; + for (; in < in_end; in++, out++) *out = std::sin(*in); + return kTfLiteOk; + } + default: { + context->ReportError(context, "Only float32 is supported currently"); + return kTfLiteError; + } + } +} + +} // namespace elementwise + +TfLiteRegistration* Register_SIN() { + static TfLiteRegistration r = {nullptr, nullptr, elementwise::SinPrepare, + elementwise::SinEval}; + return &r; +} + +} // namespace builtin +} // namespace ops +} // namespace tflite |