mlboydaisuke commited on
Commit
82ad263
·
verified ·
1 Parent(s): 5102c8d

SAM 3 LiteRT: image graphs + tracker graphs, tokenizer, card

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ assets/hero_person.png filter=lfs diff=lfs merge=lfs -text
37
+ assets/street_car.png filter=lfs diff=lfs merge=lfs -text
LICENSE ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SAM License
2
+ Last Updated: November 19, 2025
3
+
4
+ “Agreement” means the terms and conditions for use, reproduction, distribution and modification of the SAM Materials set forth herein.
5
+
6
+
7
+ “SAM Materials” means, collectively, Documentation and the models, software and algorithms, including machine-learning model code, trained model weights, inference-enabling code, training-enabling code, fine-tuning enabling code, and other elements of the foregoing distributed by Meta and made available under this Agreement.
8
+
9
+ “Documentation” means the specifications, manuals and documentation accompanying
10
+ SAM Materials distributed by Meta.
11
+
12
+
13
+ “Licensee” or “you” means you, or your employer or any other person or entity (if you are entering into this Agreement on such person or entity’s behalf), of the age required under applicable laws, rules or regulations to provide legal consent and that has legal authority to bind your employer or such other person or entity if you are entering in this Agreement on their behalf.
14
+
15
+
16
+ “Meta” or “we” means Meta Platforms Ireland Limited (if you are located in or, if you are an entity, your principal place of business is in the EEA or Switzerland) or Meta Platforms, Inc. (if you are located outside of the EEA or Switzerland).
17
+
18
+
19
+ “Sanctions” means any economic or trade sanctions or restrictions administered or enforced by the United States (including the Office of Foreign Assets Control of the U.S. Department of the Treasury (“OFAC”), the U.S. Department of State and the U.S. Department of Commerce), the United Nations, the European Union, or the United Kingdom.
20
+
21
+
22
+ “Trade Controls” means any of the following: Sanctions and applicable export and import controls.
23
+
24
+ By using or distributing any portion or element of the SAM Materials, you agree to be bound by this Agreement.
25
+
26
+
27
+ 1. License Rights and Redistribution.
28
+
29
+
30
+ a. Grant of Rights. You are granted a non-exclusive, worldwide, non-transferable and royalty-free limited license under Meta’s intellectual property or other rights owned by Meta embodied in the SAM Materials to use, reproduce, distribute, copy, create derivative works of, and make modifications to the SAM Materials.
31
+
32
+ b. Redistribution and Use.
33
+ i. Distribution of SAM Materials, and any derivative works thereof, are subject to the terms of this Agreement. If you distribute or make the SAM Materials, or any derivative works thereof, available to a third party, you may only do so under the terms of this Agreement and you shall provide a copy of this Agreement with any such SAM Materials.
34
+
35
+
36
+ ii. If you submit for publication the results of research you perform on, using, or otherwise in connection with SAM Materials, you must acknowledge the use of SAM Materials in your publication.
37
+
38
+
39
+ iii. Your use of the SAM Materials must comply with applicable laws and regulations, including Trade Control Laws and applicable privacy and data protection laws.
40
+ iv. Your use of the SAM Materials will not involve or encourage others to reverse engineer, decompile or discover the underlying components of the SAM Materials.
41
+ v. You are not the target of Trade Controls and your use of SAM Materials must comply with Trade Controls. You agree not to use, or permit others to use, SAM Materials for any activities subject to the International Traffic in Arms Regulations (ITAR) or end uses prohibited by Trade Controls, including those related to military or warfare purposes, nuclear industries or applications, espionage, or the development or use of guns or illegal weapons.
42
+ 2. User Support. Your use of the SAM Materials is done at your own discretion; Meta does not process any information nor provide any service in relation to such use. Meta is under no obligation to provide any support services for the SAM Materials. Any support provided is “as is”, “with all faults”, and without warranty of any kind.
43
+
44
+
45
+ 3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE SAM MATERIALS AND ANY OUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, AND META DISCLAIMS ALL WARRANTIES OF ANY KIND, BOTH EXPRESS AND IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE SAM MATERIALS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE SAM MATERIALS AND ANY OUTPUT AND RESULTS.
46
+
47
+ 4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY DIRECT OR INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF META OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING.
48
+
49
+ 5. Intellectual Property.
50
+
51
+
52
+ a. Subject to Meta’s ownership of SAM Materials and derivatives made by or for Meta, with respect to any derivative works and modifications of the SAM Materials that are made by you, as between you and Meta, you are and will be the owner of such derivative works and modifications.
53
+
54
+ b. If you institute litigation or other proceedings against Meta or any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the SAM Materials, outputs or results, or any portion of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable by you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold harmless Meta from and against any claim by any third party arising out of or related to your use or distribution of the SAM Materials.
55
+
56
+ 6. Term and Termination. The term of this Agreement will commence upon your acceptance of this Agreement or access to the SAM Materials and will continue in full force and effect until terminated in accordance with the terms and conditions herein. Meta may terminate this Agreement if you are in breach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete and cease use of the SAM Materials. Sections 3, 4 and 7 shall survive the termination of this Agreement.
57
+
58
+ 7. Governing Law and Jurisdiction. This Agreement will be governed and construed under the laws of the State of California without regard to choice of law principles, and the UN Convention on Contracts for the International Sale of Goods does not apply to this Agreement. The courts of California shall have exclusive jurisdiction of any dispute arising out of this Agreement.
59
+
60
+
61
+ 8. Modifications and Amendments. Meta may modify this Agreement from time to time; provided that they are similar in spirit to the current version of the Agreement, but may differ in detail to address new problems or concerns. All such changes will be effective immediately. Your continued use of the SAM Materials after any modification to this Agreement constitutes your agreement to such modification. Except as provided in this Agreement, no modification or addition to any provision of this Agreement will be binding unless it is in writing and signed by an authorized representative of both you and Meta.
README.md ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: sam-license
4
+ license_link: LICENSE
5
+ base_model: facebook/sam3
6
+ tags:
7
+ - litert
8
+ - tflite
9
+ - android
10
+ - ios
11
+ - on-device
12
+ - open-vocabulary
13
+ - object-detection
14
+ - instance-segmentation
15
+ - segment-anything
16
+ pipeline_tag: image-segmentation
17
+ ---
18
+
19
+ # SAM 3 — LiteRT (on-device, GPU)
20
+
21
+ ![SAM 3 on-device: "person"](assets/hero_person.png)
22
+
23
+ **SAM 3** (Meta, [`facebook/sam3`](https://github.com/facebookresearch/sam3)) running fully
24
+ on-device with the LiteRT **CompiledModel** API: open-vocabulary, text-prompted detection +
25
+ instance segmentation. Type *"wheel"*, *"paper bag"*, *"person"* — any phrase — and get every
26
+ match's box, score, and 288×288 instance mask. No server, no cloud.
27
+
28
+ - **ViT-L/14 trunk @1008²** + tri-neck → **CLIP-L text encoder** → **text-conditioned DETR
29
+ head** (200 queries, presence token) — ~830 M params.
30
+ - Verified on a **Pixel 8a** (vision GPU 9.2 s / text CPU 0.5 s / head GPU 1.4 s; re-prompt
31
+ 1.9 s; kept-set equal to PyTorch fp32, mask IoU ≥ 0.98) and on **Apple Metal** (exact with
32
+ `enforce_f32`).
33
+ - Every graph re-authoring is **exact** (corr 1.0 vs PyTorch) — no approximations.
34
+
35
+ ![SAM 3 on-device: "car"](assets/street_car.png)
36
+
37
+ ## Files
38
+
39
+ | File | Size | Role | Accelerator |
40
+ | ---- | ---- | ---- | ----------- |
41
+ | `sam3_vision.tflite` | 930 MB (fp16) | image `[1,3,1008,1008]` → `fpn288 \| fpn144 \| fpn72` | GPU |
42
+ | `sam3_text.tflite` | 607 MB (fp16) | token embeddings `[1,32,1024]` → text memory `[32·256]` | **CPU** (see note) |
43
+ | `sam3_head.tflite` | 68 MB (fp16) | `[fpn×3 \| text_mem \| pad]` → 200 logits + boxes cxcywh + presence + 200×288² mask logits | GPU |
44
+ | `sam3_token_embed.bin` | 101 MB | fp16 `[49408×1024]` token-embedding table (host lookup) | host |
45
+ | `tokenizer/vocab.json`, `tokenizer/merges.txt` | 2 MB | CLIP byte-level BPE (ctx 32, BOS 49406, EOT 49407, zero-pad) | host |
46
+ | `tracker/*.tflite` | 1.0 GB | video tracker graphs (shared trunk + memory attention + decoders), see below | GPU |
47
+
48
+ **Requires LiteRT ≥ 2.2.0** (2.1.5 mis-executes the head graph on Android GPU).
49
+ **Why text runs on CPU:** the CLIP-L residual stream reaches |x|≈1.2e3; fp16 GPU execution
50
+ corrupts some prompt embeddings. CPU is exact and takes ~0.5 s (on Apple Metal you can use
51
+ `enforce_f32` instead).
52
+
53
+ ## Usage (Kotlin, CompiledModel)
54
+
55
+ ```kotlin
56
+ val vision = CompiledModel.create(visionPath, CompiledModel.Options(Accelerator.GPU), null)
57
+ val text = CompiledModel.create(textPath, CompiledModel.Options(Accelerator.CPU), null)
58
+ val head = CompiledModel.create(headPath, CompiledModel.Options(Accelerator.GPU), null)
59
+
60
+ // image -> features (once per image; cache to re-prompt instantly)
61
+ visIn[0].writeFloat(preprocess(bitmap)) // 1008x1008, (x/255-0.5)/0.5, NCHW
62
+ vision.run(visIn, visOut)
63
+ val feats = visOut[0].readFloat()
64
+
65
+ // prompt -> text memory (host BPE + fp16 table lookup, then the text graph)
66
+ val ids = bpe.encode("wheel") // [BOS, ..., EOT, 0-pad] x32
67
+ textIn[0].writeFloat(lookupEmbeddings(ids)) // [1,32,1024]
68
+ text.run(textIn, textOut)
69
+
70
+ // features + text -> detections
71
+ headIn[0].writeFloat(feats + textOut[0].readFloat() + padMask(ids))
72
+ head.run(headIn, headOut)
73
+ val y = headOut[0].readFloat()
74
+ val presence = sigmoid(y[1000])
75
+ // query q kept if sigmoid(y[q]) * presence > 0.5; box y[200+4q..], mask y[1001+q*288*288..]
76
+ ```
77
+
78
+ ## Usage (Python, CompiledModel)
79
+
80
+ ```python
81
+ import numpy as np
82
+ from ai_edge_litert.compiled_model import CompiledModel
83
+ from ai_edge_litert.hardware_accelerator import HardwareAccelerator
84
+
85
+ vision = CompiledModel.from_file("sam3_vision.tflite", HardwareAccelerator.GPU)
86
+ text = CompiledModel.from_file("sam3_text.tflite", HardwareAccelerator.CPU)
87
+ head = CompiledModel.from_file("sam3_head.tflite", HardwareAccelerator.GPU)
88
+
89
+ def run(model, x, n_out):
90
+ ib, ob = model.create_input_buffers(0), model.create_output_buffers(0)
91
+ ib[0].write(np.ascontiguousarray(x, np.float32).ravel())
92
+ model.run_by_index(0, ib, ob)
93
+ return np.array(ob[0].read(n_out, np.float32))
94
+
95
+ feats = run(vision, image_1008, 256 * (288**2 + 144**2 + 72**2)) # (x/255-0.5)/0.5, NCHW
96
+ table = np.fromfile("sam3_token_embed.bin", np.float16).reshape(-1, 1024)
97
+ emb = table[token_ids].astype(np.float32) # CLIP BPE, ctx 32, 0-pad
98
+ mem = run(text, emb[None], 32 * 256)
99
+ pad = (np.array(token_ids) == 0).astype(np.float32)
100
+ y = run(head, np.concatenate([feats, mem, pad]), 1001 + 200 * 288 * 288)
101
+ prob = 1 / (1 + np.exp(-y[:200])) / (1 + np.exp(-y[1000]))
102
+ keep = np.where(prob > 0.5)[0] # boxes y[200:1000], masks y[1001:]
103
+ ```
104
+
105
+ ## Video tracker (stage 2)
106
+
107
+ `tracker/` holds the Object-Multiplex tracker graphs sharing one trunk pass:
108
+ `sam3_vision_tri.tflite` (trunk + detector/interactive/propagation necks),
109
+ `trk_memattn_n7.tflite` (memory attention, 7 spatial slots + 16 pointer frames),
110
+ `trk_maskdec.tflite` (16-object multiplex decoder), `trk_memenc.tflite`,
111
+ `trk_initdec.tflite`. The host state machine (detection↔track association, hotstart,
112
+ recondition, memory bank + temporal pos-enc) is ported and verified against the official
113
+ model (ids identical, mask IoU ≥ 0.992) — the executable spec and the Kotlin/Swift ports
114
+ live in the **LiteRT-Models** zoo sample (`sam3/`), together with all conversion scripts.
115
+
116
+ ## Conversion notes
117
+
118
+ Converted with **litert-torch**; every GPU-compatibility re-authoring is exact: the >4-D
119
+ ViT attention (silently mis-lowered otherwise) is rebuilt in ≤4-D with the interleaved RoPE
120
+ baked into the qkv weights; SafeLayerNorm handles the |x|≈300 residual stream; the DETR
121
+ decoder is batch-first rank-4 end-to-end (rank-3 `[1,N,C]` fan-outs mis-execute on mobile
122
+ GPUs); masked softmax uses the delegate-safe form; ConvTranspose necks are zero-stuff +
123
+ Conv2d. Details and the on-device debugging record ship with the sample app.
124
+
125
+ ## License
126
+
127
+ SAM Materials, © Meta Platforms — redistributed under the **SAM License** (see `LICENSE`,
128
+ provided with these materials as the license requires). Built with SAM.
assets/hero_person.png ADDED

Git LFS Details

  • SHA256: 10a2342586f958f5ea8d618b69e8ce6ea829e71da172e838b5c2e633581a6725
  • Pointer size: 132 Bytes
  • Size of remote file: 2.16 MB
assets/street_car.png ADDED

Git LFS Details

  • SHA256: 3ffbaab052d8849237674e138e514171aceba1652a6144c6931e52483d81e37d
  • Pointer size: 132 Bytes
  • Size of remote file: 2 MB
sam3_head.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a178a77caeca416ede15edae85603418052d8a79389a095c63548bf8e801e6a0
3
+ size 68374240
sam3_text.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9fd12dc4dc9b61d78c8b46337d5b83702e590eabf1e75eb49453fabb5a35143e
3
+ size 606581568
sam3_token_embed.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ec41288e6dc55129441cf933f7646daff180d6e34495e833b71f3dabd7b1a4c7
3
+ size 101187584
sam3_vision.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ec67cd249634dac82b8d54f71d19081f35805b2d1352d6e4ef2fef804a341cab
3
+ size 930244512
sha256_local.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ a178a77caeca416ede15edae85603418052d8a79389a095c63548bf8e801e6a0 sam3_head.tflite
2
+ 9fd12dc4dc9b61d78c8b46337d5b83702e590eabf1e75eb49453fabb5a35143e sam3_text.tflite
3
+ ec67cd249634dac82b8d54f71d19081f35805b2d1352d6e4ef2fef804a341cab sam3_vision.tflite
4
+ ec41288e6dc55129441cf933f7646daff180d6e34495e833b71f3dabd7b1a4c7 sam3_token_embed.bin
5
+ 778f309453f97cee1e2027961dab02b18610696962f17639dc200ddf6fd0c4b8 tracker/sam3_vision_tri.tflite
6
+ b1e49cfa7602ec8dd199d0d85ad2c439cda4dd16ddb2efa66c8e90958ba1ea98 tracker/trk_initdec.tflite
7
+ 02e1874c2da39aab52a28f79d41c955672c96f21bc717bbf49c16af312996073 tracker/trk_maskdec.tflite
8
+ beaefb131e15493055fecb3a277daaec96e8c18bbeb2a533ccb99ac2891b5fab tracker/trk_memattn_n7.tflite
9
+ 011b348691f9f9cb85926ce21066ef4c15391521338b8129c00c2e34542bd220 tracker/trk_memenc.tflite
tokenizer/merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer/vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
tracker/sam3_vision_tri.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:778f309453f97cee1e2027961dab02b18610696962f17639dc200ddf6fd0c4b8
3
+ size 958172592
tracker/trk_initdec.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1e49cfa7602ec8dd199d0d85ad2c439cda4dd16ddb2efa66c8e90958ba1ea98
3
+ size 14066192
tracker/trk_maskdec.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:02e1874c2da39aab52a28f79d41c955672c96f21bc717bbf49c16af312996073
3
+ size 13875568
tracker/trk_memattn_n7.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:beaefb131e15493055fecb3a277daaec96e8c18bbeb2a533ccb99ac2891b5fab
3
+ size 25138208
tracker/trk_memenc.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:011b348691f9f9cb85926ce21066ef4c15391521338b8129c00c2e34542bd220
3
+ size 7937376