Draft
Conversation
Contributor
Author
|
Noticed a very significant slowdown when the using Base.ScopedValues: ScopedValue, with
const _SCOPED_INF_CACHE = ScopedValue{Vector{CC.InferenceResult}}()
function cuTileInterpreter(cache::CacheView; always_inline::Bool=true)
method_table = get_method_table_view(cache.world)
inf_cache = isassigned(_SCOPED_INF_CACHE) ? _SCOPED_INF_CACHE[] : Vector{CC.InferenceResult}()
...Click to see test script with output# MWE: Shared inference cache for cuTileInterpreter
#
# Simulates the autotuning pattern: one MI, one CacheView, many
# const-seeded inference passes with different Constant values.
using cuTile
import cuTile: Constant, _SCOPED_INF_CACHE, cuTileInterpreter, cuTileMethodTable,
CuTileResults, emit_ir
import Core.Compiler as CC
using CompilerCaching: CacheView, method_instance, typeinf!
using Base.ScopedValues: with
const ct = cuTile
# ── Two kernels: with and without `order` kwarg ──────────────────────
function kernel_order(A::ct.TileArray, B::ct.TileArray, TILE_M::Int, TILE_N::Int)
bid = ct.bid(1)
tile = ct.load(A, (bid, 1i32), (TILE_M, TILE_N);
padding_mode=ct.PaddingMode.Zero, order=(1, 2))
ct.store(B, (bid, 1i32), tile)
return
end
function kernel_no_order(A::ct.TileArray, B::ct.TileArray, TILE_M::Int, TILE_N::Int)
bid = ct.bid(1)
tile = ct.load(A, (bid, 1i32), (TILE_M, TILE_N);
padding_mode=ct.PaddingMode.Zero)
ct.store(B, (bid, 1i32), tile)
return
end
# ── Benchmark: mirrors the real autotuning pattern ───────────────────
function bench_inference(f, configs; use_scoped_cache::Bool)
world = Base.get_world_counter()
base_argtypes = Tuple{ct.TileArray{Float32, 2}, ct.TileArray{Float32, 2}, Int, Int}
mi = something(
method_instance(f, base_argtypes; world, method_table=cuTileMethodTable),
method_instance(f, base_argtypes; world),
)
# One CacheView for all configs (same as real autotuning)
opts = (sm_arch="sm_120", opt_level=3, num_ctas=nothing, occupancy=nothing)
cache = CacheView{CuTileResults}((:mwe, opts, objectid(f), use_scoped_cache, time_ns()), world)
function do_infer()
# Generic inference once (establishes the CI)
interp = cuTileInterpreter(cache)
typeinf!(cache, interp, mi)
# Const-seeded inference per config (the hot path)
for (tile_m, tile_n) in configs
const_argtypes = Any[
CC.Const(f),
ct.TileArray{Float32, 2},
ct.TileArray{Float32, 2},
CC.Const(tile_m),
CC.Const(tile_n),
]
interp2 = cuTileInterpreter(cache)
typeinf!(cache, interp2, mi, const_argtypes)
end
end
if use_scoped_cache
with(_SCOPED_INF_CACHE => CC.InferenceResult[]) do
do_infer()
end
else
do_infer()
end
end
# ── Config grid (match real autotuning scale) ────────────────────────
tile_ms = [16, 32, 64, 128, 256]
tile_ns = [16, 32, 64, 128, 256]
configs = [(m, n) for m in tile_ms for n in tile_ns]
println("Configs: $(length(configs))")
# ── Warmup ───────────────────────────────────────────────────────────
print("Warmup... ")
bench_inference(kernel_order, configs[1:1]; use_scoped_cache=false)
bench_inference(kernel_no_order, configs[1:1]; use_scoped_cache=false)
println("done")
for label in ("Run 1", "Run 2")
println("\n──── $label ────")
println("\n ═══ WITH order=(1,2) ═══")
t1 = @elapsed bench_inference(kernel_order, configs; use_scoped_cache=false)
println(" Fresh: $(round(t1, digits=3))s ($(round(t1/length(configs)*1000, digits=1))ms/cfg)")
t2 = @elapsed bench_inference(kernel_order, configs; use_scoped_cache=true)
println(" Shared: $(round(t2, digits=3))s ($(round(t2/length(configs)*1000, digits=1))ms/cfg)")
println(" Speedup: $(round(t1/t2, digits=1))x")
println("\n ═══ WITHOUT order ═══")
t3 = @elapsed bench_inference(kernel_no_order, configs; use_scoped_cache=false)
println(" Fresh: $(round(t3, digits=3))s ($(round(t3/length(configs)*1000, digits=1))ms/cfg)")
t4 = @elapsed bench_inference(kernel_no_order, configs; use_scoped_cache=true)
println(" Shared: $(round(t4, digits=3))s ($(round(t4/length(configs)*1000, digits=1))ms/cfg)")
println(" Speedup: $(round(t3/t4, digits=1))x")
println("\n ═══ Summary ═══")
println(" order overhead (fresh): $(round(t1/t3, digits=2))x")
println(" order overhead (shared): $(round(t2/t4, digits=2))x")
endThis matters a lot because it was making the codegen stage for a matmul 10x slower, even though the resulting Tile IR was identical. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.