Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions ignition/components/logprobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
_VOCAB_TILE = 1024
# A global top-20 token is in its tile's top 20; round up to 32 for the merge sort.
_TILE_CANDIDATES = triton.next_power_of_2(MAX_TOP_LOGPROBS)
# Merge per-tile candidates in bounded chunks so large vocabularies (e.g. 256k -> 8192 keys)
# do not unroll an 8192-element bitonic network in Triton/PTX while keeping bit-exact ordering.
_MERGE_BLOCK = 512
_REDUCTION_WARPS = 4
_MERGE_WARPS = 8
_MERGE_WARPS = 4


@triton.jit
Expand Down Expand Up @@ -52,82 +55,95 @@ def _position(Records, Frame, Requested, Slots, Epochs, Live, row, col,
return slot, token, valid & (token >= 0)


@triton.jit
@triton.jit(do_not_specialize=["width"])
def _statistics(Logits, Records, Frame, Requested, Slots, Epochs, Live, Stats,
V: tl.constexpr, WIDTH: tl.constexpr, TILES: tl.constexpr,
V: tl.constexpr, width, TILES: tl.constexpr,
SLOTS: tl.constexpr, FIELDS: tl.constexpr, BLOCK: tl.constexpr):
row, col, tile = tl.program_id(0), tl.program_id(1), tl.program_id(2)
_, _, valid = _position(Records, Frame, Requested, Slots, Epochs, Live, row, col, SLOTS, FIELDS)
if valid:
pos = row.to(tl.int64) * width + col
ids = tile * BLOCK + tl.arange(0, BLOCK)
x = tl.load(Logits + (row * WIDTH + col) * V + ids, ids < V, other=-float("inf")).to(tl.float32)
x = tl.load(Logits + pos * V + ids, ids < V, other=-float("inf")).to(tl.float32)
maximum = tl.max(x, 0)
mass = tl.sum(tl.exp(x - tl.where(maximum == -float("inf"), 0., maximum)), 0)
index = (row * WIDTH + col) * TILES + tile
index = pos * TILES + tile
tl.store(Stats + index * 2, maximum)
tl.store(Stats + index * 2 + 1, mass)


@triton.jit
@triton.jit(do_not_specialize=["width"])
def _normalization(Records, Frame, Requested, Slots, Epochs, Live, Stats, Lse,
WIDTH: tl.constexpr, TILES: tl.constexpr, SLOTS: tl.constexpr,
width, TILES: tl.constexpr, SLOTS: tl.constexpr,
FIELDS: tl.constexpr, BLOCK: tl.constexpr):
row, col = tl.program_id(0), tl.program_id(1)
_, _, valid = _position(Records, Frame, Requested, Slots, Epochs, Live, row, col, SLOTS, FIELDS)
if valid:
base = (row * WIDTH + col) * TILES
pos = row.to(tl.int64) * width + col
base = pos * TILES
tiles = tl.arange(0, BLOCK)
maxima = tl.load(Stats + (base + tiles) * 2, tiles < TILES, other=-float("inf"))
masses = tl.load(Stats + (base + tiles) * 2 + 1, tiles < TILES, other=0.)
maximum = tl.max(maxima, 0)
lse = maximum + tl.log(tl.sum(masses * tl.exp(maxima - maximum), 0))
tl.store(Lse + row * WIDTH + col, lse)
tl.store(Lse + pos, lse)


@triton.jit
@triton.jit(do_not_specialize=["width"])
def _candidates(Logits, Records, Frame, Requested, Slots, Epochs, Live, Lse, Keys,
V: tl.constexpr, WIDTH: tl.constexpr, TILES: tl.constexpr,
V: tl.constexpr, width, TILES: tl.constexpr,
SLOTS: tl.constexpr, FIELDS: tl.constexpr, BLOCK: tl.constexpr,
CANDIDATES: tl.constexpr):
row, col, tile = tl.program_id(0), tl.program_id(1), tl.program_id(2)
_, _, valid = _position(Records, Frame, Requested, Slots, Epochs, Live, row, col, SLOTS, FIELDS)
if valid:
pos = row.to(tl.int64) * width + col
ids = tile * BLOCK + tl.arange(0, BLOCK)
x = tl.load(Logits + (row * WIDTH + col) * V + ids, ids < V, other=-float("inf")).to(tl.float32)
x -= tl.load(Lse + row * WIDTH + col)
x = tl.load(Logits + pos * V + ids, ids < V, other=-float("inf")).to(tl.float32)
x -= tl.load(Lse + pos)
# Rank the reported FP32 values, including ties introduced by subtraction.
keys = tl.sort(tl.where(ids < V, _sort_key(x, ids), 0), descending=True)
index = (row * WIDTH + col) * TILES + tile
index = pos * TILES + tile
k = tl.arange(0, BLOCK)
tl.store(Keys + index * CANDIDATES + k, keys, k < CANDIDATES)


@triton.jit
@triton.jit(do_not_specialize=["width"])
def _finish(Logits, Records, Frame, Requested, Slots, Epochs, Live, Lse, Keys, Output, TokenIds, Counts,
V: tl.constexpr, WIDTH: tl.constexpr, TILES: tl.constexpr,
V: tl.constexpr, width, TILES: tl.constexpr,
SLOTS: tl.constexpr, FIELDS: tl.constexpr, BLOCK: tl.constexpr,
CANDIDATES: tl.constexpr, TOP: tl.constexpr, IDS: tl.constexpr,
OUTPUT_TOKENS: tl.constexpr, OUTPUT_FIELDS: tl.constexpr, PAIR_FIELDS: tl.constexpr):
row, col = tl.program_id(0), tl.program_id(1)
slot, token, valid = _position(Records, Frame, Requested, Slots, Epochs, Live, row, col, SLOTS, FIELDS)
if valid:
pos = row.to(tl.int64) * width + col
out = Output + ((tl.load(Frame) * SLOTS + slot) * OUTPUT_TOKENS + col) * OUTPUT_FIELDS
chosen = tl.load(Logits + (row * WIDTH + col) * V + token).to(tl.float32) - tl.load(Lse + row * WIDTH + col)
chosen = tl.load(Logits + pos * V + token).to(tl.float32) - tl.load(Lse + pos)
tl.store(out, token.to(tl.int32))
tl.store(out + 1, chosen.to(tl.int32, bitcast=True))
count = tl.load(Counts + slot)
if count > 0:
selected = tl.arange(0, IDS)
requested_ids = tl.load(TokenIds + slot * IDS + selected, selected < count, other=-1)
requested_values = tl.load(Logits + (row * WIDTH + col) * V + requested_ids,
requested_values = tl.load(Logits + pos * V + requested_ids,
(selected < count) & (requested_ids >= 0) & (requested_ids < V), other=float("nan")).to(tl.float32)
requested_values -= tl.load(Lse + row * WIDTH + col)
requested_values -= tl.load(Lse + pos)
tl.store(out + (1 + selected) * PAIR_FIELDS, requested_ids, selected < count)
tl.store(out + (1 + selected) * PAIR_FIELDS + 1, requested_values.to(tl.int32, bitcast=True), selected < count)
else:
k = tl.arange(0, BLOCK)
keys = tl.load(Keys + (row * WIDTH + col) * TILES * CANDIDATES + k, k < TILES * CANDIDATES, other=0)
total: tl.constexpr = TILES * CANDIDATES
base = pos * total
keys = tl.load(Keys + base + k, k < total, other=0)
keys = tl.sort(keys, descending=True)
if total > BLOCK:
step: tl.constexpr = BLOCK - CANDIDATES
for offset in range(BLOCK, total, step):
idx = offset + (k - CANDIDATES)
incoming = tl.load(Keys + base + idx, (k >= CANDIDATES) & (idx < total), other=0)
keys = tl.where(k < CANDIDATES, keys, incoming)
keys = tl.sort(keys, descending=True)
ids, values = _sort_pair(keys)
tl.store(out + (1 + k) * PAIR_FIELDS, ids, k < TOP)
tl.store(out + (1 + k) * PAIR_FIELDS + 1, values.to(tl.int32, bitcast=True), k < TOP)
Expand All @@ -144,6 +160,7 @@ def __init__(self, logits, completion, slots, epochs, live, frame):
if not 1 <= width <= completion.token_capacity:
raise ValueError("raw logprob width exceeds completion token capacity")
tiles = triton.cdiv(vocab, _VOCAB_TILE)
merge_block = min(triton.next_power_of_2(tiles * _TILE_CANDIDATES), _MERGE_BLOCK)
self.logits = logits
self.row_inputs = (slots, epochs, live)
self.layout = {
Expand All @@ -162,8 +179,7 @@ def __init__(self, logits, completion, slots, epochs, live, frame):
(_candidates, (*common, self.lse, self.keys, *dimensions, _VOCAB_TILE,
_TILE_CANDIDATES), (batch, width, tiles), _REDUCTION_WARPS),
(_finish, (*common, self.lse, self.keys, completion.logprobs, completion.token_ids,
completion.token_id_counts, *dimensions,
triton.next_power_of_2(tiles * _TILE_CANDIDATES), _TILE_CANDIDATES,
completion.token_id_counts, *dimensions, merge_block, _TILE_CANDIDATES,
MAX_TOP_LOGPROBS, MAX_LOGPROB_TOKEN_IDS, completion.token_capacity, LOGPROB_FIELDS, LOGPROB_PAIR_FIELDS),
(batch, width, 1), _MERGE_WARPS),
)
Expand Down
Loading