Repository navigation
Description
Part of #2382
Problem / Background
Port Sapiens2 (sapiens2, Meta, ICLR 2026): high-resolution vision transformers pretrained on 1B human images for human-centric dense prediction. One backbone family (0.4B, 0.8B, 1B, 5B) carries five task heads: 308-keypoint pose, 29-part segmentation, surface normals, pointmaps and matting. The official HF repos (config.json with model_type: "sapiens2") load directly with no conversion. This is the pose and dense-human piece of the dense prediction request.
Upstream reference
- Model directory:
mlx_vlm/models/sapiens2(README). PR: Add port of Meta's Sapiens2 dense prediction model(s) Neywa-Labs/mlx-vlm#2283. backbone.py: its own ViT, not DINOv2 (patch 16, 8 register tokens, 2D RoPE with base 100, QK RMSNorm, grouped-query attention in the middle layers with full MHSA in the first and last 8, SwiGLU FFN, LayerScale; default 1536 x 40 at 1024x768).config.py: task selected fromarchitectures(Sapiens2ForPoseEstimation, segmentation, normal,Sapiens2ForPointmapEstimation,Sapiens2ForImageMatting; plainSapiens2Modelis the backbone),head_configfor the deconv or pixel-shuffle head and the pointmap scale branch.heads.py(DeconvHead,PixelShuffleHead),sapiens2.py(Model, qkv merge insanitize),pose.py(top-down crop withbbox_xyxy2cspadding 1.25 and aspect fix,warp_to_inputvia upstreamkernels.grid_sample, Gaussian blur, DARK-UDP sub-pixel decodeudp_decode_batch,keypoints_to_image, flip-test pairs),image.py(preprocessing with bicubic and bilinear resize),generate.py(Sapiens2Predictor.infer: dense outputs resized back to input resolution, pose keypoints mapped to source coordinates,boxes=for cropped inference defaulting to the full image,flip_test=Trueby default).- Checkpoints:
mlx-community/sapiens2-{pose,seg,normal,pointmap}-{0.4b,0.8b,1b,5b}-bf16,mlx-community/sapiens2-matting-1b-bf16.
Current Behavior
- No human-centric model in mlxcel;
model_typesapiens2is rejected byget_model_type(src/models/detection.rs:1149). mlxcel detect(RT-DETRv2,src/vision/detection/rt_detr_v2/) produces person boxes that could feed top-down pose, but there is no glue between the two. The dense CLI, interpolation and writer come from feat: Add dense-prediction output surface: mlxcel predict, map and keypoint files #2349.
Proposed Solution
- New
src/vision/dense/sapiens2/(config.rs,backbone.rs,heads.rs,pose.rs,processor.rs,model.rs,mod.rs, tests beside each) implementingDensePredictor, registered assapiens2in the feat: Add dense-prediction output surface: mlxcel predict, map and keypoint files #2349 dense table and the registry family table. The task comes from the checkpoint'sarchitectures, so--taskis not needed. - Outputs per task, matching upstream keys:
posewriteskeypoints.json(308 points per box, source-pixel x, y, score, keypoint names from the config when present) pluskeypoints,scores,boxestensors;segwritessegmentation (H, W)int32 andsegmentation.png;normalwritesnormals (H, W, 3)andnormal.png;pointmapwritespointmaps (H, W, 3)andscales (1,);mattingwritesalphas (H, W)andforegrounds (H, W, 3); backbone checkpoints writelast_hidden_stateandpooler_output. - Pose options through
--opts:boxes(list of xyxy in source pixels; default one full-image box) andflip_test(default true). - Add a
grid_samplehelper, a port of upstreamkernels.grid_samplekeeping its exact coordinate and padding convention, tosrc/vision/dense/interpolate.rswith// Used by: Sapiens2.
Scope
In scope: all five tasks and the backbone, all sizes, docs rows in docs/dense-prediction.md and docs/supported-models.md.
Out of scope: automatic person detection chained from mlxcel detect (follow-up; users pass boxes), video and webcam streaming (Sapiens2Predictor.stream).
Implementation Notes
- Reuse: feat: Add dense-prediction output surface: mlxcel predict, map and keypoint files #2349 interpolation, preprocessing, output writer and conv-permutation guard;
src/lib/mlxcel-core/src/layers.rsattention and RMSNorm primitives (update// Used by:if a shared function changes). - Conv weights: patch embed and head convs (including
ConvTranspose2din deconv heads) need the double-permute guard; the qkv merge fromsanitizemust run before quantization-agnostic loading. - Precision: checkpoints are bf16 and non-quantized; the project rule converts them to f16 at load. The 5B backbone at 1024x768 is the overflow risk: if the parity test shows inf/NaN under f16, keep bf16 for the backbone and record it.
- Edge cases: a box outside the image is clipped; an empty
boxeslist returns no keypoints rather than an error; images of other sizes are resized to the configuredimage_sizeand outputs mapped back.
Acceptance Criteria
-
mlxcel predict -m models/sapiens2-pose-0.4b-bf16 --image person.jpg -o out/writeskeypoints.jsonwith 308 points in source-pixel coordinates. - Each task produces its outputs on its 0.4B (or 1B for matting) checkpoint.
- Parity on a fixed image: pose keypoints within 1.5 px of upstream with
flip_teston and off; segmentation label agreement at least 99.5% of pixels; normals and pointmaps relative RMS below 1e-2. - Unit tests:
bbox_xyxy2csand aspect fix, DARK-UDP decode on a synthetic Gaussian heatmap recovering a known sub-pixel peak within 0.05 px,grid_sampleagainst upstream literals, flip index pairs; each fails on current main. -
mlxcel archlistssapiens2;docs/supported-models.mdupdated.
Verification
cargo build --release --features metal,accelerate
cargo test --workspace --profile test-fast --features metal,accelerate sapiens2
cargo test --workspace --profile test-fast --features metal,accelerate
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
./target/release/mlxcel predict -m models/sapiens2-pose-0.4b-bf16 --image /path/to/person.jpg -o /tmp/sapiens2-outDependencies
Blocked by #2349 (dense output surface). Does not depend on DINOv2.
Activity
feat: port Sapiens2 dense prediction (#2550)