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 #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 #include "tensorflow/core/platform/test_benchmark.h"
     29 
     30 namespace tensorflow {
     31 
     32 // Declared here so we don't need to include it in a public header.
     33 void CalculateUsedRange(const Tensor& input, qint32* actual_min_quantized,
     34                         qint32* actual_max_quantized);
     35 
     36 class RequantizationRangeTest : public OpsTestBase {
     37  protected:
     38 };
     39 
     40 // Runs a manually generated array through the operator, and makes sure that the
     41 // results match the expected hand-calculated values.
     42 TEST_F(RequantizationRangeTest, HandCrafted) {
     43   TF_ASSERT_OK(NodeDefBuilder("requantization_range", "RequantizationRange")
     44                    .Input(FakeInput(DT_QINT32))
     45                    .Input(FakeInput(DT_FLOAT))
     46                    .Input(FakeInput(DT_FLOAT))
     47                    .Attr("Tinput", DataTypeToEnum<qint32>::v())
     48                    .Finalize(node_def()));
     49   TF_ASSERT_OK(InitOp());
     50 
     51   // For this test we have an input that has the theoretical range of -256.0f to
     52   // +256.0f, but the actual values present only span -1.0f to 1.0f. We expect
     53   // the operator to take advantage of this, and rescale the output to fill up
     54   // the available range in the lower bit depth, and update to the true min and
     55   // max ranges.
     56   const int value_count = 3;
     57   AddInputFromArray<qint32>(TensorShape({value_count}),
     58                             {-(1 << 23), 0, (1 << 23)});
     59   AddInputFromArray<float>(TensorShape({1}), {-256.0f});
     60   AddInputFromArray<float>(TensorShape({1}), {256.0f});
     61   TF_ASSERT_OK(RunOpKernel());
     62   Tensor expected_min(allocator(), DT_FLOAT, TensorShape({}));
     63   test::FillValues<float>(&expected_min, {-1.0f});
     64   test::ExpectTensorEqual<float>(expected_min, *GetOutput(0));
     65   Tensor expected_max(allocator(), DT_FLOAT, TensorShape({}));
     66   test::FillValues<float>(&expected_max, {1.0f});
     67   test::ExpectTensorEqual<float>(expected_max, *GetOutput(1));
     68 }
     69 
     70 static void BM_RequantizationRange(int iters, int size) {
     71   testing::StopTiming();
     72   testing::UseRealTime();
     73   testing::ItemsProcessed(static_cast<int64>(iters) * size);
     74   testing::ItemsProcessed(static_cast<int64>(iters) * size * 4);
     75 
     76   Tensor quantized_tensor(DT_QINT32, TensorShape({1, size}));
     77   test::FillFn<qint32>(&quantized_tensor, [](int n) { return qint32(n); });
     78 
     79   qint32 actual_min;
     80   qint32 actual_max;
     81   testing::StartTiming();
     82   for (int iter = 0; iter < iters; ++iter) {
     83     CalculateUsedRange(quantized_tensor, &actual_min, &actual_max);
     84   }
     85 }
     86 
     87 static void BM_RequantizationRange100(int iters) {
     88   BM_RequantizationRange(100, iters);
     89 }
     90 BENCHMARK(BM_RequantizationRange100);
     91 
     92 static void BM_RequantizationRange1000(int iters) {
     93   BM_RequantizationRange(1000, iters);
     94 }
     95 BENCHMARK(BM_RequantizationRange1000);
     96 
     97 static void BM_RequantizationRange10000(int iters) {
     98   BM_RequantizationRange(10000, iters);
     99 }
    100 BENCHMARK(BM_RequantizationRange10000);
    101 
    102 static void BM_RequantizationRange100000(int iters) {
    103   BM_RequantizationRange(100000, iters);
    104 }
    105 BENCHMARK(BM_RequantizationRange100000);
    106 
    107 static void BM_RequantizationRange1000000(int iters) {
    108   BM_RequantizationRange(1000000, iters);
    109 }
    110 BENCHMARK(BM_RequantizationRange1000000);
    111 
    112 static void BM_RequantizationRange10000000(int iters) {
    113   BM_RequantizationRange(10000000, iters);
    114 }
    115 BENCHMARK(BM_RequantizationRange10000000);
    116 
    117 static void BM_RequantizationRange100000000(int iters) {
    118   BM_RequantizationRange(100000000, iters);
    119 }
    120 BENCHMARK(BM_RequantizationRange100000000);
    121 
    122 }  // end namespace tensorflow
    123