Skip to content

fix(inference): apply g_max reference correctly in pcm and reram noise models - #846

Merged
PabloCarmona merged 5 commits into
masterfrom
fix/830-noise-model-g-max-reference
Nov 25, 2026
Merged

PabloCarmona merged 5 commits into
masterfrom
fix/830-noise-model-g-max-reference

Conversation

@PabloCarmona

@PabloCarmona PabloCarmona commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Related issues

Fixes #830

Description

Fixes how PCMLikeNoiseModel and ReRamWan2022NoiseModel handle the g_max reference used to scale programming noise, following the approach of HermesPCMNoiseModel. Passing a reference no longer raises an AttributeError, and ReRAM noise now scales with g_max. With the default coefficients, the reference is fixed and g_max is checked against it: a larger g_max is an error and a smaller one warns.

Details

Bugs fixed

  • ReRamWan2022NoiseModel(coeff_g_max_reference=...) raised AttributeError: ... no attribute 'coeff_g_max_reference' on the first noise call.
  • With the default coefficients, ReRAM stored the 40 µS fitting reference in prog_coeff_g_max_reference, but _apply_poly read coeff_g_max_reference, which was always g_max. The scale factor g_max / reference was therefore always 1, and the noise did not change with g_max.
  • PCMLikeNoiseModel(prog_coeff=..., prog_coeff_g_max_reference=...) raised the same AttributeError.

Reference and g_max handling (PCM, CustomDriftPCM, ReRAM)

  • Default coefficients: the reference is fixed to the characterized value, 25 µS for PCM and 40 µS for ReRAM. As in Hermes, a reference passed alongside the default coefficients is ignored. g_max can still be set.
  • Custom coefficients: the given reference is used, or g_max if none is given.
  • g_max check: BaseNoiseModel._check_g_max_reference checks the final g_max against the reference. It is shared by all three models.
    • g_max > prog_coeff_g_max_reference raises a ValueError, because the coefficients are not characterized beyond the reference.
    • g_max < prog_coeff_g_max_reference raises a UserWarning, because the scaled noise may differ from the measured hardware noise.
  • Converter path: unlike Hermes, which only checks the g_max argument, the check runs after g_max is resolved. A g_max that comes from g_converter is therefore checked too. It raises ValueError rather than using assert, which is removed under python -O and matches the existing errors in these constructors.
  • ReRAM default g_max: ReRamWan2022NoiseModel now uses g_max = 40 µS when neither g_max nor g_converter is given, the same way Hermes sets its default. Without this, the converter's 25 µS default would warn on every default construction.

Attribute naming and compatibility

  • ReRAM now stores the reference only as prog_coeff_g_max_reference, the same name the PCM and Hermes models use. The constructor argument is still coeff_g_max_reference, so the public API does not change.
  • ReRamWan2022NoiseModel.__setstate__ maps the old coeff_g_max_reference attribute when unpickling, so existing checkpoints still load.
  • utils/legacy.py now copies g_max only when it is set. This fixes a mypy error caused by the PCM reference attribute now being typed as float.

Behaviour changes

  • PCMLikeNoiseModel with g_max > 25, and ReRamWan2022NoiseModel with g_max > 40, now raise a ValueError when the default coefficients are used. All examples and library defaults use g_max=25 for PCM and are not affected.
  • ReRamWan2022NoiseModel() now defaults to g_max = 40 instead of 25.
  • ReRAM programming noise now scales with g_max / 40. Results at g_max = 40 are unchanged. Smaller values give proportionally less noise and a warning.

Tests

New NoiseModelGMaxReferenceTest in tests/test_inference.py, with 26 cases covering both models:

  • defaults
  • g_max equal to, larger than, and smaller than the reference, including a g_max that comes from the converter
  • a reference passed with the default coefficients is ignored
  • custom coefficients with and without a reference
  • the two AttributeError repros
  • the CustomDriftPCMLikeNoiseModel checks
  • with a fixed seed, doubling g_max doubles the noise
  • loading old ReRAM pickles

…e models

ReRamWan2022NoiseModel raised an AttributeError when coeff_g_max_reference was passed and, with the default coefficients, scaled the noise against g_max instead of the 40 uS fitting reference. The reference is now stored as prog_coeff_g_max_reference, consistent with the PCM models, and legacy pickles are mapped on load.

PCMLikeNoiseModel raised the same AttributeError when prog_coeff and prog_coeff_g_max_reference were given together.

Both models now warn when the calibrated reference (25 uS for PCM, 40 uS for ReRAM) is overridden while using the default coefficients, since this can produce noise values that differ from the measured hardware ones.

Signed-off-by: Pablo Carmona Gonzalez <pablocarmonagonzalez@gmail.com>
@PabloCarmona
PabloCarmona requested a review from anu-pub November 18, 2026 12:25
@PabloCarmona PabloCarmona self-assigned this Nov 18, 2026
@PabloCarmona PabloCarmona added the bug Something isn't working label Nov 18, 2026
PCMLikeNoiseModel.prog_coeff_g_max_reference is now inferred as float, so assigning the optional g_max in the legacy checkpoint conversion failed mypy. Only copy g_max when it is set, which the noise model constructor already guarantees.

Signed-off-by: Pablo Carmona Gonzalez <pablocarmonagonzalez@gmail.com>
…n hermes

Follow the HermesPCMNoiseModel approach: with the default coefficients the reference g_max is fixed (25 uS for PCM, 40 uS for ReRAM) and cannot be overridden, while g_max can be set. With custom coefficients the reference can still be given and falls back to g_max.

A g_max larger than prog_coeff_g_max_reference now raises a ValueError, since the coefficients are not characterized beyond it, and a smaller g_max issues a warning. The check lives in BaseNoiseModel and runs on the resolved g_max, so it also covers a g_max coming from the conductance converter. ReRamWan2022NoiseModel defaults g_max to 40 uS when neither g_max nor g_converter are given.

Signed-off-by: Pablo Carmona Gonzalez <pablocarmonagonzalez@gmail.com>
Comment thread src/aihwkit/inference/noise/pcm.py Resolved
Signed-off-by: Pablo Carmona Gonzalez <pablocarmonagonzalez@gmail.com>
@PabloCarmona
PabloCarmona requested a review from anu-pub November 23, 2026 15:48
Signed-off-by: Pablo Carmona Gonzalez <pablocarmonagonzalez@gmail.com>
@PabloCarmona
PabloCarmona merged commit d829ef7 into master Nov 25, 2026
16 of 17 checks passed
@PabloCarmona
PabloCarmona deleted the fix/830-noise-model-g-max-reference branch November 25, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReRamWan2022NoiseModel: explicit coeff_g_max_reference raises AttributeError; 40 µS reference unused?

2 participants