Skip to content

Higher order: handle mixed duplicated on llvm side #2712

@gdalle

Description

@gdalle

This is the new form of the error in #2640, with exactly the same MWE:

import Enzyme

num_to_vec(x::Number) = sin.([1, 2] .* x)
num_to_mat(x::Number) = hcat(num_to_vec(x), num_to_vec(3x))

derivative(f, x) = Enzyme.jacobian(Enzyme.Reverse, f, x)[1]
second_derivative(f, x) = derivative(_x -> derivative(f, _x), x)

derivative(num_to_mat, 1.0)  # works
second_derivative(num_to_mat, 1.0)  # errors

Error:

julia> second_derivative(num_to_mat, 1.0)  # crashes
ERROR: Enzyme compilation failed due to an internal error.
 Please open an issue with the code to reproduce and full error log on github.com/EnzymeAD/Enzyme.jl
 To toggle more information for debugging (needed for bug reports), set Enzyme.Compiler.VERBOSE_ERRORS[] = true (default false)

Stacktrace:
 [1] num_to_mat
   @ ./REPL[6]:1
 [2] num_to_mat
   @ ./REPL[6]:0
 [3] diffejulia_num_to_mat_15171_inner_4wrap
   @ ./REPL[6]:0
 [4] macro expansion
   @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5883
 [5] enzyme_call
   @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5417
 [6] AdjointThunk
   @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5335
 [7] macro expansion
   @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:1167
 [8] jacobian_helper
   @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:797

Stacktrace:
  [1] julia_error(msg::String, val::Ptr{LLVM.API.LLVMOpaqueValue}, errtype::Enzyme.API.ErrorType, data::Ptr{Nothing}, data2::Ptr{LLVM.API.LLVMOpaqueValue}, B::Ptr{LLVM.API.LLVMOpaqueBuilder})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/errors.jl:514
  [2] julia_error(cstr::Cstring, val::Ptr{LLVM.API.LLVMOpaqueValue}, errtype::Enzyme.API.ErrorType, data::Ptr{Nothing}, data2::Ptr{LLVM.API.LLVMOpaqueValue}, B::Ptr{LLVM.API.LLVMOpaqueBuilder})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/errors.jl:348
  [3] EnzymeCreateAugmentedPrimal(logic::Enzyme.Logic, todiff::LLVM.Function, retType::Enzyme.API.CDIFFE_TYPE, constant_args::Vector{…}, TA::Enzyme.TypeAnalysis, returnUsed::Bool, shadowReturnUsed::Bool, typeInfo::Enzyme.FnTypeInfo, uncacheable_args::Vector{…}, forceAnonymousTape::Bool, runtimeActivity::Bool, strongZero::Bool, width::Int64, atomicAdd::Bool)
    @ Enzyme.API ~/.julia/packages/Enzyme/Op3Un/src/api.jl:418
  [4] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:2541 [inlined]
  [5] macro expansion
    @ ~/.julia/packages/LLVM/iza6e/src/base.jl:97 [inlined]
  [6] enzyme!(job::GPUCompiler.CompilerJob{…}, interp::Enzyme.Compiler.Interpreter.EnzymeInterpreter{…}, mod::LLVM.Module, primalf::LLVM.Function, TT::Type, mode::Enzyme.API.CDerivativeMode, width::Int64, parallel::Bool, actualRetType::Type, wrap::Bool, modifiedBetween::NTuple{…} where N, returnPrimal::Bool, expectedTapeType::Type, loweredArgs::Set{…}, boxedArgs::Set{…})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:2509
  [7] compile_unhooked(output::Symbol, job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget{GPUCompiler.NativeCompilerTarget}, Enzyme.Compiler.EnzymeCompilerParams{Enzyme.Compiler.PrimalCompilerParams}})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5102
  [8] compile(target::Symbol, job::GPUCompiler.CompilerJob; kwargs::@Kwargs{})
    @ GPUCompiler ~/.julia/packages/GPUCompiler/Gp8bZ/src/driver.jl:67
  [9] compile
    @ ~/.julia/packages/GPUCompiler/Gp8bZ/src/driver.jl:55 [inlined]
 [10] _thunk(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget{GPUCompiler.NativeCompilerTarget}, Enzyme.Compiler.EnzymeCompilerParams{Enzyme.Compiler.PrimalCompilerParams}}, postopt::Bool)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5956
 [11] _thunk
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5954 [inlined]
 [12] cached_compilation
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:6011 [inlined]
 [13] thunkbase(mi::Core.MethodInstance, World::UInt64, FA::Type{…}, A::Type{…}, TT::Type, Mode::Enzyme.API.CDerivativeMode, width::Int64, ModifiedBetween::NTuple{…} where N, ReturnPrimal::Bool, ShadowInit::Bool, ABI::Type, ErrIfFuncWritten::Bool, RuntimeActivity::Bool, StrongZero::Bool, edges::Vector{…})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:6127
 [14] thunk_generator(world::UInt64, source::Union{…}, FA::Type, A::Type, TT::Type, Mode::Enzyme.API.CDerivativeMode, Width::Int64, ModifiedBetween::NTuple{…} where N, ReturnPrimal::Bool, ShadowInit::Bool, ABI::Type, ErrIfFuncWritten::Bool, RuntimeActivity::Bool, StrongZero::Bool, self::Any, fakeworld::Any, fa::Type, a::Type, tt::Type, mode::Type, width::Type, modifiedbetween::Type, returnprimal::Type, shadowinit::Type, abi::Type, erriffuncwritten::Type, runtimeactivity::Type, strongzero::Type)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:6271
 [15] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/rules/jitrules.jl:464 [inlined]
 [16] runtime_generic_augfwd(::Type{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::typeof(Enzyme.jacobian_helper), ::Nothing, ::EnzymeCore.ReverseMode{…}, ::Nothing, ::Type{…}, ::Nothing, ::Val{…}, ::Nothing, ::Nothing, ::Nothing, ::typeof(num_to_mat), ::Nothing, ::Float64, ::Base.RefValue{…})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/rules/jitrules.jl:574
 [17] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:864 [inlined]
 [18] jacobian_helper
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:797 [inlined]
 [19] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:1251 [inlined]
 [20] jacobian
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:1225
 [21] derivative
    @ ./REPL[7]:1 [inlined]
 [22] augmented_julia_derivative_26567wrap
    @ ./REPL[7]:0
 [23] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5883 [inlined]
 [24] enzyme_call
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5417 [inlined]
 [25] AugmentedForwardThunk
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5367 [inlined]
 [26] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/rules/jitrules.jl:447 [inlined]
 [27] runtime_generic_augfwd(::Type{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::Val{…}, ::typeof(derivative), ::Nothing, ::typeof(num_to_mat), ::Nothing, ::Float64, ::Base.RefValue{…})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/Op3Un/src/rules/jitrules.jl:574
 [28] #3
    @ ./REPL[8]:1 [inlined]
 [29] augmented_julia__3_25673_inner_1wrap
    @ ./REPL[8]:0
 [30] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5883 [inlined]
 [31] enzyme_call
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5417 [inlined]
 [32] AugmentedForwardThunk
    @ ~/.julia/packages/Enzyme/Op3Un/src/compiler.jl:5367 [inlined]
 [33] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:1167 [inlined]
 [34] jacobian_helper(mode::EnzymeCore.ReverseMode{false, false, false, EnzymeCore.FFIABI, false, false}, RT::Type{Any}, n_outs::Val{(2, 2)}, chunk::Nothing, f::var"#3#4"{typeof(num_to_mat)}, xs::Float64)
    @ Enzyme ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:797
 [35] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:864 [inlined]
 [36] jacobian_helper
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:797 [inlined]
 [37] macro expansion
    @ ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:1251 [inlined]
 [38] jacobian(mode::EnzymeCore.ReverseMode{false, false, false, EnzymeCore.FFIABI, false, false}, f::var"#3#4"{typeof(num_to_mat)}, xs::Float64)
    @ Enzyme ~/.julia/packages/Enzyme/Op3Un/src/sugar.jl:1225
 [39] derivative(f::Function, x::Float64)
    @ Main ./REPL[7]:1
 [40] second_derivative(f::Function, x::Float64)
    @ Main ./REPL[8]:1
 [41] top-level scope
    @ REPL[10]:1
Some type information was truncated. Use `show(err)` to see complete types.

Complete log:

error.txt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions