Performance improvements: type stability for OperatorConv and meshgrid#86
Closed
ChrisRackauckas-Claude wants to merge 1 commit intoSciML:mainfrom
Closed
Conversation
This commit improves type stability in two key areas:
1. OperatorConv - Replace NNlib.pad_constant (type-unstable) with a
custom type-stable pad_zeros_spatial function. This ensures the
forward pass returns concrete types instead of Any.
2. meshgrid - Change signature from AbstractVector... to
Vararg{AbstractVector, N} to make the number of dimensions a
type parameter, enabling full type stability.
Benchmark results show modest improvements:
- SpectralConv 1D: ~96 bytes fewer allocations
- SpectralConv 2D: ~112 bytes fewer allocations
- SpectralKernel 1D: ~144 bytes fewer allocations, ~2% faster
- FourierNeuralOperator 1D: ~1136 bytes fewer allocations, ~1.6% faster
The key improvement is type stability which helps the compiler
optimize better and may enable further optimizations in complex
code paths.
Also adds performance regression tests that verify:
- Type stability of key functions
- Allocation bounds for model forward passes
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Member
|
Not GPU |
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.
Summary
This PR improves type stability in the core spectral convolution layer, resulting in modest performance improvements and cleaner generated code.
Changes
OperatorConv - Replace
NNlib.pad_constant(type-unstable) with a custom type-stablepad_zeros_spatialfunction. This ensures the forward pass returns concrete types instead ofAny.meshgrid - Change signature from
AbstractVector...toVararg{AbstractVector, N}to make the number of dimensions a type parameter, enabling full type stability.Performance tests - Added new performance regression tests that verify:
Benchmark Results
The key improvement is type stability which helps the compiler optimize better. Before the fix,
OperatorConvreturnedTUPLE{ANY, @NAMEDTUPLE{}}. After the fix, it returnsTuple{Array{Float32, 3}, @NamedTuple{}}(concrete types).Test plan
@code_warntypeNote: The full test suite has some Reactant-related issues (SIGSEGV in worker process) that appear to be pre-existing and unrelated to these changes.
🤖 Generated with Claude Code
/cc @ChrisRackauckas