pdf-vlm-raster
Zero-cgo (CGO_ENABLED=0) macOS document VLM ingestion demo that synthesizes a
multi-page PDF, rasterizes pages with PDFKit + CoreGraphics + Metal-backed
CoreImage into an IOSurface-backed MTLTexture, and feeds the IOSurface
zero-copy into x/screentensor to produce [3, 384, 384] FP16 NCHW tensors.
No external PDF files and no TCC permissions are required.
Pipeline
- Synthesize PDF (
CoreGraphics + CoreText): writes a 6-page PDF into
$TMPDIR via CGPDFContextCreateWithURL, CGContextBeginPage, vector bar
charts/shapes, and CTLineDraw text (including exact duplicate pages and a
localized callout-badge page to exercise x/screentensor's GPU tile-diff
gate). The temporary PDF is removed on exit.
- Rasterize & Filter (
PDFKit + Metal CIContext): opens the PDF with
PDFDocument / PDFPage, draws each page with DrawWithBoxToContext,
applies GPU CISharpenLuminance and a 16 px margin crop (800x800 ->
768x768), and renders directly into a BGRA8 IOSurface-backed MTLTexture
using CIContext.RenderToMTLTextureCommandBufferBoundsColorSpace.
- Zero-Copy VLM Tensor (
x/screentensor): binds the IOSurface zero-copy
via ProcessSurface, runs the 32x32 GPU tile-diff gate (DiffThreshold = 0.005), skips duplicate pages, and runs bilinear resize + CLIP RGB
normalization into a [3, 384, 384] FP16 Metal buffer (884,736 bytes).
- CPU-Copy Control Parity: locks the
IOSurface read-only on each
keyframe, copies BGRA8 pixels into Go memory, computes the float32 bilinear
resize + CLIP normalization reference, and reports max/mean absolute error
against the GPU FP16 tensor.
Usage
CGO_ENABLED=0 go vet ./examples/coreimage/pdf-vlm-raster
CGO_ENABLED=0 go test -v -count=1 -timeout 60s ./examples/coreimage/pdf-vlm-raster
CGO_ENABLED=0 go run ./examples/coreimage/pdf-vlm-raster
Flags:
-raster-size (default 800): pre-crop PDF raster width and height in pixels
-crop-margin (default 16): CoreImage margin crop per edge (768x768 after crop)
-tensor-size (default 384): output NCHW tensor spatial dimension
-diff-threshold (default 0.005): GPU tile-diff threshold for duplicate skipping
-sharpness (default 0.4): CISharpenLuminance sharpness
Measured Results (Apple M4 Max)
From CGO_ENABLED=0 go run ./examples/coreimage/pdf-vlm-raster:
Metal device: Apple M4 Max
Output tensor: [3, 384, 384] FP16 (884736 bytes)
Pages: 6 total (3 keyframes, 3 duplicate skips -> 50.0% skip rate)
Page Status Diff Raster VLM Wall VLM GPU CPU Ctrl MaxErr Extracted Text
1 KEYFRAME 1.0000 116.07ms 2.04ms 0.17ms 6.35ms 0.00097 Q1 Financial Overview
2 SKIPPED 0.0000 37.01ms 66.89ms 0.05ms - - Q1 Financial Overview
3 KEYFRAME 0.4980 4.57ms 1.06ms 0.07ms 2.55ms 0.00097 Zero-Copy VLM Ingestion
4 SKIPPED 0.0000 3.29ms 0.64ms 0.04ms - - Zero-Copy VLM Ingestion
5 SKIPPED 0.0000 40.92ms 0.34ms 0.04ms - - Zero-Copy VLM Ingestion
6 KEYFRAME 0.4920 3.52ms 0.85ms 0.07ms 3.48ms 0.00097 Zero-Copy VLM Ingestion
Averages: raster=34.23 ms/page | vlm=11.97 ms/page | total=46.20 ms/page | cpu-copy control=4.13 ms/keyframe
Parity: worst max_abs_err=0.000974 (FP16 vs CPU-copy float32 control)
Key observations:
- Duplicate-page skip rate:
50.0% (3 of 6 pages skipped with DiffScore = 0.0000; warm tile-diff GPU time 0.04 ms).
- Localized change detection: Page 6 adds only a bottom-right callout badge over Page 3 and triggers the tile-diff gate (
DiffScore = 0.4920, VLM GPU = 0.07 ms).
- Numerical parity: worst
max_abs_err = 0.000974 across all [3, 384, 384] FP16 elements versus the CPU-copy float32 reference (within 1 ULP of FP16).