LeetKernels
ProblemsStart solving
Problems

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.

x B×D_in W D_out×D_in x@Wᵗ B×D_out A rank×D_in x@Aᵗ B×rank B D_out×rank α×(x@Aᵗ)@Bᵗ B×D_out + output B×D_out

Implementation Requirements

  • Implement the solve function; do not change its signature.
  • Do not use external libraries beyond those provided.
  • Write the result into output.

Examples

Example 1:

x=[10​01​−11​2−1​],W=​100​010​001​000​​,A=[10​01​00​00​],B=​100​010​​

With lora_scale = 0.5:

output=xWT+0.5⋅(xAT)BT=[10​01​−11​]+0.5⋅[10​01​][10​01​00​]=[1.50​01.5​−11​]

Constraints

  • 1 ≤ batch ≤ 1,024
  • 1 ≤ d_in, d_out ≤ 8,192
  • 1 ≤ rank ≤ 256; rank < min(d_in, d_out)
  • All tensors are float32 on 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) {}

Solve it on your GPU

Previous: SwiGLU MLP BlockNext: Speculative Decoding Verification

From LeetGPU, CC BY-NC-ND 4.0.