Skip to content

Fix gemma4 multi component quantization - #2637

Open
Xiaoyu (xiaoyu-work) wants to merge 9 commits into
mainfrom
fix/gemma4-multi-component-quantization
Open

Fix gemma4 multi component quantization#2637
Xiaoyu (xiaoyu-work) wants to merge 9 commits into
mainfrom
fix/gemma4-multi-component-quantization

Conversation

@xiaoyu-work

Copy link
Copy Markdown
Collaborator

Describe your changes

This pull request improves quantization robustness for small floating point values and enhances the ModelConfig component selection logic to support multiple components, along with corresponding test updates.

Quantization robustness:

  • In olive/common/quant/utils.py, the find_qparams method now clamps the calculated scales to a minimum positive value to prevent underflow to zero for tiny FP16 values, ensuring numerical stability during quantization.
  • Added a test (test_find_qparams_tiny_fp16_values_do_not_underflow) to verify that quantizing nonzero FP16 groups does not produce zero scales, preventing underflow issues.

ModelConfig component selection enhancements:

  • Removed the restriction that only one HuggingFace model component can be selected at a time in ModelConfig.select_components, allowing aggregation of multiple components for component-scoped PyTorch optimization.
  • Updated the logic in _select_hf_component to handle multiple selected components, aggregating their names and source paths into model_attributes, and improved error handling for missing source paths.
  • Updated the test for multiple component selection to verify the correct aggregation of component names and source paths, replacing the previous test that expected an error.

Checklist before requesting a review

  • Add unit tests for this change.
  • Make sure all tests can pass.
  • Update documents if necessary.
  • Lint and apply fixes to your code by running lintrunner -a
  • Is this a user-facing change? If yes, give a description of this change to be included in the release notes.

(Optional) Issue link

Copilot AI lite review requested due to automatic review settings August 26, 2026 01:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves robustness in Olive’s weight quantization for tiny FP16 values and extends ModelConfig.select_components to support selecting multiple Hugging Face model components (aggregating their runtime source paths) for component-scoped PyTorch optimization.

Changes:

  • Clamp WeightQuantizer.find_qparams scales to the smallest positive representable value to avoid FP16 underflow to zero.
  • Allow HfModel component selection of multiple components and aggregate their component_source_paths into model_attributes.
  • Update/add tests covering multi-component selection aggregation and tiny-FP16 scale underflow prevention.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
olive/common/quant/utils.py Clamps computed quantization scales to a minimum positive value to prevent underflow to zero.
olive/model/config/model_config.py Updates HF component selection logic to support multiple selected components and aggregate source paths into model_attributes.
test/common/quant/test_utils.py Adds a unit test ensuring tiny nonzero FP16 weights do not yield zero scales.
test/model/test_composite_model.py Updates HF-model component selection tests to validate multi-component aggregation instead of expecting an error.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread olive/model/config/model_config.py
Comment thread test/passes/onnx/test_rtn_quantization.py Fixed
Comment thread test/passes/onnx/test_rtn_quantization.py Fixed
Comment thread olive/passes/pytorch/quant_utils.py Fixed
Co-authored-by: xiaoyu-work <85524621+xiaoyu-work@users.noreply.github.com>
@xiaoyu-work
Xiaoyu (xiaoyu-work) enabled auto-merge (squash) August 31, 2026 22:35
@xiaoyu-work
Xiaoyu (xiaoyu-work) force-pushed the fix/gemma4-multi-component-quantization branch from 5996154 to 74a029e Compare August 31, 2026 22:42
@titaiwangms

Copy link
Copy Markdown
Contributor

Review team verdict: Request changes

The review team found one critical model-correctness issue and several major checkpoint integrity and integration problems.

Severity Location Finding
Critical olive/workflows/run/hf_component_assembly.py:182-224 Quantization metadata is merged by copying the first build and reconciling only selected fields. tie_word_embeddings can therefore depend on build order, causing missing quantized lm_head buffers or incorrectly tied embeddings and silently corrupting the loaded model. Merge every load-affecting field explicitly and reject incompatible configurations.
Major olive/workflows/run/hf_component_assembly.py:192-206 Generated exact overrides are appended after inherited regex overrides, but Olive uses first-match-wins ordering. A broad earlier override can reconstruct component tensors with the wrong group size or bit width. Resolve effective settings per module or place exact overrides first.
Major olive/workflows/run/hf_component_assembly.py:208-224 Removing any skip pattern that matches one quantized module also removes protection for unrelated float modules matched by that pattern. Derive exact skip entries from assembled float-weight keys, following the existing logic in quant_utils.py.
Major olive/workflows/run/hf_component_assembly.py:367-406 Publication is non-atomic: the new index is copied before referenced component shards, then existing outputs and metadata are deleted. ENOSPC, interruption, or concurrent assembly can leave a corrupt checkpoint and destroy valid build outputs. Publish shards first and atomically replace the index last; use unique staging and locking.
Major olive/workflows/run/hf_component_assembly.py:381-395 Cleanup does not remove a pre-existing top-level model.safetensors or pytorch_model.bin. Transformers prefers that file over the new index, so loading can silently use a stale model. Reject or remove all conflicting checkpoint formats before publication.
Major olive/workflows/run/hf_component_assembly.py:384-406, olive/cli/run.py:114-131 Assembly deletes each build's model/ and model_config.json, while CLI output and persisted footprints still point to those locations. Chained commands, packaging, and provenance can reference deleted artifacts. Preserve or rewrite those artifacts and print the assembled parent path.
Major olive/model/config/model_config.py:213-219 Multi-component selection removes component_role, but downstream PyTorch quantization still uses that field for wrapper selection, decoder handling, and target eligibility. Mixed decoder/vision selections can now succeed while only partially applying the requested optimization. Either restore one-component-per-build or fully support component_names downstream.
Major olive/passes/pytorch/quant_utils.py:1106-1117 The meta-parameter fallback ties output embeddings to input embeddings based only on equal shape. Models with intentionally untied embeddings can be silently changed. Require explicit tying metadata such as config.tie_word_embeddings.
Major olive/workflows/run/hf_component_assembly.py:344-358 Unoptimized tensors are copied exclusively from the first build without validating other builds' key sets, shapes, or configs. Keys unique to later builds can disappear, and incompatible component outputs can be combined. Validate cross-build checkpoint/config compatibility before assembly.
Major olive/workflows/run/builds.py:84-96 Automatic assembly is inferred from whether named builds explicitly set output_dir, rather than whether resolved outputs share a parent as documented. This both rejects valid sibling directories and enables destructive assembly without an explicit opt-out. Derive the parent from resolved paths and add an assembly setting.
Major olive/cli/capture_onnx.py:199-208 The new Diffusers Mobius path uses get_diffusers_input_model, which does not forward trust_remote_code; commands requiring custom model code now invoke Mobius with it disabled. Propagate the shared load arguments.

Non-blocking compatibility questions

  • Safetensors shards are written without metadata={"format": "pt"}; compatibility with older Transformers versions needs a targeted run.
  • Component shards appear in subdirectories. Transformers supports index-relative paths, but consumers that scan only root-level *.safetensors may load an incomplete model.
  • The new ONNX IR round-trip should be checked against the pass's documented support for models larger than 2 GB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants