Fix LRR apply: detect block-diagonal clusters from the weight matrix - #20
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a correctness bug in LRRTransformer where applying pre-fit/loaded weights could silently produce an incorrect rereference when the transformer’s cluster hint (derived from block_size / unresolved cluster_by_field) didn’t match the true block-diagonal structure encoded in the weight matrix.
Changes:
- Build the internal
AffineTransformTransformerwithout passingchannel_clusters, so block structure is derived from the weight matrix itself. - Add a regression test covering non-contiguous clusters with a mismatched (finer)
block_size, asserting apply matchesX @ (I - W).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/ezmsg/learn/process/ssr.py |
Stops passing potentially incorrect cluster hints when constructing the affine transform from weights, preventing silent overwrite in block-apply optimization. |
tests/unit/test_ssr.py |
Adds a regression test ensuring apply follows the weight matrix’s true block structure even when block_size is incompatible. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
kylmcgr
marked this pull request as ready for review
August 6, 2026 16:29
Contributor
Author
|
@cboulay ready for review |
This was referenced Aug 7, 2026
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.
Summary
LRRTransformercould silently apply the wrong rereference when the fitted/loaded weight matrix is block-diagonal over clusters that don't match the transformer'sblock_size— most notably whencluster_by_fieldgroups channels non-contiguously (e.g. an electrode array split across two connector banks). The decode/output looked valid but each block was rereferenced against only a subset of its channels.Root cause
_on_weights_updatedbuilt the internalAffineTransformwithchannel_clusters=self._get_channel_clusters(n). That call runs when the weights are set — at construction for a pre-fit/loaded W, before any message has arrived — socluster_by_fieldhasn't been resolved yet and it falls back toblock_sizeclusters. When those fallback clusters are finer than the W's real blocks, two input sub-groups of one true block resolve to the same output indices. The affine's block-diagonal matmul writes with assignment (result[out_idx] = chunk @ subW, not+=), so the second sub-group silently overwrites the first — leaving each block rereferenced against only part of its channels. (Contiguous / matching clusters, e.g. bank-aligned, were unaffected, which is why this went unnoticed.) The cluster hint is only a performance optimization; it is not needed for correctness, since the weight matrix already encodes its own block structure.Fix
Pass
channel_clusters=Noneso the affine auto-detects the block-diagonal structure directly from the weight matrix — the single source of truth — removing any fit/apply cluster mismatch. Auto-detection runs once at first build; adaptive weight updates reuse it via the in-placeset_weightspath, so there's no added per-update cost and the block-diagonal speedup is retained.Testing
New regression test
TestApplyFollowsWeightBlocks: fits a W over two non-contiguous 64-ch groups, applies it with a mismatchedblock_size=32, and asserts the output equalsX @ (I − W). Fails onmain, passes with this change. Fulltests/unit/test_ssr.pysuite (41 tests) passes.Follow-up?
ezmsg-sigproc'sAffineTransformwill silently mis-computes for any caller that passeschannel_clustersnot matching the weights' nonzero structure. Should we add a PR there to validate/auto-correct/raise on a cluster–weight mismatch?