forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify_l2_normalization_test.cc
141 lines (110 loc) · 4.61 KB
/
identify_l2_normalization_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <tuple>
#include <vector>
#include <gtest/gtest.h>
#include "absl/memory/memory.h"
#include "tensorflow/lite/toco/graph_transformations/graph_transformations.h"
#include "tensorflow/lite/toco/model.h"
#include "tensorflow/lite/toco/tooling_util.h"
namespace toco {
namespace {
void RunIdentifyL2Normalization(const std::vector<float>& input,
const std::vector<int>& input_shape,
const std::vector<int>& output_shape,
const bool div_square = false) {
Model model;
Array& input0 = model.GetOrCreateArray("input0");
Array& output = model.GetOrCreateArray("output");
*input0.mutable_shape()->mutable_dims() = input_shape;
input0.data_type = ArrayDataType::kFloat;
input0.GetMutableBuffer<ArrayDataType::kFloat>().data = input;
*output.mutable_shape()->mutable_dims() = output_shape;
auto sq_op = new TensorFlowSquareOperator;
sq_op->inputs = {"input0"};
sq_op->outputs = {"output"};
Array& sumoutput = model.GetOrCreateArray("Sumoutput");
*sumoutput.mutable_shape()->mutable_dims() = output_shape;
auto sum_op = new TensorFlowSumOperator;
sum_op->inputs = {sq_op->outputs[0]};
sum_op->outputs = {"Sumoutput"};
if (div_square) {
Array& sqrtoutput = model.GetOrCreateArray("squarertoutput");
*sqrtoutput.mutable_shape()->mutable_dims() = output_shape;
auto sqrt_op = new TensorFlowSqrtOperator;
sqrt_op->inputs = {sum_op->outputs[0]};
sqrt_op->outputs = {"squarertoutput"};
Array& divoutput = model.GetOrCreateArray("Divoutput");
*divoutput.mutable_shape()->mutable_dims() = output_shape;
auto div_op = new DivOperator;
div_op->inputs = {"input0", sqrt_op->outputs[0]};
div_op->outputs = {"Divoutput"};
/*Stack everything with the model*/
model.operators.push_back(std::unique_ptr<Operator>(div_op));
model.operators.push_back(std::unique_ptr<Operator>(sqrt_op));
model.operators.push_back(std::unique_ptr<Operator>(sum_op));
model.operators.push_back(std::unique_ptr<Operator>(sq_op));
} else {
Array& rsqoutput = model.GetOrCreateArray("Rsquareoutput");
*rsqoutput.mutable_shape()->mutable_dims() = output_shape;
auto rsqrt_op = new TensorFlowRsqrtOperator;
rsqrt_op->inputs = {sum_op->outputs[0]};
rsqrt_op->outputs = {"Rsquareoutput"};
Array& muloutput = model.GetOrCreateArray("Muloutput");
*muloutput.mutable_shape()->mutable_dims() = output_shape;
auto mul_op = new MulOperator;
mul_op->inputs = {"input0", rsqrt_op->outputs[0]};
mul_op->outputs = {"Muloutput"};
/*Stack everything with the model*/
model.operators.push_back(std::unique_ptr<Operator>(mul_op));
model.operators.push_back(std::unique_ptr<Operator>(rsqrt_op));
model.operators.push_back(std::unique_ptr<Operator>(sum_op));
model.operators.push_back(std::unique_ptr<Operator>(sq_op));
}
bool modified;
ASSERT_TRUE(IdentifyL2Normalization().Run(&model, 0, &modified).ok());
for (auto& op_it : model.operators) {
Operator* op = op_it.get();
// Since the optimization has kicked in we should not find any
// Mul, Rsqrt, Add, Sqr operators
if (div_square) {
EXPECT_FALSE(op->type == OperatorType::kDiv);
EXPECT_FALSE(op->type == OperatorType::kSqrt);
} else {
EXPECT_FALSE(op->type == OperatorType::kMul);
EXPECT_FALSE(op->type == OperatorType::kRsqrt);
}
EXPECT_FALSE(op->type == OperatorType::kAdd);
EXPECT_FALSE(op->type == OperatorType::kSquare);
}
}
// Test for reverse input in Min
TEST(IdentifyL2Normalization, MulRsqrtTest) {
RunIdentifyL2Normalization(
// Input data
{3, 1, 4, 1, -5, 9, -2, 6, 5, 3, 5, 8},
// Input shape
{3, 4},
{3, 4},
false);
}
TEST(IdentifyL2Normalization, DivSqrtNormTest) {
RunIdentifyL2Normalization(
// Input data
{3, 1, 4, 1, -5, 9, -2, 6, 5, 3, 5, 8},
// Input shape
{3, 4},
{3, 4},
true);
}
} // namespace
} // namespace toco