Skip to content

[Docs] Add Developer Guides section#2517

Open
dsikka wants to merge 7 commits intomainfrom
dev-tutorials
Open

[Docs] Add Developer Guides section#2517
dsikka wants to merge 7 commits intomainfrom
dev-tutorials

Conversation

@dsikka
Copy link
Collaborator

@dsikka dsikka commented Mar 25, 2026

Summary

Adds a new Developer Guides section to the docs for contributors extending LLM Compressor:

  • Adding a New Modifier — modifier lifecycle (on_initialize, on_start, on_update, on_end, on_finalize), Pydantic parameter declaration, HooksMixin usage, and a concrete WeightClampModifier walkthrough
  • Adding a New Observer — clarifies that observers compute min/max values (not scale/zero_point directly), explains how compressed-tensors calculate_qparams and generate_gparam consume those values, covers the Observer contract, stateful observers, and a PercentileObserver example with recipe usage
  • Adding MoE Calibration Support for a New Model — explains why MoEs need special calibration handling, the MoECalibrationModule contract, and a step-by-step guide using SequentialLlama4TextMoe as the canonical reference. Covers Llama4-specific patterns: packed expert unpacking via a helper ModuleList, is_permanent=True for vLLM compatibility, shared_expert handling, and applying routing scores to expert outputs (not inputs) to avoid NaNs during calibration

Also updates .nav.yml to add the new section between User Guides and Examples.

Updates the existing User Guides:

  • Observers (guides/observers.md) — corrects observer descriptions (min/max values, not scale/zero_point), fixes base class method names, documents all previously undocumented observer variants (memoryless_minmax, static_minmax, memoryless_mse), and splits observer_kwargs table by observer type with accurate defaults

🤖 Generated with Claude Code

dsikka and others added 2 commits March 25, 2026 15:25
Adds a new Developer Tutorials section with three tutorials:
- Adding a New Modifier: covers the modifier lifecycle, Pydantic params,
  HooksMixin, and a concrete WeightClampModifier example
- Adding a New Observer: covers the Observer contract, stateful observers,
  and a PercentileObserver example with recipe usage
- Adding MoE Model Support: covers MoECalibrationModule, the calibration
  forward pattern, and a step-by-step guide referencing the GLM-4.7 example

Also adds the section to .nav.yml between User Guides and Examples.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link

👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review.

Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to the project's documentation by adding a dedicated "Developer Guides" section. This new section provides essential resources for contributors looking to extend the LLM Compressor framework, covering key areas such as implementing custom modifiers, creating new observers for quantization, and integrating support for Mixture of Experts (MoE) models. The guides offer in-depth explanations of core contracts, lifecycles, and practical examples to facilitate easier and more effective contributions.

Highlights

  • New Developer Guides Section: A new "Developer Guides" section has been added to the documentation, aimed at contributors extending LLM Compressor.
  • Adding a New Modifier Guide: Detailed guide on the modifier contract, lifecycle (on_initialize, on_start, on_update, on_end, on_finalize), Pydantic parameter declaration, HooksMixin usage, and a concrete WeightClampModifier walkthrough.
  • Adding a New Observer Guide: Comprehensive guide on the Observer contract (get_min_max, get_global_min_max), @Observer.register decorator, stateful observers, and a PercentileObserver example with recipe usage.
  • Adding MoE Model Support Guide: Explains the need for special calibration handling for Mixture of Experts (MoE) models, the MoECalibrationModule contract, the calibration forward pattern, and a step-by-step guide referencing the existing GLM-4.7 implementation.
  • Navigation Update: The .nav.yml file was updated to integrate the new "Developer Guides" section between "User Guides" and "Examples".

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

…ew Model"

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces new developer tutorials for LLM Compressor, covering how to add custom Modifiers, Observers, and support for MoE models. The documentation updates include new markdown files and navigation links. Review feedback suggests improving the clarity of lifecycle hook descriptions in the modifier tutorial, defining the ds variable in the MoE example, and optimizing the get_min_max implementation in the observer tutorial for efficiency and robustness.

return True

def on_start(self, state: State, event: Event, **kwargs):
# Called when calibration starts (first BATCH_START event).
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The comment here is slightly misleading. on_start is not necessarily called on the first BATCH_START event of calibration, but rather on the first BATCH_START event for which the modifier becomes active (i.e., when event.current_index >= self.start). For better clarity, I suggest changing the comment.

Suggested change
# Called when calibration starts (first BATCH_START event).
# Called on the BATCH_START event when the modifier's `start` step is reached.

...

def on_end(self, state: State, event: Event, **kwargs):
# Called when calibration ends.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to on_start, the comment for on_end could be more precise. It's triggered when the modifier's end step is reached, on a BATCH_END event. I suggest updating the comment for accuracy.

Suggested change
# Called when calibration ends.
# Called on the BATCH_END event when the modifier's `end` step is reached.


oneshot(
model=model,
dataset=ds,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The ds variable used for the dataset parameter is not defined in this code snippet. For developers following this guide, it could be confusing. Please consider adding a comment before the oneshot call to clarify that ds should be a loaded calibration dataset, for example:

# ds = load_dataset(...)

Comment on lines +100 to +111
min_vals = torch.tensor(
[
torch.quantile(observed[..., i], lower / 100.0).item()
for i in range(observed.shape[-2])
]
)
max_vals = torch.tensor(
[
torch.quantile(observed[..., i], upper / 100.0).item()
for i in range(observed.shape[-2])
]
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation of get_min_max uses a list comprehension, which can be inefficient and is not robust for multi-dimensional qparam_shape. A vectorized approach using torch.quantile directly on a reshaped tensor would be more idiomatic, performant, and correct.

Consider replacing the logic for computing min_vals and max_vals with the following:

        # Vectorized implementation for efficiency and correctness with multi-dimensional qparam_shape
        # The observed tensor has shape (num_observations, *qparam_shape, group_size)
        qparam_shape_len = len(observed.shape) - 2
        # Permute to bring qparam_shape dimensions to the front, then flatten observation and group dims
        permute_dims = list(range(1, 1 + qparam_shape_len)) + [0, qparam_shape_len + 1]
        reshaped = observed.permute(*permute_dims).flatten(start_dim=qparam_shape_len)

        min_vals = torch.quantile(reshaped, lower / 100.0, dim=-1)
        max_vals = torch.quantile(reshaped, upper / 100.0, dim=-1)

@mergify mergify bot added the documentation Improvements or additions to documentation label Mar 25, 2026
dsikka and others added 4 commits March 25, 2026 15:43
Replaces the GLM-4.7 reference with the Llama4 implementation as the
canonical example. Updates the forward pattern, contract section, and
step-by-step to reflect Llama4-specific details including packed expert
unpacking, is_permanent=True, shared_expert handling, and router score
application to expert outputs rather than inputs.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…e/zp

Clarifies that observers return min and max values, which are then passed
to calculate_qparams and generate_gparam in compressed-tensors to produce
scale and zero_point. Adds a section explaining how the base class uses
observer output, and updates the example docstring and tips accordingly.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…vers

- Correct: observers compute min/max values, not scale/zero_point directly;
  compressed-tensors calculate_qparams/generate_gparam handle that conversion
- Fix base class description: correct method names (get_min_max,
  get_global_min_max) and accurate description of what the base class handles
- Document all observer variants: memoryless_minmax, static_minmax, minmax,
  memoryless_mse, mse (previously only MinMax and MSE were mentioned)
- Split observer_kwargs table by observer type with accurate defaults

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant