EnzymeAD / Enzyme.jl

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

Adding KA.synchronize rule #1464

Closed michel2323 closed 4 months ago

michel2323 commented 4 months ago

I'm using the customconst branch https://github.com/EnzymeAD/Enzyme.jl/pull/1371 to add a synchronize rule. I did a rebase of the branch on main. I know this worked some time before the ABI fix and whatnot like a month ago pre-0.12 release. Now it segaults in EnzymeRegisterDiffUseCallHandler(name, handle) which was added through this PR.

[3127366] signal (11.2): Segmentation fault
in expression starting at /disk/mschanen/git/ka_gpu/mwe.jl:61
isSmall at /opt/x86_64-linux-gnu/x86_64-linux-gnu/sys-root/usr/local/include/llvm/ADT/SmallPtrSet.h:195 [inlined]
EndPointer at /opt/x86_64-linux-gnu/x86_64-linux-gnu/sys-root/usr/local/include/llvm/ADT/SmallPtrSet.h:120 [inlined]
end at /opt/x86_64-linux-gnu/x86_64-linux-gnu/sys-root/usr/local/include/llvm/ADT/SmallPtrSet.h:408 [inlined]
getReturnDiffeType at /workspace/srcdir/Enzyme/enzyme/Enzyme/GradientUtils.cpp:4387
EnzymeGradientUtilsGetReturnDiffeType at /workspace/srcdir/Enzyme/enzyme/Enzyme/CApi.cpp:423
EnzymeGradientUtilsGetReturnDiffeType at /disk/mschanen/julia_depot/dev/Enzyme/src/api.jl:269
enzyme_custom_setup_ret at /disk/mschanen/julia_depot/dev/Enzyme/src/rules/customrules.jl:225
unknown function (ip: 0x7f67101db3f5)
_jl_invoke at /cache/build/builder-amdci4-2/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined]
ijl_apply_generic at /cache/build/builder-amdci4-2/julialang/julia-release-1-dot-10/src/gf.c:3077
aug_fwd_mi at /disk/mschanen/julia_depot/dev/Enzyme/src/rules/customrules.jl:507 [inlined]
has_aug_fwd_rule at /disk/mschanen/julia_depot/dev/Enzyme/src/rules/customrules.jl:556 [inlined]
enzyme_custom_diffuse at /disk/mschanen/julia_depot/dev/Enzyme/src/rules/customrules.jl:971
unknown function (ip: 0x7f67101d7c0f)
_jl_invoke at /cache/build/builder-amdci4-2/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined]
ijl_apply_generic at /cache/build/builder-amdci4-2/julialang/julia-release-1-dot-10/src/gf.c:3077
#174 at /disk/mschanen/julia_depot/dev/Enzyme/src/rules/llvmrules.jl:1178
unknown function (ip: 0x7f67101d642e)
_jl_invoke at /cache/build/builder-amdci4-2/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined]
ijl_apply_generic at /cache/build/builder-amdci4-2/julialang/julia-release-1-dot-10/src/gf.c:3077
jlcapi_YY.174_5944 at /disk/mschanen/julia_depot/compiled/v1.10/Enzyme/G1p5n_stvVA.so (unknown line)
operator() at /workspace/srcdir/Enzyme/enzyme/Enzyme/CApi.cpp:381 [inlined]
__invoke_impl<bool, EnzymeRegisterDiffUseCallHandler(char*, CustomFunctionDiffUse)::<lambda(const llvm::CallInst*, const GradientUtils*, const llvm::Value*, bool, DerivativeMode, bool&)>&, const llvm::CallInst*, const GradientUtils*, const llvm::Value*, bool, DerivativeMode, bool&> at /opt/x86_64-linux-gnu/x86_64-linux-gnu/include/c++/10.2.0/bits/invoke.h:60 [inlined]
__invoke_r<bool, EnzymeRegisterDiffUseCallHandler(char*, CustomFunctionDiffUse)::<lambda(const llvm::CallInst*, const GradientUtils*, const llvm::Value*, bool, DerivativeMode, bool&)>&, const llvm::CallInst*, const GradientUtils*, const llvm::Value*, bool, DerivativeMode, bool&> at /opt/x86_64-linux-gnu/x86_64-linux-gnu/include/c++/10.2.0/bits/invoke.h:113 [inlined]
_M_invoke at /opt/x86_64-linux-gnu/x86_64-linux-gnu/include/c++/10.2.0/bits/std_function.h:291
operator() at /opt/x86_64-linux-gnu/x86_64-linux-gnu/include/c++/10.2.0/bits/std_function.h:622 [inlined]
is_use_directly_needed_in_reverse at /workspace/srcdir/Enzyme/enzyme/Enzyme/DifferentialUseAnalysis.cpp:464
is_value_needed_in_reverse<(QueryType)0> at /workspace/srcdir/Enzyme/enzyme/Enzyme/DifferentialUseAnalysis.h:158
is_value_needed_in_reverse<(QueryType)0> at /workspace/srcdir/Enzyme/enzyme/Enzyme/DifferentialUseAnalysis.h:243
is_value_needed_in_reverse<(QueryType)0> at /workspace/srcdir/Enzyme/enzyme/Enzyme/DifferentialUseAnalysis.h:243
operator() at /workspace/srcdir/Enzyme/enzyme/Enzyme/EnzymeLogic.cpp:856 [inlined]
callback_fn<calculateUnusedValuesInFunction(llvm::Function&, llvm::SmallPtrSetImpl<const llvm::Value*>&, llvm::SmallPtrSetImpl<const llvm::Instruction*>&, bool, DerivativeMode, GradientUtils*, llvm::TargetLibraryInfo&, llvm::ArrayRef<DIFFE_TYPE>, const llvm::SmallPtrSetImpl<llvm::BasicBlock*>&)::<lambda(const llvm::Value*)> > at /opt/x86_64-linux-gnu/x86_64-linux-gnu/sys-root/usr/local/include/llvm/ADT/STLFunctionalExtras.h:45 [inlined]
operator() at /opt/x86_64-linux-gnu/x86_64-linux-gnu/sys-root/usr/local/include/llvm/ADT/STLFunctionalExtras.h:68 [inlined]
calculateUnusedValues at /workspace/srcdir/Enzyme/enzyme/Enzyme/FunctionUtils.h:278
calculateUnusedValuesInFunction at /workspace/srcdir/Enzyme/enzyme/Enzyme/EnzymeLogic.cpp:852
CreatePrimalAndGradient at /workspace/srcdir/Enzyme/enzyme/Enzyme/EnzymeLogic.cpp:4223
EnzymeCreatePrimalAndGradient at /workspace/srcdir/Enzyme/enzyme/Enzyme/CApi.cpp:622
EnzymeCreatePrimalAndGradient at /disk/mschanen/julia_depot/dev/Enzyme/src/api.jl:154
michel2323 commented 4 months ago

MWE without KA and only CUDA because CUDABackend is required to trigger the segfault:

using Enzyme
using CUDA
using EnzymeCore
using EnzymeCore.EnzymeRules

# Commented lines would work
# struct Backend end
# my_synchronize(::Backend) = synchronize()
my_synchronize(::CUDABackend) = synchronize()
function EnzymeRules.augmented_primal(
    config::Config,
    func::Const{typeof(my_synchronize)},
    ::Type{Const{Nothing}},
    backend::T
) where T <: EnzymeCore.Annotation
    my_synchronize(backend.val)
    println("Forward synchronize")
    return AugmentedReturn(
        nothing, nothing, nothing
    )
end

function EnzymeRules.reverse(config::Config, func::Const{typeof(my_synchronize)}, ::Type{Const{Nothing}}, tape, backend)
    return (nothing,)
end

function square_caller(backend)
    my_synchronize(backend)
    return nothing
end

function enzyme_testsuite(backend)
    Enzyme.autodiff(
        Reverse, square_caller, Const(backend())
    )
end

# enzyme_testsuite(Backend)
enzyme_testsuite(CUDABackend)
wsmoses commented 4 months ago

@michel2323 this runs for me

wsmoses commented 4 months ago

@michel2323 please reopen if it persists, closing