[quantization] Add generation step to StaticGemma4Runtime - #849
Merged
Conversation
dvsav
force-pushed
the
generate
branch
3 times, most recently
from
July 30, 2026 08:20
576b4f2 to
acbdd28
Compare
dvsav
marked this pull request as ready for review
July 30, 2026 08:41
Torrero
reviewed
Jul 31, 2026
| print(f"[verify_step_generation] Runtime text: {rt_text}") | ||
| print(f"[verify_step_generation] HF text: {hf_text}") | ||
|
|
||
| print("[verify_step_generation] All checks passed.") |
Contributor
There was a problem hiding this comment.
This message will be printed even if there are some mismatches.
Contributor
Author
There was a problem hiding this comment.
👍 Fixed.
Now the message accurately reflects the verification result:
- "All checks passed." - printed only when all tokens match between runtime and HF generation
- "Mismatches detected." - printed when token mismatches exist
Torrero
reviewed
Jul 31, 2026
| dtype=hidden_states.dtype, | ||
| ) | ||
|
|
||
| valid_positions = (attention_masks["full_attention"] == 0.0).sum().item() |
Contributor
There was a problem hiding this comment.
Where do you plan to use valid_positions? This variable is computed but it is never used.
Contributor
Author
There was a problem hiding this comment.
👍 Removed the unused variable.
Torrero
reviewed
Jul 31, 2026
| # Write only valid K/V (excluding padding) into the cache. | ||
| # new_k/new_v from prefill have shape (B, kv_heads, max_seq, head_dim) | ||
| # but positions >= valid_length contain garbage from padding tokens. | ||
| valid_len = int(batch["valid_length"].item()) |
Contributor
There was a problem hiding this comment.
It would be better to move this statement outside the loop to avoid a GPU→CPU sync per layer.
Contributor
Author
There was a problem hiding this comment.
👍 Moved valid_len = int(batch["valid_length"].item()) outside the prefill layer loop.
Implement greedy generation with side-by-side HF verification for Gemma4 E2B. 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.
Related issue: #768
Related PR: #822
What
This PR implements the generation step (Step 8) for the
StaticGemma4Runtimein the TICO project, completing the full 8-step static runtime verification flow for Gemma4 E2B. It adds a greedy generation method with side-by-side validation against HuggingFace's reference implementation.Additionally, this PR fixes the KV cache write logic in
prefillto only store valid tokens (excluding padding), and adds proper shared-KV cache pre-population indecode_onefor consumer layers.Why
The static runtime needed a complete generation loop to verify end-to-end functionality beyond single-step decode verification. This enables:
Key Design Decisions
Greedy generation loop: The
generate_greedymethod reusesprefill()anddecode_one()internally, ensuring consistency with the verified single-step implementations.Image token preservation: The method extracts input IDs from
_raw_inputs["input_ids"](which preserves original image tokens) rather thanllm_input_ids(which has image tokens replaced withpad_token_idfor NPU compute).Token accuracy metric: Instead of exact sequence match (which is unrealistic for quantized vs. FP models), we report token-level accuracy percentage and first mismatch position.
Detokenized output: Both runtime and HF generated sequences are detokenized and printed for visual inspection, enabling qualitative assessment beyond numerical metrics.
KV cache fix: In
prefill, only valid K/V positions (up tovalid_length) are written to the cache, excluding padding positions that contain garbage from padding tokens.Shared-KV pre-population: In
decode_one, shared-KV consumer layers are pre-populated fromlayer_cachesbefore store layers update the cache, ensuring correct KV access across decode steps.Changes
tico/quantization/recipes/debug/static_gemma4_runtime.py(+190/-9 lines):StaticGemma4Runtime.prefill: Fixed KV cache write to only store valid-length K/V (excluding padding garbage).StaticGemma4Runtime.decode_one: Added shared-KV cache pre-population for consumer layers before the layer loop.StaticGemma4Runtime.generate_greedy(new, ~49 lines): Greedy generation method that:prefillto process prompt + image_raw_inputsto preserve image tokensdecode_oneand collects generated tokensverify_step_generation(new, ~92 lines): Validation function that:run_static_gemma4_runtime: Wired Step 8 to callverify_step_generationand print generated text.Tests
Example Script