feat(ad): replace CRC extension with native Enzyme and Mooncake rules#83
Merged
feat(ad): replace CRC extension with native Enzyme and Mooncake rules#83
Conversation
…ights
Mooncake's @from_rrule bridge fails with Vector{Vector{Float64}} tangents
from ChainRulesCore rrules. This commit replaces the bridged rules with
native Mooncake rrule!! implementations.
Key changes:
- Add @is_primitive declarations for all _build_weights signatures
- Implement native rrule!! for Laplacian/Partial with PHS/IMQ/Gaussian
- Use zero_fcodual() for proper FData output type
- Read gradients from FData.data.nzval (not RData) in pullback
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move backward pass infrastructure (caches, shape parameter derivatives, operator second derivatives) from extensions into the main package so both Enzyme and Mooncake extensions can share it. Remove ChainRulesCore extension entirely — AD is now provided via native EnzymeRules and Mooncake rrule!! implementations.
Replace CRC-bridged rules with direct EnzymeRules for basis functions, operators, interpolators, and _build_weights. Add shape parameter (ε) differentiation tests for IMQ and Gaussian bases.
Replace @from_rrule CRC bridge with native Mooncake rrule!! implementations for all AD entry points. Add shape parameter (ε) differentiation tests for IMQ and Gaussian bases.
Replace Enzyme basis macro with single AbstractRadialBasis rule, unify
_build_weights rules via Union{Partial, Laplacian} and shared pullback
core, consolidate Mooncake @is_primitive declarations, and extract
shared test utilities to eliminate ~520 lines of duplication.
IMQ/Gaussian have active ε field, so NoRData() causes increment!! type mismatch when basis is captured in a closure.
Keep our branch's native Enzyme/Mooncake rules, delete CRC extension files and tests from main's enzyme PR (#76). Retain DI tests and docs additions from main.
Contributor
Benchmark Results
Benchmark PlotsA plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR. |
The Interpolator(x, y, basis) constructor calls Symmetric(A) \ b which hits unsupported LAPACK foreigncalls in Mooncake. This adds a primitive that runs the solve opaquely in the forward pass and uses the implicit function theorem (Δy = (A⁻¹ [Δw_rbf; Δw_mon])[1:k]) in the backward pass to propagate cotangents back to y.
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
- Use ::Type{RT} where {RT} instead of ::Type{<:Active} for
array-returning rules (operator call, _eval_op, batch interpolator)
so Enzyme can match Duplicated return types
- Add xi::Const basis function rule variant for closures using
function_annotation=Enzyme.Const
- Fix SVector immutability in _build_weights reverse by using
assignment instead of .+= on immutable static array elements
- Add trailing comma in Mooncake @is_primitive to satisfy Runic
Remove RadialBasisOperator from Enzyme tape to avoid LLVM IR verification errors on Julia 1.11. The operator is Const and accessible via op.val in the reverse pass. Also handle DuplicatedNoNeed return type in shadow allocation and remove update_weights! from augmented_primal since Const operators have pre-computed weights.
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
EnzymeRulesand Mooncakerrule!!implementationssrc/solve/backward.jlandsrc/solve/ad_shared.jl, eliminating duplication between extensionstest/extensions/ad_test_utils.jl)RadialBasisFunctionsChainRulesCoreExtextension (~960 lines deleted)13 commits, 25 files changed, +2280 / -1269 lines. All tests green.
Test plan
Pkg.test())rrule!!-based differentiation