Giter Site home page Giter Site logo

Static array shape checking about jet.jl HOT 6 OPEN

aviatesk avatar aviatesk commented on June 10, 2024 1
Static array shape checking

from jet.jl.

Comments (6)

MasonProtter avatar MasonProtter commented on June 10, 2024 1

Yes, statically sized arrays work out of the box, it's trivial to check:

julia> using StaticArrays, JET

julia> x = SizedVector{3}(rand(3)); y = SizedMatrix{4, 3}(rand(4, 3));

julia> f(x, y) = x' * (y*x)
f (generic function with 1 method)

julia> @report_call f(x, y)
═════ 1 possible error found ═════
┌ @ REPL[9]:1 Main.*(Main.'(x), Main.*(y, x))
│┌ @ /home/mason/julia/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/adjtrans.jl:308 LinearAlgebra.dot(Base.getproperty(u, :parent), v)
││┌ @ /home/mason/.julia/packages/StaticArrays/NTbHj/src/linalg.jl:189 StaticArrays.same_size(a, b)
│││┌ @ /home/mason/.julia/packages/StaticArrays/NTbHj/src/traits.jl:186 StaticArrays._throw_size_mismatch(as...)
││││┌ @ /home/mason/.julia/packages/StaticArrays/NTbHj/src/traits.jl:191 StaticArrays._throw_size_mismatch(::SizedVector{3, Float64, Vector{Float64}}, ::SizedVector{4, Float64, Vector{Float64}})
│││││ may throw: StaticArrays.throw(StaticArrays.DimensionMismatch(Base.string("Sizes ", StaticArrays.map(StaticArrays._size, Argument(2))::Any, " of input arrays do not match")::Any)::Any)
││││└────────────────────────────────────────────────────────────────────
Union{}

julia> @report_call f(x, x*x')
No errors !
Float64

from jet.jl.

aviatesk avatar aviatesk commented on June 10, 2024

Thanks for raising this interesting issue.

In Julia, most information about array shape doesn't appear in Julia's type lattice (i.e. compile time), which its type inference works on, but only appear in runtime.
Since JET.jl is internally powered by Julia's native type inference system (and so it inherits its limitations), JET.jl can reports type level errors like NoMethodError but it can't report those involved with shape and such, like DimensionMismatch.

julia> using JET
[ Info: Precompiling JET [c3a54625-cd67-489e-a8e7-0a5a0ff4e31b]

julia> @report_call [1,2,3]' * [1,2,3]'
═════ 1 possible error found ═════
┌ @ /Users/aviatesk/julia/julia/usr/share/julia/stdlib/v1.7/LinearAlgebra/src/adjtrans.jl:317 *(::LinearAlgebra.Adjoint{Int64, Vector{Int64}}, ::LinearAlgebra.Adjoint{Int64, Vector{Int64}})
│ may throw: LinearAlgebra.throw(LinearAlgebra.MethodError(LinearAlgebra.*, Core.tuple(Argument(2), Argument(3))::Tuple{LinearAlgebra.Adjoint{Int64, Vector{Int64}}, LinearAlgebra.Adjoint{Int64, Vector{Int64}}})::Any)
└─────────────────────────────────────────────────────────────────────────────────────────────
Union{}

julia> @report_call [1,2,3]' * [1,2,3,4]
No errors !
Int64

Static array shape checking will require lots of work, extending the type lattice and maybe huge modification of its abstract interpretation logic, as such I don't think I will working on it in near future (I'm focusing on improving the quality of type-level checks for now).

But, hopefully, once I've got satisfied with the type-level error check, I think a more advanced error check like array shape checking might be my next scope.


On the other hand, there are static analysis approaches like : https://github.com/google-research/swift-tfp

Well, JET.jl is actually supposed to check errors mostly statically, and so swift-tfp's approach (abstract interpretation) is in fact same as JET for array shape checking.

Anyway, thanks for telling me interesting sets of discussions and projects !

from jet.jl.

AriMKatz avatar AriMKatz commented on June 10, 2024

Glad to hear! What about packages like https://github.com/JuliaArrays/StaticArrays.jl which have array dims in type parameters? (If we can separate codegen from type logic and dispatch)

There are also abstract tracing type approaches : FluxML/Mjolnir.jl#1

Also can you elaborate on the "mostly statically" and the connection to tfp?

Thanks !

from jet.jl.

AriMKatz avatar AriMKatz commented on June 10, 2024

That's awesome. Now we just need to deal with the codegen issue right ?

from jet.jl.

MasonProtter avatar MasonProtter commented on June 10, 2024

What codegen issue?

from jet.jl.

AriMKatz avatar AriMKatz commented on June 10, 2024

JuliaLang/julia#11339

Would make static arrays more versatile

from jet.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.