Skip to content

fix: library bugs found by the e2e tests (stacked on #70) - #71

Draft
soroushdeimi wants to merge 58 commits into
cnrl:mainfrom
soroushdeimi:fix/e2e-found-bugs
Draft

soroushdeimi wants to merge 58 commits into
cnrl:mainfrom
soroushdeimi:fix/e2e-found-bugs

Conversation

@soroushdeimi

@soroushdeimi soroushdeimi commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #70. The first 45 commits are the content of #70; please merge that one first. After it is merged this diff shrinks to the 10 commits below.

Summary

Fixes library bugs that the end-to-end tests in #70 uncovered. Each fix is one commit with a regression test that fails on the unfixed code, and the e2e tests that used to assert the buggy behaviour are updated in the same commit.

Fixes

  • fix(io_layer): OutputLayer built its NeuronGroups with positional arguments in the wrong order for pymonntorch 0.1.4, so the constructor raised AttributeError. It now passes keyword arguments.
  • fix(replication): save_ports crashed with AttributeError for a port whose behavior is None. behaviors_to_list(None) now returns an empty list, and a save/load round trip is tested with and without a behavior.
  • docs(replication): the type hint and docstring of build_ports described the wrong format. It takes the output of save_ports; a round-trip test is added.
  • fix(synapses): WeightInitializer converted sparse weights to CSC, which fails on torch 1.x ("Expected result Tensor to be of format CSR") and broke the Sparse*STDP rules. It keeps COO now and derives src_idx and dst_idx from it. A sparse STDP rule is tested end to end and stays inside its weight bounds.
  • fix(synapses): the One2One learning rules now raise a ValueError naming the rule and the shape when the weight is not 1D. One2OneDendriticInput already cannot work with a 2D weight, and the elementwise update would otherwise broadcast over the rows silently.
  • fix(spatial_pooler): winners were picked with torch.topk, whose tie order is platform dependent, so the same input gave different active columns on Linux and macOS. Ties now go to the lowest column index through a stable sort, checked against a plain Python reference.
  • fix(masks): GridEraseMask and GridCropMask imported torchvision in __init__, which does not import with torch 1.13.1. They use small torch helpers with the same semantics: crop zero-pads where the window leaves the image, so grids that do not divide the image still give equal-size tiles.
  • docs(history): adds an "Unreleased" section. 0.1.5 is already on PyPI, so these changes belong to the next version; the version bump is left to the release.

Verification

Run on Python 3.10 with the pinned torch==1.13.1 and pymonntorch==0.1.4, and on torch 2.14.1:

environment pytest tests
macOS arm64, torch 1.13.1 747 passed
Linux aarch64 (container), torch 1.13.1 747 passed
macOS arm64, torch 2.14.1 747 passed

flake8 conex tests prints nothing.

Probe results (pytest -q on tests/, macOS arm64, uv venvs):

pymonntorch 0.1.0 + torch 1.13.1: 47 failed, 87 passed, 100 errors
pymonntorch 0.1.1 + torch 1.13.1: 47 failed, 87 passed, 100 errors
pymonntorch 0.1.2 + torch 1.13.1: 41 failed, 93 passed, 100 errors
pymonntorch 0.1.3 + torch 1.13.1: 224 passed, 10 errors
pymonntorch 0.1.4 + torch 1.13.1: 234 passed
pymonntorch 0.1.4 + torch 2.14.1: 234 passed

Root cause: tests/conftest.py passes dtype= to Network, which only
pymonntorch >= 0.1.4 accepts. 0.1.4 is the smallest fully-green version.

sympy: pymonntorch imports it but does not declare it; torch 1.13.1
does not pull it in (torch 2.x does), so it must be listed explicitly.

numpy<2: torch 1.13.1 with numpy 2.2.6 fails
tests/test_voting.py::TestColumnVoting::test_partial_match_still_votes
(1 failed, 233 passed); numpy 1.26.4 gives 234 passed.

torch pin kept at 1.13.1: 0.1.4 passes with it on py3.8/3.9/3.10.
Runs on push to main, pull_request and workflow_dispatch.
- lint: flake8 on conex and tests with the project config
- test: matrix ubuntu-latest and macos-latest x python 3.8/3.9/3.10,
  fail-fast false, pip cache, pytest on tests/
- build: python -m build, twine check, install wheel in a fresh venv
- ci-ok: single check that fails unless lint, test and build all succeed

All six matrix cells have torch 1.13.1 wheels (checked on PyPI:
cp38/39/310 x manylinux1_x86_64, macosx_10_9_x86_64, macosx_11_0_arm64).
Remove F841 (unused local) and F811 (redefinition) from the flake8
extend-ignore and fix each site with the smallest change:
- dataset.py: drop duplicate `import torch` (F811)
- active_dendrites.py: drop unused device/dtype (x2) and predicted_activation
- grid_cells.py: drop unused n_cells in forward
- predictive_coding.py: drop unused pred_dim (keep the parameter() call,
  which registers the key in used_attr_keys), unused dt/tau/alpha, and
  two unused cfg reads
- sequence_memory.py: drop unused device

Full suite still 234 passed; flake8 clean.
- test job now runs `pytest tests/ --ignore=tests/e2e -n auto`
- new e2e job runs `pytest tests/e2e` on the same OS/python matrix;
  skips cleanly (exit 0) when tests/e2e does not exist yet
- pytest-xdist installed in the workflow only (not requirements.txt);
  kept because the suite is 234 passed and identical across 3 local runs
- ci-ok now needs lint, test, e2e and build

actionlint clean.
On torch 1.13.1 (the CI pin) WeightInitializer crashes: it converts the
weights to CSC (specs.py:189) then multiplies by scale (specs.py:198), and
torch 1.x cannot multiply a CSC sparse tensor by a scalar. Branch on
torch.__version__ inside the test: assert the RuntimeError on <2.0 and the
sparse density on >=2.0. No skip/xfail.
Remove unused imports (TimeResolution, seed, Conv2dSTDP, Local2dSTDP),
drop the unused 'n' binding, and wrap long lines / reword docstrings so
'flake8 tests/e2e' prints nothing. The library attribute 'I' (pymonntorch
API name) gets a targeted noqa: E741.
Resolve E127/E128 continuation-line indents and an F841 unused variable,
drop unused imports, and add a create_structure_from_dict test for the
NeuronGroup branch.
Replace the two-line note about the unmerged shared conftest with a single
line stating each file carries its own small network builder.
Add 4 end-to-end learning scenarios built from the public API that assert
on what the networks learn, not a single function:
- competitive STDP: Poisson input -> LIF+KWTA+homeostasis, per-pattern
  selectivity and weight-level preference, weights clipped to [0,1]
- reward-modulated: Payoff + Dopamine + SimpleRSTDP, rewarded pattern's
  weights grow more than the unrewarded one's
- dataset to network: Poisson-encoded SpikeNdDataset batches -> InputLayer
  -> readout, label-wise spike statistics and readout weight movement
- reproducibility matrix: same seed bit-identical (torch.equal), different
  seed differs, each scenario run twice in-process
Plus per-scenario failure/edge cases (zero input, saturated weights,
wrong shapes). Inputs are Poisson-encoded so networks are stochastic and
seed-dependent.
The selectivity was baked into a hand-biased 0.8/0.2 init, so the test
stayed green when SimpleSTDP.compute_dw was broken. Start from an unbiased
init (0.5 base + tiny seeded noise) and assert learning emerges as both LTP
and LTD being active (some weights potentiate, some depress). This fails for
a_plus=0 (no LTP) and for the LTD->LTP sign mutation (no LTD).
Raise a_plus and run more steps so the readout weight change is well above
the 0.5 -> 0.52 noise floor (max |dw| 0.116-0.500 across seeds 0-4), and
assert the direction per label: each label's active input channels
potentiate. Fails when a_plus=0 (no potentiation).
Record the min/max observed across seeds 0-4 for the RSTDP rewarded growth
(gA 0.219-0.257, gB 0.0) and the dataset readout change (max |dw| 0.116-0.500),
so the thresholds are justified by a sweep, not a single seed.
Add a scenario using the Intensity2Latency encoder (the task asked for it):
two intensity patterns drive a LIF readout over a time window. Assert the
latency-coding property (a stronger intensity spikes sooner, one spike per
channel) and that the readout weights learn from the encoded input.
pymonntorch 0.1.4's NeuronGroup signature is (size, net, behavior, tag);
the positional call passed the behavior dict as net, raising
AttributeError on construction. Mirror InputLayer's keyword call.

Add e2e regression tests: population construction, a full
input->output spiking pipeline, and user-defined behavior passthrough.
save_ports crashed with AttributeError on a port whose behavior is None.
The minimal correct serialization of 'no behavior' is the empty list:
build_ports rebuilds it via build_behavior_dict([]) == {}.

Drop the now-stale 'pre-existing bug' comments in the e2e tests.
…h 1.x

WeightInitializer converted sparse weights to CSC, which breaks the scalar
scale on torch 1.x ('Expected result Tensor to be of format CSR') and whose
row/col index accessors are unavailable on COO-free 1.x. Keep the weights in
COO and derive src_idx/dst_idx from .indices() (row=src, col=dst in the
default SxD mode), which works on both torch 1.x and 2.x.

Update test_weight_initializer_sparse_density to assert the fixed COO
behaviour on both versions instead of the torch-major branch that expected a
RuntimeError on <2.0. Add e2e regression tests for the sparse init and a
SparseSTDP run that stays within the weight bounds.
Winners were chosen with torch.topk on overlap counts. Overlaps tie often
and topk's tie order is platform dependent, so the same input produced
different active columns on Linux vs macOS. Replace topk with a stable
descending sort (equal scores keep their original, lowest-index order) and
take the first n_active, so the lowest-index column wins ties on every
platform. Output type/shape are unchanged.

Adds TestSpatialPoolerTieBreak: tied overlaps pick the lowest indices,
deterministic across runs and call order, matching a plain-python
reference, for both global and local inhibition.
build_ports consumes the serialized format save_ports emits, not the
original List[Port] format its old annotation claimed. Fix the type hint
and add a docstring stating it is the inverse of save_ports. No behaviour
change.

Add a regression test asserting build_ports(save_ports(x)) round-trips
object identity, label, v0 and behavior class.
A one-to-one connection has one weight per neuron pair, so the weight must
be 1D. With a 2D weight the elementwise update row-broadcasts (matching no
sensible rule) and One2OneDendriticInput emits a 2D I that crashes the
network step ('Index tensor must have the same number of dimensions as src
tensor'). One2OneSTDP and One2OneiSTDP now raise a ValueError in
initialize() naming the rule and the offending weight shape; One2OneRSTDP
inherits the check through both.

Update the three existing One2One rule tests to build a 1D weight
(weight_shape=(2,)) and add e2e regression tests: 2D weight -> ValueError
for all three rules, 1D weight -> rewarded pairing potentiates and zero
dopamine leaves the weights unchanged.
GridEraseMask and GridCropMask imported torchvision.transforms.functional
in __init__, which does not import under the pinned torch==1.13.1. Replace
the two calls (erase, crop) with plain torch slicing helpers and add
regression tests for both classes (shape, erased/cropped values, invalid
grid size).

GridEraseMask location marks kept cells (ones with the erased cell set to
False), so each output has m*n-1 True entries.
_crop clamped the window to the image bounds, so a non-divisible grid
(e.g. 7x7 with m=n=2) produced tiles of differing sizes and GridCropMask
raised 'stack expects each tensor to be equal size'. torchvision.crop
instead always returns (C, height, width), zero-padding where the window
overruns the image and zero-padding (not clamping) a negative top/left.
Rewrite _crop to match that contract and add regression tests (7x7 grid,
fully-outside window, negative offset).
torchvision.erase is a plain slice assignment with no clamping; the port
clamped top/left/height/width to 0, which diverged for negative or
out-of-range windows (e.g. top=-2,h=4 is a no-op in torchvision but the
clamped port erased rows 0-3). GridEraseMask always passes non-negative
offsets so behaviour is unchanged there, but the helper is now a faithful
drop-in. Verified against torchvision 0.29.2 on torch 2.x: 300/300 random
shapes/offsets match exactly (max abs diff 0). Added regression tests.
@soroushdeimi soroushdeimi changed the title fix: four library bugs found by the e2e tests (stacked on #70) fix: library bugs found by the e2e tests (stacked on #70) Nov 16, 2026
@saeedark

Copy link
Copy Markdown
Collaborator

please close this PR and open a new PR for each of fixes. As I glanced the fixes, they are rather small; each PR should be only one commit. I'll discuss my concerns in newly created ones.

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