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

Inference issues with broadcasting #153

Open
tshort opened this issue Oct 18, 2019 · 20 comments
Open

Inference issues with broadcasting #153

tshort opened this issue Oct 18, 2019 · 20 comments

Comments

@tshort
Copy link

tshort commented Oct 18, 2019

I'm not sure if this is a repeat.

julia> function gbitarray(i)
           a = 1.0:0.1:10.0
           @inbounds i > 3 ? a[(a.>3.0) .& (a .< 5.0)][1] : a[5] 
       end

julia> using Cassette: @context, overdub

julia> using Test

julia> @inferred(gbitarray(4))
3.1

julia> @context Ctx;

julia> @inferred(overdub(Ctx(), gbitarray, 4))
ERROR: return type Float64 does not match inferred return type Any
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at REPL[7]:1

The following function infers fine:

function gbitarray1(i)
    a = 1.0:0.1:10.0
    @inbounds i > 3 ? a[a.>3.0][1] : a[5] 
end

I tried it on Cassette master and v0.2.6. This is on Julia v1.3.0-rc3.0 on Linux. I'm not sure how to debug inference issues.

@tshort
Copy link
Author

tshort commented Oct 18, 2019

Defining the following seems to make it infer (I think). That's based on a hint in another inference issue.

@inline Cassette.overdub(ctx::Ctx, f, args...) = f(args...)

@tshort
Copy link
Author

tshort commented Oct 18, 2019

But, does that bypass the overdubbing?

@vchuravy
Copy link
Member

vchuravy commented Oct 18, 2019 via email

@tshort
Copy link
Author

tshort commented Oct 19, 2019

I think it's a broadcasting operation causing the issue. The example in #56 also fails for me, so there may have been an inference regression on this.

@tshort
Copy link
Author

tshort commented Oct 19, 2019

Here's a simpler function where inference fails:

@inferred Cassette.overdub(Ctx(), x -> (x.+x)[1], [8,9,99])

@tshort
Copy link
Author

tshort commented Oct 19, 2019

I can follow the path of inference failures with Cthulhu, but I don't know why it's failing.

@tshort
Copy link
Author

tshort commented Oct 19, 2019

There is a broadcast test, and it is failing for me.

@tshort
Copy link
Author

tshort commented Oct 19, 2019

These seem to infer fine on Julia v1.2.0, so something changed with broadcasting since then.

@vchuravy
Copy link
Member

Thanks I will have a look at it.

@tshort
Copy link
Author

tshort commented Dec 30, 2019

The following infers with Julia v1.2.0 and v1.4.0-DEV.666 (2019-12-26), but in doesn't infer with Julia v1.3.0.

using Cassette: @context, overdub
using Test
@context Ctx;
@inferred overdub(Ctx(), x -> (x.+x)[1], [8,9,99])

Currently, Cassette's tests don't pass on Julia v1.3.0. Does that mean that "full" Cassette support will skip v1.3.0? I mainly ask because its an unusual situation where Cassette is at the mercy of base-Julia changes.

Edit: this also fails to infer in Julia v1.3.1.

@oxinabox
Copy link
Contributor

Can this issue be retitled sinmce as the MWEs demonstrate this is not restricted to bitarry ?

@oxinabox
Copy link
Contributor

oxinabox commented Feb 18, 2020

The following infers with Julia v1.2.0 and v1.4.0-DEV.666 (2019-12-26), but in doesn't infer with Julia v1.3.0.

for me it fails to infer in:
Version 1.4.0-DEV.713 (2020-01-22) and in 1.5.0-DEV.139 (2020-01-22)
(as well as 1.3, but it is fine in 1.2)


We can even go slightly simpler:

using Cassette: @context, overdub
using Test
@context Ctx;
@inferred overdub(Ctx(), x -> x.+x, [99])

@tshort tshort changed the title Inference issue with a bitarray operation Inference issues with broadcasting Feb 18, 2020
@vchuravy
Copy link
Member

vchuravy commented Feb 18, 2020

We can even go slightly simpler:

That passes for me on (on Cassette master):

Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

@oxinabox
Copy link
Contributor

That passes for me on (on Cassette master):
Thats weird. weird. weird.

Here are my version infos:

Julia Version 1.2.1-pre.0
Commit 93929550b6 (2019-08-20 05:33 UTC)

Julia Version 1.3.2-pre.0
Commit 2e6715c045 (2019-12-31 00:49 UTC)

Julia Version 1.4.0-DEV.713
Commit 8852200b83 (2020-01-22 12:38 UTC)

Julia Version 1.5.0-DEV.139
Commit c2abaeedf8* (2020-01-22 06:54 UTC)

Platform Info:
  OS: macOS (x86_64-apple-darwin19.0.0)
  CPU: Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

(12) pkg> st --manifest
Status `~/temp/12/Manifest.toml`
  [7057c7e9] Cassette v0.3.1 #master (https://github.com/jrevels/Cassette.jl.git)

Possibilities:

  1. Weird Mac OS interaction?
  2. Something to do with me building from source?
  3. Ghosts.

@oxinabox
Copy link
Contributor

Digging down into it

Something close to metal that fails

using Test
using Cassette: @context, overdub, disablehooks
@context Ctx;
const ctx = disablehooks(Ctx())  # disabling hooks makes code_typed shorter to read

@inferred overdub(ctx, Base.Broadcast.instantiate, Base.broadcasted(+, [99],[99]))

And if we look at the @code_warntype (on Julia 1.5-DEV)
This seems to be where thigns fo wrong

3 ┄ %17 = Cassette.overdub(##overdub_context#253, Core.apply_type, Base.Broadcast.Broadcasted, $(QuoteNode(Base.Broadcast.DefaultArrayStyle{1})))::Core.Compiler.Const(Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Axes,F,Args} where Args<:Tuple where F where Axes, false)
│   %18 = Cassette.overdub(##overdub_context#253, Base.getproperty, bc, :f)::Core.Compiler.Const(+, false)
│   %19 = Cassette.overdub(##overdub_context#253, Base.getproperty, bc, :args)::Tuple{Array{Int64,1},Array{Int64,1}}
│   %20 = Cassette.overdub(##overdub_context#253, %17, %18, %19, axes)::Any

Whereas in Julia 1.2
It is:

3 ┄ %19 = Cassette.overdub(##overdub_context#371, Core.apply_type, Base.Broadcast.Broadcasted, $(QuoteNode(Base.Broadcast.DefaultArrayStyle{1})))::Core.Compiler.Const(Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Axes,F,Args} where Args<:Tuple where F where Axes, false)
│   %20 = Cassette.overdub(##overdub_context#371, Base.getproperty, bc, :f)::Core.Compiler.Const(+, false)
│   %21 = Cassette.overdub(##overdub_context#371, Base.getproperty, bc, :args)::Tuple{Array{Int64,1},Array{Int64,1}}
│   %22 = Cassette.overdub(##overdub_context#371, %19, %20, %21, axes)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(+),Tuple{Array{Int64,1},Array{Int64,1}}}
└──       return %22

@vchuravy
Copy link
Member

Digging into that with Cthulhu shows that a failure to infer print_to_string which I have noticed elsewhere leads to a Base.throw being inferred to return Any, thus polluting the return type of bcs. This reminded me that I started looking at it #166

@vchuravy
Copy link
Member

@tshort can you test that this helps your use-case?

@tshort
Copy link
Author

tshort commented Feb 18, 2020

With Julia v1.3.1 and Cassette master, the following both infer fine now!

  • gbitarray() from the first post
  • overdub(Ctx(), x -> (x.+x)[1], [8,9,99])

@tshort
Copy link
Author

tshort commented Feb 18, 2020

@oxinabox, does that fix your tests, too?

@oxinabox
Copy link
Contributor

Working on 1.3. 🎉
Still fails on 1.4 and 1.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants