Segmentation fault using gemma 3 with --image_file candy.ppm
Abort at matmul-inl.h:868: Assert hwy::IsAligned(A.RowBytes(1), vector_bytes): Aborted (core dumped)
Hi @weiyang121 Thanks for reaching out. Could you please provide a few more details regarding the issue you’re experiencing?
Thanks for reporting. Presumably the image file dimensions are not a power of two? Our image handling code is very basic/brittle and seems to require that the width be a multiple of 64. Can you confirm that cropping to that multiple solves the problem?
I think this is unrelated to the image dimensions, at least on main (3ed403e). Before patch extraction, run.cc always resizes the image to vit_config.image_size (896x896 for Gemma 3), and image_patches in vit.cc uses MatPadding::kOdd.
The abort text matches ops/matmul-inl.h:868, the IsAligned(A.RowBytes(1), vector_bytes) check in EnsureAligned. For Gemma 3 (GEMMA_VLM), VitAttention calls DotSoftmaxWeightedSumMatrix(). On main, that function builds
MatStorageT<float> Q("Q2", Extents2D(num_tokens_, qkv_dim),
env_.ctx.allocator, MatPadding::kPacked);
and passes it as A to CallMatMul. The ViT qkv_dim is 72 (LayerConfigVit in configs.cc), so each packed row is 72 * 4 = 288 bytes. vector_bytes comes from hwy::VectorBytes() (util/allocator.cc:138). That is 64 on AVX-512 targets, and 288 % 64 = 32, so RowBytes(1) is misaligned for every image. With AVX2 (32 B) or NEON (16 B), 288 is a multiple of the vector size, which would explain why most people don't hit this.
On dev, this looks already fixed. a084d33 (2025-12-01) replaced the per-call kPacked Q2/K2/C2 with preallocated activations_.attention.vit_Q/vit_K/vit_C built via MatFactory, which defaults to MatPadding::kOdd. With 64 B cache lines (as on AVX-512 CPUs), that pads each 288 B row to 320 B, which is 64 B-aligned. Later, bea8b1c moved ViT attention to flash attention entirely. Neither commit is in main yet.
I haven't reproduced this on hardware (I have no AVX-512 machine), so two questions:
- @weiyang121: is your CPU AVX-512 capable (e.g.
grep -o avx512f /proc/cpuinfo | head -1), and does--image_filework if you build from thedevbranch? - Maintainers: since this appears to affect only
main, is a dev-to-main sync planned that would pick up a084d33, or should this be closed as fixed on dev?
AI assistance: drafted with an AI coding assistant (Claude).