SymbolicUtils.jl icon indicating copy to clipboard operation
SymbolicUtils.jl copied to clipboard

Issue with type inference in `build_function` with matrix

Open david-pl opened this issue 3 years ago • 3 comments

Posting this here rather than Symbolics because the issue disappears on SymbolicUtils v0.10.3. I'm trying to generate code out of a matrix (actually as part of QuantumCumulants.jl), but things seem to fail for sufficiently large matrices. MWE:

using SymbolicUtils
using Symbolics

# Parameters
@syms κ ω Δ γ ν

# Variables
N = 50
x = [SymbolicUtils.Sym{Number}(Symbol(:x_,j)) for j=1:N+1]

A = Matrix{Any}(undef, N+1, N+1)
A[:] .= 0
A[1,1] = (im*ω - 0.5κ)*x[1]
for j=2:N+1
    A[j,j] = (im*Δ-0.5γ-0.5ν)*x[j]
end

ps = [Δ,κ,γ,ν]
Afunc = build_function(A, ω, x, ps; expression=false)
A0 = Afunc[1](0.0, zeros(ComplexF64, N+1), ones(length(ps)))

The code runs fine up until the call to the RGF. Then the REPL starts printing the following in a loop:

_typeinf at ./compiler/typeinfer.jl:214
typeinf at ./compiler/typeinfer.jl:209
typeinf_edge at ./compiler/typeinfer.jl:806
abstract_call_method at ./compiler/abstractinterpretation.jl:490
abstract_call_gf_by_type at ./compiler/abstractinterpretation.jl:143
abstract_call_known at ./compiler/abstractinterpretation.jl:1033
abstract_call at ./compiler/abstractinterpretation.jl:1056
abstract_apply at ./compiler/abstractinterpretation.jl:738
abstract_call_known at ./compiler/abstractinterpretation.jl:952
abstract_call at ./compiler/abstractinterpretation.jl:1056
abstract_call at ./compiler/abstractinterpretation.jl:1040
abstract_eval_statement at ./compiler/abstractinterpretation.jl:1167
typeinf_local at ./compiler/abstractinterpretation.jl:1462
typeinf_nocycle at ./compiler/abstractinterpretation.jl:1520

The printing actually just slows things down but the run will still finish and produce correct results as far as I can tell. If you reduce the size, e.g. to N=5, things work perfectly fine.

Some additional notes:

  • I'm on Julia 1.6.0
  • Symbolics is v0.1.21
  • SymbolicUtils v0.11.0
  • The issue disappears on SymbolicUtils v0.10.3

david-pl avatar Apr 19 '21 09:04 david-pl

That is a pretty big matrix... But it should be made to work without this problem.

We use promote_type to infer the output type "at runtime" (hope is that it gets compiled away.) promote_type will have to splat.

shashi avatar Apr 19 '21 14:04 shashi

Big splats will cause this. Is there a way to not splat? Loop instead?

ChrisRackauckas avatar Apr 19 '21 18:04 ChrisRackauckas

Yeah we can use loops for arrays longer than 15 elements or something.

shashi avatar Apr 20 '21 02:04 shashi