Downloads · 30 days
6
16% of all-time downloads
Reza2kn/surya-ocr-2-coreml-runtime
surya-ocr-2-coreml-runtime is a image-text-to-text model from Reza2kn. Use it for the image-text-to-text task on the model card, and read the license before you ship it in a product. It is set up for coremltools. The card lists the license as openrail.
This repository contains an early CoreML runtime bundle derived from datalab-to/surya-ocr-2 at source commit 3b3d4cdf88d6928b0acdc75181b13206ea67c4a3.
Downloads · 30 days
6
16% of all-time downloads
All-time downloads
38
Public
Repo size
2.7 GB
Likes
0
Public
Click a slice to open those files.
.bin2.7 GB · 100%
From the Hugging Face model README
This repository contains an early CoreML runtime bundle derived from datalab-to/surya-ocr-2 at source commit 3b3d4cdf88d6928b0acdc75181b13206ea67c4a3.
It is intended for native Apple OCR experiments and future iOS/macOS demo-app work. This revision includes a SwiftPM runtime package that runs the fixed-shape OCR canary path without Python.
| File | Purpose | Precision / quantization | Shape contract |
|---|---|---|---|
surya_vision_fp16.mlpackage | Vision tower | CoreML FP16 compute | pixel_values [1024,1536] -> image_embeds [256,1024] |
surya_vision_int8.mlpackage | Quantized vision tower | CoreML linear INT8 weight compression | pixel_values [1024,1536] -> image_embeds [256,1024] |
surya_prefill_fp16_seq300_cache512.mlpackage | Language prefill, logits, and initial cache | CoreML FP16 compute | fixed prefill length 300, cache length 512 |
surya_decode_step_fp16_cache512.mlpackage | One-token cached decode step | CoreML FP16 compute | one token at a time, cache length 512 |
Processor/tokenizer assets are included under processor/. Native constants and embedding assets are under native_assets/. Validation JSONs are under validation/.
The SwiftPM package lives in native/SuryaCoreMLRuntime.
import SuryaCoreMLRuntime
let runtime = try SuryaCoreMLRuntime(modelDirectory: modelDirectoryURL)
let result = try runtime.generate(image: cgImage, maxNewTokens: 128, useInt8Vision: true)
print(result.text ?? "")
The package currently handles fixed 512x512 Qwen image preprocessing, INT8/FP16 vision selection, fixed OCR prompt embeddings, image placeholder insertion, precomputed RoPE tables, generated-token embedding lookup, KV cache insertion, CoreML prefill/decode, and basic token decoding.
All validation below was run on a Mac Studio with CoreML package prediction, using the canary prompt/image:
OCR this image to HTML.
The canary image contains:
Invoice 123
Total $42.00
| Gate | Result |
|---|---|
| Prefill parity before CoreML export | native/custom first token 1039; logits max diff 2.6702880859375e-05 |
| Prefill CoreML smoke | Torch/CoreML first token 1039; logits max diff 0.3057253360748291; mean diff 0.03853870555758476 |
| Decode CoreML iterative smoke | 9/9 tokens match native; text <p>Invoice |
| CoreML prefill -> CoreML decode | 9/9 tokens match native; text <p>Invoice |
| CoreML FP16 vision -> CoreML prefill -> CoreML decode | 9/9 tokens match native; text <p>Invoice |
| CoreML INT8 vision -> CoreML prefill -> CoreML decode | 9/9 tokens match native; text <p>Invoice |
| Native Swift image -> CoreML INT8 vision -> CoreML prefill -> CoreML decode | 9/9 tokens match native; text <p>Invoice |
The INT8 vision package has mean absolute diff 0.021211756393313408 vs the PyTorch vision tower on the canary.
The included Swift package is a fixed-shape native runtime for the current OCR path. The host app still owns:
CGImage or already-preprocessed pixel_valuesThe included scripts/export_surya_coreml_runtime.py shows the Python reference glue used for validation. The included native/SuryaCoreMLRuntime package is the native implementation.
pip install coremltools torch transformers pillow qwen-vl-utils huggingface_hub
python scripts/export_surya_coreml_runtime.py vision-combined-runtime-smoke \
--model-id datalab-to/surya-ocr-2 \
--vision-package surya_vision_int8.mlpackage \
--prefill-package surya_prefill_fp16_seq300_cache512.mlpackage \
--decode-package surya_decode_step_fp16_cache512.mlpackage \
--output validation/local_vision_int8_prefill_decode_smoke.json \
--max-cache-length 512 \
--steps 8
Expected canary output today:
{
"all_tokens_match": true,
"coreml_text": "<p>Invoice ",
"native_text": "<p>Invoice "
}
cd native/SuryaCoreMLRuntime
swift run surya-coreml-smoke \
--model-dir ../.. \
--image /path/to/512x512-document.png \
--max-tokens 8 \
--vision int8
Validated canary output:
1039 2009 2046 2054 2047 2041 2035 2037 1979
<p>Invoice
pixel_values [1024,1536], prefill length 300, and full-attention cache length 512.allenai/olmOCR-bench.Generated non-destructively from datalab-to/surya-ocr-2. No fine-tuning was performed.