gemma.cpp icon indicating copy to clipboard operation
gemma.cpp copied to clipboard

Segmentation fault using gemma 3 with --image_file candy.ppm

Open weiyang121 opened this issue 11 months ago • 3 comments

Abort at matmul-inl.h:868: Assert hwy::IsAligned(A.RowBytes(1), vector_bytes): Aborted (core dumped)

weiyang121 avatar Dec 26 '25 01:12 weiyang121

Hi @weiyang121 Thanks for reaching out. Could you please provide a few more details regarding the issue you’re experiencing?

pannaga-33 avatar Dec 30 '25 19:12 pannaga-33

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?

jan-wassenberg avatar Jan 06 '26 14:01 jan-wassenberg

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_file work if you build from the dev branch?
  • 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).

Zhuoxi2000 avatar Oct 01 '26 19:10 Zhuoxi2000