Skip to content

[SYCL][Joint Matrix] Remove duplicated matrix_multiply_ref in tests #11609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
// XFAIL: gpu

#include "../common.hpp"
#include <iostream>
#include <sycl/sycl.hpp>

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
using bfloat16 = sycl::ext::oneapi::bfloat16;

constexpr size_t SG_SZ = 32;
constexpr size_t TN = 16;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/SG32/joint_matrix_half.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

// XFAIL: gpu

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/SG32/joint_matrix_int8_vnni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

// XFAIL: gpu

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/SG32/joint_matrix_ss_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/SG32/joint_matrix_su_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/SG32/joint_matrix_us_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/SG32/joint_matrix_uu_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
4 changes: 1 addition & 3 deletions sycl/test-e2e/Matrix/XMX8/joint_matrix_bfloat16_32x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

// XFAIL: *

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
using bfloat16 = sycl::ext::oneapi::bfloat16;

#define SG_SZ 8
constexpr size_t TN = 8;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/XMX8/joint_matrix_half.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/XMX8/joint_matrix_int8_vnni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

// XFAIL: *

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/XMX8/joint_matrix_ss_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/XMX8/joint_matrix_su_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/XMX8/joint_matrix_us_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
3 changes: 1 addition & 2 deletions sycl/test-e2e/Matrix/XMX8/joint_matrix_uu_int8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
// RUN: %{build} -o %t.out -DSYCL_EXT_ONEAPI_MATRIX_VERSION=4
// RUN: %{run} %t.out

#include <iostream>
#include <sycl/sycl.hpp>
#include "../common.hpp"

using namespace sycl;
using namespace sycl::ext::oneapi::experimental::matrix;
Expand Down
40 changes: 28 additions & 12 deletions sycl/test-e2e/Matrix/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,38 @@ float make_fp32(bfloat16 x) {
return *res;
}

template <typename Ta, typename Tc>
void matrix_multiply_ref(Ta *A, Ta *B, Tc *C, int M, int N, int K,
bool transpose_c = false) {
template <typename Ta, typename Tb, typename Tc, uint VF = 1>
void matrix_multiply_ref(Ta *A, Tb *B, Tc *C, int M, int N, int K,
bool transpose_c = false, bool colmajor_a = false,
bool colmajor_b = false) {
for (unsigned int m = 0; m < M; m++) {
for (unsigned int n = 0; n < N; n++) {
for (unsigned int k = 0; k < K; k++) {

int a_ind = colmajor_a ? (k * M + m) : m * K + k;
int b_ind = colmajor_b ? (n * K + k) : k * N + n;
int c_ind = transpose_c ? (n * M + m) : m * N + n;
if constexpr (std::is_same_v<Ta, bfloat16> && std::is_same_v<Tc, float>)
C[c_ind] += make_fp32(A[m * K + k]) * make_fp32(B[k * N + n]);
else if constexpr (std::is_same_v<Ta, float> &&
std::is_same_v<Tc, float> ||
std::is_same_v<Ta, int8_t> &&
std::is_same_v<Tc, int32_t>)
C[c_ind] += A[m * K + k] * B[k * N + n];
else
assert(false && "Unsupported type in matrix_multiply_ref.");

Ta *va = (Ta *)(A + a_ind * VF);
Tb *vb = (Tb *)(B + b_ind * VF);
Tc acc = *(C + c_ind);

for (uint i = 0; i < VF; i++) {
if constexpr (std::is_same_v<Ta, bfloat16> &&
std::is_same_v<Tc, float>)
acc += make_fp32(va[i]) * make_fp32(vb[i]);
else if constexpr (std::is_same_v<Ta, float> &&
std::is_same_v<Tc, float> ||
std::is_integral_v<Ta> && std::is_integral_v<Tc>)
acc += va[i] * vb[i];
else if constexpr (std::is_same_v<Ta, sycl::half> &&
std::is_same_v<Tc, float>)
acc += (float)va[i] * (float)vb[i];
else
assert(false && "Unsupported type in matrix_multiply_ref.");
}

*(C + c_ind) = acc;
}
}
}
Expand Down
59 changes: 17 additions & 42 deletions sycl/test-e2e/Matrix/joint_matrix_bfloat16_32x64_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,55 +67,30 @@ void matrix_multiply(big_matrix<T1, M, N> &C, big_matrix<T2, M, K> &A,
}).wait();
}

static constexpr size_t MATRIX_M = TM * 2;
static constexpr size_t MATRIX_N = TN * 2;
static constexpr size_t MATRIX_K = TK * 2;
bfloat16 A[MATRIX_M][MATRIX_K];
bfloat16 B[MATRIX_K / 2][MATRIX_N * 2];
float C[MATRIX_M][MATRIX_N];
float D[MATRIX_M][MATRIX_N];

void matrix_multiply_ref(int *A_mem, int *B_mem, int *C_mem, int M, int N,
int K) {
for (int m = 0; m < M; m++)
for (int n = 0; n < N; n++) {
for (int k = 0; k < K; k++) {
bfloat16 *va = (bfloat16 *)(A_mem + m * K + k);
bfloat16 *vb = (bfloat16 *)(B_mem + k * N + n);
float acc = *((float *)(C_mem + m * N + n));
for (int i = 0; i < 2; i++) {
acc += (make_fp32(va[i]) * make_fp32(vb[i]));
}
*((float *)(C_mem + m * N + n)) = acc;
}
}
}

int main() {
for (int i = 0; i < MATRIX_M; i++) {
for (int j = 0; j < MATRIX_K; j++) {
A[i][j] = bfloat16(1.0f * (i + j));
}
}
for (int i = 0; i < MATRIX_K / 2; i++) {
for (int j = 0; j < MATRIX_N * 2; j++) {
B[i][j] = bfloat16(2.0f * i + 3.0f * j);
}
}
for (int i = 0; i < MATRIX_M; i++) {
for (int j = 0; j < MATRIX_N; j++) {
C[i][j] = 1.0;
D[i][j] = 1.0;
}
}
static constexpr size_t MATRIX_M = TM * 2;
static constexpr size_t MATRIX_N = TN * 2;
static constexpr size_t MATRIX_K = TK * 2;
bfloat16 A[MATRIX_M][MATRIX_K];
bfloat16 B[MATRIX_K / 2][MATRIX_N * 2];
float C[MATRIX_M][MATRIX_N];
float D[MATRIX_M][MATRIX_N];

matrix_fill(MATRIX_M, MATRIX_K, (bfloat16 *)A,
[](int i, int j) { return 1.0f * (i + j); });
matrix_fill(MATRIX_K / 2, MATRIX_N * 2, (bfloat16 *)B,
[](int i, int j) { return 2.0f * i + 3.0f * j; });
matrix_fill(MATRIX_M, MATRIX_N, (float *)C, 1.0f);
matrix_fill(MATRIX_M, MATRIX_N, (float *)D, 1.0f);

big_matrix<float, MATRIX_M, MATRIX_N> MC((float *)&C);
big_matrix<float, MATRIX_M, MATRIX_N> MD((float *)&D);
big_matrix<bfloat16, MATRIX_M, MATRIX_K> MA((bfloat16 *)&A);
big_matrix<bfloat16, MATRIX_K / 2, MATRIX_N * 2> MB((bfloat16 *)&B);
matrix_multiply(MC, MA, MB);
matrix_multiply_ref((int32_t *)A, (int32_t *)B, (int32_t *)D, MATRIX_M,
MATRIX_N, MATRIX_K / 2);
matrix_multiply_ref<bfloat16, bfloat16, float, 2>(
(bfloat16 *)A, (bfloat16 *)B, (float *)D, MATRIX_M, MATRIX_N,
MATRIX_K / 2);

bool res = matrix_compare(MATRIX_M, MATRIX_N, (float *)C, (float *)D);
std::cout << (res ? "passed" : "failed") << std::endl;
Expand Down
64 changes: 19 additions & 45 deletions sycl/test-e2e/Matrix/joint_matrix_bfloat16_array_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,59 +81,33 @@ void matrix_multiply(big_matrix<T1, M, N> &C, big_matrix<T2, M, K> &A,
}).wait();
}

static constexpr size_t MATRIX_M = TM * 2;
static constexpr size_t MATRIX_N = TN * 2;
static constexpr size_t MATRIX_K = TK * 2;

bfloat16 A[MATRIX_M][MATRIX_K];
bfloat16 B[MATRIX_K / 2][MATRIX_N * 2];

float C[MATRIX_M][MATRIX_N];
float D[MATRIX_M][MATRIX_N];

void matrix_multiply_ref(int *A_mem, int *B_mem, int *C_mem, int M, int N,
int K) {
for (int m = 0; m < M; m++)
for (int n = 0; n < N; n++) {
for (int k = 0; k < K; k++) {
// Because B was assumed VNNIed
bfloat16 *va = (bfloat16 *)(A_mem + m * K + k);
bfloat16 *vb = (bfloat16 *)(B_mem + k * N + n);
float acc = *((float *)(C_mem + m * N + n));
for (int i = 0; i < 2; i++) {
acc += (make_fp32(va[i]) * make_fp32(vb[i]));
}
*((float *)(C_mem + m * N + n)) = acc;
}
}
}

int main() {
for (int i = 0; i < MATRIX_M; i++) {
for (int j = 0; j < MATRIX_K; j++) {
A[i][j] = bfloat16(1.0f * (i + j));
}
}
for (int i = 0; i < MATRIX_K / 2; i++) {
for (int j = 0; j < MATRIX_N * 2; j++) {
B[i][j] = bfloat16(2.0f * i + 3.0f * j);
}
}
for (int i = 0; i < MATRIX_M; i++) {
for (int j = 0; j < MATRIX_N; j++) {
C[i][j] = 1.0;
D[i][j] = 1.0;
}
}
static constexpr size_t MATRIX_M = TM * 2;
static constexpr size_t MATRIX_N = TN * 2;
static constexpr size_t MATRIX_K = TK * 2;

bfloat16 A[MATRIX_M][MATRIX_K];
bfloat16 B[MATRIX_K / 2][MATRIX_N * 2];

float C[MATRIX_M][MATRIX_N];
float D[MATRIX_M][MATRIX_N];

matrix_fill(MATRIX_M, MATRIX_K, (bfloat16 *)A,
[](int i, int j) { return 1.0f * (i + j); });
matrix_fill(MATRIX_K / 2, MATRIX_N * 2, (bfloat16 *)B,
[](int i, int j) { return 2.0f * i + 3.0f * j; });
matrix_fill(MATRIX_M, MATRIX_N, (float *)C, 1.0f);
matrix_fill(MATRIX_M, MATRIX_N, (float *)D, 1.0f);

big_matrix<float, MATRIX_M, MATRIX_N> MC((float *)&C);
big_matrix<float, MATRIX_M, MATRIX_N> MD((float *)&D);
big_matrix<bfloat16, MATRIX_M, MATRIX_K> MA((bfloat16 *)&A);
big_matrix<bfloat16, MATRIX_K / 2, MATRIX_N * 2> MB((bfloat16 *)&B);

matrix_multiply(MC, MA, MB);
matrix_multiply_ref((int32_t *)A, (int32_t *)B, (int32_t *)D, MATRIX_M,
MATRIX_N, MATRIX_K / 2);
matrix_multiply_ref<bfloat16, bfloat16, float, 2>(
(bfloat16 *)A, (bfloat16 *)B, (float *)D, MATRIX_M, MATRIX_N,
MATRIX_K / 2);

bool res = matrix_compare(MATRIX_M, MATRIX_N, (float *)C, (float *)D);
std::cout << (res ? "passed" : "failed") << std::endl;
Expand Down
Loading