This is a guide for exporting and building native CoreML models to leverage Apple Silicon acceleration.
All commands below should be executed from the repository root directory (SAM3DBody-cpp/).
- Xcode Command Line Tools must be installed beforehand for model compilation (
xcrun). Usexcode-select --install. coremltools 9.0has been tested and verified up totorch==2.7.0.
conda create -n coreml_export python=3.11
conda activate coreml_export
conda install opencv=4.6.0
uv pip install -r coreml_export/requirements.txtThe conversion/export scripts depend on the original Python repository (AmmarkoV/Fast-SAM-3D-Body) and the PyTorch checkpoints.
# Clone the original Python repository
git clone --depth 1 https://github.com/AmmarkoV/Fast-SAM-3D-Body.git
# Download the PyTorch checkpoints
python -c "from huggingface_hub import snapshot_download; snapshot_download('facebook/sam-3d-body-dinov3', local_dir='coreml_export/checkpoints/sam-3d-body-dinov3')"make modelsAfter exporting, the models are automatically compiled into .mlmodelc using Xcode's coremlcompiler.
The C++ engine strictly requires the legacy OpenCV 4.6 API to maintain upstream compatibility. By default, the Makefile will use the coreml_export conda environment to resolve these dependencies.
make clean
make build./build/fast_sam_3dbody_run \
--from ../person.jpg \
--coreml-backbone coreml_export/checkpoints/backbone_coreml.mlmodelc \
--coreml-decoder coreml_export/checkpoints/decoder_coreml.mlmodelc \
--coreml-yolo coreml_export/checkpoints/yolo11m-pose.mlmodelc \
--headlessNote: The
--coreml-*argument family can be inherited by any other execution commands. For example, in the lightweight C++ webcam frontend or the offline BVH export pipeline, you can simply pass these CoreML paths in place of--onnx-dirto enable CoreML inference seamlessly.# Example: Running C++ webcam inference ./build/fast_sam_3dbody_run \ --onnx-dir ./onnx \ --gguf ./onnx/pipeline.gguf \ --coreml-backbone coreml_export/checkpoints/backbone_coreml.mlmodelc \ --coreml-decoder coreml_export/checkpoints/decoder_coreml.mlmodelc \ --coreml-yolo coreml_export/checkpoints/yolo11m-pose.mlmodelc \ --from 0 # Example: Offline multi-person BVH generation ./build/offline_sam_3dbody_render \ --from video.mp4 \ --bvh output.bvh \ --coreml-backbone coreml_export/checkpoints/backbone_coreml.mlmodelc \ --coreml-decoder coreml_export/checkpoints/decoder_coreml.mlmodelc \ --coreml-yolo coreml_export/checkpoints/yolo11m-pose.mlmodelc
(Tested on: Apple M1 Max, 10-core CPU, 32GB RAM)
The first frame takes around 700 ms due to ANE/GPU warmup, but in the steady state from the second frame onwards, you can expect an approximate latency of 400 ms / frame (2.5 fps).
| Module | CoreML Measured (Steady State) | ONNX Runtime (CUDA Baseline) |
|---|---|---|
| YOLOv11m | ~20 ms | ~15 ms |
| Backbone | ~360 ms | ~191 ms |
| Decoder | ~13 ms | ~8.6 ms |
- Decoder Batch Size Constraint: Apple's GPU (Metal) compiler (E5RT) does not permit complex tensor reshaping (e.g.
viewfor AttentionQ, K, V) containing dynamic dimensions. If dynamic batch sizes (RangeDim) are used, shape inference fails at compile time, causing a silent fallback to CPU execution which severely degrades performance. Therefore, the Decoder is strictly compiled with a static batch size ofB=1. At runtime, the C++ inference engine wraps theB=1model in anMLArrayBatchProviderto efficiently schedule and parallelize inference for multiple detections. - Backbone Computation Speed: The CoreML execution of the Backbone currently takes significantly longer compared to ONNX Runtime (CUDA). This may improve in the future if model graph optimizations or full ANE mapping are introduced.
- Export Script Options: The export process utilizes
torch.jit.tracewithFLOAT32precision to preserve the ATen computational graph, enabling thecoremltoolscompiler to successfully apply Scaled Dot Product Attention (SDPA) fusion. We intentionally omitFLOAT16casting during PyTorch export to bypass a known compiler hang incoremltoolswhen exporting complex Vision Transformer models.