math_v2.hpp 6.87 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
3

Chao Liu's avatar
Chao Liu committed
4
#pragma once
5

Qianfeng's avatar
Qianfeng committed
6
#ifndef __HIP_DEVICE_COMPILE__
7
#include <cmath>
Qianfeng's avatar
Qianfeng committed
8
#endif
Chao Liu's avatar
Chao Liu committed
9
10
11

#include "ck/utility/data_type.hpp"
#include "ck/utility/type.hpp"
12
#include "ck/utility/type_convert.hpp"
13
14
15
16

namespace ck {
namespace math {

Umang Yadav's avatar
Umang Yadav committed
17
#ifndef __HIPCC_RTC__
18
19
// math functions for the host,  some are implemented by calling C++ std functions

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
static inline __host__ float abs(float x) { return std::abs(x); };

static inline __host__ double abs(double x) { return std::abs(x); };

static inline __host__ int8_t abs(int8_t x)
{
    int8_t sgn = x >> (8 - 1);

    return (x ^ sgn) - sgn;
};

static inline __host__ int32_t abs(int32_t x)
{
    int32_t sgn = x >> (32 - 1);

    return (x ^ sgn) - sgn;
};

static inline __host__ half_t abs(half_t x)
{
40
    uint16_t xx = ck::bit_cast<uint16_t>(x);
41

42
    uint16_t abs_xx = xx & 0x7fff;
43

44
    half_t abs_x = ck::bit_cast<half_t>(abs_xx);
45
46
47
48

    return abs_x;
};

Adam Osewski's avatar
Adam Osewski committed
49
50
51
52
53
54
55
56
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __host__ int4_t abs(int4_t x)
{
    int4_t sgn = x >> (4 - 1);
    return (x ^ sgn) - sgn;
}
#endif

57
static inline __host__ bool isnan(float x) { return std::isnan(x); };
58

59
static inline __host__ bool isnan(double x) { return std::isnan(x); };
60

61
static inline __host__ bool isnan(int8_t x)
62
63
64
65
66
{
    (void)x;
    return false;
};

67
static inline __host__ bool isnan(int32_t x)
68
69
70
71
72
73
74
{
    (void)x;
    return false;
};

static inline __host__ bool isnan(half_t x)
{
75
76
77
78
79
    uint16_t xx = ck::bit_cast<uint16_t>(x);

    return (xx & 0x7FFF) > 0x7C00;
};

Adam Osewski's avatar
Adam Osewski committed
80
81
82
83
84
85
86
87
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __host__ bool isnan(int4_t x)
{
    (void)x;
    return false;
};
#endif

rocking5566's avatar
rocking5566 committed
88
89
90
91
92
static inline __host__ half_t sqrt(half_t x)
{
    return static_cast<half_t>(std::sqrt(static_cast<float>(x)));
};

93
94
95
96
static inline __host__ float sqrt(float x) { return std::sqrt(x); };

static inline __host__ double sqrt(double x) { return std::sqrt(x); };

97
98
template <typename T>
inline __host__ T tanh(T x)
99
{
100
    return ck::type_convert<T>(std::tanhf(ck::type_convert<float>(x)));
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
template <>
inline __host__ float tanh<float>(float x)
{
    return std::tanhf(x);
};

template <>
inline __host__ double tanh<double>(double x)
{
    return std::tanh(x);
};

template <typename T>
inline __host__ T exp(T x)
{
    return ck::type_convert<T>(std::expf(ck::type_convert<float>(x)));
}

template <>
inline __host__ float exp<float>(float x)
{
    return std::expf(x);
}
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
template <>
inline __host__ double exp<double>(double x)
{
    return std::exp(x);
}

template <typename T>
inline __host__ T log(T x)
{
    return ck::type_convert<T>(std::logf(ck::type_convert<float>(x)));
}

template <>
inline __host__ float log<float>(float x)
{
    return std::logf(x);
}

template <>
inline __host__ double log<double>(double x)
{
    return std::log(x);
}

template <typename T>
inline __host__ T pow(T x, T gamma)
{
    return ck::type_convert<T>(
        std::powf(ck::type_convert<float>(x), ck::type_convert<float>(gamma)));
}

template <>
inline __host__ float pow<float>(float x, float gamma)
{
    return std::powf(x, gamma);
}

template <>
inline __host__ double pow<double>(double x, double gamma)
{
    return std::pow(x, gamma);
}

template <typename T>
inline __host__ T expm1(T x)
{
    return ck::type_convert<T>(std::expm1f(ck::type_convert<float>(x)));
}

template <>
inline __host__ float expm1<float>(float x)
{
    return std::expm1f(x);
}

template <>
inline __host__ double expm1<double>(double x)
{
    return std::expm1(x);
}
187

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// math functions for the HIP kernel,  some are implemented by calling hip builtin functions

static inline __device__ float abs(float x) { return ::abs(x); };

static inline __device__ double abs(double x) { return ::abs(x); };

static inline __device__ int8_t abs(int8_t x)
{
    int8_t sgn = x >> (8 - 1);

    return (x ^ sgn) - sgn;
};

static inline __device__ int32_t abs(int32_t x)
{
    int32_t sgn = x >> (32 - 1);

    return (x ^ sgn) - sgn;
};

Adam Osewski's avatar
Adam Osewski committed
208
209
210
211
212
213
214
215
216
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __device__ int4_t abs(int4_t x)
{
    int4_t sgn = x >> (4 - 1);

    return (x ^ sgn) - sgn;
};
#endif

217
218
219
220
221
222
223
224
225
226
static inline __device__ half_t abs(half_t x)
{
    uint16_t xx = ck::bit_cast<uint16_t>(x);

    uint16_t abs_xx = xx & 0x7fff;

    half_t abs_x = ck::bit_cast<half_t>(abs_xx);

    return abs_x;
};
227
228
229
230
231
232
233
234
235
236

static inline __device__ bool isnan(float x) { return ::isnan(x); };

static inline __device__ bool isnan(double x) { return ::isnan(x); };

static inline __device__ bool isnan(int8_t x)
{
    (void)x;
    return false;
};
237

238
239
240
241
static inline __device__ bool isnan(int32_t x)
{
    (void)x;
    return false;
242
};
243

Adam Osewski's avatar
Adam Osewski committed
244
245
246
247
248
249
250
251
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __device__ bool isnan(int4_t x)
{
    (void)x;
    return false;
};
#endif

252
253
254
255
256
257
static inline __device__ bool isnan(half_t x)
{
    uint16_t xx = ck::bit_cast<uint16_t>(x);

    return (xx & 0x7FFF) > 0x7C00;
};
258

rocking5566's avatar
rocking5566 committed
259
260
261
262
263
static inline __device__ half_t sqrt(half_t x)
{
    return static_cast<half_t>(__builtin_amdgcn_sqrtf(static_cast<float>(x)));
};

264
static inline __device__ float sqrt(float x) { return __builtin_amdgcn_sqrtf(x); };
265

266
static inline __device__ double sqrt(double x) { return __builtin_amdgcn_sqrt(x); };
267

268
269
270
271
272
273
274
275
template <typename T>
inline __device__ T tanh(T x)
{
    return ck::type_convert<T>(::tanhf(ck::type_convert<float>(x)));
};

template <>
inline __device__ float tanh<float>(float x)
276
{
277
    return ::tanhf(x);
278
279
};

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
template <>
inline __device__ double tanh<double>(double x)
{
    return ::tanh(x);
};

template <typename T>
inline __device__ T exp(T x)
{
    return ck::type_convert<T>(__expf(ck::type_convert<float>(x)));
};

template <>
inline __device__ half_t exp<half_t>(half_t x)
{
    return hexp(x);
};

template <>
inline __device__ float exp<float>(float x)
{
    return __expf(x);
};
303

304
305
306
307
308
309
310
311
312
313
314
315
316
317
template <>
inline __device__ double exp<double>(double x)
{
    return exp(x);
};

template <typename T>
inline __device__ T log(T x)
{
    return ck::type_convert<T>(__logf(ck::type_convert<float>(x)));
};

template <>
inline __device__ half_t log<half_t>(half_t x)
318
{
319
    return hlog(x);
320
321
};

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
template <>
inline __device__ float log<float>(float x)
{
    return __logf(x);
};

template <>
inline __device__ double log<double>(double x)
{
    return log(x);
};

template <typename T>
inline __device__ T pow(T x, T gamma)
{
    return ck::type_convert<T>(powf(ck::type_convert<float>(x), ck::type_convert<float>(gamma)));
};

template <>
inline __device__ float pow<float>(float x, float gamma)
{
    return powf(x, gamma);
};
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
template <>
inline __device__ double pow<double>(double x, double gamma)
{
    return pow(x, gamma);
};

template <typename T>
inline __device__ T expm1(T x)
{
    return ck::type_convert<T>(expm1f(ck::type_convert<float>(x)));
};

template <>
inline __device__ float expm1<float>(float x)
{
    return expm1f(x);
};

template <>
inline __device__ double expm1<double>(double x)
{
    return expm1(x);
};
369

370
371
} // namespace math
} // namespace ck