Skip to content

Fix TimeDistributed for non-contiguous sequence batches - #1220

Merged
bruAristimunha merged 6 commits into
braindecode:masterfrom
lindicaphxag-tech:fix/time-distributed-noncontiguous
Oct 5, 2026
Merged

bruAristimunha merged 6 commits into
braindecode:masterfrom
lindicaphxag-tech:fix/time-distributed-noncontiguous

Conversation

@lindicaphxag-tech

Copy link
Copy Markdown
Contributor

Summary

Make TimeDistributed accept non-contiguous input tensors by using reshape instead of requiring view-compatible strides.

Root cause

TimeDistributed.forward() currently flattens and restores the batch/sequence axes with .view(...). A tensor can have the documented shape (batch, seq, channels, time) while being non-contiguous—for example after a transpose or other layout-preserving upstream operation. In that case .view raises even though the tensor shape is otherwise valid for the layer.

Fix

Use .reshape for both the flatten and restore operations. Contiguous inputs keep the same behavior; non-contiguous inputs are copied only when required by the underlying strides.

Regression

The test constructs a non-contiguous tensor whose shape remains (4, 4, 3, 10), verifies the current view path is not required, and checks that TimeDistributed(nn.Identity()) returns (4, 4, 30) with the expected values.

Independent CPU reproduction:

  • current implementation: raises RuntimeError: view size is not compatible with input tensor's size and stride;
  • reshape implementation: succeeds and matches explicit reshaping exactly.

AI assistance was used to audit the tensor-layout boundary and prepare the patch; the change is limited to layout handling plus regression coverage.

@codecov

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.79%. Comparing base (dfc19d0) to head (a733552).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1220   +/-   ##
=======================================
  Coverage   87.79%   87.79%           
=======================================
  Files         149      149           
  Lines       17462    17462           
=======================================
  Hits        15330    15330           
  Misses       2132     2132           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Resolved docs/whats_new.rst by keeping both changelog entries.

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

Thanks. Reproduced the non-contiguous batch failure on master, verified the fix keeps contiguous inputs bit-identical for all TimeDistributed callers. CI green.

Resolved docs/whats_new.rst by keeping both changelog entries.

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

Thanks. Reproduced the non-contiguous batch failure on master, verified the fix keeps contiguous inputs bit-identical for all TimeDistributed callers. Tests green; the only red check is the Codecov upload (codecov.io SSL error, infra).

@bruAristimunha
bruAristimunha merged commit 2426ddc into braindecode:master Oct 5, 2026
30 of 35 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