Skip to content

Comments

Performance improvements: type stability for OperatorConv and meshgrid#86

Closed
ChrisRackauckas-Claude wants to merge 1 commit intoSciML:mainfrom
ChrisRackauckas-Claude:perf-improvements-20260107-160035
Closed

Performance improvements: type stability for OperatorConv and meshgrid#86
ChrisRackauckas-Claude wants to merge 1 commit intoSciML:mainfrom
ChrisRackauckas-Claude:perf-improvements-20260107-160035

Conversation

@ChrisRackauckas-Claude
Copy link
Contributor

Summary

This PR improves type stability in the core spectral convolution layer, resulting in modest performance improvements and cleaner generated code.

Changes

  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.

  3. Performance tests - Added new performance regression tests that verify:

    • Type stability of key functions
    • Allocation bounds for model forward passes

Benchmark Results

Layer/Model Before (median) After (median) Before Allocs After Allocs
SpectralConv 1D 206 μs 205 μs 255872 bytes 255776 bytes
SpectralConv 2D 298 μs 302 μs 597792 bytes 597680 bytes
SpectralKernel 1D 276 μs 271 μs 512672 bytes 512528 bytes
FourierNeuralOp 1D 3.07 ms 3.02 ms 3131936 bytes 3130800 bytes
DeepONet 8.29 μs 8.3 μs 3712 bytes 3712 bytes
NOMAD 5.88 μs 5.8 μs 3216 bytes 3216 bytes

The key improvement is type stability which helps the compiler optimize better. Before the fix, OperatorConv returned TUPLE{ANY, @NAMEDTUPLE{}}. After the fix, it returns Tuple{Array{Float32, 3}, @NamedTuple{}} (concrete types).

Test plan

  • Run sanity tests on all models (SpectralConv, FourierNeuralOperator, DeepONet, NOMAD)
  • Verify type stability improvements with @code_warntype
  • Compare allocation counts before/after

Note: 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

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>
@ChrisRackauckas
Copy link
Member

Not GPU

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.

3 participants