JuliaLabs / Cassette.jl

Overdub Your Julia Code
Other
371 stars 35 forks source link

Error in overdub with StaticArrays.jl #198

Closed ranocha closed 2 years ago

ranocha commented 2 years ago

I observed the following error with overdub and StaticArrays.jl in Julia v1.7.3:

julia> using Pkg; Pkg.activate(temp=true)
  Activating new project at `/tmp/jl_15u02F`

julia> Pkg.add("Cassette")
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `/tmp/jl_15u02F/Project.toml`
  [7057c7e9] + Cassette v0.3.10
    Updating `/tmp/jl_15u02F/Manifest.toml`
  [7057c7e9] + Cassette v0.3.10

julia> Pkg.add(name="StaticArrays", version="1.4.5")
   Resolving package versions...
    Updating `/tmp/jl_15u02F/Project.toml`
  [90137ffa] + StaticArrays v1.4.5
    Updating `/tmp/jl_15u02F/Manifest.toml`
  [90137ffa] + StaticArrays v1.4.5
  [56f22d72] + Artifacts
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [9a3f8284] + Random
  [ea8e919c] + SHA
  [9e88b42a] + Serialization
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [e66e0078] + CompilerSupportLibraries_jll
  [4536629a] + OpenBLAS_jll
  [8e850b90] + libblastrampoline_jll

julia> using Cassette, StaticArrays

julia> SVector((0.0, 1.0)) # fine without overdub
2-element SVector{2, Float64} with indices SOneTo(2):
 0.0
 1.0

julia> Cassette.@context Ctx
Cassette.Context{nametype(Ctx)}

julia> Cassette.overdub(Ctx(), () -> begin
           SVector((0.0, 1.0))
       end)
ERROR: MethodError: no method matching Size(::TypeVar)
Closest candidates are:
  Size(::Union{StaticArrays.Dynamic, Int64}...) at ~/.julia/packages/StaticArrays/G7IlJ/src/traits.jl:67
  Size(::Tuple{Vararg{Union{StaticArrays.Dynamic, Int64}}}) at ~/.julia/packages/StaticArrays/G7IlJ/src/traits.jl:66
  Size(::T) where T<:AbstractArray at ~/.julia/packages/StaticArrays/G7IlJ/src/traits.jl:88
  ...
Stacktrace:
  [1] call
    @ ~/.julia/packages/Cassette/34vIw/src/context.jl:456 [inlined]
  [2] fallback
    @ ~/.julia/packages/Cassette/34vIw/src/context.jl:454 [inlined]
  [3] _overdub_fallback(::Any, ::Vararg{Any})
    @ ~/.julia/packages/Cassette/34vIw/src/overdub.jl:586 [inlined]
  [4] overdub
    @ ~/.julia/packages/Cassette/34vIw/src/overdub.jl:586 [inlined]
  [5] Size(::Type{SVector})
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/traits.jl:90 [inlined]
  [6] overdub
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/traits.jl:90 [inlined]
  [7] overdub(::Cassette.Context{nametype(Ctx), Nothing, Nothing, Cassette.var"##PassType#301", Nothing, Nothing}, ::Type{Size}, ::Type{SVector})
    @ Cassette ~/.julia/packages/Cassette/34vIw/src/overdub.jl:0
  [8] size(::Type{SVector})
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/abstractarray.jl:4 [inlined]
  [9] overdub
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/abstractarray.jl:4 [inlined]
 [10] overdub(::Cassette.Context{nametype(Ctx), Nothing, Nothing, Cassette.var"##PassType#301", Nothing, Nothing}, ::typeof(size), ::Type{SVector})
    @ Cassette ~/.julia/packages/Cassette/34vIw/src/overdub.jl:0
 [11] overdub
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/convert.jl:114 [inlined]
 [12] overdub(::Cassette.Context{nametype(Ctx), Nothing, Nothing, Cassette.var"##PassType#301", Nothing, Nothing}, ::typeof(StaticArrays.length_match_size), ::Type{SVector}, ::Tuple{Float64, Float64})
    @ Cassette ~/.julia/packages/Cassette/34vIw/src/overdub.jl:0
 [13] adapt_size(::Type{SVector}, ::Tuple{Float64, Float64})
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/convert.jl:86 [inlined]
 [14] overdub
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/convert.jl:86 [inlined]
 [15] overdub(::Cassette.Context{nametype(Ctx), Nothing, Nothing, Cassette.var"##PassType#301", Nothing, Nothing}, ::typeof(StaticArrays.adapt_size), ::Type{SVector}, ::Tuple{Float64, Float64})
    @ Cassette ~/.julia/packages/Cassette/34vIw/src/overdub.jl:0
 [16] construct_type(::Type{SVector}, ::Tuple{Float64, Float64})
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/convert.jl:79 [inlined]
 [17] overdub
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/convert.jl:79 [inlined]
 [18] overdub(::Cassette.Context{nametype(Ctx), Nothing, Nothing, Cassette.var"##PassType#301", Nothing, Nothing}, ::typeof(StaticArrays.construct_type), ::Type{SVector}, ::Tuple{Float64, Float64})
    @ Cassette ~/.julia/packages/Cassette/34vIw/src/overdub.jl:0
 [19] (SVector)(::Tuple{Float64, Float64})
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/convert.jl:154 [inlined]
 [20] overdub
    @ ~/.julia/packages/StaticArrays/G7IlJ/src/convert.jl:154 [inlined]
 [21] (::var"
    @ ./REPL[7]:2 [inlined]
 [22] overdub
    @ ./REPL[7]:2 [inlined]
 [23] overdub(overdub_context#299::Cassette.Context{nametype(Ctx), Nothing, Nothing, Cassette.var"##PassType#301", Nothing, Nothing}, overdub_arguments#300::var"#2#3")
    @ Cassette ~/.julia/packages/Cassette/34vIw/src/overdub.jl:0
 [24] top-level scope
    @ REPL[7]:1

Note that everything was fine with StaticArrays.jl v1.4.4:

julia> using Pkg; Pkg.activate(temp=true)
  Activating new project at `/tmp/jl_ujBhRQ`

julia> Pkg.add("Cassette")
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `/tmp/jl_ujBhRQ/Project.toml`
  [7057c7e9] + Cassette v0.3.10
    Updating `/tmp/jl_ujBhRQ/Manifest.toml`
  [7057c7e9] + Cassette v0.3.10

julia> Pkg.add(name="StaticArrays", version="1.4.4")
   Resolving package versions...
    Updating `/tmp/jl_ujBhRQ/Project.toml`
  [90137ffa] + StaticArrays v1.4.4
    Updating `/tmp/jl_ujBhRQ/Manifest.toml`
  [90137ffa] + StaticArrays v1.4.4
  [56f22d72] + Artifacts
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [9a3f8284] + Random
  [ea8e919c] + SHA
  [9e88b42a] + Serialization
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [e66e0078] + CompilerSupportLibraries_jll
  [4536629a] + OpenBLAS_jll
  [8e850b90] + libblastrampoline_jll

julia> using Cassette, StaticArrays

julia> SVector((0.0, 1.0)) # fine without overdub
2-element SVector{2, Float64} with indices SOneTo(2):
 0.0
 1.0

julia> Cassette.@context Ctx
Cassette.Context{nametype(Ctx)}

julia> Cassette.overdub(Ctx(), () -> begin
           SVector((0.0, 1.0))
       end)
2-element SVector{2, Float64} with indices SOneTo(2):
 0.0
 1.0

However, it looks more like a problem with Cassette.jl to me, since the version without overdub works fine with both versions of StaticArrays.jl.

ranocha commented 2 years ago

@mateuszbaran Do you have an idea what might have caused this problem in StaticArrays.jl?

mateuszbaran commented 2 years ago

My guess is that it was most likely caused by this change: https://github.com/JuliaArrays/StaticArrays.jl/pull/1016 but it looks like a Cassette bug:

julia> Cassette.overdub(Ctx(), () -> begin
                  StaticArrays.has_size(SVector)
              end)
true

julia> StaticArrays.has_size(SVector)
false
ranocha commented 2 years ago

Thanks, @mateuszbaran! Yes, I think it's a bug in Cassette.jl, too.

ferrolho commented 2 years ago

@ranocha, this should now be fixed with StaticArrays v1.5.1. Can you confirm and then close this issue? :)

ranocha commented 2 years ago

Yes, this specific issue is fixed with StaticArrays v1.5.1, but it looks like the underlying bug in Cassette.jl is probably still there. I can't say for sure whether there is anything else since we just removed Cassette.jl from our test suite, see https://github.com/trixi-framework/Trixi.jl/pull/1173.