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 #include "tensorflow/core/framework/allocator.h"
     17 #include "tensorflow/core/framework/fake_input.h"
     18 #include "tensorflow/core/framework/node_def_builder.h"
     19 #include "tensorflow/core/framework/op_kernel.h"
     20 #include "tensorflow/core/framework/tensor.h"
     21 #include "tensorflow/core/framework/tensor_testutil.h"
     22 #include "tensorflow/core/framework/types.h"
     23 #include "tensorflow/core/framework/types.pb.h"
     24 #include "tensorflow/core/kernels/ops_testutil.h"
     25 #include "tensorflow/core/kernels/ops_util.h"
     26 #include "tensorflow/core/lib/core/status_test_util.h"
     27 #include "tensorflow/core/platform/test.h"
     28 
     29 namespace tensorflow {
     30 
     31 class RequantizeTest : public OpsTestBase {
     32  protected:
     33   void ConfigureRequantize() {
     34     TF_ASSERT_OK(NodeDefBuilder("requantize", "Requantize")
     35                      .Input(FakeInput(DT_QINT32))
     36                      .Input(FakeInput(DT_FLOAT))
     37                      .Input(FakeInput(DT_FLOAT))
     38                      .Input(FakeInput(DT_FLOAT))
     39                      .Input(FakeInput(DT_FLOAT))
     40                      .Attr("Tinput", DataTypeToEnum<qint32>::v())
     41                      .Attr("out_type", DataTypeToEnum<quint8>::v())
     42                      .Finalize(node_def()));
     43     TF_ASSERT_OK(InitOp());
     44   }
     45 };
     46 
     47 // Runs a manually generated array through the operator, and makes sure that the
     48 // results match the expected hand-calculated values.
     49 TEST_F(RequantizeTest, HandCraftedRequantize) {
     50   ConfigureRequantize();
     51   const int value_count = 3;
     52 
     53   // Requantize to -1 to 1.
     54   AddInputFromArray<qint32>(TensorShape({value_count}),
     55                             {-(1 << 23), 0, (1 << 23)});
     56   AddInputFromArray<float>(TensorShape({1}), {-256.0f});
     57   AddInputFromArray<float>(TensorShape({1}), {256.0f});
     58   AddInputFromArray<float>(TensorShape({1}), {-1.0f});
     59   AddInputFromArray<float>(TensorShape({1}), {1.0f});
     60   TF_ASSERT_OK(RunOpKernel());
     61   Tensor expected(allocator(), DT_QUINT8, TensorShape({value_count}));
     62   test::FillValues<quint8>(&expected, {0, 128, 255});
     63   test::ExpectTensorEqual<quint8>(expected, *GetOutput(0));
     64   test::ExpectTensorEqual<float>(test::AsScalar<float>(-1.0f), *GetOutput(1));
     65   test::ExpectTensorEqual<float>(test::AsScalar<float>(1.0f), *GetOutput(2));
     66 }
     67 
     68 TEST_F(RequantizeTest, InvalidOutputMin) {
     69   ConfigureRequantize();
     70   const int value_count = 3;
     71 
     72   AddInputFromArray<qint32>(TensorShape({value_count}),
     73                             {-(1 << 23), 0, (1 << 23)});
     74   AddInputFromArray<float>(TensorShape({1}), {-256.0f});
     75   AddInputFromArray<float>(TensorShape({1}), {256.0f});
     76   AddInputFromArray<float>(TensorShape({1}), {0.01f});
     77   AddInputFromArray<float>(TensorShape({1}), {1.0f});
     78   EXPECT_EQ("requested_output_min must be <= 0, but got 0.01",
     79             RunOpKernel().error_message());
     80 }
     81 
     82 TEST_F(RequantizeTest, InvalidOutputMax) {
     83   ConfigureRequantize();
     84   const int value_count = 3;
     85 
     86   AddInputFromArray<qint32>(TensorShape({value_count}),
     87                             {-(1 << 23), 0, (1 << 23)});
     88   AddInputFromArray<float>(TensorShape({1}), {-256.0f});
     89   AddInputFromArray<float>(TensorShape({1}), {256.0f});
     90   AddInputFromArray<float>(TensorShape({1}), {-10.0f});
     91   AddInputFromArray<float>(TensorShape({1}), {-11.0f});
     92   EXPECT_EQ(
     93       "requested_output_max must be >= requested_output_min, but got -11 and "
     94       "-10",
     95       RunOpKernel().error_message());
     96 }
     97 
     98 }  // end namespace tensorflow
     99