Skip to content

add Unthunk #64

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

Merged
merged 2 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "0.4.0"
version = "0.5.0-DEV"

[compat]
julia = "^1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/ChainRulesCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Base.Broadcast: materialize, materialize!, broadcasted, Broadcasted, broad
export frule, rrule
export wirtinger_conjugate, wirtinger_primal, refine_differential
export @scalar_rule, @thunk
export extern, store!
export extern, store!, unthunk
export Wirtinger, Zero, One, DoesNotExist, Thunk, InplaceableThunk
export NO_FIELDS

Expand Down
12 changes: 6 additions & 6 deletions src/differential_arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ for T in (:AbstractThunk, :Any)
end


Base.:+(a::AbstractThunk, b::AbstractThunk) = extern(a) + extern(b)
Base.:*(a::AbstractThunk, b::AbstractThunk) = extern(a) * extern(b)
Base.:+(a::AbstractThunk, b::AbstractThunk) = unthunk(a) + unthunk(b)
Base.:*(a::AbstractThunk, b::AbstractThunk) = unthunk(a) * unthunk(b)
for T in (:Any,)
@eval Base.:+(a::AbstractThunk, b::$T) = extern(a) + b
@eval Base.:+(a::$T, b::AbstractThunk) = a + extern(b)
@eval Base.:+(a::AbstractThunk, b::$T) = unthunk(a) + b
@eval Base.:+(a::$T, b::AbstractThunk) = a + unthunk(b)

@eval Base.:*(a::AbstractThunk, b::$T) = extern(a) * b
@eval Base.:*(a::$T, b::AbstractThunk) = a * extern(b)
@eval Base.:*(a::AbstractThunk, b::$T) = unthunk(a) * b
@eval Base.:*(a::$T, b::AbstractThunk) = a * unthunk(b)
end
20 changes: 16 additions & 4 deletions src/differentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ end
return element, (externed, new_state)
end

"""
unthunk(x)

On `AbstractThunk`s this removes 1 layer of thunking.
On any other type, it is the identity operation.

In contrast to `extern` this is nonrecursive.
"""
@inline unthunk(x) = x

@inline extern(x::AbstractThunk) = extern(unthunk(x))

#####
##### `Thunk`
#####
Expand Down Expand Up @@ -228,9 +240,9 @@ end
# have to define this here after `@thunk` and `Thunk` is defined
Base.conj(x::AbstractThunk) = @thunk(conj(extern(x)))


(x::Thunk)() = x.f()
@inline extern(x::Thunk) = extern(x())
@inline unthunk(x::Thunk) = x()


Base.show(io::IO, x::Thunk) = println(io, "Thunk($(repr(x.f)))")

Expand All @@ -252,8 +264,8 @@ struct InplaceableThunk{T<:Thunk, F} <: AbstractThunk
add!::F
end

(x::InplaceableThunk)() = x.val()
@inline extern(x::InplaceableThunk) = extern(x.val)
@inline unthunk(x::InplaceableThunk) = unthunk(x.val)
(x::InplaceableThunk)() = unthunk(x)

function Base.show(io::IO, x::InplaceableThunk)
println(io, "InplaceableThunk($(repr(x.val)), $(repr(x.add!)))")
Expand Down
5 changes: 5 additions & 0 deletions test/differentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
@test extern(@thunk(@thunk(3))) == 3
end

@testset "unthunk" begin
@test unthunk(@thunk(3)) == 3
@test unthunk(@thunk(@thunk(3))) isa Thunk
end

@testset "calling thunks should call inner function" begin
@test (@thunk(3))() == 3
@test (@thunk(@thunk(3)))() isa Thunk
Expand Down