-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathmath.cpp
More file actions
192 lines (157 loc) · 5.86 KB
/
math.cpp
File metadata and controls
192 lines (157 loc) · 5.86 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
/*******************************************************
* Copyright (c) 2025, 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/data.h>
#include <af/device.h>
#include <af/exception.h>
#include <af/random.h>
#include <complex>
// This makes the macros cleaner
using af::array;
using af::dim4;
using af::dtype_traits;
using af::exception;
using af::randu;
using half_float::half;
using std::abs;
using std::endl;
using std::vector;
const int num = 10000;
const float hlf_err = 1e-2;
const float flt_err = 1e-3;
const double dbl_err = 1e-6;
typedef std::complex<float> complex_float;
typedef std::complex<double> complex_double;
template<typename T>
T sigmoid(T in) {
return T(1.0 / (1.0 + std::exp(-in)));
}
template<typename T>
T rsqrt(T in) {
return T(1.0 / sqrt(in));
}
#define MATH_TEST(T, func, err, lo, hi) \
TEST(Math, func##_##T) { \
try { \
SUPPORTED_TYPE_CHECK(T); \
af_dtype ty = (af_dtype)dtype_traits<T>::af_type; \
array a = (hi - lo) * randu(num, ty) + lo + err; \
a = a.as(ty); \
eval(a); \
array b = func(a); \
vector<T> h_a(a.elements()); \
a.host(&h_a[0]); \
for (size_t i = 0; i < h_a.size(); i++) { h_a[i] = func(h_a[i]); } \
\
ASSERT_VEC_ARRAY_NEAR(h_a, dim4(h_a.size()), b, err); \
} catch (exception & ex) { FAIL() << ex.what(); } \
}
#define MATH_TESTS_HALF(func) MATH_TEST(half, func, hlf_err, 0.05f, 0.95f)
#define MATH_TESTS_FLOAT(func) MATH_TEST(float, func, flt_err, 0.05f, 0.95f)
#define MATH_TESTS_DOUBLE(func) MATH_TEST(double, func, dbl_err, 0.05, 0.95)
#define MATH_TESTS_CFLOAT(func) \
MATH_TEST(complex_float, func, flt_err, 0.05f, 0.95f)
#define MATH_TESTS_CDOUBLE(func) \
MATH_TEST(complex_double, func, dbl_err, 0.05, 0.95)
#define MATH_TESTS_REAL(func) \
MATH_TESTS_HALF(func) \
MATH_TESTS_FLOAT(func) \
MATH_TESTS_DOUBLE(func)
#define MATH_TESTS_CPLX(func) \
MATH_TESTS_CFLOAT(func) \
MATH_TESTS_CDOUBLE(func)
#define MATH_TESTS_ALL(func) \
MATH_TESTS_REAL(func) \
MATH_TESTS_CPLX(func)
#define MATH_TESTS_LIMITS_REAL(func, lo, hi) \
MATH_TEST(half, func, hlf_err, lo, hi) \
MATH_TEST(float, func, flt_err, lo, hi) \
MATH_TEST(double, func, dbl_err, lo, hi)
#define MATH_TESTS_LIMITS_CPLX(func, lo, hi) \
MATH_TEST(complex_float, func, flt_err, lo, hi) \
MATH_TEST(complex_double, func, dbl_err, lo, hi)
MATH_TESTS_ALL(sin)
MATH_TESTS_ALL(cos)
MATH_TESTS_ALL(tan)
MATH_TESTS_REAL(asin)
MATH_TESTS_REAL(acos)
MATH_TESTS_REAL(atan)
MATH_TESTS_ALL(sinh)
MATH_TESTS_ALL(cosh)
MATH_TESTS_ALL(tanh)
MATH_TESTS_ALL(sqrt)
MATH_TESTS_ALL(exp)
MATH_TESTS_ALL(log)
MATH_TESTS_REAL(log10)
MATH_TESTS_REAL(log2)
MATH_TESTS_REAL(rsqrt)
MATH_TESTS_REAL(sigmoid)
MATH_TESTS_LIMITS_REAL(abs, -10, 10)
MATH_TESTS_LIMITS_REAL(ceil, -10, 10)
MATH_TESTS_LIMITS_REAL(floor, -10, 10)
#if __cplusplus > 199711L || _MSC_VER >= 1800
MATH_TESTS_CPLX(asin)
MATH_TESTS_CPLX(acos)
MATH_TESTS_CPLX(atan)
MATH_TESTS_ALL(asinh)
MATH_TESTS_ALL(atanh)
MATH_TESTS_LIMITS_REAL(acosh, 1, 5)
MATH_TESTS_LIMITS_CPLX(acosh, 1, 5)
MATH_TESTS_LIMITS_REAL(round, -10, 10)
MATH_TESTS_REAL(cbrt)
MATH_TESTS_REAL(expm1)
MATH_TESTS_REAL(log1p)
MATH_TESTS_REAL(erf)
MATH_TESTS_REAL(erfc)
#endif
TEST(Math, Not) {
array a = randu(5, 5, b8);
array b = !a;
char *ha = a.host<char>();
char *hb = b.host<char>();
for (int i = 0; i < a.elements(); i++) { ASSERT_EQ(ha[i] ^ hb[i], true); }
af_free_host(ha);
af_free_host(hb);
}
TEST(Math, Modulus) {
af::dim4 shape(2, 2);
std::vector<long long> aData{3, 3, 3, 3};
std::vector<long long> bData{2, 2, 2, 2};
auto a = af::array(shape, aData.data(), afHost);
auto b = af::array(shape, bData.data(), afHost);
auto rem = a % b;
auto neg_rem = -a % b;
ASSERT_ARRAYS_EQ(af::constant(1, shape, s64), rem);
ASSERT_ARRAYS_EQ(af::constant(-1, shape, s64), neg_rem);
}
TEST(Math, ModulusFloat) {
SUPPORTED_TYPE_CHECK(half_float::half);
af::dim4 shape(2, 2);
auto a = af::constant(3, shape, af::dtype::f16);
auto b = af::constant(2, shape, af::dtype::f16);
auto a32 = af::constant(3, shape, af::dtype::f32);
auto b32 = af::constant(2, shape, af::dtype::f32);
auto a64 = af::constant(3, shape, af::dtype::f64);
auto b64 = af::constant(2, shape, af::dtype::f64);
auto rem = a % b;
auto rem32 = a32 % b32;
auto rem64 = a64 % b64;
auto neg_rem = -a % b;
auto neg_rem32 = -a32 % b32;
auto neg_rem64 = -a64 % b64;
ASSERT_ARRAYS_EQ(af::constant(1, shape, af::dtype::f16), rem);
ASSERT_ARRAYS_EQ(af::constant(1, shape, af::dtype::f32), rem32);
ASSERT_ARRAYS_EQ(af::constant(1, shape, af::dtype::f64), rem64);
ASSERT_ARRAYS_EQ(af::constant(-1, shape, af::dtype::f16), neg_rem);
ASSERT_ARRAYS_EQ(af::constant(-1, shape, af::dtype::f32), neg_rem32);
ASSERT_ARRAYS_EQ(af::constant(-1, shape, af::dtype::f64), neg_rem64);
ASSERT_ARRAYS_EQ(rem32.as(f16), rem);
}