Skip to content

Feat (utils): replace weights with quantized ones - #1505

Merged
Giuseppe5 merged 15 commits into
Xilinx:devfrom
Giuseppe5:merge_ln
Jun 16, 2026
Merged

Feat (utils): replace weights with quantized ones#1505
Giuseppe5 merged 15 commits into
Xilinx:devfrom
Giuseppe5:merge_ln

Conversation

@Giuseppe5

@Giuseppe5 Giuseppe5 commented Apr 4, 2026

Copy link
Copy Markdown
Collaborator

Reason for this PR

In certain instances, like learned round, the weight tensors has extra parameters attached to it that could make export somewhat complicated.

Changes Made in this PR

We perform a destructive replacement of the original weight tensor with its quantized counterparts. This allows for easier exports in many scenarios.

There is an optional flag to keeep track of the original weights.

After merging, we reset the quantizers, including setting rounding mode to round, which is what is most commonly supported during export process.

What is missing:

  • Integration in the LLM entrypoint.
  • Handling the WeightWithInputQuantizer (i.e., the one required by A2Q and A2Q+)

Testing Summary

New tests added.

@Giuseppe5
Giuseppe5 requested a review from pablomlago April 9, 2026 08:38
@Giuseppe5 Giuseppe5 self-assigned this Apr 9, 2026

@nickfraser nickfraser left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

See comments

Comment thread src/brevitas/nn/utils.py Outdated
@Giuseppe5 Giuseppe5 added next release PRs which should be merged for the next release labels Apr 20, 2026
Comment thread src/brevitas/nn/utils.py Outdated
Comment thread src/brevitas/nn/utils.py
Comment thread src/brevitas/nn/utils.py Outdated
Comment thread src/brevitas/nn/utils.py Outdated
Comment thread src/brevitas/nn/utils.py Outdated
@staticmethod
def _reset_quantizer(proxy) -> None:
"""Switch a weight quant proxy from LearnedRound back to standard Round."""
reinit_on_state_dict = config.REINIT_ON_STATE_DICT_LOAD

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This pattern of overriding values in config and then restoring to the original values appears multiple times. Can we extract this common functionality? E.g.:

from contextlib import contextmanager
@contextmanager
def override_config(**overrides):
    old = {}
    try:
        for k, v in overrides.items():
            old[k] = getattr(config, k)
            setattr(config, k, v)
        yield
    finally:
        for k, v in old.items():
            setattr(config, k, v)

and then use it like:

with override_config(
        REINIT_ON_STATE_DICT_LOAD=False,
        IGNORE_MISSING_KEYS=True,
    ):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm going to open this as an issue and do it specifically in its own PR

Comment thread tests/brevitas/nn/test_merge_quant_weights.py Outdated
Comment thread tests/brevitas/nn/test_merge_quant_weights.py Outdated
Comment thread tests/brevitas/nn/test_merge_quant_weights.py
Comment thread src/brevitas/nn/utils.py
Comment thread src/brevitas/nn/utils.py Outdated

@pablomlago pablomlago left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, feel free to ignore the following change.

Comment thread src/brevitas/nn/utils.py
@Giuseppe5
Giuseppe5 requested a review from pablomlago June 10, 2026 10:07
@Giuseppe5
Giuseppe5 requested review from pablomlago and removed request for pablomlago June 10, 2026 12:27
@Giuseppe5
Giuseppe5 requested review from pablomlago and removed request for pablomlago June 16, 2026 11:22
@Giuseppe5
Giuseppe5 merged commit fe92b59 into Xilinx:dev Jun 16, 2026
572 of 585 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

next release PRs which should be merged for the next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants