| 1 |
#ifndef AMX_CHECK_H_INCLUDED |
| 2 |
#define AMX_CHECK_H_INCLUDED |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <unistd.h> |
| 8 |
#ifdef __linux__ |
| 9 |
#include <sys/syscall.h> |
| 10 |
#endif |
| 11 |
#ifdef DEBUG |
| 12 |
#include <stdio.h> |
| 13 |
#endif |
| 14 |
#include "cpuid.h" |
| 15 |
|
| 16 |
#define XFEATURE_XTILECFG 17 |
| 17 |
#define XFEATURE_XTILEDATA 18 |
| 18 |
#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG) |
| 19 |
#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA) |
| 20 |
#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) |
| 21 |
|
| 22 |
#define ARCH_GET_XCOMP_PERM 0x1022 |
| 23 |
#define ARCH_REQ_XCOMP_PERM 0x1023 |
| 24 |
|
| 25 |
/* TODO: The tmm emulation is temporary for current |
| 26 |
AMX implementation with no tmm regclass, should |
| 27 |
be changed in the future. */ |
| 28 |
typedef struct __tile_config |
| 29 |
{ |
| 30 |
uint8_t palette_id; |
| 31 |
uint8_t start_row; |
| 32 |
uint8_t reserved_0[14]; |
| 33 |
uint16_t colsb[8]; /* Colum size of each tmm register in bytes */ |
| 34 |
uint16_t reserved_1[8]; |
| 35 |
uint8_t rows[8]; /* Row size of each tmm reg in bytes */ |
| 36 |
uint8_t reserved_2[8]; |
| 37 |
} __tilecfg; |
| 38 |
|
| 39 |
typedef union __union_tile_config |
| 40 |
{ |
| 41 |
__tilecfg s; |
| 42 |
uint8_t a[64]; |
| 43 |
} __tilecfg_u; |
| 44 |
|
| 45 |
typedef struct __tile |
| 46 |
{ |
| 47 |
/* Max size of tile register */ |
| 48 |
uint8_t buf[1024]; |
| 49 |
int rows; |
| 50 |
int colsb; |
| 51 |
} __tile; |
| 52 |
|
| 53 |
typedef struct __tilepair |
| 54 |
{ |
| 55 |
/* Max size of tile register */ |
| 56 |
uint8_t buf[2048]; |
| 57 |
int rows; |
| 58 |
int colsb; |
| 59 |
} __tilepair; |
| 60 |
|
| 61 |
/* Maxium col/row size in bytes */ |
| 62 |
#define MAX_ROWS 16 |
| 63 |
#define MAX_COLS 64 |
| 64 |
|
| 65 |
/* Stride (colum width in byte) used for tileload/store */ |
| 66 |
#define _STRIDE 64 |
| 67 |
|
| 68 |
#ifdef __linux__ |
| 69 |
/* We need syscall to use amx functions */ |
| 70 |
int request_perm_xtile_data() |
| 71 |
{ |
| 72 |
unsigned long bitmask; |
| 73 |
|
| 74 |
if (syscall (SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA) || |
| 75 |
syscall (SYS_arch_prctl, ARCH_GET_XCOMP_PERM, &bitmask)) |
| 76 |
return 0; |
| 77 |
|
| 78 |
return (bitmask & XFEATURE_MASK_XTILE) != 0; |
| 79 |
} |
| 80 |
#endif |
| 81 |
|
| 82 |
/* Initialize tile config by setting all tmm size to 16x64 */ |
| 83 |
void init_tile_config (__tilecfg_u *dst) |
| 84 |
{ |
| 85 |
int i; |
| 86 |
|
| 87 |
dst->s.palette_id = 1; |
| 88 |
dst->s.start_row = 0; |
| 89 |
|
| 90 |
for (i = 0; i < 14; i++) |
| 91 |
dst->s.reserved_0[i] = 0; |
| 92 |
|
| 93 |
for (i = 0; i < 8; i++) |
| 94 |
{ |
| 95 |
dst->s.colsb[i] = _STRIDE; |
| 96 |
dst->s.rows[i] = 16; |
| 97 |
dst->s.reserved_1[i] = 0; |
| 98 |
dst->s.reserved_2[i] = 0; |
| 99 |
} |
| 100 |
|
| 101 |
_tile_loadconfig (dst->a); |
| 102 |
} |
| 103 |
|
| 104 |
/* Init __tile variable that going to be store to register |
| 105 |
w/o extra buffer. If buffer exists, it should be the same |
| 106 |
size matrix as corresponding tmm register. |
| 107 |
Should execute init_tile_config first */ |
| 108 |
void init_tile_src (const int tmm_num, __tile *src, uint8_t *buffer) |
| 109 |
{ |
| 110 |
int rows, colsb, i, j; |
| 111 |
__tilecfg_u tmp; |
| 112 |
|
| 113 |
_tile_storeconfig (tmp.a); |
| 114 |
|
| 115 |
src->rows = rows = tmp.s.rows[tmm_num]; |
| 116 |
src->colsb = colsb = tmp.s.colsb[tmm_num]; |
| 117 |
|
| 118 |
for (i = 0; i < rows; i++) |
| 119 |
for (j = 0; j < colsb; j++) |
| 120 |
{ |
| 121 |
if(buffer) |
| 122 |
src->buf[i * colsb + j] = buffer[i * colsb + j]; |
| 123 |
else |
| 124 |
src->buf[i * colsb + j] = (i + 11 * j) % 256; |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/* Init __tile src and corresponding tmm register */ |
| 130 |
#define init_tile_reg_and_src(tmm_num, src) \ |
| 131 |
{ \ |
| 132 |
init_tile_src (tmm_num, &src, NULL); \ |
| 133 |
_tile_loadd (tmm_num, src.buf, _STRIDE); \ |
| 134 |
} |
| 135 |
|
| 136 |
#define init_tile_reg_and_src_with_buffer(tmm_num, src, buffer) \ |
| 137 |
{ \ |
| 138 |
init_tile_src (tmm_num, &src, buffer); \ |
| 139 |
_tile_loadd (tmm_num, src.buf, _STRIDE); \ |
| 140 |
} |
| 141 |
|
| 142 |
/* Zero __tile src. It should be init first. */ |
| 143 |
void zero_tile_src (__tile *src) |
| 144 |
{ |
| 145 |
int i, j; |
| 146 |
|
| 147 |
for (i = 0; i < src->rows; i++) |
| 148 |
for (j = 0; j < src->colsb; j++) |
| 149 |
src->buf[i * src->colsb + j] = 0; |
| 150 |
} |
| 151 |
|
| 152 |
/* Zero __tilepair src. It should be init first. */ |
| 153 |
void zero_pair_tile_src (__tilepair *src) |
| 154 |
{ |
| 155 |
memset(src->buf, 0, 2048); |
| 156 |
} |
| 157 |
|
| 158 |
/* Compare tile config value with __tilecfg_u dst */ |
| 159 |
int check_tile_config (__tilecfg_u *src, __tilecfg_u *dst) |
| 160 |
{ |
| 161 |
size_t size = sizeof(__tilecfg); |
| 162 |
uint8_t *pa_src = (uint8_t *) src->a; |
| 163 |
uint8_t *pa_dst = (uint8_t *) dst->a; |
| 164 |
|
| 165 |
for (int i = 0; i < size; i++) |
| 166 |
if (pa_src[i] != pa_dst[i]) |
| 167 |
return 0; |
| 168 |
|
| 169 |
return 1; |
| 170 |
} |
| 171 |
|
| 172 |
/* Compare tile register value with __tile variable */ |
| 173 |
int check_tile_register (__tile* ref, __tile* target) |
| 174 |
{ |
| 175 |
/* Tile register should be stored from tmm to |
| 176 |
memory and compare with emulation results. */ |
| 177 |
int rows = target->rows; |
| 178 |
int colsb = target->colsb; |
| 179 |
int i, j; |
| 180 |
|
| 181 |
for (i = 0; i < rows; i++) |
| 182 |
for (j = 0; j < colsb; j++) |
| 183 |
if (ref->buf[i * colsb + j] != target->buf[i * colsb + j]) |
| 184 |
return 0; |
| 185 |
|
| 186 |
return 1; |
| 187 |
} |
| 188 |
|
| 189 |
/* Compare float tile register value with __tile variable */ |
| 190 |
int check_float_tile_register (__tile* ref, __tile* target) |
| 191 |
{ |
| 192 |
/* Tile register should be stored from tmm to |
| 193 |
memory and compare with emulation results. */ |
| 194 |
int rows = target->rows; |
| 195 |
int colsb = target->colsb / 4; |
| 196 |
int i, j; |
| 197 |
uint32_t *ref_buf = (uint32_t *) ref->buf; |
| 198 |
uint32_t *target_buf = (uint32_t *) target->buf; |
| 199 |
|
| 200 |
for (i = 0; i < rows; i++) |
| 201 |
for (j = 0; j < colsb; j++) |
| 202 |
if (abs(ref_buf[i * colsb + j] - target_buf[i * colsb + j]) > 1) |
| 203 |
return 0; |
| 204 |
|
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
/* Compare pair_tile register value with __tile variable */ |
| 209 |
int check_pair_tile_register (__tile* ref_0, __tile* ref_1, __tilepair* target) |
| 210 |
{ |
| 211 |
/* Tile register should be stored from tmm to |
| 212 |
memory and compare with emulation results. */ |
| 213 |
int rows = target->rows; |
| 214 |
int colsb = target->colsb; |
| 215 |
int i, j; |
| 216 |
|
| 217 |
for (i = 0; i < rows; i++) |
| 218 |
for (j = 0; j < colsb; j++) |
| 219 |
{ |
| 220 |
if (ref_0->buf[i * colsb + j] != target->buf[i * colsb + j]) |
| 221 |
return 0; |
| 222 |
if (ref_1->buf[i * colsb + j] != target->buf[rows * colsb + i * colsb + j]) |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
#ifndef DO_TEST |
| 230 |
#define DO_TEST do_test |
| 231 |
static void test_amx (void); |
| 232 |
__attribute__ ((noinline)) |
| 233 |
static void |
| 234 |
do_test (void) |
| 235 |
{ |
| 236 |
test_amx (); |
| 237 |
} |
| 238 |
#endif |
| 239 |
|
| 240 |
int |
| 241 |
main () |
| 242 |
{ |
| 243 |
/* Check cpu support for AMX */ |
| 244 |
if (__builtin_cpu_supports ("amx-tile") |
| 245 |
#ifdef AMX_INT8 |
| 246 |
&& __builtin_cpu_supports ("amx-int8") |
| 247 |
#endif |
| 248 |
#ifdef AMX_BF16 |
| 249 |
&& __builtin_cpu_supports ("amx-bf16") |
| 250 |
#endif |
| 251 |
#ifdef AMX_FP16 |
| 252 |
&& __builtin_cpu_supports ("amx-fp16") |
| 253 |
#endif |
| 254 |
#ifdef AMX_COMPLEX |
| 255 |
&& __builtin_cpu_supports ("amx-complex") |
| 256 |
#endif |
| 257 |
#ifdef AMX_AVX512 |
| 258 |
&& __builtin_cpu_supports ("amx-avx512") |
| 259 |
#endif |
| 260 |
#ifdef AMX_TF32 |
| 261 |
&& __builtin_cpu_supports ("amx-tf32") |
| 262 |
#endif |
| 263 |
#ifdef AMX_TRANSPOSE |
| 264 |
&& __builtin_cpu_supports ("amx-transpose") |
| 265 |
#endif |
| 266 |
#ifdef AMX_FP8 |
| 267 |
&& __builtin_cpu_supports ("amx-fp8") |
| 268 |
#endif |
| 269 |
#ifdef AMX_MOVRS |
| 270 |
&& __builtin_cpu_supports ("amx-movrs") |
| 271 |
#endif |
| 272 |
#ifdef __linux__ |
| 273 |
&& request_perm_xtile_data () |
| 274 |
#endif |
| 275 |
) |
| 276 |
{ |
| 277 |
DO_TEST (); |
| 278 |
#ifdef DEBUG |
| 279 |
printf ("PASSED\n"); |
| 280 |
#endif |
| 281 |
} |
| 282 |
#ifdef DEBUG |
| 283 |
else |
| 284 |
printf ("SKIPPED\n"); |
| 285 |
#endif |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
#endif |