-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathclamp.cpp
More file actions
234 lines (200 loc) · 7.36 KB
/
clamp.cpp
File metadata and controls
234 lines (200 loc) · 7.36 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/arith.h>
#include <af/array.h>
#include <af/data.h>
#include <af/defines.h>
#include <af/random.h>
#include <af/traits.hpp>
#include <sstream>
#include <string>
#include <vector>
using af::array;
using af::dim4;
using af::dtype;
using af::randu;
using std::abs;
using std::string;
using std::stringstream;
using std::vector;
const int num = 10000;
struct clamp_params {
dim4 size_;
dtype in_type_;
dtype lo_type_;
dtype hi_type_;
dtype out_type_;
clamp_params(dim4 size, dtype itype, dtype ltype, dtype htype, dtype otype)
: size_(size)
, in_type_(itype)
, lo_type_(ltype)
, hi_type_(htype)
, out_type_(otype) {}
};
template<typename T>
class Clamp : public ::testing::TestWithParam<clamp_params> {
public:
void SetUp() {
clamp_params params = GetParam();
SUPPORTED_TYPE_CHECK(double);
if (noDoubleTests(params.in_type_))
GTEST_SKIP() << "Double not supported on this device";
if (noHalfTests(params.in_type_))
GTEST_SKIP() << "Half not supported on this device";
if (noDoubleTests(params.hi_type_))
GTEST_SKIP() << "Double not supported on this device";
if (noHalfTests(params.hi_type_))
GTEST_SKIP() << "Half not supported on this device";
if (noDoubleTests(params.lo_type_))
GTEST_SKIP() << "Double not supported on this device";
if (noHalfTests(params.lo_type_))
GTEST_SKIP() << "Half not supported on this device";
in_ = randu(params.size_, params.in_type_);
lo_ = randu(params.size_, params.lo_type_) / T(10);
hi_ = T(1) - randu(params.size_, params.hi_type_) / T(10);
lo_ = lo_.as(params.lo_type_);
hi_ = hi_.as(params.hi_type_);
size_t num = params.size_.elements();
vector<T> hgold(num), hin(num), hlo(num), hhi(num);
in_.as((dtype)af::dtype_traits<T>::af_type).host(&hin[0]);
lo_.as((dtype)af::dtype_traits<T>::af_type).host(&hlo[0]);
hi_.as((dtype)af::dtype_traits<T>::af_type).host(&hhi[0]);
for (size_t i = 0; i < num; i++) {
if (hin[i] < hlo[i])
hgold[i] = hlo[i];
else if (hin[i] > hhi[i])
hgold[i] = hhi[i];
else
hgold[i] = hin[i];
}
gold_ = array(params.size_, &hgold[0]);
gold_ = gold_.as(params.out_type_);
gold_.eval();
}
af::array in_;
af::array lo_;
af::array hi_;
af::array gold_;
};
string pd4(dim4 dims) {
string out(32, '\0');
int len = snprintf(const_cast<char*>(out.data()), 32, "%lld_%lld_%lld_%lld",
dims[0], dims[1], dims[2], dims[3]);
out.resize(len);
return out;
}
string testNameGenerator(const ::testing::TestParamInfo<clamp_params> info) {
stringstream ss;
ss << "size_" << pd4(info.param.size_) << "_in_" << info.param.in_type_
<< "_lo_" << info.param.lo_type_ << "_hi_" << info.param.hi_type_;
return ss.str();
}
typedef Clamp<double> ClampFloatingPoint;
// clang-format off
INSTANTIATE_TEST_SUITE_P(
SmallDims, ClampFloatingPoint,
::testing::Values(
clamp_params(dim4(10), f32, f32, f32, f32),
clamp_params(dim4(10), f64, f32, f32, f64),
clamp_params(dim4(10), f16, f32, f32, f32),
clamp_params(dim4(10), f64, f64, f64, f64),
clamp_params(dim4(10), f16, f16, f16, f16),
clamp_params(dim4(10), s32, f32, f32, f32),
clamp_params(dim4(10), u32, f32, f32, f32),
clamp_params(dim4(10), s8, f32, f32, f32),
clamp_params(dim4(10), u8, f32, f32, f32),
clamp_params(dim4(10), b8, f32, f32, f32),
clamp_params(dim4(10), s64, f32, f32, f32),
clamp_params(dim4(10), u64, f32, f32, f32),
clamp_params(dim4(10), s16, f32, f32, f32),
clamp_params(dim4(10), u16, f32, f32, f32),
clamp_params(dim4(10, 10), f32, f32, f32, f32),
clamp_params(dim4(10, 10), f64, f32, f32, f64),
clamp_params(dim4(10, 10), f16, f32, f32, f32),
clamp_params(dim4(10, 10), f64, f64, f64, f64),
clamp_params(dim4(10, 10), f16, f16, f16, f16),
clamp_params(dim4(10, 10, 10), f32, f32, f32, f32),
clamp_params(dim4(10, 10, 10), f64, f32, f32, f64),
clamp_params(dim4(10, 10, 10), f16, f32, f32, f32),
clamp_params(dim4(10, 10, 10), f64, f64, f64, f64),
clamp_params(dim4(10, 10, 10), f16, f16, f16, f16)
),
testNameGenerator);
// clang-format on
TEST_P(ClampFloatingPoint, Basic) {
clamp_params params = GetParam();
array out = clamp(in_, lo_, hi_);
ASSERT_ARRAYS_NEAR(gold_, out, 1e-5);
}
TEST(Clamp, FloatArrayArray) {
array in = randu(num, f32);
array lo = randu(num, f32) / 10; // Ensure lo <= 0.1
array hi = 1.0 - randu(num, f32) / 10; // Ensure hi >= 0.9
eval(lo, hi);
vector<float> hout(num), hin(num), hlo(num), hhi(num);
array out = clamp(in, lo, hi);
out.host(&hout[0]);
in.host(&hin[0]);
lo.host(&hlo[0]);
hi.host(&hhi[0]);
for (int i = 0; i < num; i++) {
ASSERT_LE(hout[i], hhi[i]);
ASSERT_GE(hout[i], hlo[i]);
ASSERT_EQ(true,
hout[i] == hin[i] || hout[i] == hlo[i] || hout[i] == hhi[i]);
}
}
TEST(Clamp, FloatArrayScalar) {
array in = randu(num, f32);
array lo = randu(num, f32) / 10; // Ensure lo <= 0.1
float hi = 0.9;
vector<float> hout(num), hin(num), hlo(num);
array out = clamp(in, lo, hi);
out.host(&hout[0]);
in.host(&hin[0]);
lo.host(&hlo[0]);
for (int i = 0; i < num; i++) {
ASSERT_LE(hout[i], hi);
ASSERT_GE(hout[i], hlo[i]);
ASSERT_EQ(true,
hout[i] == hin[i] || hout[i] == hlo[i] || hout[i] == hi);
}
}
TEST(Clamp, FloatScalarArray) {
array in = randu(num, f32);
float lo = 0.1;
array hi = 1.0 - randu(num, f32) / 10; // Ensure hi >= 0.9
vector<float> hout(num), hin(num), hhi(num);
array out = clamp(in, lo, hi);
out.host(&hout[0]);
in.host(&hin[0]);
hi.host(&hhi[0]);
for (int i = 0; i < num; i++) {
ASSERT_LE(hout[i], hhi[i]);
ASSERT_GE(hout[i], lo);
ASSERT_EQ(true,
hout[i] == hin[i] || hout[i] == lo || hout[i] == hhi[i]);
}
}
TEST(Clamp, FloatScalarScalar) {
array in = randu(num, f32);
float lo = 0.1;
float hi = 0.9;
vector<float> hout(num), hin(num);
array out = clamp(in, lo, hi);
out.host(&hout[0]);
in.host(&hin[0]);
for (int i = 0; i < num; i++) {
ASSERT_LE(hout[i], hi);
ASSERT_GE(hout[i], lo);
ASSERT_EQ(true, hout[i] == hin[i] || hout[i] == lo || hout[i] == hi);
}
}