-
Notifications
You must be signed in to change notification settings - Fork 82
Improve thunk errors #2780
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
Improve thunk errors #2780
Conversation
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/compiler.jl b/src/compiler.jl
index 496033a7..78c19b7b 100644
--- a/src/compiler.jl
+++ b/src/compiler.jl
@@ -385,11 +385,11 @@ end
::AdjointThunk{PT,FA,RT,TT,Width,TapeType},
) where {PT,FA,RT,TT,Width,TapeType} = TapeType
-@inline fn_type(::Type{<:CombinedAdjointThunk{<:Any,FA}}) where FA = FA
-@inline fn_type(::Type{<:ForwardModeThunk{<:Any,FA}}) where FA = FA
-@inline fn_type(::Type{<:AugmentedForwardThunk{<:Any,FA}}) where FA = FA
-@inline fn_type(::Type{<:AdjointThunk{<:Any,FA}}) where FA = FA
-@inline fn_type(::Type{<:PrimalErrorThunk{<:Any,FA}}) where FA = FA
+@inline fn_type(::Type{<:CombinedAdjointThunk{<:Any, FA}}) where {FA} = FA
+@inline fn_type(::Type{<:ForwardModeThunk{<:Any, FA}}) where {FA} = FA
+@inline fn_type(::Type{<:AugmentedForwardThunk{<:Any, FA}}) where {FA} = FA
+@inline fn_type(::Type{<:AdjointThunk{<:Any, FA}}) where {FA} = FA
+@inline fn_type(::Type{<:PrimalErrorThunk{<:Any, FA}}) where {FA} = FA
using .JIT
@@ -5397,7 +5397,7 @@ end
) where {PT,FA,Width,RT,TT,TapeT} = enzyme_call(
Val(true),
thunk.adjoint,
- AdjointThunk{PT,FA,RT,TT,Width,TapeT},
+ AdjointThunk{PT, FA, RT, TT, Width, TapeT},
Val(Width),
Val(false),
TT,
@@ -5413,7 +5413,7 @@ end
) where {PT,FA,Width,RT,TT,ReturnPrimal,TapeT} = enzyme_call(
Val(false),
thunk.primal,
- AugmentedForwardThunk{PT,FA,RT,TT,Width,ReturnPrimal,TapeT},
+ AugmentedForwardThunk{PT, FA, RT, TT, Width, ReturnPrimal, TapeT},
Val(Width),
Val(ReturnPrimal),
TT,
@@ -5429,7 +5429,7 @@ end
) where {PT,FA,Width,RT,TT,ReturnPrimal,TapeT} = enzyme_call(
Val(true),
thunk.primal,
- AugmentedForwardThunk{PT,FA,RT,TT,Width,ReturnPrimal,TapeT},
+ AugmentedForwardThunk{PT, FA, RT, TT, Width, ReturnPrimal, TapeT},
Val(Width),
Val(ReturnPrimal),
TT,
@@ -5465,11 +5465,11 @@ end
::Val{returnPrimal},
tt::Type{T},
rt::Type{RT},
- fn,
+ fn,
::Type{TapeType},
args::Vararg{Any,N},
-) where {RawCall,PT,T,RT,TapeType,N,CC,width,returnPrimal}
- FA = fn_type(CC)
+ ) where {RawCall, PT, T, RT, TapeType, N, CC, width, returnPrimal}
+ FA = fn_type(CC)
F = eltype(FA)
is_forward =
CC <: AugmentedForwardThunk || CC <: ForwardModeThunk || CC <: PrimalErrorThunk
@@ -5497,48 +5497,50 @@ end
end
if !RawCall && !(CC <: PrimalErrorThunk)
- argtys = copy(argtypes)
+ argtys = copy(argtypes)
- pushfirst!(argtys, FA)
+ pushfirst!(argtys, FA)
- hint = "Arguments to the thunk should be the activities of the function and arguments"
+ hint = "Arguments to the thunk should be the activities of the function and arguments"
- if is_adjoint
- if rettype <: Active ||
- rettype <: MixedDuplicated ||
- rettype <: BatchMixedDuplicated
+ if is_adjoint
+ if rettype <: Active ||
+ rettype <: MixedDuplicated ||
+ rettype <: BatchMixedDuplicated
- push!(argtys,
- if width == 1
- eltype(rettype)
- else
- NTuple{width,eltype(rettype)}
- end)
+ push!(
+ argtys,
if width == 1
- hint *=", then the seed of the active return"
+ eltype(rettype)
else
- hint *=", then an NTuple of width $width for the seeds of the batched active return"
+ NTuple{width, eltype(rettype)}
+ end
+ )
+ if width == 1
+ hint *= ", then the seed of the active return"
+ else
+ hint *= ", then an NTuple of width $width for the seeds of the batched active return"
end
end
- end
+ end
- if needs_tape
- push!(argtys, TapeType)
- hint *=", then the tape from the forward pass"
- end
+ if needs_tape
+ push!(argtys, TapeType)
+ hint *= ", then the tape from the forward pass"
+ end
- truety = Tuple{argtys...}
- if length(argtys) != length(args) + 1
- return quote
- throw(ThunkCallError($CC, $fn, $args, $truety, $hint))
+ truety = Tuple{argtys...}
+ if length(argtys) != length(args) + 1
+ return quote
+ throw(ThunkCallError($CC, $fn, $args, $truety, $hint))
end
- end
+ end
- for (expected, found) in zip(argtys, (fn, args...))
- if !(found <: expected)
+ for (expected, found) in zip(argtys, (fn, args...))
+ if !(found <: expected)
return quote
- throw(ThunkCallError($CC, $fn, $args, $truety, $hint))
+ throw(ThunkCallError($CC, $fn, $args, $truety, $hint))
end
end
end
diff --git a/src/errors.jl b/src/errors.jl
index c6c949bc..77c56e88 100644
--- a/src/errors.jl
+++ b/src/errors.jl
@@ -1647,7 +1647,7 @@ end
struct ThunkCallError <: Exception
thunk::Type
fn::Type
- args::(NTuple{N, Type} where N)
+ args::(NTuple{N, Type} where {N})
correct::Type
hint::String
end
@@ -1656,17 +1656,17 @@ function Base.showerror(io::IO, e::ThunkCallError)
print(io, "ThunkCallError:\n")
print(io, " No method matching:\n ")
Base.show_signature_function(io, e.thunk)
- Base.show_tuple_as_call(io, :var"", Tuple{e.fn, e.args...}, hasfirst=false)
+ Base.show_tuple_as_call(io, :var"", Tuple{e.fn, e.args...}, hasfirst = false)
println(io)
println(io)
print(io, " Expected:\n ")
Base.show_signature_function(io, e.thunk)
- Base.show_tuple_as_call(io, :function, e.correct; hasfirst=false, kwargs=nothing)
+ Base.show_tuple_as_call(io, :function, e.correct; hasfirst = false, kwargs = nothing)
println(io)
println(io)
printstyled(io, "Hint"; bold = true, color = :cyan)
- printstyled(
+ return printstyled(
io,
": " * e.hint * "\n",
color = :cyan,
diff --git a/test/errors.jl b/test/errors.jl
index 3a23b80a..9e70cc26 100644
--- a/test/errors.jl
+++ b/test/errors.jl
@@ -8,15 +8,15 @@ end
function sumsin(x)
- return sin(sum(x))
+ return sin(sum(x))
end
@testset "Incorrect thunk arguments" begin
- fwd, rev = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(sumsin)}, Active, Duplicated{Vector{Float64}})
+ fwd, rev = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(sumsin)}, Active, Duplicated{Vector{Float64}})
- @test_throws Enzyme.Compiler.ThunkCallError fwd(Duplicated([1.0], [2.0]))
-
- @test_throws Enzyme.Compiler.ThunkCallError fwd(Const(sumsin), Duplicated([1.0], [2.0]), Active(3.14))
+ @test_throws Enzyme.Compiler.ThunkCallError fwd(Duplicated([1.0], [2.0]))
+
+ @test_throws Enzyme.Compiler.ThunkCallError fwd(Const(sumsin), Duplicated([1.0], [2.0]), Active(3.14))
end
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2780 +/- ##
==========================================
+ Coverage 68.94% 68.96% +0.01%
==========================================
Files 58 58
Lines 19961 19994 +33
==========================================
+ Hits 13763 13788 +25
- Misses 6198 6206 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Benchmark Results
Benchmark PlotsA plot of the benchmark results has been uploaded as an artifact at https://github.com/EnzymeAD/Enzyme.jl/actions/runs/19353197931/artifacts/4564249973. |
No description provided.