IndexErrorThe commited on
Commit
aa1ce65
·
verified ·
1 Parent(s): 1692750

Add model card

Browse files
Files changed (1) hide show
  1. README.md +106 -0
README.md ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: topdu/unirec-0.1b
4
+ tags:
5
+ - coreml
6
+ - ocr
7
+ - latex
8
+ - mathematics
9
+ library_name: coreml
10
+ ---
11
+
12
+ # UniRec-0.1B — Core ML
13
+
14
+ Core ML conversion of [topdu/unirec-0.1b](https://huggingface.co/topdu/unirec-0.1b),
15
+ a unified recogniser for printed text, mathematical formulas and tables. Packaged
16
+ for on-device use by [MathOCR](https://github.com/kihun-nam/MathOCR), a macOS app
17
+ that turns PDF pages without a text layer into Markdown with LaTeX.
18
+
19
+ Nothing here is a new model. The weights are the original authors'; this
20
+ repository only changes the format.
21
+
22
+ ## Contents
23
+
24
+ | File | Size | Input | Output |
25
+ |---|---|---|---|
26
+ | `UniRecEncoder.mlpackage` | 82 MB | `pixel_values [1,3,H,W]` | `cross_k`, `cross_v` `[6,1,6,S,128]` |
27
+ | `UniRecDecoder.mlpackage` | 191 MB | one token + cross-attention K/V | `logits [1,56371]` |
28
+
29
+ Both float16, `minimum_deployment_target = macOS 15`.
30
+
31
+ The split follows the maintainers' own ONNX export: the encoder also computes
32
+ the decoder's cross-attention key and value projections, which depend only on
33
+ the image and so are evaluated once per region rather than once per token.
34
+
35
+ The decoder is **stateful** — its self-attention KV cache lives on the Core ML
36
+ side rather than being passed in and out each step.
37
+
38
+ ## Requirements and limits
39
+
40
+ **Run with compute units restricted to CPU and GPU.**
41
+
42
+ ```swift
43
+ let configuration = MLModelConfiguration()
44
+ configuration.computeUnits = .cpuAndGPU
45
+ ```
46
+
47
+ This is not a performance preference. With the Neural Engine enabled, the
48
+ encoder is executed incorrectly: at a 192×640 input its output deviates from the
49
+ reference by **56%**, against **0.19%** on CPU+GPU. FocalSVTR's focal modulation
50
+ uses depthwise convolutions with kernels up to 15×15, which appears to be the
51
+ cause. The failure is silent — the model returns plausible but wrong text rather
52
+ than an error.
53
+
54
+ Other limits, fixed at conversion time:
55
+
56
+ - **512 encoder positions** (`cross_len` marks how many are real; pad the rest).
57
+ That is a region of roughly 960×544 px. Larger regions must be split.
58
+ - **512 generated tokens** — the self-attention cache size.
59
+
60
+ ## Preprocessing
61
+
62
+ Identical to the original model:
63
+
64
+ 1. RGB.
65
+ 2. Fit inside 960×1408 preserving aspect ratio. Images already smaller are **not**
66
+ enlarged.
67
+ 3. Round both sides *down* to a multiple of 64, minimum 64.
68
+ 4. Bicubic resample.
69
+ 5. Scale to `[0,1]`, then `(x - 0.5) / 0.5`.
70
+ 6. `NCHW`, float32.
71
+
72
+ ## Decoding
73
+
74
+ Greedy. Start from `bos = 0`, stop at `eos = 2`. The position index follows
75
+ M2M100's convention: `position = pad_token_id + 1 + step`, i.e. `2 + step`.
76
+
77
+ Token IDs map to strings through `unirec_tokenizer_mapping.json` in
78
+ [topdu/unirec_0_1b_onnx](https://huggingface.co/topdu/unirec_0_1b_onnx).
79
+ Formulas are emitted as `\( … \)` and `\[ … \]`; tables as HTML.
80
+
81
+ ## Verification
82
+
83
+ Checked against the maintainers' ONNX export on six rendered crops — an inline
84
+ formula, three display equations, a nested radical and a multi-line paragraph.
85
+ **All six produce token-for-token identical greedy sequences.**
86
+
87
+ On an M1: encoder 29–217 ms per region, decoder ~10 ms per token.
88
+
89
+ The conversion and verification scripts are in the MathOCR repository under
90
+ `tools/unirec_coreml/`.
91
+
92
+ ## Licence and attribution
93
+
94
+ Apache-2.0, inherited from the original model.
95
+
96
+ - Original model and weights: [topdu/unirec-0.1b](https://huggingface.co/topdu/unirec-0.1b)
97
+ - Source implementation: [Topdu/OpenOCR](https://github.com/Topdu/OpenOCR)
98
+
99
+ Changes made in this redistribution, as Apache-2.0 §4(b) requires:
100
+
101
+ 1. Exported from PyTorch to Core ML (`.mlpackage`, ML Program), float16.
102
+ 2. The decoding step was reimplemented with a fixed-size KV cache updated by a
103
+ masked write, because HuggingFace's `Cache` class does not trace to a static
104
+ graph. Same weights, same arithmetic — verified against the original.
105
+ 3. The encoder additionally returns the decoder's cross-attention K/V
106
+ projections, matching the upstream ONNX export's split.