Skip to content

Enzyme + SparseArrays + LinearSolve = Not HappyΒ #835

@AbdAlazezAhmed

Description

@AbdAlazezAhmed

Describe the bug 🐞

When differentiating a solve with A being sparse, the structure of A changes (rowvals changes size and values, colptr changes values)

Expected behavior

The sparsity pattern (or A in general) should not be mutated iiuc

Minimal Reproducible Example πŸ‘‡

using SparseArrays, Enzyme, LinearSolve, LinearAlgebra

function do_sth(_K, some_scalar, ret)
    # nonzeros(_K) .+= some_scalar # We're not even doing anything here
    f = rand(4)
    m  = _K
    prob = LinearProblem(m, f)
    linsolve = init(prob, KrylovJL_CG())
    sol = solve!(linsolve)
    ret[1] = sol|>sum
    return nothing
end


K = sprand(4, 4, 0.3) + SparseArrays.I
K = make_zero(K) .+ K
display(K) # to see if it changes later
@show K.rowval 
@show K.nzval
@show K.m
@show K.n
@show K.colptr
y = [0.0]
# Enzyme.API.printall!(true)
Enzyme.autodiff(Reverse |> set_runtime_activity, do_sth , Duplicated(K, make_zero(K)), Active(1.0), Duplicated(y, [1.0]))
display(K[1:end, 1:end]) # This looks like as if it concatinated the nonzeros and added grabage afterwards
@show K.rowval 
@show K.nzval
@show K.m
@show K.n
@show K.colptr
display(K) # Warum?

Error & Stacktrace ⚠️
It's not about the error tho

4Γ—4 SparseMatrixCSC{Float64, Int64} with 7 stored entries:
 1.0       0.196913   β‹…    β‹… 
  β‹…        1.0        β‹…    β‹… 
  β‹…         β‹…        1.0  0.0132919
 0.715638   β‹…         β‹…   1.75787
K.rowval = [1, 4, 1, 2, 3, 3, 4]
K.nzval = [1.0, 0.715638225985187, 0.19691255689136355, 1.0, 1.0, 0.013291905495585454, 1.7578727660564724]
K.m = 4
K.n = 4
K.colptr = [1, 3, 5, 6, 8]
β”Œ Warning: Verbosity toggle: max_iters 
β”‚  Solver reached maximum number of iterations
β”” @ LinearSolve ~/.julia/dev/LinearSolve/src/iterative_wrappers.jl:328
β”Œ Warning: Verbosity toggle: max_iters 
β”‚  Solver reached maximum number of iterations
β”” @ LinearSolve ~/.julia/dev/LinearSolve/src/iterative_wrappers.jl:328
4Γ—4 SparseMatrixCSC{Float64, Int64} with 16 stored entries:
 1.0       1.0           6.92923e-310  5.0e-324
 0.715638  0.0132919     6.92923e-310  6.92923e-310
 0.196913  1.75787       3.546e-320    5.0e-324
 1.0       6.92923e-310  1.3251e-319   6.92888e-310
K.rowval = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
K.nzval = [1.0, 0.715638225985187, 0.19691255689136355, 1.0, 1.0, 0.013291905495585454, 1.7578727660564724]
K.m = 4
K.n = 4
K.colptr = [1, 5, 9, 13, 17]
ERROR: AssertionError: _goodbuffers(S)
Stacktrace:
  [1] _checkbuffers
    @ ~/.julia/juliaup/julia-1.11.5+0.x64.linux.gnu/share/julia/stdlib/v1.11/SparseArrays/src/sparsematrix.jl:168 [inlined]
  [2] array_summary
    @ ~/.julia/juliaup/julia-1.11.5+0.x64.linux.gnu/share/julia/stdlib/v1.11/SparseArrays/src/sparsematrix.jl:339 [inlined]
  [3] summary
    @ ./show.jl:3152 [inlined]
  [4] show(io::IOContext{Base.TTY}, ::MIME{Symbol("text/plain")}, X::SparseMatrixCSC{Float64, Int64})
    @ Base ./arrayshow.jl:368
  [5] (::REPL.var"#68#69"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.5+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:394
  [6] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.11.5+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:678
  [7] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.5+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:380
  [8] display(d::REPL.REPLDisplay, x::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.5+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:399
  [9] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:340
 [10] top-level scope
    @ ~/Vorlagen/mwe.jl:38

The error is related to displaying broken CSC arrays

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
βŒƒ [a0c0ee7d] DifferentiationInterface v0.7.4
  [7da242da] Enzyme v0.13.108 `https://github.com/EnzymeAD/Enzyme.jl.git#main`
  [c061ca5d] Ferrite v1.2.0 `https://github.com/Ferrite-FEM/Ferrite.jl.git#master`
  [7ed4a6bd] LinearSolve v3.48.0 `~/.julia/dev/LinearSolve`
βŒ… [da2b9cff] Mooncake v0.4.139
  [2f01184e] SparseArrays v1.11.0
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
  [47edcb42] ADTypes v1.20.0
  [7d9f7c33] Accessors v0.1.42
  [79e6a3ab] Adapt v4.4.0
  [ec485272] ArnoldiMethod v0.4.0
  [4fba245c] ArrayInterface v7.22.0
  [4c555306] ArrayLayouts v1.12.2
  [fa961155] CEnum v0.5.0
  [082447d4] ChainRules v1.72.6
  [d360d2e6] ChainRulesCore v1.26.0
  [944b1d66] CodecZlib v0.7.8
  [38540f10] CommonSolve v0.2.4
  [bbf7d656] CommonSubexpressions v0.3.1
  [34da2185] Compat v4.18.1
  [a33af91c] CompositionsBase v0.1.2
  [2569d6c7] ConcreteStructs v0.2.3
  [187b0558] ConstructionBase v1.6.0
  [9a962f9c] DataAPI v1.16.0
  [864edb3b] DataStructures v0.19.3
  [e2d170a0] DataValueInterfaces v1.0.0
  [163ba53b] DiffResults v1.1.0
  [b552c78f] DiffRules v1.15.1
βŒƒ [a0c0ee7d] DifferentiationInterface v0.7.4
  [8d63f2c5] DispatchDoctor v0.4.26
  [b4f34e82] Distances v0.10.12
  [ffbed154] DocStringExtensions v0.9.5
  [4e289a0a] EnumX v1.0.5
  [7da242da] Enzyme v0.13.108 `https://github.com/EnzymeAD/Enzyme.jl.git#main`
  [f151be2c] EnzymeCore v0.8.17
  [e2ba6199] ExprTools v0.1.10
  [55351af7] ExproniconLite v0.10.14
  [c061ca5d] Ferrite v1.2.0 `https://github.com/Ferrite-FEM/Ferrite.jl.git#master`
  [1a297f60] FillArrays v1.15.0
  [f6369f11] ForwardDiff v1.3.0
  [069b7b12] FunctionWrappers v1.1.3
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [46192b85] GPUArraysCore v0.2.0
  [61eb1bfa] GPUCompiler v1.7.5
  [86223c79] Graphs v1.13.1
  [d25df0c9] Inflate v0.1.5
  [3587e190] InverseFunctions v0.1.17
  [92d709cd] IrrationalConstants v0.2.6
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.7.1
  [ae98c720] Jieko v0.2.1
  [ba0b0d4f] Krylov v0.10.3
  [929cbde3] LLVM v9.4.4
  [5078a376] LazyArrays v2.9.3
  [9c8b4983] LightXML v0.9.3
  [7ed4a6bd] LinearSolve v3.48.0 `~/.julia/dev/LinearSolve`
  [2ab3a3ac] LogExpFunctions v0.3.29
  [e6f89c97] LoggingExtras v1.2.0
  [1914dd2f] MacroTools v0.5.16
  [dbe65cb8] MistyClosures v2.1.0
βŒ… [da2b9cff] Mooncake v0.4.139
  [2e0e35c7] Moshi v0.3.7
  [77ba4419] NaNMath v1.1.3
  [b8a86587] NearestNeighbors v0.4.24
  [d8793406] ObjectFile v0.5.0
  [bac558e1] OrderedCollections v1.8.1
  [d236fae5] PreallocationTools v0.4.34
βŒ… [aea7be01] PrecompileTools v1.2.1
  [21216c6a] Preferences v1.5.0
  [c1ae055f] RealDot v0.1.0
  [3cdcf5f2] RecipesBase v1.3.4
  [731186ca] RecursiveArrayTools v3.39.0
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.1
  [7e49a35a] RuntimeGeneratedFunctions v0.5.16
  [fdea26ae] SIMD v3.7.2
  [0bca4576] SciMLBase v2.127.0
  [a6db7da4] SciMLLogging v1.6.0
  [c0aeaf25] SciMLOperators v1.13.0
  [431bcebd] SciMLPublic v1.0.0
  [53ae85a6] SciMLStructures v1.7.0
  [6c6a2e73] Scratch v1.3.0
  [efcf1570] Setfield v1.1.2
  [699a6c99] SimpleTraits v0.9.5
  [dc90abb0] SparseInverseSubset v0.1.2
  [276daf66] SpecialFunctions v2.6.1
  [90137ffa] StaticArrays v1.9.15
  [1e83bf80] StaticArraysCore v1.4.4
  [10745b16] Statistics v1.11.1
  [82ae8749] StatsAPI v1.7.1
  [09ab397b] StructArrays v0.7.2
  [53d494c1] StructIO v0.3.1
  [2efcf032] SymbolicIndexingInterface v0.3.46
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.12.1
  [48a634ad] Tensors v1.16.2
  [e689c965] Tracy v0.1.6
  [3bb67fe8] TranscodingStreams v0.11.3
  [3a884ed6] UnPack v1.0.2
  [4004b06d] VTKBase v1.0.1
  [64499a7a] WriteVTK v1.21.2
βŒ… [7cc45869] Enzyme_jll v0.0.221+0
  [1d5cc7b8] IntelOpenMP_jll v2025.2.0+0
  [dad2f222] LLVMExtra_jll v0.0.38+0
  [ad6e5548] LibTracyClient_jll v0.9.1+6
  [94ce4f54] Libiconv_jll v1.18.0+0
  [856f044c] MKL_jll v2025.2.0+0
  [efe28fd5] OpenSpecFun_jll v0.5.6+0
  [02c8fc9c] XML2_jll v2.15.1+0
  [1317d2d5] oneTBB_jll v2022.0.0+1
  [0dad84c5] ArgTools v1.1.2
  [56f22d72] Artifacts v1.11.0
  [2a0f44e3] Base64 v1.11.0
  [ade2ca70] Dates v1.11.0
  [8ba89e20] Distributed v1.11.0
  [f43a241f] Downloads v1.6.0
  [7b1f6079] FileWatching v1.11.0
  [9fa8497b] Future v1.11.0
  [b77e0a4c] InteractiveUtils v1.11.0
  [4af54fe1] LazyArtifacts v1.11.0
  [b27032c2] LibCURL v0.6.4
  [76f85450] LibGit2 v1.11.0
  [8f399da3] Libdl v1.11.0
  [37e2e46d] LinearAlgebra v1.11.0
  [56ddb016] Logging v1.11.0
  [d6f4376e] Markdown v1.11.0
  [a63ad114] Mmap v1.11.0
  [ca575930] NetworkOptions v1.2.0
  [44cfe95a] Pkg v1.11.0
  [de0858da] Printf v1.11.0
  [9a3f8284] Random v1.11.0
  [ea8e919c] SHA v0.7.0
  [9e88b42a] Serialization v1.11.0
  [1a1011a3] SharedArrays v1.11.0
  [6462fe0b] Sockets v1.11.0
  [2f01184e] SparseArrays v1.11.0
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML v1.0.3
  [a4e569a6] Tar v1.10.0
  [8dfed614] Test v1.11.0
  [cf7118a7] UUIDs v1.11.0
  [4ec0a83e] Unicode v1.11.0
  [e66e0078] CompilerSupportLibraries_jll v1.1.1+0
  [deac9b47] LibCURL_jll v8.6.0+0
  [e37daf67] LibGit2_jll v1.7.2+0
  [29816b5a] LibSSH2_jll v1.11.0+1
  [c8ffd9c3] MbedTLS_jll v2.28.6+0
  [14a3606d] MozillaCACerts_jll v2023.12.12
  [4536629a] OpenBLAS_jll v0.3.27+1
  [05823500] OpenLibm_jll v0.8.5+0
  [bea87d4a] SuiteSparse_jll v7.7.0+0
  [83775a58] Zlib_jll v1.2.13+1
  [8e850b90] libblastrampoline_jll v5.11.0+0
  [8e850ede] nghttp2_jll v1.59.0+0
  [3f19e933] p7zip_jll v17.4.0+2
  • Output of versioninfo()
Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 20 Γ— 13th Gen Intel(R) Core(TM) i5-13500
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, goldmont)
Threads: 20 default, 0 interactive, 10 GC (on 20 virtual cores)
Environment:
  JULIA_LOAD_PATH = /usr/share/gmsh/api/julia:
  JULIA_EDITOR = code
  JULIA_VSCODE_REPL = 1

Additional context

AD not happy

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions