85. LoRA Linear medium
Implement a LoRA (Low-Rank Adaptation) linear layer forward pass. Given an input matrix
x of shape batch × d_in, a base weight matrix W of
shape d_out × d_in, a LoRA down-projection matrix A of shape
rank × d_in, and a LoRA up-projection matrix B of shape
d_out × rank, compute
output = x × WT + lora_scale × (x × AT) × BT.
All tensors are float32.
Implementation Requirements
- Implement the
solvefunction; do not change its signature. - Do not use external libraries beyond those provided.
- Write the result into
output.
Examples
Example 1:
With lora_scale = 0.5:
Constraints
- 1 ≤
batch≤ 1,024 - 1 ≤
d_in,d_out≤ 8,192 - 1 ≤
rank≤ 256;rank< min(d_in,d_out) - All tensors are
float32on GPU. - Performance is measured with
batch= 256,d_in= 4,096,d_out= 4,096,rank= 64
Starter code (CUDA)
#include <cuda_runtime.h>
// x, W, A, B, output are device pointers
extern "C" void solve(const float* x, const float* W, const float* A, const float* B, float* output,
int batch, int d_in, int d_out, int rank, float lora_scale) {}
From LeetGPU, CC BY-NC-ND 4.0.