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 RequantizeOp : public OpKernel {
     37  public:
     38   explicit RequantizeOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
     39 
     40   void Compute(OpKernelContext* ctx) override {
     41     const Tensor& input = ctx->input(0);
     42     const float input_min_float = ctx->input(1).flat<float>()(0);
     43     const float input_max_float = ctx->input(2).flat<float>()(0);
     44     const float requested_output_min_float = ctx->input(3).flat<float>()(0);
     45     const float requested_output_max_float = ctx->input(4).flat<float>()(0);
     46 
     47     Tensor* output = nullptr;
     48     OP_REQUIRES_OK(ctx, ctx->allocate_output(0, input.shape(), &output));
     49     Tensor* output_min = nullptr;
     50     OP_REQUIRES_OK(ctx, ctx->allocate_output(1, TensorShape({}), &output_min));
     51     Tensor* output_max = nullptr;
     52     OP_REQUIRES_OK(ctx, ctx->allocate_output(2, TensorShape({}), &output_max));
     53 
     54     OP_REQUIRES(
     55         ctx, requested_output_min_float <= 0.0f,
     56         errors::InvalidArgument("requested_output_min must be <= 0, but got ",
     57                                 requested_output_min_float));
     58     OP_REQUIRES(
     59         ctx, requested_output_max_float >= requested_output_min_float,
     60         errors::InvalidArgument(
     61             "requested_output_max must be >= requested_output_min, but got ",
     62             requested_output_max_float, " and ", requested_output_min_float));
     63 
     64     auto input_array = input.flat<T1>();
     65 
     66 #if 0
     67     // This is the reference, non-eigen implementation:
     68     auto output_array = output->flat<T2>();
     69     RequantizeManyInNewRange<T1, T2>(
     70         input_array.data(), input_array.size(),
     71         input_min_float, input_max_float,
     72         requested_output_min_float, requested_output_max_float,
     73         output_array.data());
     74 #endif
     75 
     76     if (input_array.size() > 0) {
     77       if (meta::IsSupportedAndEnabled() && std::is_same<T1, qint32>() &&
     78           std::is_same<T2, quint8>()) {
     79         auto input_i32_array = input.flat<qint32>();
     80         meta::Requantize(ctx, input_i32_array.data(), input_i32_array.size(),
     81                          input_min_float, input_max_float,
     82                          requested_output_min_float, requested_output_max_float,
     83                          output->flat<quint8>().data());
     84       } else {
     85         RequantizeManyInNewRangeUsingEigen<T1, T2>(
     86             ctx->eigen_device<CPUDevice>(), input, input_min_float,
     87             input_max_float, requested_output_min_float,
     88             requested_output_max_float, output);
     89       }
     90     }
     91 
     92     output_min->flat<float>().setConstant(requested_output_min_float);
     93     output_max->flat<float>().setConstant(requested_output_max_float);
     94   }
     95 };
     96 
     97 REGISTER_KERNEL_BUILDER(Name("Requantize")
     98                             .Device(DEVICE_CPU)
     99                             .TypeConstraint<qint32>("Tinput")
    100                             .TypeConstraint<quint8>("out_type"),
    101                         RequantizeOp<qint32, quint8>);
    102 
    103 }  // namespace tensorflow
    104