Repository navigation
Add custom metal kernel for bilinear upsampling - #4812
Open
starkAhmed43 wants to merge 1 commit into
Open
starkAhmed43 wants to merge 1 commit into
starkAhmed43 wants to merge 1 commit into
Conversation
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.
Closes #4435
Related: #4418
This PR speeds up bilinear upsampling on Apple Silicon. It adds a fused Metal kernel for the forward pass and one for the backward pass.
What changed
mx.fast.upsample_bilinear(x, scale_factor, align_corners).nn.Upsampleuses the primitive in linear mode for 4-D input without antialiasing.Results
I measured on a base M1 with 16 GB. The baseline is stock
nn.Upsamplein MLX 0.32.4. Each number is the median of 10 fresh-process runs.Upsample shapes from the issue, in ms (F = forward, F+B = forward + backward):
#4418 is slower than stock on these shapes. It did not trigger on the issue's shapes.
The issue also lists two downscale shapes: 768×1280 ×0.5 and ×0.25. Speedups there are 7.1–11.1× over stock. The full tables below include them.
Full results for all 53 shapes
All times are in ms. Each number is the median of 10 fresh-process runs.
Forward:
Forward + backward:
Limits
vmap/jvp. The fallback gives the same values and no speedup.Tests
I added 13 tests in
test_fast.py, plus an export round trip.Questions for maintainers
mx.fast.upsample_bilinearstay public? The other option is to make it internal and use it only throughnn.Upsample.☑️ I understand it is strictly prohibited to use AI to write PR description
AI usage disclosure:
I wrote the Metal kernels. An AI assistant reviewed them for errors and refactored them for readability and wrote the C++ primitive, the Python binding, the
nn.Upsamplechange and the tests. It also wrote the benchmark and analysis scripts. I set the direction, made the design decisions and reviewed the changes and results.