EnzymeAD / Enzyme.jl

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

`ERROR: TypeError: non-boolean (String) used in boolean context` #1985

Closed BioTurboNick closed 1 month ago

BioTurboNick commented 1 month ago

Seems there's a bad assert at the affected line.

ERROR: TypeError: non-boolean (String) used in boolean context
Stacktrace:
  [1] check_ir!(job::GPUCompiler.CompilerJob{…}, errors::Vector{…}, imported::Set{…}, f::LLVM.Function, deletedfns::Vector{…})
    @ Enzyme.Compiler C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler\validation.jl:464
  [2] check_ir!(job::GPUCompiler.CompilerJob{…}, errors::Vector{…}, mod::LLVM.Module)
    @ Enzyme.Compiler C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler\validation.jl:382
  [3] check_ir
    @ C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler\validation.jl:163 [inlined]
  [4] codegen(output::Symbol, job::GPUCompiler.CompilerJob{…}; libraries::Bool, deferred_codegen::Bool, optimize::Bool, toplevel::Bool, strip::Bool, validate::Bool, only_entry::Bool, parent_job::Nothing)
    @ Enzyme.Compiler C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler.jl:6007
  [5] codegen
    @ C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler.jl:5931 [inlined]    
  [6] _thunk(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, postopt::Bool)
    @ Enzyme.Compiler C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler.jl:8206
  [7] _thunk
    @ C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler.jl:8206 [inlined]    
  [8] cached_compilation
    @ C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler.jl:8247 [inlined]    
  [9] thunkbase(ctx::LLVM.Context, mi::Core.MethodInstance, ::Val{…}, ::Type{…}, ::Type{…}, tt::Type{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::Type{…}, ::Val{…}, ::Val{…})
    @ Enzyme.Compiler C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler.jl:8379
 [10] #s2070#19131
    @ C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\compiler.jl:8516 [inlined]    
 [11]
    @ Enzyme.Compiler .\none:0
 [12] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
    @ Core .\boot.jl:707
 [13] autodiff
    @ C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\Enzyme.jl:473 [inlined]       
 [14] autodiff(::ReverseMode{…}, ::typeof(_applyloss), ::Type{…}, ::Const{…}, ::Duplicated{…}, ::Const{…}, ::Const{…})
    @ Enzyme C:\Users\nicho\.julia\packages\Enzyme\vgArw\src\Enzyme.jl:512
 [15] top-level scope
    @ REPL[76]:1

MWE:

using Flux
using Enzyme

x0 = [0.25 0.75
      0.75 0.25
      0.25 0.25] .|> Float32
y0 = [0.75 0.25
      0.25 0.75
      0.75 0.25] .|> Float32

model = Chain(
    Dense(3 => 3),
    Dense(3 => 3, σ)
)

loss = (m, x, y) -> Flux.mse(m(x), y)
_applyloss(loss, model, d...) = loss(model, d...)

Enzyme.autodiff(ReverseWithPrimal, _applyloss, Active, Const(loss), Duplicated(model, make_zero(model)), Const(x0), Const(y0))
prbzrg commented 1 month ago

I'm getting this error too.

prbzrg commented 1 month ago

Error:

┌ Warning: `Base.size(vectyp::VectorType)` is deprecated, use `length(vectyp)` instead.
│   caller = Enzyme.Compiler.CountTrackedPointers(T::LLVM.VectorType) at compiler.jl:5065
└ @ Enzyme.Compiler C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:5065
ERROR: LoadError: TypeError: non-boolean (String) used in boolean context
Stacktrace:
  [1] check_ir!(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, errors::Vector{Tuple{String, Vector{Base.StackTraces.StackFrame}, Any}}, imported::Set{String},
 f::LLVM.Function, deletedfns::Vector{LLVM.Function})
    @ Enzyme.Compiler C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler\validation.jl:464
  [2] check_ir!(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, errors::Vector{Tuple{String, Vector{Base.StackTraces.StackFrame}, Any}}, mod::LLVM.Module)
    @ Enzyme.Compiler C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler\validation.jl:382
  [3] check_ir
    @ C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler\validation.jl:163 [inlined]
  [4] codegen(output::Symbol, job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}; libraries::Bool, deferred_codegen::Bool, optimize::Bool, toplevel::Bool, strip::Bool, validate::Bool, only_entry::Bool, parent_job::Nothing)
    @ Enzyme.Compiler C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:6007
  [5] codegen
    @ C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:5931 [inlined]
  [6] _thunk(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, postopt::Bool)
    @ Enzyme.Compiler C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:8206
  [7] _thunk
    @ C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:8206 [inlined]
  [8] cached_compilation
    @ C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:8247 [inlined]
  [9] thunkbase(ctx::LLVM.Context, mi::Core.MethodInstance, ::Val{0x00000000000068c2}, ::Type{Const{StatefulLuxLayer{Static.True, Dense{typeof(tanh), Int64, Int64, Nothing, Nothing, Static.True}, ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(weight = ViewAxis(1:4, ShapedAxis((2, 2))), bias = 5:6)}}}, @NamedTuple{}}}}, ::Type{Duplicated{Vector{Float32}}}, tt::Type{Tuple{Duplicated{Vector{Float32}}}}, ::Val{Enzyme.API.DEM_ForwardMode}, ::Val{1}, ::Val{(false, false)}, ::Val{false}, ::Val{false}, ::Type{FFIABI}, ::Val{false}, ::Val{true})
    @ Enzyme.Compiler C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:8379
 [10] #s2070#19131
    @ C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\compiler.jl:8516 [inlined]
 [11] var"#s2070#19131"(FA::Any, A::Any, TT::Any, Mode::Any, ModifiedBetween::Any, width::Any, ReturnPrimal::Any, ShadowInit::Any, World::Any, ABI::Any, ErrIfFuncWritten::Any, RuntimeActivity::Any, ::Any, ::Any, ::Any, ::Any, tt::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any)
    @ Enzyme.Compiler .\none:0
 [12] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
    @ Core .\boot.jl:707
 [13] autodiff
    @ C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\Enzyme.jl:633 [inlined]
 [14] autodiff
    @ C:\Users\prbzr\.julia\packages\Enzyme\vgArw\src\Enzyme.jl:537 [inlined]
 [15] pushforward(::StatefulLuxLayer{Static.True, Dense{typeof(tanh), Int64, Int64, Nothing, Nothing, Static.True}, ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(weight = ViewAxis(1:4, ShapedAxis((2, 2))), bias = 5:6)}}}, @NamedTuple{}}, ::DifferentiationInterface.NoPushforwardPrep, ::AutoEnzyme{ForwardMode{false, FFIABI, false, true}, Const}, ::Vector{Float32}, ::Tuple{Vector{Float32}})
    @ DifferentiationInterfaceEnzymeExt C:\Users\prbzr\.julia\packages\DifferentiationInterface\tBGja\ext\DifferentiationInterfaceEnzymeExt\forward_onearg.jl:58
 [16] pushforward(::StatefulLuxLayer{Static.True, Dense{typeof(tanh), Int64, Int64, Nothing, Nothing, Static.True}, ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(weight = ViewAxis(1:4, ShapedAxis((2, 2))), bias = 5:6)}}}, @NamedTuple{}}, ::AutoEnzyme{ForwardMode{false, FFIABI, false, true}, Const}, ::Vector{Float32}, ::Tuple{Vector{Float32}})
    @ DifferentiationInterface C:\Users\prbzr\.julia\packages\DifferentiationInterface\tBGja\src\fallbacks\no_prep.jl:128
 [17] top-level scope
    @ D:\Codes\Mine\Bug Reports\br-1\br-1.jl:9
in expression starting at D:\Codes\Mine\Bug Reports\br-1\br-1.jl:9

Code:

using ADTypes, ComponentArrays, DifferentiationInterface, Enzyme, Lux, Random

nn = Dense(2 => 2, tanh)
r = rand(Float32, 2)
ps, st = Lux.setup(Random.default_rng(), nn)
ps = ComponentArray(ps)
snn = StatefulLuxLayer{true}(nn, ps, st)

pushforward(
    snn,
    AutoEnzyme(; mode = set_runtime_activity(Forward), function_annotation = Const),
    r,
    (r,),
)

Environment:

Status `D:\Codes\Mine\Bug Reports\br-1\Project.toml`
  [47edcb42] ADTypes v1.9.0
  [b0b7db55] ComponentArrays v0.15.17
  [a0c0ee7d] DifferentiationInterface v0.6.16
  [7da242da] Enzyme v0.13.11
  [b2108857] Lux v1.1.0
  [9a3f8284] Random v1.11.0
Status `D:\Codes\Mine\Bug Reports\br-1\Manifest.toml`
  [47edcb42] ADTypes v1.9.0
  [79e6a3ab] Adapt v4.0.4
  [dce04be8] ArgCheck v2.3.0
  [4fba245c] ArrayInterface v7.16.0
  [a9b6321e] Atomix v0.1.0
  [62783981] BitTwiddlingConvenienceFunctions v0.1.6
  [fa961155] CEnum v0.5.0
  [2a0fbf3d] CPUSummary v0.2.6
  [d360d2e6] ChainRulesCore v1.25.0
  [fb6a15b2] CloseOpenIntervals v0.1.13
  [bbf7d656] CommonSubexpressions v0.3.1
  [f70d9fcc] CommonWorldInvalidations v1.0.0
  [34da2185] Compat v4.16.0
  [b0b7db55] ComponentArrays v0.15.17
  [2569d6c7] ConcreteStructs v0.2.3
  [187b0558] ConstructionBase v1.5.8
  [adafc99b] CpuId v0.3.1
  [163ba53b] DiffResults v1.1.0
  [b552c78f] DiffRules v1.15.1
  [a0c0ee7d] DifferentiationInterface v0.6.16
  [8d63f2c5] DispatchDoctor v0.4.16
  [ffbed154] DocStringExtensions v0.9.3
  [7da242da] Enzyme v0.13.11
  [f151be2c] EnzymeCore v0.8.4
  [e2ba6199] ExprTools v0.1.10
  [9aa1b823] FastClosures v0.3.2
  [f6369f11] ForwardDiff v0.10.36
  [d9f16b24] Functors v0.4.12
⌅ [46192b85] GPUArraysCore v0.1.6
  [61eb1bfa] GPUCompiler v1.0.0
  [3e5b6fbb] HostCPUFeatures v0.1.17
  [0e44f5e4] Hwloc v3.3.0
  [615f187c] IfElse v0.1.1
  [92d709cd] IrrationalConstants v0.2.2
  [692b3bcd] JLLWrappers v1.6.1
  [63c18a36] KernelAbstractions v0.9.28
  [929cbde3] LLVM v9.1.2
  [10f19ff3] LayoutPointers v0.1.17
  [2ab3a3ac] LogExpFunctions v0.3.28
  [bdcacae8] LoopVectorization v0.12.171
  [30fc2ffe] LossFunctions v0.11.2
  [b2108857] Lux v1.1.0
  [bb33d45b] LuxCore v1.0.1
  [82251201] LuxLib v1.3.3
  [7e8f7934] MLDataDevices v1.2.1
  [1914dd2f] MacroTools v0.5.13
  [d125e4d3] ManualMemory v0.1.8
  [872c559c] NNlib v0.9.24
  [77ba4419] NaNMath v1.0.2
  [d8793406] ObjectFile v0.4.2
  [6fd5a793] Octavian v0.3.28
  [6fe1bfb0] OffsetArrays v1.14.1
  [3bd65402] Optimisers v0.3.3
  [65ce6f38] PackageExtensionCompat v1.0.2
  [f517fe37] Polyester v0.7.16
  [1d0040c9] PolyesterWeave v0.2.2
  [aea7be01] PrecompileTools v1.2.1
  [21216c6a] Preferences v1.4.3
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [94e857df] SIMDTypes v0.1.0
  [476501e8] SLEEFPirates v0.6.43
  [6c6a2e73] Scratch v1.2.1
  [efcf1570] Setfield v1.1.1
  [276daf66] SpecialFunctions v2.4.0
  [aedffcd0] Static v1.1.1
  [0d7ed370] StaticArrayInterface v1.8.0
  [90137ffa] StaticArrays v1.9.7
  [1e83bf80] StaticArraysCore v1.4.3
  [10745b16] Statistics v1.11.1
  [7792a7ef] StrideArraysCore v0.5.7
  [53d494c1] StructIO v0.3.1
  [8290d209] ThreadingUtilities v0.5.2
  [a759f4b9] TimerOutputs v0.5.25
  [3a884ed6] UnPack v1.0.2
  [013be700] UnsafeAtomics v0.2.1
  [d80eeb9a] UnsafeAtomicsLLVM v0.2.1
  [3d5dd08c] VectorizationBase v0.21.70
  [d49dbf32] WeightInitializers v1.0.4
  [7cc45869] Enzyme_jll v0.0.154+0
  [e33a78d0] Hwloc_jll v2.11.2+0
  [dad2f222] LLVMExtra_jll v0.0.34+0
  [efe28fd5] OpenSpecFun_jll v0.5.5+0
  [0dad84c5] ArgTools v1.1.2
  [56f22d72] Artifacts v1.11.0
  [2a0f44e3] Base64 v1.11.0
  [ade2ca70] Dates 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
  [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
  [2f01184e] SparseArrays v1.11.0
  [fa267f1f] TOML v1.0.3
  [a4e569a6] Tar v1.10.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.1+2
  [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
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
Julia Version 1.11.1
Commit 8f5b7ca12a (2024-10-16 10:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 12 × Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, skylake)
Threads: 12 default, 2 interactive, 7 GC (on 12 virtual cores)
wsmoses commented 1 month ago

What happens on Julia 1.10, enzyme doesn't fully support 1.11 yet (though we're working on now that it's released)

prbzrg commented 1 month ago

I tested it with 1.10.5 and it's working, but there's something else: with set_runtime_activity the output is:

(Float32[0.0, 0.0],)

without it:

(Float32[0.2511607, 0.5426447],)
wsmoses commented 1 month ago

Hm can you show a MWE of that by directly calling enzyme (and not DI)?

wsmoses commented 1 month ago

With another big 1.11 patch [not complete but ongoing] I think the root issue here is fixed, if not reopen.

@prbzrg open another issue for your runtime activity bug?

prbzrg commented 1 month ago

I will test it by the new version with julia v1.11, and report a new issue.