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 <vector>
17 #include "tensorflow/core/framework/allocator.h"
18 #include "tensorflow/core/framework/fake_input.h"
19 #include "tensorflow/core/framework/node_def_builder.h"
20 #include "tensorflow/core/framework/op_kernel.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/framework/tensor_testutil.h"
23 #include "tensorflow/core/framework/types.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 class FusedBatchNormOpTest : public OpsTestBase {};
31
TEST_F(FusedBatchNormOpTest,Training)32 TEST_F(FusedBatchNormOpTest, Training) {
33 TF_EXPECT_OK(NodeDefBuilder("batch_norm_op", "FusedBatchNorm")
34 .Input(FakeInput(DT_FLOAT))
35 .Input(FakeInput(DT_FLOAT))
36 .Input(FakeInput(DT_FLOAT))
37 .Input(FakeInput(DT_FLOAT))
38 .Input(FakeInput(DT_FLOAT))
39 .Attr("epsilon", 0.001)
40 .Attr("is_training", true)
41 .Finalize(node_def()));
42 TF_EXPECT_OK(InitOp());
43 AddInputFromArray<float>(TensorShape({1, 1, 6, 2}),
44 {5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15});
45 AddInputFromArray<float>(TensorShape({2}), {4.0, 4.0});
46 AddInputFromArray<float>(TensorShape({2}), {2.0, 2.0});
47 AddInputFromArray<float>(TensorShape({0}), {});
48 AddInputFromArray<float>(TensorShape({0}), {});
49
50 TF_ASSERT_OK(RunOpKernel());
51
52 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 1, 6, 2}));
53 test::FillValues<float>(&expected, {-3.86, -3.86, -1.51, -1.51, 0.83, 0.83,
54 3.17, 3.17, 5.51, 5.51, 7.86, 7.86});
55 test::ExpectTensorNear<float>(expected, *GetOutput(0), 0.01);
56
57 Tensor expected_mean(allocator(), DT_FLOAT, TensorShape({2}));
58 test::FillValues<float>(&expected_mean, {10, 10});
59 test::ExpectTensorNear<float>(expected_mean, *GetOutput(1), 0.01);
60
61 Tensor expected_variance(allocator(), DT_FLOAT, TensorShape({2}));
62 test::FillValues<float>(&expected_variance, {14.00, 14.00});
63 test::ExpectTensorNear<float>(expected_variance, *GetOutput(2), 0.01);
64 }
65
TEST_F(FusedBatchNormOpTest,Inference)66 TEST_F(FusedBatchNormOpTest, Inference) {
67 TF_EXPECT_OK(NodeDefBuilder("batch_norm_op", "FusedBatchNorm")
68 .Input(FakeInput(DT_FLOAT))
69 .Input(FakeInput(DT_FLOAT))
70 .Input(FakeInput(DT_FLOAT))
71 .Input(FakeInput(DT_FLOAT))
72 .Input(FakeInput(DT_FLOAT))
73 .Attr("epsilon", 0.001)
74 .Attr("is_training", false)
75 .Finalize(node_def()));
76 TF_EXPECT_OK(InitOp());
77 AddInputFromArray<float>(TensorShape({1, 1, 6, 2}),
78 {5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15});
79 AddInputFromArray<float>(TensorShape({2}), {4.0, 4.0});
80 AddInputFromArray<float>(TensorShape({2}), {2.0, 2.0});
81 AddInputFromArray<float>(TensorShape({2}), {10, 10});
82 AddInputFromArray<float>(TensorShape({2}), {11.67f, 11.67f});
83
84 TF_ASSERT_OK(RunOpKernel());
85
86 Tensor expected(allocator(), DT_FLOAT, TensorShape({1, 1, 6, 2}));
87 test::FillValues<float>(&expected, {-3.86, -3.86, -1.51, -1.51, 0.83, 0.83,
88 3.17, 3.17, 5.51, 5.51, 7.86, 7.86});
89 test::ExpectTensorNear<float>(expected, *GetOutput(0), 0.01);
90 }
91
92 class FusedBatchNormGradOpTest : public OpsTestBase {};
93
TEST_F(FusedBatchNormGradOpTest,Simple)94 TEST_F(FusedBatchNormGradOpTest, Simple) {
95 TF_EXPECT_OK(NodeDefBuilder("batch_norm_grad_op", "FusedBatchNormGrad")
96 .Input(FakeInput(DT_FLOAT))
97 .Input(FakeInput(DT_FLOAT))
98 .Input(FakeInput(DT_FLOAT))
99 .Input(FakeInput(DT_FLOAT))
100 .Input(FakeInput(DT_FLOAT))
101 .Attr("epsilon", 0.001)
102 .Finalize(node_def()));
103 TF_EXPECT_OK(InitOp());
104 AddInputFromArray<float>(TensorShape({1, 1, 6, 2}),
105 {2, 2, 9, 9, -4, -4, 5, 5, 8, 8, 7, 7});
106 AddInputFromArray<float>(TensorShape({1, 1, 6, 2}),
107 {1, 1, 7, 7, 4, 4, -3, -3, -11, -11, 13, 13});
108 AddInputFromArray<float>(TensorShape({2}), {4, 4});
109 AddInputFromArray<float>(TensorShape({2}), {1.833f, 1.833f});
110 AddInputFromArray<float>(TensorShape({2}), {57.472f, 57.472f});
111
112 TF_ASSERT_OK(RunOpKernel());
113
114 Tensor expected_x(allocator(), DT_FLOAT, TensorShape({1, 1, 6, 2}));
115 test::FillValues<float>(&expected_x, {-1.34, -1.34, 2.47, 2.47, -4.44, -4.44,
116 0.17, 0.17, 1.60, 1.60, 1.53, 1.53});
117 test::ExpectTensorNear<float>(expected_x, *GetOutput(0), 0.01);
118
119 Tensor expected_scale(allocator(), DT_FLOAT, TensorShape({2}));
120 test::FillValues<float>(&expected_scale, {-1.6488, -1.6488});
121 test::ExpectTensorNear<float>(expected_scale, *GetOutput(1), 0.01);
122
123 Tensor expected_offset(allocator(), DT_FLOAT, TensorShape({2}));
124 test::FillValues<float>(&expected_offset, {27, 27});
125 test::ExpectTensorNear<float>(expected_offset, *GetOutput(2), 0.01);
126 }
127 } // namespace tensorflow
128