[quantization] Complete QuantGemma4Model PTQ Wrapper#797
Open
dvsav wants to merge 1 commit into
Open
Conversation
1a91bb5 to
a44dcbc
Compare
…lacement and PLE Add multimodal placeholder token replacement, PLE computation, and input validation to QuantGemma4Model.forward. Co-authored-by: Cline TICO-DCO-1.0-Signed-off-by: d.savchenkov <d.savchenkov@partner.samsung.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
This PR completes the
QuantGemma4ModelPTQ wrapper — the top-level multimodal (image-text) model for Gemma4 E2B. It implements the full calibration forward path with multimodal placeholder replacement, PLE computation, and fixed-slot fusion, plus an export-friendlyforward_export()andas_export_module()for Circle conversion.Why
The
QuantGemma4Modelwrapper previously had a skeletonforward()that only handled the simplest text-only path — multimodal placeholder tokens (image/video/audio) were not replaced before embedding, PLE was not computed, and the wrapper was not registered for automatic discovery byprepare(). Without these, calibration through the full model was incorrect (wrong embeddings at image positions, no PLE statistics) and the wrapper was never instantiated. Additionally, there was no export path to convert the quantized model to Circle format, which is the final deliverable of the TICO pipeline.Key Design Decisions
Placeholder replacement via
torch.whereinstead ofmasked_scatter: The original HuggingFaceGemma4Model.forwardusesmasked_scatterto insert image features, which is not export-friendly. We replace placeholder token IDs withpad_token_idbefore embedding and usefixed_slot_fuseto insert image features at a static position range. This matches theStaticGemma4Runtimedesign where CPU owns dynamic operations and NPU owns static compute.Two forward methods —
forward()for calibration,forward_export()for export: The calibrationforward()contains dynamic control flow (if pixel_values is not None,if multimodal_mask.any(), conditional PLE) that cannot be exported. The exportforward_export()takes pre-fusedinputs_embedsand precomputed masks/RoPE/PLE, with no dynamic control flow — following the pattern established byQuantGemma4VisionModel.CPU/NPU split aligned with
StaticGemma4Runtime: The export path assumes the CPU runtime has already performed token embedding, vision tower, MM fusion, PLE computation, and mask/RoPE generation. The NPU subgraph runs only the text decoder layers and final norm.Changes
tico/quantization/wrapq/wrappers/gemma4/quant_model.py— Added_get_placeholder_mask()helper; completedforward()with placeholder replacement, PLE computation, and input validation; addedforward_export()(static-shape export path) andas_export_module()(export preparation)tico/quantization/wrapq/wrappers/gemma4/export_adapters.py— AddedGemma4ModelPrefillExportAdapterthat delegates toforward_export()tico/quantization/wrapq/wrappers/registry.py— Enabledquant_modelin_CORE_MODULESfor automatic discoverytico/quantization/wrapq/examples/gemma4/quantize_model.py— New example script demonstrating full PTQ flow (text-only + image-text calibration, PEIR evaluation, Circle export)test/quantization/wrapq/wrappers/gemma4/test_quant_model.py— New unit tests for_get_placeholder_maskandQuantGemma4Modelwrappertest/quantization/wrapq/wrappers/gemma4/test_quantize_model.py— New smoke tests for prepare-calibrate-convert flow (text-only, image-text, export adapter)tico/quantization/recipes/debug/wrapper_smoke/cases/gemma4.py— AddedGemma4ModelCaseand registered it inGEMMA4_CASESTests
test_quantize_model.py: 5 smoke tests — no-quant parity, prepare-convert text-only flow, prepare-convert image-text flow,as_export_moduleflow,_get_placeholder_maskunit testtest_quant_model.py: Unit tests forQuantGemma4Modelwrapper and helper functionsGemma4ModelCasepasses with Mean |diff| = 0.006353, PEIR = 0.028361, and successful Circle exporttest/quantization/wrapq/wrappers/gemma4/passUnit Tests
Internal Tests
Smoke Test
Example Script
tico/quantization/wrapq/examples/gemma4/quantize_model.pydemonstrates the complete workflow:Gemma4Modelwith random weights (no download)build_gemma4_e2b_ptq_config()as_export_module("prefill")and converts to Circle format (gemma4_model.q.circle)