ChainRulesCore.jl icon indicating copy to clipboard operation
ChainRulesCore.jl copied to clipboard

What to do if `@thunk` is applied to a variable (rather than an expression) ?

Open oxinabox opened this issue 6 years ago • 4 comments

maybe make @thunk error if applies to a variable (rather than an expression) ? #50

I can imagine an easy mistake to make would be to write:

∂A = foo(x)
return @thunk(∂A)

When one whould have written:

∂A = @thunk(foo(x))
return ∂A

oxinabox avatar Sep 12 '19 11:09 oxinabox

Or we could just make it not return a Thunk

so by adding something like:

macro thunk(expr::Symbol)
    return expr  # don't thunk single symbols
end

Advantage of this is that we can keep using it as it is currently used in @scalar_rule (I think). Maybe maybe @scalar_rule should just be calling maybe_thunk or something

oxinabox avatar Sep 13 '19 08:09 oxinabox

So if I understand you correctly then:

julia> function foo(x)
           dA = goo(x) + 1
           return @thunk(dA)
       end
foo (generic function with 1 method)

julia> goo(x) = x^2 + x + 1
goo (generic function with 1 method)

julia> unthunk(foo(1))
4

is not a correct way to use @thunk?

gxyd avatar Oct 18 '20 11:10 gxyd

If you use @thunk in that way then it does not delay any computation. You might as well just do

function foo(x)
    dA = goo(x) + 1
    return dA
end

The correct thing to do is:

function foo(x)
    dA = @thunk(goo(x) + 1)
    return dA
end

So that running goo(x) + 1) is delayed until it is unthunked

oxinabox avatar Oct 18 '20 14:10 oxinabox

Understood.

gxyd avatar Oct 18 '20 14:10 gxyd