mlboydaisuke commited on
Commit
82972af
·
verified ·
1 Parent(s): 09e139d

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +101 -0
README.md ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ library_name: litert
4
+ pipeline_tag: image-feature-extraction
5
+ tags:
6
+ - litert
7
+ - tflite
8
+ - sam2
9
+ - segment-anything
10
+ - image-encoder
11
+ - on-device
12
+ - gpu
13
+ base_model: facebook/sam2.1-hiera-tiny
14
+ ---
15
+
16
+ # SAM 2.1 (Hiera-Tiny) image encoder — LiteRT GPU
17
+
18
+ On-device **LiteRT / TFLite** conversion of the **image encoder** of
19
+ [**SAM 2.1 Hiera-Tiny**](https://huggingface.co/facebook/sam2.1-hiera-tiny) (Meta, Apache-2.0),
20
+ running **fully on the mobile GPU** via the LiteRT `CompiledModel` API (ML Drift / `LITERT_CL` delegate).
21
+ The whole graph is GPU-resident — no CPU/XNNPACK fallback ops.
22
+
23
+ This is the heavy backbone of the Segment Anything 2 image path: it turns an RGB image into the
24
+ multi-scale feature pyramid that a (small) prompt-encoder + mask-decoder then query per click/box.
25
+
26
+ | | |
27
+ |---|---|
28
+ | Task | Image encoder for promptable segmentation (SAM 2 image path) |
29
+ | Backbone | Hiera-Tiny (hierarchical ViT, window + global attention) + FPN neck |
30
+ | Input | `[1, 3, 1024, 1024]` NCHW float32, ImageNet-normalized |
31
+ | Outputs | 3 FPN feature maps: `[1,256,256,256]`, `[1,256,128,128]`, `[1,256,64,64]` |
32
+ | Precision / size | FP16, **80 MB** |
33
+ | Device | Pixel 8a, LiteRT GPU (`Accelerator.GPU`), **~7 ms / image** |
34
+ | Residency | **`Replacing 862 out of 862 node(s) with delegate (LITERT_CL)`** (full, single partition) |
35
+
36
+ ## Preprocessing (must match)
37
+
38
+ ```
39
+ resize to 1024x1024 (bilinear) -> x/255 -> (x - mean) / std
40
+ mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225] # ImageNet, RGB, NCHW
41
+ ```
42
+
43
+ ## GPU-clean conversion (what was re-authored)
44
+
45
+ Converted with `litert-torch`. SAM 2's Hiera encoder is not GPU-clean out of the box; these exact,
46
+ weights-faithful rewrites were applied (model-side only — **no converter patch**):
47
+
48
+ 1. **`window_partition` / `window_unpartition`**: the 6-D `view`+`permute` window reshape rejected by the
49
+ GPU delegate (>4-D) is re-expressed as a sequence of **≤4-D** `reshape`/`transpose` ops (numerically
50
+ exact, verified vs the original).
51
+ 2. **`Sam2MultiScaleAttention`**: the 5-D fused-QKV reshape is decomposed into separate q/k/v, and
52
+ attention runs as a **3-D batched SDPA** (`[B*heads, N, d]`). A 4-D SDPA makes the delegate emit a
53
+ `[C,C]->[nW,ws,C,C]` `BROADCAST_TO` on every windowed block; the 3-D form removes all 9.
54
+ 3. **Windowed positional embedding**: the bicubic-interpolate + tile of the constant `pos_embed` is
55
+ **baked to a buffer** (add only) — removes a runtime interpolate of a constant.
56
+ 4. **Neck**: the (constant, shape-only) sine FPN position encodings are dropped from the graph (compute
57
+ them host-side) — removes the remaining `BROADCAST_TO` ops.
58
+ 5. **Overflow-safe LayerNorm** (scale-before-square) as an fp16 safety margin for the deep stages.
59
+
60
+ Net: `banned ops = NONE`, `>4-D tensors = 0`, full GPU residency.
61
+
62
+ ## Fidelity (honest)
63
+
64
+ Eager re-authoring is **numerically exact** (`cos = 1.000`, `mae = 0`). On-device GPU output vs the
65
+ CPU reference, per FPN level:
66
+
67
+ | Output | cosine |
68
+ |---|---|
69
+ | FPN-0 `256x256` (high-res, drives mask detail) | **0.99998** |
70
+ | FPN-1 `128x128` | **0.99994** |
71
+ | FPN-2 `64x64` (coarse image embedding) | **0.99253** |
72
+
73
+ The deepest 64×64 feature drifts slightly on the GPU. This is **not** LayerNorm overflow
74
+ (scale-before-square LayerNorm doesn't change it, and the CPU fp16 model matches PyTorch fp32 at
75
+ corr 0.999999) — it is the mobile GPU computing the deep-stage global attention (64×64 = 4096 tokens)
76
+ in true fp16, where the CPU path upcasts to fp32. The high-resolution features that carry mask
77
+ boundaries are near-exact, so mask quality is preserved in practice.
78
+
79
+ ## Usage (Android / LiteRT CompiledModel)
80
+
81
+ ```kotlin
82
+ val model = CompiledModel.create(context.assets, "sam2_tiny_image_encoder_fp16.tflite",
83
+ CompiledModel.Options(Accelerator.GPU), null)
84
+ // input: [1,3,1024,1024] NCHW, ImageNet-normalized
85
+ // outputs: 3 FPN feature maps -> feed to the SAM 2 prompt encoder + mask decoder
86
+ ```
87
+
88
+ ## Training data & PII
89
+
90
+ SAM 2 was trained by Meta on **SA-1B** (licensed photos) and **SA-V** (licensed videos) with
91
+ model-in-the-loop mask annotation. No new training was performed for this conversion — it is a
92
+ weights-faithful format change of the public `facebook/sam2.1-hiera-tiny` checkpoint. Because the
93
+ source data is real-world imagery, it may incidentally contain people, faces, vehicles, signage and
94
+ other PII; no PII was deliberately collected and this conversion adds none. Apply your own content/PII
95
+ filtering as appropriate. See the [SAM 2 release](https://github.com/facebookresearch/sam2) and
96
+ [paper](https://arxiv.org/abs/2408.00714) for full dataset details.
97
+
98
+ ## License
99
+
100
+ Apache-2.0, inherited from the upstream [SAM 2.1](https://huggingface.co/facebook/sam2.1-hiera-tiny).
101
+ This is a format conversion; all credit to the original authors (Meta AI).