Skip to content

fix: load Affine bias into the module's bias parameter - #55

Merged
Jegp merged 1 commit into
neuromorphs:mainfrom
w4ffl35:fix/affine-bias
Sep 18, 2026
Merged

Jegp merged 1 commit into
neuromorphs:mainfrom
w4ffl35:fix/affine-bias

Conversation

@w4ffl35

@w4ffl35 w4ffl35 commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

The defect

_default_map_affine assigns the NIR node's bias to module.weight.bias, which
attaches an attribute to the weight tensor and never touches module.bias:

module.weight.data = torch.from_numpy(linear.weight)
module.weight.bias = torch.from_numpy(linear.bias)   # should be module.bias.data

So the randomly-initialised bias that torch.nn.Linear(..., bias=True) creates
is what gets used, and an nir.Affine node mapped through DEFAULT_MAP
silently computes weight @ x + <random> instead of weight @ x + bias.

There is no exception and no warning — the output looks plausible, and it
differs on every rebuild because the bias is random.

_default_map_conv1d and _default_map_conv2d in the same file already use
module.bias.data; this makes Affine consistent with them.

Reproduction

nirtorch 2.6 (also present on main at 2604ce2), nir 1.0.8, torch 2.14.0,
Python 3.13.5:

import numpy as np, nir
from nirtorch.nir_interpreter import _default_map_affine

node = nir.Affine(weight=np.array([[1., 2., 3.], [.5, -1., .25]]),
                  bias=np.array([7., -9.]))
m = _default_map_affine(node)

m.bias.data      # -> tensor([0.2482, -0.4959])   random init, not the node's bias
m.weight.bias    # -> tensor([ 7., -9.])          the real bias, on the wrong object

Scope

Affects the default Affine mapping — i.e. nir_to_torch(graph, node_map={})
or any call relying on DEFAULT_MAP for nir.Affine.

Callers that supply their own Affine mapper are unaffected. I checked snnTorch
specifically: its importer registers its own nir.Affine handler which sets
mod.bias.data, and I confirmed empirically that import_from_nir preserves the
bias. So this is not a downstream-wide issue, and I am not claiming it is.

I did not find an existing issue or PR for this.

Tests

Two regression tests in tests/test_nir_interpreter.py, following the existing
conv tests' pattern of asserting weight, bias and forward output:

  • test_map_affine_node — bias chosen well outside the range of weight @ x,
    so a dropped or randomly-initialised bias cannot pass by coincidence.
  • test_map_affine_node_zero_bias — pins an explicitly zero bias. This is the
    part that distinguishes dropped from replaced: a random bias fails it,
    a merely dropped one would not.

Fixed values rather than np.random, so a failure is reproducible.

Before the fix both fail. After it, the selected suite reports
67 passed, 1 skipped, with tests/test_graph.py excluded — it imports
sinabs, which I did not install, so those tests were not run at all.

I left the unrelated pre-existing black drift in _can_reach alone.


Prepared with Claude acting as a coding agent. The agent executed the reported
regression tests locally. No separate human review is claimed. Happy to adjust
the tests to whatever style you prefer.

_default_map_affine assigned the NIR node's bias to `module.weight.bias`,
which attaches an attribute to the weight *tensor* and never touches
`module.bias`. The randomly-initialised bias created by `torch.nn.Linear`
is therefore used for inference, so an Affine node imported through the
default map silently computes `weight @ x + <random>` instead of
`weight @ x + bias`.

    module.weight.data = torch.from_numpy(linear.weight)
    module.weight.bias = torch.from_numpy(linear.bias)   # -> module.bias.data

_default_map_conv1d and _default_map_conv2d in the same file already use
`module.bias.data`; this makes Affine consistent with them.

Add two regression tests, mirroring the existing conv tests' assertions on
weight, bias and forward output:

- test_map_affine_node uses a bias well outside the range of `weight @ x`,
  so a dropped or randomly-initialised bias cannot pass by coincidence.
- test_map_affine_node_zero_bias pins an explicitly zero bias. It separates
  "the bias is dropped" from "the bias is replaced": a random bias fails it,
  a dropped one would not.

Both use fixed values rather than np.random so a failure is reproducible.

@Jegp Jegp 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.

Solid catch! That's definitely a bug and I appreciate you picking this up and even writing tests for it. Nice.

@Jegp
Jegp merged commit 1ad5e67 into neuromorphs:main Sep 18, 2026
12 checks passed
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.

2 participants