Skip to content

Commit 86ce950

Browse files
steffenlarsenbb-sycl
authored andcommitted
[SYCL] Fix result check in joint matrix tests (intel#1432)
Some tests in SYCL/Matrix verify their results but only use the results to print "passed" or "failed". A subset of these tests have checks for "passed" in the output, but does not use FileCheck to govern the check. This commit changes these tests to return a non-zero value in the main function if the result verification failed. This allows the test system to correctly identify tests that fail this verification. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 14ba9e7 commit 86ce950

10 files changed

+172
-27
lines changed

SYCL/Matrix/Legacy/joint_matrix_bf16_impl.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ int main() {
155155
res = false;
156156
}
157157
}
158-
if (res)
159-
std::cout << "passed\n";
160-
else
161-
std::cout << "failed\n";
158+
std::cout << (res ? "passed" : "failed") << std::endl;
159+
return !res;
162160
}

SYCL/Matrix/Legacy/joint_matrix_bfloat16_impl.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ int main() {
152152
res = false;
153153
}
154154
}
155-
if (res)
156-
std::cout << "passed\n";
157-
else
158-
std::cout << "failed\n";
155+
std::cout << (res ? "passed" : "failed") << std::endl;
156+
return !res;
159157
}

SYCL/Matrix/joint_matrix_bfloat16_32x64.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,164 @@ using bfloat16 = sycl::ext::oneapi::bfloat16;
2222

2323
#define SG_SZ 16
2424

25+
<<<<<<< HEAD
2526
#include "joint_matrix_bfloat16_32x64_impl.hpp"
27+
=======
28+
#define TM 32
29+
#define TN 64
30+
#define TK 16
31+
32+
#define BF16_EPSILON 0.00781250
33+
34+
template <typename T, size_t NUM_ROWS, size_t NUM_COLS> struct big_matrix {
35+
private:
36+
T *mat;
37+
38+
public:
39+
T *get_data() { return mat; }
40+
void set_data(T *data) { mat = data; }
41+
big_matrix(T *data) : mat(data) {}
42+
};
43+
44+
template <typename T1, typename T2, size_t M, size_t N, size_t K>
45+
void matrix_multiply(big_matrix<T1, M, N> &C, big_matrix<T2, M, K> &A,
46+
big_matrix<T2, K / 2, N * 2> &B) {
47+
size_t NDRangeM = M / TM;
48+
size_t NDRangeN = N / TN;
49+
buffer<bfloat16, 2> bufA(A.get_data(), range<2>(M, K));
50+
buffer<bfloat16, 2> bufB(B.get_data(), range<2>(K, N));
51+
buffer<float, 2> bufC((float *)C.get_data(), range<2>(M, N));
52+
53+
queue q;
54+
q.submit([&](handler &cgh) {
55+
auto accC = bufC.get_access<access::mode::read_write>(cgh);
56+
auto accA = bufA.get_access<access::mode::read_write>(cgh);
57+
auto accB = bufB.get_access<access::mode::read_write>(cgh);
58+
59+
cgh.parallel_for<class imatrix>(
60+
nd_range<2>({NDRangeM, NDRangeN * SG_SZ}, {1, 1 * SG_SZ}),
61+
[=](nd_item<2> spmd_item) [[intel::reqd_sub_group_size(SG_SZ)]]
62+
63+
{
64+
// The submatrix API has to be accessed by all the workitems in a
65+
// subgroup these functions will be called once by the subgroup no
66+
// code divergence between the workitems
67+
const auto global_idx = spmd_item.get_global_id(0);
68+
const auto global_idy = spmd_item.get_global_id(1);
69+
const auto sg_startx = global_idx - spmd_item.get_local_id(0);
70+
const auto sg_starty = global_idy - spmd_item.get_local_id(1);
71+
72+
ext::oneapi::sub_group sg = spmd_item.get_sub_group();
73+
joint_matrix<bfloat16, TM, TK> sub_a(sg);
74+
// For B, since current implementation does not support non-packed
75+
// layout, users need to specify the updated VNNI sizes along with
76+
// the packed_b layout. By default, the layout is row_major and size
77+
// is (TK, TN).
78+
joint_matrix<bfloat16, TK, TN, matrix_layout::packed_b> sub_b(sg);
79+
joint_matrix<float, TM, TN> sub_c(sg);
80+
81+
joint_matrix_load(sg, sub_c,
82+
accC.get_pointer() + (sg_startx * TM) * N +
83+
sg_starty / SG_SZ * TN,
84+
N, matrix_layout::row_major);
85+
for (int k = 0; k < K / TK; k += 1) { //
86+
joint_matrix_load(
87+
sg, sub_a, accA.get_pointer() + (sg_startx * TM) * K + k * TK,
88+
K, matrix_layout::row_major);
89+
// Assuming B data is already in VNNI format.
90+
joint_matrix_load(sg, sub_b,
91+
accB.get_pointer() + (k * TK / 2) * (N * 2) +
92+
sg_starty / SG_SZ * TN * 2,
93+
N * 2, matrix_layout::packed_b);
94+
sub_c = joint_matrix_mad(sg, sub_a, sub_b, sub_c);
95+
}
96+
joint_matrix_store(sg, sub_c,
97+
accC.get_pointer() + (sg_startx * TM) * N +
98+
sg_starty / SG_SZ * TN,
99+
N, matrix_layout::row_major);
100+
}); // parallel for
101+
}).wait();
102+
}
103+
104+
static constexpr size_t MATRIX_M = TM * 2;
105+
static constexpr size_t MATRIX_N = TN * 2;
106+
static constexpr size_t MATRIX_K = TK * 2;
107+
bfloat16 A[MATRIX_M][MATRIX_K];
108+
bfloat16 B[MATRIX_K / 2][MATRIX_N * 2];
109+
unsigned short Aref[MATRIX_M][MATRIX_K];
110+
unsigned short Bref[MATRIX_K / 2][MATRIX_N * 2];
111+
float C[MATRIX_M][MATRIX_N];
112+
float D[MATRIX_M][MATRIX_N];
113+
114+
float make_fp32(short x) {
115+
unsigned int y = x;
116+
y = y << 16;
117+
float *res = reinterpret_cast<float *>(&y);
118+
return *res;
119+
}
120+
121+
unsigned short make_bf16(float x) {
122+
int *res = reinterpret_cast<int *>(&x);
123+
*res = *res >> 16;
124+
return (unsigned short)*res;
125+
}
126+
127+
void matrix_multiply_ref(int *A_mem, int *B_mem, int *C_mem, int M, int N,
128+
int K) {
129+
// tiling
130+
for (int m = 0; m < M; m++)
131+
for (int n = 0; n < N; n++) {
132+
for (int k = 0; k < K; k++) {
133+
short *va = (short *)(A_mem + m * K + k);
134+
short *vb = (short *)(B_mem + k * N + n);
135+
float acc = *((float *)(C_mem + m * N + n));
136+
// FIXME: Should we do reduce-add in another version?
137+
for (int i = 0; i < 2; i++) {
138+
acc += (make_fp32(va[i]) * make_fp32(vb[i]));
139+
}
140+
*((float *)(C_mem + m * N + n)) = acc;
141+
}
142+
}
143+
}
144+
145+
int main() {
146+
for (int i = 0; i < MATRIX_M; i++) {
147+
for (int j = 0; j < MATRIX_K; j++) {
148+
// bfloat16 is created using unsigned short since conversion from float to
149+
// bfloat16 is not supported on the host side yet
150+
A[i][j] = make_bf16(1.0f * (i + j));
151+
Aref[i][j] = make_bf16(1.0f * (i + j));
152+
}
153+
}
154+
for (int i = 0; i < MATRIX_K / 2; i++) {
155+
for (int j = 0; j < MATRIX_N * 2; j++) {
156+
B[i][j] = make_bf16(2.0f * i + 3.0f * j);
157+
Bref[i][j] = make_bf16(2.0f * i + 3.0f * j);
158+
}
159+
}
160+
for (int i = 0; i < MATRIX_M; i++) {
161+
for (int j = 0; j < MATRIX_N; j++) {
162+
C[i][j] = 1.0;
163+
D[i][j] = 1.0;
164+
}
165+
}
166+
167+
big_matrix<float, MATRIX_M, MATRIX_N> MC((float *)&C);
168+
big_matrix<float, MATRIX_M, MATRIX_N> MD((float *)&D);
169+
big_matrix<bfloat16, MATRIX_M, MATRIX_K> MA((bfloat16 *)&A);
170+
big_matrix<bfloat16, MATRIX_K / 2, MATRIX_N * 2> MB((bfloat16 *)&B);
171+
matrix_multiply(MC, MA, MB);
172+
matrix_multiply_ref((int32_t *)Aref, (int32_t *)Bref, (int32_t *)D, MATRIX_M,
173+
MATRIX_N, MATRIX_K / 2);
174+
175+
bool res = true;
176+
for (int i = 0; i < MATRIX_M; i++) {
177+
for (int j = 0; j < MATRIX_N; j++) {
178+
if ((fabs(C[i][j]) - fabs(D[i][j])) > BF16_EPSILON)
179+
res = false;
180+
}
181+
}
182+
std::cout << (res ? "passed" : "failed") << std::endl;
183+
return !res;
184+
}
185+
>>>>>>> 45daa9b4d ([SYCL] Fix result check in joint matrix tests (#1432))

SYCL/Matrix/joint_matrix_bfloat16_colmajorA_colmajorB.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// RUN: %clangxx -fsycl %s -o %t.out
1111
// RUN: %CPU_RUN_PLACEHOLDER %t.out
1212
// RUN: %GPU_RUN_PLACEHOLDER %t.out
13-
// CHECK: passed
1413

1514
// This tests support of col major layout for matrix B which does transpose and
1615
// then VNNI transform. This is currently only available on AMX

SYCL/Matrix/joint_matrix_bfloat16_colmajorA_colmajorB_impl.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ int main() {
136136
res = false;
137137
}
138138
}
139-
if (res)
140-
std::cout << "passed\n";
141-
else
142-
std::cout << "failed\n";
139+
std::cout << (res ? "passed" : "failed") << std::endl;
140+
return !res;
143141
}

SYCL/Matrix/joint_matrix_bfloat16_rowmajorA_rowmajorB.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// RUN: %clangxx -fsycl %s -o %t.out
1111
// RUN: %CPU_RUN_PLACEHOLDER %t.out
1212
// RUN: %GPU_RUN_PLACEHOLDER %t.out
13-
// CHECK: passed
1413

1514
// This tests support of row major layout for matrix B which does automatic VNNI
1615
// transform. This is currently only available on AMX

SYCL/Matrix/joint_matrix_bfloat16_rowmajorA_rowmajorB_impl.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ int main() {
136136
res = false;
137137
}
138138
}
139-
if (res)
140-
std::cout << "passed\n";
141-
else
142-
std::cout << "failed\n";
139+
std::cout << (res ? "passed" : "failed") << std::endl;
140+
return !res;
143141
}

SYCL/Matrix/joint_matrix_int8_colmajorA_colmajorB.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// RUN: %clangxx -fsycl %s -o %t.out
1111
// RUN: %CPU_RUN_PLACEHOLDER %t.out
1212
// RUN: %GPU_RUN_PLACEHOLDER %t.out
13-
// CHECK: passed
1413

1514
// This tests support of col major layout for matrix B which does transpose and
1615
// then VNNI transform. This is currently only available on AMX

SYCL/Matrix/joint_matrix_int8_colmajorA_colmajorB_impl.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ int main() {
133133
res = false;
134134
}
135135
}
136-
if (res)
137-
std::cout << "passed\n";
138-
else
139-
std::cout << "failed\n";
136+
std::cout << (res ? "passed" : "failed") << std::endl;
137+
return !res;
140138
}

SYCL/Matrix/joint_matrix_query_default.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ int main() {
159159
res = false;
160160
}
161161
}
162-
if (res)
163-
std::cout << "passed\n";
164-
else
165-
std::cout << "failed\n";
162+
std::cout << (res ? "passed" : "failed") << std::endl;
163+
return !res;
166164
}

0 commit comments

Comments
 (0)