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 // Implements a quantized version of the Relu6 operation. 17 #define EIGEN_USE_THREADS 18 19 #include "tensorflow/core/framework/numeric_op.h" 20 #include "tensorflow/core/framework/op_kernel.h" 21 #include "tensorflow/core/framework/tensor.h" 22 #include "tensorflow/core/kernels/meta_support.h" 23 #include "tensorflow/core/kernels/quantization_utils.h" 24 #include "tensorflow/core/lib/core/errors.h" 25 26 namespace tensorflow { 27 28 template <typename T> 29 class QuantizedReluOp : public OpKernel { 30 public: 31 explicit QuantizedReluOp(OpKernelConstruction* context) : OpKernel(context) {} 32 33 void Compute(OpKernelContext* context) override { 34 const Tensor& input = context->input(0); 35 const float min_input = context->input(1).flat<float>()(0); 36 const float max_input = context->input(2).flat<float>()(0); 37 Tensor* output = nullptr; 38 OP_REQUIRES_OK(context, 39 context->allocate_output(0, input.shape(), &output)); 40 const T min_as_quantized = FloatToQuantized<T>(0.0f, min_input, max_input); 41 42 if (meta::IsSupportedAndEnabled() && std::is_same<T, quint8>()) { 43 auto input_ui8_array = input.flat<quint8>(); 44 meta::Clamp(context, input_ui8_array.data(), input_ui8_array.size(), 45 min_as_quantized, 255, output->flat<quint8>().data()); 46 } else { 47 output->flat<T>().device(context->eigen_cpu_device()) = 48 input.flat<T>().cwiseMax(min_as_quantized).template cast<T>(); 49 } 50 51 Tensor* output_min = nullptr; 52 OP_REQUIRES_OK(context, context->allocate_output(1, {}, &output_min)); 53 output_min->flat<float>()(0) = min_input; 54 Tensor* output_max = nullptr; 55 OP_REQUIRES_OK(context, context->allocate_output(2, {}, &output_max)); 56 output_max->flat<float>()(0) = max_input; 57 } 58 }; 59 60 template <typename T> 61 class QuantizedRelu6Op : public OpKernel { 62 public: 63 explicit QuantizedRelu6Op(OpKernelConstruction* context) 64 : OpKernel(context) {} 65 66 void Compute(OpKernelContext* context) override { 67 const Tensor& input = context->input(0); 68 const float min_input = context->input(1).flat<float>()(0); 69 const float max_input = context->input(2).flat<float>()(0); 70 Tensor* output = nullptr; 71 OP_REQUIRES_OK(context, 72 context->allocate_output(0, input.shape(), &output)); 73 const T min_as_quantized = FloatToQuantized<T>(0.0f, min_input, max_input); 74 const T max_as_quantized = FloatToQuantized<T>(6.0f, min_input, max_input); 75 76 if (meta::IsSupportedAndEnabled() && std::is_same<T, quint8>()) { 77 auto input_ui8_array = input.flat<quint8>(); 78 meta::Clamp(context, input_ui8_array.data(), input_ui8_array.size(), 79 min_as_quantized, max_as_quantized, 80 output->flat<quint8>().data()); 81 } else { 82 output->flat<T>().device(context->eigen_cpu_device()) = 83 input.flat<T>() 84 .cwiseMax(min_as_quantized) 85 .cwiseMin(max_as_quantized) 86 .template cast<T>(); 87 } 88 89 Tensor* output_min = nullptr; 90 OP_REQUIRES_OK(context, context->allocate_output(1, {}, &output_min)); 91 output_min->flat<float>()(0) = min_input; 92 Tensor* output_max = nullptr; 93 OP_REQUIRES_OK(context, context->allocate_output(2, {}, &output_max)); 94 output_max->flat<float>()(0) = max_input; 95 } 96 }; 97 98 REGISTER_KERNEL_BUILDER(Name("QuantizedRelu") 99 .Device(DEVICE_CPU) 100 .TypeConstraint<qint32>("Tinput") 101 .TypeConstraint<qint32>("out_type"), 102 QuantizedReluOp<qint32>); 103 REGISTER_KERNEL_BUILDER(Name("QuantizedRelu") 104 .Device(DEVICE_CPU) 105 .TypeConstraint<quint8>("Tinput") 106 .TypeConstraint<quint8>("out_type"), 107 QuantizedReluOp<quint8>); 108 109 REGISTER_KERNEL_BUILDER(Name("QuantizedRelu6") 110 .Device(DEVICE_CPU) 111 .TypeConstraint<qint32>("Tinput") 112 .TypeConstraint<qint32>("out_type"), 113 QuantizedRelu6Op<qint32>); 114 REGISTER_KERNEL_BUILDER(Name("QuantizedRelu6") 115 .Device(DEVICE_CPU) 116 .TypeConstraint<quint8>("Tinput") 117 .TypeConstraint<quint8>("out_type"), 118 QuantizedRelu6Op<quint8>); 119 } // namespace tensorflow 120