mcabbott / mcabbott/TransmuteDims.jl

Constant-propagation / inference

Open
#25 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
8
Forks
1
PR merge metrics
No merged PRs in 30d

Description

The example of https://github.com/JuliaLang/julia/issues/33435:

julia> a = rand(2, 2)
2×2 Array{Float64,2}:
 0.159947  0.956223
 0.712618  0.864652

julia> @code_warntype (x -> PermutedDimsArray(x, (2, 1)))(a)
Variables
  #self#::Core.Compiler.Const(var"##28#29"(), false)
  x::Array{Float64,2}

Body::PermutedDimsArray{Float64,2,_A,_B,Array{Float64,2}} where _B where _A
1 ─ %1 = Core.tuple(2, 1)::Core.Compiler.Const((2, 1), false)
│   %2 = Main.PermutedDimsArray(x, %1)::PermutedDimsArray{Float64,2,_A,_B,Array{Float64,2}} where _B where _A
└──      return %2

... works cleanly here:

julia> using TransmuteDims

julia> @code_warntype (x -> transmute(x, (2, 1)))(a)
Variables
  #self#::Core.Const(var"#17#18"())
  x::Matrix{Float64}

Body::LinearAlgebra.Transpose{Float64, Matrix{Float64}}
1 ─ %1 = Core.tuple(2, 1)::Core.Const((2, 1))
│   %2 = Main.transmute(x, %1)::LinearAlgebra.Transpose{Float64, Matrix{Float64}}
└──      return %2

... but makes a Transpose. Ones that make a TransmutedDimsArray aren't quite so clean:

julia> b = rand(2,2,2);

julia> @code_warntype (x -> transmute(x, (2, 3, 0, 1)))(b)
Variables
  #self#::Core.Const(var"#27#28"())
  x::Array{Float64, 3}

Body::Union{Array{Float64, 4}, TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}}
1 ─ %1 = Core.tuple(2, 3, 0, 1)::Core.Const((2, 3, 0, 1))
│   %2 = Main.transmute(x, %1)::Union{Array{Float64, 4}, TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}}
└──      return %2

julia> @code_warntype (x -> transmute(x, Val((2, 3, 0, 1))))(b)
MethodInstance for (::var"#29#30")(::Array{Float64, 3})
  from (::var"#29#30")(x) in Main at REPL[200]:1
Arguments
  #self#::Core.Const(var"#29#30"())
  x::Array{Float64, 3}
Body::TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}
1 ─ %1 = Core.tuple(2, 3, 0, 1)::Core.Const((2, 3, 0, 1))
│   %2 = Main.Val(%1)::Core.Const(Val{(2, 3, 0, 1)}())
│   %3 = Main.transmute(x, %2)::TransmutedDimsArray{Float64, 4, (2, 3, 0, 1), (4, 1, 2), Array{Float64, 3}}
└──      return %3

Although still quite fast:

julia> @btime (x -> PermutedDimsArray(x, (2, 1)))($a);
  315.051 ns (4 allocations: 176 bytes)

julia> @btime (x -> transmute(x, (2, 1)))($a);
  1.500 ns (0 allocations: 0 bytes)

julia> @btime (x -> transmute(x, (2, 3, 0, 1)))($b);
  11.845 ns (1 allocation: 16 bytes)

julia> @btime (x -> transmute(x, Val((2, 3, 0, 1))))($b);
  0.875 ns (0 allocations: 0 bytes)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the @code_warntype and @btime examples in the issue for PermutedDimsArray and transmute, including both tuple and Val forms. Read the transmute entry points and related inference behavior; done means constant propagation produces a concrete TransmutedDimsArray type without the shown union while preserving the reported performance.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.