vlm-ocr-prefilter
Self-contained zero-cgo (CGO_ENABLED=0) 3-stage Apple Neural Engine (ANE) + Metal VLM prefilter example on macOS.
Uses purego vision, coregraphics, coretext, iosurface, metal, x/screentensor, and the Go standard library only, with zero TCC / screen-recording prompts (all frames are rendered in memory into IOSurface-backed CGBitmapContexts).
Pipeline
- Synthetic Frame Generator (
render.go): Renders a 10-frame 1280x800 BGRA IOSurface stream via CGBitmapContext and CoreText, mixing:
- Text-heavy document pages (3 distinct pages with ground-truth text)
- Identical repeats and minor-jitter frames (3px cursor blink / status dot)
- Mixed image+text visual dashboard frames (color heatmap + bar chart, geometric topology diagram + short caption)
- Stage 1 — Feature-Print Deduplication (
VNGenerateImageFeaturePrintRequest): Computes VNFeaturePrintObservation distance against the previous keyframe and drops duplicates/jitter below 0.15 (0 VLM image tokens, skipping Stages 2 and 3).
- Stage 2 — Document Segmentation + OCR (
VNDetectDocumentSegmentationRequest + VNRecognizeTextRequest): Locates document regions and extracts text (comparing Fast vs. Accurate recognition levels). Dense text-heavy pages route to OCR-only (0 VLM image tokens).
- Stage 3 — Zero-Copy Metal VLM Tensor Preprocessing (
x/screentensor.ProcessSurface): Routes non-text / visual-heavy changed frames directly from IOSurface to a normalized FP16 planar NCHW [3, 384, 384] Metal buffer (884,736 bytes, 729 ViT-14 image tokens/frame).
Running and Testing
CGO_ENABLED=0 go vet ./examples/vision/vlm-ocr-prefilter
CGO_ENABLED=0 go test -v -count=1 ./examples/vision/vlm-ocr-prefilter
CGO_ENABLED=0 go run ./examples/vision/vlm-ocr-prefilter
Measured Results
Measured on Apple M4 Max, macOS 26.6.2, go1.27.0 darwin/arm64, CGO_ENABLED=0:
Per-Frame 3-Stage Routing & Latency
| Frame |
Kind |
Route |
FP Dist |
Doc Conf |
Recall |
S1 (ms) |
S2 (ms) |
S3 (ms) |
Total (ms) |
Img Tokens |
doc-architecture-p1 |
text-doc |
stage2-ocr-only |
1.000 |
0.99 |
98.7% |
85.03 |
1447.91 |
0.00 |
1533.01 |
0 |
doc-architecture-p1-repeat |
jitter |
stage1-dedup |
0.000 |
- |
- |
9.44 |
0.00 |
0.00 |
9.44 |
0 |
doc-architecture-p1-cursor-blink |
jitter |
stage1-dedup |
0.048 |
- |
- |
8.77 |
0.00 |
0.00 |
8.77 |
0 |
doc-architecture-p1-status-dot |
jitter |
stage1-dedup |
0.044 |
- |
- |
16.46 |
0.00 |
0.00 |
16.46 |
0 |
doc-release-notes-p2 |
text-doc |
stage2-ocr-only |
0.868 |
0.00 |
100.0% |
92.54 |
671.24 |
0.00 |
763.84 |
0 |
doc-release-notes-p2-cursor |
jitter |
stage1-dedup |
0.066 |
- |
- |
13.71 |
0.00 |
0.00 |
13.71 |
0 |
mixed-telemetry-heatmap |
mixed-visual |
stage3-vlm-tensor |
1.117 |
0.70 |
100.0% |
7.37 |
529.15 |
1.17 |
537.74 |
729 |
doc-metrics-summary-p3 |
text-doc |
stage2-ocr-only |
1.179 |
0.99 |
100.0% |
8.49 |
393.54 |
0.00 |
402.08 |
0 |
doc-metrics-summary-p3-repeat |
jitter |
stage1-dedup |
0.000 |
- |
- |
40.90 |
0.00 |
0.00 |
40.90 |
0 |
mixed-architecture-diagram |
mixed-visual |
stage3-vlm-tensor |
1.104 |
0.44 |
100.0% |
8.27 |
187.37 |
0.77 |
196.43 |
729 |
Summary Metrics
- Stage 1 separation: Jitter/repeat distances
0.000–0.066 (< 0.15) vs. page-change distances 0.868–1.179 (> 0.40).
- VLM image-token reduction (ViT-14 @ 384x384 = 729 tokens/frame):
- Overall stream:
7290 -> 1458 image tokens (80.0% reduction)
- Text-heavy + jitter frames:
5832 -> 0 image tokens (100.0% reduction)
- Mean latency breakdown:
- Stage 1 (
VNGenerateImageFeaturePrintRequest, n=10): 29.10 ms/frame
- Stage 2 (
VNDetectDocumentSegmentationRequest + VNRecognizeTextRequest, n=5): 645.84 ms/frame
- Stage 3 (
x/screentensor.ProcessSurface, n=2): 0.97 ms/frame
- End-to-end average (n=10):
352.24 ms/frame
Stage 2 OCR Comparison (Fast vs. Accurate)
| Frame |
Fast Recall |
Fast Latency |
Accurate Recall |
Accurate Latency |
Tokens |
doc-architecture-p1 |
100.0% |
368.20 ms |
98.7% |
417.58 ms |
76 |
doc-release-notes-p2 |
100.0% |
320.22 ms |
100.0% |
351.95 ms |
73 |
doc-metrics-summary-p3 |
100.0% |
250.50 ms |
100.0% |
388.46 ms |
71 |