Home | History | Annotate | Download | only in kernels
      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 <string.h>
     16 #include <vector>
     17 
     18 #include "tensorflow/lite/c/builtin_op_data.h"
     19 #include "tensorflow/lite/c/c_api_internal.h"
     20 #include "tensorflow/lite/kernels/internal/optimized/optimized_ops.h"
     21 #include "tensorflow/lite/kernels/internal/reference/integer_ops/dequantize.h"
     22 #include "tensorflow/lite/kernels/internal/tensor.h"
     23 #include "tensorflow/lite/kernels/kernel_util.h"
     24 #include "tensorflow/lite/kernels/op_macros.h"
     25 
     26 namespace tflite {
     27 namespace ops {
     28 namespace builtin {
     29 namespace dequantize {
     30 
     31 struct OpContext {
     32   OpContext(TfLiteContext* context, TfLiteNode* node) {
     33     input = GetInput(context, node, 0);
     34     output = GetOutput(context, node, 0);
     35   }
     36   const TfLiteTensor* input;
     37   TfLiteTensor* output;
     38 };
     39 
     40 struct OpData {
     41   // This boolean value is only used when the input tensor is constant.
     42   bool float_dequantized_weights_initialized;
     43 };
     44 
     45 void* Init(TfLiteContext* context, const char* buffer, size_t length) {
     46   auto* op_data = new OpData();
     47   op_data->float_dequantized_weights_initialized = false;
     48   return op_data;
     49 }
     50 
     51 void Free(TfLiteContext* context, void* buffer) {
     52   delete reinterpret_cast<OpData*>(buffer);
     53 }
     54 
     55 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
     56   TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
     57   TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
     58 
     59   OpContext op_context(context, node);
     60 
     61   TF_LITE_ENSURE(context, op_context.input->type == kTfLiteUInt8 ||
     62                               op_context.input->type == kTfLiteInt8);
     63 
     64   op_context.output->type = kTfLiteFloat32;
     65   // If the input tensor is constant, we can persist the dequantized value in
     66   // the output tensor. Otherwise we run dequantize upon each eval.
     67   if (IsConstantTensor(op_context.input)) {
     68     op_context.output->allocation_type = kTfLiteArenaRwPersistent;
     69   }
     70   return context->ResizeTensor(context, op_context.output,
     71                                TfLiteIntArrayCopy(op_context.input->dims));
     72 }
     73 
     74 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
     75   OpData* op_data = reinterpret_cast<OpData*>(node->user_data);
     76   OpContext op_context(context, node);
     77   if (IsConstantTensor(op_context.input) &&
     78       op_data->float_dequantized_weights_initialized) {
     79     return kTfLiteOk;
     80   }
     81 
     82   tflite::DequantizationParams op_params;
     83   op_params.zero_point = op_context.input->params.zero_point;
     84   op_params.scale = op_context.input->params.scale;
     85   switch (op_context.input->type) {
     86     case kTfLiteUInt8:
     87       optimized_ops::Dequantize(op_params, GetTensorShape(op_context.input),
     88                                 GetTensorData<uint8_t>(op_context.input),
     89                                 GetTensorShape(op_context.output),
     90                                 GetTensorData<float>(op_context.output));
     91       break;
     92     case kTfLiteInt8:
     93       reference_integer_ops::Dequantize(
     94           op_params, GetTensorShape(op_context.input),
     95           GetTensorData<int8_t>(op_context.input),
     96           GetTensorShape(op_context.output),
     97           GetTensorData<float>(op_context.output));
     98       break;
     99     default:
    100       context->ReportError(context, "Type %d not supported.",
    101                            op_context.input->type);
    102       return kTfLiteError;
    103   }
    104 
    105   if (IsConstantTensor(op_context.input)) {
    106     op_data->float_dequantized_weights_initialized = true;
    107   }
    108 
    109   return kTfLiteOk;
    110 }
    111 
    112 }  // namespace dequantize
    113 
    114 TfLiteRegistration* Register_DEQUANTIZE_OPT() {
    115   static TfLiteRegistration r = {dequantize::Init, dequantize::Free,
    116                                  dequantize::Prepare, dequantize::Eval};
    117   return &r;
    118 }
    119 
    120 TfLiteRegistration* Register_DEQUANTIZE() { return Register_DEQUANTIZE_OPT(); }
    121 
    122 }  // namespace builtin
    123 }  // namespace ops
    124 }  // namespace tflite
    125