Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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 
     16 // See docs in ../ops/array_ops.cc.
     17 
     18 #define EIGEN_USE_THREADS
     19 
     20 #include <math.h>
     21 
     22 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     23 #include "tensorflow/core/framework/op.h"
     24 #include "tensorflow/core/framework/op_kernel.h"
     25 #include "tensorflow/core/framework/type_traits.h"
     26 #include "tensorflow/core/framework/types.h"
     27 #include "tensorflow/core/kernels/meta_support.h"
     28 #include "tensorflow/core/kernels/quantization_utils.h"
     29 #include "tensorflow/core/lib/core/errors.h"
     30 
     31 namespace tensorflow {
     32 
     33 typedef Eigen::ThreadPoolDevice CPUDevice;
     34 
     35 template <class T1, class T2>
     36 class QuantizeDownAndShrinkRangeOp : public OpKernel {
     37  public:
     38   explicit QuantizeDownAndShrinkRangeOp(OpKernelConstruction* ctx)
     39       : OpKernel(ctx) {}
     40 
     41   void Compute(OpKernelContext* ctx) override {
     42     const Tensor& input = ctx->input(0);
     43     const float input_min_float = ctx->input(1).flat<float>()(0);
     44     const float input_max_float = ctx->input(2).flat<float>()(0);
     45     Tensor* output = nullptr;
     46     OP_REQUIRES_OK(ctx, ctx->allocate_output(0, input.shape(), &output));
     47     Tensor* output_min = nullptr;
     48     OP_REQUIRES_OK(ctx, ctx->allocate_output(1, TensorShape({}), &output_min));
     49     Tensor* output_max = nullptr;
     50     OP_REQUIRES_OK(ctx, ctx->allocate_output(2, TensorShape({}), &output_max));
     51 
     52     // See QuantizationRangeOp as well, which has a copy of this logic.
     53     auto input_array = input.flat<T1>();
     54     const int32 input_lowest_quantized =
     55         static_cast<int32>(Eigen::NumTraits<T1>::lowest());
     56     const int32 input_highest_quantized =
     57         static_cast<int32>(Eigen::NumTraits<T1>::highest());
     58     T1 actual_min_quantized = input_highest_quantized;
     59     T1 actual_max_quantized = input_lowest_quantized;
     60     for (int i = 0; i < input_array.size(); ++i) {
     61       const T1 value = input_array(i);
     62       actual_min_quantized = std::min(actual_min_quantized, value);
     63       actual_max_quantized = std::max(actual_max_quantized, value);
     64     }
     65     // We want to make sure that the minimum is no larger than zero, so that the
     66     // convolution operation can run efficiently.
     67     const float actual_min_float =
     68         std::min(0.0f, QuantizedToFloat(actual_min_quantized, input_min_float,
     69                                         input_max_float));
     70     const float actual_max_float = QuantizedToFloat(
     71         actual_max_quantized, input_min_float, input_max_float);
     72 
     73 #if 0
     74     // This is the reference, non-eigen implementation:
     75     auto output_array = output->flat<T2>();
     76     RequantizeManyInNewRange<T1, T2>(input_array.data(), input_array.size(),
     77                                      input_min_float, input_max_float,
     78                                      actual_min_float, actual_max_float,
     79                                      output_array.data());
     80 #endif
     81 
     82     if (input_array.size() > 0) {
     83       if (meta::IsSupportedAndEnabled() && std::is_same<T1, qint32>() &&
     84           std::is_same<T2, quint8>()) {
     85         auto input_i32_array = input.flat<qint32>();
     86         meta::Requantize(ctx, input_i32_array.data(), input_i32_array.size(),
     87                          input_min_float, input_max_float, actual_min_float,
     88                          actual_max_float, output->flat<quint8>().data());
     89       } else {
     90         RequantizeManyInNewRangeUsingEigen<T1, T2>(
     91             ctx->eigen_device<CPUDevice>(), input, input_min_float,
     92             input_max_float, actual_min_float, actual_max_float, output);
     93       }
     94     }
     95 
     96     output_min->flat<float>().setConstant(actual_min_float);
     97     output_max->flat<float>().setConstant(actual_max_float);
     98   }
     99 };
    100 
    101 REGISTER_KERNEL_BUILDER(Name("QuantizeDownAndShrinkRange")
    102                             .Device(DEVICE_CPU)
    103                             .TypeConstraint<qint32>("Tinput")
    104                             .TypeConstraint<quint8>("out_type"),
    105                         QuantizeDownAndShrinkRangeOp<qint32, quint8>);
    106 
    107 }  // namespace tensorflow
    108