gemm.h file
Packed dense GEMM (C = α op(A) op(B) + β C), batch helpers, and vm_.
Not a BLAS.
Typedefs
- using vm_gemm_ukernel_fn = void(*)(vm_float_t*acc, const vm_float_t*Ap, const vm_float_t*Bp, int K)
- Micro-kernel for one packed tile:
acc[i + j*mr] += Ap[k*mr+i] * Bp[k*nr+j].
Functions
-
void vm_gemm(vm_
float_ t* C, int ldc, const vm_ float_ t* A, int lda, const vm_ float_ t* B, int ldb, int M, int N, int K, vm_ float_ t alpha, vm_ float_ t beta, bool transA, bool transB, vm_ layout_ t layout) - C = alpha * op(A) * op(B) + beta * C for dense float panels.
-
void vm_gemm_ref(vm_
float_ t* C, int ldc, const vm_ float_ t* A, int lda, const vm_ float_ t* B, int ldb, int M, int N, int K, vm_ float_ t alpha, vm_ float_ t beta, bool transA, bool transB, vm_ layout_ t layout) - Triple-loop reference GEMM.
-
void vm_gemm_batch(vm_
float_ t*const* C, int ldc, const vm_ float_ t*const* A, int lda, const vm_ float_ t*const* B, int ldb, int M, int N, int K, vm_ float_ t alpha, vm_ float_ t beta, bool transA, bool transB, vm_ layout_ t layout, int batch) - Batched GEMM:
batchindependent GEMMs with shared shape. -
void vm_gemm_strided_batch(vm_
float_ t* C, int ldc, int strideC, const vm_ float_ t* A, int lda, int strideA, const vm_ float_ t* B, int ldb, int strideB, int M, int N, int K, vm_ float_ t alpha, vm_ float_ t beta, bool transA, bool transB, vm_ layout_ t layout, int batch) - Strided batched GEMM: problems live
strideXelements apart. -
void vm_gemm_ex(vm_
float_ t* C, int ldc, const vm_ float_ t* A, int lda, const vm_ float_ t* B, int ldb, int M, int N, int K, vm_ float_ t alpha, vm_ float_ t beta, bool transA, bool transB, vm_ layout_ t layout, int op, const vm_ float_ t* bias) - C = alpha * op(A) * op(B) + beta * C with optional fused epilogue.
- auto vm_gemm_threads(void) -> int
- Resolve the GEMM worker-thread budget.
- void vm_gemm_set_threads(int n)
- Cap or force the GEMM worker-thread budget.
-
auto vm_gemm_tile_register(int mr,
int nr,
vm_
gemm_ ukernel_ fn ukernel) -> int - Register an extra packed GEMM tile.
- auto vm_gemm_tile_select(int mr, int nr) -> int
- Choose the packed tile for later
vm_calls in this process.gemm - void vm_gemm_tile_shape(int* mr, int* nr)
- Write the tile
vm_will use now.gemm -
void vm_im2col(vm_
float_ t* col, int ld_col, const vm_ float_ t* img, int n, int c, int h, int w, int kh, int kw, int pad_h, int pad_w, int stride_h, int stride_w, vm_ layout_ t layout) - NCHW im2col into a GEMM-ready panel.
Typedef documentation
typedef void(*vm_gemm_ukernel_fn)(vm_float_t*acc, const vm_float_t*Ap, const vm_float_t*Bp, int K)
Micro-kernel for one packed tile: acc[i + j*mr] += Ap[k*mr+i] * Bp[k*nr+j].
mr and nr are the tile the kernel was registered with. They are not arguments: the packer pads to that shape before the call.
Function documentation
void vm_gemm(vm_ float_ t* C,
int ldc,
const vm_ float_ t* A,
int lda,
const vm_ float_ t* B,
int ldb,
int M,
int N,
int K,
vm_ float_ t alpha,
vm_ float_ t beta,
bool transA,
bool transB,
vm_ layout_ t layout)
C = alpha * op(A) * op(B) + beta * C for dense float panels.
| Parameters | |
|---|---|
| C | Output matrix (M×N), updated in place. |
| ldc | Leading dimension of C. |
| A | Left input matrix; ignored when alpha == 0 or K <= 0. |
| lda | Leading dimension of A. |
| B | Right input matrix; ignored when alpha == 0 or K <= 0. |
| ldb | Leading dimension of B. |
| M | Number of rows of op(A) and C. |
| N | Number of columns of op(B) and C. |
| K | Inner product length. |
| alpha | Scale factor for the A*B product. |
| beta | Scale factor for the existing C values. |
| transA | If true, use A^T; otherwise A. |
| transB | If true, use B^T; otherwise B. |
| layout | Memory layout for A, B, and C. |
Blocked packed kernel with thread-local A/B workspaces. Equivalent to vm_ with op == VM_.
void vm_gemm_ref(vm_ float_ t* C,
int ldc,
const vm_ float_ t* A,
int lda,
const vm_ float_ t* B,
int ldb,
int M,
int N,
int K,
vm_ float_ t alpha,
vm_ float_ t beta,
bool transA,
bool transB,
vm_ layout_ t layout)
Triple-loop reference GEMM.
| Parameters | |
|---|---|
| C | Output matrix (M×N), updated in place. |
| ldc | Leading dimension of C. |
| A | Left input matrix; ignored when alpha == 0 or K <= 0. |
| lda | Leading dimension of A. |
| B | Right input matrix; ignored when alpha == 0 or K <= 0. |
| ldb | Leading dimension of B. |
| M | Number of rows of op(A) and C. |
| N | Number of columns of op(B) and C. |
| K | Inner product length (columns of op(A), rows of op(B)). |
| alpha | Scale factor for the A*B product. |
| beta | Scale factor for the existing C values (0 skips reading C). |
| transA | If true, use A^T; otherwise A. |
| transB | If true, use B^T; otherwise B. |
| layout | Memory layout for A, B, and C. |
Useful for tests and tiny/fallback paths.
Computes C = alpha * op(A) * op(B) + beta * C. op(X) = X or X^T according to the matching transpose flag. Layout selects row-major or column-major indexing for A, B and C together.
void vm_gemm_batch(vm_ float_ t*const* C,
int ldc,
const vm_ float_ t*const* A,
int lda,
const vm_ float_ t*const* B,
int ldb,
int M,
int N,
int K,
vm_ float_ t alpha,
vm_ float_ t beta,
bool transA,
bool transB,
vm_ layout_ t layout,
int batch)
Batched GEMM: batch independent GEMMs with shared shape.
| Parameters | |
|---|---|
| C | Array of batch pointers to output matrices (each M×N). |
| ldc | Leading dimension shared by every C[p]. |
| A | Array of batch pointers to left matrices, or NULL. |
| lda | Leading dimension shared by every A[p]. |
| B | Array of batch pointers to right matrices, or NULL. |
| ldb | Leading dimension shared by every B[p]. |
| M | Number of rows of op(A) and C (same for all problems). |
| N | Number of columns of op(B) and C (same for all problems). |
| K | Inner product length (same for all problems). |
| alpha | Scale factor for each A*B product. |
| beta | Scale factor for each existing C[p]. |
| transA | If true, use A^T for every problem; otherwise A. |
| transB | If true, use B^T for every problem; otherwise B. |
| layout | Memory layout for all A, B, and C panels. |
| batch | Number of independent GEMM problems. |
A[p], B[p], C[p] are the p-th problem. If every B[p] aliases the same buffer, uses a shared-B pack path; otherwise runs per-item vm_. The batch range is split across worker threads when the work is large enough.
void vm_gemm_strided_batch(vm_ float_ t* C,
int ldc,
int strideC,
const vm_ float_ t* A,
int lda,
int strideA,
const vm_ float_ t* B,
int ldb,
int strideB,
int M,
int N,
int K,
vm_ float_ t alpha,
vm_ float_ t beta,
bool transA,
bool transB,
vm_ layout_ t layout,
int batch)
Strided batched GEMM: problems live strideX elements apart.
| Parameters | |
|---|---|
| C | Base pointer for output matrices; problem p at C + p*strideC. |
| ldc | Leading dimension shared by every C panel. |
| strideC | Element stride between consecutive C problems. |
| A | Base pointer for left matrices, or NULL; problem p at A + p*strideA. |
| lda | Leading dimension shared by every A panel. |
| strideA | Element stride between consecutive A problems. |
| B | Base pointer for right matrices, or NULL; problem p at B + p*strideB. |
| ldb | Leading dimension shared by every B panel. |
| strideB | Element stride between B problems (0 means one shared B). |
| M | Number of rows of op(A) and C (same for all problems). |
| N | Number of columns of op(B) and C (same for all problems). |
| K | Inner product length (same for all problems). |
| alpha | Scale factor for each A*B product. |
| beta | Scale factor for each existing C panel. |
| transA | If true, use A^T for every problem; otherwise A. |
| transB | If true, use B^T for every problem; otherwise B. |
| layout | Memory layout for all A, B, and C panels. |
| batch | Number of independent GEMM problems. |
Problem p uses A + p * strideA, B + p * strideB, C + p * strideC. When strideB == 0 (shared B), reuses the shared-B batch path.
void vm_gemm_ex(vm_ float_ t* C,
int ldc,
const vm_ float_ t* A,
int lda,
const vm_ float_ t* B,
int ldb,
int M,
int N,
int K,
vm_ float_ t alpha,
vm_ float_ t beta,
bool transA,
bool transB,
vm_ layout_ t layout,
int op,
const vm_ float_ t* bias)
C = alpha * op(A) * op(B) + beta * C with optional fused epilogue.
| Parameters | |
|---|---|
| C | Output matrix (M×N), updated in place. |
| ldc | Leading dimension of C. |
| A | Left input matrix; ignored when alpha == 0 or K <= 0. |
| lda | Leading dimension of A. |
| B | Right input matrix; ignored when alpha == 0 or K <= 0. |
| ldb | Leading dimension of B. |
| M | Number of rows of op(A) and C. |
| N | Number of columns of op(B) and C. |
| K | Inner product length. |
| alpha | Scale factor for the A*B product. |
| beta | Scale factor for the existing C values. |
| transA | If true, use A^T; otherwise A. |
| transB | If true, use B^T; otherwise B. |
| layout | Memory layout for A, B, and C. |
| op | Epilogue flags (VM_GEMM_OP_*). |
| bias | Optional length-N bias vector (may be NULL). |
Same blocked packed path as vm_, plus last-K bias add and/or ReLU when requested via op.
int vm_gemm_threads(void)
Resolve the GEMM worker-thread budget.
| Returns | Requested thread count (>= 1 from hardware fallback when unset). |
|---|
Order: vm_ limit if > 0; else a positive integer from VECMAT_GEMM_THREADS (invalid values ignored); else online CPU count.
void vm_gemm_set_threads(int n)
Cap or force the GEMM worker-thread budget.
n > 0 sets a fixed limit used by vm_; n == 0 (or negative, treated as 0) restores auto selection via env / hardware.
int vm_gemm_tile_register(int mr,
int nr,
vm_ gemm_ ukernel_ fn ukernel)
Register an extra packed GEMM tile.
| Parameters | |
|---|---|
| mr | Tile rows, in [1, 16]. |
| nr | Tile columns, in [1, 16]. |
| ukernel | Micro-kernel for this mr×nr packing. |
| Returns | 0, or -1 when the arguments are unusable or the table is full. |
The built-in 8×8 kernel stays the vm_gemm_ukernel dispatch slot, including a registered backend, so (8, 8) is rejected. The same pair replaces a previous registration. ukernel must outlive the process and must use that pair's packing.
int vm_gemm_tile_select(int mr, int nr)
Choose the packed tile for later vm_ calls in this process.
| Parameters | |
|---|---|
| mr | Tile rows. 0 together with nr == 0 restores the automatic choice. |
| nr | Tile columns. |
| Returns | 0, or -1 when that shape is not available. A failed call leaves the previous choice in place. |
(0, 0) restores the automatic choice. With no backend, AVX-512 selects 16×14 and AVX2 selects 12×8 when that kernel was compiled; otherwise 8×8. (8, 8) forces the dispatched 8×8 kernel. Any other pair must be built in (scalar 4×4, AVX2 12×8, AVX-512 16×14) or registered.
void vm_gemm_tile_shape(int* mr, int* nr)
Write the tile vm_ will use now.
| Parameters | |
|---|---|
| mr | Out: rows of the packed tile. May be NULL. |
| nr | Out: columns of the packed tile. May be NULL. |
void vm_im2col(vm_ float_ t* col,
int ld_col,
const vm_ float_ t* img,
int n,
int c,
int h,
int w,
int kh,
int kw,
int pad_h,
int pad_w,
int stride_h,
int stride_w,
vm_ layout_ t layout)
NCHW im2col into a GEMM-ready panel.
| Parameters | |
|---|---|
| col | Destination matrix for unfolded patches. |
| ld_col | Leading dimension of col under layout. |
| img | Source image tensor in NCHW order. |
| n | |
| c | Channel count (C). |
| h | Input height (H). |
| w | Input width (W). |
| kh | Kernel height. |
| kw | Kernel width. |
| pad_h | Top/bottom padding in pixels. |
| pad_w | Left/right padding in pixels. |
| stride_h | Vertical kernel stride (<= 0 treated as 1). |
| stride_w | Horizontal kernel stride (<= 0 treated as 1). |
| layout | Row-major or column-major storage for col. |
img is N×C×H×W packed as ((n*C+c)*H+y)*W+x. Output has C*kH*kW rows and N*outH*outW columns (zero-filled off-image taps). outH/outW use standard floor division with the given pads and strides; non-positive stride_h / stride_w are treated as 1.