basal-engine 3.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- basal/__init__.py +7 -0
- basal/adapter.py +420 -0
- basal/adapters.py +8 -0
- basal/agree.py +393 -0
- basal/attach.py +160 -0
- basal/attn512.py +132 -0
- basal/auth.py +194 -0
- basal/bench.py +367 -0
- basal/blockquant.py +227 -0
- basal/calibrate.py +2198 -0
- basal/chat.py +175 -0
- basal/cli.py +70 -0
- basal/client.py +8 -0
- basal/decision_log.py +189 -0
- basal/decision_rules.py +569 -0
- basal/decisions.py +158 -0
- basal/derive.py +98 -0
- basal/disconnect.py +85 -0
- basal/engine.py +6023 -0
- basal/evidence.py +133 -0
- basal/examples/choice.jsonl +8 -0
- basal/examples/complex.jsonl +5 -0
- basal/examples/labelled_tickets.jsonl +500 -0
- basal/examples/local_backend.py +34 -0
- basal/examples/noul.jsonl +8 -0
- basal/examples/questions.jsonl +20 -0
- basal/examples/score.jsonl +8 -0
- basal/examples/selfcheck_basal-omni-pico-Q8_0.json +1 -0
- basal/examples/selfcheck_basal-omni-pico.json +1 -0
- basal/examples/types_demo.py +74 -0
- basal/facts.py +497 -0
- basal/feedback.py +234 -0
- basal/file_identity.py +39 -0
- basal/finetune.py +321 -0
- basal/fit_decisions.py +944 -0
- basal/fla_configs.json +1822 -0
- basal/fold.py +206 -0
- basal/fonts/OFL-DMSans.txt +93 -0
- basal/fonts/OFL-SpaceGrotesk.txt +93 -0
- basal/fonts/dm-sans-latin-ext.woff2 +0 -0
- basal/fonts/dm-sans-latin.woff2 +0 -0
- basal/fonts/space-grotesk-latin-ext.woff2 +0 -0
- basal/fonts/space-grotesk-latin.woff2 +0 -0
- basal/fp8block.py +100 -0
- basal/gdn_graph.py +1792 -0
- basal/gemma4.py +726 -0
- basal/gemma4_graph.py +931 -0
- basal/gguf_export.py +497 -0
- basal/identity.py +217 -0
- basal/int8block.py +50 -0
- basal/interpolate.py +330 -0
- basal/jsonl.py +172 -0
- basal/lean.py +204 -0
- basal/llama_attn.py +252 -0
- basal/llama_attn_kernels.py +266 -0
- basal/llama_graph.py +587 -0
- basal/llama_kernels.py +593 -0
- basal/loadtest.py +189 -0
- basal/mcp_server.py +432 -0
- basal/media.py +138 -0
- basal/metrics.py +16 -0
- basal/mlx_attn_kernels.py +1057 -0
- basal/mlx_forward.py +326 -0
- basal/mlx_kernels.py +543 -0
- basal/mlx_metal.py +510 -0
- basal/navcut.py +283 -0
- basal/navigate.py +646 -0
- basal/navtree.py +397 -0
- basal/nvfp4.py +245 -0
- basal/ordercheck.py +165 -0
- basal/pdf.py +183 -0
- basal/prepared.py +355 -0
- basal/presets.py +8 -0
- basal/prompt.py +462 -0
- basal/rank.py +373 -0
- basal/readout.py +121 -0
- basal/request.py +327 -0
- basal/response_cache.py +105 -0
- basal/router.py +1551 -0
- basal/router_ui.html +902 -0
- basal/router_ui.py +277 -0
- basal/run.py +376 -0
- basal/selfcheck.py +215 -0
- basal/server.py +5279 -0
- basal/sets.py +214 -0
- basal/signals.py +28 -0
- basal/state_disk.py +942 -0
- basal/strix.py +505 -0
- basal/synth.py +92 -0
- basal/tasks.py +848 -0
- basal/textio.py +21 -0
- basal/think.py +390 -0
- basal/tools.py +171 -0
- basal/tpu.py +336 -0
- basal/tpu_kernels.py +129 -0
- basal/trace.py +125 -0
- basal/ui.html +861 -0
- basal/ui.py +526 -0
- basal/varloop.py +813 -0
- basal/varloop_graph.py +1377 -0
- basal/varloop_kernels.py +907 -0
- basal/viz.html +660 -0
- basal/viz.py +375 -0
- basal/vllm_unified.py +100 -0
- basal_engine-3.0.3.dist-info/METADATA +416 -0
- basal_engine-3.0.3.dist-info/RECORD +111 -0
- basal_engine-3.0.3.dist-info/WHEEL +5 -0
- basal_engine-3.0.3.dist-info/entry_points.txt +21 -0
- basal_engine-3.0.3.dist-info/licenses/LICENSE +202 -0
- basal_engine-3.0.3.dist-info/licenses/NOTICE +34 -0
- basal_engine-3.0.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"""Self-contained pure-Triton attention kernels for Llama / Bielik 3.0 (basal/llama_attn.py).
|
|
2
|
+
Kept separate from 3.1+'s attn_meta_kernels.py so 3.0.1 forward-ports cleanly across release/3.1..3.4 with zero merge
|
|
3
|
+
conflicts.
|
|
4
|
+
|
|
5
|
+
Supports every Llama / Bielik full-attention row kind in one CUDA-graph-capturable kernel:
|
|
6
|
+
- Plain causal rows (single prompts, Pass-1 roots)
|
|
7
|
+
- Compact varlen rows (HAS_ST: prompt start index per token)
|
|
8
|
+
- Pass-2 prefix-trie rows over cached root KV (TREE: SB block id, ANC ancestor matrix, NR root keys)
|
|
9
|
+
- Single-pass Flat SOAM rows (FLAT: PR prefix end, SE subtree end)
|
|
10
|
+
- Native two-way, prefix-bidirectional, and dual reading (HAS_TW: TW [B] for plain, TW [Lk] for flat)
|
|
11
|
+
- Last-layer GQA-grouped readout rows (HAS_QI, HP = largest pow2 divisor of GQA group)
|
|
12
|
+
- Pure-Triton Split-KV readout attention (readout_splitk_kernel + readout_splitk_reduce) for long contexts with R << Lk
|
|
13
|
+
"""
|
|
14
|
+
try:
|
|
15
|
+
import triton
|
|
16
|
+
import triton.language as tl
|
|
17
|
+
except ImportError as _e: # CUDA/ROCm only; imported lazily inside llama_attn._build()
|
|
18
|
+
raise ImportError("triton is required for basal.llama_attn_kernels") from _e
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@triton.jit
|
|
22
|
+
def _mm(a, b, IEEE: tl.constexpr):
|
|
23
|
+
if IEEE:
|
|
24
|
+
return tl.dot(a, b, input_precision="ieee")
|
|
25
|
+
return tl.dot(a, b)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@triton.jit
|
|
29
|
+
def _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, keys, kin, vis, offs_d, qk_scale,
|
|
30
|
+
MASKED: tl.constexpr, D: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr,
|
|
31
|
+
IEEE: tl.constexpr):
|
|
32
|
+
if MASKED:
|
|
33
|
+
k = tl.load(kb + keys[None, :] * sk_l + offs_d[:, None], mask=kin[None, :], other=0.0)
|
|
34
|
+
else:
|
|
35
|
+
k = tl.load(kb + keys[None, :] * sk_l + offs_d[:, None])
|
|
36
|
+
s = _mm(q, k, IEEE) * qk_scale
|
|
37
|
+
if MASKED:
|
|
38
|
+
s = tl.where(vis, s, float("-inf"))
|
|
39
|
+
m_new = tl.maximum(m_i, tl.max(s, 1))
|
|
40
|
+
m_use = tl.where(m_new == float("-inf"), 0.0, m_new)
|
|
41
|
+
else:
|
|
42
|
+
m_new = tl.maximum(m_i, tl.max(s, 1))
|
|
43
|
+
m_use = m_new
|
|
44
|
+
p = tl.math.exp2(s - m_use[:, None])
|
|
45
|
+
alpha = tl.math.exp2(m_i - m_use)
|
|
46
|
+
l_i = l_i * alpha + tl.sum(p, 1)
|
|
47
|
+
if MASKED:
|
|
48
|
+
v = tl.load(vb + keys[:, None] * sv_l + offs_d[None, :], mask=kin[:, None], other=0.0)
|
|
49
|
+
else:
|
|
50
|
+
v = tl.load(vb + keys[:, None] * sv_l + offs_d[None, :])
|
|
51
|
+
acc = acc * alpha[:, None] + _mm(p.to(v.dtype), v, IEEE)
|
|
52
|
+
return acc, m_new, l_i
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@triton.jit(do_not_specialize=["Mq", "NR"])
|
|
56
|
+
def llama_attn_kernel(Q, K, V, O, QI, PR, ST, SB, ANC, SE, TW,
|
|
57
|
+
sq_b16, sq_h16, sq_l16, sk_b16, sk_h16, sk_l16, sv_b16, sv_h16, sv_l16,
|
|
58
|
+
so_b16, so_l16, so_h16, s_qi, s_sb, s_anc_b, s_anc_r,
|
|
59
|
+
Mq, NR,
|
|
60
|
+
H: tl.constexpr, GROUP: tl.constexpr, HP: tl.constexpr,
|
|
61
|
+
BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, D: tl.constexpr,
|
|
62
|
+
HAS_QI: tl.constexpr, HAS_PR: tl.constexpr, HAS_ST: tl.constexpr,
|
|
63
|
+
TREE: tl.constexpr, FLAT: tl.constexpr, HAS_TW: tl.constexpr,
|
|
64
|
+
IEEE: tl.constexpr, QK_SCALE: tl.constexpr):
|
|
65
|
+
qk_scale = QK_SCALE
|
|
66
|
+
sq_b, sq_h, sq_l = sq_b16 * 16, sq_h16 * 16, sq_l16 * 16
|
|
67
|
+
sk_b, sk_h, sk_l = sk_b16 * 16, sk_h16 * 16, sk_l16 * 16
|
|
68
|
+
sv_b, sv_h, sv_l = sv_b16 * 16, sv_h16 * 16, sv_l16 * 16
|
|
69
|
+
so_b, so_l, so_h = so_b16 * 16, so_l16 * 16, so_h16 * 16
|
|
70
|
+
MQT: tl.constexpr = BLOCK_M // HP
|
|
71
|
+
HG: tl.constexpr = H // HP
|
|
72
|
+
pid_t = tl.num_programs(0) - 1 - tl.program_id(0) # longest causal rows first
|
|
73
|
+
bh = tl.program_id(1)
|
|
74
|
+
b = (bh // HG).to(tl.int64)
|
|
75
|
+
hg = bh % HG
|
|
76
|
+
hk = (hg * HP) // GROUP
|
|
77
|
+
m = tl.arange(0, BLOCK_M)
|
|
78
|
+
h = hg * HP + m // MQT
|
|
79
|
+
i = pid_t * MQT + m % MQT
|
|
80
|
+
qv = i < Mq
|
|
81
|
+
offs_d = tl.arange(0, D)
|
|
82
|
+
offs_n = tl.arange(0, BLOCK_N)
|
|
83
|
+
q = tl.load(Q + b * sq_b + h[:, None].to(tl.int64) * sq_h + i[:, None].to(tl.int64) * sq_l + offs_d[None, :],
|
|
84
|
+
mask=qv[:, None], other=0.0)
|
|
85
|
+
qi = tl.load(QI + b * s_qi + i, mask=qv, other=0) if HAS_QI else i
|
|
86
|
+
# Root / prefix key interval [lo, hi)
|
|
87
|
+
if TREE: # Pass 2: every branch token comes after the root and sees all pr[b] root keys
|
|
88
|
+
if HAS_PR:
|
|
89
|
+
hi = tl.minimum(tl.full([BLOCK_M], tl.load(PR + b), tl.int64), NR)
|
|
90
|
+
else:
|
|
91
|
+
hi = tl.full([BLOCK_M], NR, tl.int64)
|
|
92
|
+
else:
|
|
93
|
+
hi = tl.minimum(qi + 1, NR)
|
|
94
|
+
if HAS_TW:
|
|
95
|
+
if FLAT:
|
|
96
|
+
hi = tl.maximum(hi, tl.minimum(tl.load(TW + qi, mask=qv, other=0), NR))
|
|
97
|
+
else:
|
|
98
|
+
hi = tl.minimum(tl.maximum(qi + 1, tl.load(TW + b)), NR)
|
|
99
|
+
if HAS_PR:
|
|
100
|
+
if FLAT:
|
|
101
|
+
hi = tl.minimum(hi, tl.load(PR + qi, mask=qv, other=0))
|
|
102
|
+
else:
|
|
103
|
+
hi = tl.minimum(hi, tl.load(PR + b))
|
|
104
|
+
lo = tl.load(ST + qi, mask=qv, other=0) if HAS_ST else tl.zeros_like(qi)
|
|
105
|
+
big = 2147483647
|
|
106
|
+
empty = hi <= lo
|
|
107
|
+
ulo = tl.min(tl.where(qv & ~empty, lo, big), 0)
|
|
108
|
+
uhi = tl.max(tl.where(qv & ~empty, hi, 0), 0)
|
|
109
|
+
has = ulo < uhi
|
|
110
|
+
ulo = tl.where(has, ulo, 0)
|
|
111
|
+
uhi = tl.where(has, uhi, 0)
|
|
112
|
+
ilo = tl.max(tl.where(qv, lo, -big), 0)
|
|
113
|
+
ihi = tl.min(tl.where(qv, hi, big), 0)
|
|
114
|
+
a0 = ulo + (tl.maximum(ilo - ulo, 0) + BLOCK_N - 1) // BLOCK_N * BLOCK_N
|
|
115
|
+
a1 = a0 + tl.maximum(ihi - a0, 0) // BLOCK_N * BLOCK_N
|
|
116
|
+
ok = has & (tl.max((qv & empty).to(tl.int32), 0) == 0) & (a1 > a0)
|
|
117
|
+
a0 = tl.where(ok, a0, uhi)
|
|
118
|
+
a1 = tl.where(ok, a1, uhi)
|
|
119
|
+
kb = K + b * sk_b + hk * sk_h
|
|
120
|
+
vb = V + b * sv_b + hk * sv_h
|
|
121
|
+
m_i = tl.full([BLOCK_M], float("-inf"), tl.float32)
|
|
122
|
+
l_i = tl.zeros([BLOCK_M], tl.float32)
|
|
123
|
+
acc = tl.zeros([BLOCK_M, D], tl.float32)
|
|
124
|
+
for s0 in tl.range(ulo, a0, BLOCK_N):
|
|
125
|
+
keys = s0 + offs_n
|
|
126
|
+
vis = (keys[None, :] >= lo[:, None]) & (keys[None, :] < hi[:, None])
|
|
127
|
+
acc, m_i, l_i = _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, keys.to(tl.int64), keys < uhi, vis,
|
|
128
|
+
offs_d, qk_scale, True, D, BLOCK_M, BLOCK_N, IEEE)
|
|
129
|
+
for s0 in tl.range(a0, a1, BLOCK_N):
|
|
130
|
+
keys = s0 + offs_n
|
|
131
|
+
acc, m_i, l_i = _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, keys.to(tl.int64), keys < uhi, keys,
|
|
132
|
+
offs_d, qk_scale, False, D, BLOCK_M, BLOCK_N, IEEE)
|
|
133
|
+
for s0 in tl.range(a1, uhi, BLOCK_N):
|
|
134
|
+
keys = s0 + offs_n
|
|
135
|
+
vis = (keys[None, :] >= lo[:, None]) & (keys[None, :] < hi[:, None])
|
|
136
|
+
acc, m_i, l_i = _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, keys.to(tl.int64), keys < uhi, vis,
|
|
137
|
+
offs_d, qk_scale, True, D, BLOCK_M, BLOCK_N, IEEE)
|
|
138
|
+
if TREE: # Pass-2 branch keys NR + j: own earlier block + ancestor blocks
|
|
139
|
+
bq = tl.load(SB + b * s_sb + qi, mask=qv, other=0)
|
|
140
|
+
jend = tl.max(tl.where(qv, qi, -1), 0) + 1
|
|
141
|
+
for s0 in tl.range(0, jend, BLOCK_N):
|
|
142
|
+
j = s0 + offs_n
|
|
143
|
+
jin = j < jend
|
|
144
|
+
bj = tl.load(SB + b * s_sb + j, mask=jin, other=0)
|
|
145
|
+
an = tl.load(ANC + b * s_anc_b + bq[:, None].to(tl.int64) * s_anc_r + bj[None, :],
|
|
146
|
+
mask=qv[:, None] & jin[None, :], other=0)
|
|
147
|
+
vis = (((an != 0) & (j[None, :] <= qi[:, None])) | (j[None, :] == qi[:, None])) & jin[None, :]
|
|
148
|
+
acc, m_i, l_i = _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, (NR + j).to(tl.int64), jin, vis,
|
|
149
|
+
offs_d, qk_scale, True, D, BLOCK_M, BLOCK_N, IEEE)
|
|
150
|
+
if FLAT: # Flat SOAM branch keys past root prefix hi up to qi, filtered by subtree-end SE
|
|
151
|
+
br = qv & (qi >= hi)
|
|
152
|
+
f0 = tl.min(tl.where(br, hi, big), 0)
|
|
153
|
+
f1 = tl.max(tl.where(br, qi + 1, 0), 0)
|
|
154
|
+
for s0 in tl.range(f0, f1, BLOCK_N):
|
|
155
|
+
keys = s0 + offs_n
|
|
156
|
+
kin = keys < f1
|
|
157
|
+
se = tl.load(SE + keys, mask=kin, other=0)
|
|
158
|
+
vis = (keys[None, :] >= hi[:, None]) & (keys[None, :] <= qi[:, None]) & (qi[:, None] < se[None, :]) & kin[None, :]
|
|
159
|
+
acc, m_i, l_i = _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, keys.to(tl.int64), kin, vis,
|
|
160
|
+
offs_d, qk_scale, True, D, BLOCK_M, BLOCK_N, IEEE)
|
|
161
|
+
out = acc / tl.where(l_i > 0, l_i, 1.0)[:, None]
|
|
162
|
+
tl.store(O + b * so_b + i[:, None].to(tl.int64) * so_l + h[:, None].to(tl.int64) * so_h + offs_d[None, :],
|
|
163
|
+
out.to(O.dtype.element_ty), mask=qv[:, None])
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@triton.jit(do_not_specialize=["Mq", "NR"])
|
|
167
|
+
def readout_splitk_kernel(Q, K, V, OP, LP, QI, PR, ST, SE, TW,
|
|
168
|
+
sq_b16, sq_h16, sq_l16, sk_b16, sk_h16, sk_l16, sv_b16, sv_h16, sv_l16,
|
|
169
|
+
s_qi, Mq, NR,
|
|
170
|
+
H: tl.constexpr, GROUP: tl.constexpr, HP: tl.constexpr, BLOCK_M: tl.constexpr,
|
|
171
|
+
BLOCK_N: tl.constexpr, D: tl.constexpr, NSPLITS: tl.constexpr,
|
|
172
|
+
HAS_QI: tl.constexpr, HAS_PR: tl.constexpr, HAS_ST: tl.constexpr,
|
|
173
|
+
FLAT: tl.constexpr, HAS_TW: tl.constexpr, IEEE: tl.constexpr, QK_SCALE: tl.constexpr):
|
|
174
|
+
qk_scale = QK_SCALE
|
|
175
|
+
sq_b, sq_h, sq_l = sq_b16 * 16, sq_h16 * 16, sq_l16 * 16
|
|
176
|
+
sk_b, sk_h, sk_l = sk_b16 * 16, sk_h16 * 16, sk_l16 * 16
|
|
177
|
+
sv_b, sv_h, sv_l = sv_b16 * 16, sv_h16 * 16, sv_l16 * 16
|
|
178
|
+
MQT: tl.constexpr = BLOCK_M // HP
|
|
179
|
+
HG: tl.constexpr = H // HP
|
|
180
|
+
pid_t = tl.program_id(0)
|
|
181
|
+
bh = tl.program_id(1)
|
|
182
|
+
sp = tl.program_id(2)
|
|
183
|
+
b = (bh // HG).to(tl.int64)
|
|
184
|
+
hg = bh % HG
|
|
185
|
+
hk = (hg * HP) // GROUP
|
|
186
|
+
m = tl.arange(0, BLOCK_M)
|
|
187
|
+
h = hg * HP + m // MQT
|
|
188
|
+
i = pid_t * MQT + m % MQT
|
|
189
|
+
qv = i < Mq
|
|
190
|
+
offs_d = tl.arange(0, D)
|
|
191
|
+
offs_n = tl.arange(0, BLOCK_N)
|
|
192
|
+
q = tl.load(Q + b * sq_b + h[:, None].to(tl.int64) * sq_h + i[:, None].to(tl.int64) * sq_l + offs_d[None, :],
|
|
193
|
+
mask=qv[:, None], other=0.0)
|
|
194
|
+
qi = tl.load(QI + b * s_qi + i, mask=qv, other=0) if HAS_QI else i
|
|
195
|
+
hi = tl.minimum(qi + 1, NR)
|
|
196
|
+
if HAS_TW:
|
|
197
|
+
if FLAT:
|
|
198
|
+
hi = tl.maximum(hi, tl.minimum(tl.load(TW + qi, mask=qv, other=0), NR))
|
|
199
|
+
else:
|
|
200
|
+
hi = tl.minimum(tl.maximum(qi + 1, tl.load(TW + b)), NR)
|
|
201
|
+
if HAS_PR:
|
|
202
|
+
hi = tl.minimum(hi, tl.load(PR + qi, mask=qv, other=0) if FLAT else tl.load(PR + b))
|
|
203
|
+
lo = tl.load(ST + qi, mask=qv, other=0) if HAS_ST else tl.zeros_like(qi)
|
|
204
|
+
big = 2147483647
|
|
205
|
+
empty = hi <= lo
|
|
206
|
+
ulo = tl.min(tl.where(qv & ~empty, lo, big), 0)
|
|
207
|
+
uhi = tl.max(tl.where(qv & ~empty, hi, 0), 0)
|
|
208
|
+
has = ulo < uhi
|
|
209
|
+
ulo = tl.where(has, ulo, 0)
|
|
210
|
+
uhi = tl.where(has, uhi, 0)
|
|
211
|
+
total_blks = tl.cdiv(uhi - ulo, BLOCK_N)
|
|
212
|
+
per = tl.cdiv(total_blks, NSPLITS)
|
|
213
|
+
k0 = ulo + sp * per * BLOCK_N
|
|
214
|
+
k1 = tl.minimum(ulo + (sp + 1) * per * BLOCK_N, uhi)
|
|
215
|
+
kb = K + b * sk_b + hk * sk_h
|
|
216
|
+
vb = V + b * sv_b + hk * sv_h
|
|
217
|
+
m_i = tl.full([BLOCK_M], float("-inf"), tl.float32)
|
|
218
|
+
l_i = tl.zeros([BLOCK_M], tl.float32)
|
|
219
|
+
acc = tl.zeros([BLOCK_M, D], tl.float32)
|
|
220
|
+
for s0 in tl.range(k0, k1, BLOCK_N):
|
|
221
|
+
keys = s0 + offs_n
|
|
222
|
+
vis = (keys[None, :] >= lo[:, None]) & (keys[None, :] < hi[:, None]) & (keys[None, :] < k1)
|
|
223
|
+
acc, m_i, l_i = _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, keys.to(tl.int64), keys < k1, vis,
|
|
224
|
+
offs_d, qk_scale, True, D, BLOCK_M, BLOCK_N, IEEE)
|
|
225
|
+
if FLAT:
|
|
226
|
+
if sp == NSPLITS - 1:
|
|
227
|
+
br = qv & (qi >= hi)
|
|
228
|
+
f0 = tl.min(tl.where(br, hi, big), 0)
|
|
229
|
+
f1 = tl.max(tl.where(br, qi + 1, 0), 0)
|
|
230
|
+
for s0 in tl.range(f0, f1, BLOCK_N):
|
|
231
|
+
keys = s0 + offs_n
|
|
232
|
+
kin = keys < f1
|
|
233
|
+
se = tl.load(SE + keys, mask=kin, other=0)
|
|
234
|
+
vis = (keys[None, :] >= hi[:, None]) & (keys[None, :] <= qi[:, None]) & (qi[:, None] < se[None, :]) & kin[None, :]
|
|
235
|
+
acc, m_i, l_i = _step(acc, m_i, l_i, q, kb, vb, sk_l, sv_l, keys.to(tl.int64), kin, vis,
|
|
236
|
+
offs_d, qk_scale, True, D, BLOCK_M, BLOCK_N, IEEE)
|
|
237
|
+
has_l = l_i > 0
|
|
238
|
+
lse = tl.where(has_l, m_i + tl.math.log2(tl.where(has_l, l_i, 1.0)), float("-inf"))
|
|
239
|
+
out = tl.where(has_l[:, None], acc / tl.where(has_l, l_i, 1.0)[:, None], 0.0)
|
|
240
|
+
base_l = ((b * Mq + i.to(tl.int64)) * H + h.to(tl.int64)) * NSPLITS + sp
|
|
241
|
+
tl.store(LP + base_l, lse, mask=qv)
|
|
242
|
+
tl.store(OP + base_l[:, None] * D + offs_d[None, :], out, mask=qv[:, None])
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
@triton.jit
|
|
246
|
+
def readout_splitk_reduce(OP, LP, O, so_b, so_l, so_h, Mq, H: tl.constexpr, D: tl.constexpr, NSPLITS: tl.constexpr):
|
|
247
|
+
idx = tl.program_id(0)
|
|
248
|
+
h = idx % H
|
|
249
|
+
bi = idx // H
|
|
250
|
+
i = bi % Mq
|
|
251
|
+
b = (bi // Mq).to(tl.int64)
|
|
252
|
+
offs_d = tl.arange(0, D)
|
|
253
|
+
offs_s = tl.arange(0, NSPLITS)
|
|
254
|
+
base = ((b * Mq + i) * H + h) * NSPLITS
|
|
255
|
+
lse = tl.load(LP + base + offs_s)
|
|
256
|
+
m_max = tl.max(lse, 0)
|
|
257
|
+
m_use = tl.where(m_max == float("-inf"), 0.0, m_max)
|
|
258
|
+
w = tl.math.exp2(lse - m_use)
|
|
259
|
+
w_sum = tl.sum(w, 0)
|
|
260
|
+
w_norm = tl.where(w_sum > 0, w / tl.where(w_sum > 0, w_sum, 1.0), 0.0)
|
|
261
|
+
acc = tl.zeros([D], tl.float32)
|
|
262
|
+
for sp in tl.static_range(NSPLITS):
|
|
263
|
+
ws = tl.sum(tl.where(offs_s == sp, w_norm, 0.0), 0)
|
|
264
|
+
part = tl.load(OP + (base + sp) * D + offs_d)
|
|
265
|
+
acc += part * ws
|
|
266
|
+
tl.store(O + b * so_b + i * so_l + h * so_h + offs_d, acc.to(O.dtype.element_ty))
|