Skip to content
youneslaaroussiPublic

About

Pic2PDF converts handwritten math, lecture notes, and problem sets into professional PDFs.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Repository files navigation

Img2LaTeX

Photograph handwritten math, get LaTeX and a PDF. Everything runs on the device (iPhone, iPad or Mac): no server, no account, no network after the model download.

Arm On-device Model MediaPipe License

Download on the App Store · Research write-up · Privacy

Preview, analytics and SME2 benchmark screens

Real screenshots from an iPhone 16 Pro Max. Left: a generated matrix, rendered offline. Middle: per-run analytics. Right: the built-in SME2 vs NEON benchmark.

Arm and the Arm logo are trademarks of Arm Limited. This is an independent project and is not endorsed by or affiliated with Arm.

What it does

  1. Take or pick a photo of handwritten or printed math.
  2. A vision-language model (Gemma 3n E2B, INT4) reads it on the phone and streams LaTeX as it goes.
  3. The LaTeX is checked, repaired if needed, and rendered to a PDF you can share.
Photo What the app produces
Handwritten integral and derivative → \int_0^1 x^2\,dx = \left[\frac{x^3}{3}\right]_0^1 = \frac13, \frac{d}{dx}(\sin x\cdot e^x) = e^x(\sin x+\cos x)
Handwritten matrix → A, det(A) and A⁻¹ as typeset matrices (see the preview in the picture above)
Handwritten series → The series and its sum, typeset
Handwritten quadratic → The quadratic and its roots, typeset

Generate screen

Arm SME2

Some newer Arm chips have SME2, Arm's Scalable Matrix Extension 2, which can multiply matrices much faster than the older NEON instructions. It was confirmed on the A18 Pro in the iPhone 16 Pro Max used here and on an M4 MacBook Air; the app asks the chip at launch whether it supports SME2 instead of going by model name, and falls back to NEON when it does not.

The AI runtime this app uses (MediaPipe → XNNPACK → Arm KleidiAI) already contains SME2 kernels, but ships with them switched off on iOS. Img2LaTeX switches them on at launch when the chip supports SME2 (Pic2PDF/SME2/). Settings has the only switch. The Generate screen shows what XNNPACK actually chose, and when that's NEON it says why: "SME2 not available" or "SME2 off in Settings". A benchmark in the Analytics tab measures both on your own device.

"Supported" means the CPU reports FEAT_SME2 and the OS lets apps run SME instructions (hw.optional.arm.sme_max_svl_b > 0). Some iOS versions report SME2 on A18-class chips but trap SME instructions in apps (pytorch/cpuinfo#432). On those, the app uses NEON and Settings explains why.

How the SME2 switch works

Measured on an iPhone 16 Pro Max

Same photo, same prompt, greedy decoding, 64 output tokens, 18 measured runs per setting, baseline and SME2 alternated across 6 separate processes each:

NEON (default MediaPipe) SME2 Change
Time to first token (median) 5.02 s 2.96 s −41 %
Decode speed (median) 10.1 tok/s 11.0 tok/s +9.7 %
Whole generate call (median) 11.3 s 8.7 s −23 %
Peak memory 1841 MB 1868 MB +1.5 %

A/B headline chart

What this means in plain terms:

  • The wait before text starts appearing drops by about 40 %. That is the phase where the model reads the image and the prompt, which is large matrix work, and SME2 helps there.
  • Writing the answer speeds up only a little. Producing one token at a time is limited by how fast weights can be read from memory, not by arithmetic. Also, the INT4 weight layers in this model still run on NEON in this MediaPipe build; only the float and INT8 layers move to SME2.
  • Caveats. One phone, one model, one image. The phone was warm for most of the session (12 of 18 baseline and 14 of 18 SME2 runs started in the "serious" thermal state), so absolute speeds are lower than on a cool phone. Alternating the two settings keeps that from favouring either. Full method, raw data and scripts: research/.

The weight-cache trap

XNNPACK saves repacked weights next to the model (<model>.xnnpack_cache). The file is not tagged with the instruction set it was built for, so a cache built with NEON and loaded with SME2 (or the reverse) crashes with SIGABRT. The app records which mode built the cache, deletes it when the mode changes, and keeps a .loading marker so a crash during load cleans up after itself.

How it works

How a photo becomes a PDF

  • Inference: MediaPipe Tasks GenAI 0.10.24 (OnDeviceLLMService.swift). The language model runs on the CPU; part of the vision pipeline runs on the GPU through Metal.
  • Output cleanup (Pic2PDF/LaTeX/): small models sometimes loop or stop mid-sentence. LaTeXAutoFixer collapses runaway repetition, drops a truncated tail, closes open environments and wraps stray math in $…$. LaTeXValidator checks the result with latex.js and KaTeX before it is shown.
  • Rendering: latex.js is bundled in the app (LatexJS.bundle) and served to a WKWebView through a custom URL scheme, so previews work in Airplane Mode.
  • Diagnostics (Pic2PDF/Diagnostics/): every run records timings, token counts, peak memory, thermal state and which fixes were applied. They stay on the phone, show up in Analytics, and can be exported as JSON or CSV.
  • Storage: SwiftData history with search and favourites.

Build it

Requirements: Xcode 26 or newer and CocoaPods. Run it on an iPhone or iPad with iOS 17+, or on an Apple silicon Mac. To see SME2 you need an A18 / M4 or newer chip.

git clone https://github.com/younes-laaroussi/Pic2PDF.git
cd Pic2PDF
pod install
open Pic2PDF.xcworkspace

In Xcode, select the Pic2PDF target → Signing & Capabilities, choose your own team, and change the bundle identifier to one of your own (for example com.yourname.img2latex). Then pick a run destination and press Run.

On a Mac

Pick My Mac (Designed for iPad) as the run destination. The app runs natively on Apple silicon in a resizable window. It uses the same code, the same MediaPipe build and the same SME2 switch as on iPhone. On M4 and newer Macs, macOS lets apps use SME2, so the Generate screen shows On-device · SME2.

On a Mac, use Choose Files to pick images from Finder. The camera button only appears on devices with a camera.

A smaller check on an M4 MacBook Air (32 GB) with the in-app benchmark: 9 measured runs per setting from 3 processes each, alternating the settings, answers running to their natural end (118 to 130 tokens), other apps open. Medians. It's noisier than the iPhone study above (NEON's time to first token ranged from 4.2 to 8.9 s).

NEON SME2 Change
Time to first token 6.21 s 3.48 s −44 %
Decode speed 8.2 tok/s 10.2 tok/s +25 %
Whole generate call 21.8 s 15.9 s −27 %

The model

The model isn't in the repository. The app downloads it on first use (welcome screen, or Settings → AI Model): gemma-3n-E2B-it-int4.task, about 3.1 GB. It tries Hugging Face first, then Cloudflare R2, and keeps the file only if its SHA-256 matches. Both hold unmodified copies of Google's LiteRT release, which is gated on Hugging Face. The mirror list and checksums are in ModelDownloadManager.swift.

If your network blocks both, the app says so. Download the file on another network and copy it into the app's Documents/Models/ folder. On a Mac that's ~/Library/Containers/<bundle identifier>/Data/Documents/Models/.

The iOS Simulator

The app builds and runs in the Simulator, but transcription does not work there. The MediaPipe vision path copies the image through Metal, and under the Simulator that copy produces an all-black image, so the model sees nothing. To try it on a computer, use My Mac (Designed for iPad) instead. The preview, history, analytics and LaTeX auto-fixer all work in the Simulator.

Repository layout

Pic2PDF/                 App source
  SME2/                  SME2 switch, C shim into XNNPACK, in-app benchmark
  LaTeX/                 Validator, auto-fixer, sanitizer, latex.js asset loader
  Diagnostics/           Per-run diagnostics model and views
  LatexJS.bundle/        latex.js 0.12.6 (MIT) for offline rendering
research/                SME2 investigation: method, raw data, scripts, evidence
docs/images/             Screenshots used in this README
PRIVACY.md

Limitations

  • Accuracy depends on handwriting and on a small 2-billion-parameter model; always read the result before relying on it. Long pages and dense tables are the weakest cases.
  • MediaPipe 0.10.24 cannot cancel a generation in progress.
  • The first model load builds a weight cache and takes noticeably longer than later loads.
  • The SME2 study is from one iPhone, plus a short check on one M4 Mac. Other SME2 chips are expected to behave similarly but have not been measured.

Privacy

No accounts, no analytics, no uploads. Photos, LaTeX and diagnostics stay on the phone; the only network use is the one-time model download. See PRIVACY.md.

Acknowledgements

License

MIT, see LICENSE. Third-party components keep their own licenses.

About

Pic2PDF converts handwritten math, lecture notes, and problem sets into professional PDFs.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Contributors

Languages