from transformers import FineGrainedFP8Config, AutoModelForImageTextToText, AutoTokenizer, AutoProcessorimport torch model_id = "google/gemma-4-26B-A4B-it" quant_config = FineGrainedFP8Config( # 64x64, not the default 128x128: the language model's moe_intermediate_size # (704) and intermediate_size (2112) are not divisible by 128, only by 64 # (704 = 64*11, 2112 = 64*33, hidden_size 2816 = 64*44). With block_size=128 # the MoE grouped-matmul kernel hits `AssertionError: K (704) must be # divisible by block_k (128)` at the first forward pass. weight_block_size=(64, 64), # model.vision_tower's MLP intermediate_size is 4304 = 16*269 (269 is prime), # so it isn't divisible by 64 either -- excluded explicitly rather than # relying on the quantizer's shape-mismatch fallback (which silently leaves # a layer in BF16 if its shape doesn't tile, but still wraps it in the FP8 # module class). lm_head is excluded too, the conventional choice for # numerical stability of the output projection. modules_to_not_convert=["model.vision_tower", "lm_head"],) model = AutoModelForImageTextToText.from_pretrained( model_id, quantization_config=quant_config, dtype="auto", device_map="auto",)tok = AutoTokenizer.from_pretrained(model_id)proc = AutoProcessor.from_pretrained(model_id) model.save_pretrained("gemma-4-26B-A4B-it-FP8")tok.save_pretrained("gemma-4-26B-A4B-it-FP8")proc.save_pretrained("gemma-4-26B-A4B-it-FP8")