JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.68k stars 5.48k forks source link

`hvcat` with UniformScaling type unstable #44408

Closed baggepinnen closed 2 years ago

baggepinnen commented 2 years ago

Tested on Version 1.9.0-DEV.105 (2022-02-28)

julia> foo() = [1.0*I zeros(2,3); zeros(2, 5)]
foo (generic function with 1 method)

julia> foo()
4×5 Matrix{Float64}:
 1.0  0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0

julia> @code_warntype foo()
MethodInstance for foo()
  from foo() in Main at REPL[155]:1
Arguments
  #self#::Core.Const(foo)
Body::Any
1 ─ %1 = Core.tuple(2, 1)::Core.Const((2, 1))
│   %2 = (1.0 * Main.I)::Core.Const(UniformScaling{Float64}(1.0))
│   %3 = Main.zeros(2, 3)::Matrix{Float64}
│   %4 = Main.zeros(2, 5)::Matrix{Float64}
│   %5 = Base.hvcat(%1, %2, %3, %4)::Any
└──      return %5
baggepinnen commented 2 years ago

A very similar problem appears with Adjoints, but only on julia v1.7.1, not on v1.9

Julia v1.7

julia> foo() = [randn(1,1)' zeros(1,1)]
foo (generic function with 1 method)

julia> @code_warntype foo()
MethodInstance for foo()
  from foo() in Main at REPL[163]:1
Arguments
  #self#::Core.Const(foo)
Body::Any
1 ─ %1 = Main.randn(1, 1)::Matrix{Float64}
│   %2 = Main.:var"'"(%1)::Adjoint{Float64, Matrix{Float64}}
│   %3 = Main.zeros(1, 1)::Matrix{Float64}
│   %4 = Base.hcat(%2, %3)::Any
└──      return %4

julia v1.9

julia> foo() = [randn(1,1)' zeros(1,1)]
foo (generic function with 1 method)

julia> @code_warntype foo()
MethodInstance for foo()
  from foo() in Main at REPL[3]:1
Arguments
  #self#::Core.Const(foo)
Body::Matrix{Float64}
1 ─ %1 = Main.randn(1, 1)::Matrix{Float64}
│   %2 = Main.:var"'"(%1)::LinearAlgebra.Adjoint{Float64, Matrix{Float64}}
│   %3 = Main.zeros(1, 1)::Matrix{Float64}
│   %4 = Base.hcat(%2, %3)::Matrix{Float64}
└──      return %4
dkarrasch commented 2 years ago

I can't reproduce the issue with the adjoint.

              _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.7.1 (2021-12-22)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using LinearAlgebra

julia> foo() = [randn(1,1)' zeros(1,1)]
foo (generic function with 1 method)

julia> foo()
1×2 Matrix{Float64}:
 1.05996  0.0

julia> @code_warntype foo()
MethodInstance for foo()
  from foo() in Main at REPL[2]:1
Arguments
  #self#::Core.Const(foo)
Body::Matrix{Float64}
1 ─ %1 = Main.randn(1, 1)::Matrix{Float64}
│   %2 = Main.:var"'"(%1)::Adjoint{Float64, Matrix{Float64}}
│   %3 = Main.zeros(1, 1)::Matrix{Float64}
│   %4 = Base.hcat(%2, %3)::Matrix{Float64}
└──      return %4

And I can't reproduce the first issue on a very fresh built of master.

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.9.0-DEV.127 (2022-03-03)
 _/ |\__'_|_|_|\__'_|  |  Commit 8eb872c159* (0 days old master)
|__/                   |

julia> using LinearAlgebra

julia> foo() = [1.0*I zeros(2,3); zeros(2, 5)]
foo (generic function with 1 method)

julia> foo()
4×5 Matrix{Float64}:
 1.0  0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0

julia> @code_warntype foo()
MethodInstance for foo()
  from foo() in Main at REPL[2]:1
Arguments
  #self#::Core.Const(foo)
Body::Matrix{Float64}
1 ─ %1 = Core.tuple(2, 1)::Core.Const((2, 1))
│   %2 = (1.0 * Main.I)::Core.Const(UniformScaling{Float64}(1.0))
│   %3 = Main.zeros(2, 3)::Matrix{Float64}
│   %4 = Main.zeros(2, 5)::Matrix{Float64}
│   %5 = Base.hvcat(%1, %2, %3, %4)::Matrix{Float64}
└──      return %5

julia> foo() = [randn(1,1)' zeros(1,1)]
foo (generic function with 1 method)

julia> @code_warntype foo()
MethodInstance for foo()
  from foo() in Main at REPL[5]:1
Arguments
  #self#::Core.Const(foo)
Body::Matrix{Float64}
1 ─ %1 = Main.randn(1, 1)::Matrix{Float64}
│   %2 = Main.:var"'"(%1)::Adjoint{Float64, Matrix{Float64}}
│   %3 = Main.zeros(1, 1)::Matrix{Float64}
│   %4 = Base.hcat(%2, %3)::Matrix{Float64}
└──      return %4
baggepinnen commented 2 years ago

Hmm, you're right. I must have been bitten by https://github.com/andreasvarga/DescriptorSystems.jl/issues/8 again, loading DescriptorSystems makes the problem appear. I can't imagine how I managed to get the problem to appear on v1.9 though since DescriptorSystems can not precompile there

baggepinnen commented 2 years ago

Thanks for checking :)