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 #define EIGEN_USE_THREADS
     17 
     18 #include "tensorflow/core/framework/allocator.h"
     19 #include "tensorflow/core/framework/fake_input.h"
     20 #include "tensorflow/core/framework/node_def_builder.h"
     21 #include "tensorflow/core/framework/op_kernel.h"
     22 #include "tensorflow/core/framework/tensor.h"
     23 #include "tensorflow/core/framework/tensor_testutil.h"
     24 #include "tensorflow/core/framework/types.h"
     25 #include "tensorflow/core/framework/types.pb.h"
     26 #include "tensorflow/core/kernels/ops_testutil.h"
     27 #include "tensorflow/core/kernels/ops_util.h"
     28 #include "tensorflow/core/kernels/quantization_utils.h"
     29 #include "tensorflow/core/lib/core/status_test_util.h"
     30 #include "tensorflow/core/platform/test.h"
     31 
     32 namespace tensorflow {
     33 
     34 class QuantizedActivationsTest : public OpsTestBase {
     35  protected:
     36 };
     37 
     38 TEST_F(QuantizedActivationsTest, TestRelu) {
     39   TF_ASSERT_OK(NodeDefBuilder("quantized_relu_op", "QuantizedRelu")
     40                    .Input(FakeInput(DT_QUINT8))
     41                    .Input(FakeInput(DT_FLOAT))
     42                    .Input(FakeInput(DT_FLOAT))
     43                    .Finalize(node_def()));
     44   TF_ASSERT_OK(InitOp());
     45   const float input_min = -128.0f;
     46   const float input_max = 127.0f;
     47   const int input_width = 2;
     48   const int input_height = 4;
     49   Tensor input_float(DT_FLOAT, {input_height, input_width});
     50   test::FillValues<float>(&input_float, {-100, -1, 0, 1, 3, 6, 7, 100});
     51   Tensor input_quantized =
     52       FloatTensorToQuantized<quint8>(input_float, input_min, input_max);
     53   Tensor expected_float(DT_FLOAT, {input_height, input_width});
     54   test::FillValues<float>(&expected_float, {0, 0, 0, 1, 3, 6, 7, 100});
     55 
     56   AddInputFromArray<quint8>(input_quantized.shape(),
     57                             input_quantized.flat<quint8>());
     58   AddInputFromArray<float>(TensorShape({1}), {input_min});
     59   AddInputFromArray<float>(TensorShape({1}), {input_max});
     60   TF_ASSERT_OK(RunOpKernel());
     61   const Tensor& output_quantized = *GetOutput(0);
     62   const float output_min = GetOutput(1)->flat<float>()(0);
     63   const float output_max = GetOutput(2)->flat<float>()(0);
     64   Tensor output_float =
     65       QuantizedTensorToFloat<quint8>(output_quantized, output_min, output_max);
     66   test::ExpectTensorNear<float>(expected_float, output_float, 0.2);
     67 }
     68 
     69 TEST_F(QuantizedActivationsTest, TestRelu6) {
     70   TF_ASSERT_OK(NodeDefBuilder("quantized_relu6_op", "QuantizedRelu6")
     71                    .Input(FakeInput(DT_QUINT8))
     72                    .Input(FakeInput(DT_FLOAT))
     73                    .Input(FakeInput(DT_FLOAT))
     74                    .Finalize(node_def()));
     75   TF_ASSERT_OK(InitOp());
     76   const float input_min = -128.0f;
     77   const float input_max = 127.0f;
     78   const int input_width = 2;
     79   const int input_height = 4;
     80   Tensor input_float(DT_FLOAT, {input_height, input_width});
     81   test::FillValues<float>(&input_float, {-100, -1, 0, 1, 3, 6, 7, 100});
     82   Tensor input_quantized =
     83       FloatTensorToQuantized<quint8>(input_float, input_min, input_max);
     84   Tensor expected_float(DT_FLOAT, {input_height, input_width});
     85   test::FillValues<float>(&expected_float, {0, 0, 0, 1, 3, 6, 6, 6});
     86 
     87   AddInputFromArray<quint8>(input_quantized.shape(),
     88                             input_quantized.flat<quint8>());
     89   AddInputFromArray<float>(TensorShape({1}), {input_min});
     90   AddInputFromArray<float>(TensorShape({1}), {input_max});
     91   TF_ASSERT_OK(RunOpKernel());
     92   const Tensor& output_quantized = *GetOutput(0);
     93   const float output_min = GetOutput(1)->flat<float>()(0);
     94   const float output_max = GetOutput(2)->flat<float>()(0);
     95   Tensor output_float =
     96       QuantizedTensorToFloat<quint8>(output_quantized, output_min, output_max);
     97   test::ExpectTensorNear<float>(expected_float, output_float, 0.2);
     98 }
     99 
    100 }  // namespace tensorflow
    101