-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
instance_norm_compute_test.cc
210 lines (194 loc) · 6.94 KB
/
instance_norm_compute_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2019 PaddlePaddle 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 <gtest/gtest.h>
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/core/test/arena/framework.h"
#include "lite/tests/utils/fill_data.h"
namespace paddle {
namespace lite {
class InstanceNormComputeTest : public arena::TestCase {
protected:
// common attributes for this op.
std::string x_ = "x";
std::string y_ = "y";
std::string saved_mean_ = "saved_mean";
std::string saved_variance_ = "saved_variance";
std::string scale_ = "scale";
std::string bias_ = "bias";
DDim dims_{{4, 5, 19, 19}};
float epsilon_ = 1e-5f;
bool has_scale_bias_ = true;
public:
InstanceNormComputeTest(const Place& place,
const std::string& alias,
DDim dims,
float epsilon,
bool has_scale_bias)
: TestCase(place, alias),
dims_(dims),
epsilon_(epsilon),
has_scale_bias_(has_scale_bias) {}
void RunBaseline(Scope* scope) override {
const Tensor* x = scope->FindTensor(x_);
const float* x_data = x->data<float>();
const float* scale_data =
has_scale_bias_ ? scope->FindTensor(scale_)->data<float>() : nullptr;
const float* bias_data =
has_scale_bias_ ? scope->FindTensor(bias_)->data<float>() : nullptr;
auto y = scope->NewTensor(y_);
auto saved_mean = scope->NewTensor(saved_mean_);
auto saved_variance = scope->NewTensor(saved_variance_);
CHECK(y);
CHECK(saved_mean);
CHECK(saved_variance);
DDim saved_dim({dims_[0] * dims_[1]});
y->Resize(dims_);
saved_mean->Resize(saved_dim);
saved_variance->Resize(saved_dim);
auto y_data = y->mutable_data<float>();
auto saved_mean_data = saved_mean->mutable_data<float>();
auto saved_variance_data = saved_variance->mutable_data<float>();
int n = x->dims()[0];
int c = x->dims()[1];
int spatial_size = x->dims()[2] * x->dims()[3];
// compute mean
for (int i = 0; i < n * c; ++i) {
const float* x_ptr = x_data + i * spatial_size;
float sum = 0.f;
for (int j = 0; j < spatial_size; ++j) {
sum += x_ptr[j];
}
saved_mean_data[i] = sum / spatial_size;
}
// compute variance
for (int i = 0; i < n * c; ++i) {
const float* x_ptr = x_data + i * spatial_size;
float sum = 0.f;
for (int j = 0; j < spatial_size; ++j) {
sum +=
(x_ptr[j] - saved_mean_data[i]) * (x_ptr[j] - saved_mean_data[i]);
}
saved_variance_data[i] = 1.f / sqrtf(sum / spatial_size + epsilon_);
}
// compute out
for (int i = 0; i < n * c; ++i) {
const float* x_ptr = x_data + i * spatial_size;
float* y_ptr = y_data + i * spatial_size;
float scale_val = scale_data == nullptr ? 1 : scale_data[i % c];
float bias_val = bias_data == nullptr ? 0 : bias_data[i % c];
for (int j = 0; j < spatial_size; ++j) {
y_ptr[j] = scale_val * (x_ptr[j] - saved_mean_data[i]) *
saved_variance_data[i] +
bias_val;
}
}
}
void PrepareOpDesc(cpp::OpDesc* op_desc) override {
op_desc->SetType("instance_norm");
op_desc->SetInput("X", {x_});
if (has_scale_bias_) {
op_desc->SetInput("Bias", {bias_});
op_desc->SetInput("Scale", {scale_});
}
op_desc->SetOutput("Y", {y_});
op_desc->SetOutput("SavedMean", {saved_mean_});
op_desc->SetOutput("SavedVariance", {saved_variance_});
op_desc->SetAttr("epsilon", epsilon_);
}
void PrepareData() override {
std::vector<float> x(dims_.production());
fill_data_rand(x.data(), -1.f, 1.f, dims_.production());
SetCommonTensor(x_, dims_, x.data());
if (has_scale_bias_) {
DDim scale_bias_dims{{dims_[1]}};
std::vector<float> scale(scale_bias_dims.production());
fill_data_rand(scale.data(), -1.f, 1.f, scale_bias_dims.production());
std::vector<float> bias(scale_bias_dims.production());
fill_data_rand(bias.data(), -1.f, 1.f, scale_bias_dims.production());
SetCommonTensor(scale_, scale_bias_dims, scale.data(), {}, true);
SetCommonTensor(bias_, scale_bias_dims, bias.data(), {}, true);
}
}
};
void TestInstanceNorm(Place place,
float abs_error = 6e-5,
std::vector<std::string> ignored_outs = {}) {
for (auto& n : {1, 3}) {
for (auto& c : {1, 3}) {
for (auto& h : {1, 33}) {
for (auto& w : {1, 5, 34}) {
for (auto& has_scale_bias : {true, false}) {
DDim dim_in({n, c, h, w});
float epsilon = 1e-5f;
std::unique_ptr<arena::TestCase> tester(new InstanceNormComputeTest(
place, "def", dim_in, epsilon, has_scale_bias));
#if defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU)
if (w == 1 && h == 1 && (n != 1 || c != 1)) continue;
#endif
#ifdef LITE_WITH_ARM
if (place == TARGET(kARM)) {
auto& ctx = tester->context()->As<ARMContext>();
ctx.SetRunMode(lite_api::LITE_POWER_HIGH, 4);
}
#endif
arena::Arena arena(std::move(tester), place, abs_error);
if (!arena.TestPrecision(ignored_outs)) {
LOG(ERROR) << "run n: " << n << ", c: " << c << ", h: " << h
<< ", w: " << w
<< ", has_scale_bias:" << has_scale_bias;
return;
}
}
}
}
}
}
}
TEST(InstanceNorm, precision) {
Place place;
float abs_error = 3e-3;
std::vector<std::string> ignored_outs = {};
#if defined(LITE_WITH_NNADAPTER)
place = TARGET(kNNAdapter);
#if defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU)
abs_error = 1e-2;
ignored_outs = {"saved_mean", "saved_variance"};
#elif defined(NNADAPTER_WITH_HUAWEI_KIRIN_NPU)
abs_error = 1e-2;
ignored_outs = {"saved_mean", "saved_variance"};
// TODO(liusiyuan): support later
return;
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
abs_error = 1e-1;
ignored_outs = {"saved_mean", "saved_variance"};
#elif defined(NNADAPTER_WITH_VERISILICON_TIMVX)
abs_error = 1e-1;
ignored_outs = {"saved_mean", "saved_variance"};
#else
return;
#endif
#elif defined(LITE_WITH_XPU)
place = TARGET(kXPU);
#elif defined(LITE_WITH_ARM)
place = TARGET(kARM);
#elif defined(LITE_WITH_X86)
place = TARGET(kX86);
#else
return;
#endif
TestInstanceNorm(place, abs_error, ignored_outs);
}
} // namespace lite
} // namespace paddle