Repository navigation
Conversation
The Metal extension only implemented conv!, ∇conv_data! and ∇conv_filter! for
DenseConvDims{2}. 1D convolutions went to the generic im2col path, which calls
CPU BLAS with Metal pointers and errors (MethodError: no method matching gemm!).
Run them as 2D convolutions with a singleton second spatial dimension (reshape
shares the buffer). Adds 1D forward/backward tests to test/gpu/metal/conv_pool.jl
(Float16/Float32, groups 1 and 2, flipkernel), mirroring the 2D tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
The Metal extension implements
conv!,∇conv_data!and∇conv_filter!with MPSGraph forDenseConvDims{2}(4-dimensional arrays). For 1D convolutions (DenseConvDims{1}, 3-dimensionalarrays) there is no method, so they go to the generic
im2colpath. That path calls CPU BLAS withMetal pointers:
Lux hits this in the backward pass of any 1D
Convon Metal. Its forward pass uses its own fallback,but the gradients go through
NNlib.∇conv_data/∇conv_filter.Change
A 1D convolution is a 2D convolution with a singleton second spatial dimension:
(W, C, N)becomes(W, 1, C, N)and the kernel(k, Cin, Cout)becomes(k, 1, Cin, Cout), with stride 1, padding 0and dilation 1 in that dimension.
reshapeon anMtlArrayshares the buffer, so nothing is copied.Grouped and flipped kernels carry over unchanged.
These methods go in
ext/NNlibMetalExt.jl, after the 2D methods. Withkwargs(thealphaand
betaofconv!) they keep today's behavior, like the 2D methods.Tests (added to
test/gpu/metal/conv_pool.jl)These follow the format of the existing 2D tests. They pass on an Apple M6 (macOS 27.0.1, Julia 1.13.4,
Metal.jl 1.12.1): 48/48. All of
test/gpu/metal/conv_pool.jlon this branch passes too:102/102.
Results
conv,∇conv_dataand∇conv_filter, with k = 3, 4, 5, stride 1–3, asymmetric padding (2, 1), dilation 2 andflipkernel.Convon Metal (forward plus gradients of x and W), which errors today:Conv((5,), 64=>128, relu; pad=2)on 256×64×64 now takes 6.3 ms and matches the CPU to 7e-7;DepthwiseConvDims. Both still go tothe generic path.
🤖 Generated with Claude Code