Add support for Warp Backend for Gradient-Based Optimization#163
Open
Medyan-Naser wants to merge 6 commits into
Open
Add support for Warp Backend for Gradient-Based Optimization#163Medyan-Naser wants to merge 6 commits into
Medyan-Naser wants to merge 6 commits into
Conversation
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.
Contributing Guidelines
Description
This PR enables automatic differentiation for the Warp backend, allowing gradient-based optimization through multi-step LBM simulations. All operators have been modified to be compatible with Warp's tape-based reverse-mode AD while preserving accuracy.
closes #161
Final iteration for differentiable example using WARP and JAX:
JAX:

WARP:

Convergence:
JAX:

WARP:

Type of change
Core Operator Modifications
Four operators were modified to remove non-differentiable control flow that breaks Warp's autodiff:
1.
xlb/operator/macroscopic/zero_moment.py- Density Computationif-elsebranches)2.
xlb/operator/macroscopic/first_moment.py- Velocity Computation3.
xlb/operator/stream/stream.py- Streaming Operatorif-else)4.
xlb/operator/stepper/nse_stepper.py- NSE StepperUsage Requirements
Critical Pattern for Multi-Step Optimization
Warp's tape-based AD requires pre-allocating all intermediate states:
Common Mistake (Breaks Autodiff)
Why it fails: Each
wp.zeros_like()creates a new array, breaking the computational graph. Gradients cannot flow back through severed connections.How Has This Been Tested?
The single test failure also exist on the main branch.
Validation Results
Single-Step Gradient Accuracy
Forward Simulation Consistency
Multi-Step Optimization (Actual Example)
Conclusion: Warp optimization achieves identical performance to JAX when using correct pattern.
Linting and Code Formatting
Make sure the code follows the project's linting and formatting standards. This project uses Ruff for linting.
To run Ruff, execute the following command from the root of the repository:
ruff check .