Skip to content

Add Torch grid interpolation using grid_sample with autograd support#493

Merged
HarrisonKramer merged 2 commits intoHarrisonKramer:masterfrom
gustavu92:feature/torch-grid-interpolator
Mar 1, 2026
Merged

Add Torch grid interpolation using grid_sample with autograd support#493
HarrisonKramer merged 2 commits intoHarrisonKramer:masterfrom
gustavu92:feature/torch-grid-interpolator

Conversation

@gustavu92
Copy link
Contributor

Add Torch support for grid-based phase interpolation

This PR adds full Torch backend support for grid-based phase profiles using torch.nn.functional.grid_sample.

Key updates:

  • Implement backend-agnostic GridInterpolator
  • Add Torch support with autograd for accurate gradients
  • Ensure gradients are computed in physical coordinates
  • Keep NumPy backend using RectBivariateSpline
  • Unify tests for NumPy and Torch backends
  • Restore consistent serialization for grid and height profiles

Closes #492.

@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

❌ Patch coverage is 65.00000% with 35 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
optiland/phase/interpolators.py 50.70% 35 Missing ⚠️

❌ Your patch status has failed because the patch coverage (65.00%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #493      +/-   ##
==========================================
- Coverage   93.19%   93.05%   -0.15%     
==========================================
  Files         310      311       +1     
  Lines       18381    18437      +56     
==========================================
+ Hits        17130    17156      +26     
- Misses       1251     1281      +30     
Files with missing lines Coverage Δ
optiland/phase/grid.py 100.00% <100.00%> (+7.89%) ⬆️
optiland/phase/height_profile.py 97.61% <100.00%> (+3.74%) ⬆️
optiland/phase/interpolators.py 50.70% <50.70%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gustavu92 gustavu92 force-pushed the feature/torch-grid-interpolator branch from 7f3e20c to a897290 Compare February 24, 2026 09:32
@gustavu92 gustavu92 force-pushed the feature/torch-grid-interpolator branch from a897290 to 0be404c Compare February 24, 2026 09:33
@HarrisonKramer
Copy link
Owner

Hi @gustavu92,

Thanks for adding this! It was on the todo list for a long time and I am very glad it's now implemented. Merging now!

Regards,
Kramer

@HarrisonKramer HarrisonKramer merged commit ee6fa4c into HarrisonKramer:master Mar 1, 2026
10 of 11 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.

Add Torch backend support for GridPhaseProfile and HeightProfile using grid_sample with autograd

2 participants