Home | History | Annotate | Download | only in kernels
      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/kernels/kernel_util.h"
     16 
     17 #include <algorithm>
     18 #include <cmath>
     19 #include <memory>
     20 
     21 #include "tensorflow/contrib/lite/kernels/internal/round.h"
     22 
     23 namespace tflite {
     24 
     25 TfLiteStatus GetQuantizedConvolutionMultipler(
     26     TfLiteContext* context, TfLiteTensor* input, TfLiteTensor* filter,
     27     TfLiteTensor* bias, TfLiteTensor* output, double* multiplier) {
     28   const double input_product_scale = input->params.scale * filter->params.scale;
     29   const double bias_scale = bias->params.scale;
     30   const double output_scale = output->params.scale;
     31 
     32   // TODO(ahentz): The following conditions must be guaranteed by the training
     33   // pipeline.
     34   TF_LITE_ENSURE(context, std::abs(input_product_scale - bias_scale) <=
     35                               1e-6 * std::min(input_product_scale, bias_scale));
     36   TF_LITE_ENSURE(context, input_product_scale >= 0);
     37   TF_LITE_ENSURE(context, input_product_scale < output_scale);
     38 
     39   *multiplier = input_product_scale / output_scale;
     40 
     41   return kTfLiteOk;
     42 }
     43 
     44 void CalculateActivationRangeUint8(TfLiteFusedActivation activation,
     45                                    TfLiteTensor* output, int32_t* act_min,
     46                                    int32_t* act_max) {
     47   const int32_t qmin = std::numeric_limits<uint8_t>::min();
     48   const int32_t qmax = std::numeric_limits<uint8_t>::max();
     49 
     50   const auto scale = output->params.scale;
     51   const auto zero_point = output->params.zero_point;
     52 
     53   auto quantize = [scale, zero_point](float f) {
     54     return zero_point + static_cast<int32_t>(TfLiteRound(f / scale));
     55   };
     56 
     57   if (activation == kTfLiteActRelu) {
     58     *act_min = std::max(qmin, quantize(0.0));
     59     *act_max = qmax;
     60   } else if (activation == kTfLiteActRelu6) {
     61     *act_min = std::max(qmin, quantize(0.0));
     62     *act_max = std::min(qmax, quantize(6.0));
     63   } else if (activation == kTfLiteActRelu1) {
     64     *act_min = std::max(qmin, quantize(-1.0));
     65     *act_max = std::min(qmax, quantize(1.0));
     66   } else {
     67     *act_min = qmin;
     68     *act_max = qmax;
     69   }
     70 }
     71 
     72 void CalculateActivationRangeFloat(TfLiteFusedActivation activation,
     73                                    float* activation_min,
     74                                    float* activation_max) {
     75   if (activation == kTfLiteActRelu) {
     76     *activation_min = 0.f;
     77     *activation_max = std::numeric_limits<float>::max();
     78   } else if (activation == kTfLiteActRelu6) {
     79     *activation_min = 0.f;
     80     *activation_max = 6.f;
     81   } else if (activation == kTfLiteActRelu1) {
     82     *activation_min = -1.f;
     83     *activation_max = 1.f;
     84   } else {
     85     *activation_min = std::numeric_limits<float>::lowest();
     86     *activation_max = std::numeric_limits<float>::max();
     87   }
     88 }
     89 
     90 bool HaveSameShapes(TfLiteTensor* input1, TfLiteTensor* input2) {
     91   return TfLiteIntArrayEqual(input1->dims, input2->dims);
     92 }
     93 
     94 TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context,
     95                                         TfLiteTensor* input1,
     96                                         TfLiteTensor* input2,
     97                                         TfLiteIntArray** output_shape) {
     98   int64_t dims1 = NumDimensions(input1);
     99   int64_t dims2 = NumDimensions(input2);
    100   int64_t out_dims = std::max(dims1, dims2);
    101   std::unique_ptr<TfLiteIntArray, void (*)(TfLiteIntArray*)> shape(
    102       TfLiteIntArrayCreate(out_dims), TfLiteIntArrayFree);
    103   for (int i = 0; i < out_dims; ++i) {
    104     int64_t d1 = i >= dims1 ? 1 : SizeOfDimension(input1, dims1 - i - 1);
    105     int64_t d2 = i >= dims2 ? 1 : SizeOfDimension(input2, dims2 - i - 1);
    106     TF_LITE_ENSURE(context, d1 == d2 || d1 == 1 || d2 == 1);
    107     shape->data[out_dims - i - 1] = std::max(d1, d2);
    108   }
    109   *output_shape = shape.release();
    110   return kTfLiteOk;
    111 }
    112 
    113 }  // namespace tflite
    114