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.
TEST_F(RequantizationRangeTest,HandCrafted)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
BM_RequantizationRange(::testing::benchmark::State & state)70 static void BM_RequantizationRange(::testing::benchmark::State& state) {
71 const int size = state.range(0);
72
73 Tensor quantized_tensor(DT_QINT32, TensorShape({1, size}));
74 test::FillFn<qint32>(&quantized_tensor, [](int n) { return qint32(n); });
75
76 qint32 actual_min;
77 qint32 actual_max;
78 for (auto s : state) {
79 CalculateUsedRange(quantized_tensor, &actual_min, &actual_max);
80 }
81 state.SetItemsProcessed(static_cast<int64>(state.iterations()) * size);
82 state.SetBytesProcessed(static_cast<int64>(state.iterations()) * size * 4);
83 }
84
85 BENCHMARK(BM_RequantizationRange)
86 ->UseRealTime()
87 ->Arg(100)
88 ->Arg(1000)
89 ->Arg(10000)
90 ->Arg(100000)
91 ->Arg(1000000)
92 ->Arg(10000000)
93 ->Arg(100000000);
94
95 } // end namespace tensorflow
96