Skip to content

Error in autodiff involving mul! of SparseMatrixCSC #2099

@albertomercurio

Description

@albertomercurio

The following example doesn't work

using LinearAlgebra
using SparseArrays
using Enzyme

# Assume `dudt!` is defined as:
function dudt!(du, u, p, t)
  mul!(du, A, u, p[1], p[2])  # In-place multiplication
  return sum(du)             # Return scalar sum
end

# Example inputs
# const A = rand(5, 5)
const A = sprand(5, 5, 0.4)
du = zeros(5)
u = rand(5)
p = [1.0, 0.5]  # Active parameters
dp = similar(p)
t = 0.0         # Constant parameter

# Define Enzyme gradient computation
grad = Enzyme.autodiff(
  Reverse,
  dudt!,                # Function to differentiate
  Const(du),            # Do not differentiate with respect to `du`
  Const(u),             # `u` is treated as a constant
  Duplicated(p, dp),    # Differentiate with respect to `p`
  Const(t)              # `t` is treated as a constant
)

and returns the error

ERROR: UndefVarError: `` not defined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.
Stacktrace:
  [1] reverse
    @ ~/.julia/packages/Enzyme/azJki/src/internal_rules.jl:849 [inlined]
  [2] dudt!
    @ ~/GitHub/Research/Undef/Autodiff QuantumToolbox/autodiff.jl:93 [inlined]
  [3] diffejulia_dudt__19520wrap
    @ ~/GitHub/Research/Undef/Autodiff QuantumToolbox/autodiff.jl:0
  [4] macro expansion
    @ ~/.julia/packages/Enzyme/azJki/src/compiler.jl:8369 [inlined]
  [5] enzyme_call
    @ ~/.julia/packages/Enzyme/azJki/src/compiler.jl:7932 [inlined]
  [6] CombinedAdjointThunk
    @ ~/.julia/packages/Enzyme/azJki/src/compiler.jl:7705 [inlined]
  [7] autodiff
    @ ~/.julia/packages/Enzyme/azJki/src/Enzyme.jl:491 [inlined]
  [8] autodiff
    @ ~/.julia/packages/Enzyme/azJki/src/Enzyme.jl:537 [inlined]
  [9] autodiff(::ReverseMode{…}, ::typeof(dudt!), ::Const{…}, ::Const{…}, ::Duplicated{…}, ::Const{…})
    @ Enzyme ~/.julia/packages/Enzyme/azJki/src/Enzyme.jl:504
 [10] top-level scope
    @ ~/GitHub/Research/Undef/Autodiff QuantumToolbox/autodiff.jl:108
Some type information was truncated. Use `show(err)` to see complete types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions