Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2016 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/quantization_utils.h"
     28 #include "tensorflow/core/lib/core/errors.h"
     29 
     30 namespace tensorflow {
     31 
     32 typedef Eigen::ThreadPoolDevice CPUDevice;
     33 
     34 void CalculateUsedRange(const Tensor& input, qint32* used_min_quantized,
     35                         qint32* used_max_quantized) {
     36   auto input_array = input.flat<qint32>();
     37   Eigen::Tensor<qint32, 0, Eigen::RowMajor> min = input_array.minimum();
     38   Eigen::Tensor<qint32, 0, Eigen::RowMajor> max = input_array.maximum();
     39   *used_min_quantized = min();
     40   *used_max_quantized = max();
     41 }
     42 
     43 class RequantizationRangeOp : public OpKernel {
     44  public:
     45   explicit RequantizationRangeOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
     46 
     47   void Compute(OpKernelContext* ctx) override {
     48     const Tensor& input = ctx->input(0);
     49     const float input_min_float = ctx->input(1).flat<float>()(0);
     50     const float input_max_float = ctx->input(2).flat<float>()(0);
     51     Tensor* output_min = nullptr;
     52     OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &output_min));
     53     Tensor* output_max = nullptr;
     54     OP_REQUIRES_OK(ctx, ctx->allocate_output(1, TensorShape({}), &output_max));
     55 
     56     qint32 used_min_quantized;
     57     qint32 used_max_quantized;
     58     CalculateUsedRange(input, &used_min_quantized, &used_max_quantized);
     59 
     60     // We want to make sure that the minimum is no larger than zero, so that the
     61     // convolution operation can run efficiently.
     62     const float used_min_float = std::min(
     63         0.0f,
     64         QuantizedToFloat(used_min_quantized, input_min_float, input_max_float));
     65     const float used_max_float =
     66         QuantizedToFloat(used_max_quantized, input_min_float, input_max_float);
     67 
     68     output_min->flat<float>().setConstant(used_min_float);
     69     output_max->flat<float>().setConstant(used_max_float);
     70   }
     71 };
     72 
     73 REGISTER_KERNEL_BUILDER(Name("RequantizationRange")
     74                             .Device(DEVICE_CPU)
     75                             .TypeConstraint<qint32>("Tinput"),
     76                         RequantizationRangeOp);
     77 
     78 }  // namespace tensorflow
     79