-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enzyme (Both Forward and Reverse) fails to differentiate the total energy of a simulation using the linear scalar advection equation wrt k #1
Labels
bug
Something isn't working
help wanted
Extra attention is needed
question
Further information is requested
Comments
Additional information, the version information from the previous comment: julia> begin
using InteractiveUtils
versioninfo()
using Pkg
Pkg.status(["Trixi", "Enzyme"],
mode = PKGMODE_MANIFEST)
end
|
Retest issue with Enzyme v0.13.24 and Trixi v0.9.13 MWE:using Trixi, OrdinaryDiffEq
import Enzyme
using Enzyme: autodiff, Forward, Duplicated, Const
using Enzyme: Reverse, Active
function energy_at_final_time(k) # k is the wave number of the initial condition
equations = LinearScalarAdvectionEquation2D(1.0, -0.3)
mesh = TreeMesh((-1.0, -1.0), (1.0, 1.0), initial_refinement_level=3, n_cells_max=10^4)
solver = DGSEM(3, flux_lax_friedrichs)
initial_condition = (x, t, equation) -> begin
x_trans = Trixi.x_trans_periodic_2d(x - equation.advection_velocity * t)
return SVector(sinpi(k * sum(x_trans)))
end
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
ode = semidiscretize(semi, (0.0, 1.0))
sol = solve(ode, BS3(), save_everystep=false)
Trixi.integrate(energy_total, sol.u[end], semi)
end
# autodiff(Forward, energy_at_final_time, Duplicated(1.0, 1.0))
autodiff(Reverse, energy_at_final_time, Active, Active(1.0)) Forward mode Error Message
Reverse mode Error Message
Environment
|
From the error stacktrace, the call chain:
|
# Calculate Legendre vandermonde matrix and its inverse
function vandermonde_legendre(nodes, N::Integer, RealT = Float64)
n_nodes = length(nodes)
n_modes = N + 1
vandermonde = zeros(RealT, 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
# for very high polynomial degree, this is not well conditioned
inverse_vandermonde = inv(vandermonde)
return vandermonde, inverse_vandermonde
end
vandermonde_legendre(nodes, RealT = Float64) = vandermonde_legendre(nodes,
length(nodes) - 1,
RealT)
end # @muladd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
Something isn't working
help wanted
Extra attention is needed
question
Further information is requested
Difficulty with differentiating total energy w.r.t. wave number using Enzyme.jl and Trixi.jl:
Description:
Successfully applying Enzyme in simple cases like differentiating the sine function:
However, when extending this approach to a more complex scenario using Trixi.jl to differentiate the total energy with respect to the wave number, I encountered an issue. Here’s the minimal working example (MWE):
MWE:
ForwardDiff Considerations:
ForwardDiff.jl
uses dual numbers, which store both the result and its derivative with respect to a specified parameter. This means we need to ensure that we can handleForwardDiff.Dual
numbers throughout the computation. In this case, we can address this by specifying the element type foruEltype
in theSemidiscretizationHyperbolic
call:semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver, uEltype=typeof(k))
Enzyme Considerations:
Enzyme works by operating on LLVM's intermediate representation (IR), so additional modifications might be needed. For instance, we might need to expose certain parts of the cache for differentiation to succeed.
Additionally, I received the following warning from Enzyme.jl:
Warning: Using fallback BLAS replacements for (["dgemm_64_"]), performance may be degraded
This suggests that the BLAS linear algebra library could also be causing some issues, although according to Enzyme’s documentation, this is unlikely to be the main problem.
Error:
I encountered the following error when trying to differentiate
energy_at_final_time
using Enzyme's reverse-mode autodiff.Request:
The text was updated successfully, but these errors were encountered: