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

オープン
#1,060 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
julia
領域
compilers

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Julia
スター
190
フォーク
72
平均マージ
16時間 57分
マージ済み PR(30日)
40

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

JuliaGPU/GPUCompiler.jl のほかの issue

JuliaGPU/GPUCompiler.jl の issue をすべて見る

似ている issue

Julia の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。