LuxDL / Lux.jl

Elegant & Performant Scientific Machine Learning in Julia
https://lux.csail.mit.edu/
MIT License
446 stars 50 forks source link

Static Type Parameters not accessible inside `@compact` #692

Closed avik-pal closed 3 weeks ago

avik-pal commented 1 month ago
Lux.@compact(; basis_fns=Tuple(basis_fns),
        dispatch=:TensorLayer) do x::AbstractArray{T, N} where {T, N}
    x_ = Lux._eachslice(x, Val(N - 1))
    @argcheck length(x_) == length(basis_fns)
    @return map(((m, xi),) -> m(xi), zip(basis_fns, x_))
end

I would expect N to be accessible inside the function body.

avik-pal commented 3 weeks ago

This is more of a Julia syntax issue not a compact issue.

julia> map([[1], [2], [3]]) do x::AbstractVector{T} where {T}
           @show T
           return x
       end
ERROR: UndefVarError: `T` not defined
Stacktrace:
 [1] macro expansion
   @ ./show.jl:1181 [inlined]
 [2] (::var"#37#38")(x::Vector{Int64})
   @ Main ./REPL[25]:2
 [3] iterate
   @ ./generator.jl:47 [inlined]
 [4] _collect(c::Vector{Vector{Int64}}, itr::Base.Generator{Vector{Vector{Int64}}, var"#37#38"}, ::Base.EltypeUnknown, isz::Base.HasShape{1})
   @ Base ./array.jl:854
 [5] collect_similar(cont::Vector{Vector{Int64}}, itr::Base.Generator{Vector{Vector{Int64}}, var"#37#38"})
   @ Base ./array.jl:763
 [6] map(f::Function, A::Vector{Vector{Int64}})
   @ Base ./abstractarray.jl:3285
 [7] top-level scope
   @ REPL[25]:1
 [8] top-level scope
   @ none:1