Skip to content

FIX invalidate AvgPool2dWithConv cache on exact device changes - #1217

Merged
bruAristimunha merged 6 commits into
braindecode:masterfrom
lindicaphxag-tech:fix/avgpool-exact-device-cache-clean
Oct 5, 2026
Merged

bruAristimunha merged 6 commits into
braindecode:masterfrom
lindicaphxag-tech:fix/avgpool-exact-device-cache-clean

Conversation

@lindicaphxag-tech

Copy link
Copy Markdown
Contributor

Summary

Fix AvgPool2dWithConv so its cached averaging kernel is reused only when shape, dtype, and the exact device still match the current input.

The current cache validity check compares only is_cuda and tensor type. That treats cuda:0 and cuda:1 as equivalent, so a module moved between CUDA devices can reuse a stale cached kernel and fail inside conv2d with a device mismatch. The same coarse distinction can also be wrong for non-CUDA backends.

Fix

  • compare self._pool_weights.device == x.device;
  • compare dtype directly;
  • rebuild the cached kernel with .to(device=x.device, dtype=x.dtype).

Regression coverage

Adds tests that:

  • verify AvgPool2dWithConv remains numerically equivalent to avg_pool2d;
  • verify the cached kernel tracks the input device/dtype;
  • explicitly distinguish cuda:0 from cuda:1 at the cache-validity boundary without requiring a CUDA runner.

This is a narrow cache-correctness fix; pooling semantics and the public API are unchanged.

@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.96%. Comparing base (2426ddc) to head (fab24db).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1217      +/-   ##
==========================================
+ Coverage   87.84%   87.96%   +0.12%     
==========================================
  Files         151      151              
  Lines       17602    17609       +7     
==========================================
+ Hits        15462    15490      +28     
+ Misses       2140     2119      -21     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bruAristimunha bruAristimunha added the maintenance Bug fix / refactor / tests — not a new model label Oct 5, 2026
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 stale-cache bug on master; exact-device invalidation verified; default outputs unchanged. Import order fixed for ruff.

@bruAristimunha
bruAristimunha merged commit 6c1d0a9 into braindecode:master Oct 5, 2026
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Bug fix / refactor / tests — not a new model

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants