use std::f32::consts::LOG2_E;
use eyre::{Result, bail, ensure};
use crate::metal::gpu::{Arg, DType, Gpu, Tensor, groups};
use crate::precision::Precision;
use crate::weights::Weights;
const GEMM_THREADS: usize = 128;
const GEMM_TILE: usize = 64;
const ROW_THREADS: usize = 256;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub enum Act {
None = 0,
Relu = 1,
Gelu = 2,
GeluTanh = 3,
Sigmoid = 4,
}
impl From<Act> for Arg<'_> {
fn from(act: Act) -> Self {
Arg::I32(act as i32)
}
}
pub fn weight_dtype(gpu: &Gpu) -> DType {
match gpu.precision() {
Precision::Bf16 => DType::Bf16,
Precision::Fp32 | Precision::Tf32 => DType::F32,
}
}
pub fn convert(gpu: &Gpu, x: Tensor, dtype: DType) -> Result<Tensor> {
let name = match (x.dtype(), dtype) {
(a, b) if a == b => return Ok(x),
(DType::F32, DType::Bf16) => "f32_to_bf16",
(DType::Bf16, DType::F32) => "bf16_to_f32",
(a, b) => bail!("no conversion from {a:?} to {b:?}"),
};
let out = gpu.empty(x.shape(), dtype)?;
gpu.dispatch_1d(name, out.len(), &[out.arg(), x.arg()])?;
Ok(out)
}
pub fn add_assign(gpu: &Gpu, x: &Tensor, y: &Tensor) -> Result<()> {
ensure!(x.len() == y.len(), "add_assign of {:?} and {:?}", x.shape(), y.shape());
let name = match y.dtype() {
DType::Bf16 => "add_assign_bf16",
_ => "add_assign",
};
gpu.dispatch_1d(name, x.len(), &[x.arg(), y.arg()])
}
pub fn bias_act(gpu: &Gpu, x: &Tensor, bias: Option<&Tensor>, act: Act) -> Result<()> {
let b = bias.map_or_else(|| x.buf.arg(), Tensor::arg);
let args = [x.arg(), b, x.cols().into(), u32::from(bias.is_some()).into(), act.into()];
gpu.dispatch_1d("bias_act", x.len(), &args)
}
fn gemm_kernel(prefix: &'static str, ti: DType, to: DType) -> Result<&'static str> {
Ok(match (prefix, ti, to) {
("linear", DType::F32, DType::F32) => "linear_f32_f32",
("linear", DType::Bf16, DType::Bf16) => "linear_bf16_bf16",
("linear", DType::Bf16, DType::F32) => "linear_bf16_f32",
("convt2x2", DType::F32, DType::F32) => "convt2x2_f32_f32",
("convt2x2", DType::Bf16, DType::Bf16) => "convt2x2_bf16_bf16",
("conv3x3", DType::F32, DType::F32) => "conv3x3_f32_f32",
("conv3x3", DType::Bf16, DType::F32) => "conv3x3_bf16_f32",
_ => bail!("no {prefix} kernel from {ti:?} to {to:?}"),
})
}
pub struct Linear {
w: Tensor,
b: Option<Tensor>,
}
impl Linear {
pub fn new(w: Tensor, b: Option<Tensor>) -> Self {
Self { w, b }
}
pub fn fuse_shifted(gpu: &Gpu, parts: &[(&Linear, bool)], c: &Tensor) -> Result<Self> {
ensure!(!parts.is_empty(), "nothing to fuse");
let (k, r) = (parts[0].0.in_features(), c.rows());
ensure!(c.cols() == k && c.dtype() == DType::F32, "shift {:?} for inputs of {k}", c.shape());
let n: usize = parts.iter().map(|(l, _)| l.out_features()).sum();
let w = gpu.empty(&[n, k], DType::F32)?;
let mut bias = vec![0f32; r * n];
let mut col = 0;
for (lin, shifted) in parts {
let o = lin.out_features();
ensure!(lin.in_features() == k && lin.w.dtype() == DType::F32, "fusing linear {:?}", lin.w.shape());
ensure!(lin.bias_rows()? <= 1, "fusing a linear whose bias is already a matrix");
gpu.dispatch_1d("copy_f32", lin.w.len(), &[w.at(col * k), lin.w.arg()])?;
let rows = if *shifted {
gpu.download(&lin.forward(gpu, c, Act::None)?)?
} else {
let b = lin.b.as_ref().map_or_else(|| Ok(vec![0f32; o]), |b| gpu.download(b))?;
b.repeat(r)
};
for (i, row) in rows.chunks_exact(o).enumerate() {
bias[i * n + col..][..o].copy_from_slice(row);
}
col += o;
}
Ok(Self::new(w, Some(gpu.upload_f32(&bias, &[r, n])?)))
}
fn bias_rows(&self) -> Result<usize> {
let n = self.out_features();
let Some(b) = &self.b else { return Ok(0) };
ensure!(b.len().is_multiple_of(n) && b.len() > 0, "bias {:?} for {n} outputs", b.shape());
Ok(b.len() / n)
}
pub fn load(gpu: &Gpu, wts: &Weights, prefix: &str, dtype: DType) -> Result<Self> {
let w = wts.get_as(gpu, &format!("{prefix}.weight"), dtype)?;
let (o, len) = (w.shape()[0], w.len());
let w = w.reshape(&[o, len / o]);
let bias = format!("{prefix}.bias");
let b = if wts.contains(&bias) { Some(wts.get(gpu, &bias)?) } else { None };
Ok(Self::new(w, b))
}
pub fn load_concat(gpu: &Gpu, wts: &Weights, prefixes: &[String], dtype: DType) -> Result<Self> {
let names = |s: &str| prefixes.iter().map(|p| format!("{p}.{s}")).collect::<Vec<_>>();
let w = wts.concat_as(gpu, &names("weight"), dtype)?;
Ok(Self::new(w, Some(wts.concat_as(gpu, &names("bias"), DType::F32)?)))
}
pub fn out_features(&self) -> usize {
self.w.rows()
}
pub fn in_features(&self) -> usize {
self.w.cols()
}
pub fn forward_to(&self, gpu: &Gpu, x: &Tensor, act: Act, out: DType) -> Result<Tensor> {
let y = gpu.empty(&[x.rows(), self.out_features()], out)?;
self.run(gpu, x, &y, act, false)?;
Ok(y)
}
pub fn forward(&self, gpu: &Gpu, x: &Tensor, act: Act) -> Result<Tensor> {
self.forward_to(gpu, x, act, self.w.dtype())
}
pub fn forward_residual(&self, gpu: &Gpu, x: &Tensor, y: &Tensor) -> Result<()> {
ensure!(
y.shape() == [x.rows(), self.out_features()],
"residual {:?} for linear {:?}",
y.shape(),
self.w.shape()
);
self.run(gpu, x, y, Act::None, true)
}
fn run(&self, gpu: &Gpu, x: &Tensor, y: &Tensor, act: Act, accumulate: bool) -> Result<()> {
let (m, n, k) = (x.rows(), self.out_features(), self.in_features());
ensure!(x.cols() == k, "linear input {:?} vs weight {:?}", x.shape(), self.w.shape());
ensure!(x.dtype() == self.w.dtype(), "linear input {:?} vs weights {:?}", x.dtype(), self.w.dtype());
let b = self.b.as_ref().map_or_else(|| self.w.buf.arg(), Tensor::arg);
let bias_rows = self.bias_rows()?;
let tiles = groups(n, GEMM_TILE) * groups(m, GEMM_TILE);
let (splits, kc) = k_splits(tiles, k);
if splits > 1 {
let part = gpu.empty(&[splits * m, n], DType::F32)?;
let gemm = match x.dtype() {
DType::Bf16 => "linear_splitk_bf16",
_ => "linear_splitk_f32",
};
let args = [x.arg(), self.w.arg(), part.arg(), m.into(), n.into(), k.into(), kc.into()];
gpu.dispatch(gemm, [groups(n, GEMM_TILE), groups(m, GEMM_TILE), splits], [GEMM_THREADS, 1, 1], &args)?;
let reduce = match y.dtype() {
DType::Bf16 => "linear_splitk_reduce_bf16",
_ => "linear_splitk_reduce_f32",
};
let args = [
y.arg(),
part.arg(),
b,
m.into(),
n.into(),
n.into(),
splits.into(),
bias_rows.into(),
act.into(),
u32::from(accumulate).into(),
];
return gpu.dispatch_1d(reduce, m * n, &args);
}
let name = gemm_kernel("linear", x.dtype(), y.dtype())?;
let args = [
x.arg(),
self.w.arg(),
b,
y.arg(),
m.into(),
n.into(),
k.into(),
k.into(),
n.into(),
bias_rows.into(),
act.into(),
u32::from(accumulate).into(),
];
gpu.dispatch(name, [groups(n, GEMM_TILE), groups(m, GEMM_TILE), 1], [GEMM_THREADS, 1, 1], &args)
}
}
const SPLIT_K_MIN: usize = 256;
fn k_splits(tiles: usize, k: usize) -> (usize, usize) {
if tiles >= KV_TARGET_GROUPS || k < 2 * SPLIT_K_MIN {
return (1, k);
}
let want = KV_TARGET_GROUPS.div_ceil(tiles).min(k / SPLIT_K_MIN);
let chunk = k.div_ceil(want).next_multiple_of(32);
(k.div_ceil(chunk), chunk)
}
pub struct LayerNorm {
w: Tensor,
b: Tensor,
eps: f32,
}
impl LayerNorm {
pub fn load(gpu: &Gpu, wts: &Weights, prefix: &str, eps: f32) -> Result<Self> {
Ok(Self { w: wts.get(gpu, &format!("{prefix}.weight"))?, b: wts.get(gpu, &format!("{prefix}.bias"))?, eps })
}
fn rows(&self, gpu: &Gpu, name: &'static str, args: &[Arg], rows: usize) -> Result<()> {
gpu.dispatch(name, [rows, 1, 1], [ROW_THREADS, 1, 1], args)
}
pub fn forward_to(&self, gpu: &Gpu, x: &Tensor, out: DType) -> Result<Tensor> {
let y = gpu.empty(x.shape(), out)?;
let name = match out {
DType::Bf16 => "layernorm_bf16",
_ => "layernorm",
};
let args = [y.arg(), x.arg(), self.w.arg(), self.b.arg(), x.cols().into(), self.eps.into()];
self.rows(gpu, name, &args, x.rows())?;
Ok(y)
}
pub fn forward_inplace(&self, gpu: &Gpu, x: &Tensor) -> Result<()> {
let args = [x.arg(), self.w.arg(), self.b.arg(), x.cols().into(), self.eps.into()];
self.rows(gpu, "layernorm_inplace", &args, x.rows())
}
pub fn forward_act_narrow(&self, gpu: &Gpu, x: &Tensor, act: Act) -> Result<()> {
ensure!(x.cols() <= 256 && x.dtype() == DType::F32, "narrow LayerNorm of {:?}", x.shape());
let args = [x.arg(), self.w.arg(), self.b.arg(), x.rows().into(), x.cols().into(), self.eps.into(), act.into()];
gpu.dispatch("layernorm_act_simd", [groups(x.rows(), 8), 1, 1], [256, 1, 1], &args)
}
pub fn add_forward(&self, gpu: &Gpu, x: &Tensor, add: &Tensor) -> Result<Tensor> {
ensure!(x.shape() == add.shape(), "add_layernorm of {:?} and {:?}", x.shape(), add.shape());
let y = gpu.empty(x.shape(), add.dtype())?;
let name = match add.dtype() {
DType::Bf16 => "add_layernorm_bf16",
_ => "add_layernorm_f32",
};
let args = [y.arg(), x.arg(), add.arg(), self.w.arg(), self.b.arg(), x.cols().into(), self.eps.into()];
self.rows(gpu, name, &args, x.rows())?;
Ok(y)
}
}
pub struct ConvT2x2 {
w: Tensor,
b: Tensor,
}
impl ConvT2x2 {
pub fn load(gpu: &Gpu, wts: &Weights, prefix: &str, dtype: DType) -> Result<Self> {
let (w, shape) = wts.host(&format!("{prefix}.weight"))?;
let c = shape[0];
let n = w.len() / c;
let o = n / 4;
let mut t = vec![0f32; w.len()];
for ci in 0..c {
for oc in 0..o {
for tap in 0..4 {
t[(tap * o + oc) * c + ci] = w[ci * n + oc * 4 + tap];
}
}
}
Ok(Self { w: gpu.upload_as(&t, &[n, c], dtype)?, b: wts.get(gpu, &format!("{prefix}.bias"))? })
}
pub fn forward(&self, gpu: &Gpu, x: &Tensor, h: usize, w: usize, act: Act) -> Result<Tensor> {
self.forward_add(gpu, x, h, w, None, act)
}
pub fn forward_add(
&self,
gpu: &Gpu,
x: &Tensor,
h: usize,
w: usize,
add: Option<&Tensor>,
act: Act,
) -> Result<Tensor> {
let (n, c) = (self.w.rows(), self.w.cols());
ensure!(x.shape() == [h * w, c], "convT input {:?} for {h}x{w}x{c}", x.shape());
let dtype = self.w.dtype();
let y = gpu.empty(&[4 * h * w, n / 4], dtype)?;
if let Some(a) = add {
ensure!(
a.dtype() == DType::F32 && a.cols() == n / 4 && y.len().is_multiple_of(a.len()),
"convT addend {:?} for output {:?}",
a.shape(),
y.shape()
);
}
let m = h * w;
let args = [
x.arg(),
self.w.arg(),
self.b.arg(),
y.arg(),
m.into(),
n.into(),
c.into(),
w.into(),
act.into(),
add.map_or_else(|| self.b.arg(), Tensor::arg),
add.map_or(0, Tensor::len).into(),
];
let name = gemm_kernel("convt2x2", dtype, dtype)?;
gpu.dispatch(name, [groups(n, GEMM_TILE), groups(m, GEMM_TILE), 1], [GEMM_THREADS, 1, 1], &args)?;
Ok(y)
}
}
pub struct Conv3x3 {
taps: Tensor,
b: Tensor,
}
impl Conv3x3 {
pub fn load(gpu: &Gpu, wts: &Weights, prefix: &str, dtype: DType) -> Result<Self> {
let (w, shape) = wts.host(&format!("{prefix}.weight"))?;
let (o, c) = (shape[0], shape[1]);
ensure!(shape[2..] == [3, 3], "{prefix}: a 3x3 conv has weights {shape:?}");
let mut taps = vec![0f32; w.len()];
for oc in 0..o {
for ic in 0..c {
for t in 0..9 {
taps[(t * o + oc) * c + ic] = w[(oc * c + ic) * 9 + t];
}
}
}
Ok(Self { taps: gpu.upload_as(&taps, &[9, o, c], dtype)?, b: wts.get(gpu, &format!("{prefix}.bias"))? })
}
pub fn forward(&self, gpu: &Gpu, x: &Tensor, h: usize, w: usize) -> Result<Tensor> {
let (o, c) = (self.taps.shape()[1], self.taps.shape()[2]);
ensure!(x.shape() == [h * w, c], "conv3x3 input {:?} for {h}x{w}x{c}", x.shape());
let ti = self.taps.dtype();
let xp = gpu.empty(&[(h + 2) * (w + 2) + 2, c], ti)?;
let pad = if ti == DType::Bf16 { "pad1_f32_bf16" } else { "pad1_f32_f32" };
gpu.dispatch_1d(pad, xp.len(), &[xp.arg(), x.arg(), h.into(), w.into(), c.into()])?;
let y = gpu.empty(&[h * w, o], DType::F32)?;
let args = [xp.arg(), self.taps.arg(), self.b.arg(), y.arg(), h.into(), w.into(), c.into(), o.into()];
let rows = h * (w + 2);
let name = gemm_kernel("conv3x3", ti, DType::F32)?;
gpu.dispatch(name, [groups(o, GEMM_TILE), groups(rows, GEMM_TILE), 1], [GEMM_THREADS, 1, 1], &args)?;
Ok(y)
}
}
#[derive(Clone, Copy)]
pub struct View<'a> {
pub t: &'a Tensor,
pub offset: usize,
pub ld: usize,
pub stride: usize,
}
impl<'a> View<'a> {
pub fn rows(t: &'a Tensor) -> Self {
Self { t, offset: 0, ld: t.cols(), stride: 0 }
}
pub fn heads(t: &'a Tensor, col: usize, hd: usize) -> Self {
Self { t, offset: col, ld: t.cols(), stride: hd }
}
pub fn skip(self, rows: usize) -> Self {
Self { offset: self.offset + rows * self.ld, ..self }
}
fn check(&self, batch: usize, rows: usize, width: usize) -> Result<()> {
let end = self.offset + (batch - 1) * self.stride + (rows - 1) * self.ld + width;
ensure!(rows == 0 || end <= self.t.len(), "attention view reaches {end} of {:?}", self.t.shape());
ensure!(self.t.dtype() == DType::F32, "attention reads FP32, got {:?}", self.t.dtype());
Ok(())
}
}
#[derive(Clone, Copy, Default)]
pub struct Mask<'a> {
pub key_pad: Option<&'a Tensor>,
pub causal: bool,
pub rpb: Option<BoxRpb<'a>>,
}
#[derive(Clone, Copy)]
pub struct BoxRpb<'a> {
pub dx: &'a Tensor,
pub dy: &'a Tensor,
pub side: usize,
}
const ATTN_ROWS: usize = 32;
const ATTN_THREADS: usize = 128;
const TOPS_ROWS: usize = 64;
#[allow(clippy::too_many_arguments)]
pub fn attention(
gpu: &Gpu,
q: View,
k: View,
v: View,
out: View,
batch: usize,
lq: usize,
lk: usize,
(hd, dv): (usize, usize),
mask: Mask,
) -> Result<()> {
if batch == 0 || lq == 0 {
return Ok(());
}
ensure!(lk > 0, "attention over no keys");
let name = match (hd, dv) {
(16, 16) => "attn_d16",
(32, 32) => "attn_d32",
(64, 64) => "attn_d64",
(256, 256) => "attn_d256",
(256, 64) => "attn_d256_v64",
_ => bail!("no attention kernel for head dim {hd} and value width {dv}"),
};
q.check(batch, lq, hd)?;
k.check(batch, lk, hd)?;
v.check(batch, lk, dv)?;
out.check(batch, lq, dv)?;
let mut flags = 0u32;
if let Some(pad) = mask.key_pad {
ensure!(pad.dtype() == DType::U8 && pad.len() >= lk, "key padding {:?} for {lk} keys", pad.shape());
flags |= 1;
}
if mask.causal {
flags |= 2;
}
let side = match mask.rpb {
Some(r) => {
ensure!(lk == r.side * r.side, "box RPB covers a {0}x{0} key grid, not {lk} keys", r.side);
ensure!(r.dx.len() == batch * (lq - 1) * r.side && r.dy.len() == r.dx.len(), "box RPB shape");
flags |= 4;
r.side
}
None => 0,
};
let scale_log2 = LOG2_E / (hd as f32).sqrt();
let strides = [out.ld, out.stride, q.ld, q.stride, k.ld, k.stride, v.ld, v.stride];
if (hd, dv, flags) == (256, 64, 0) {
return attention_coop(gpu, q, k, v, out, batch, lq, lk, scale_log2);
}
if (hd, dv, flags) == (256, 256, 0) {
return attention_unfused(gpu, q, k, v, out, batch, lq, lk, (hd, dv), scale_log2);
}
let tops = match (hd, dv, flags) {
(32, 32, 0) => Some("attn_tops_d32"),
(64, 64, 0) => Some("attn_tops_d64"),
_ => None,
};
if let Some(tops) = tops {
let mut args = vec![
out.t.at(out.offset),
q.t.at(q.offset),
k.t.at(k.offset),
v.t.at(v.offset),
lq.into(),
lk.into(),
scale_log2.into(),
];
args.extend(strides.map(Arg::from));
return gpu.dispatch(tops, [groups(lq, TOPS_ROWS), batch, 1], [ATTN_THREADS, 1, 1], &args);
}
let unused = out.t.buf.arg();
let args = [
out.t.at(out.offset),
q.t.at(q.offset),
k.t.at(k.offset),
v.t.at(v.offset),
mask.key_pad.map_or(unused, Tensor::arg),
mask.rpb.map_or(unused, |r| r.dx.arg()),
mask.rpb.map_or(unused, |r| r.dy.arg()),
lq.into(),
lk.into(),
scale_log2.into(),
flags.into(),
side.into(),
];
let blocks = groups(lq, ATTN_ROWS) * batch;
let splits = kv_splits(blocks, lk);
if splits == 1 {
let args: Vec<Arg> =
args.into_iter().chain(strides.map(Arg::from)).chain([unused, unused, lk.into()]).collect();
return gpu.dispatch(name, [groups(lq, ATTN_ROWS), batch, 1], [ATTN_THREADS, 1, 1], &args);
}
let kchunk = lk.div_ceil(splits).next_multiple_of(KV_ALIGN);
let splits = lk.div_ceil(kchunk);
let part = gpu.empty(&[splits * batch * lq, dv], DType::F32)?;
let ml = gpu.empty(&[splits * batch * lq, 2], DType::F32)?;
let args: Vec<Arg> =
args.into_iter().chain(strides.map(Arg::from)).chain([part.arg(), ml.arg(), kchunk.into()]).collect();
gpu.dispatch(name, [groups(lq, ATTN_ROWS), batch, splits], [ATTN_THREADS, 1, 1], &args)?;
let args = [
out.t.at(out.offset),
part.arg(),
ml.arg(),
splits.into(),
batch.into(),
lq.into(),
dv.into(),
out.ld.into(),
out.stride.into(),
];
gpu.dispatch_1d("attn_combine", batch * lq * dv, &args)
}
const COOP_ROWS: usize = 64;
const COOP_TARGET_GROUPS: usize = 320;
const COOP_MIN_CHUNK: usize = 1024;
#[allow(clippy::too_many_arguments)]
fn attention_coop(
gpu: &Gpu,
q: View,
k: View,
v: View,
out: View,
batch: usize,
lq: usize,
lk: usize,
scale_log2: f32,
) -> Result<()> {
let blocks = groups(lq, COOP_ROWS) * batch;
let splits = COOP_TARGET_GROUPS.div_ceil(blocks).min(lk / COOP_MIN_CHUNK).max(1);
let kchunk = lk.div_ceil(splits).next_multiple_of(COOP_ROWS);
let splits = lk.div_ceil(kchunk);
let dv = 64;
let (part, ml) = if splits > 1 {
(
Some(gpu.empty(&[splits * batch * lq, dv], DType::F32)?),
Some(gpu.empty(&[splits * batch * lq, 2], DType::F32)?),
)
} else {
(None, None)
};
let unused = out.t.buf.arg();
let strides = [out.ld, out.stride, q.ld, q.stride, k.ld, k.stride, v.ld, v.stride];
let args: Vec<Arg> = [
out.t.at(out.offset),
q.t.at(q.offset),
k.t.at(k.offset),
v.t.at(v.offset),
lq.into(),
lk.into(),
scale_log2.into(),
]
.into_iter()
.chain(strides.map(Arg::from))
.chain([part.as_ref().map_or(unused, Tensor::arg), ml.as_ref().map_or(unused, Tensor::arg), kchunk.into()])
.collect();
gpu.dispatch("attn_coop_d256_v64", [groups(lq, COOP_ROWS), batch, splits], [128, 1, 1], &args)?;
let (Some(part), Some(ml)) = (part, ml) else {
return Ok(());
};
let args = [
out.t.at(out.offset),
part.arg(),
ml.arg(),
splits.into(),
batch.into(),
lq.into(),
dv.into(),
out.ld.into(),
out.stride.into(),
];
gpu.dispatch_1d("attn_combine", batch * lq * dv, &args)
}
const UNFUSED_SCORES: usize = 64 << 20;
#[allow(clippy::too_many_arguments)]
fn attention_unfused(
gpu: &Gpu,
q: View,
k: View,
v: View,
out: View,
batch: usize,
lq: usize,
lk: usize,
(hd, dv): (usize, usize),
scale_log2: f32,
) -> Result<()> {
let chunk = (UNFUSED_SCORES / lk).clamp(GEMM_TILE, lq.next_multiple_of(GEMM_TILE)) / GEMM_TILE * GEMM_TILE;
let s = gpu.empty(&[chunk.min(lq), lk], DType::F32)?;
let inv = gpu.empty(&[chunk.min(lq)], DType::F32)?;
for b in 0..batch {
for r0 in (0..lq).step_by(chunk) {
let rows = chunk.min(lq - r0);
let qa = q.t.at(q.offset + b * q.stride + r0 * q.ld);
let args = [
s.arg(),
qa,
k.t.at(k.offset + b * k.stride),
rows.into(),
lk.into(),
hd.into(),
q.ld.into(),
k.ld.into(),
scale_log2.into(),
];
gpu.dispatch(
"attn_scores",
[groups(lk, GEMM_TILE), groups(rows, GEMM_TILE), 1],
[GEMM_THREADS, 1, 1],
&args,
)?;
gpu.dispatch("attn_softmax_rows", [rows, 1, 1], [SOFTMAX_THREADS, 1, 1], &[s.arg(), inv.arg(), lk.into()])?;
let oa = out.t.at(out.offset + b * out.stride + r0 * out.ld);
let args = [
oa,
s.arg(),
v.t.at(v.offset + b * v.stride),
inv.arg(),
rows.into(),
lk.into(),
dv.into(),
v.ld.into(),
out.ld.into(),
];
gpu.dispatch("attn_pv", [groups(dv, GEMM_TILE), groups(rows, GEMM_TILE), 1], [GEMM_THREADS, 1, 1], &args)?;
}
}
Ok(())
}
const SOFTMAX_THREADS: usize = 256;
const KV_TARGET_GROUPS: usize = 160;
const KV_MIN_CHUNK: usize = 256;
const KV_ALIGN: usize = 32;
fn kv_splits(blocks: usize, lk: usize) -> usize {
if blocks >= KV_TARGET_GROUPS {
return 1;
}
KV_TARGET_GROUPS.div_ceil(blocks).min(lk / KV_MIN_CHUNK).max(1)
}
pub fn mha(gpu: &Gpu, q: View, k: View, v: View, (lq, lk): (usize, usize), heads: usize, mask: Mask) -> Result<Tensor> {
let hd = q.stride;
let out = gpu.empty(&[lq, heads * hd], DType::F32)?;
attention(gpu, q, k, v, View::heads(&out, 0, hd), heads, lq, lk, (hd, hd), mask)?;
Ok(out)
}
pub struct Mlp {
pub layers: Vec<Linear>,
pub act: Act,
}
impl Mlp {
pub fn load(gpu: &Gpu, wts: &Weights, prefixes: &[String], act: Act) -> Result<Self> {
let layers = prefixes.iter().map(|p| Linear::load(gpu, wts, p, DType::F32)).collect::<Result<_>>()?;
Ok(Self { layers, act })
}
pub fn load_numbered(gpu: &Gpu, wts: &Weights, prefix: &str, n: usize, act: Act) -> Result<Self> {
let prefixes: Vec<String> = (1..=n).map(|i| format!("{prefix}.layer{i}")).collect();
Self::load(gpu, wts, &prefixes, act)
}
pub fn load_indexed(gpu: &Gpu, wts: &Weights, prefix: &str, n: usize, act: Act) -> Result<Self> {
let prefixes: Vec<String> = (0..n).map(|i| format!("{prefix}.layers.{i}")).collect();
Self::load(gpu, wts, &prefixes, act)
}
pub fn load_in_out(gpu: &Gpu, wts: &Weights, prefix: &str, n: usize, act: Act) -> Result<Self> {
let mut prefixes = vec![format!("{prefix}.proj_in")];
prefixes.extend((0..n - 2).map(|i| format!("{prefix}.layers.{i}")));
prefixes.push(format!("{prefix}.proj_out"));
Self::load(gpu, wts, &prefixes, act)
}
pub fn forward(&self, gpu: &Gpu, x: &Tensor) -> Result<Tensor> {
let last = self.layers.len() - 1;
let act = |i: usize| if i == last { Act::None } else { self.act };
let mut h = self.layers[0].forward(gpu, x, act(0))?;
for (i, layer) in self.layers.iter().enumerate().skip(1) {
h = layer.forward(gpu, &h, act(i))?;
}
Ok(h)
}
}
pub struct Attention {
pub q: Linear,
pub k: Linear,
pub v: Linear,
pub o: Linear,
pub heads: usize,
}
impl Attention {
pub fn load(gpu: &Gpu, wts: &Weights, prefix: &str, heads: usize) -> Result<Self> {
let l = |n: &str| Linear::load(gpu, wts, &format!("{prefix}.{n}"), DType::F32);
Ok(Self { q: l("q_proj")?, k: l("k_proj")?, v: l("v_proj")?, o: l("o_proj")?, heads })
}
pub fn forward_residual(
&self,
gpu: &Gpu,
q_in: &Tensor,
k_in: &Tensor,
v_in: &Tensor,
mask: Mask,
residual: &Tensor,
) -> Result<()> {
let attn = self.attend(gpu, q_in, k_in, v_in, 1, mask)?;
self.o.forward_residual(gpu, &attn, residual)
}
pub fn forward_batch(
&self,
gpu: &Gpu,
q_in: &Tensor,
k_in: &Tensor,
v_in: &Tensor,
batch: usize,
) -> Result<Tensor> {
let attn = self.attend(gpu, q_in, k_in, v_in, batch, Mask::default())?;
self.o.forward(gpu, &attn, Act::None)
}
pub fn forward_residual_batch(
&self,
gpu: &Gpu,
q_in: &Tensor,
k_in: &Tensor,
v_in: &Tensor,
batch: usize,
residual: &Tensor,
) -> Result<()> {
let attn = self.attend(gpu, q_in, k_in, v_in, batch, Mask::default())?;
self.o.forward_residual(gpu, &attn, residual)
}
fn attend(
&self,
gpu: &Gpu,
q_in: &Tensor,
k_in: &Tensor,
v_in: &Tensor,
batch: usize,
mask: Mask,
) -> Result<Tensor> {
let q = self.q.forward(gpu, q_in, Act::None)?;
let k = self.k.forward(gpu, k_in, Act::None)?;
let v = self.v.forward(gpu, v_in, Act::None)?;
self.attend_projected(gpu, (&q, 0), (&k, 0), (&v, 0), batch, mask)
}
pub fn attend_projected(
&self,
gpu: &Gpu,
(q, qc): (&Tensor, usize),
(k, kc): (&Tensor, usize),
(v, vc): (&Tensor, usize),
batch: usize,
mask: Mask,
) -> Result<Tensor> {
let (dim, heads) = (self.q.out_features(), self.heads);
let hd = dim / heads;
ensure!(
qc + dim <= q.cols() && kc + dim <= k.cols() && vc + dim <= v.cols(),
"attention columns past the projections"
);
ensure!(q.rows().is_multiple_of(batch) && k.rows().is_multiple_of(batch), "rows of {batch} sequences");
ensure!(k.rows() == v.rows(), "{} keys, {} values", k.rows(), v.rows());
let (lq, lk) = (q.rows() / batch, k.rows() / batch);
let out = gpu.empty(&[q.rows(), dim], DType::F32)?;
for b in 0..batch {
let (qv, kv, vv) = (View::heads(q, qc, hd), View::heads(k, kc, hd), View::heads(v, vc, hd));
let ov = View::heads(&out, 0, hd);
let (qv, kv, vv, ov) = (qv.skip(b * lq), kv.skip(b * lk), vv.skip(b * lk), ov.skip(b * lq));
attention(gpu, qv, kv, vv, ov, heads, lq, lk, (hd, hd), mask)?;
}
Ok(out)
}
}
pub fn concat_rows(gpu: &Gpu, a: &Tensor, b: &Tensor) -> Result<Tensor> {
ensure!(a.cols() == b.cols(), "concat_rows of {:?} and {:?}", a.shape(), b.shape());
let out = gpu.empty(&[a.rows() + b.rows(), a.cols()], DType::F32)?;
gpu.dispatch_1d("copy_f32", a.len(), &[out.arg(), a.arg()])?;
gpu.dispatch_1d("copy_f32", b.len(), &[out.at(a.len()), b.arg()])?;
Ok(out)
}
pub fn gather_rows(gpu: &Gpu, x: &Tensor, idx: &[i32]) -> Result<Tensor> {
let cols = x.cols();
ensure!(idx.iter().all(|&i| (i as usize) < x.rows()), "gather of rows {idx:?} from {:?}", x.shape());
let idx = gpu.upload(idx, &[idx.len()], DType::I32)?;
let out = gpu.empty(&[idx.len(), cols], DType::F32)?;
gpu.dispatch_1d("gather_rows", out.len(), &[out.arg(), x.arg(), idx.arg(), cols.into()])?;
Ok(out)
}