Skip to content
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 Mutability Error with reverse mode #2255

Open
mhauru opened this issue Jan 7, 2025 · 6 comments
Open

Enzyme Mutability Error with reverse mode #2255

mhauru opened this issue Jan 7, 2025 · 6 comments

Comments

@mhauru
Copy link
Contributor

mhauru commented Jan 7, 2025

MWE

module MWE

using Enzyme: Enzyme

function _logpdf(x)
    s = sum(i for (i, xi) in enumerate(x))
    return s
end

function f(x)
    v = eachrow(x)
    return sum(_logpdf, v)
end

x = reshape([0.8, 0.3, 0.2, 0.7], (2, 2))
@show f(x)
Enzyme.gradient(Enzyme.Reverse, f, x)

end

Output:

f(x) = 6
ERROR: Enzyme Mutability Error: Cannot add one in place to immutable value 6
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] add_one_in_place(x::Int64)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/R6sE8/src/compiler.jl:4870
  [3] augmented_julia_f_4784wrap
    @ ./REPL[14]:0
  [4] macro expansion
    @ ~/.julia/packages/Enzyme/R6sE8/src/compiler.jl:5340 [inlined]
  [5] enzyme_call
    @ ~/.julia/packages/Enzyme/R6sE8/src/compiler.jl:4878 [inlined]
  [6] AugmentedForwardThunk
    @ ~/.julia/packages/Enzyme/R6sE8/src/compiler.jl:4814 [inlined]
  [7] autodiff
    @ ~/.julia/packages/Enzyme/R6sE8/src/Enzyme.jl:396 [inlined]
  [8] autodiff
    @ ~/.julia/packages/Enzyme/R6sE8/src/Enzyme.jl:524 [inlined]
  [9] macro expansion
    @ ~/.julia/packages/Enzyme/R6sE8/src/sugar.jl:324 [inlined]
 [10] gradient(::EnzymeCore.ReverseMode{false, false, EnzymeCore.FFIABI, false, false}, ::typeof(Main.MWE.f), ::Matrix{Float64})
    @ Enzyme ~/.julia/packages/Enzyme/R6sE8/src/sugar.jl:263
 [11] top-level scope
    @ REPL[14]:17

On Enzyme v0.13.28. This is a descendant of #1812.

@vchuravy
Copy link
Member

vchuravy commented Jan 9, 2025

Could you try #1852 ?

@mhauru
Copy link
Contributor Author

mhauru commented Jan 9, 2025

On the latest of that branch (16d5b65), getting

ERROR: LoadError: UndefVarError: `isvectortype` not defined
Stacktrace:
 [1] include(mod::Module, _path::String)
   @ Base ./Base.jl:495
 [2] include(x::String)
   @ Enzyme.Compiler ~/projects/Enzyme.jl/src/compiler.jl:1
 [3] top-level scope
   @ ~/projects/Enzyme.jl/src/compiler.jl:318
 [4] include(mod::Module, _path::String)
   @ Base ./Base.jl:495
 [5] include(x::String)
   @ Enzyme ~/projects/Enzyme.jl/src/Enzyme.jl:1
 [6] top-level scope
   @ ~/projects/Enzyme.jl/src/Enzyme.jl:127
 [7] include
   @ ./Base.jl:495 [inlined]
 [8] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt128}}, source::Nothing)
   @ Base ./loading.jl:2222
 [9] top-level scope
   @ stdin:3
in expression starting at /Users/mhauru/projects/Enzyme.jl/src/typeutils/recursive_maps.jl:1
in expression starting at /Users/mhauru/projects/Enzyme.jl/src/compiler.jl:1
in expression starting at /Users/mhauru/projects/Enzyme.jl/src/Enzyme.jl:1
in expression starting at stdin:3
  ✗ Enzyme
  0 dependencies successfully precompiled in 3 seconds. 18 already precompiled.

ERROR: The following 1 direct dependency failed to precompile:

Enzyme [7da242da-08ed-463a-9acd-ee780be4f1d9]

Failed to precompile Enzyme [7da242da-08ed-463a-9acd-ee780be4f1d9] to "/Users/mhauru/.julia/compiled/v1.10/Enzyme/jl_Dskjik".
ERROR: LoadError: UndefVarError: `isvectortype` not defined
Stacktrace:
 [1] include(mod::Module, _path::String)
   @ Base ./Base.jl:495
 [2] include(x::String)
   @ Enzyme.Compiler ~/projects/Enzyme.jl/src/compiler.jl:1
 [3] top-level scope
   @ ~/projects/Enzyme.jl/src/compiler.jl:318
 [4] include(mod::Module, _path::String)
   @ Base ./Base.jl:495
 [5] include(x::String)
   @ Enzyme ~/projects/Enzyme.jl/src/Enzyme.jl:1
 [6] top-level scope
   @ ~/projects/Enzyme.jl/src/Enzyme.jl:127
 [7] include
   @ ./Base.jl:495 [inlined]
 [8] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt128}}, source::Nothing)
   @ Base ./loading.jl:2222
 [9] top-level scope
   @ stdin:3
in expression starting at /Users/mhauru/projects/Enzyme.jl/src/typeutils/recursive_maps.jl:1
in expression starting at /Users/mhauru/projects/Enzyme.jl/src/compiler.jl:1
in expression starting at /Users/mhauru/projects/Enzyme.jl/src/Enzyme.jl:1
in expression starting at stdin:
Stacktrace:
  [1] pkgerror(msg::String)
    @ Pkg.Types ~/.julia/juliaup/julia-1.10.6+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/Types.jl:70
  [2] precompile(ctx::Pkg.Types.Context, pkgs::Vector{…}; internal_call::Bool, strict::Bool, warn_loaded::Bool, already_instantiated::Bool, timing::Bool, _from_loading::Bool, kwargs::@Kwargs{})
    @ Pkg.API ~/.julia/juliaup/julia-1.10.6+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/API.jl:1700
  [3] precompile(pkgs::Vector{Pkg.Types.PackageSpec}; io::Base.TTY, kwargs::@Kwargs{_from_loading::Bool})
    @ Pkg.API ~/.julia/juliaup/julia-1.10.6+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/API.jl:159
  [4] precompile
    @ ~/.julia/juliaup/julia-1.10.6+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/API.jl:147 [inlined]
  [5] #precompile#114
    @ ~/.julia/juliaup/julia-1.10.6+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/API.jl:146 [inlined]
  [6] #invokelatest#2
    @ ./essentials.jl:894 [inlined]
  [7] invokelatest
    @ ./essentials.jl:889 [inlined]
  [8] _require(pkg::Base.PkgId, env::String)
    @ Base ./loading.jl:1963
  [9] __require_prelocked(uuidkey::Base.PkgId, env::String)
    @ Base ./loading.jl:1812
 [10] #invoke_in_world#3
    @ ./essentials.jl:926 [inlined]
 [11] invoke_in_world
    @ ./essentials.jl:923 [inlined]
 [12] _require_prelocked(uuidkey::Base.PkgId, env::String)
    @ Base ./loading.jl:1803
 [13] macro expansion
    @ ./loading.jl:1790 [inlined]
 [14] macro expansion
    @ ./lock.jl:267 [inlined]
 [15] __require(into::Module, mod::Symbol)
    @ Base ./loading.jl:1753
 [16] #invoke_in_world#3
    @ ./essentials.jl:926 [inlined]
 [17] invoke_in_world
    @ ./essentials.jl:923 [inlined]
 [18] require(into::Module, mod::Symbol)
    @ Base ./loading.jl:1746
Some type information was truncated. Use `show(err)` to see complete types.

@danielwe
Copy link
Contributor

danielwe commented Jan 9, 2025

You need to also pick up the modified EnzymeCore from the same branch:

]dev /Users/mhauru/projects/Enzyme.jl/lib/EnzymeCore

@mhauru
Copy link
Contributor Author

mhauru commented Jan 10, 2025

With that, getting the original Enzyme Mutability Error from OP.

@danielwe
Copy link
Contributor

Oh yeah, #1852 won't make a difference here as the error happens in add_one_in_place but in a separate branch from where recursive_add is called. #1852 only replaces the innards of recursive_add. Clearly, add_one_in_place is being called on an object that's not a RefValue or a zero-dimensional Array.

@wsmoses
Copy link
Member

wsmoses commented Jan 10, 2025

The error here is that you're returning an int, which is not a differentiable float type expected by gradient.

We definitely should fix the error message, but that's the core issue here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants