File size: 9,781 Bytes
b7e49b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5afa0d
b7e49b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Serve Depth Pro 2 (`apple/DepthPro2-*`) on one Tenstorrent p150 with tt-model-manager

| item | value |
| --- | --- |
| package | `changh95/DepthPro2` (tt-model `name: depthpro2`, kind `tt-dit-server`, manifest schema 5.1) |
| weights (pointers, not redistributed) | primary `apple/DepthPro2-focal@622659d4`; auxiliary `apple/DepthPro2-S@903a1c1a`, `-B@85e67fdf`, `-L@849dc546`, `-H@68426c78` (Apple Machine Learning Research Model License: research purposes only) |
| serve profiles | `s`, `b`, `l` (default), `h`: each sets `DEPTHPRO2_SIZE`; one image serves all four |
| app | `tt_depthpro2.server.app:app` (FastAPI, uvicorn), port 20000 |
| device | one p150, ETH dispatch (12 x 10 grid), 1 command queue; tt-metal 44d6650 with the patches in `patches/` |
| engines (default) | encoder, decoder and focal-estimator trunk: persistent megakernels (`tt/mk_enc`, `tt/mk_dec`, `tt/mk_fov`); tt-nn fallbacks per stage (`tt/ttnn_enc`, `tt/ttnn_dec`, `tt/ttnn_fov`); unbuffered KV steps: tt-nn encoder (`tt/ttnn_kv`) + the decoder megakernel |

Apple Machine Learning Research Model is licensed under the Apple Machine Learning Research Model License Agreement.
This package is a third-party port and is not affiliated with or endorsed by Apple
(`code/tt_depthpro2/licenses/NOTICE.md`).

## 1. Run on the host (validation, no Docker)

```bash
source <workspace>/bin/tt-env.sh                     # tt-metal python_env (ttnn, torch 2.11.0+cpu) + fastapi / uvicorn
cd <this repo>/code
# optional: DEPTHPRO2_WEIGHTS_DIR=<a parent of DepthPro2-<size>/ and DepthPro2-focal/> (offline)
DEPTHPRO2_SIZE=L DEPTHPRO2_RESOLUTIONS=vga,vga_portrait \
    python -m uvicorn --host 127.0.0.1 --port 20000 --lifespan on tt_depthpro2.server.app:app
python3 tt_depthpro2/server/smoke_test.py --url http://127.0.0.1:20000 --wait 1800
```

On the shared workspace box every command that opens the device runs through the device lock
(`bin/devrun -t <s> -- ...`). The server logs `Loading weights`, `Opening device`, `Warming up` per bucket and clip
length, `Warmup complete`, then uvicorn's `Application startup complete`. `DEPTHPRO2_BACKEND=cpu` serves the float32
CPU reference instead of the device (slow; for testing the HTTP contract without a chip).

## 2. Package, serve and smoke with tt-model

```bash
cd <this repo>                                       # every tt-model command runs from here
sg docker -c "tt-model package --container tt-model.yaml --out <out>"      # hours cold (tt-metal C++ build)
# serve -> smoke -> stop, one profile at a time (a trap always stops the container), keeping the evidence:
bash code/scripts/container_smoke.sh <out>/depthpro2 20000 --profile l
tt-model push <out>/depthpro2 --publish                                    # HF_TOKEN from the environment only
```

`tt-model serve` downloads the weights of all four sizes and of the focal estimator (about 5.4 GB, once) whichever
profile it serves: tt-model fetches every weights pointer of a package. The container reads the commits from
`TT_AUXILIARY_WEIGHTS` / `TT_MODEL_WEIGHTS_REVISION` and the size from `DEPTHPRO2_SIZE`.

## 3. Request and response contract

| route | body | answer |
| --- | --- | --- |
| `GET /health`, `/v1/health` | | always 200: `{"status": "ok" | "starting" | "error", "device", "error"}` |
| `GET /info` | | model, size, weights + commits, device (dispatch, grid), engines, configuration (buckets, device sizes, FOV sizes, window, overlap, clip lengths, video modes), limits, warm-up latency per bucket and clip length, licence |
| `GET /config` | | the configuration and the limits |
| `GET /v1/models` | | a stub (`apple/DepthPro2-<size>`), not a chat API |
| `POST /predict` | JSON `{"image": <base64>, "f_px"?, "use_exif"?, "output_format"?: npy16 \| npy32 \| png16 \| none, "include_inverse_depth"?, "png_scale"?}` | depth (base64 `.npy` float16 by default), `inverse_depth` (optional), `focallength_px`, `focal_source`, `fov_deg`, `stats_m`, `input_size`, `bucket`, `device_size`, `resized`, `timing_ms` |
| `POST /predict_png` | the image file; query `f_px`, `png_scale` (1000), `use_exif` | `image/png` 16-bit grey, `uint16 = round(depth_m * png_scale)`, 0 = infinite; `X-Focal-Length-Px`, `X-Focal-Source`, `X-Depth-Scale`, `X-Shape`, `X-Bucket`, `X-Resized`, `X-Saturated-Pixels`, `X-Infinite-Pixels`, `X-Stats-M`, `X-Timing-Ms` |
| `POST /predict_video` | JSON `{"frames": [<base64>...] | "clip": {"format": "npy" \| "npz", "data"}, "mode"?: buffered \| clip \| unbuffered, "f_px"?, "window"?, "overlap"?, "stream"?: {"id", "end"}, "output_format"?: npy16 \| npy32 \| none, "include_inverse_depth"?}` | `frames`, `frame_indices`, `depth` `(n, H, W)`, `stats_m` per frame, `focallength_px`, `window`, `overlap`, `stream` state, `timing_ms` |

- Buckets: an input of exactly a bucket's size runs natively (replicate-padded to a multiple of 32 on the device, as
  Apple's code does); any other size is resized (bilinear, antialias) to the bucket of the nearest aspect ratio and
  the outputs are resized back.
- Focal length: `f_px` > the image's EXIF 35 mm-equivalent focal length (`use_exif`, files only) > the FOV estimate at
  the bucket, converted to input pixels. `depth = f_px / (W * inverse_depth)`, `inf` where the inverse depth is 0.
- Video `buffered`: Apple's `DepthStream` semantics (clips of `window` frames overlapping by `overlap`, linear
  cross-fade, one focal length per video from the first window); `unbuffered`: each frame attends a device
  key-value cache of the previous `window - 1` frames and is returned at once; `clip`: the frames as one clip (up to
  the window); `stream`: a buffered or unbuffered stream kept open across requests (at most
  `DEPTHPRO2_MAX_STREAMS`, unbuffered at most `DEPTHPRO2_KV_STREAMS`, closed after `DEPTHPRO2_STREAM_TTL_S` idle),
  each request returning the frames it finishes. Video files are not
  accepted (no decoder in the image: FFmpeg / PyAV would add a large, separately licensed decoder of untrusted input);
  `code/tt_depthpro2/server/client.py --video clip.mp4` decodes on the client (PyAV or the `ffmpeg` command).
- Errors: 400 with the reason (undecodable image, size out of range, `f_px` <= 0, frames of different sizes, too many
  frames, an uncompiled clip length, a disabled video mode, a body above `DEPTHPRO2_MAX_BODY_MB`), 422 schema
  violation, 429 too many open streams, 503 starting, 500 `inference failed: <Type>: <message>`.

## 4. Configuration (environment, read once at start; a bad value refuses to start)

| variable | default | meaning |
| --- | --- | --- |
| `DEPTHPRO2_SIZE` | `L` (profiles set it) | `S`, `B`, `L`, `H` |
| `DEPTHPRO2_RESOLUTIONS` | `vga,vga_portrait` | up to 8 buckets: `HxW` (32..4096 per side) or presets `qvga`, `vga`, `square`, `720p`, `1080p` (+ `_portrait`) |
| `DEPTHPRO2_WINDOW` | 16 | video clip length (buffered) / cache size (unbuffered), 1..16 |
| `DEPTHPRO2_OVERLAP` | 4 | buffered overlap, 0..window-1 |
| `DEPTHPRO2_CLIP_LENGTHS` | `auto` | clip lengths served besides 1 and the window (`all` or a list); the ttnn encoder serves every length up to the window on the window's program |
| `DEPTHPRO2_VIDEO_MODES` | `auto` | `buffered`, `unbuffered`, both, or `auto` = both when the unbuffered caches (buckets x streams) fit 4 GB of device DRAM (every size at the default buckets: S 217 MB ... H+ 1.99 GB) |
| `DEPTHPRO2_KV_STREAMS` | 1 | unbuffered streams open at once (a cache per bucket each, allocated at start; more answer 429) |
| `DEPTHPRO2_FOV` | 1 | load and warm the focal-length estimator |
| `DEPTHPRO2_ENGINE` | `enc=mk,dec=mk,fov=mk` | per stage `mk` (persistent megakernel) / `ttnn` (its tt-nn fallback, e.g. `dec=ttnn`); stages not named keep the default |
| `DEPTHPRO2_PRECISION` | device module default | `bf16` / `bfp8` (if the device module offers it) |
| `DEPTHPRO2_ENC_PRECISION` | `high` | encoder megakernel numerics: `high` (the megakernel's per-size defaults: on L, fp32 LayerNorm output into the QKV / FC1 matmuls and hi / lo prefix keys) or `fast` (the plain bfloat16 encoder everywhere, which changes L only; `high` costs L 5-18 % more encoder time); S, B, H+ and the focal-length estimator run the same either way |
| `DEPTHPRO2_DISPATCH`, `_NUM_CQS`, `_TRACE_REGION`, `_L1_SMALL` | `eth`, 1, sized from the configuration, 32 KB | device open (the trace region is estimated from the measured trace sizes of every bucket, clip length, FOV shape and KV step program unless `_TRACE_REGION` is set) |
| `DEPTHPRO2_WEIGHTS_DIR`, `_FOV_WEIGHTS_DIR` | the HF cache at the pinned commits | local snapshots (offline) |
| `DEPTHPRO2_MAX_BODY_MB`, `_MAX_FRAMES`, `_MAX_STREAMS`, `_STREAM_TTL_S`, `_TORCH_THREADS` | 256, 64, 4, 300, 4 | server limits |
| `TT_AUXILIARY_WEIGHTS`, `TT_MODEL_WEIGHTS_REVISION` | set by tt-model | the size repos' / focal repo's commits |

To serve another configuration from the package, edit the `docker run` line `tt-model serve --print` prints (for
example `DEPTHPRO2_RESOLUTIONS=720p,vga`) and run it; `GET /config` shows what runs. Every bucket and clip length is
a separate device program with its own start-up time and device memory.

## 5. Caveats

- Research use only (Apple's licence) and no safety-critical use.
- A clip shorter than the window runs on the window's program (full-window cost). Inputs that are not a bucket's
  size are resized to a bucket and back: configure the camera's native resolution for the most faithful results.
- tt-nn SDPA returned wrong rows above batch x heads 24; those attentions run in tt-nn's windowed mode (correct).
- One p150 (ETH dispatch: no multi-chip mesh); batch 1, requests serialised.
- Unbuffered video holds a device key-value cache per bucket and stream for the server's lifetime
  (`DEPTHPRO2_KV_STREAMS`); a stream beyond them answers 429.