rlx-sam
Meta's Segment Anything (SAM v1) for RLX. Loads the official sam_vit_{b,l,h} safetensors (candle / Meta key layout, no remapping), builds an RLX IR graph for the ViT image encoder + neck, then runs the prompt encoder, two-way transformer, and mask decoder to turn point/box prompts into masks.
Quick start
# CPU (default features)
# or directly:
# Metal / MLX / CUDA … (pick the matching feature)
CLI flags: --weights, --device (cpu|metal|mlx|cuda|rocm|gpu|vulkan|tpu), --point X,Y, --dry. The encoder variant is selected with RLX_SAM_VARIANT=vit_b|vit_l|vit_h (default vit_b). The rlx-sam-batch bin runs the same model over a batch.
Public API
use ;
let mut sam = from_safetensors_on?;
// rgb: row-major [H*W*3] u8; one positive point prompt at (cx, cy).
let = sam.forward?;
// pred.mask_logits: [num_masks, mask_side, mask_side] (threshold > 0 for binary)
// pred.iou_pred: [num_masks] self-estimated quality
println!;
# Ok
Key exports (see src/lib.rs): [Sam] / [MaskPrediction], the graph builder [build_sam_encoder_graph], and the staged pieces [preprocess_image], [prompt_encoder_forward], [two_way_transformer_forward], [mask_decoder_forward]. Config constants and SamConfig / SamEncoderConfig / SamDecoderConfig live in src/config.rs.
How it fits
| Crate | Relationship |
|---|---|
| rlx-sam-ir | shared mask-decoder IR (two-way transformer, hyper matmul, ReLU MLP) |
| rlx-sam2 | SAM 2 (Hiera) — reuses this crate's compile profiles |
| rlx-sam3 | SAM 3 (segment with concepts) — depends on this crate |
Tests
Real-weight numerical parity vs candle's ImageEncoderViT::forward() lives in tests/sam_parity.rs (max |Δ| ≈ 7e-6 on the 1×256×64×64 image embeddings for ViT-B at 1024×1024); the RLX_SAM_DEBUG_* env vars there bisect the encoder.