Update modeling_neollm.py
Browse files- modeling_neollm.py +17 -8
modeling_neollm.py
CHANGED
|
@@ -2355,11 +2355,16 @@ def affine_scaled_flash_attention_forward(
|
|
| 2355 |
v_sum_t = value_sum.transpose(1, 2) # [B,S_q,H_q,d]
|
| 2356 |
output = alpha_t * flash_out + beta_t * v_sum_t
|
| 2357 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2358 |
value_sum_kv = _affine_valid_value_sum(
|
| 2359 |
value,
|
| 2360 |
attention_mask=attention_mask,
|
| 2361 |
query_len=query.shape[-2],
|
| 2362 |
-
sliding_window=
|
| 2363 |
) # [B,H_kv,S_q,d]
|
| 2364 |
v_sum_t = value_sum_kv.transpose(1, 2) # [B,S_q,H_kv,d]
|
| 2365 |
H_q = flash_out.shape[2]
|
|
@@ -3921,11 +3926,13 @@ class NeoLLMAttention(nn.Module):
|
|
| 3921 |
) # [B, S·P, H_q, d]
|
| 3922 |
|
| 3923 |
# ── Affine-Scaled Attention inline (flash path) ───────────────────────
|
| 3924 |
-
# Decomposition
|
| 3925 |
# Term 1: α · flash_out (standard flash output)
|
| 3926 |
-
# Term 2: β · Σ_{j
|
| 3927 |
-
#
|
| 3928 |
-
#
|
|
|
|
|
|
|
| 3929 |
# α, β arrive pre-expanded to [B, H_q, S·P, 1]; permute to [B,S·P,H,1]
|
| 3930 |
# to match the flash output layout [B, S·P, H_q, d].
|
| 3931 |
if use_affine:
|
|
@@ -3934,18 +3941,20 @@ class NeoLLMAttention(nn.Module):
|
|
| 3934 |
# repeated query-head view:
|
| 3935 |
# Sum(repeat_kv(V)) == repeat_kv(Sum(V)).
|
| 3936 |
# The broadcast to H_q happens only in the final affine combine.
|
| 3937 |
-
|
|
|
|
|
|
|
| 3938 |
if is_packed:
|
| 3939 |
value_sum_kv = _segmented_causal_value_sum(
|
| 3940 |
v,
|
| 3941 |
segment_lengths=doc_lengths_p,
|
| 3942 |
-
window_left=
|
| 3943 |
) # [1,H_kv,S·P,d]
|
| 3944 |
else:
|
| 3945 |
value_sum_kv = _causal_value_sum(
|
| 3946 |
v,
|
| 3947 |
query_len=SP,
|
| 3948 |
-
window_left=
|
| 3949 |
) # [B,H_kv,S·P,d]
|
| 3950 |
|
| 3951 |
alpha_t = alpha.permute(0, 2, 1, 3) # [B,S·P,H_q,1]
|
|
|
|
| 2355 |
v_sum_t = value_sum.transpose(1, 2) # [B,S_q,H_q,d]
|
| 2356 |
output = alpha_t * flash_out + beta_t * v_sum_t
|
| 2357 |
else:
|
| 2358 |
+
# Keep the Affine β branch as a causal prefix summary even when the
|
| 2359 |
+
# attention backend itself uses a local sliding window. This restores
|
| 2360 |
+
# the original IHA/local behavior: local logits plus a cheap global
|
| 2361 |
+
# causal value context. Padding masks are still handled inside
|
| 2362 |
+
# _affine_valid_value_sum; explicit 4D masks above remain exact.
|
| 2363 |
value_sum_kv = _affine_valid_value_sum(
|
| 2364 |
value,
|
| 2365 |
attention_mask=attention_mask,
|
| 2366 |
query_len=query.shape[-2],
|
| 2367 |
+
sliding_window=None,
|
| 2368 |
) # [B,H_kv,S_q,d]
|
| 2369 |
v_sum_t = value_sum_kv.transpose(1, 2) # [B,S_q,H_kv,d]
|
| 2370 |
H_q = flash_out.shape[2]
|
|
|
|
| 3926 |
) # [B, S·P, H_q, d]
|
| 3927 |
|
| 3928 |
# ── Affine-Scaled Attention inline (flash path) ───────────────────────
|
| 3929 |
+
# Decomposition used here:
|
| 3930 |
# Term 1: α · flash_out (standard flash output)
|
| 3931 |
+
# Term 2: β · Σ_{j≤i} V_j (causal prefix value summary)
|
| 3932 |
+
# This intentionally restores the original local-IHA behavior: the
|
| 3933 |
+
# attention logits may use a sliding window, but the β branch supplies a
|
| 3934 |
+
# cheap global causal value context. For packed IHA, the prefix is
|
| 3935 |
+
# reset at each document boundary to avoid cross-document leakage.
|
| 3936 |
# α, β arrive pre-expanded to [B, H_q, S·P, 1]; permute to [B,S·P,H,1]
|
| 3937 |
# to match the flash output layout [B, S·P, H_q, d].
|
| 3938 |
if use_affine:
|
|
|
|
| 3941 |
# repeated query-head view:
|
| 3942 |
# Sum(repeat_kv(V)) == repeat_kv(Sum(V)).
|
| 3943 |
# The broadcast to H_q happens only in the final affine combine.
|
| 3944 |
+
# β uses the full causal prefix, not the local flash window.
|
| 3945 |
+
# Keep the sum in KV-head space first; GQA expansion happens only
|
| 3946 |
+
# in the final affine combine below.
|
| 3947 |
if is_packed:
|
| 3948 |
value_sum_kv = _segmented_causal_value_sum(
|
| 3949 |
v,
|
| 3950 |
segment_lengths=doc_lengths_p,
|
| 3951 |
+
window_left=None,
|
| 3952 |
) # [1,H_kv,S·P,d]
|
| 3953 |
else:
|
| 3954 |
value_sum_kv = _causal_value_sum(
|
| 3955 |
v,
|
| 3956 |
query_len=SP,
|
| 3957 |
+
window_left=None,
|
| 3958 |
) # [B,H_kv,S·P,d]
|
| 3959 |
|
| 3960 |
alpha_t = alpha.permute(0, 2, 1, 3) # [B,S·P,H_q,1]
|