Skip to content

Could not find unique name for Reactant.TracedUtils.TypeCast{Float64}()_broadcast_scalar when compiling function with lots of matrix multiplications #1616

@unit-of-inductance

Description

@unit-of-inductance

Consider the following MWE

using Reactant

n_points = 10000
dim = 10

points = Reactant.to_rarray(eachcol(rand(dim,n_points)))
matrix = Reactant.to_rarray(rand(1, dim))

function f(points)
  return sum(matrix * point for point in points)
end

f_compiled = @compile f(points)

This raises

Could not find unique name for Reactant.TracedUtils.TypeCast{Float64}()_broadcast_scalar
Stracktrace

Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] __lookup_unique_name_in_module(mod::Reactant.MLIR.IR.Module, name::String)
    @ Reactant.TracedUtils ~/.julia/packages/Reactant/EsmaI/src/TracedUtils.jl:998
  [3] (::Reactant.TracedUtils.var"#13#20"{…})()
    @ Reactant.TracedUtils ~/.julia/packages/Reactant/EsmaI/src/TracedUtils.jl:842
  [4] block!(f::Reactant.TracedUtils.var"#13#20"{…}, blk::Reactant.MLIR.IR.Block)
    @ Reactant.MLIR.IR ~/.julia/packages/Reactant/EsmaI/src/mlir/IR/Block.jl:214
  [5] finalize_mlir_fn(result::Reactant.TracedRNumber{…}, traced_args::Vector{…}, linear_args::Vector{…}, skipped_args::Vector{…}, seen_args::Reactant.OrderedIdDict{…}, fnbody::Reactant.MLIR.IR.Block, func::Reactant.MLIR.IR.Operation, mod::Reactant.MLIR.IR.Module, name::String, in_tys::Vector{…}, do_transpose::Bool, optimize_then_pad::Bool, inv_map::IdDict{…}, args_in_result::Symbol, resprefix::Symbol, argprefix::Symbol, resargprefix::Symbol, verify_arg_names::Nothing, return_dialect::Symbol, traced_args_to_shardings::Reactant.OrderedIdDict{…}, output_shardings::Nothing, sym_visibility::Reactant.MLIR.IR.Attribute, num_replicas::Int64, runtime::Nothing, construct_function_without_args::Bool, args::Tuple{…}, N::Int64, concretein::Bool, toscalar::Bool)
    @ Reactant.TracedUtils ~/.julia/packages/Reactant/EsmaI/src/TracedUtils.jl:841
  [6] make_mlir_fn(f::Reactant.TracedUtils.TypeCast{…}, args::Tuple{…}, kwargs::Tuple{}, name::String, concretein::Bool; toscalar::Bool, return_dialect::Symbol, args_in_result::Symbol, construct_function_without_args::Bool, do_transpose::Bool, input_shardings::Nothing, output_shardings::Nothing, runtime::Nothing, verify_arg_names::Nothing, argprefix::Symbol, resprefix::Symbol, resargprefix::Symbol, num_replicas::Int64, optimize_then_pad::Bool)
    @ Reactant.TracedUtils ~/.julia/packages/Reactant/EsmaI/src/TracedUtils.jl:352
  [7] elem_apply(f::Function, args::Reactant.TracedRArray{Float64, 2})
    @ Reactant.TracedUtils ~/.julia/packages/Reactant/EsmaI/src/TracedUtils.jl:1084
  [8] elem_apply(::Type{Float64}, x::Reactant.TracedRArray{Float64, 2})
    @ Reactant.TracedUtils ~/.julia/packages/Reactant/EsmaI/src/TracedUtils.jl:1016
  [9] _copyto!(dest::Reactant.TracedRArray{Float64, 2}, bc::Base.Broadcast.Broadcasted{Nothing, Tuple{…}, Type{…}, Tuple{…}})
    @ Reactant.TracedRArrayOverrides ~/.julia/packages/Reactant/EsmaI/src/TracedRArray.jl:773
 [10] copyto!(dest::Reactant.TracedRArray{Float64, 2}, bc::Base.Broadcast.Broadcasted{Nothing, Tuple{…}, Type{…}, Tuple{…}})
    @ Reactant.TracedRArrayOverrides ~/.julia/packages/Reactant/EsmaI/src/TracedRArray.jl:727
 [11] copyto!
    @ ./broadcast.jl:925 [inlined]
 [12] copy(bc::Base.Broadcast.Broadcasted{…})
    @ Reactant.TracedRArrayOverrides ~/.julia/packages/Reactant/EsmaI/src/TracedRArray.jl:718
 [13] materialize(bc::Base.Broadcast.Broadcasted{…})
    @ Base.Broadcast ./broadcast.jl:872
 [14] overloaded_mul!(C::Reactant.TracedRArray{T, 2} where T, A::AbstractMatrix, B::AbstractMatrix, α::Bool, β::Bool)
    @ Reactant.TracedLinearAlgebra ~/.julia/packages/Reactant/EsmaI/src/stdlibs/LinearAlgebra.jl:258
 [15] overloaded_mul!(C::Reactant.TracedRArray{Float64, 1}, A::AbstractMatrix, B::AbstractVector, α::Bool, β::Bool)
    @ Reactant.TracedLinearAlgebra ~/.julia/packages/Reactant/EsmaI/src/stdlibs/LinearAlgebra.jl:220
 [16] #mul!
    @ ~/.julia/packages/Reactant/EsmaI/src/Overlay.jl:111 [inlined]
 [17] (::Nothing)(none::typeof(LinearAlgebra.mul!), none::Reactant.TracedRArray{…}, none::ConcretePJRTArray{…}, none::SubArray{…}, none::Bool, none::Bool)
    @ Reactant ./<missing>:0
 [18] call_with_reactant(::typeof(LinearAlgebra.mul!), ::Reactant.TracedRArray{…}, ::ConcretePJRTArray{…}, ::SubArray{…}, ::Bool, ::Bool)
    @ Reactant ~/.julia/packages/Reactant/EsmaI/src/utils.jl:501
 [19] mul!(C::Reactant.TracedRArray{…}, A::ConcretePJRTArray{…}, B::SubArray{…})
    @ Reactant ~/.julia/packages/Reactant/EsmaI/src/Overlay.jl:126
 [20] *
    @ /nix/store/0f8r9l0fn26mjj96j08p92ak8gmbgwas-julia-bin-1.11.6/share/julia/stdlib/v1.11/LinearAlgebra/src/matmul.jl:60 [inlined]
 [21] (::Nothing)(none::typeof(*), none::ConcretePJRTArray{…}, none::SubArray{…})
    @ Reactant ./<missing>:0
 [22] getproperty
    @ ~/.julia/packages/Reactant/EsmaI/src/Types.jl:11 [inlined]
 [23] size
    @ ~/.julia/packages/Reactant/EsmaI/src/ConcreteRArray.jl:92 [inlined]
 [24] axes
    @ ./abstractarray.jl:98 [inlined]
 [25] axes
    @ ./abstractarray.jl:77 [inlined]
 [26] *
    @ /nix/store/0f8r9l0fn26mjj96j08p92ak8gmbgwas-julia-bin-1.11.6/share/julia/stdlib/v1.11/LinearAlgebra/src/matmul.jl:60 [inlined]
 [27] call_with_reactant(::typeof(*), ::ConcretePJRTArray{…}, ::SubArray{…})
    @ Reactant ~/.julia/packages/Reactant/EsmaI/src/utils.jl:0
 [28] (::Nothing)(none::var"#28#29", none::SubArray{Reactant.TracedRNumber{…}, 1, Reactant.TracedRArray{…}, Tuple{…}, true})
    @ Reactant ./<missing>:0
 [29] #28
    @ ./Base.jl:0 [inlined]
 [30] call_with_reactant(::var"#28#29", ::SubArray{Reactant.TracedRNumber{…}, 1, Reactant.TracedRArray{…}, Tuple{…}, true})
    @ Reactant ~/.julia/packages/Reactant/EsmaI/src/utils.jl:0
 [31] MappingRF
    @ ./reduce.jl:100 [inlined]
 [32] _foldl_impl
    @ ./reduce.jl:62 [inlined]
 [33] (::Nothing)(none::typeof(Base._foldl_impl), none::Base.MappingRF{…}, none::Base._InitialValue, none::ColumnSlices{…})
    @ Reactant ./<missing>:0
 [34] getproperty
    @ ./Base.jl:49 [inlined]
 [35] axes
    @ ./slicearray.jl:229 [inlined]
 [36] axes1
    @ ./abstractarray.jl:137 [inlined]
 [37] eachindex
    @ ./abstractarray.jl:321 [inlined]
 [38] iterate
    @ ./abstractarray.jl:1207 [inlined]
 [39] _foldl_impl
    @ ./reduce.jl:56 [inlined]
 [40] call_with_reactant(::typeof(Base._foldl_impl), ::Base.MappingRF{…}, ::Base._InitialValue, ::ColumnSlices{…})
    @ Reactant ~/.julia/packages/Reactant/EsmaI/src/utils.jl:0
 [41] foldl_impl
    @ ./reduce.jl:48 [inlined]
 [42] mapfoldl_impl
    @ ./reduce.jl:44 [inlined]
 [43] mapfoldl
    @ ./reduce.jl:175 [inlined]
 [44] mapreduce
    @ ./reduce.jl:307 [inlined]
 [45] sum
    @ ./reduce.jl:532 [inlined]
 [46] sum
    @ ./reduce.jl:561 [inlined]
 [47] f
    @ ./REPL[11]:2 [inlined]
 [48] (::Nothing)(none::typeof(f), none::ColumnSlices{Reactant.TracedRArray{…}, Tuple{…}, SubArray{…}})
    @ Reactant ./<missing>:0
 [49] foldl_impl
    @ ./reduce.jl:48 [inlined]
 [50] mapfoldl_impl
    @ ./reduce.jl:44 [inlined]
 [51] mapfoldl
    @ ./reduce.jl:175 [inlined]
 [52] mapreduce
    @ ./reduce.jl:307 [inlined]
 [53] sum
    @ ./reduce.jl:532 [inlined]
 [54] sum
    @ ./reduce.jl:561 [inlined]
 [55] f
    @ ./REPL[11]:2 [inlined]
 [56] call_with_reactant(::typeof(f), ::ColumnSlices{Reactant.TracedRArray{…}, Tuple{…}, SubArray{…}})
    @ Reactant ~/.julia/packages/Reactant/EsmaI/src/utils.jl:0
 [57] make_mlir_fn(f::typeof(f), args::Tuple{…}, kwargs::@NamedTuple{}, name::String, concretein::Bool; toscalar::Bool, return_dialect::Symbol, args_in_result::Symbol, construct_function_without_args::Bool, do_transpose::Bool, input_shardings::Nothing, output_shardings::Nothing, runtime::Val{…}, verify_arg_names::Nothing, argprefix::Symbol, resprefix::Symbol, resargprefix::Symbol, num_replicas::Int64, optimize_then_pad::Bool)
    @ Reactant.TracedUtils ~/.julia/packages/Reactant/EsmaI/src/TracedUtils.jl:332
 [58] compile_mlir!(mod::Reactant.MLIR.IR.Module, f::Function, args::Tuple{…}, compile_options::CompileOptions, callcache::Dict{…}, sdycache::Dict{…}; fn_kwargs::@NamedTuple{}, backend::String, runtime::Val{…}, legalize_stablehlo_to_mhlo::Bool, kwargs::@Kwargs{})
    @ Reactant.Compiler ~/.julia/packages/Reactant/EsmaI/src/Compiler.jl:1555
 [59] compile_mlir! (repeats 2 times)
    @ ~/.julia/packages/Reactant/EsmaI/src/Compiler.jl:1522 [inlined]
 [60] compile_xla(f::Function, args::Tuple{…}; before_xla_optimizations::Bool, client::Nothing, serializable::Bool, kwargs::@Kwargs{…})
    @ Reactant.Compiler ~/.julia/packages/Reactant/EsmaI/src/Compiler.jl:3433
 [61] compile_xla
    @ ~/.julia/packages/Reactant/EsmaI/src/Compiler.jl:3406 [inlined]
 [62] compile(f::Function, args::Tuple{…}; kwargs::@Kwargs{…})
    @ Reactant.Compiler ~/.julia/packages/Reactant/EsmaI/src/Compiler.jl:3505
Some type information was truncated. Use `show(err)` to see complete types.

It also dumps some other info:
...
  "func.func"() <{function_type = (tensor<f64>, tensor<f64>) -> (tensor<f64>, tensor<f64>, tensor<f64>), sym_name = "+_broadcast_scalar_4997", sym_visibility = "private"}> ({
  ^bb0(%arg6: tensor<f64>, %arg7: tensor<f64>):
    %1 = "stablehlo.add"(%arg6, %arg7) : (tensor<f64>, tensor<f64>) -> tensor<f64>
    "func.return"(%1, %arg6, %arg7) : (tensor<f64>, tensor<f64>, tensor<f64>) -> ()
  }) : () -> ()
  "func.func"() <{function_type = (tensor<f64>) -> tensor<f64>, sym_name = "Reactant.TracedUtils.TypeCast{Float64}()_broadcast_scalar_9998", sym_visibility = "private"}> ({
  ^bb0(%arg5: tensor<f64>):
    "func.return"(%arg5) : (tensor<f64>) -> ()
  }) : () -> ()
  "func.func"() <{function_type = (tensor<f64>) -> tensor<f64>, sym_name = "Reactant.TracedUtils.TypeCast{Float64}()_broadcast_scalar_9999", sym_visibility = "private"}> ({
  ^bb0(%arg4: tensor<f64>):
    "func.return"(%arg4) : (tensor<f64>) -> ()
  }) : () -> ()
  "func.func"() <{function_type = (tensor<f64>, tensor<f64>) -> (tensor<f64>, tensor<f64>, tensor<f64>), sym_name = "+_broadcast_scalar_4998", sym_visibility = "private"}> ({
  ^bb0(%arg2: tensor<f64>, %arg3: tensor<f64>):
    %0 = "stablehlo.add"(%arg2, %arg3) : (tensor<f64>, tensor<f64>) -> tensor<f64>
    "func.return"(%0, %arg2, %arg3) : (tensor<f64>, tensor<f64>, tensor<f64>) -> ()
  }) : () -> ()
  "func.func"() <{function_type = (tensor<f64>) -> tensor<f64>, sym_name = "Reactant.TracedUtils.TypeCast{Float64}()_broadcast_scalar_10000", sym_visibility = "private"}> ({
  ^bb0(%arg1: tensor<f64>):
    "func.return"(%arg1) : (tensor<f64>) -> ()
  }) : () -> ()
  "func.func"() <{function_type = (tensor<f64>) -> (), sym_name = "Reactant.TracedUtils.TypeCast{Float64}()_broadcast_scalar_tmp"}> ({
  ^bb0(%arg0: tensor<f64>):
    "func.return"(%arg0) : (tensor<f64>) -> ()
  }) : () -> ()
}) : () -> ()

Replicated in fresh environment with the following Pkg.status():

Status `/tmp/jl_qVYfKn/Project.toml`
  [3c362404] Reactant v0.2.160

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions