Skip to content

Add a Conv2D layer, and stop its gradient going through a batched matmul - #99

Merged
jessegrabowski merged 4 commits into
pymc-devs:mainfrom
jessegrabowski:conv2d
Aug 20, 2026
Merged

Add a Conv2D layer, and stop its gradient going through a batched matmul#99
jessegrabowski merged 4 commits into
pymc-devs:mainfrom
jessegrabowski:conv2d

Conversation

@jessegrabowski

Copy link
Copy Markdown
Member

We had Conv1D and no 2-D convolution. Conv2D is n_spatial = 2 and a docstring, because the layer base and the op under it were already rank-generic — so the substance here is what rank 2 turned up.

The kernel gradient was going through a batched contraction instead of one GEMM. _correlate contracted a 4-D patch tensor against a 2-D kernel, and pt.grad reads the graph as written, before rewrites normalize the forward. Collapsing the batch and window axes first makes the gradient two plain matmuls — 1.5–3.2x on dW, forward unchanged. Separately, every backend dispatch test ran at rank 1, so a 2-D convolution reached jax, mlx and torch with nothing checking the result. Each now has a rectangular-kernel case, which is what catches a transposed spatial axis.

Measured 1.5-3.2x on the kernel gradient across CNN-shaped layers; the forward is unchanged, since the rewriter already normalizes both forms to the same graph.
@codecov-commenter

codecov-commenter commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.95%. Comparing base (65c97b5) to head (047f68a).

❗ There is a different number of reports uploaded between BASE (65c97b5) and HEAD (047f68a). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (65c97b5) HEAD (047f68a)
8 6
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #99      +/-   ##
==========================================
- Coverage   97.38%   89.95%   -7.44%     
==========================================
  Files          56       56              
  Lines        2604     2609       +5     
==========================================
- Hits         2536     2347     -189     
- Misses         68      262     +194     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Nothing downstream could size itself from a conv stack before this: every output extent came back None even when the input was fully known.
@jessegrabowski
jessegrabowski merged commit af3297f into pymc-devs:main Aug 20, 2026
10 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