-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_slice.cc
189 lines (166 loc) · 5.61 KB
/
test_slice.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
/************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 "../src/model/layer/slice.h"
#include "gtest/gtest.h"
using singa::Shape;
TEST(Slice, Setup) {
singa::LayerConf conf;
conf.set_type("singa_slice");
auto slice_conf = conf.mutable_slice_conf();
slice_conf->set_axis(1);
slice_conf->add_slice_point(2);
singa::Slice layer;
layer.Setup({3u}, conf);
auto s1 = layer.GetOutputSampleShape(0);
EXPECT_EQ(s1[0], 2u);
auto s2 = layer.GetOutputSampleShape(1);
EXPECT_EQ(s2[0], 1u);
}
void ForwardSliceRowTest(std::shared_ptr<singa::Device> dev) {
size_t a = 2u, b = 1u, c = 3u;
singa::LayerConf conf;
conf.set_type("singa_slice");
auto slice_conf = conf.mutable_slice_conf();
slice_conf->set_axis(0);
slice_conf->add_slice_point(a);
singa::Slice layer;
layer.Setup({c}, conf);
layer.ToDevice(dev);
singa::Tensor t({a + b, c}, dev);
singa::Uniform(-1.f, 1.f, &t);
auto grads = layer.Forward(singa::kTrain, {t});
EXPECT_EQ(grads.size(), 2u);
t.ToHost();
const float* tptr = t.data<float>();
grads[0].ToHost();
const float* outa = grads[0].data<float>();
for (size_t i = 0; i < a; i++)
for (size_t j = 0; j < c; j++)
EXPECT_FLOAT_EQ(outa[i * c + j], tptr[i * c + j]);
grads[1].ToHost();
const float* outb = grads[1].data<float>();
for (size_t i = 0; i < b; i++)
for (size_t j = 0; j < c; j++)
EXPECT_FLOAT_EQ(outb[i * c + j], tptr[(i + a) * c + j]);
}
void ForwardSliceColumnTest(std::shared_ptr<singa::Device> dev) {
size_t a = 2u, b = 1u, c = 3u;
singa::LayerConf conf;
conf.set_type("singa_slice");
auto slice_conf = conf.mutable_slice_conf();
slice_conf->set_axis(1);
slice_conf->add_slice_point(a);
singa::Slice layer;
layer.Setup({a + b}, conf);
layer.ToDevice(dev);
singa::Tensor t({c, a + b}, dev);
singa::Uniform(-1.f, 1.f, &t);
auto out = layer.Forward(singa::kTrain, {t});
EXPECT_EQ(out.size(), 2u);
t.ToHost();
const float* tptr = t.data<float>();
out[0].ToHost();
const float* outa = out[0].data<float>();
for (size_t i = 0; i < c; i++)
for (size_t j = 0; j < a; j++)
EXPECT_FLOAT_EQ(outa[i * a + j], tptr[i * (a + b) + j]);
out[1].ToHost();
const float* outb = out[1].data<float>();
for (size_t i = 0; i < c; i++)
for (size_t j = 0; j < b; j++)
EXPECT_FLOAT_EQ(outb[i * b + j], tptr[i * (a + b) + a + j]);
}
TEST(Slice, ForwardSliceRowCpp) { ForwardSliceRowTest(singa::defaultDevice); }
TEST(Slice, ForwardSliceColumn) {
ForwardSliceColumnTest(singa::defaultDevice);
}
#ifdef USE_CUDA
TEST(Slice, ForwardSliceRowCuda) {
ForwardSliceRowTest(std::make_shared<singa::CudaGPU>());
}
TEST(Slice, ForwardSliceColumnCuda) {
ForwardSliceColumnTest(std::make_shared<singa::CudaGPU>());
}
#endif // USE_CUDA
void BackwardSliceRowTest(std::shared_ptr<singa::Device> dev) {
size_t a = 2u, b = 1u, c = 3u;
singa::LayerConf conf;
conf.set_type("singa_slice");
auto slice_conf = conf.mutable_slice_conf();
slice_conf->set_axis(0);
slice_conf->add_slice_point(2);
singa::Slice layer;
layer.Setup({c}, conf);
layer.ToDevice(dev);
singa::Tensor t1({a, c}, dev);
singa::Tensor t2({b, c}, dev);
t1.SetValue(1.0f);
t2.SetValue(2.0f);
auto out = layer.Backward(singa::kTrain, {t1, t2});
auto grad = out.first[0];
grad.ToHost();
const float* outptr = grad.data<float>();
for (size_t i = 0; i < a; i++) {
for (size_t j = 0; j < c; j++) EXPECT_FLOAT_EQ(outptr[i * c + j], 1.0f);
}
for (size_t i = a; i < a + b; i++) {
for (size_t j = 0; j < c; j++) EXPECT_FLOAT_EQ(outptr[i * c + j], 2.0f);
}
}
void BackwardSliceColumnTest(std::shared_ptr<singa::Device> dev) {
size_t a = 2u, b = 1u, c = 3u;
singa::LayerConf conf;
conf.set_type("singa_slice");
auto slice_conf = conf.mutable_slice_conf();
slice_conf->set_axis(1);
slice_conf->add_slice_point(2);
singa::Slice layer;
layer.Setup({a + b}, conf);
layer.ToDevice(dev);
singa::Tensor t1({c, a}, dev);
singa::Tensor t2({c, b}, dev);
t1.SetValue(1.0f);
t2.SetValue(2.0f);
auto out = layer.Backward(singa::kTrain, {t1, t2});
auto grad = out.first[0];
grad.ToHost();
const float* outptr = grad.data<float>();
for (size_t i = 0; i < c; i++) {
for (size_t j = 0; j < a; j++)
EXPECT_FLOAT_EQ(outptr[i * (a + b) + j], 1.0f);
}
for (size_t i = 0; i < c; i++) {
for (size_t j = a; j < a + b; j++)
EXPECT_FLOAT_EQ(outptr[i * (a + b) + j], 2.0f);
}
}
TEST(Slice, BackwardSliceRowCpp) { BackwardSliceRowTest(singa::defaultDevice); }
TEST(Slice, BackwardSliceColumn) {
BackwardSliceColumnTest(singa::defaultDevice);
}
#ifdef USE_CUDA
TEST(Slice, BackwardSliceRowCuda) {
BackwardSliceRowTest(std::make_shared<singa::CudaGPU>());
}
TEST(Slice, BackwardSliceColumnCuda) {
BackwardSliceColumnTest(std::make_shared<singa::CudaGPU>());
}
#endif // USE_CUDA