Skip to content

Commit 1645597

Browse files
committed
Porting Pallas pl.load and pl.load to modern indexing for Jax 0.8.3.dev.
1 parent da86ee3 commit 1645597

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

axlearn/common/flash_attention/gpu_decoding.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _compute(block_kv_start_idx, block_kv_seqlen, o, m_i, l_i):
9595
# Load q: it will stay in L1 throughout. Indices form a matrix because we
9696
# read, compute, and write all in 2d chunks. 1 element ~= 1 CUDA thread index.
9797
# q tile has shape [block_h, head_dim].
98-
q = pl.load(q_ref, (slice(None), slice(None)), mask=q_mask) * softmax_scale
98+
q = jnp.where(q_mask, q_ref[...], 0.0) * softmax_scale
9999

100100
mask_indices = jnp.arange(block_k)
101101

@@ -110,13 +110,11 @@ def body(start_k, carry):
110110

111111
def compute():
112112
curr_k_slice = pl.ds(start_k * block_k, block_k)
113-
k = pl.load(k_ref, (curr_k_slice, slice(None)), mask=mask[:, None], other=0.0)
113+
k = jnp.where(mask[:, None], k_ref[curr_k_slice, :], 0.0)
114114
k = k.astype(q.dtype)
115115
qk = pl.dot(q, k.T, precision=precision) # [block_h, block_k]
116116
if bias_ref is not None:
117-
qk += pl.load(
118-
bias_ref, (slice(None), curr_k_slice), mask=mask[None, :], other=0.0
119-
)
117+
qk += jnp.where(mask[None, :], bias_ref[:, curr_k_slice], 0.0)
120118

121119
qk = jnp.where(logits_mask[None, :], qk, NEG_INF)
122120

@@ -128,7 +126,7 @@ def compute():
128126
s_curr = jnp.exp(qk - m_next[:, None])
129127
l_curr = s_curr.sum(axis=-1)
130128
l_next = l_prev_corr + l_curr
131-
v = pl.load(v_ref, (curr_k_slice, slice(None)), mask=mask[:, None], other=0.0)
129+
v = jnp.where(mask[:, None], v_ref[curr_k_slice, :], 0.0)
132130
v = v.astype(q.dtype)
133131
o_curr = pl.dot(s_curr.astype(v.dtype), v, precision=precision)
134132

@@ -155,7 +153,8 @@ def no_compute():
155153
o = jnp.zeros((block_h, head_dim), dtype=jnp.float32)
156154

157155
block_kv_start_idx = prog_j * split_k_seq_len
158-
kv_seq_len = pl.load(kv_seq_len_ref, ())
156+
kv_seq_len = kv_seq_len_ref[...]
157+
159158
block_kv_seqlen = jnp.minimum((prog_j + 1) * split_k_seq_len, kv_seq_len)
160159

161160
# Skip padding in seq dim.
@@ -167,9 +166,9 @@ def no_compute():
167166

168167
# Write output to HBM.
169168
vec_q_mask = q_mask.reshape(-1)
170-
pl.store(l_ref, slice(None), l_i, mask=vec_q_mask)
171-
pl.store(m_ref, slice(None), m_i, mask=vec_q_mask)
172-
pl.store(o_ref, (slice(None), slice(None)), o, mask=q_mask)
169+
l_ref[...] = jnp.where(vec_q_mask, l_i, l_ref[...])
170+
m_ref[...] = jnp.where(vec_q_mask, m_i, m_ref[...])
171+
o_ref[...] = jnp.where(q_mask, o, o_ref[...])
173172

174173

175174
def _get_sm_count() -> int:

axlearn/common/flash_attention/gpu_paged_attention.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ def _compute(
8282
Tuple of (output, max_logits, sum_exponentials).
8383
"""
8484
q_slice = pl.ds(0, block_h)
85-
q = pl.load(q_ref, (q_slice, slice(None))) * softmax_scale
85+
q = q_ref[q_slice,:] * softmax_scale
8686

8787
def body(start_k, carry):
8888
o_prev, m_prev, l_prev = carry
8989

9090
page_tables_slice = pl.ds(start_k * pages_per_compute_block, pages_per_compute_block)
91-
page_tables = pl.load(page_tables_ref, page_tables_slice)
91+
page_tables = page_tables_ref[page_tables_slice]
9292
k = key_ref[page_tables].reshape(block_k, head_dim)
9393
v = value_ref[page_tables].reshape(block_k, head_dim)
9494
logits = pl.dot(q, k.T, precision=precision) # [block_h, block_k]
@@ -100,7 +100,7 @@ def body(start_k, carry):
100100

101101
if bias_ref is not None:
102102
bias_slice = pl.ds(start_k * block_k, block_k)
103-
bias = pl.load(bias_ref, (slice(None), bias_slice))
103+
bias = bias_ref[:,bias_slice]
104104
logits += bias
105105

106106
if mask_fn is not None:
@@ -130,7 +130,7 @@ def body(start_k, carry):
130130
m_i = jnp.zeros(block_h, dtype=jnp.float32) + jnp.finfo(jnp.float32).min
131131
l_i = jnp.zeros(block_h, dtype=jnp.float32)
132132
o = jnp.zeros((block_h, head_dim), dtype=jnp.float32)
133-
length = pl.load(lengths_ref, ())
133+
length = lengths_ref[...]
134134

135135
start_page_idx = partition_idx * pages_per_partition
136136
end_page_idx = start_page_idx + pages_per_partition

0 commit comments

Comments
 (0)