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
|
#ifndef AMX_HELPER_H_INCLUDED
#define AMX_HELPER_H_INCLUDED
#include <immintrin.h>
#include <xmmintrin.h>
#include "amx-check.h"
typedef union
{
_Float16 f16;
uint16_t u;
} union16f_uw;
typedef union
{
__bf16 bf16;
uint16_t u;
} union16bh_uw;
typedef union
{
float f;
uint32_t u;
} union32f_ud;
typedef union
{
__m512 m;
uint8_t u[64];
} union512_ub;
#if defined(AMX_FP16) || defined(AMX_COMPLEX) || defined (AMX_AVX512)
/* Transformation functions between fp16/float */
static uint16_t make_f32_fp16 (float f)
{
union16f_uw tmp;
__m128 b = _mm_set_ss (f);
__m128h a;
tmp.f16 = _mm_cvtsh_h (_mm_cvtss_sh (a, b));
return tmp.u;
}
static float make_fp16_f32 (uint16_t fp)
{
union16f_uw tmp;
tmp.u = fp;
__m128h b = _mm_set_sh (tmp.f16);
__m128 a;
return _mm_cvtss_f32 (_mm_cvtsh_ss (a, b));
}
/* Init tile buffer with fp16 pairs */
void init_fp16_max_tile_buffer (uint8_t* buf)
{
int i, j;
uint16_t* ptr = (uint16_t *) buf;
for (i = 0; i < 16; i++)
for (j = 0; j < 32; j++)
{
float f = 2.5f * i + 1.25f * j;
ptr[i * 32 + j] = make_f32_fp16 (f);
}
}
/* Init tile fp16 pair buffer with zero */
void init_fp16_max_tile_zero_buffer (uint8_t* buf)
{
int i, j;
uint16_t* ptr = (uint16_t *) buf;
for (i = 0; i < 16; i++)
for (j = 0; j < 32; j++)
ptr[i * 32 + j] = make_f32_fp16 (0.0f);
}
#endif
#if defined (AMX_AVX512)
/* Transformation functions between bf16/float */
static uint16_t make_f32_bf16 (float f)
{
union16bh_uw tmp;
tmp.bf16 = (__bf16) f;
return tmp.u;
}
static float make_bf16_f32 (uint16_t bf)
{
union16bh_uw tmp;
tmp.u = bf;
return _mm_cvtsbh_ss (tmp.bf16);
}
/* Init tile buffer with bf16 pairs */
void init_bf16_max_tile_buffer (uint8_t *buf)
{
int i, j;
uint16_t* ptr = (uint16_t *) buf;
for (i = 0; i < 16; i++)
for (j = 0; j < 32; j++)
{
float f = 2.5f * i + 1.25f * j;
ptr[i * 32 + j] = make_f32_bf16 (f);
}
}
#endif
/* Init tile buffer with fp32 */
void init_fp32_max_tile_buffer (uint8_t *buf)
{
int i, j;
float* ptr = (float *) buf;
for (i = 0; i < 16; i++)
for (j = 0; j < 16; j++)
ptr[i * 16 + j] = 2.5f * i + 1.25f * j;
}
/* Init tile buffer with int32 */
void init_int32_max_tile_buffer (uint8_t *buf)
{
int i, j;
uint32_t *ptr = (uint32_t *)buf;
for (i = 0; i < 16; i++)
for (j = 0; j < 16; j++)
ptr[i * 16 + j] = (uint32_t) (3 * j - 16 * i);
}
#define COMPARE_ZMM(A, B) \
for (int j = 0; j < 16; j++) \
{ \
union32f_ud fu1, fu2; \
fu1.f = A[j]; \
fu2.f = B[j]; \
if (fu1.u != fu2.u) \
abort (); \
}
#define COMPARE_ZMM_BF16(A, B) \
for (int j = 0; j < 32; j++) \
{ \
union16bh_uw fu1, fu2; \
fu1.bf16 = A[j]; \
fu2.bf16 = B[j]; \
if (fu1.u != fu2.u) \
abort(); \
}
#define COMPARE_ZMM_FP16(A, B) \
for (int j = 0; j < 32; j++) \
{ \
union16f_uw fu1, fu2; \
fu1.f16 = A[j]; \
fu2.f16 = B[j]; \
if (fu1.u != fu2.u) \
abort(); \
}
/* Mask low 13bits to zero */
static float zero_lower_mantissa_bits_fp32 (float x)
{
union32f_ud tmp;
tmp.f = x;
tmp.u = tmp.u & 0xffffe000;
return tmp.f;
}
/* Handle SNAN */
static float silence_snan_fp32 (float x)
{
union32f_ud tmp;
tmp.f = x;
if ((((tmp.u & 0x7f800000) >> 23) == 0xff) &&
((tmp.u & 0x007fffff) != 0) &&
((tmp.u & 0x00400000) == 0))
tmp.u = tmp.u | 0x00400000;
return tmp.f;
}
#endif
|