Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / photos / src / main / assets / clip / README.md

README.md4.8 KiBRenderedCodeRaw
1# `photos/src/main/assets/clip/`
2
3TinyCLIP-ViT-8M/16 Text-3M, the semantic-search embedder `ClipEmbedder` runs.
4
5| file | what it is |
6| :--- | :--- |
7| `tinyclip.maml` | 22.6 MiB. Both towers in one file, read by `:library:ml`'s Vulkan runtime |
8| `bpe_simple_vocab_16e6.txt` | 3.0 MB. CLIP's BPE merge table, read by `ClipTokenizer` |
9
10## Provenance
11
12**`onnx-community/TinyCLIP-ViT-8M-16-Text-3M-YFCC15M-ONNX`**, revision
13`9463a9c508a344c837ffefe9d724f3827bf2dc79`, MIT — an ONNX export of
14`wkcn/TinyCLIP-ViT-8M-16-Text-3M-YFCC15M`, also MIT.
15
16Rebuild with:
17
18 python scripts/ml/fetch_tinyclip.py
19
20That downloads `onnx/model.onnx` (94 MB, **build host only**), checks its SHA-256 against the pin
21in that script, checks `config.json` and `preprocessor_config.json` against the architecture and
22the preprocessing the runtime hardcodes, and runs `scripts/ml/maml_convert.py --graph tinyclip`.
23The rebuild is byte-identical.
24
25Until this existed, TinyCLIP was the only bundled model in the tree with **no pinned upstream
26SHA-256 and no fetch script**. `SUPPLY_CHAIN_RISKS.md` named the repo in prose and nothing checked
27it.
28
29## Why the fp32 export and not the int8 one
30
31The asset this replaced was `onnx/model_int8.onnx` (24.3 MB) from the same repo, run on
32onnxruntime. It is not what the `.maml` is built from. Its 80 `MatMulInteger` weights are
33per-output-channel with zero zero-points, but three tensors are not:
34
35* `patch_embedding` has a **scalar** scale, over a kernel whose per-channel absmax spans 0.0018 to
36 0.117.
37* `token_embedding` is **uint8 with a zero point of 226**, and `position_embedding` **uint8 with
38 220**. `conv_int8.comp` computes `scale[o] * sum(int8_w * in)` and has nowhere to put a zero
39 point.
40
41Requantising those would add a second rounding to resolution that is already gone, so the converter
42reads the fp32 graph and quantises per output channel itself. Worst measured per-tensor correlation
43between the fp32 weight and `int8 * scale` is **0.99986**, over 82 quantised tensors.
44
45The fp32 file costs nothing in fidelity over any other source, and that is measured rather than
46assumed: **all 221 of its weight tensors are exactly fp16-representable**, so it was exported from
47an fp16 checkpoint and carries no information a half-precision one would not. It is also why
48`scripts/ml/onnx_parity.py` reports its "fp16 weights alone" bar as exactly zero for this graph —
49every difference the runtime shows is int8 quantisation.
50
51### Measured against the export, and against the file it replaces
52
53Cosine of the L2-normalised 512-d embeddings, on one deterministic image and one 8-token query
54(`scripts/ml/onnx_parity.py tinyclip` and `tinyclip_text`):
55
56| | this `.maml` vs fp32 | shipped `model_int8.onnx` vs fp32 | this vs shipped |
57| :--- | ---: | ---: | ---: |
58| image | **0.999864** | 0.998478 | 0.998311 |
59| text | **0.999394** | 0.982881 | 0.983471 |
60
61The port is *closer* to the fp32 reference than the file it replaces, and the text tower is where
62that gap is wide — 0.9994 against 0.9829 — because per-row int8 recovers most of what the shipped
63export threw away on the 49,408-row token table.
64
65The third column is why `ClipEmbedder.EMBEDDER_VERSION` is bumped: at 0.983 in the text tower, a
66library indexed by the old path and queried through the new one would rank noticeably worse than one
67indexed by either alone.
68
69The result is *smaller* than the file it replaces — 23,734,912 bytes against 24,281,512 — because
70the token embedding is int8 per row rather than uint8 per tensor and nothing is stored twice. The
71APK saves more than that, because the reduced `onnxruntime-reduced-android-1.27.0-r1.aar` contributed
72**10,466,856 bytes** of arm64 `.so` (about 3.4 MiB deflated), against the 797 KB `libmodelrunner.so`
73that was already there for face detection, subject segmentation and OCR. The measured `:photos`
74release APK went from 59.0 MiB to **53.8 MiB**.
75
76It contains **no `onnxruntime` entry and no `.onnx` asset at all**.
77
78## What the `.maml` holds
79
80306 tensors, 114 layers, 23,446,016 model parameters. Order is the contract between
81`scripts/ml/maml_convert.py` and `library/ml/src/main/rust/src/nets/tinyclip.rs`, which indexes it
82positionally — there are no names in the file. `maml_convert.py --graph tinyclip --print-layers`
83prints the table.
84
85Two transposes matter and are asserted rather than assumed. A `MatMul` weight is ONNX's
86`[in, out]` and becomes `[out, in, 1, 1]`; the `Conv` patch kernel is `[M, C, kH, kW]` already and
87is emitted **verbatim**. Every attention projection is square 256 x 256, so reading one convention
88as the other gives a tensor of exactly the right shape holding exactly the wrong numbers.
89
90## Licence
91
92MIT, for both the export and the checkpoint it came from. This file is inside `assets/`, so it is
93packaged into the APK — deliberately, so the attribution travels with the weights beside it.