Skip to content

Commit 76da813

Browse files
lkdvosclaude
andcommitted
Explain the two-stage GPU trace kernels next to the kernels
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b5b6991 commit 76da813

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,6 @@ function DeviceGenericTreeTransformer(
271271
)
272272
end
273273

274-
# Planar traces run in two stages. The first computes partial traces of every source subblock:
275-
# each thread sums a chunk of at most `chunksize` traced entries for a single remaining entry,
276-
# such that large traces (e.g. full reductions) are still spread over many threads. The second
277-
# recouples these partials with one thread per destination element, as for the generic transformers,
278-
# such that the traced entries of a source are summed only once, rather than once per destination tree.
279-
280274
"""
281275
TraceTransformerBlock{N}
282276
@@ -556,8 +550,19 @@ end
556550
end
557551
end
558552

559-
# First stage: one thread per (chunk, remaining entry) of every source subblock, writing its
560-
# partial trace to `partials[w + 1]`. Consecutive threads handle consecutive remaining entries.
553+
# Planar traces run in two kernels, such that the work per thread stays bounded even when a few
554+
# destination elements receive a huge number of traced entries (e.g. a full trace):
555+
# 1. `partial_trace_kernel!` splits the traced entries of every source subblock into `nchunks`
556+
# chunks of `chunksize`, with one thread summing one chunk for one remaining entry.
557+
# 2. `trace_recouple_kernel!` has one thread per destination element, which only combines the
558+
# `nchunks` partials of each of the `cols` sources of its block with the coefficients in `U`.
559+
# Since `chunksize` and `nchunks` are both about `√ntraced`, no thread in either kernel sums more
560+
# than `max(256, √ntraced)` entries per source, and each traced entry is read only once rather
561+
# than once per destination tree. A full trace of `n` entries uses `√n` threads in the first
562+
# kernel and a single thread per destination tree in the second.
563+
564+
# `partials[w + 1]` is the sum of chunk `k` of the traced entries of source `j` at its remaining
565+
# entry `e`; consecutive threads share `k` and have consecutive `e`.
561566
@kernel function partial_trace_kernel!(partials, data_src, sources, partial_offsets, npartials)
562567
w = @index(Global, Linear) - 1
563568
if w < npartials
@@ -577,8 +582,8 @@ end
577582
end
578583
end
579584

580-
# Second stage: one thread per destination element, as for the generic transformers, where
581-
# every source contributes the sum of its partial traces.
585+
# The traced entries were already summed by `partial_trace_kernel!`, so each thread only adds up
586+
# the partials of the sources of its destination tree.
582587
@kernel function trace_recouple_kernel!(
583588
data_dst, partials, blocks, work_offsets, coeffs, structs_dst, sources, partial_offsets,
584589
α, β, nwork

0 commit comments

Comments
 (0)