use eyre::Result;
use crate::cuda::gpu::{Arg, Gpu, Mat, Tensor, launch};
use crate::cuda::ops::{
Act, B256, Bf16, Conv3x3, ConvT2x2, LayerNorm, Linear, Mask, add_assign_bf16, attention, bias_act, ew,
};
use crate::precision::Precision;
use crate::weights::Weights;
pub const IMAGE_SIZE: usize = 1008;
pub const PATCH: usize = 14;
pub const GRID: usize = IMAGE_SIZE / PATCH;
const DIM: usize = 1024;
const HEADS: usize = 16;
const HEAD_DIM: usize = DIM / HEADS;
const WINDOW: usize = 24;
const GLOBAL_BLOCKS: [usize; 4] = [7, 15, 23, 31];
fn window_to_raster(r: usize) -> usize {
let nwx = GRID / WINDOW;
let (w, l) = (r / (WINDOW * WINDOW), r % (WINDOW * WINDOW));
((w / nwx) * WINDOW + l / WINDOW) * GRID + (w % nwx) * WINDOW + l % WINDOW
}
fn rope_tables(gpu: &Gpu, side: usize, scale: f32, window_major: bool) -> Result<(Tensor, Tensor)> {
let quarter = HEAD_DIM / 4;
let freqs: Vec<f32> = (0..quarter).map(|i| 1.0 / 10000f32.powf((4 * i) as f32 / HEAD_DIM as f32)).collect();
let half = HEAD_DIM / 2;
let mut cos = vec![0f32; side * side * half];
let mut sin = vec![0f32; side * side * half];
for row in 0..side * side {
let tok = if window_major { window_to_raster(row) } else { row };
let tx = (tok % side) as f32 * scale;
let ty = (tok / side) as f32 * scale;
for p in 0..half {
let angle = if p < quarter { tx * freqs[p] } else { ty * freqs[p - quarter] };
cos[row * half + p] = (angle as f64).cos() as f32;
sin[row * half + p] = (angle as f64).sin() as f32;
}
}
let shape = [side * side, half];
Ok((gpu.upload(cos, &shape)?, gpu.upload(sin, &shape)?))
}
struct Block {
norm1: LayerNorm,
qkv: Linear,
proj: Linear,
norm2: LayerNorm,
fc1: Linear,
fc2: Linear,
global: bool,
}
pub struct Backbone {
patch: Linear,
pos: Tensor,
ln_pre: LayerNorm,
blocks: Vec<Block>,
rope_window: (Tensor, Tensor),
rope_global: (Tensor, Tensor),
rope_global_wm: (Tensor, Tensor),
}
impl Backbone {
pub fn load(gpu: &Gpu, wts: &Weights) -> Result<Self> {
let p = "detector_model.vision_encoder.backbone";
let patch_w = wts.get(gpu, &format!("{p}.embeddings.patch_embeddings.projection.weight"))?;
let patch = Linear::new(patch_w.reshape(&[DIM, 3 * PATCH * PATCH]), None);
let pos = wts.get(gpu, &format!("{p}.embeddings.position_embeddings"))?;
let ln_pre = LayerNorm::load(gpu, wts, &format!("{p}.layer_norm"), 1e-5)?;
let mut blocks = Vec::new();
for i in 0..32 {
let l = format!("{p}.layers.{i}");
let a = format!("{l}.attention");
blocks.push(Block {
norm1: LayerNorm::load(gpu, wts, &format!("{l}.layer_norm1"), 1e-5)?,
qkv: Linear::load_concat(
gpu,
wts,
&[format!("{a}.q_proj"), format!("{a}.k_proj"), format!("{a}.v_proj")],
)?
.bf16(gpu)?,
proj: Linear::load(gpu, wts, &format!("{a}.o_proj"))?.bf16(gpu)?,
norm2: LayerNorm::load(gpu, wts, &format!("{l}.layer_norm2"), 1e-5)?,
fc1: Linear::load(gpu, wts, &format!("{l}.mlp.fc1"))?.bf16(gpu)?,
fc2: Linear::load(gpu, wts, &format!("{l}.mlp.fc2"))?.bf16(gpu)?,
global: GLOBAL_BLOCKS.contains(&i),
});
}
Ok(Self {
patch,
pos,
ln_pre,
blocks,
rope_window: rope_tables(gpu, WINDOW, 1.0, false)?,
rope_global: rope_tables(gpu, GRID, WINDOW as f32 / GRID as f32, false)?,
rope_global_wm: rope_tables(gpu, GRID, WINDOW as f32 / GRID as f32, true)?,
})
}
pub fn forward(&self, gpu: &Gpu, pixels: &Tensor) -> Result<Tensor> {
let k = 3 * PATCH * PATCH;
let patches = gpu.empty(&[GRID * GRID, k])?;
launch!(gpu, "patchify", ew(patches.len()), B256, 0; &patches, pixels, IMAGE_SIZE, PATCH)?;
let x = self.patch.forward(gpu, &patches)?;
drop(patches);
launch!(gpu, "add_pos_tiled", ew(x.len()), B256, 0; &x, &self.pos, GRID, 24usize)?;
self.ln_pre.forward_inplace(gpu, &x)?;
if gpu.precision() != Precision::Bf16 {
for block in &self.blocks {
self.block(gpu, block, &x)?;
}
return Ok(x);
}
let x = window_order(gpu, &x, true)?;
let mut pending = None;
for block in &self.blocks {
pending = Some(self.block16(gpu, block, &x, pending)?);
}
if let Some(fc2) = pending {
add_assign_bf16(gpu, &x, &fc2)?;
}
window_order(gpu, &x, false)
}
fn block16(&self, gpu: &Gpu, b: &Block, x: &Tensor, pending: Option<Bf16>) -> Result<Bf16> {
let tokens = GRID * GRID;
let h = match &pending {
Some(fc2) => b.norm1.add_forward16(gpu, x, fc2)?,
None => b.norm1.forward16(gpu, x)?,
};
drop(pending);
let qkv = b.qkv.forward16_act16(gpu, &h, Act::None)?;
drop(h);
let (cos, sin) = if b.global { &self.rope_global_wm } else { &self.rope_window };
let windowed = usize::from(!b.global);
let k = Bf16::empty(gpu, tokens, DIM)?;
launch!(gpu, "vit_rope_k_bf16", ew(tokens * DIM / 2), B256, 0;
&k, &qkv, cos, sin, DIM, HEAD_DIM, windowed)?;
let merged = Bf16::empty(gpu, tokens, DIM)?;
let view = |ptr: u64, ld: usize| {
let extent = (HEADS - 1) * HEAD_DIM + (tokens - 1) * ld + HEAD_DIM;
[Arg::slice(ptr, extent), Arg::from(ld), Arg::from(HEAD_DIM)]
};
let qkv_col = |col: usize| qkv.buf.ptr() + (col * 2) as u64;
let mut args =
vec![Arg::slice(merged.buf.ptr(), tokens * DIM / 2), Arg::from(DIM / 2), Arg::from(HEAD_DIM / 2)];
args.extend(view(qkv_col(0), 3 * DIM));
args.extend(view(k.buf.ptr(), DIM));
args.extend(view(qkv_col(2 * DIM), 3 * DIM));
args.extend([
Arg::from(tokens),
Arg::from(tokens),
Arg::from(std::f32::consts::LOG2_E / (HEAD_DIM as f32).sqrt()),
Arg::from(cos),
Arg::from(sin),
Arg::from(windowed),
Arg::EMPTY,
]);
gpu.launch("vit_attn_bf16_d64", ((tokens / 64) as u32, HEADS as u32, 1), (128, 1, 1), 0, args)?;
drop((qkv, k));
let attn = b.proj.forward16_act16(gpu, &merged, Act::None)?;
drop(merged);
let hidden = b.fc1.forward16_act16(gpu, &b.norm2.add_forward16(gpu, x, &attn)?, Act::GeluTanh)?;
drop(attn);
b.fc2.forward16_act16(gpu, &hidden, Act::None)
}
fn block(&self, gpu: &Gpu, b: &Block, x: &Tensor) -> Result<()> {
let tokens = GRID * GRID;
let (win, rope) = if b.global { (GRID, &self.rope_global) } else { (WINDOW, &self.rope_window) };
let batch = (GRID / win) * (GRID / win) * HEADS;
let l = win * win;
let o = gpu.empty(&[batch, l, HEAD_DIM])?;
let out = Mat::new(o.ptr(), HEAD_DIM).stride(l * HEAD_DIM);
let qkv = b.qkv.forward(gpu, &b.norm1.forward(gpu, x)?)?;
let q = gpu.empty(&[batch, l, HEAD_DIM])?;
let k = gpu.empty(&[batch, l, HEAD_DIM])?;
let v = gpu.empty(&[batch, l, HEAD_DIM])?;
launch!(gpu, "vit_split_rope", ew(tokens * DIM / 2), B256, 0;
&q, &k, &v, &qkv, &rope.0, &rope.1, GRID, win, HEADS, HEAD_DIM)?;
drop(qkv);
let view = |t: &Tensor| Mat::new(t.ptr(), HEAD_DIM).stride(l * HEAD_DIM);
attention(gpu, view(&q), view(&k), view(&v), out, batch, l, l, HEAD_DIM, Mask::default())?;
let merged = gpu.empty(&[tokens, DIM])?;
launch!(gpu, "vit_merge_heads", ew(merged.len()), B256, 0; &merged, &o, GRID, win, HEADS, HEAD_DIM)?;
drop(o);
b.proj.forward_residual(gpu, &merged, x)?;
drop(merged);
let h = b.norm2.forward(gpu, x)?;
let hidden = b.fc1.forward_act(gpu, &h, Act::GeluTanh)?;
drop(h);
b.fc2.forward_residual(gpu, &hidden, x)
}
}
fn window_order(gpu: &Gpu, x: &Tensor, to_window: bool) -> Result<Tensor> {
let out = gpu.empty(x.shape())?;
launch!(gpu, "vit_window_order", ew(x.len()), B256, 0; &out, x, x.cols(), usize::from(to_window))?;
Ok(out)
}
pub struct Fpn {
pub fine: Tensor,
pub mid: Tensor,
pub coarse: Tensor,
}
enum Scale {
Up4(ConvT2x2, ConvT2x2),
Up2(ConvT2x2),
Same,
}
struct Level {
scale: Scale,
proj1: Linear,
proj2: Conv3x3,
}
impl Level {
fn load(gpu: &Gpu, wts: &Weights, prefix: &str, scale: Scale) -> Result<Self> {
Ok(Self {
scale,
proj1: Linear::load(gpu, wts, &format!("{prefix}.proj1"))?.bf16(gpu)?,
proj2: Conv3x3::load(gpu, wts, &format!("{prefix}.proj2"))?,
})
}
fn forward(&self, gpu: &Gpu, trunk: &Trunk) -> Result<Tensor> {
let (h, side) = match (&self.scale, &trunk.bf16) {
(Scale::Up4(a, b), Some(t16)) => {
let h16 = a.forward16(gpu, t16, GRID, GRID, Act::Gelu)?;
let y16 = b.forward16(gpu, &h16, 2 * GRID, 2 * GRID, Act::None)?;
(self.proj1.forward_from16(gpu, &y16)?, 4 * GRID)
}
(Scale::Up2(a), Some(t16)) => {
(self.proj1.forward_from16(gpu, &a.forward16(gpu, t16, GRID, GRID, Act::None)?)?, 2 * GRID)
}
(Scale::Same, Some(t16)) => (self.proj1.forward_from16(gpu, t16)?, GRID),
(Scale::Up4(a, b), None) => {
let h = a.forward(gpu, &trunk.f32, GRID, GRID)?;
bias_act(gpu, &h, None, Act::Gelu)?;
(self.proj1.forward(gpu, &b.forward(gpu, &h, 2 * GRID, 2 * GRID)?)?, 4 * GRID)
}
(Scale::Up2(a), None) => (self.proj1.forward(gpu, &a.forward(gpu, &trunk.f32, GRID, GRID)?)?, 2 * GRID),
(Scale::Same, None) => (self.proj1.forward(gpu, &trunk.f32)?, GRID),
};
self.proj2.forward(gpu, &h, side, side)
}
}
pub struct Neck {
fine: Level,
mid: Level,
coarse: Level,
}
impl Neck {
pub fn load(gpu: &Gpu, wts: &Weights, prefix: &str) -> Result<Self> {
let level = |i: usize| format!("{prefix}.fpn_layers.{i}");
let up = |i: usize, j: usize| ConvT2x2::load(gpu, wts, &format!("{}.scale_layers.{j}", level(i)));
Ok(Self {
fine: Level::load(gpu, wts, &level(0), Scale::Up4(up(0, 0)?, up(0, 2)?))?,
mid: Level::load(gpu, wts, &level(1), Scale::Up2(up(1, 0)?))?,
coarse: Level::load(gpu, wts, &level(2), Scale::Same)?,
})
}
pub fn forward(&self, gpu: &Gpu, trunk: &Trunk) -> Result<Fpn> {
Ok(Fpn {
fine: self.fine.forward(gpu, trunk)?,
mid: self.mid.forward(gpu, trunk)?,
coarse: self.coarse.forward(gpu, trunk)?,
})
}
}
pub struct Trunk {
pub f32: Tensor,
pub bf16: Option<Bf16>,
}
impl Trunk {
pub fn new(gpu: &Gpu, f32: Tensor) -> Result<Self> {
let bf16 = match gpu.precision() {
Precision::Bf16 => Some(Bf16::from_f32(gpu, &f32)?),
Precision::Fp32 | Precision::Tf32 => None,
};
Ok(Self { f32, bf16 })
}
}