// ======================= runtime LoRA on the attention projections (INK_LORA; src/lora.rs) =======================
// y[row] += scale * B (A x[row]) beside a projection the engine's GEMM already wrote, all in fp32 from fp32 weights.
// Buffers: 2 = A (activation arena: x read, y updated in place), 15 = LW (the adapter: A^T [k][32] and B [nout][32] per
// matrix, ranks past r zero), 16 = LU (rank-space scratch), 5 = args. Unset INK_LORA dispatches neither kernel.
// Every sum runs in one fixed order (k cut into 8 x ksplit slices, ksplit fixed by the host): a row gets the same bits at
// any batch width or place in the batch.
#define LORA_R 32
#define LORA_RT 8
#define LORA_JOBS 4
// x / y offsets are in floats of A; strides are in elements (halves when the side is half).
struct LoraAJob { uint at_off, x_off, x_stride, x_half, k, rows, rows_pad, u_off; };
struct LoraAArgs { uint njobs, ksplit, pad0, pad1; LoraAJob job[LORA_JOBS]; };
// u_p[j][row][0..32) = (A_j x_row) over k-slice p. grid (row tiles, jobs, ksplit), 256 threads: the 8 simdgroups split the
// slice again, lane = rank component; A^T rows are one coalesced 128-byte load, x is one broadcast load per row.
kernel void k_lora_a(device const float * A [[buffer(2)]], device const float * LW [[buffer(15)]],
device float * LU [[buffer(16)]], constant LoraAArgs & a [[buffer(5)]],
uint3 tgp [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]],
ushort lane [[thread_index_in_simdgroup]], ushort sg [[simdgroup_index_in_threadgroup]]) {
threadgroup float red[8][LORA_RT][LORA_R];
const LoraAJob J = a.job[tgp.y];
const uint r0 = tgp.x * LORA_RT;
const uint nr = r0 < J.rows ? min((uint) LORA_RT, J.rows - r0) : 0u;
const uint ks = J.k / (8 * a.ksplit);
const uint k0 = (tgp.z * 8 + sg) * ks;
float acc[LORA_RT];
for (uint i = 0; i < LORA_RT; ++i) acc[i] = 0.0f;
device const float * at = LW + J.at_off + (ulong) k0 * LORA_R + lane;
if (J.x_half) {
device const half * x = (device const half *) (A + J.x_off) + (ulong) r0 * J.x_stride + k0;
for (uint kk = 0; kk < ks; ++kk) {
const float w = at[(ulong) kk * LORA_R];
for (uint i = 0; i < LORA_RT; ++i) if (i < nr) acc[i] = fma(w, float(x[(ulong) i * J.x_stride + kk]), acc[i]);
}
} else {
device const float * x = A + J.x_off + (ulong) r0 * J.x_stride + k0;
for (uint kk = 0; kk < ks; ++kk) {
const float w = at[(ulong) kk * LORA_R];
for (uint i = 0; i < LORA_RT; ++i) if (i < nr) acc[i] = fma(w, x[(ulong) i * J.x_stride + kk], acc[i]);
}
}
for (uint i = 0; i < LORA_RT; ++i) red[sg][i][lane] = acc[i];
threadgroup_barrier(mem_flags::mem_threadgroup);
const uint i = tid / LORA_R, r = tid % LORA_R; // 256 threads = LORA_RT x LORA_R
if (i < nr) {
float s = 0.0f;
for (uint q = 0; q < 8; ++q) s += red[q][i][r];
LU[J.u_off + ((ulong) tgp.z * J.rows_pad + r0 + i) * LORA_R + r] = s;
}
}
struct LoraBJob { uint b_off, u_off, y_off, y_stride, y_half, nout, rows, rows_pad, tile0, pad0, pad1, pad2; };
struct LoraBArgs { uint njobs, ksplit; float scale; uint pad; LoraBJob job[LORA_JOBS]; };
// y[row][o] += scale * (B_j[o] . u_j[row]), u_j the k-split partials added in order. grid (output tiles of 256 over all jobs,
// row tiles), 256 threads: one output column per thread, its B row held in registers across the tile's rows.
kernel void k_lora_b(device float * A [[buffer(2)]], device const float * LW [[buffer(15)]],
device const float * LU [[buffer(16)]], constant LoraBArgs & a [[buffer(5)]],
uint2 tgp [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]]) {
threadgroup float us[LORA_RT * LORA_R];
uint j = 0;
for (uint q = 1; q < a.njobs; ++q) if (tgp.x >= a.job[q].tile0) j = q;
const LoraBJob J = a.job[j];
const uint r0 = tgp.y * LORA_RT;
const uint nr = r0 < J.rows ? min((uint) LORA_RT, J.rows - r0) : 0u;
{
const uint i = tid / LORA_R, r = tid % LORA_R;
float s = 0.0f;
if (i < nr) for (uint p = 0; p < a.ksplit; ++p) s += LU[J.u_off + ((ulong) p * J.rows_pad + r0 + i) * LORA_R + r];
us[tid] = s;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
const uint o = (tgp.x - J.tile0) * 256 + tid;
if (o >= J.nout) return;
device const float4 * b = (device const float4 *) (LW + J.b_off + (ulong) o * LORA_R);
float4 bv[LORA_R / 4];
for (uint c = 0; c < LORA_R / 4; ++c) bv[c] = b[c];
threadgroup const float4 * u4 = (threadgroup const float4 *) us;
for (uint i = 0; i < nr; ++i) {
float s = 0.0f;
for (uint c = 0; c < LORA_R / 4; ++c) s += dot(bv[c], u4[i * (LORA_R / 4) + c]);
s *= a.scale;
const ulong at = (ulong) (r0 + i) * J.y_stride + o;
if (J.y_half) {
device half * y = (device half *) (A + J.y_off);
y[at] = half(float(y[at]) + s);
} else {
A[J.y_off + at] += s;
}
}
}