laya / code /tt_laya /tests /test_host_plan.py
changh95's picture
tt-model push laya (container)
f882f26 verified
Raw History Blame Contribute Delete
6.41 kB
# SPDX-License-Identifier: Apache-2.0
"""Host tests of the megakernel's launch plan (no device): packed row placement, plan words, attention items,
launch packing.
python -m pytest tt_laya/tests/test_host_plan.py -q
"""
from __future__ import annotations
import pytest
from tt_laya.tt.mk import geometry as G
def rows(*lens):
return [G.LaunchRow(n, i % 3) for i, n in enumerate(lens)]
def test_packed_back_to_back():
p = G.place_rows(rows(67, 71, 67, 60))
assert [x.start for x in p] == [0, 67, 138, 205]
assert sum(x.tokens for x in p) == 265 and -(-(p[-1].start + p[-1].tokens) // 32) == 9
assert [(x.g0, x.tiles, x.a, x.e) for x in p][1] == (2, 3, 3, 10)
def test_aligned_mode():
p = G.place_rows(rows(67, 71), align=True)
assert [x.start for x in p] == [0, 96]
def test_limits_force_alignment():
# a 512-token row may not straddle 17 tiles; short rows may not put more than NSEG rows on one tile
p = G.place_rows(rows(20, 512))
assert p[1].start == 32 and p[1].tiles == 16
p = G.place_rows(rows(*([5] * 10)))
for g in range(3):
assert sum(1 for x in p if x.g0 <= g < x.g0 + x.tiles) <= G.NSEG
assert G.place_rows(rows(*([400] * 2))) is None # 25 row tiles > T_MAX
def test_plan_words_and_segments():
lr = rows(67, 71, 67, 60)
w, p = G.plan_rows(lr, 10, 120, 28, 2)
T = w[G.PL_T]
assert (T, w[G.PL_NBA], w[G.PL_NROWS]) == (9, 9, 4)
nseg = [w[G.PL_TILE + 4 * g] for g in range(T)]
assert nseg == [1, 1, 2, 1, 2, 1, 2, 1, 1]
seg0 = [w[G.PL_ROW + 8 * r + 6] for r in range(4)]
assert seg0 == [0, 1, 1, 1]
per = G.assign_items(p, 16, 120, w)
items = [it for c in per for it in c]
assert len(items) == 16 * sum(x.tiles for x in p) and max(len(c) for c in per) <= G.IT_MAX
for it in items: # every (row, head, query tile) exactly once
assert 1 <= ((it - 1) & 31) <= 16
def test_pack_launches_respects_capacity():
from tt_laya.text import Row
from tt_laya.tt.mk.engine import pack_launches
rs = [Row(ids=list(range(150)), markers=[5, 9], qtype=0, n_options=2, options_distinct=2, tokens_per_option=None,
state_tokens=0, state_used=0) for _ in range(9)]
groups = pack_launches(rs)
assert [len(g) for g in groups] == [4, 4, 1]
for g in groups:
assert G.place_rows([G.LaunchRow(150, 0)] * len(g)) is not None
def test_one_row_tile_program_fits_and_deepens_the_ring():
for m in (G.Model(32, 82, 128, 28, 2, 16, 3), G.Model(24, 36, 96, 22, 2, 12, 3)): # english, multilingual
s2, s1 = G.max_slots(m, 12, 12), G.max_slots(m, 12, 64, rmax=1)
assert G.cb_bytes(m, 12, s2) <= G.CB_LIMIT and G.cb_bytes(m, 12, s1, rmax=1) <= G.CB_LIMIT
assert s2 == 12 and s1 >= 32
def test_pack_launches_row_tile_limit():
from tt_laya.text import Row
from tt_laya.tt.mk.engine import pack_launches
rs = [Row(ids=list(range(n)), markers=[1], qtype=0, n_options=2, options_distinct=2, tokens_per_option=None,
state_tokens=0, state_used=0) for n in (114, 67, 78, 64, 71)]
assert pack_launches(rs) == [[0, 1, 2, 3, 4]] # 13 row tiles: the 2-row-tile program
assert pack_launches(rs, t_max=10) == [[0, 1, 2], [3, 4]]
def test_trace_guard_releases_every_registered_engine():
from tt_laya.tt import traces
class Eng:
def __init__(self):
self.dropped = 0
def drop_traces(self):
self.dropped += 1
return True
dev, other = object(), object()
a, b, c = Eng(), Eng(), Eng()
traces.register(dev, a)
traces.register(dev, b)
traces.register(other, c)
assert traces.invalidate(dev) == 2 and (a.dropped, b.dropped, c.dropped) == (1, 1, 0)
def test_item_assignment_is_the_plain_lpt():
import random
def lpt(placed, H, n_cores, qch): # the reference: linear scan for the least-loaded core with room
items = []
for rho, p in enumerate(placed):
for h in range(H):
for i0 in range(0, p.tiles, qch):
n = min(qch, p.tiles - i0)
items.append((n * p.tiles, rho, h, i0, n))
items.sort(key=lambda z: (-z[0], z[1], z[2], z[3]))
load, per = [0] * n_cores, [[] for _ in range(n_cores)]
for cost, rho, h, i0, n in items:
k = min((i for i in range(n_cores) if len(per[i]) < G.IT_MAX), key=lambda i: (load[i], i))
per[k].append(G.item_code(rho, h, i0, n))
load[k] += cost
return per
rnd = random.Random(0)
for _ in range(30):
lens = [rnd.randint(5, 300) for _ in range(rnd.randint(1, 8))]
placed = G.place_rows(rows(*lens))
if placed is None:
continue
T = sum(p.tiles for p in placed)
qch = 1 if 16 * T <= 240 else (2 if 16 * T <= 480 else 4)
w = [0] * G.PLAN_WORDS
assert G.assign_items(placed, 16, 120, w) == lpt(placed, 16, 120, qch)
def test_sdpa_key_chunk_count_is_even_for_every_bucket():
"""tt-metal 44d6650's non-causal SDPA is wrong for an odd key-chunk count (1 included): every sequence bucket the
configuration can produce (LAYA_SEQ_BUCKETS 64..8192 step 32, LAYA_MAX_LEN rounded up to 32) gets an even count;
the default buckets keep their chunks (1024 -> 512)."""
import sys
from tt_laya import config as C
from tt_laya.tt.ttnn_engine import sdpa_key_chunk
assert "ttnn" not in sys.modules
for S in range(64, 8192 + 1, 32):
k = sdpa_key_chunk(S) # the served path
assert k % 32 == 0 and 32 <= k <= min(S, 512) and -(-S // k) % 2 == 0, (S, k)
for kmax in (256, 1024): # the debug override (--sdpa q/k caps)
assert -(-S // sdpa_key_chunk(S, kmax)) % 2 == 0, (S, kmax)
assert [sdpa_key_chunk(S) for S in (1024, 2048, 4096, 8192, 1216, 160)] == [512, 512, 512, 512, 384, 128]
for env in ({}, {"LAYA_PROFILE": "multilingual"}, {"LAYA_MAX_LEN": "1216"}, {"LAYA_MAX_LEN": "1100"},
{"LAYA_MAX_LEN": "8192"}, {"LAYA_ENGINE": "ttnn", "LAYA_SEQ_BUCKETS": "160,192,288,320,416,448,512"}):
for S in C.resolve_config(env).seq_buckets:
assert -(-S // sdpa_key_chunk(S)) % 2 == 0, (env, S)
with pytest.raises(ValueError):
C.resolve_config({"LAYA_ENGINE": "ttnn", "LAYA_SEQ_BUCKETS": "32,512"})