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

Error with non-integer sizeof when differentiating Base.diff #2168

Closed
gdalle opened this issue Dec 5, 2024 · 5 comments
Closed

Error with non-integer sizeof when differentiating Base.diff #2168

gdalle opened this issue Dec 5, 2024 · 5 comments

Comments

@gdalle
Copy link
Contributor

gdalle commented Dec 5, 2024

Here's an MWE with Enzyme v0.13.19:

julia> import Enzyme

julia> sumdiff(x) = sum(diff(x));

julia> x, dx = float.(1:5), float.(6:10);

julia> Enzyme.hvp(sum, x, dx)  # works
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> Enzyme.hvp(sumdiff, x, dx)  # fails
ERROR: InexactError: Int64(0.125)
Stacktrace:
  [1] Int64
    @ ./float.jl:994 [inlined]
  [2] sizeof
    @ ~/.julia/packages/LLVM/wMjUU/src/datalayout.jl:91 [inlined]
  [3] should_recurse(typ2::Any, arg_t::LLVM.LLVMType, byref::GPUCompiler.ArgumentCC, dl::LLVM.DataLayout)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/ottqJ/src/absint.jl:208
  [4] abs_typeof(arg::LLVM.Value, partial::Bool, seenphis::Set{LLVM.PHIInst})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/ottqJ/src/absint.jl:601
  [5] abs_typeof(arg::LLVM.Value, partial::Bool, seenphis::Set{LLVM.PHIInst})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/ottqJ/src/absint.jl:710
  [6] abs_typeof
    @ ~/.julia/packages/Enzyme/ottqJ/src/absint.jl:283 [inlined]
  [7] 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 ~/.julia/packages/Enzyme/ottqJ/src/compiler.jl:4255
  [8] codegen
    @ ~/.julia/packages/Enzyme/ottqJ/src/compiler.jl:3218 [inlined]
  [9] _thunk(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, postopt::Bool)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/ottqJ/src/compiler.jl:5265
 [10] cached_compilation(job::GPUCompiler.CompilerJob)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/ottqJ/src/compiler.jl:5306
 [11] 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)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/ottqJ/src/compiler.jl:5410
 [12] thunk_generator(world::UInt64, source::LineNumberNode, 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, 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)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/ottqJ/src/compiler.jl:5562
 [13] autodiff
    @ ~/.julia/packages/Enzyme/ottqJ/src/Enzyme.jl:640 [inlined]
 [14] autodiff
    @ ~/.julia/packages/Enzyme/ottqJ/src/Enzyme.jl:544 [inlined]
 [15] autodiff
    @ ~/.julia/packages/Enzyme/ottqJ/src/Enzyme.jl:516 [inlined]
 [16] hvp!
    @ ~/.julia/packages/Enzyme/ottqJ/src/sugar.jl:1105 [inlined]
 [17] hvp(f::typeof(sumdiff), x::Vector{Float64}, v::Vector{Float64})
    @ Enzyme ~/.julia/packages/Enzyme/ottqJ/src/sugar.jl:1072
 [18] top-level scope
    @ ~/Work/GitHub/Julia/DifferentiationInterface.jl/DifferentiationInterface/test/playground.jl:7
Some type information was truncated. Use `show(err)` to see complete types.

Note that the error doesn't happen when I reimplement diff manually:

julia> function mydiff(x)
           y = similar(x, length(x)-1)
           for i in eachindex(y)
               y[i] = x[i+1]-x[i]
           end
           return y
       end
mydiff (generic function with 1 method)

julia> summydiff(x) = sum(mydiff(x));

julia> Enzyme.hvp(summydiff, x, dx)  # works
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0
@wsmoses
Copy link
Member

wsmoses commented Dec 5, 2024

your test case works for me on main, does it err on the latest Enzyme for you still?

julia> sumdiff(x) = sum(diff(x));

julia> x, dx = float.(1:5), float.(6:10);

julia> Enzyme.hvp(sum, x, dx)  # works
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> Enzyme.hvp(sumdiff, x, dx)  # fails
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> 

@gdalle
Copy link
Contributor Author

gdalle commented Dec 5, 2024

If I'm not mistaken, v0.3.19 is the latest version of Enzyme? It corresponds to commit hash 6606cd9 which is also the last commit on main?

@wsmoses
Copy link
Member

wsmoses commented Dec 5, 2024

oh yeah, and hm odd. Regardless this appears to be a bug in LLVM.jl here: https://github.com/maleadt/LLVM.jl/blob/a1c3b217c1c9e5db4223e4e0af1754aae53d113f/src/datalayout.jl#L91

Maybe file an issue there?

@vchuravy
Copy link
Member

vchuravy commented Dec 6, 2024

@wsmoses not really a bug in LLVM.jl

You need to decide what you want to do for i1 in should_recurse(typ2::Any, arg_t::LLVM.LLVMType, byref::GPUCompiler.ArgumentCC, dl::LLVM.DataLayout)

@wsmoses
Copy link
Member

wsmoses commented Dec 7, 2024

workaround presumably added here #2181

@wsmoses wsmoses closed this as completed Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants