EnzymeAD / Enzyme.jl

Julia bindings for the Enzyme automatic differentiator
https://enzyme.mit.edu
MIT License
440 stars 62 forks source link

"Constant memory is stored..." in `MLUtils.unsqueeze` #1866

Open BioTurboNick opened 2 hours ago

BioTurboNick commented 2 hours ago

Not sure what to make of this? Why would this basic function in MLUtils be a problem for Enzyme?

So opening an issue as suggested. I can't tell if 0.13 fixes it because Flux et al doesn't yet support it.

ERROR: Constant memory is stored (or returned) to a differentiable variable.
As a result, Enzyme cannot provably ensure correctness and throws this error.
This might be due to the use of a constant variable as temporary storage for active memory (https://enzyme.mit.edu/julia/stable/faq/#Activity-of-temporary-storage).
If Enzyme should be able to prove this use non-differentable, open an issue!
To work around this issue, either:
 a) rewrite this variable to not be conditionally active (fastest, but requires a code change), or
 b) set Enzyme.API.runtimeActivity!(true) immediately after loading Enzyme (which maintains correctness, but may slightly reduce performance).
Mismatched activity for:   ret {} addrspace(10)* %20, !dbg !481 const val:   %20 = call nonnull "enzyme_type"="{[-1]:Pointer}" {} addrspace(10)* @ijl_reshape_array({} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 280629280561936 to {}*) to {} addrspace(10)*), {} addrspace(10)* noundef nonnull %0, {} addrspace(10)* noundef nonnull %box) #381, !dbg !494
Type tree: {}
 llvalue=  %20 = call nonnull "enzyme_type"="{[-1]:Pointer}" {} addrspace(10)* @ijl_reshape_array({} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 280629280561936 to {}*) to {} addrspace(10)*), {} addrspace(10)* noundef nonnull %0, {} addrspace(10)* noundef nonnull %box) #381, !dbg !494

Stacktrace:
 [1] #unsqueeze#95
   @ ~/.julia/packages/MLUtils/LmmaQ/src/utils.jl:40

Stacktrace:
  [1] #unsqueeze#95
    @ ~/.julia/packages/MLUtils/LmmaQ/src/utils.jl:40
  [2] unsqueeze
    @ ~/.julia/packages/MLUtils/LmmaQ/src/utils.jl:37 [inlined]
  [3] unsqueeze
    @ ./deprecated.jl:105
  [4] ...omitted...
  [5] ...omitted...
  [6] _applyloss
    @ ~/.julia/packages/Flux/HBF2N/ext/FluxEnzymeExt/FluxEnzymeExt.jl:15 [inlined]
  [7] _applyloss
    @ ~/.julia/packages/Flux/HBF2N/ext/FluxEnzymeExt/FluxEnzymeExt.jl:0 [inlined]
  [8] augmented_julia__applyloss_6153_inner_1wrap
    @ ~/.julia/packages/Flux/HBF2N/ext/FluxEnzymeExt/FluxEnzymeExt.jl:0
  [9] macro expansion
    @ ~/.julia/packages/Enzyme/TiboG/src/compiler.jl:7187 [inlined]
 [10] enzyme_call
    @ ~/.julia/packages/Enzyme/TiboG/src/compiler.jl:6794 [inlined]
 [11] AugmentedForwardThunk
    @ ~/.julia/packages/Enzyme/TiboG/src/compiler.jl:6682 [inlined]
 [12] autodiff(::EnzymeCore.ReverseMode{…}, ::EnzymeCore.Const{…}, ::Type{…}, ::EnzymeCore.Const{…}, ::EnzymeCore.Duplicated{…}, ::EnzymeCore.Const{…}, ::EnzymeCore.Const{…})
    @ Enzyme ~/.julia/packages/Enzyme/TiboG/src/Enzyme.jl:264
 [13] autodiff
    @ ~/.julia/packages/Enzyme/TiboG/src/Enzyme.jl:332 [inlined]
 [14] macro expansion
    @ ~/.julia/packages/Flux/HBF2N/ext/FluxEnzymeExt/FluxEnzymeExt.jl:34 [inlined]
 [15] macro expansion
    @ ~/.julia/packages/ProgressLogging/6KXlp/src/ProgressLogging.jl:328 [inlined]
 [16] train!(loss::Function, model::EnzymeCore.Duplicated{…}, data::Tuple{…}, opt::@NamedTuple{…}; cb::Nothing)
    @ FluxEnzymeExt ~/.julia/packages/Flux/HBF2N/ext/FluxEnzymeExt/FluxEnzymeExt.jl:30
 [17] train!(loss::Function, model::EnzymeCore.Duplicated{…}, data::Tuple{…}, opt::@NamedTuple{…})
    @ FluxEnzymeExt ~/.julia/packages/Flux/HBF2N/ext/FluxEnzymeExt/FluxEnzymeExt.jl:27
 [18] ...omitted...
 [19] top-level scope
    @ REPL[5]:1
Some type information was truncated. Use `show(err)` to see complete types.
Julia Version 1.10.5
Commit 6f3fdf7b362 (2024-08-27 14:19 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (aarch64-linux-gnu)
  CPU: 12 × unknown
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, generic)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)
wsmoses commented 2 hours ago

hm can you post the stack trace and reproducer?