"git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "c754652fcd1a5ac0e727343486657f5ef71b3252"
math_v2.hpp 2.41 KB
Newer Older
1
2
3
#ifndef CK_MATH_V2_HPP
#define CK_MATH_V2_HPP

4
#include <cmath>
5
#include "data_type.hpp"
6
#include "type.hpp"
7
8
9
10

namespace ck {
namespace math {

11
12
// math functions for the host,  some are implemented by calling C++ std functions

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)
{
33
    uint16_t xx = ck::bit_cast<uint16_t>(x);
34

35
    uint16_t abs_xx = xx & 0x7fff;
36

37
    half_t abs_x = ck::bit_cast<half_t>(abs_xx);
38
39
40
41

    return abs_x;
};

42
static inline __host__ bool isnan(float x) { return std::isnan(x); };
43

44
static inline __host__ bool isnan(double x) { return std::isnan(x); };
45

46
static inline __host__ bool isnan(int8_t x)
47
48
49
50
51
{
    (void)x;
    return false;
};

52
static inline __host__ bool isnan(int32_t x)
53
54
55
56
57
58
59
{
    (void)x;
    return false;
};

static inline __host__ bool isnan(half_t x)
{
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
    uint16_t xx = ck::bit_cast<uint16_t>(x);

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

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

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

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

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

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

101
102
103
104
static inline __device__ bool isnan(int32_t x)
{
    (void)x;
    return false;
105
};
106

107
108
109
110
111
112
static inline __device__ bool isnan(half_t x) { return ::__hisnan(x); };

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

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

113
114
115
116
} // namespace math
} // namespace ck

#endif