22. General Matrix Multiplication (GEMM) medium
Implement a basic General Matrix Multiplication (GEMM). Given matrix of dimensions , matrix of dimensions , input/output matrix of dimensions , and scalar multipliers and , compute the operation:
The input matrices , , and the initial state of contain 16-bit floating-point numbers (FP16/half). All matrices are stored in row-major order. The scalars and are 32-bit floats.
Implementation Requirements
- Use only native features (external libraries other than WMMA are not permitted).
- The
solvefunction signature must remain unchanged. - Accumulation during multiplication should use FP32 for better precision before converting the final result to FP16.
- The final result must be stored back into matrix
Cashalf.
Example:
Input:
(Note: Input matrices A, B, C_initial are FP16 type for the problem)
Matrix ():
Matrix ():
Matrix ():
Output (FP16):
Matrix ():
Constraints
- 16 ≤
M,N,K≤ 4096 - Performance is measured with
K= 1,024,M= 1,024,N= 1,024
Starter code (CUDA)
#include <cuda_fp16.h>
#include <cuda_runtime.h>
// A, B, and C are device pointers
extern "C" void solve(const half* A, const half* B, half* C, int M, int N, int K, float alpha,
float beta) {}
From LeetGPU, CC BY-NC-ND 4.0.