use crate::output::{TrackedFrame, TrackedObject};
use std::collections::VecDeque;
use eyre::{Result, ensure, eyre};
use crate::cuda::gpu::{Arg, Buf, Gpu, Tensor, launch};
use crate::cuda::image::{AaResize, preprocess};
use crate::cuda::nvtx::range;
use crate::cuda::ops::{
Act, B256, LayerNorm, Linear, Mask, Mlp, add, attention_f16_d256, attention_f16_qk256_v64, ew, mha,
};
use crate::cuda::pipeline::Sam3;
use crate::cuda::point::{Decoded, LOW_RES, MASK_TOKEN_ROW, MaskResize, PointImage, argmax, row};
use crate::cuda::vision::{GRID, IMAGE_SIZE, Trunk};
use crate::image::RgbImage;
use crate::posenc::sine_2d;
use crate::precision::Precision;
use crate::prompt::PointPrompt;
use crate::weights::Weights;
const D: usize = 256;
const MEM_D: usize = 64;
const TOKENS: usize = GRID * GRID;
const NUM_MASKMEM: usize = 7;
const MAX_COND_FRAMES: usize = 4;
const MAX_OBJ_PTRS: usize = 16;
const MAX_CLICKS: usize = 16;
const MEMORY_SCORE_THRESHOLD: f32 = 0.01;
const NO_OBJ_SCORE: f32 = -1024.0;
const MASK_SIDE: usize = 1152;
const ROPE_THETA: f32 = 10000.0;
struct RopeAttention {
q: Linear,
k: Linear,
v: Linear,
o: Linear,
vo: Option<Linear>,
}
impl RopeAttention {
fn load(gpu: &Gpu, wts: &Weights, prefix: &str) -> Result<Self> {
let l = |n: &str| Linear::load(gpu, wts, &format!("{prefix}.{n}"))?.bf16(gpu);
Ok(Self { q: l("q_proj")?, k: l("k_proj")?, v: l("v_proj")?, o: l("o_proj")?, vo: None })
}
fn load_cross(gpu: &Gpu, wts: &Weights, prefix: &str) -> Result<Self> {
let mut out = Self::load(gpu, wts, prefix)?;
if gpu.precision() == Precision::Bf16 {
let (wv, sv) = wts.host(&format!("{prefix}.v_proj.weight"))?;
let (bv, _) = wts.host(&format!("{prefix}.v_proj.bias"))?;
let (wo, so) = wts.host(&format!("{prefix}.o_proj.weight"))?;
let (bo, _) = wts.host(&format!("{prefix}.o_proj.bias"))?;
ensure!(sv == [D, MEM_D] && so == [D, D], "{prefix}: v_proj {sv:?}, o_proj {so:?}");
let mut w = vec![0f32; D * MEM_D];
let mut b = vec![0f32; D];
for i in 0..D {
let row = &wo[i * D..(i + 1) * D];
for j in 0..MEM_D {
w[i * MEM_D + j] =
row.iter().enumerate().map(|(k, &x)| x as f64 * wv[k * MEM_D + j] as f64).sum::<f64>() as f32;
}
b[i] = (row.iter().zip(&bv).map(|(&x, &y)| x as f64 * y as f64).sum::<f64>() + bo[i] as f64) as f32;
}
out.vo = Some(Linear::new(gpu.upload(w, &[D, MEM_D])?, Some(gpu.upload(b, &[D])?)).bf16(gpu)?);
}
Ok(out)
}
#[allow(clippy::too_many_arguments)]
fn forward_residual(
&self,
gpu: &Gpu,
rope: &Rope,
q_in: &Tensor,
k_in: &Tensor,
v_in: &Tensor,
rope_keys: usize,
residual: &Tensor,
) -> Result<()> {
let (q, k, v) = (self.q.forward(gpu, q_in)?, self.k.forward(gpu, k_in)?, self.v.forward(gpu, v_in)?);
let attn = if gpu.precision() == Precision::Bf16 {
let (q16, k16, v16) =
(rope.apply_f16(gpu, &q, q.rows())?, rope.apply_f16(gpu, &k, rope_keys)?, rope.apply_f16(gpu, &v, 0)?);
attention_f16_d256(gpu, &q16, &k16, &v16, q.rows(), k.rows())?
} else {
let (q, k) = (rope.apply(gpu, &q, q.rows())?, rope.apply(gpu, &k, rope_keys)?);
mha(gpu, (q.ptr(), D), (k.ptr(), D), (v.ptr(), D), q.rows(), k.rows(), 1, D, Mask::default())?
};
self.o.forward_residual(gpu, &attn, residual)
}
#[allow(clippy::too_many_arguments)]
fn forward_residual_folded(
&self,
gpu: &Gpu,
rope: &Rope,
q_in: &Tensor,
k_in: &Tensor,
memory16: &Buf,
rope_keys: usize,
residual: &Tensor,
) -> Result<()> {
let k = self.k.forward(gpu, k_in)?;
let k16 = rope.apply_f16(gpu, &k, rope_keys)?;
self.attend_folded(gpu, &self.query16(gpu, rope, q_in)?, &k16, memory16, k.rows(), residual)
}
fn query16(&self, gpu: &Gpu, rope: &Rope, q_in: &Tensor) -> Result<Buf> {
let q = self.q.forward(gpu, q_in)?;
rope.apply_f16(gpu, &q, q.rows())
}
fn attend_folded(
&self,
gpu: &Gpu,
q16: &Buf,
k16: &Buf,
memory16: &Buf,
lk: usize,
residual: &Tensor,
) -> Result<()> {
let vo = self.vo.as_ref().expect("folded projection loaded in BF16 mode");
let attn = attention_f16_qk256_v64(gpu, q16, k16, memory16, TOKENS, lk)?;
vo.forward_residual(gpu, &attn, residual)
}
}
struct Rope {
cos: Tensor,
sin: Tensor,
}
impl Rope {
fn new(gpu: &Gpu) -> Result<Self> {
let quarter = D / 4;
let freqs: Vec<f32> = (0..quarter).map(|i| 1.0 / ROPE_THETA.powf((4 * i) as f32 / D as f32)).collect();
let (mut cos, mut sin) = (Vec::with_capacity(TOKENS * D / 2), Vec::with_capacity(TOKENS * D / 2));
for t in 0..TOKENS {
let (x, y) = ((t % GRID) as f32, (t / GRID) as f32);
for (pos, f) in freqs.iter().map(|f| (x, f)).chain(freqs.iter().map(|f| (y, f))) {
let angle = (pos * f) as f64;
cos.push(angle.cos() as f32);
sin.push(angle.sin() as f32);
}
}
Ok(Self { cos: gpu.upload(cos, &[TOKENS, D / 2])?, sin: gpu.upload(sin, &[TOKENS, D / 2])? })
}
fn apply(&self, gpu: &Gpu, x: &Tensor, rows: usize) -> Result<Tensor> {
let out = gpu.empty(x.shape())?;
launch!(gpu, "rope_rows", ew(out.len()), B256, 0; &out, x, &self.cos, &self.sin, D, rows)?;
Ok(out)
}
fn apply_f16(&self, gpu: &Gpu, x: &Tensor, rows: usize) -> Result<Buf> {
let out = gpu.alloc_bytes(x.len() * 2)?;
launch!(gpu, "rope_rows_f16", ew(x.len() / 2), B256, 0; out.slice::<u32>(), x, &self.cos, &self.sin, D, rows)?;
Ok(out)
}
}
struct MemoryLayer {
norm1: LayerNorm,
self_attn: RopeAttention,
norm2: LayerNorm,
cross_attn: RopeAttention,
norm3: LayerNorm,
linear1: Linear,
linear2: Linear,
}
pub(crate) struct FrameKeys {
keys: Vec<Buf>,
values: Buf,
}
pub(crate) struct FrameStart {
x: Tensor,
q16: Buf,
}
enum Bank<'a> {
Assembled { keys: Tensor, memory: &'a Tensor, memory16: Option<Buf>, rope_keys: usize },
Cached { keys: Vec<Buf>, values: Buf, lk: usize },
}
pub struct MemoryAttention {
layers: Vec<MemoryLayer>,
norm: LayerNorm,
rope: Rope,
pub(crate) src_pos: Tensor,
mem_sine: Tensor,
tpos_keys: Vec<Tensor>,
}
impl MemoryAttention {
fn load(gpu: &Gpu, wts: &Weights) -> Result<Self> {
let p = "tracker_model.memory_attention";
let layers = (0..4)
.map(|i| {
let l = format!("{p}.layers.{i}");
let ln = |n: &str| LayerNorm::load(gpu, wts, &format!("{l}.{n}"), 1e-5);
Ok(MemoryLayer {
norm1: ln("layer_norm1")?,
self_attn: RopeAttention::load(gpu, wts, &format!("{l}.self_attn"))?,
norm2: ln("layer_norm2")?,
cross_attn: RopeAttention::load_cross(gpu, wts, &format!("{l}.cross_attn_image"))?,
norm3: ln("layer_norm3")?,
linear1: Linear::load(gpu, wts, &format!("{l}.linear1"))?.bf16(gpu)?,
linear2: Linear::load(gpu, wts, &format!("{l}.linear2"))?.bf16(gpu)?,
})
})
.collect::<Result<_>>()?;
let src_pos = sine_2d(GRID, D).into_iter().map(|v| 0.1 * v).collect();
let mut tpos_keys = Vec::new();
if gpu.precision() == Precision::Bf16 {
let (tpos, _) = wts.host("tracker_model.memory_temporal_positional_encoding")?;
ensure!(tpos.len() == NUM_MASKMEM * MEM_D, "{} temporal encoding values", tpos.len());
for i in 0..4 {
let (wk, shape) = wts.host(&format!("{p}.layers.{i}.cross_attn_image.k_proj.weight"))?;
ensure!(shape == [D, MEM_D], "k_proj {shape:?}");
let c: Vec<f32> = tpos
.chunks(MEM_D)
.flat_map(|t| {
let wk = &wk;
(0..D).map(move |o| {
wk[o * MEM_D..][..MEM_D].iter().zip(t).map(|(&w, &x)| w as f64 * x as f64).sum::<f64>()
as f32
})
})
.collect();
tpos_keys.push(gpu.upload(c, &[NUM_MASKMEM, D])?);
}
}
Ok(Self {
layers,
norm: LayerNorm::load(gpu, wts, &format!("{p}.layer_norm"), 1e-5)?,
rope: Rope::new(gpu)?,
src_pos: gpu.upload(src_pos, &[TOKENS, D])?,
mem_sine: gpu.upload(sine_2d(GRID, MEM_D), &[TOKENS, MEM_D])?,
tpos_keys,
})
}
pub(crate) fn frame_keys(&self, gpu: &Gpu, mem: &Tensor) -> Result<FrameKeys> {
assert_eq!(mem.shape(), &[TOKENS, MEM_D]);
let keys_in = add(gpu, mem, &self.mem_sine)?;
let keys = self
.layers
.iter()
.map(|l| self.rope.apply_f16(gpu, &l.cross_attn.k.forward(gpu, &keys_in)?, TOKENS))
.collect::<Result<_>>()?;
Ok(FrameKeys { keys, values: self.rope.apply_f16(gpu, mem, 0)? })
}
pub(crate) fn forward_cached(
&self,
gpu: &Gpu,
start: &FrameStart,
slots: &[(&FrameKeys, usize)],
ptr: &Tensor,
ptr_pos: &Tensor,
) -> Result<Tensor> {
assert_eq!(ptr.shape(), ptr_pos.shape());
let p = ptr.rows();
let lk = slots.len() * TOKENS + p;
ensure!(lk > 0, "no memory to attend to");
let frame_bytes = |w: usize| TOKENS * w * 2;
let values = gpu.alloc_bytes(lk * MEM_D * 2)?;
for (i, (f, _)) in slots.iter().enumerate() {
gpu.copy_dtod(f.values.ptr(), values.ptr() + (i * frame_bytes(MEM_D)) as u64, frame_bytes(MEM_D))?;
}
let tail = |buf: &Buf, w: usize| buf.ptr() + (slots.len() * frame_bytes(w)) as u64;
let ptr_keys_in = if p > 0 {
let v16 = self.rope.apply_f16(gpu, ptr, 0)?;
gpu.copy_dtod(v16.ptr(), tail(&values, MEM_D), p * MEM_D * 2)?;
Some(add(gpu, ptr, ptr_pos)?)
} else {
None
};
let mut keys = Vec::with_capacity(self.layers.len());
for (l, tpos_keys) in self.layers.iter().zip(&self.tpos_keys) {
let k = gpu.alloc_bytes(lk * D * 2)?;
for (i, (f, tpos)) in slots.iter().enumerate() {
let out = Arg::slice(k.ptr() + (i * frame_bytes(D)) as u64, TOKENS * D / 2);
let c = Arg::slice(tpos_keys.at(tpos * D), D);
launch!(gpu, "slot_keys_f16", ew(TOKENS * D / 2), B256, 0;
out, f.keys[keys.len()].slice::<u32>(), c, &self.rope.cos, &self.rope.sin)?;
}
if let Some(ptr_keys_in) = &ptr_keys_in {
let k16 = self.rope.apply_f16(gpu, &l.cross_attn.k.forward(gpu, ptr_keys_in)?, 0)?;
gpu.copy_dtod(k16.ptr(), tail(&k, D), p * D * 2)?;
}
keys.push(k);
}
self.layers_from(gpu, gpu.clone_tensor(&start.x)?, Some(&start.q16), &Bank::Cached { keys, values, lk })
}
pub(crate) fn frame_start(&self, gpu: &Gpu, src: &Tensor) -> Result<FrameStart> {
assert_eq!(src.shape(), &[TOKENS, D]);
let x = add(gpu, src, &self.src_pos)?;
let l = &self.layers[0];
let h = l.norm1.forward(gpu, &x)?;
l.self_attn.forward_residual(gpu, &self.rope, &h, &h, &h, TOKENS, &x)?;
let q16 = l.cross_attn.query16(gpu, &self.rope, &l.norm2.forward(gpu, &x)?)?;
Ok(FrameStart { x, q16 })
}
pub fn forward(
&self,
gpu: &Gpu,
src: &Tensor,
memory: &Tensor,
memory_pos: &Tensor,
ptr_tokens: usize,
) -> Result<Tensor> {
assert_eq!(memory.shape(), memory_pos.shape());
let keys = add(gpu, memory, memory_pos)?;
let rope_keys = memory.rows() - ptr_tokens;
let memory16 =
if gpu.precision() == Precision::Bf16 { Some(self.rope.apply_f16(gpu, memory, 0)?) } else { None };
self.run(gpu, src, &Bank::Assembled { keys, memory, memory16, rope_keys })
}
fn run(&self, gpu: &Gpu, src: &Tensor, bank: &Bank) -> Result<Tensor> {
assert_eq!(src.shape(), &[TOKENS, D]);
self.layers_from(gpu, add(gpu, src, &self.src_pos)?, None, bank)
}
fn layers_from(&self, gpu: &Gpu, x: Tensor, q0: Option<&Buf>, bank: &Bank) -> Result<Tensor> {
let bf16 = gpu.precision() == Precision::Bf16;
let rope = &self.rope;
for (i, l) in self.layers.iter().enumerate() {
let q16 = if i == 0 { q0 } else { None };
if q16.is_none() {
let h = l.norm1.forward(gpu, &x)?;
l.self_attn.forward_residual(gpu, rope, &h, &h, &h, TOKENS, &x)?;
}
match bank {
Bank::Assembled { keys, memory16: Some(m16), rope_keys, .. } => {
assert!(q16.is_none(), "an assembled bank starts at layer 0");
let h = l.norm2.forward(gpu, &x)?;
l.cross_attn.forward_residual_folded(gpu, rope, &h, keys, m16, *rope_keys, &x)?
}
Bank::Assembled { keys, memory, memory16: None, rope_keys } => {
assert!(q16.is_none(), "an assembled bank starts at layer 0");
let h = l.norm2.forward(gpu, &x)?;
l.cross_attn.forward_residual(gpu, rope, &h, keys, memory, *rope_keys, &x)?
}
Bank::Cached { keys, values, lk } => {
let own;
let q16 = match q16 {
Some(q) => q,
None => {
own = l.cross_attn.query16(gpu, rope, &l.norm2.forward(gpu, &x)?)?;
&own
}
};
l.cross_attn.attend_folded(gpu, q16, &keys[i], values, *lk, &x)?
}
}
if bf16 {
let hidden = l.linear1.forward16_act16(gpu, &l.norm3.forward16(gpu, &x)?, Act::Relu)?;
l.linear2.forward16_residual(gpu, &hidden, &x)?;
} else {
let h = l.norm3.forward(gpu, &x)?;
l.linear2.forward_residual(gpu, &l.linear1.forward_act(gpu, &h, Act::Relu)?, &x)?;
}
}
self.norm.forward(gpu, &x)
}
}
const DOWN_GEMM_CIN: usize = 16;
enum DownConv {
Direct(Tensor, Tensor),
Gemm(Linear),
}
struct DownLayer {
conv: DownConv,
norm: LayerNorm,
cin: usize,
cout: usize,
}
struct CxBlock {
dw_w: Tensor,
dw_b: Tensor,
norm: LayerNorm,
fc1: Linear,
fc2: Linear,
}
pub struct MemoryEncoder {
resize: AaResize,
down: Vec<DownLayer>,
final_conv: Linear,
pix_proj: Linear,
fuser: Vec<CxBlock>,
out_proj: Linear,
}
impl MemoryEncoder {
fn load(gpu: &Gpu, wts: &Weights) -> Result<Self> {
let p = "tracker_model.memory_encoder";
let mut down = Vec::new();
for i in 0..4 {
let l = format!("{p}.mask_downsampler.layers.{i}");
let w = wts.get(gpu, &format!("{l}.conv.weight"))?;
let (cout, cin) = (w.shape()[0], w.shape()[1]);
let b = wts.get(gpu, &format!("{l}.conv.bias"))?;
let conv = if cin >= DOWN_GEMM_CIN {
DownConv::Gemm(Linear::new(w.reshape(&[cout, cin * 9]), Some(b)))
} else {
DownConv::Direct(w, b)
};
down.push(DownLayer {
conv,
norm: LayerNorm::load(gpu, wts, &format!("{l}.layer_norm"), 1e-6)?,
cin,
cout,
});
}
let mut fuser = Vec::new();
for i in 0..2 {
let l = format!("{p}.memory_fuser.layers.{i}");
let (gamma, _) = wts.host(&format!("{l}.scale"))?;
let (mut w2, shape) = wts.host(&format!("{l}.pointwise_conv2.weight"))?;
let (mut b2, _) = wts.host(&format!("{l}.pointwise_conv2.bias"))?;
for (o, g) in gamma.iter().enumerate() {
w2[o * shape[1]..][..shape[1]].iter_mut().for_each(|v| *v *= g);
b2[o] *= g;
}
fuser.push(CxBlock {
dw_w: wts.get(gpu, &format!("{l}.depthwise_conv.weight"))?,
dw_b: wts.get(gpu, &format!("{l}.depthwise_conv.bias"))?,
norm: LayerNorm::load(gpu, wts, &format!("{l}.layer_norm"), 1e-6)?,
fc1: Linear::load(gpu, wts, &format!("{l}.pointwise_conv1"))?,
fc2: Linear::new(gpu.upload(w2, &shape)?, Some(gpu.upload(b2, &[shape[0]])?)),
});
}
Ok(Self {
resize: AaResize::new(gpu, (IMAGE_SIZE, IMAGE_SIZE), (MASK_SIDE, MASK_SIDE))?,
down,
final_conv: Linear::load(gpu, wts, &format!("{p}.mask_downsampler.final_conv"))?,
pix_proj: Linear::load(gpu, wts, &format!("{p}.feature_projection"))?,
fuser,
out_proj: Linear::load(gpu, wts, &format!("{p}.projection"))?,
})
}
pub fn forward(&self, gpu: &Gpu, pix_feat: &Tensor, mask: &Tensor) -> Result<Tensor> {
assert_eq!(mask.len(), IMAGE_SIZE * IMAGE_SIZE);
let mut x = self.resize.forward(gpu, mask)?.reshape(&[MASK_SIDE * MASK_SIDE, 1]);
let mut side = MASK_SIDE;
for l in &self.down {
side /= 2;
let y = match &l.conv {
DownConv::Direct(w, b) => {
let y = gpu.empty(&[side * side, l.cout])?;
launch!(gpu, "conv3x3_s2", ew(y.len()), B256, 0; &y, &x, w, b, 2 * side, l.cin)?;
y
}
DownConv::Gemm(conv) => {
let cols = gpu.empty(&[side * side, l.cin * 9])?;
launch!(gpu, "im2col3x3_s2", ew(cols.len()), B256, 0; &cols, &x, 2 * side, l.cin)?;
conv.forward(gpu, &cols)?
}
};
let n = &l.norm;
launch!(gpu, "layernorm_act_rows", ew(side * side), B256, 0; &y, &n.w, &n.b, l.cout, n.eps, Act::Gelu)?;
x = y;
}
assert_eq!(side, GRID);
let x = self.final_conv.forward(gpu, &x)?;
self.pix_proj.forward_residual(gpu, pix_feat, &x)?;
for b in &self.fuser {
let y = gpu.empty(x.shape())?;
launch!(gpu, "dwconv7x7", ew(y.len()), B256, 0; &y, &x, &b.dw_w, &b.dw_b, GRID)?;
b.norm.forward_inplace(gpu, &y)?;
b.fc2.forward_residual(gpu, &b.fc1.forward_act(gpu, &y, Act::Gelu)?, &x)?;
}
self.out_proj.forward(gpu, &x)
}
}
pub fn mask_for_mem(gpu: &Gpu, x: &Tensor, binarize: bool) -> Result<Tensor> {
let out = gpu.empty(x.shape())?;
launch!(gpu, "mask_for_mem", ew(out.len()), B256, 0; &out, x, binarize as i32)?;
Ok(out)
}
pub struct Tracker {
pub mem_attn: MemoryAttention,
pub mem_enc: MemoryEncoder,
pub obj_ptr_proj: Mlp,
tpos_proj: Linear,
no_obj_ptr: Tensor,
pub(crate) no_obj_spatial: Tensor,
mem_pos: Vec<Tensor>,
}
impl Tracker {
pub fn load(gpu: &Gpu, wts: &Weights) -> Result<Self> {
let p = "tracker_model";
let (tpos, _) = wts.host(&format!("{p}.memory_temporal_positional_encoding"))?;
let sine = sine_2d(GRID, MEM_D);
let mem_pos = tpos
.chunks(MEM_D)
.map(|t| {
let pos: Vec<f32> = sine.iter().enumerate().map(|(i, v)| v + t[i % MEM_D]).collect();
gpu.upload(pos, &[TOKENS, MEM_D])
})
.collect::<Result<Vec<_>>>()?;
ensure!(mem_pos.len() == NUM_MASKMEM, "{} temporal encodings, expected {NUM_MASKMEM}", mem_pos.len());
let no_obj_spatial = wts.get(gpu, &format!("{p}.occlusion_spatial_embedding_parameter"))?;
Ok(Self {
mem_attn: MemoryAttention::load(gpu, wts)?,
mem_enc: MemoryEncoder::load(gpu, wts)?,
obj_ptr_proj: Mlp::load_in_out(gpu, wts, &format!("{p}.object_pointer_proj"), 3, Act::Relu)?,
tpos_proj: Linear::load(gpu, wts, &format!("{p}.temporal_positional_encoding_projection_layer"))?,
no_obj_ptr: wts.get(gpu, &format!("{p}.no_object_pointer"))?,
no_obj_spatial: no_obj_spatial.reshape(&[MEM_D]),
mem_pos,
})
}
pub fn pointer(&self, gpu: &Gpu, token: &Tensor, present: bool) -> Result<Tensor> {
if present { self.obj_ptr_proj.forward(gpu, token) } else { gpu.clone_tensor(&self.no_obj_ptr) }
}
pub fn memory(&self, gpu: &Gpu, pix_feat: &Tensor, mask: &Tensor, present: bool) -> Result<Tensor> {
let mem = self.mem_enc.forward(gpu, pix_feat, mask)?;
if !present {
launch!(gpu, "add_row", ew(mem.len()), B256, 0; &mem, &self.no_obj_spatial)?;
}
launch!(gpu, "round_bf16", ew(mem.len()), B256, 0; &mem)?;
Ok(mem)
}
fn pointer_pos(&self, gpu: &Gpu, positions: &[usize], max_abs_pos: usize) -> Result<Tensor> {
let t_max = max_abs_pos.saturating_sub(1).max(1) as f32;
let half = D / 2;
let dims: Vec<f32> = (0..half).map(|i| 10000f32.powf((2 * (i / 2)) as f32 / half as f32)).collect();
let mut pe = Vec::with_capacity(positions.len() * D);
for &p in positions {
let v = p as f32 / t_max;
pe.extend(dims.iter().map(|d| ((v / d) as f64).sin() as f32));
pe.extend(dims.iter().map(|d| ((v / d) as f64).cos() as f32));
}
self.tpos_proj.forward(gpu, &gpu.upload(pe, &[positions.len(), D])?)
}
}
struct MemFrame {
frame: usize,
mem: Vec<Tensor>,
ptr: Vec<Tensor>,
keys: Vec<FrameKeys>,
}
pub(crate) struct Selection<'a> {
frames: Vec<(&'a MemFrame, usize)>,
ptr_frames: Vec<&'a MemFrame>,
ptr_pos: Tensor,
}
impl Selection<'_> {
pub(crate) fn ptr_tokens(&self) -> usize {
self.ptr_frames.len() * (D / MEM_D)
}
}
pub(crate) struct MemoryInputs {
pub(crate) objects: Vec<(Tensor, Tensor)>,
}
pub struct VideoTracker<'a> {
model: &'a Sam3,
width: usize,
height: usize,
num_frames: usize,
next_frame: usize,
objects: usize,
cond: Vec<MemFrame>,
passing: VecDeque<MemFrame>,
prev: Option<MemFrame>,
}
pub(crate) struct FrameFeatures {
pub(crate) point: PointImage,
pub(crate) coarse: Tensor,
}
impl Sam3 {
pub(crate) fn video_features(&self, pixels: &Tensor) -> Result<FrameFeatures> {
let gpu = &self.gpu;
let trunk = {
let _r = range(c"backbone");
self.backbone.forward(gpu, pixels)?
};
let _r = range(c"neck");
let fpn = self.tracker_neck.forward(gpu, &Trunk::new(gpu, trunk)?)?;
let point = self.point.prepare_image(gpu, &fpn)?;
Ok(FrameFeatures { point, coarse: fpn.coarse })
}
pub fn track(
&self,
first: &RgbImage,
objects: &[Vec<PointPrompt>],
num_frames: usize,
) -> Result<(VideoTracker<'_>, TrackedFrame)> {
self.gpu.bind()?;
let pixels = preprocess(&self.gpu, first)?;
self.track_pixels(&pixels, first.width(), first.height(), objects, num_frames)
}
pub(crate) fn track_pixels(
&self,
pixels: &Tensor,
width: usize,
height: usize,
objects: &[Vec<PointPrompt>],
num_frames: usize,
) -> Result<(VideoTracker<'_>, TrackedFrame)> {
ensure!(!objects.is_empty(), "track needs at least one object");
ensure!(objects.iter().all(|o| !o.is_empty()), "every object needs at least one click");
ensure!(num_frames >= 1, "a video has at least one frame");
let mut tracker = VideoTracker {
model: self,
width,
height,
num_frames,
next_frame: 1,
objects: objects.len(),
cond: Vec::new(),
passing: VecDeque::new(),
prev: None,
};
let out = tracker.prompt_frame(pixels, objects)?;
Ok((tracker, out))
}
}
impl VideoTracker<'_> {
pub fn next(&mut self, frame: &RgbImage) -> Result<TrackedFrame> {
ensure!(
(frame.width(), frame.height()) == (self.width, self.height),
"frame is {}x{}, the video {}x{}",
frame.width(),
frame.height(),
self.width,
self.height
);
let gpu = &self.model.gpu;
gpu.bind()?;
let pixels = preprocess(gpu, frame)?;
self.next_pixels(&pixels)
}
fn prompt_frame(&mut self, pixels: &Tensor, objects: &[Vec<PointPrompt>]) -> Result<TrackedFrame> {
let (m, gpu) = (self.model, &self.model.gpu);
let feats = self.model.video_features(pixels)?;
let side = LOW_RES * LOW_RES;
let low_res = gpu.empty(&[objects.len(), side])?;
let (mut scores, mut ptrs, mut picks) = (Vec::new(), Vec::new(), Vec::new());
for (b, points) in objects.iter().enumerate() {
let _r = range(c"decoder");
let points = &cap_clicks(points);
let dec =
m.point.decode(gpu, &feats.point.embed, &feats.point, points, (self.width, self.height), None, 0)?;
let score = gpu.download(&m.point.obj_score(gpu, &dec)?)?[0];
let iou = gpu.download(&dec.iou)?;
let (mask, token) = if points.len() <= 1 {
let best = 1 + argmax(&iou[1..]);
(best, best)
} else {
let stable = stability(&gpu.download(&row(gpu, &dec.low_res, 0)?)?) >= 0.98;
(if stable { 0 } else { 1 + argmax(&iou[1..]) }, 0)
};
self.select_mask(&dec, mask, score > 0.0, &low_res, b)?;
let token = row(gpu, &dec.tokens, MASK_TOKEN_ROW + token)?;
ptrs.push(m.tracker.pointer(gpu, &token, score > 0.0)?);
scores.push(score);
picks.push((iou, mask));
}
let _r = range(c"memory_encoder");
let high = upsample(gpu, (&low_res).into(), objects.len(), IMAGE_SIZE, IMAGE_SIZE)?;
let high = if objects.len() > 1 {
let out = gpu.empty(high.shape())?;
launch!(gpu, "nonoverlap", ew(out.len()), B256, 0; &out, &high, objects.len())?;
out
} else {
high
};
let input = mask_for_mem(gpu, &high, true)?;
let n = IMAGE_SIZE * IMAGE_SIZE;
let mut mem = Vec::new();
for (b, &score) in scores.iter().enumerate() {
let mask = gpu.empty(&[n])?;
gpu.copy_dtod(input.at(b * n), mask.ptr(), n * 4)?;
mem.push(m.tracker.memory(gpu, &feats.coarse, &mask, score > 0.0)?);
}
let keys = self.frame_keys(&mem)?;
self.cond.push(MemFrame { frame: 0, mem, ptr: ptrs, keys });
self.output(0, low_res, &scores, picks, 0, 0)
}
fn select_mask(&self, dec: &Decoded, index: usize, present: bool, low_res: &Tensor, b: usize) -> Result<()> {
let gpu = &self.model.gpu;
let side = LOW_RES * LOW_RES;
let first = 4 - dec.low_res.rows();
gpu.copy_dtod(dec.low_res.at((index - first) * side), low_res.at(b * side), side * 4)?;
if !present {
launch!(gpu, "clamp", ew(side), B256, 0; Arg::slice(low_res.at(b * side), side), NO_OBJ_SCORE, NO_OBJ_SCORE)?;
}
Ok(())
}
fn selected(&self) -> Vec<&MemFrame> {
let mut out: Vec<&MemFrame> = self.passing.iter().collect();
out.extend(self.prev.as_ref());
out
}
pub(crate) fn selection(&self) -> Result<Selection<'_>> {
let (m, gpu) = (self.model, &self.model.gpu);
let t = self.next_frame;
let selected = self.selected();
let cond = &self.cond[self.cond.len().saturating_sub(MAX_COND_FRAMES)..];
let mut frames: Vec<(&MemFrame, usize)> = cond.iter().map(|f| (f, NUM_MASKMEM - 1)).collect();
for t_pos in 1..NUM_MASKMEM {
let t_rel = NUM_MASKMEM - t_pos;
if t_rel <= selected.len() {
frames.push((selected[selected.len() - t_rel], NUM_MASKMEM - 1 - t_pos));
}
}
let max_abs_pos = self.num_frames.min(MAX_OBJ_PTRS);
let mut ptr_frames: Vec<(&MemFrame, usize)> = cond.iter().map(|f| (f, t - f.frame)).collect();
for t_diff in 1..max_abs_pos {
if t_diff >= selected.len() {
break;
}
ptr_frames.push((selected[selected.len() - t_diff], t_diff));
}
let positions: Vec<usize> = ptr_frames.iter().map(|(_, p)| *p).collect();
let ptr_pos = m.tracker.pointer_pos(gpu, &positions, max_abs_pos)?;
Ok(Selection { frames, ptr_frames: ptr_frames.into_iter().map(|(f, _)| f).collect(), ptr_pos })
}
fn pointer_tokens(&self, sel: &Selection, b: usize) -> Result<(Tensor, Tensor)> {
let gpu = &self.model.gpu;
let split = D / MEM_D;
let rows = sel.ptr_frames.len() * split;
let (ptr, pos) = (gpu.empty(&[rows, MEM_D])?, gpu.empty(&[rows, MEM_D])?);
for (i, f) in sel.ptr_frames.iter().enumerate() {
gpu.copy_dtod(f.ptr[b].ptr(), ptr.at(i * D), D * 4)?;
for s in 0..split {
gpu.copy_dtod(sel.ptr_pos.at(i * MEM_D), pos.at(i * D + s * MEM_D), MEM_D * 4)?;
}
}
Ok((ptr, pos))
}
pub(crate) fn memory_inputs(&self) -> Result<MemoryInputs> {
let (m, gpu) = (self.model, &self.model.gpu);
let sel = self.selection()?;
let ptr_tokens = sel.ptr_tokens();
let mem_tokens = sel.frames.len() * TOKENS + ptr_tokens;
let mut objects = Vec::with_capacity(self.objects);
for b in 0..self.objects {
let memory = gpu.empty(&[mem_tokens, MEM_D])?;
let memory_pos = gpu.empty(&[mem_tokens, MEM_D])?;
let bytes = TOKENS * MEM_D * 4;
for (i, (f, tpos)) in sel.frames.iter().enumerate() {
gpu.copy_dtod(f.mem[b].ptr(), memory.at(i * TOKENS * MEM_D), bytes)?;
gpu.copy_dtod(m.tracker.mem_pos[*tpos].ptr(), memory_pos.at(i * TOKENS * MEM_D), bytes)?;
}
let base = sel.frames.len() * TOKENS * MEM_D;
let (ptr, pos) = self.pointer_tokens(&sel, b)?;
gpu.copy_dtod(ptr.ptr(), memory.at(base), ptr_tokens * MEM_D * 4)?;
gpu.copy_dtod(pos.ptr(), memory_pos.at(base), ptr_tokens * MEM_D * 4)?;
objects.push((memory, memory_pos));
}
Ok(MemoryInputs { objects })
}
pub(crate) fn memory_attention_cached(&self, sel: &Selection, b: usize, start: &FrameStart) -> Result<Tensor> {
let slots: Vec<(&FrameKeys, usize)> = sel
.frames
.iter()
.map(|(f, tpos)| {
let keys = f.keys.get(b).ok_or_else(|| eyre!("frame {} has no cached keys", f.frame))?;
Ok((keys, *tpos))
})
.collect::<Result<_>>()?;
let (ptr, pos) = self.pointer_tokens(sel, b)?;
self.model.tracker.mem_attn.forward_cached(&self.model.gpu, start, &slots, &ptr, &pos)
}
fn frame_keys(&self, mems: &[Tensor]) -> Result<Vec<FrameKeys>> {
let gpu = &self.model.gpu;
if gpu.precision() != Precision::Bf16 {
return Ok(Vec::new());
}
mems.iter().map(|mem| self.model.tracker.mem_attn.frame_keys(gpu, mem)).collect()
}
pub(crate) fn next_pixels(&mut self, pixels: &Tensor) -> Result<TrackedFrame> {
ensure!(self.next_frame < self.num_frames, "the video has {} frames", self.num_frames);
let (m, gpu) = (self.model, &self.model.gpu);
let t = self.next_frame;
let feats = self.model.video_features(pixels)?;
let max_abs_pos = self.num_frames.min(MAX_OBJ_PTRS);
let sel = self.selection()?;
let ptr_tokens = sel.ptr_tokens();
let mem_tokens = sel.frames.len() * TOKENS + ptr_tokens;
let mut inputs =
if gpu.precision() == Precision::Bf16 { None } else { Some(self.memory_inputs()?.objects.into_iter()) };
let start = if inputs.is_none() {
let _r = range(c"memory_attention");
Some(m.tracker.mem_attn.frame_start(gpu, &feats.coarse)?)
} else {
None
};
let side = LOW_RES * LOW_RES;
let low_res = gpu.empty(&[self.objects, side])?;
let (mut scores, mut ptrs, mut mems, mut picks) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
let (mut norm_sum, mut iou_sum) = (0f32, 0f32);
for b in 0..self.objects {
let embed = {
let _r = range(c"memory_attention");
match inputs.as_mut().and_then(Iterator::next) {
Some((memory, memory_pos)) => {
m.tracker.mem_attn.forward(gpu, &feats.coarse, &memory, &memory_pos, ptr_tokens)?
}
None => self.memory_attention_cached(&sel, b, start.as_ref().expect("BF16 frame start"))?,
}
};
let _r = range(c"decoder");
let dec = m.point.decode(gpu, &embed, &feats.point, &[], (self.width, self.height), None, 1)?;
let score = gpu.download(&m.point.obj_score(gpu, &dec)?)?[0];
let iou = gpu.download(&dec.iou)?;
let best = 1 + argmax(&iou[1..]);
let present = score > 0.0;
self.select_mask(&dec, best, present, &low_res, b)?;
ptrs.push(m.tracker.pointer(gpu, &row(gpu, &dec.tokens, MASK_TOKEN_ROW + best)?, present)?);
let _r = range(c"memory_encoder");
let high = upsample(gpu, Arg::slice(low_res.at(b * side), side), 1, IMAGE_SIZE, IMAGE_SIZE)?;
let input = mask_for_mem(gpu, &high, false)?;
mems.push(m.tracker.memory(gpu, &feats.coarse, &input, present)?);
norm_sum += if present { 2.0 / (1.0 + (-score).exp()) - 1.0 } else { 0.0 };
iou_sum += iou[best];
scores.push(score);
picks.push((iou, best));
}
let n = self.objects as f32;
let eff = (norm_sum / n) * (iou_sum / n);
drop(sel);
let keys = self.frame_keys(&mems)?;
let frame = MemFrame { frame: t, mem: mems, ptr: ptrs, keys };
self.prev = None;
if eff > MEMORY_SCORE_THRESHOLD {
self.passing.push_back(frame);
if self.passing.len() >= max_abs_pos {
self.passing.pop_front();
}
let window = self.passing.len().saturating_sub(NUM_MASKMEM - 1);
for f in self.passing.iter_mut().take(window) {
f.keys.clear();
}
} else {
self.prev = Some(frame);
}
self.next_frame += 1;
self.output(t, low_res, &scores, picks, mem_tokens, ptr_tokens)
}
fn output(
&self,
frame: usize,
low_res: Tensor,
scores: &[f32],
picks: Vec<(Vec<f32>, usize)>,
mem_tokens: usize,
ptr_tokens: usize,
) -> Result<TrackedFrame> {
let gpu = &self.model.gpu;
let _r = range(c"postprocess");
let (w, h) = (self.width, self.height);
let resize = MaskResize::new(gpu, self.objects, w, h)?;
let plane = w * h;
let mask = gpu.alloc_bytes(resize.group() * plane)?;
let mut masks = Vec::with_capacity(self.objects);
for range in resize.groups() {
let n = range.len() * plane;
resize.run(gpu, &low_res, range, Arg::slice(mask.ptr(), n))?;
let mut bytes = vec![0u8; n];
gpu.download_into(&mask, &mut bytes)?;
masks.extend(bytes.chunks_exact(plane).map(<[u8]>::to_vec));
}
let logits = gpu.download(&low_res)?;
let objects = scores
.iter()
.zip(masks)
.zip(logits.chunks(LOW_RES * LOW_RES))
.zip(picks)
.map(|(((&score, mask), low_res), (ious, chosen))| TrackedObject {
score,
mask,
low_res: low_res.to_vec(),
ious,
chosen,
})
.collect();
Ok(TrackedFrame { frame, objects, memory_tokens: mem_tokens, pointer_tokens: ptr_tokens })
}
}
fn upsample(gpu: &Gpu, low_res: Arg, n: usize, h: usize, w: usize) -> Result<Tensor> {
let out = gpu.empty(&[n, h * w])?;
launch!(gpu, "resize_bilinear", ew(out.len()), B256, 0; &out, low_res, LOW_RES, LOW_RES, h, w)?;
Ok(out)
}
fn stability(logits: &[f32]) -> f32 {
let inter = logits.iter().filter(|&&v| v > 0.05).count();
let union = logits.iter().filter(|&&v| v > -0.05).count();
if union > 0 { inter as f32 / union as f32 } else { 1.0 }
}
fn cap_clicks(points: &[PointPrompt]) -> Vec<PointPrompt> {
if points.len() <= MAX_CLICKS {
return points.to_vec();
}
let first = MAX_CLICKS / 2;
points[..first].iter().chain(&points[points.len() - (MAX_CLICKS - first)..]).copied().collect()
}