methodinstance re-specializes on every lookup when the dispatch cache holds a widened method instance

Aperta
#1,060 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
julia
Ambito
compilers

Direzione di ricerca

The issue points to src/jlgen.jl where methodinstance calls specialize_method on every lookup. First read that file and the dispatch cache handling to understand how widened method instances are stored. Implement a cache for the specialized method instance keyed on the widened one, then verify with the benchmark snippet in the issue that lookup time drops to ~350 ns. Done when k_passes shows similar latency to k_typed.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When a kernel only passes a function argument on, Julia doesn't specialize on it, and the method instance in its dispatch cache has widened specTypes. methodinstance then calls specialize_method on every lookup to get the concrete one (src/jlgen.jl), which costs about 200 ns per launch:

using CUDACore
@noinline apply(f, x) = f(x)
function k_passes(f, a)      # `f` only passed on: specialized as `Function`
    i = threadIdx().x
    @inbounds a[i] = apply(f, a[i])
    return
end
function k_typed(f::F, a) where {F}
    i = threadIdx().x
    @inbounds a[i] = apply(f, a[i])
    return
end
a = CuArray(zeros(Float32, 32))
for (name, k) in (("passes", k_passes), ("typed", k_typed))
    @cuda launch=false k(sin, a)
    lookup() = cufunction(k, Tuple{typeof(sin), typeof(cudaconvert(a))})
    lookup()
    t = minimum(begin t0 = time_ns(); for _ in 1:100_000; lookup(); end; (time_ns() - t0) / 100_000 end for _ in 1:10)
    println(name, ": ", round(t), " ns")
end
passes: 555.0 ns
typed: 351.0 ns

(CUDACore 6.4.2, Julia 1.12.) AcceleratedKernels.jl hit this in nine reduction kernels and its merge sorts, where it was 2-6% of the time of a small reduction; it now spells out the argument types. Caching the specialized method instance, e.g. keyed on the widened one, would avoid it for everyone.

Lingua principale
Julia
Stelle
190
Fork
72
Merge medio
20h 35m
PR unite (30g)
59

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaGPU/GPUCompiler.jl

Tutte le issue di JuliaGPU/GPUCompiler.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.