Repository navigation
fix: load Affine bias into the module's bias parameter - #55
Merged
Merged
Conversation
_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.
w4ffl35
force-pushed
the
fix/affine-bias
branch
from
September 16, 2026 00:07
72737ca to
5d966c5
Compare
Jegp
approved these changes
Sep 18, 2026
Jegp
left a comment
Collaborator
There was a problem hiding this comment.
Solid catch! That's definitely a bug and I appreciate you picking this up and even writing tests for it. Nice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
_default_map_affineassigns the NIR node's bias tomodule.weight.bias, whichattaches an attribute to the weight tensor and never touches
module.bias:So the randomly-initialised bias that
torch.nn.Linear(..., bias=True)createsis what gets used, and an
nir.Affinenode mapped throughDEFAULT_MAPsilently computes
weight @ x + <random>instead ofweight @ 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_conv1dand_default_map_conv2din the same file already usemodule.bias.data; this makesAffineconsistent with them.Reproduction
nirtorch2.6 (also present onmainat 2604ce2),nir1.0.8, torch 2.14.0,Python 3.13.5:
Scope
Affects the default
Affinemapping — i.e.nir_to_torch(graph, node_map={})or any call relying on
DEFAULT_MAPfornir.Affine.Callers that supply their own
Affinemapper are unaffected. I checked snnTorchspecifically: its importer registers its own
nir.Affinehandler which setsmod.bias.data, and I confirmed empirically thatimport_from_nirpreserves thebias. 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 existingconv tests' pattern of asserting weight, bias and forward output:
test_map_affine_node— bias chosen well outside the range ofweight @ 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 thepart 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.pyexcluded — it importssinabs, which I did not install, so those tests were not run at all.I left the unrelated pre-existing
blackdrift in_can_reachalone.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.