FluxML / FluxML/IRTools.jl

Do dynamo call broken for calls inside of methods

Open
#62 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
113
Forks
37
PR merge metrics
No merged PRs in 30d

Description

I found this interesting bug today as I was trying to move some of my code away from using macros. In the following, I'm using anonymous funcs, but I encountered the same bug in methods.
```julia
anon = () -> begin
tr = Trace()
tr() do
foo()
end
tr
end
```
This code works fine when you evaluate `anon` - it returns the `Trace`, which has been modified in the dynamo call, as expected. However, if I abstract out the call to `foo`:
```julia
anon = (call, args) -> begin
tr = Trace()
tr() do
call(args...)
end
tr
end
```
the dynamo does not apply (i.e. the `Trace` is not mutated, as a side effect).
I found a related bug as well - here, `foo` is nullary function so you might expect this to work:
```julia
anon = call -> begin
tr = Trace()
tr() do
call()
end
tr
end
```
but when I pass in `foo` as `anon(foo)`, it throws:
```julia
LoadError: BoundsError: attempt to access 0-element Array{Any,1} at index [0]
```
This is obviously not what happens outside of the dynamo, i.e.:
```julia
anon2 = call -> begin
call()
end
```
returns the result of calling `foo`.

---

Lastly, maybe I should show what the result of the dynamo is:
```julia
tr = Trace()
tr() do
foo()
end
println(tr, [:val])
```
results in
```julia
| <> (:foo => :bar) => (:q => 2) <>
| val = 1.0100076975990129
|
| <> (:foo => :bar) => (:q => 10) <>
| val = -1.0067463813037731
|
| <> (:foo => :bar) => (:q => 9) <>
| val = 0.3533852496897451
|
| <> :foo => :y <>
| val = -0.29516473908558977
|
| <> (:foo => :bar) => (:q => 3) <>
| val = -0.3524573630007816
...
```
when called outside of methods.

Edit: I'm guessing this is an `@generated` thing?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.