Files
Strata/tools
Niko1221 22ff409079 ling32 metal: picture encoder on the Metal GPU (the CPU encoder stays as reference and fallback)
The Vision tower (27 blocks, hidden 1152, 16 x 72, ffn 4304, merger, two linear layers) runs on the M5's matrix unit: new ling32_vision.metal (layer norm, GEMM with bias / residual / GELU epilogues on MetalPerformancePrimitives matmul2d, 2-D rope + q/k/v split, scores / softmax / P.V per block of 256 query rows) and ling32_metal_vision_gpu.hpp (BF16 weights widened to fp16 once, resident ~0.83 GiB after the first picture or at server start; activations allocated per picture; a picture that does not fit in the free GPU memory, or a kernel failure, falls back to the CPU encoder for that picture). Every product splits the activations in two fp16 numbers (hi, lo) with an fp32 accumulator.
- --vision-device auto (default) / gpu use it, cpu forces the CPU encoder; INFO says `vision=gpu`; --vision-encode ... --compare checks it against the CPU encoder per stage, --prof prints the GPU time per kernel.
- Measured on the M5 Max (GPU compute, CPU encoder in brackets): 448x448 (196 embeddings) 46 ms (0.8 s), 640x480 (300) 69 ms (1.2 s), 1920x1080 (2040) 2.0 s (22 s), 1536x2048 (3072) 3.9 s (52 s), 2304x1728 (3888) 6.3-7.5 s (81 s). Output vs the CPU encoder: rel L2 1.4e-5 .. 4.7e-5 up to 12288 patches, 1.1e-4 at 15552 patches (CUDA GPU vs CPU encoder: 5.7e-5 at 8160). Two products per attention matmul instead of three are no faster and 10 times less accurate (not used).
- Real model: circle / triangle / document-text answers token-identical to the CPU-encoder run, two pictures identical to the CUDA GPU-encoder run; text logits hash unchanged; serve test plain and with the MTP sidecar passes.
2026-12-03 00:20:29 +01:00
..