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 QuantizeDownAndShrinkRangeTest : public OpsTestBase {
     32  protected:
     33 };
     34 
     35 // Runs a manually generated array through the operator, and makes sure that the
     36 // results match the expected hand-calculated values.
     37 TEST_F(QuantizeDownAndShrinkRangeTest, HandCrafted) {
     38   TF_ASSERT_OK(NodeDefBuilder("quantize_down_and_shrink_range_op",
     39                               "QuantizeDownAndShrinkRange")
     40                    .Input(FakeInput(DT_QINT32))
     41                    .Input(FakeInput(DT_FLOAT))
     42                    .Input(FakeInput(DT_FLOAT))
     43                    .Attr("Tinput", DataTypeToEnum<qint32>::v())
     44                    .Attr("out_type", DataTypeToEnum<quint8>::v())
     45                    .Finalize(node_def()));
     46   TF_ASSERT_OK(InitOp());
     47 
     48   // For this test we have an input that has the theoretical range of -256.0f to
     49   // +256.0f, but the actual values present only span -1.0f to 1.0f. We expect
     50   // the operator to take advantage of this, and rescale the output to fill up
     51   // the available range in the lower bit depth, and update to the true min and
     52   // max ranges.
     53   const int value_count = 3;
     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   TF_ASSERT_OK(RunOpKernel());
     59   Tensor expected(allocator(), DT_QUINT8, TensorShape({value_count}));
     60   test::FillValues<quint8>(&expected, {0, 127, 255});
     61   test::ExpectTensorEqual<quint8>(expected, *GetOutput(0));
     62   Tensor expected_min(allocator(), DT_FLOAT, TensorShape({}));
     63   test::FillValues<float>(&expected_min, {-1.0f});
     64   test::ExpectTensorEqual<float>(expected_min, *GetOutput(1));
     65   Tensor expected_max(allocator(), DT_FLOAT, TensorShape({}));
     66   test::FillValues<float>(&expected_max, {1.0f});
     67   test::ExpectTensorEqual<float>(expected_max, *GetOutput(2));
     68 }
     69 
     70 }  // end namespace tensorflow
     71