math_v2.hpp 7.47 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 {

17
18
// math functions for the host,  some are implemented by calling C++ std functions

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)
{
39
    uint16_t xx = ck::bit_cast<uint16_t>(x);
40

41
    uint16_t abs_xx = xx & 0x7fff;
42

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

    return abs_x;
};

Adam Osewski's avatar
Adam Osewski committed
48
49
50
51
52
53
54
55
#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

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

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

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

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

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

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

Adam Osewski's avatar
Adam Osewski committed
79
80
81
82
83
84
85
86
#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
87
88
89
90
91
static inline __host__ half_t sqrt(half_t x)
{
    return static_cast<half_t>(std::sqrt(static_cast<float>(x)));
};

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

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

96
97
template <typename T>
inline __host__ T tanh(T x)
98
{
99
    return ck::type_convert<T>(std::tanhf(ck::type_convert<float>(x)));
100
101
};

102
103
104
105
106
107
108
109
110
111
112
113
template <>
inline __host__ float tanh<float>(float x)
{
    return std::tanhf(x);
};

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

114
// prevent implicit type casting
115
template <typename T>
116
inline __host__ T exp(T x);
117
118
119
120
121
122

template <>
inline __host__ float exp<float>(float x)
{
    return std::expf(x);
}
123

124
125
126
127
128
129
template <>
inline __host__ half_t exp<half_t>(half_t x)
{
    return type_convert<half_t>(std::expf(type_convert<float>(x)));
};

130
131
132
133
134
135
template <>
inline __host__ double exp<double>(double x)
{
    return std::exp(x);
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// prevent implicit type casting
template <typename T>
inline __host__ T exp2(T x);

template <>
inline __host__ float exp2<float>(float x)
{
    return std::exp2f(x);
}

template <>
inline __host__ double exp2<double>(double x)
{
    return std::exp2l(x); // TODO: std does not have exp2 for double till c++23
}

152
153
154
155
156
template <typename T>
inline __host__ T log(T x)
{
    return ck::type_convert<T>(std::logf(ck::type_convert<float>(x)));
}
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
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);
}
206

207
208
209
210
211
212
213
214
215
216
217
218
219
// 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;
};

Chao Liu's avatar
Chao Liu committed
220
static inline __device__ constexpr int32_t abs(int32_t x)
221
222
223
224
225
226
{
    int32_t sgn = x >> (32 - 1);

    return (x ^ sgn) - sgn;
};

Adam Osewski's avatar
Adam Osewski committed
227
228
229
230
231
232
233
234
235
#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

236
237
238
239
240
241
242
243
244
245
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;
};
246
247
248
249
250
251
252
253
254
255

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;
};
256

257
258
259
260
static inline __device__ bool isnan(int32_t x)
{
    (void)x;
    return false;
261
};
262

Adam Osewski's avatar
Adam Osewski committed
263
264
265
266
267
268
269
270
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __device__ bool isnan(int4_t x)
{
    (void)x;
    return false;
};
#endif

271
272
273
274
275
276
static inline __device__ bool isnan(half_t x)
{
    uint16_t xx = ck::bit_cast<uint16_t>(x);

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

rocking5566's avatar
rocking5566 committed
278
279
280
281
282
static inline __device__ half_t sqrt(half_t x)
{
    return static_cast<half_t>(__builtin_amdgcn_sqrtf(static_cast<float>(x)));
};

283
static inline __device__ float sqrt(float x) { return __builtin_amdgcn_sqrtf(x); };
284

285
static inline __device__ double sqrt(double x) { return __builtin_amdgcn_sqrt(x); };
286

287
288
template <typename T>
inline __device__ T tanh(T x)
289
{
290
    return ck::type_convert<T>(::tanhf(ck::type_convert<float>(x)));
291
292
};

293
294
template <>
inline __device__ float tanh<float>(float x)
295
{
296
    return ::tanhf(x);
297
298
};

299
300
301
302
303
304
template <>
inline __device__ double tanh<double>(double x)
{
    return ::tanh(x);
};

305
// prevent implicit type casting
306
template <typename T>
307
308
309
310
inline __device__ T exp(T x);

template <>
inline __device__ float exp<float>(float x)
311
{
312
313
    return __expf(x);
}
314
315
316
317
318
319
320
321

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

template <>
322
inline __device__ double exp<double>(double x)
323
{
324
325
326
327
328
329
    return exp(x);
}

// prevent implicit type casting
template <typename T>
inline __device__ T exp2(T x);
330

331
template <>
332
inline __device__ float exp2<float>(float x)
333
{
334
335
336
337
338
339
340
341
    return exp2f(x);
}

template <>
inline __device__ double exp2<double>(double x)
{
    return exp2(x);
}
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401

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)
{
    return hlog(x);
};

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);
};

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);
};
402

403
404
} // namespace math
} // namespace ck