Skip to content

Distribute and run LLMs with a single file.

Read and write column-major matrices better (#164)

Gauthamf6ee33c24d0e
diff --git a/llamafile/tinyblas.cu b/llamafile/tinyblas.cu
index f6c85a47..208ea028 100644
--- a/llamafile/tinyblas.cu
+++ b/llamafile/tinyblas.cu
@@ -23,15 +23,16 @@
#define BM 64
#define BN 32
-#define BK BM
+#define BK 64
#define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
static __device__ void matmul32_block2d(int m, int n, int k, int x, int y,
const float *A, int lda, float *As,
const float *B, int ldb, float *Bs,
void *C, int ldc, float *Cs) {
- assert(blockDim.x == BM);
- static_assert(BK <= BM);
+ assert(blockDim.x == BK);
+ static_assert(BK == BM);
+ static_assert(BN <= BM);
const int i = threadIdx.x;
int j, l, blob;
// within each block
@@ -39,33 +40,28 @@ static __device__ void matmul32_block2d(int m, int n, int k, int x, int y,
for (j = 0; j < BN; ++j) Cs[j] = 0;
for (blob = 0; blob < k; blob += BK) {
- // we copy into As from A
- if (i < BM) {
- if ((x + i) < m) {
- for (j = 0; j < BK && blob + j < k; ++j) {
- As[(i * BK) + j] =
- READ(A, CUBLAS_OP_T, lda, x + i, blob + j);
- }
- for (; j < BK; ++j) As[(i * BK) + j] = 0;
- } else { // UNLIKELY
- for (j = 0; j < BK; ++j) As[(i * BK) + j] = 0;
- }
- }
-
- // we copy into Bs from B
if (i < BK) {
if ((blob + i) < k) {
+ // we copy into As from A
+ for (j = 0; j < BM && x + j < m; ++j) {
+ As[(j * BK) + i] =
+ READ(A, CUBLAS_OP_T, lda, x + j, blob + i);
+ }
+ for (; j < BM; ++j) As[(j * BK) + i] = 0;
+ // we copy into Bs from B
for (j = 0; j < BN && y + j < n; ++j) {
Bs[(i * BN) + j] =
READ(B, CUBLAS_OP_N, ldb, blob + i, y + j);
}
for (; j < BN; ++j) Bs[(i * BN) + j] = 0;
} else { // UNLIKELY
+ for (j = 0; j < BM; ++j) As[(j * BK) + i] = 0;
for (j = 0; j < BN; ++j) Bs[(i * BN) + j] = 0;
}
}
__syncthreads();
+
// We matmul the blobs, basically Cs += matmul(As, Bs)
for (j = 0; j < BN; ++j) {
for (l = 0; l < BK; ++l) {
@@ -75,10 +71,14 @@ static __device__ void matmul32_block2d(int m, int n, int k, int x, int y,
__syncthreads();
}
+ for (j = 0; j < BN; ++j) {
+ As[(i*BN) + j] = Cs[j];
+ }
+
// We write Cs out into C
- if (x + i < m) {
- for (j = 0; j < BN && y + j < n; ++j) {
- *((float *)C + (x + i) + (y + j) * ldc) = Cs[j];
+ if (y + i < n && i < BN) {
+ for (j = 0; j < BM && x + j < m; ++j) {
+ *((float *)C + (x + j) + (y + i) * ldc) = As[j*BN + i];
}
}
__syncthreads();
@@ -143,7 +143,7 @@ cublasStatus_t tinyblasSgemm(cudaStream_t stream,
}
dim3 maxblocks(CEIL_DIV(m, BM), CEIL_DIV(n, BN), 1);
- int maxthreads = BM;
+ int maxthreads = BK;
tinyblasS_entry<<<maxblocks, maxthreads,
(sizeof(float) * (BM * BK + BK * BN)), stream>>>(
@@ -156,8 +156,9 @@ static __device__ void matmul_block2d(int m, int n, int k, int x, int y,
const half *B, int ldb, float *Bs,
void *C, cudaDataType_t Ctype, int ldc,
float *Cs) {
- assert(blockDim.x == BM);
- static_assert(BK <= BM);
+ assert(blockDim.x == BK);
+ static_assert(BK == BM);
+ static_assert(BN <= BM);
const int i = threadIdx.x;
int j, l, blob;
// within each block
@@ -165,28 +166,22 @@ static __device__ void matmul_block2d(int m, int n, int k, int x, int y,
for (j = 0; j < BN; ++j) Cs[j] = 0;
for (blob = 0; blob < k; blob += BK) {
- // we copy into As from A
- if (i < BM) {
- if ((x + i) < m) {
- for (j = 0; j < BK && blob + j < k; ++j) {
- As[(i * BK) + j] =
- READ16(A, CUBLAS_OP_T, lda, x + i, blob + j);
- }
- for (; j < BK; ++j) As[(i * BK) + j] = 0;
- } else { // UNLIKELY
- for (j = 0; j < BK; ++j) As[(i * BK) + j] = 0;
- }
- }
-
- // we copy into Bs from B
if (i < BK) {
if ((blob + i) < k) {
+ // we copy into As from A
+ for (j = 0; j < BM && x + j < m; ++j) {
+ As[(j * BK) + i] =
+ READ16(A, CUBLAS_OP_T, lda, x + j, blob + i);
+ }
+ for (; j < BM; ++j) As[(j * BK) + i] = 0;
+ // we copy into Bs from B
for (j = 0; j < BN && y + j < n; ++j) {
Bs[(i * BN) + j] =
READ16(B, CUBLAS_OP_N, ldb, blob + i, y + j);
}
for (; j < BN; ++j) Bs[(i * BN) + j] = 0;
} else { // UNLIKELY
+ for (j = 0; j < BM; ++j) As[(j * BK) + i] = 0;
for (j = 0; j < BN; ++j) Bs[(i * BN) + j] = 0;
}
}
@@ -201,15 +196,19 @@ static __device__ void matmul_block2d(int m, int n, int k, int x, int y,
__syncthreads();
}
+ for (j = 0; j < BN; ++j) {
+ As[(i*BN) + j] = Cs[j];
+ }
+
// We write Cs out into C
- if (x + i < m) {
+ if (y + i < n && i < BN) {
if (Ctype == CUDA_R_16F) {
- for (j = 0; j < BN && y + j < n; ++j) {
- *((half *)C + (x + i) + (y + j) * ldc) = __float2half(Cs[j]);
+ for (j = 0; j < BM && x + j < m; ++j) {
+ *((half *)C + (x + j) + (y + i) * ldc) = __float2half(As[j*BN + i]);
}
} else {
- for (j = 0; j < BN && y + j < n; ++j) {
- *((float *)C + (x + i) + (y + j) * ldc) = Cs[j];
+ for (j = 0; j < BM && x + j < m; ++j) {
+ *((float *)C + (x + j) + (y + i) * ldc) = As[j*BN + i];
}
}
}
@@ -268,7 +267,7 @@ cublasStatus_t tinyblasGemmEx(cudaStream_t stream,
}
dim3 maxblocks(CEIL_DIV(m, BM), CEIL_DIV(n, BN), 1);
- int maxthreads = BM;
+ int maxthreads = BK;
tinyblasGE_entry<<<maxblocks, maxthreads,
(sizeof(float) * (BM * BK + BK * BN)), stream>>>(
@@ -336,7 +335,7 @@ cublasStatus_t tinyblasGemmBatchedEx(cudaStream_t stream,
}
dim3 maxblocks(CEIL_DIV(m, BM), CEIL_DIV(n, BN), 32);
- int maxthreads = BM;
+ int maxthreads = BK;
tinyblasGBE_entry<<<maxblocks, maxthreads,
(sizeof(float) * (BM * BK + BK * BN)), stream>>>(
@@ -349,17 +348,18 @@ cublasStatus_t tinyblasGemmBatchedEx(cudaStream_t stream,
#undef BM
#undef BN
#undef BK
-#define BM 32
+#define BM 64
#define BN 4
-#define BK 32
+#define BK 64
static __device__ void matmul_block2d_sb(int m, int n, int k, int x, int y,
const half *A, int lda, float *As,
const half *B, int ldb, float *Bs,
void *C, cudaDataType_t Ctype, int ldc,
float *Cs) {
- assert(blockDim.x == BM);
- static_assert(BK <= BM);
+ assert(blockDim.x == BK);
+ static_assert(BK == BM);
+ static_assert(BN <= BM);
const int i = threadIdx.x;
int j, l, blob;
// within each block
@@ -367,28 +367,22 @@ static __device__ void matmul_block2d_sb(int m, int n, int k, int x, int y,
for (j = 0; j < BN; ++j) Cs[j] = 0;
for (blob = 0; blob < k; blob += BK) {
- // we copy into As from A
- if (i < BM) {
- if ((x + i) < m) {
- for (j = 0; j < BK && blob + j < k; ++j) {
- As[(i * BK) + j] =
- READ16(A, CUBLAS_OP_T, lda, x + i, blob + j);
- }
- for (; j < BK; ++j) As[(i * BK) + j] = 0;
- } else { // UNLIKELY
- for (j = 0; j < BK; ++j) As[(i * BK) + j] = 0;
- }
- }
-
- // we copy into Bs from B
if (i < BK) {
if ((blob + i) < k) {
+ // we copy into As from A
+ for (j = 0; j < BM && x + j < m; ++j) {
+ As[(j * BK) + i] =
+ READ16(A, CUBLAS_OP_T, lda, x + j, blob + i);
+ }
+ for (; j < BM; ++j) As[(j * BK) + i] = 0;
+ // we copy into Bs from B
for (j = 0; j < BN && y + j < n; ++j) {
Bs[(i * BN) + j] =
READ16(B, CUBLAS_OP_N, ldb, blob + i, y + j);
}
for (; j < BN; ++j) Bs[(i * BN) + j] = 0;
} else { // UNLIKELY
+ for (j = 0; j < BM; ++j) As[(j * BK) + i] = 0;
for (j = 0; j < BN; ++j) Bs[(i * BN) + j] = 0;
}
}
@@ -403,15 +397,19 @@ static __device__ void matmul_block2d_sb(int m, int n, int k, int x, int y,
__syncthreads();
}
+ for (j = 0; j < BN; ++j) {
+ As[(i*BN) + j] = Cs[j];
+ }
+
// We write Cs out into C
- if (x + i < m) {
+ if (y + i < n && i < BN) {
if (Ctype == CUDA_R_16F) {
- for (j = 0; j < BN && y + j < n; ++j) {
- *((half *)C + (x + i) + (y + j) * ldc) = __float2half(Cs[j]);
+ for (j = 0; j < BM && x + j < m; ++j) {
+ *((half *)C + (x + j) + (y + i) * ldc) = __float2half(As[j*BN + i]);
}
} else {
- for (j = 0; j < BN && y + j < n; ++j) {
- *((float *)C + (x + i) + (y + j) * ldc) = Cs[j];
+ for (j = 0; j < BM && x + j < m; ++j) {
+ *((float *)C + (x + j) + (y + i) * ldc) = As[j*BN + i];
}
}
}
@@ -487,7 +485,7 @@ cublasStatus_t tinyblasGemmStridedBatchedEx(cudaStream_t stream,
// call the entry function
dim3 maxblocks(CEIL_DIV(m, BM), CEIL_DIV(n, BN), 32);
- int maxthreads = BM;
+ int maxthreads = BK;
tinyblasGSBE_entry<<<maxblocks, maxthreads,
(sizeof(float) * (BM * BK + BK * BN)), stream>>>(