Skip to content

feat: Port Sapiens2 (sapiens2) human pose and dense human prediction #2371

Closed

Description

@inureyes

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 from architectures (Sapiens2ForPoseEstimation, segmentation, normal, Sapiens2ForPointmapEstimation, Sapiens2ForImageMatting; plain Sapiens2Model is the backbone), head_config for the deconv or pixel-shuffle head and the pointmap scale branch.
  • heads.py (DeconvHead, PixelShuffleHead), sapiens2.py (Model, qkv merge in sanitize), pose.py (top-down crop with bbox_xyxy2cs padding 1.25 and aspect fix, warp_to_input via upstream kernels.grid_sample, Gaussian blur, DARK-UDP sub-pixel decode udp_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=True by default).
  • Checkpoints: mlx-community/sapiens2-{pose,seg,normal,pointmap}-{0.4b,0.8b,1b,5b}-bf16, mlx-community/sapiens2-matting-1b-bf16.

Current Behavior

Proposed Solution

  • New src/vision/dense/sapiens2/ (config.rs, backbone.rs, heads.rs, pose.rs, processor.rs, model.rs, mod.rs, tests beside each) implementing DensePredictor, registered as sapiens2 in 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's architectures, so --task is not needed.
  • Outputs per task, matching upstream keys: pose writes keypoints.json (308 points per box, source-pixel x, y, score, keypoint names from the config when present) plus keypoints, scores, boxes tensors; seg writes segmentation (H, W) int32 and segmentation.png; normal writes normals (H, W, 3) and normal.png; pointmap writes pointmaps (H, W, 3) and scales (1,); matting writes alphas (H, W) and foregrounds (H, W, 3); backbone checkpoints write last_hidden_state and pooler_output.
  • Pose options through --opts: boxes (list of xyxy in source pixels; default one full-image box) and flip_test (default true).
  • Add a grid_sample helper, a port of upstream kernels.grid_sample keeping its exact coordinate and padding convention, to src/vision/dense/interpolate.rs with // 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.rs attention and RMSNorm primitives (update // Used by: if a shared function changes).
  • Conv weights: patch embed and head convs (including ConvTranspose2d in deconv heads) need the double-permute guard; the qkv merge from sanitize must 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 boxes list returns no keypoints rather than an error; images of other sizes are resized to the configured image_size and outputs mapped back.

Acceptance Criteria

  • mlxcel predict -m models/sapiens2-pose-0.4b-bf16 --image person.jpg -o out/ writes keypoints.json with 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_test on and off; segmentation label agreement at least 99.5% of pixels; normals and pointmaps relative RMS below 1e-2.
  • Unit tests: bbox_xyxy2cs and aspect fix, DARK-UDP decode on a synthetic Gaussian heatmap recovering a known sub-pixel peak within 0.05 px, grid_sample against upstream literals, flip index pairs; each fails on current main.
  • mlxcel arch lists sapiens2; docs/supported-models.md updated.

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-out

Dependencies

Blocked by #2349 (dense output surface). Does not depend on DINOv2.

Activity

  1. added
    type:enhancement
    priority:low
    status:blocked
    area:models
    modeltype:vlm
    modelsize:medium
    on Oct 16, 2026
  2. added
    status:in-progress
    status:done
    and removed
    status:blocked
    status:in-progress
    on Oct 22, 2026
  3. added a commit that references this issue on Oct 22, 2026
    84d3629
  4. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:modelsmodelsize:mediummodeltype:vlmpriority:lowstatus:donetype:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions