EnzymeAD / EnzymeAD/Enzyme.jl

Error in differentiating `inv`(matrix) with LAPACK function `getrf!`

Open
#2,260 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
586
Forks
108
Avg merge
1d 5h
Merged PRs (30d)
44

Description

When using Trixi.jl with Enzyme.jl's automatic differentiation, I encountered an error with LAPACK's `getrf!` function:

```
ERROR: No augmented forward pass found for dgetrf_64_
```

The error occurs in the following function from Trixi.jl (src/solvers/dgsem/basis_lobatto_legendre.jl:771): https://github.com/junyixu/TrixiEnzyme.jl/issues/1#issuecomment-2580639062

I've distilled the problem into a minimal working example:

## MWE
```julia
using Enzyme

# Helper function for Legendre polynomials
function legendre_polynomial_and_derivative(n, x)
if n == 0
return 1.0, 0.0
elseif n == 1
return x, 1.0
end

p_prev = 1.0 # P₀(x)
p = x # P₁(x)
dp_prev = 0.0 # P₀'(x)
dp = 1.0 # P₁'(x)

for i in 2:n
p_new = ((2i-1)*x*p - (i-1)*p_prev)/i
dp_new = ((2i-1)*(p + x*dp) - (i-1)*dp_prev)/i
p_prev, dp_prev = p, dp
p, dp = p_new, dp_new
end
return p, dp
end

# Main function to test
function vandermonde_legendre(nodes, N::Integer)
n_nodes = length(nodes)
n_modes = N + 1
vandermonde = zeros(n_nodes, n_modes)
for i in 1:n_nodes
for m in 1:n_modes
vandermonde[i, m], _ = legendre_polynomial_and_derivative(m - 1, nodes[i])
end
end
return inv(vandermonde) # only return inverse for simplification
end

# Test function using vandermonde_legendre
function f(x)
nodes = [-1.0, 0.0, 1.0] # fixed nodes for testing
V_inv = vandermonde_legendre(nodes, 2)
return sum(V_inv) # return scalar output
end

# Try automatic differentiation
x = 1.0
result = autodiff(Reverse, f, Active, Active(1.0))
```

## Environment
```julia
Enzyme v0.13.24
Trixi v0.9.13
```

I noticed a similar discussion in #1820 about LAPACK functions. Would defining custom Enzyme rules be a solution here, or are there other recommended approaches for handling LAPACK functions in automatic differentiation?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.