Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +2 -0
- fla/layers/__pycache__/forgetting_attn.cpython-312.pyc +0 -0
- fla/layers/attn.py +203 -0
- fla/layers/gsa.py +227 -0
- fla/layers/multiscale_retention.py +298 -0
- fla/models/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/abc/__init__.py +13 -0
- fla/models/bitnet/__init__.py +13 -0
- fla/models/delta_net/__init__.py +12 -0
- fla/models/forgetting_transformer/__init__.py +16 -0
- fla/models/forgetting_transformer/configuration_forgetting_transformer.py +68 -0
- fla/models/gated_deltanet/configuration_gated_deltanet.py +83 -0
- fla/models/gated_deltaproduct/modeling_gated_deltaproduct.py +520 -0
- fla/models/gla/configuration_gla.py +95 -0
- fla/models/gla/modeling_gla.py +417 -0
- fla/models/linear_attn/__init__.py +12 -0
- fla/models/linear_attn/configuration_linear_attn.py +91 -0
- fla/models/linear_attn/modeling_linear_attn.py +406 -0
- fla/models/mamba2/modeling_mamba2.py +1093 -0
- fla/models/nsa/configuration_nsa.py +75 -0
- fla/models/retnet/__init__.py +13 -0
- fla/models/rwkv6/__init__.py +13 -0
- fla/models/rwkv7/modeling_rwkv7.py +505 -0
- fla/models/samba/configuration_samba.py +92 -0
- fla/models/samba/modeling_samba.py +413 -0
- fla/models/transformer/__init__.py +13 -0
- fla/models/transformer/modeling_transformer.py +406 -0
- fla/models/transformer_top/__init__.py +13 -0
- fla/models/transformer_top/configuration_transformer.py +76 -0
- fla/models/transformer_top/modeling_transformer.py +438 -0
- fla/models/utils.py +147 -0
- fla/modules/convolution.py +434 -0
- fla/modules/feature_map.py +300 -0
- fla/modules/fused_kl_div.py +323 -0
- fla/modules/l2norm.py +176 -0
- fla/ops/based/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/ops/based/__pycache__/parallel.cpython-312.pyc +0 -0
- fla/ops/common/__init__.py +1 -0
- fla/ops/common/__pycache__/chunk_delta_h.cpython-312.pyc +0 -0
- fla/ops/common/__pycache__/chunk_scaled_dot_kkt.cpython-312.pyc +0 -0
- fla/ops/common/__pycache__/utils.cpython-312.pyc +0 -0
- fla/ops/common/chunk_delta_h.py +399 -0
- fla/ops/common/chunk_h.py +422 -0
- fla/ops/common/chunk_h_split.py +677 -0
- fla/ops/delta_rule/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/ops/delta_rule/__pycache__/chunk.cpython-312.pyc +0 -0
- fla/ops/delta_rule/__pycache__/wy_fast.cpython-312.pyc +0 -0
- fla/ops/delta_rule/fused_recurrent.py +607 -0
- fla/ops/forgetting_attn/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/ops/forgetting_attn/__pycache__/parallel.cpython-312.pyc +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
logs/none_kbb7wwge/attempt_0/0/stderr.log filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
tb/20250726-1848/wandb/run-20250726_184802-mtp_transformer-mtp.7B.batch8.seqlen4096.context4096.warmup2000.update2.steps200000.lr1.2e-4.cosine-202507261839/run-mtp_transformer-mtp.7B.batch8.seqlen4096.context4096.warmup2000.update2.steps200000.lr1.2e-4.cosine-202507261839.wandb filter=lfs diff=lfs merge=lfs -text
|
fla/layers/__pycache__/forgetting_attn.cpython-312.pyc
ADDED
|
Binary file (5.3 kB). View file
|
|
|
fla/layers/attn.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import TYPE_CHECKING, Optional, Tuple
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.nn.functional as F
|
| 12 |
+
import torch.utils.checkpoint
|
| 13 |
+
from einops import rearrange
|
| 14 |
+
from transformers.utils import logging
|
| 15 |
+
|
| 16 |
+
from fla.modules import RMSNorm, RotaryEmbedding
|
| 17 |
+
|
| 18 |
+
if TYPE_CHECKING:
|
| 19 |
+
from fla.models.utils import Cache
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
from flash_attn import flash_attn_func, flash_attn_varlen_func
|
| 23 |
+
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
|
| 24 |
+
except ImportError:
|
| 25 |
+
warnings.warn(
|
| 26 |
+
"Flash Attention is not installed. Please install it via `pip install flash-attn --no-build-isolation`",
|
| 27 |
+
category=ImportWarning
|
| 28 |
+
)
|
| 29 |
+
flash_attn_func = None
|
| 30 |
+
|
| 31 |
+
logger = logging.get_logger(__name__)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class Attention(nn.Module):
|
| 35 |
+
|
| 36 |
+
def __init__(
|
| 37 |
+
self,
|
| 38 |
+
hidden_size: int = 2048,
|
| 39 |
+
num_heads: int = 32,
|
| 40 |
+
num_kv_heads: Optional[int] = None,
|
| 41 |
+
qkv_bias: bool = False,
|
| 42 |
+
qk_norm: bool = False,
|
| 43 |
+
window_size: Optional[int] = None,
|
| 44 |
+
rope_theta: Optional[float] = 10000.,
|
| 45 |
+
max_position_embeddings: Optional[int] = None,
|
| 46 |
+
layer_idx: int = None
|
| 47 |
+
):
|
| 48 |
+
super().__init__()
|
| 49 |
+
|
| 50 |
+
self.hidden_size = hidden_size
|
| 51 |
+
self.num_heads = num_heads
|
| 52 |
+
if num_kv_heads is None:
|
| 53 |
+
self.num_kv_heads = self.num_heads
|
| 54 |
+
else:
|
| 55 |
+
self.num_kv_heads = num_kv_heads
|
| 56 |
+
self.num_kv_groups = num_heads // self.num_kv_heads
|
| 57 |
+
self.head_dim = self.hidden_size // self.num_heads
|
| 58 |
+
self.kv_dim = self.num_kv_heads * self.head_dim
|
| 59 |
+
self.qkv_bias = qkv_bias
|
| 60 |
+
self.qk_norm = qk_norm
|
| 61 |
+
|
| 62 |
+
self.window_size = window_size
|
| 63 |
+
self.rope_theta = rope_theta
|
| 64 |
+
self.max_position_embeddings = max_position_embeddings
|
| 65 |
+
self.layer_idx = layer_idx
|
| 66 |
+
|
| 67 |
+
self.q_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=self.qkv_bias)
|
| 68 |
+
self.k_proj = nn.Linear(self.hidden_size, self.kv_dim, bias=self.qkv_bias)
|
| 69 |
+
self.v_proj = nn.Linear(self.hidden_size, self.kv_dim, bias=self.qkv_bias)
|
| 70 |
+
self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
|
| 71 |
+
|
| 72 |
+
if qk_norm:
|
| 73 |
+
self.q_norm = RMSNorm(self.head_dim)
|
| 74 |
+
self.k_norm = RMSNorm(self.head_dim)
|
| 75 |
+
|
| 76 |
+
self.rotary = RotaryEmbedding(dim=self.head_dim, base=self.rope_theta)
|
| 77 |
+
|
| 78 |
+
def forward(
|
| 79 |
+
self,
|
| 80 |
+
hidden_states: torch.Tensor,
|
| 81 |
+
attention_mask: Optional[torch.LongTensor] = None,
|
| 82 |
+
past_key_values: Optional[Cache] = None,
|
| 83 |
+
output_attentions: bool = False,
|
| 84 |
+
use_cache: bool = False,
|
| 85 |
+
**kwargs,
|
| 86 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
| 87 |
+
if attention_mask is not None:
|
| 88 |
+
assert len(attention_mask.shape) == 2, (
|
| 89 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 90 |
+
"for padding purposes (0 indicating padding). "
|
| 91 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
batch_size, q_len, _ = hidden_states.size()
|
| 95 |
+
|
| 96 |
+
q, k, v = self.q_proj(hidden_states), self.k_proj(hidden_states), self.v_proj(hidden_states)
|
| 97 |
+
|
| 98 |
+
q = rearrange(q, '... (h d) -> ... h d', d=self.head_dim)
|
| 99 |
+
k = rearrange(k, '... (h d) -> ... h d', d=self.head_dim)
|
| 100 |
+
v = rearrange(v, '... (h d) -> ... h d', d=self.head_dim)
|
| 101 |
+
|
| 102 |
+
if self.qk_norm:
|
| 103 |
+
q, k = self.q_norm(q), self.k_norm(k)
|
| 104 |
+
|
| 105 |
+
# equivalent to cu_seqlens in `flash_attn`
|
| 106 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 107 |
+
|
| 108 |
+
seqlen_offset, max_seqlen = 0, q_len
|
| 109 |
+
if past_key_values is not None:
|
| 110 |
+
seqlen_offset = past_key_values.get_seq_length(self.layer_idx)
|
| 111 |
+
max_seqlen = q.shape[1] + seqlen_offset
|
| 112 |
+
|
| 113 |
+
if attention_mask is not None:
|
| 114 |
+
# to deliminate the offsets of padding tokens
|
| 115 |
+
seqlen_offset = seqlen_offset + attention_mask.sum(-1) - attention_mask.shape[-1]
|
| 116 |
+
max_seqlen = q.shape[1] + max(seqlen_offset)
|
| 117 |
+
|
| 118 |
+
if self.max_position_embeddings is not None:
|
| 119 |
+
max_seqlen = max(max_seqlen, self.max_position_embeddings)
|
| 120 |
+
q, k = self.rotary(q, k, seqlen_offset=seqlen_offset, max_seqlen=max_seqlen, cu_seqlens=cu_seqlens)
|
| 121 |
+
|
| 122 |
+
if past_key_values is not None:
|
| 123 |
+
cache_has_content = past_key_values.get_seq_length(self.layer_idx) > 0
|
| 124 |
+
k_cached, v_cached = past_key_values.update(
|
| 125 |
+
attn_state=(k.flatten(-2, -1), v.flatten(-2, -1)),
|
| 126 |
+
layer_idx=self.layer_idx,
|
| 127 |
+
offset=q_len,
|
| 128 |
+
cache_kwargs=dict(window_size=self.window_size)
|
| 129 |
+
)['attn_state']
|
| 130 |
+
if cache_has_content:
|
| 131 |
+
k, v = k_cached, v_cached
|
| 132 |
+
k = rearrange(k, '... (h d) -> ... h d', d=self.head_dim)
|
| 133 |
+
v = rearrange(v, '... (h d) -> ... h d', d=self.head_dim)
|
| 134 |
+
|
| 135 |
+
if flash_attn_func is None:
|
| 136 |
+
raise ImportError("Please install Flash Attention via `pip install flash-attn --no-build-isolation` first")
|
| 137 |
+
|
| 138 |
+
# Contains at least one padding token in the sequence
|
| 139 |
+
if attention_mask is not None:
|
| 140 |
+
q, k, v, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(q, k, v, attention_mask, q_len)
|
| 141 |
+
cu_seqlens_q, cu_seqlens_k = cu_seq_lens
|
| 142 |
+
max_seqlen_q, max_seqlen_k = max_seq_lens
|
| 143 |
+
o = flash_attn_varlen_func(
|
| 144 |
+
q, k, v,
|
| 145 |
+
cu_seqlens_q=cu_seqlens_q,
|
| 146 |
+
cu_seqlens_k=cu_seqlens_k,
|
| 147 |
+
max_seqlen_q=max_seqlen_q,
|
| 148 |
+
max_seqlen_k=max_seqlen_k,
|
| 149 |
+
causal=True,
|
| 150 |
+
window_size=(-1, -1) if self.window_size is None else (self.window_size-1, 0)
|
| 151 |
+
)
|
| 152 |
+
o = pad_input(o, indices_q, batch_size, q_len)
|
| 153 |
+
elif cu_seqlens is not None:
|
| 154 |
+
o = flash_attn_varlen_func(
|
| 155 |
+
q.squeeze(0), k.squeeze(0), v.squeeze(0),
|
| 156 |
+
cu_seqlens_q=cu_seqlens,
|
| 157 |
+
cu_seqlens_k=cu_seqlens,
|
| 158 |
+
max_seqlen_q=max_seqlen,
|
| 159 |
+
max_seqlen_k=max_seqlen,
|
| 160 |
+
causal=True,
|
| 161 |
+
window_size=(-1, -1) if self.window_size is None else (self.window_size-1, 0)
|
| 162 |
+
).unsqueeze(0)
|
| 163 |
+
else:
|
| 164 |
+
o = flash_attn_func(
|
| 165 |
+
q, k, v,
|
| 166 |
+
causal=True,
|
| 167 |
+
window_size=(-1, -1) if self.window_size is None else (self.window_size-1, 0)
|
| 168 |
+
)
|
| 169 |
+
o = o.reshape(batch_size, q_len, -1)
|
| 170 |
+
o = self.o_proj(o)
|
| 171 |
+
|
| 172 |
+
if not output_attentions:
|
| 173 |
+
attentions = None
|
| 174 |
+
|
| 175 |
+
return o, attentions, past_key_values
|
| 176 |
+
|
| 177 |
+
def _upad_input(self, q, k, v, attention_mask, q_len):
|
| 178 |
+
batch_size, seq_len, num_key_value_heads, head_dim = k.shape
|
| 179 |
+
cache_mask = attention_mask[:, -seq_len:]
|
| 180 |
+
seqlens = cache_mask.sum(-1, dtype=torch.int32)
|
| 181 |
+
indices_k = torch.nonzero(cache_mask.flatten(), as_tuple=False).flatten()
|
| 182 |
+
max_seqlen_k = seqlens.max().item()
|
| 183 |
+
cu_seqlens_k = F.pad(torch.cumsum(seqlens, dim=0, dtype=torch.int32), (1, 0))
|
| 184 |
+
|
| 185 |
+
k = index_first_axis(k.reshape(batch_size * seq_len, num_key_value_heads, head_dim), indices_k)
|
| 186 |
+
v = index_first_axis(v.reshape(batch_size * seq_len, num_key_value_heads, head_dim), indices_k)
|
| 187 |
+
if q_len == seq_len:
|
| 188 |
+
q = index_first_axis(q.reshape(batch_size * seq_len, self.num_heads, head_dim), indices_k)
|
| 189 |
+
cu_seqlens_q = cu_seqlens_k
|
| 190 |
+
max_seqlen_q = max_seqlen_k
|
| 191 |
+
indices_q = indices_k
|
| 192 |
+
elif q_len == 1:
|
| 193 |
+
max_seqlen_q = 1
|
| 194 |
+
# There is a memcpy here, that is very bad.
|
| 195 |
+
cu_seqlens_q = torch.arange(batch_size + 1, dtype=torch.int32, device=q.device)
|
| 196 |
+
indices_q = cu_seqlens_q[:-1]
|
| 197 |
+
q = q.squeeze(1)
|
| 198 |
+
else:
|
| 199 |
+
# The -q_len: slice assumes left padding.
|
| 200 |
+
attention_mask = attention_mask[:, -q_len:]
|
| 201 |
+
q, indices_q, cu_seqlens_q, max_seqlen_q = unpad_input(q, attention_mask)
|
| 202 |
+
|
| 203 |
+
return q, k, v, indices_q, (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k)
|
fla/layers/gsa.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import TYPE_CHECKING, Dict, Optional, Tuple
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.nn.functional as F
|
| 12 |
+
from einops import rearrange
|
| 13 |
+
|
| 14 |
+
from fla.modules import RMSNorm, ShortConvolution
|
| 15 |
+
from fla.modules.feature_map import ReLUFeatureMap, SwishFeatureMap, T2RFeatureMap
|
| 16 |
+
from fla.modules.layernorm import rms_norm_linear
|
| 17 |
+
from fla.ops.gsa import chunk_gsa, fused_recurrent_gsa
|
| 18 |
+
|
| 19 |
+
if TYPE_CHECKING:
|
| 20 |
+
from transformers.processing_utils import Unpack
|
| 21 |
+
|
| 22 |
+
from fla.models.utils import Cache
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class GatedSlotAttention(nn.Module):
|
| 26 |
+
|
| 27 |
+
def __init__(
|
| 28 |
+
self,
|
| 29 |
+
mode: str = 'chunk',
|
| 30 |
+
hidden_size: int = 1024,
|
| 31 |
+
expand_k: float = 1.,
|
| 32 |
+
expand_v: float = 1.,
|
| 33 |
+
num_heads: int = 4,
|
| 34 |
+
num_kv_heads: Optional[int] = None,
|
| 35 |
+
use_short_conv: bool = False,
|
| 36 |
+
conv_size: int = 4,
|
| 37 |
+
conv_bias: bool = False,
|
| 38 |
+
num_slots: Optional[int] = None,
|
| 39 |
+
elementwise_affine: Optional[bool] = True,
|
| 40 |
+
norm_eps: float = 1e-5,
|
| 41 |
+
gate_logit_normalizer: int = 8,
|
| 42 |
+
feature_map: str = 'swish',
|
| 43 |
+
use_output_gate: bool = False,
|
| 44 |
+
use_norm: bool = True,
|
| 45 |
+
layer_idx: Optional[int] = None,
|
| 46 |
+
scale: Optional[float] = 1.,
|
| 47 |
+
**kwargs
|
| 48 |
+
) -> GatedSlotAttention:
|
| 49 |
+
super().__init__()
|
| 50 |
+
|
| 51 |
+
self.mode = mode
|
| 52 |
+
self.hidden_size = hidden_size
|
| 53 |
+
self.expand_k = expand_k
|
| 54 |
+
self.expand_v = expand_v
|
| 55 |
+
self.num_heads = num_heads
|
| 56 |
+
self.num_kv_heads = num_heads if num_kv_heads is None else num_kv_heads
|
| 57 |
+
self.num_kv_groups = self.num_heads // self.num_kv_heads
|
| 58 |
+
self.key_dim = int(hidden_size * expand_k)
|
| 59 |
+
self.value_dim = int(hidden_size * expand_v)
|
| 60 |
+
self.key_dim_per_group = self.key_dim // self.num_kv_groups
|
| 61 |
+
self.value_dim_per_group = self.value_dim // self.num_kv_groups
|
| 62 |
+
self.head_k_dim = self.key_dim // self.num_heads
|
| 63 |
+
self.head_v_dim = self.value_dim // self.num_heads
|
| 64 |
+
|
| 65 |
+
self.use_short_conv = use_short_conv
|
| 66 |
+
self.conv_size = conv_size
|
| 67 |
+
self.conv_bias = conv_bias
|
| 68 |
+
|
| 69 |
+
self.gate_logit_normalizer = gate_logit_normalizer
|
| 70 |
+
|
| 71 |
+
self.use_output_gate = use_output_gate
|
| 72 |
+
self.use_norm = use_norm
|
| 73 |
+
self.scale = scale
|
| 74 |
+
|
| 75 |
+
if num_slots is None:
|
| 76 |
+
num_slots = self.head_k_dim
|
| 77 |
+
self.num_slots = num_slots
|
| 78 |
+
|
| 79 |
+
self.layer_idx = layer_idx
|
| 80 |
+
|
| 81 |
+
if layer_idx is None:
|
| 82 |
+
warnings.warn(
|
| 83 |
+
f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
|
| 84 |
+
"to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
|
| 85 |
+
"when creating this class."
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
self.register_module('feature_map', None)
|
| 89 |
+
if feature_map == 'swish':
|
| 90 |
+
self.feature_map = SwishFeatureMap()
|
| 91 |
+
elif feature_map == 'relu':
|
| 92 |
+
self.feature_map = ReLUFeatureMap()
|
| 93 |
+
elif feature_map == 't2r':
|
| 94 |
+
self.feature_map = T2RFeatureMap(self.head_k_dim, self.head_k_dim)
|
| 95 |
+
else:
|
| 96 |
+
raise NotImplementedError(f"Feature map `{feature_map}` is not supported now.")
|
| 97 |
+
|
| 98 |
+
self.q_proj = nn.Linear(self.hidden_size, self.key_dim, bias=False)
|
| 99 |
+
self.k_proj = nn.Linear(self.hidden_size, self.key_dim_per_group, bias=False)
|
| 100 |
+
self.v_proj = nn.Linear(self.hidden_size, self.value_dim_per_group, bias=False)
|
| 101 |
+
self.f_proj = nn.Linear(self.hidden_size, self.num_kv_heads * self.num_slots, bias=False)
|
| 102 |
+
|
| 103 |
+
if use_short_conv:
|
| 104 |
+
self.conv_size = conv_size
|
| 105 |
+
self.q_conv1d = ShortConvolution(self.key_dim, conv_size, activation='silu')
|
| 106 |
+
self.k_conv1d = ShortConvolution(self.key_dim_per_group, conv_size, activation='silu')
|
| 107 |
+
self.v_conv1d = ShortConvolution(self.value_dim_per_group, conv_size, activation='silu')
|
| 108 |
+
|
| 109 |
+
self.g_norm = RMSNorm(self.hidden_size, elementwise_affine, eps=norm_eps)
|
| 110 |
+
self.o_proj = nn.Linear(self.value_dim, self.hidden_size, bias=False)
|
| 111 |
+
|
| 112 |
+
def forward(
|
| 113 |
+
self,
|
| 114 |
+
hidden_states: torch.Tensor,
|
| 115 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 116 |
+
past_key_values: Optional[Cache] = None,
|
| 117 |
+
use_cache: Optional[bool] = False,
|
| 118 |
+
output_attentions: Optional[bool] = False,
|
| 119 |
+
**kwargs: Unpack[Dict]
|
| 120 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]:
|
| 121 |
+
if attention_mask is not None:
|
| 122 |
+
assert len(attention_mask.shape) == 2, (
|
| 123 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 124 |
+
"for padding purposes (0 indicating padding). "
|
| 125 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# launching the triton kernel for just one token will actually be slower
|
| 129 |
+
mode = 'fused_recurrent' if hidden_states.shape[1] <= 64 else self.mode
|
| 130 |
+
|
| 131 |
+
last_state = None
|
| 132 |
+
if past_key_values is not None and len(past_key_values) > self.layer_idx:
|
| 133 |
+
last_state = past_key_values[self.layer_idx]
|
| 134 |
+
|
| 135 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 136 |
+
if self.use_short_conv:
|
| 137 |
+
conv_state_q, conv_state_k, conv_state_v = None, None, None
|
| 138 |
+
if last_state is not None:
|
| 139 |
+
conv_state_q, conv_state_k, conv_state_v = last_state['conv_state']
|
| 140 |
+
conv_mask = attention_mask[:, -hidden_states.shape[1]:] if attention_mask is not None else None
|
| 141 |
+
q, conv_state_q = self.q_conv1d(
|
| 142 |
+
x=self.q_proj(hidden_states),
|
| 143 |
+
mask=conv_mask,
|
| 144 |
+
cache=conv_state_q,
|
| 145 |
+
output_final_state=use_cache,
|
| 146 |
+
cu_seqlens=cu_seqlens
|
| 147 |
+
)
|
| 148 |
+
k, conv_state_k = self.k_conv1d(
|
| 149 |
+
x=self.k_proj(hidden_states),
|
| 150 |
+
mask=conv_mask,
|
| 151 |
+
cache=conv_state_k,
|
| 152 |
+
output_final_state=use_cache,
|
| 153 |
+
cu_seqlens=cu_seqlens
|
| 154 |
+
)
|
| 155 |
+
v, conv_state_v = self.v_conv1d(
|
| 156 |
+
x=self.v_proj(hidden_states),
|
| 157 |
+
mask=conv_mask,
|
| 158 |
+
cache=conv_state_v,
|
| 159 |
+
output_final_state=use_cache,
|
| 160 |
+
cu_seqlens=cu_seqlens
|
| 161 |
+
)
|
| 162 |
+
else:
|
| 163 |
+
q = self.q_proj(hidden_states)
|
| 164 |
+
k = self.k_proj(hidden_states)
|
| 165 |
+
v = self.v_proj(hidden_states)
|
| 166 |
+
f = self.f_proj(hidden_states)
|
| 167 |
+
|
| 168 |
+
q = rearrange(q, 'b t (h d) -> b t h d', d=self.head_k_dim)
|
| 169 |
+
k = rearrange(k, 'b t (h d) -> b t h d', d=self.head_k_dim)
|
| 170 |
+
v = rearrange(v, 'b t (h d) -> b t h d', d=self.head_v_dim)
|
| 171 |
+
f = rearrange(f, 'b t (h m) -> b t h m', m=self.num_slots)
|
| 172 |
+
|
| 173 |
+
if self.feature_map is not None:
|
| 174 |
+
q, k = map(lambda x: self.feature_map(x), (q, k))
|
| 175 |
+
v = F.silu(v)
|
| 176 |
+
|
| 177 |
+
f = F.logsigmoid(f) / self.gate_logit_normalizer
|
| 178 |
+
s = (1 - f.exp()).to(f.dtype)
|
| 179 |
+
# dealing with left-padding
|
| 180 |
+
if attention_mask is not None:
|
| 181 |
+
s = s.mul_(attention_mask[:, -s.shape[1]:, None, None])
|
| 182 |
+
v = v.mul_(attention_mask[:, -v.shape[1]:, None, None])
|
| 183 |
+
|
| 184 |
+
recurrent_state = last_state['recurrent_state'] if last_state is not None else None
|
| 185 |
+
if mode == 'fused_recurrent':
|
| 186 |
+
o, recurrent_state = fused_recurrent_gsa(
|
| 187 |
+
q=q,
|
| 188 |
+
k=k,
|
| 189 |
+
v=v,
|
| 190 |
+
s=s,
|
| 191 |
+
g=f,
|
| 192 |
+
initial_state=recurrent_state,
|
| 193 |
+
output_final_state=use_cache,
|
| 194 |
+
scale=self.scale,
|
| 195 |
+
cu_seqlens=cu_seqlens,
|
| 196 |
+
head_first=False
|
| 197 |
+
)
|
| 198 |
+
elif mode == 'chunk':
|
| 199 |
+
o, recurrent_state = chunk_gsa(
|
| 200 |
+
q=q,
|
| 201 |
+
k=k,
|
| 202 |
+
v=v,
|
| 203 |
+
s=s,
|
| 204 |
+
g=f,
|
| 205 |
+
initial_state=recurrent_state,
|
| 206 |
+
output_final_state=use_cache,
|
| 207 |
+
scale=self.scale,
|
| 208 |
+
cu_seqlens=cu_seqlens,
|
| 209 |
+
head_first=False
|
| 210 |
+
)
|
| 211 |
+
else:
|
| 212 |
+
raise NotImplementedError(f"Not supported mode `{mode}`.")
|
| 213 |
+
|
| 214 |
+
if past_key_values is not None:
|
| 215 |
+
past_key_values.update(
|
| 216 |
+
recurrent_state=recurrent_state,
|
| 217 |
+
conv_state=(conv_state_q, conv_state_k, conv_state_v) if self.use_short_conv else None,
|
| 218 |
+
layer_idx=self.layer_idx,
|
| 219 |
+
offset=q.shape[1]
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
o = rearrange(o, 'b t h d -> b t (h d)')
|
| 223 |
+
o = rms_norm_linear(F.silu(o), self.g_norm.weight, self.g_norm.bias, self.o_proj.weight, self.o_proj.bias)
|
| 224 |
+
return o, None, past_key_values
|
| 225 |
+
|
| 226 |
+
def state_size(self, *args, **kwargs) -> int:
|
| 227 |
+
return 2 * self.num_slots * self.hidden_size
|
fla/layers/multiscale_retention.py
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
from typing import TYPE_CHECKING, Optional, Tuple
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
from einops import rearrange, repeat
|
| 11 |
+
from transformers.activations import ACT2FN
|
| 12 |
+
|
| 13 |
+
from fla.modules import FusedRMSNormGated, RMSNorm, ShortConvolution
|
| 14 |
+
from fla.modules.rotary import RotaryEmbedding
|
| 15 |
+
from fla.ops.retention import chunk_retention, fused_chunk_retention, fused_recurrent_retention, parallel_retention
|
| 16 |
+
|
| 17 |
+
if TYPE_CHECKING:
|
| 18 |
+
from fla.models.utils import Cache
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class MultiScaleRetention(nn.Module):
|
| 22 |
+
r"""
|
| 23 |
+
The layer implementaion for [Retentive Network: A Successor to Transformer for Large Language Models](https://arxiv.org/pdf/2307.08621.pdf). # noqa
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
mode (str, Optional):
|
| 27 |
+
Which Retention kernel to use.
|
| 28 |
+
Currently available: `chunk`, `fused_recurrent`, `parallel`, and `fused_chunk`.
|
| 29 |
+
Default: `chunk`.
|
| 30 |
+
hidden_size (int, Optional):
|
| 31 |
+
The hidden size of the input. Default: 1024.
|
| 32 |
+
expand_k (float, Optional):
|
| 33 |
+
The expansion ratio for the key dim. Default: 1.0.
|
| 34 |
+
expand_v (float, Optional):
|
| 35 |
+
The expansion ratio for the value dim. Default: 2.0.
|
| 36 |
+
num_heads (int, Optional):
|
| 37 |
+
The number of heads. Default: 8.
|
| 38 |
+
num_kv_heads (int, Optional):
|
| 39 |
+
The number of key/value heads, used for MQA. Default: None.
|
| 40 |
+
feature_map (str, Optional):
|
| 41 |
+
Feature map function applied to queries/keys. Default: None.
|
| 42 |
+
use_short_conv (bool, Optional):
|
| 43 |
+
Whether to use short convolutions. Default: `False`.
|
| 44 |
+
conv_size (int, Optional):
|
| 45 |
+
The kernel size of the short convolution, only used when `use_short_conv` is `True`. Default: 4.
|
| 46 |
+
conv_bias (bool, Optional):
|
| 47 |
+
Whether to use bias in the short convolution, only used when `use_short_conv` is `True`. Default: `False`.
|
| 48 |
+
use_output_gate (bool, Optional):
|
| 49 |
+
Whether to use output gate. Default: `True`.
|
| 50 |
+
gate_fn (str, Optional):
|
| 51 |
+
The activation function for the output gate. Default: `swish`.
|
| 52 |
+
elementwise_affine (bool, Optional):
|
| 53 |
+
If `True`, applies elementwise affine to LayerNorm with learnable parameters. Default: `True`.
|
| 54 |
+
norm_eps (float, Optional):
|
| 55 |
+
The epsilon value for the layernorm/rmsnorm layer. Default: 1e-5.
|
| 56 |
+
fuse_norm (bool, Optional):
|
| 57 |
+
Whether to fuse the norm and the output gate for better memory footprint. Default: `True`.
|
| 58 |
+
layer_idx (int, Optional):
|
| 59 |
+
The index of the layer. Default: None.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
def __init__(
|
| 63 |
+
self,
|
| 64 |
+
mode: str = 'chunk',
|
| 65 |
+
hidden_size: int = 1024,
|
| 66 |
+
expand_k: float = 1.0,
|
| 67 |
+
expand_v: float = 2.0,
|
| 68 |
+
num_heads: int = 8,
|
| 69 |
+
num_kv_heads: Optional[int] = None,
|
| 70 |
+
feature_map: Optional[str] = None,
|
| 71 |
+
use_short_conv: bool = False,
|
| 72 |
+
conv_size: int = 4,
|
| 73 |
+
conv_bias: bool = False,
|
| 74 |
+
use_output_gate: bool = True,
|
| 75 |
+
gate_fn: str = 'swish',
|
| 76 |
+
elementwise_affine: Optional[bool] = True,
|
| 77 |
+
norm_eps: float = 1e-5,
|
| 78 |
+
fuse_norm: bool = True,
|
| 79 |
+
layer_idx: int = None,
|
| 80 |
+
**kwargs
|
| 81 |
+
) -> MultiScaleRetention:
|
| 82 |
+
super().__init__()
|
| 83 |
+
|
| 84 |
+
self.mode = mode
|
| 85 |
+
self.hidden_size = hidden_size
|
| 86 |
+
self.expand_k = expand_k
|
| 87 |
+
self.expand_v = expand_v
|
| 88 |
+
self.num_heads = num_heads
|
| 89 |
+
self.num_kv_heads = num_kv_heads if num_kv_heads is not None else num_heads
|
| 90 |
+
self.num_kv_groups = self.num_heads // self.num_kv_heads
|
| 91 |
+
self.feature_map_fn = ACT2FN[feature_map] if feature_map is not None else None
|
| 92 |
+
|
| 93 |
+
self.use_short_conv = use_short_conv
|
| 94 |
+
self.conv_size = conv_size
|
| 95 |
+
self.conv_bias = conv_bias
|
| 96 |
+
self.use_output_gate = use_output_gate
|
| 97 |
+
|
| 98 |
+
self.key_dim = int(hidden_size * expand_k)
|
| 99 |
+
self.value_dim = int(hidden_size * expand_v)
|
| 100 |
+
self.key_dim_per_group = self.key_dim // self.num_kv_groups
|
| 101 |
+
self.value_dim_per_group = self.value_dim // self.num_kv_groups
|
| 102 |
+
self.layer_idx = layer_idx
|
| 103 |
+
|
| 104 |
+
assert mode in ['chunk', 'fused_chunk', 'parallel', 'fused_recurrent'], f"Not suppoerted mode `{mode}`."
|
| 105 |
+
assert self.key_dim % num_heads == 0, f"key dim must be divisible by num_heads of {num_heads}"
|
| 106 |
+
assert self.value_dim % num_heads == 0, f"value dim must be divisible by num_heads of {num_heads}"
|
| 107 |
+
|
| 108 |
+
self.head_k_dim = self.key_dim // num_heads
|
| 109 |
+
self.head_v_dim = self.value_dim // num_heads
|
| 110 |
+
|
| 111 |
+
self.q_proj = nn.Linear(hidden_size, self.key_dim, bias=False)
|
| 112 |
+
self.k_proj = nn.Linear(hidden_size, self.key_dim_per_group, bias=False)
|
| 113 |
+
self.v_proj = nn.Linear(hidden_size, self.value_dim_per_group, bias=False)
|
| 114 |
+
if self.use_output_gate:
|
| 115 |
+
self.g_proj = nn.Linear(hidden_size, self.value_dim, bias=False)
|
| 116 |
+
|
| 117 |
+
if use_short_conv:
|
| 118 |
+
self.conv_size = conv_size
|
| 119 |
+
self.q_conv1d = ShortConvolution(self.key_dim, conv_size, activation='silu')
|
| 120 |
+
self.k_conv1d = ShortConvolution(self.key_dim_per_group, conv_size, activation='silu')
|
| 121 |
+
self.v_conv1d = ShortConvolution(self.value_dim_per_group, conv_size, activation='silu')
|
| 122 |
+
|
| 123 |
+
self.o_proj = nn.Linear(self.value_dim, hidden_size, bias=False)
|
| 124 |
+
|
| 125 |
+
if gate_fn == 'swish' and fuse_norm and use_output_gate:
|
| 126 |
+
self.g_norm_swish_gate = FusedRMSNormGated(
|
| 127 |
+
hidden_size=self.head_v_dim,
|
| 128 |
+
elementwise_affine=elementwise_affine,
|
| 129 |
+
eps=norm_eps
|
| 130 |
+
)
|
| 131 |
+
self.fuse_norm_and_gate = True
|
| 132 |
+
else:
|
| 133 |
+
self.fuse_norm_and_gate = False
|
| 134 |
+
self.g_norm = RMSNorm(
|
| 135 |
+
hidden_size=self.head_v_dim,
|
| 136 |
+
elementwise_affine=elementwise_affine,
|
| 137 |
+
eps=norm_eps
|
| 138 |
+
)
|
| 139 |
+
self.gate_fn = ACT2FN[gate_fn]
|
| 140 |
+
|
| 141 |
+
# TODO: fix this issue
|
| 142 |
+
# https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/ops/triton/rotary.py#L180
|
| 143 |
+
# Ideally, we would want to support arbitrary d_head_qk
|
| 144 |
+
assert self.head_k_dim <= 256, "head_k_dim must be less than or equal to 256"
|
| 145 |
+
self.rotary = RotaryEmbedding(dim=self.head_k_dim)
|
| 146 |
+
|
| 147 |
+
def forward(
|
| 148 |
+
self,
|
| 149 |
+
hidden_states: torch.Tensor,
|
| 150 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 151 |
+
past_key_values: Optional[Cache] = None,
|
| 152 |
+
use_cache: Optional[bool] = False,
|
| 153 |
+
output_attentions: Optional[bool] = False,
|
| 154 |
+
**kwargs
|
| 155 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]:
|
| 156 |
+
if attention_mask is not None:
|
| 157 |
+
assert len(attention_mask.shape) == 2, (
|
| 158 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 159 |
+
"for padding purposes (0 indicating padding). "
|
| 160 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# launching the triton kernel for just one token will actually be slower
|
| 164 |
+
mode = 'fused_recurrent' if hidden_states.shape[1] <= 64 else self.mode
|
| 165 |
+
|
| 166 |
+
last_state = None
|
| 167 |
+
if past_key_values is not None and len(past_key_values) > self.layer_idx:
|
| 168 |
+
last_state = past_key_values[self.layer_idx]
|
| 169 |
+
|
| 170 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 171 |
+
if self.use_short_conv:
|
| 172 |
+
conv_state_q, conv_state_k, conv_state_v = None, None, None
|
| 173 |
+
if last_state is not None:
|
| 174 |
+
conv_state_q, conv_state_k, conv_state_v = last_state['conv_state']
|
| 175 |
+
conv_mask = attention_mask[:, -hidden_states.shape[1]:] if attention_mask is not None else None
|
| 176 |
+
q, conv_state_q = self.q_conv1d(
|
| 177 |
+
x=self.q_proj(hidden_states),
|
| 178 |
+
mask=conv_mask,
|
| 179 |
+
cache=conv_state_q,
|
| 180 |
+
output_final_state=use_cache,
|
| 181 |
+
cu_seqlens=cu_seqlens
|
| 182 |
+
)
|
| 183 |
+
k, conv_state_k = self.k_conv1d(
|
| 184 |
+
x=self.k_proj(hidden_states),
|
| 185 |
+
mask=conv_mask,
|
| 186 |
+
cache=conv_state_k,
|
| 187 |
+
output_final_state=use_cache,
|
| 188 |
+
cu_seqlens=cu_seqlens
|
| 189 |
+
)
|
| 190 |
+
v, conv_state_v = self.v_conv1d(
|
| 191 |
+
x=self.v_proj(hidden_states),
|
| 192 |
+
mask=conv_mask,
|
| 193 |
+
cache=conv_state_v,
|
| 194 |
+
output_final_state=use_cache,
|
| 195 |
+
cu_seqlens=cu_seqlens
|
| 196 |
+
)
|
| 197 |
+
else:
|
| 198 |
+
q = self.q_proj(hidden_states)
|
| 199 |
+
k = self.k_proj(hidden_states)
|
| 200 |
+
v = self.v_proj(hidden_states)
|
| 201 |
+
|
| 202 |
+
# dealing with left-padding
|
| 203 |
+
if attention_mask is not None:
|
| 204 |
+
v = v.mul_(attention_mask[:, -v.shape[-2]:, None])
|
| 205 |
+
q = rearrange(q, '... (h d) -> ... h d', d=self.head_k_dim)
|
| 206 |
+
k = rearrange(k, '... (h d) -> ... h d', d=self.head_k_dim)
|
| 207 |
+
if self.feature_map_fn is not None:
|
| 208 |
+
q, k = map(self.feature_map_fn, (q, k))
|
| 209 |
+
|
| 210 |
+
seqlen_offset, max_seqlen = 0, q.shape[1]
|
| 211 |
+
if past_key_values is not None:
|
| 212 |
+
seqlen_offset = past_key_values.get_seq_length(self.layer_idx)
|
| 213 |
+
max_seqlen = q.shape[1] + seqlen_offset
|
| 214 |
+
|
| 215 |
+
if attention_mask is not None:
|
| 216 |
+
# to deliminate the offsets of padding tokens
|
| 217 |
+
seqlen_offset = seqlen_offset + attention_mask.sum(-1) - attention_mask.shape[-1]
|
| 218 |
+
max_seqlen = q.shape[1] + max(seqlen_offset)
|
| 219 |
+
|
| 220 |
+
q, k = self.rotary(q, k, seqlen_offset=seqlen_offset, max_seqlen=max_seqlen, cu_seqlens=cu_seqlens)
|
| 221 |
+
|
| 222 |
+
if self.num_kv_groups > 1:
|
| 223 |
+
k = repeat(k, 'b t h d -> b t (h g) d', g=self.num_kv_groups)
|
| 224 |
+
v = repeat(v, 'b t (h d) -> b t (h g) d', d=self.head_v_dim, g=self.num_kv_groups)
|
| 225 |
+
else:
|
| 226 |
+
v = rearrange(v, 'b t (h d) -> b t h d', d=self.head_v_dim)
|
| 227 |
+
|
| 228 |
+
recurrent_state = last_state['recurrent_state'] if last_state is not None else None
|
| 229 |
+
if mode == 'chunk':
|
| 230 |
+
o, recurrent_state = chunk_retention(
|
| 231 |
+
q=q,
|
| 232 |
+
k=k,
|
| 233 |
+
v=v,
|
| 234 |
+
initial_state=recurrent_state,
|
| 235 |
+
output_final_state=use_cache,
|
| 236 |
+
cu_seqlens=cu_seqlens,
|
| 237 |
+
head_first=False
|
| 238 |
+
)
|
| 239 |
+
elif mode == 'fused_chunk':
|
| 240 |
+
o, recurrent_state = fused_chunk_retention(
|
| 241 |
+
q=q,
|
| 242 |
+
k=k,
|
| 243 |
+
v=v,
|
| 244 |
+
initial_state=recurrent_state,
|
| 245 |
+
output_final_state=use_cache,
|
| 246 |
+
cu_seqlens=cu_seqlens,
|
| 247 |
+
head_first=False
|
| 248 |
+
)
|
| 249 |
+
elif mode == 'parallel':
|
| 250 |
+
o, recurrent_state = parallel_retention(
|
| 251 |
+
q=q,
|
| 252 |
+
k=k,
|
| 253 |
+
v=v,
|
| 254 |
+
cu_seqlens=cu_seqlens,
|
| 255 |
+
head_first=False
|
| 256 |
+
)
|
| 257 |
+
elif mode == 'fused_recurrent':
|
| 258 |
+
o, recurrent_state = fused_recurrent_retention(
|
| 259 |
+
q=q,
|
| 260 |
+
k=k,
|
| 261 |
+
v=v,
|
| 262 |
+
initial_state=recurrent_state,
|
| 263 |
+
output_final_state=use_cache,
|
| 264 |
+
cu_seqlens=cu_seqlens,
|
| 265 |
+
head_first=False
|
| 266 |
+
)
|
| 267 |
+
else:
|
| 268 |
+
raise NotImplementedError(f"Not supported mode `{mode}`.")
|
| 269 |
+
|
| 270 |
+
if past_key_values is not None:
|
| 271 |
+
past_key_values.update(
|
| 272 |
+
recurrent_state=recurrent_state,
|
| 273 |
+
conv_state=(conv_state_q, conv_state_k, conv_state_v) if self.use_short_conv else None,
|
| 274 |
+
layer_idx=self.layer_idx,
|
| 275 |
+
offset=q.shape[1]
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
if self.use_output_gate:
|
| 279 |
+
g = self.g_proj(hidden_states)
|
| 280 |
+
if self.fuse_norm_and_gate:
|
| 281 |
+
g = rearrange(g, 'b t (h d) -> b t h d', d=self.head_v_dim)
|
| 282 |
+
o = self.g_norm_swish_gate(o, g)
|
| 283 |
+
o = rearrange(o, 'b t h d -> b t (h d)')
|
| 284 |
+
else:
|
| 285 |
+
o = rearrange(self.g_norm(o), 'b t h d -> b t (h d)')
|
| 286 |
+
o = o * self.gate_fn(g)
|
| 287 |
+
else:
|
| 288 |
+
o = rearrange(self.g_norm(o), 'b t h d -> b t (h d)')
|
| 289 |
+
o = self.o_proj(o)
|
| 290 |
+
|
| 291 |
+
return o, None, past_key_values
|
| 292 |
+
|
| 293 |
+
def state_size(self, **kwargs) -> int:
|
| 294 |
+
state_size = self.key_dim * self.head_v_dim
|
| 295 |
+
for module in self.children():
|
| 296 |
+
if isinstance(module, ShortConvolution):
|
| 297 |
+
state_size += module.state_size
|
| 298 |
+
return state_size
|
fla/models/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (3.07 kB). View file
|
|
|
fla/models/abc/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.abc.configuration_abc import ABCConfig
|
| 6 |
+
from fla.models.abc.modeling_abc import ABCForCausalLM, ABCModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(ABCConfig.model_type, ABCConfig)
|
| 9 |
+
AutoModel.register(ABCConfig, ABCModel)
|
| 10 |
+
AutoModelForCausalLM.register(ABCConfig, ABCForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['ABCConfig', 'ABCForCausalLM', 'ABCModel']
|
fla/models/bitnet/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.bitnet.configuration_bitnet import BitNetConfig
|
| 6 |
+
from fla.models.bitnet.modeling_bitnet import BitNetForCausalLM, BitNetModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(BitNetConfig.model_type, BitNetConfig)
|
| 9 |
+
AutoModel.register(BitNetConfig, BitNetModel)
|
| 10 |
+
AutoModelForCausalLM.register(BitNetConfig, BitNetForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['BitNetConfig', 'BitNetForCausalLM', 'BitNetModel']
|
fla/models/delta_net/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.delta_net.configuration_delta_net import DeltaNetConfig
|
| 6 |
+
from fla.models.delta_net.modeling_delta_net import DeltaNetForCausalLM, DeltaNetModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(DeltaNetConfig.model_type, DeltaNetConfig)
|
| 9 |
+
AutoModel.register(DeltaNetConfig, DeltaNetModel)
|
| 10 |
+
AutoModelForCausalLM.register(DeltaNetConfig, DeltaNetForCausalLM)
|
| 11 |
+
|
| 12 |
+
__all__ = ['DeltaNetConfig', 'DeltaNetForCausalLM', 'DeltaNetModel']
|
fla/models/forgetting_transformer/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.forgetting_transformer.configuration_forgetting_transformer import ForgettingTransformerConfig
|
| 6 |
+
from fla.models.forgetting_transformer.modeling_forgetting_transformer import (
|
| 7 |
+
ForgettingTransformerForCausalLM,
|
| 8 |
+
ForgettingTransformerModel
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
AutoConfig.register(ForgettingTransformerConfig.model_type, ForgettingTransformerConfig)
|
| 12 |
+
AutoModel.register(ForgettingTransformerConfig, ForgettingTransformerModel)
|
| 13 |
+
AutoModelForCausalLM.register(ForgettingTransformerConfig, ForgettingTransformerForCausalLM)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
__all__ = ['ForgettingTransformerConfig', 'ForgettingTransformerForCausalLM', 'ForgettingTransformerModel']
|
fla/models/forgetting_transformer/configuration_forgetting_transformer.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class ForgettingTransformerConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'forgetting_transformer'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
hidden_size: int = 2048,
|
| 16 |
+
num_hidden_layers: int = 24,
|
| 17 |
+
num_heads: int = 32,
|
| 18 |
+
num_kv_heads: Optional[int] = None,
|
| 19 |
+
qkv_bias: bool = False,
|
| 20 |
+
qk_norm: bool = False,
|
| 21 |
+
window_size: Optional[int] = None,
|
| 22 |
+
use_output_gate: bool = False,
|
| 23 |
+
hidden_ratio: Optional[int] = 4,
|
| 24 |
+
intermediate_size: Optional[int] = None,
|
| 25 |
+
hidden_act: str = "swish",
|
| 26 |
+
initializer_range: float = 0.006,
|
| 27 |
+
elementwise_affine: Optional[bool] = True,
|
| 28 |
+
norm_eps: float = 1e-6,
|
| 29 |
+
use_cache: bool = True,
|
| 30 |
+
pad_token_id: Optional[int] = None,
|
| 31 |
+
bos_token_id: int = 1,
|
| 32 |
+
eos_token_id: int = 2,
|
| 33 |
+
tie_word_embeddings: bool = False,
|
| 34 |
+
fuse_norm: bool = True,
|
| 35 |
+
fuse_swiglu: bool = True,
|
| 36 |
+
fuse_cross_entropy: bool = True,
|
| 37 |
+
vocab_size: int = 32000,
|
| 38 |
+
**kwargs,
|
| 39 |
+
):
|
| 40 |
+
self.hidden_size = hidden_size
|
| 41 |
+
self.num_hidden_layers = num_hidden_layers
|
| 42 |
+
self.num_heads = num_heads
|
| 43 |
+
self.num_kv_heads = num_kv_heads
|
| 44 |
+
self.qkv_bias = qkv_bias
|
| 45 |
+
self.qk_norm = qk_norm
|
| 46 |
+
self.window_size = window_size
|
| 47 |
+
self.use_output_gate = use_output_gate
|
| 48 |
+
self.hidden_ratio = hidden_ratio
|
| 49 |
+
self.intermediate_size = intermediate_size
|
| 50 |
+
self.hidden_act = hidden_act
|
| 51 |
+
|
| 52 |
+
self.initializer_range = initializer_range
|
| 53 |
+
self.elementwise_affine = elementwise_affine
|
| 54 |
+
self.norm_eps = norm_eps
|
| 55 |
+
self.use_cache = use_cache
|
| 56 |
+
|
| 57 |
+
self.fuse_norm = fuse_norm
|
| 58 |
+
self.fuse_swiglu = fuse_swiglu
|
| 59 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 60 |
+
self.vocab_size = vocab_size
|
| 61 |
+
|
| 62 |
+
super().__init__(
|
| 63 |
+
pad_token_id=pad_token_id,
|
| 64 |
+
bos_token_id=bos_token_id,
|
| 65 |
+
eos_token_id=eos_token_id,
|
| 66 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 67 |
+
**kwargs,
|
| 68 |
+
)
|
fla/models/gated_deltanet/configuration_gated_deltanet.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Dict, Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class GatedDeltaNetConfig(PretrainedConfig):
|
| 9 |
+
model_type = 'gated_deltanet'
|
| 10 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 11 |
+
|
| 12 |
+
def __init__(
|
| 13 |
+
self,
|
| 14 |
+
attn_mode: str = "chunk",
|
| 15 |
+
hidden_size: int = 2048,
|
| 16 |
+
expand_v: int = 2,
|
| 17 |
+
use_gate: bool = True,
|
| 18 |
+
use_short_conv: bool = True,
|
| 19 |
+
conv_size: int = 4,
|
| 20 |
+
head_dim: int = 256,
|
| 21 |
+
num_heads: int = 6,
|
| 22 |
+
max_position_embeddings: int = 2048,
|
| 23 |
+
hidden_ratio: Optional[int] = 4,
|
| 24 |
+
intermediate_size: Optional[int] = None,
|
| 25 |
+
hidden_act: str = "swish",
|
| 26 |
+
num_hidden_layers: int = 21,
|
| 27 |
+
norm_eps: float = 1e-6,
|
| 28 |
+
attn: Optional[Dict] = None,
|
| 29 |
+
use_cache: bool = True,
|
| 30 |
+
pad_token_id: int = None,
|
| 31 |
+
bos_token_id: int = 1,
|
| 32 |
+
eos_token_id: int = 2,
|
| 33 |
+
tie_word_embeddings: bool = False,
|
| 34 |
+
initializer_range: float = 0.006,
|
| 35 |
+
fuse_norm: bool = True,
|
| 36 |
+
fuse_swiglu: bool = True,
|
| 37 |
+
fuse_cross_entropy: bool = True,
|
| 38 |
+
vocab_size: int = 32000,
|
| 39 |
+
**kwargs
|
| 40 |
+
):
|
| 41 |
+
self.attn_mode = attn_mode
|
| 42 |
+
self.hidden_size = hidden_size
|
| 43 |
+
self.expand_v = expand_v
|
| 44 |
+
self.use_gate = use_gate
|
| 45 |
+
self.use_short_conv = use_short_conv
|
| 46 |
+
self.conv_size = conv_size
|
| 47 |
+
self.head_dim = head_dim
|
| 48 |
+
self.num_heads = num_heads
|
| 49 |
+
self.max_position_embeddings = max_position_embeddings
|
| 50 |
+
|
| 51 |
+
self.hidden_ratio = hidden_ratio
|
| 52 |
+
self.intermediate_size = intermediate_size
|
| 53 |
+
self.hidden_act = hidden_act
|
| 54 |
+
self.num_hidden_layers = num_hidden_layers
|
| 55 |
+
self.norm_eps = norm_eps
|
| 56 |
+
self.attn = attn
|
| 57 |
+
self.use_cache = use_cache
|
| 58 |
+
self.initializer_range = initializer_range
|
| 59 |
+
|
| 60 |
+
self.fuse_norm = fuse_norm
|
| 61 |
+
self.fuse_swiglu = fuse_swiglu
|
| 62 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 63 |
+
self.vocab_size = vocab_size
|
| 64 |
+
|
| 65 |
+
if attn is not None:
|
| 66 |
+
if not isinstance(attn, Dict):
|
| 67 |
+
raise ValueError("attn must be a dictionary")
|
| 68 |
+
if 'layers' not in attn:
|
| 69 |
+
raise ValueError("Layer indices must be provided to initialize hybrid attention layers")
|
| 70 |
+
if 'num_heads' not in attn:
|
| 71 |
+
raise ValueError("Number of heads must be provided to initialize hybrid attention layers")
|
| 72 |
+
attn['num_kv_heads'] = attn.get('num_kv_heads', attn['num_heads'])
|
| 73 |
+
attn['qkv_bias'] = attn.get('qkv_bias', False)
|
| 74 |
+
attn['window_size'] = attn.get('window_size', None)
|
| 75 |
+
attn['rope_theta'] = attn.get('rope_theta', 10000.)
|
| 76 |
+
|
| 77 |
+
super().__init__(
|
| 78 |
+
pad_token_id=pad_token_id,
|
| 79 |
+
bos_token_id=bos_token_id,
|
| 80 |
+
eos_token_id=eos_token_id,
|
| 81 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 82 |
+
**kwargs,
|
| 83 |
+
)
|
fla/models/gated_deltaproduct/modeling_gated_deltaproduct.py
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.utils.checkpoint
|
| 12 |
+
from transformers.activations import ACT2FN
|
| 13 |
+
from transformers.generation import GenerationMixin
|
| 14 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 15 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 16 |
+
from transformers.utils import logging
|
| 17 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 18 |
+
|
| 19 |
+
from fla.layers.attn import Attention
|
| 20 |
+
from fla.layers.gated_deltaproduct import GatedDeltaProduct
|
| 21 |
+
from fla.models.gated_deltaproduct.configuration_gated_deltaproduct import GatedDeltaProductConfig
|
| 22 |
+
from fla.models.utils import Cache
|
| 23 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss, RMSNorm
|
| 24 |
+
from fla.modules.activations import swiglu_linear
|
| 25 |
+
from fla.modules.layernorm import rms_norm_linear
|
| 26 |
+
|
| 27 |
+
if TYPE_CHECKING:
|
| 28 |
+
from transformers.processing_utils import Unpack
|
| 29 |
+
|
| 30 |
+
logger = logging.get_logger(__name__)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class GatedDeltaNetMLP(nn.Module):
|
| 34 |
+
def __init__(
|
| 35 |
+
self,
|
| 36 |
+
hidden_size: int,
|
| 37 |
+
hidden_ratio: Optional[int] = None,
|
| 38 |
+
intermediate_size: Optional[int] = None,
|
| 39 |
+
hidden_act: str = "swish",
|
| 40 |
+
norm_first: bool = True,
|
| 41 |
+
norm_eps: float = 1e-5,
|
| 42 |
+
) -> GatedDeltaNetMLP:
|
| 43 |
+
super().__init__()
|
| 44 |
+
|
| 45 |
+
self.hidden_size = hidden_size
|
| 46 |
+
# the final number of params is `hidden_ratio * hidden_size^2`
|
| 47 |
+
# `intermediate_size` is chosen to be a multiple of 256 closest to `2/3 * hidden_size * hidden_ratio`
|
| 48 |
+
if hidden_ratio is None:
|
| 49 |
+
hidden_ratio = 4
|
| 50 |
+
if intermediate_size is None:
|
| 51 |
+
intermediate_size = int(hidden_size * hidden_ratio * 2 / 3)
|
| 52 |
+
intermediate_size = 256 * ((intermediate_size + 256 - 1) // 256)
|
| 53 |
+
self.hidden_ratio = hidden_ratio
|
| 54 |
+
self.intermediate_size = intermediate_size
|
| 55 |
+
self.norm_first = norm_first
|
| 56 |
+
|
| 57 |
+
if norm_first:
|
| 58 |
+
self.norm = RMSNorm(hidden_size=hidden_size, eps=norm_eps)
|
| 59 |
+
|
| 60 |
+
self.gate_proj = nn.Linear(
|
| 61 |
+
self.hidden_size, self.intermediate_size * 2, bias=False
|
| 62 |
+
)
|
| 63 |
+
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
|
| 64 |
+
self.act_fn = ACT2FN[hidden_act]
|
| 65 |
+
|
| 66 |
+
def forward(
|
| 67 |
+
self,
|
| 68 |
+
x: torch.Tensor,
|
| 69 |
+
**kwargs: Unpack[Dict],
|
| 70 |
+
) -> torch.Tensor:
|
| 71 |
+
if self.norm_first:
|
| 72 |
+
x = rms_norm_linear(
|
| 73 |
+
x,
|
| 74 |
+
self.norm.weight,
|
| 75 |
+
self.norm.bias,
|
| 76 |
+
self.gate_proj.weight,
|
| 77 |
+
self.gate_proj.bias,
|
| 78 |
+
)
|
| 79 |
+
else:
|
| 80 |
+
x = self.gate_proj(x)
|
| 81 |
+
gate, y = x.chunk(2, -1)
|
| 82 |
+
return swiglu_linear(gate, y, self.down_proj.weight, self.down_proj.bias)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class GatedDeltaProductBlock(nn.Module):
|
| 86 |
+
def __init__(self, config: GatedDeltaProductConfig, layer_idx: int):
|
| 87 |
+
super().__init__()
|
| 88 |
+
self.hidden_size = config.hidden_size
|
| 89 |
+
|
| 90 |
+
if not config.norm_first:
|
| 91 |
+
self.attn_norm = RMSNorm(
|
| 92 |
+
hidden_size=config.hidden_size, eps=config.norm_eps
|
| 93 |
+
)
|
| 94 |
+
if config.attn is not None and layer_idx in config.attn["layers"]:
|
| 95 |
+
self.attn = Attention(
|
| 96 |
+
hidden_size=config.hidden_size,
|
| 97 |
+
num_heads=config.attn["num_heads"],
|
| 98 |
+
num_kv_heads=config.attn["num_kv_heads"],
|
| 99 |
+
window_size=config.attn["window_size"],
|
| 100 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 101 |
+
layer_idx=layer_idx,
|
| 102 |
+
)
|
| 103 |
+
else:
|
| 104 |
+
self.attn = GatedDeltaProduct(
|
| 105 |
+
mode=config.attn_mode,
|
| 106 |
+
hidden_size=config.hidden_size,
|
| 107 |
+
expand_v=config.expand_v,
|
| 108 |
+
head_dim=config.head_dim,
|
| 109 |
+
num_heads=config.num_heads,
|
| 110 |
+
use_gate=config.use_gate,
|
| 111 |
+
use_forget_gate=config.use_forget_gate,
|
| 112 |
+
use_short_conv=config.use_short_conv,
|
| 113 |
+
conv_size=config.conv_size,
|
| 114 |
+
norm_first=config.norm_first,
|
| 115 |
+
norm_eps=config.norm_eps,
|
| 116 |
+
allow_neg_eigval=config.allow_neg_eigval,
|
| 117 |
+
num_householder=config.num_householder,
|
| 118 |
+
layer_idx=layer_idx,
|
| 119 |
+
use_beta_conv=config.use_beta_conv
|
| 120 |
+
)
|
| 121 |
+
if not config.norm_first:
|
| 122 |
+
self.mlp_norm = RMSNorm(hidden_size=config.hidden_size, eps=config.norm_eps)
|
| 123 |
+
self.mlp = GatedDeltaNetMLP(
|
| 124 |
+
hidden_size=config.hidden_size,
|
| 125 |
+
hidden_ratio=config.hidden_ratio,
|
| 126 |
+
intermediate_size=config.intermediate_size,
|
| 127 |
+
hidden_act=config.hidden_act,
|
| 128 |
+
norm_first=config.norm_first,
|
| 129 |
+
norm_eps=config.norm_eps,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
def forward(
|
| 133 |
+
self,
|
| 134 |
+
hidden_states: torch.Tensor,
|
| 135 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 136 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 137 |
+
use_cache: Optional[bool] = False,
|
| 138 |
+
output_attentions: Optional[bool] = False,
|
| 139 |
+
**kwargs: Unpack[Dict],
|
| 140 |
+
) -> Tuple[
|
| 141 |
+
torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]
|
| 142 |
+
]:
|
| 143 |
+
residual = hidden_states
|
| 144 |
+
if hasattr(self, "attn_norm"):
|
| 145 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 146 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 147 |
+
hidden_states=hidden_states,
|
| 148 |
+
attention_mask=attention_mask,
|
| 149 |
+
past_key_values=past_key_values,
|
| 150 |
+
use_cache=use_cache,
|
| 151 |
+
output_attentions=output_attentions,
|
| 152 |
+
**kwargs,
|
| 153 |
+
)
|
| 154 |
+
if hasattr(self, "mlp_norm"):
|
| 155 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 156 |
+
else:
|
| 157 |
+
hidden_states = residual + hidden_states
|
| 158 |
+
residual = hidden_states
|
| 159 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 160 |
+
hidden_states = residual + hidden_states
|
| 161 |
+
|
| 162 |
+
outputs = (hidden_states, attentions, past_key_values)
|
| 163 |
+
|
| 164 |
+
return outputs
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
class GatedDeltaProductPreTrainedModel(PreTrainedModel):
|
| 168 |
+
config_class = GatedDeltaProductConfig
|
| 169 |
+
supports_gradient_checkpointing = True
|
| 170 |
+
_no_split_modules = ["GatedDeltaNetBlock"]
|
| 171 |
+
|
| 172 |
+
def __init__(self, *inputs, **kwargs):
|
| 173 |
+
super().__init__(*inputs, **kwargs)
|
| 174 |
+
|
| 175 |
+
def _init_weights(
|
| 176 |
+
self,
|
| 177 |
+
module: nn.Module,
|
| 178 |
+
rescale_prenorm_residual: bool = True,
|
| 179 |
+
num_residuals_per_layer: int = 2,
|
| 180 |
+
):
|
| 181 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 182 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 183 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 184 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 185 |
+
if module.bias is not None:
|
| 186 |
+
nn.init.zeros_(module.bias)
|
| 187 |
+
elif isinstance(module, nn.Embedding):
|
| 188 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 189 |
+
if module.padding_idx is not None:
|
| 190 |
+
module.weight.data[module.padding_idx].zero_()
|
| 191 |
+
|
| 192 |
+
if rescale_prenorm_residual:
|
| 193 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 194 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 195 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 196 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 197 |
+
#
|
| 198 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 199 |
+
for name, p in module.named_parameters():
|
| 200 |
+
if name in ["o_proj.weight", "down_proj.weight"]:
|
| 201 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 202 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 203 |
+
# We need to reinit p since this code could be called multiple times
|
| 204 |
+
# Having just p *= scale would repeatedly scale it down
|
| 205 |
+
with torch.no_grad():
|
| 206 |
+
p /= math.sqrt(
|
| 207 |
+
num_residuals_per_layer * self.config.num_hidden_layers
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
class GatedDeltaProductModel(GatedDeltaProductPreTrainedModel):
|
| 212 |
+
def __init__(self, config: GatedDeltaProductConfig):
|
| 213 |
+
super().__init__(config)
|
| 214 |
+
self.padding_idx = config.pad_token_id
|
| 215 |
+
self.vocab_size = config.vocab_size
|
| 216 |
+
|
| 217 |
+
self.embeddings = nn.Embedding(
|
| 218 |
+
config.vocab_size, config.hidden_size, self.padding_idx
|
| 219 |
+
)
|
| 220 |
+
self.layers = nn.ModuleList(
|
| 221 |
+
[
|
| 222 |
+
GatedDeltaProductBlock(config, layer_idx)
|
| 223 |
+
for layer_idx in range(config.num_hidden_layers)
|
| 224 |
+
]
|
| 225 |
+
)
|
| 226 |
+
self.norm = RMSNorm(config.hidden_size, eps=config.norm_eps)
|
| 227 |
+
|
| 228 |
+
self.gradient_checkpointing = False
|
| 229 |
+
|
| 230 |
+
self.post_init()
|
| 231 |
+
|
| 232 |
+
def get_input_embeddings(self):
|
| 233 |
+
return self.embeddings
|
| 234 |
+
|
| 235 |
+
def set_input_embeddings(self, value):
|
| 236 |
+
self.embeddings = value
|
| 237 |
+
|
| 238 |
+
def forward(
|
| 239 |
+
self,
|
| 240 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 241 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 242 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 243 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 244 |
+
use_cache: Optional[bool] = None,
|
| 245 |
+
output_attentions: Optional[bool] = None,
|
| 246 |
+
output_hidden_states: Optional[bool] = None,
|
| 247 |
+
return_dict: Optional[bool] = None,
|
| 248 |
+
**kwargs: Unpack[Dict],
|
| 249 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 250 |
+
if output_attentions:
|
| 251 |
+
warnings.warn(
|
| 252 |
+
"`GatedDeltaNetModel` does not `output_attentions` now, setting it to `False`.",
|
| 253 |
+
stacklevel=2,
|
| 254 |
+
)
|
| 255 |
+
output_attentions = False
|
| 256 |
+
output_attentions = (
|
| 257 |
+
output_attentions
|
| 258 |
+
if output_attentions is not None
|
| 259 |
+
else self.config.output_attentions
|
| 260 |
+
)
|
| 261 |
+
output_hidden_states = (
|
| 262 |
+
output_hidden_states
|
| 263 |
+
if output_hidden_states is not None
|
| 264 |
+
else self.config.output_hidden_states
|
| 265 |
+
)
|
| 266 |
+
use_cache = (
|
| 267 |
+
use_cache
|
| 268 |
+
if use_cache is not None
|
| 269 |
+
else (self.config.use_cache if not self.training else False)
|
| 270 |
+
)
|
| 271 |
+
return_dict = (
|
| 272 |
+
return_dict if return_dict is not None else self.config.use_return_dict
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
# retrieve input_ids and inputs_embeds
|
| 276 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 277 |
+
raise ValueError(
|
| 278 |
+
"You cannot specify both input_ids and inputs_embeds at the same time"
|
| 279 |
+
)
|
| 280 |
+
if input_ids is None and inputs_embeds is None:
|
| 281 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 282 |
+
|
| 283 |
+
if inputs_embeds is None:
|
| 284 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 285 |
+
hidden_states = inputs_embeds
|
| 286 |
+
|
| 287 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 288 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 289 |
+
|
| 290 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 291 |
+
logger.warning_once(
|
| 292 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
| 293 |
+
)
|
| 294 |
+
use_cache = False
|
| 295 |
+
|
| 296 |
+
all_hidden_states = () if output_hidden_states else None
|
| 297 |
+
all_attns = () if output_attentions else None
|
| 298 |
+
for layer in self.layers:
|
| 299 |
+
if output_hidden_states:
|
| 300 |
+
all_hidden_states += (hidden_states,)
|
| 301 |
+
|
| 302 |
+
if self.gradient_checkpointing and self.training:
|
| 303 |
+
hidden_states, attentions, past_key_values = (
|
| 304 |
+
self._gradient_checkpointing_func(
|
| 305 |
+
layer.__call__,
|
| 306 |
+
hidden_states,
|
| 307 |
+
attention_mask,
|
| 308 |
+
past_key_values,
|
| 309 |
+
use_cache,
|
| 310 |
+
output_attentions,
|
| 311 |
+
**kwargs,
|
| 312 |
+
)
|
| 313 |
+
)
|
| 314 |
+
else:
|
| 315 |
+
hidden_states, attentions, past_key_values = layer(
|
| 316 |
+
hidden_states,
|
| 317 |
+
attention_mask=attention_mask,
|
| 318 |
+
past_key_values=past_key_values,
|
| 319 |
+
use_cache=use_cache,
|
| 320 |
+
output_attentions=output_attentions,
|
| 321 |
+
**kwargs,
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
if output_attentions:
|
| 325 |
+
all_attns += (attentions,)
|
| 326 |
+
|
| 327 |
+
hidden_states = self.norm(hidden_states)
|
| 328 |
+
# add hidden states from the last decoder layer
|
| 329 |
+
if output_hidden_states:
|
| 330 |
+
all_hidden_states += (hidden_states,)
|
| 331 |
+
|
| 332 |
+
if not return_dict:
|
| 333 |
+
return tuple(
|
| 334 |
+
i
|
| 335 |
+
for i in [
|
| 336 |
+
hidden_states,
|
| 337 |
+
past_key_values,
|
| 338 |
+
all_hidden_states,
|
| 339 |
+
all_attns,
|
| 340 |
+
]
|
| 341 |
+
if i is not None
|
| 342 |
+
)
|
| 343 |
+
return BaseModelOutputWithPast(
|
| 344 |
+
last_hidden_state=hidden_states,
|
| 345 |
+
past_key_values=past_key_values,
|
| 346 |
+
hidden_states=all_hidden_states,
|
| 347 |
+
attentions=all_attns,
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
class GatedDeltaProductForCausalLM(GatedDeltaProductPreTrainedModel, GenerationMixin):
|
| 352 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 353 |
+
|
| 354 |
+
def __init__(self, config):
|
| 355 |
+
super().__init__(config)
|
| 356 |
+
self.model = GatedDeltaProductModel(config)
|
| 357 |
+
self.vocab_size = config.vocab_size
|
| 358 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 359 |
+
|
| 360 |
+
# Initialize weights and apply final processing
|
| 361 |
+
self.post_init()
|
| 362 |
+
|
| 363 |
+
def get_input_embeddings(self):
|
| 364 |
+
return self.model.embeddings
|
| 365 |
+
|
| 366 |
+
def set_input_embeddings(self, value):
|
| 367 |
+
self.model.embeddings = value
|
| 368 |
+
|
| 369 |
+
def get_output_embeddings(self):
|
| 370 |
+
return self.lm_head
|
| 371 |
+
|
| 372 |
+
def set_output_embeddings(self, new_embeddings):
|
| 373 |
+
self.lm_head = new_embeddings
|
| 374 |
+
|
| 375 |
+
def set_decoder(self, decoder):
|
| 376 |
+
self.model = decoder
|
| 377 |
+
|
| 378 |
+
def get_decoder(self):
|
| 379 |
+
return self.model
|
| 380 |
+
|
| 381 |
+
def generate(self, *args, **kwargs):
|
| 382 |
+
try:
|
| 383 |
+
return super().generate(*args, **kwargs)
|
| 384 |
+
except AttributeError as exception:
|
| 385 |
+
if "past_key_values" in str(exception):
|
| 386 |
+
raise AttributeError(
|
| 387 |
+
f"You tried to call `generate` with a decoding strategy that manipulates `past_key_values`, "
|
| 388 |
+
f"which is not supported for {self.__class__.__name__}. "
|
| 389 |
+
f"Try another generation strategy instead. "
|
| 390 |
+
f"For the available generation strategies, check this doc: "
|
| 391 |
+
f"https://huggingface.co/docs/transformers/en/generation_strategies#decoding-strategies"
|
| 392 |
+
)
|
| 393 |
+
else:
|
| 394 |
+
raise exception
|
| 395 |
+
|
| 396 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 397 |
+
def prepare_inputs_for_generation(
|
| 398 |
+
self,
|
| 399 |
+
input_ids: torch.LongTensor = None,
|
| 400 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 401 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 402 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 403 |
+
use_cache: bool = True,
|
| 404 |
+
num_logits_to_keep: Optional[int] = None,
|
| 405 |
+
logits_to_keep: Optional[int] = None,
|
| 406 |
+
**kwargs,
|
| 407 |
+
):
|
| 408 |
+
# only last token for `inputs_ids` if the `past_key_values` is passed along is not empty.
|
| 409 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 410 |
+
input_ids = input_ids[:, -1:]
|
| 411 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 412 |
+
if inputs_embeds is not None and past_key_values is None:
|
| 413 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
| 414 |
+
else:
|
| 415 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 416 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 417 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 418 |
+
# TODO: use `next_tokens` directly instead.
|
| 419 |
+
model_inputs = {"input_ids": input_ids.contiguous()}
|
| 420 |
+
|
| 421 |
+
if logits_to_keep is not None:
|
| 422 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 423 |
+
|
| 424 |
+
model_inputs.update(
|
| 425 |
+
{
|
| 426 |
+
"past_key_values": past_key_values,
|
| 427 |
+
"use_cache": use_cache,
|
| 428 |
+
"attention_mask": attention_mask,
|
| 429 |
+
"num_logits_to_keep": num_logits_to_keep,
|
| 430 |
+
}
|
| 431 |
+
)
|
| 432 |
+
return model_inputs
|
| 433 |
+
|
| 434 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 435 |
+
def forward(
|
| 436 |
+
self,
|
| 437 |
+
input_ids: torch.LongTensor = None,
|
| 438 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 439 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 440 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 441 |
+
labels: Optional[torch.LongTensor] = None,
|
| 442 |
+
use_cache: Optional[bool] = None,
|
| 443 |
+
output_attentions: Optional[bool] = None,
|
| 444 |
+
output_hidden_states: Optional[bool] = None,
|
| 445 |
+
return_dict: Optional[bool] = None,
|
| 446 |
+
num_logits_to_keep: Optional[int] = 0,
|
| 447 |
+
logits_to_keep: Optional[int] = 0,
|
| 448 |
+
**kwargs: Unpack[Dict],
|
| 449 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 450 |
+
num_logits_to_keep = 0 if num_logits_to_keep is None else num_logits_to_keep
|
| 451 |
+
output_attentions = (
|
| 452 |
+
output_attentions
|
| 453 |
+
if output_attentions is not None
|
| 454 |
+
else self.config.output_attentions
|
| 455 |
+
)
|
| 456 |
+
output_hidden_states = (
|
| 457 |
+
output_hidden_states
|
| 458 |
+
if output_hidden_states is not None
|
| 459 |
+
else self.config.output_hidden_states
|
| 460 |
+
)
|
| 461 |
+
return_dict = (
|
| 462 |
+
return_dict if return_dict is not None else self.config.use_return_dict
|
| 463 |
+
)
|
| 464 |
+
kwargs.pop("num_items_in_batch", None)
|
| 465 |
+
outputs = self.model(
|
| 466 |
+
input_ids=input_ids,
|
| 467 |
+
attention_mask=attention_mask,
|
| 468 |
+
inputs_embeds=inputs_embeds,
|
| 469 |
+
past_key_values=past_key_values,
|
| 470 |
+
use_cache=use_cache,
|
| 471 |
+
output_attentions=output_attentions,
|
| 472 |
+
output_hidden_states=output_hidden_states,
|
| 473 |
+
return_dict=return_dict,
|
| 474 |
+
**kwargs,
|
| 475 |
+
)
|
| 476 |
+
hidden_states = outputs[0]
|
| 477 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 478 |
+
|
| 479 |
+
loss, logits = None, None
|
| 480 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 481 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 482 |
+
if labels is not None:
|
| 483 |
+
if self.config.fuse_cross_entropy:
|
| 484 |
+
if fuse_linear_and_cross_entropy:
|
| 485 |
+
loss_fct = FusedLinearCrossEntropyLoss()
|
| 486 |
+
else:
|
| 487 |
+
loss_fct = FusedCrossEntropyLoss(inplace_backward=True)
|
| 488 |
+
else:
|
| 489 |
+
loss_fct = nn.CrossEntropyLoss()
|
| 490 |
+
# Enable model parallelism
|
| 491 |
+
labels = labels.to(hidden_states.device)
|
| 492 |
+
labels = torch.cat(
|
| 493 |
+
(
|
| 494 |
+
labels[..., 1:],
|
| 495 |
+
torch.full_like(labels[:, :1], loss_fct.ignore_index),
|
| 496 |
+
),
|
| 497 |
+
1,
|
| 498 |
+
)
|
| 499 |
+
if fuse_linear_and_cross_entropy:
|
| 500 |
+
loss = loss_fct(
|
| 501 |
+
hidden_states.view(-1, self.config.hidden_size),
|
| 502 |
+
labels.view(-1),
|
| 503 |
+
self.lm_head.weight,
|
| 504 |
+
self.lm_head.bias,
|
| 505 |
+
)
|
| 506 |
+
else:
|
| 507 |
+
loss = loss_fct(
|
| 508 |
+
logits.view(-1, self.config.vocab_size), labels.view(-1)
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
if not return_dict:
|
| 512 |
+
output = (logits,) + outputs[1:]
|
| 513 |
+
return (loss, *output) if loss is not None else output
|
| 514 |
+
return CausalLMOutputWithPast(
|
| 515 |
+
loss=loss,
|
| 516 |
+
logits=logits,
|
| 517 |
+
past_key_values=outputs.past_key_values,
|
| 518 |
+
hidden_states=outputs.hidden_states,
|
| 519 |
+
attentions=outputs.attentions,
|
| 520 |
+
)
|
fla/models/gla/configuration_gla.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Dict, Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class GLAConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'gla'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
hidden_size: int = 2048,
|
| 16 |
+
expand_k: int = 0.5,
|
| 17 |
+
expand_v: int = 1,
|
| 18 |
+
hidden_ratio: Optional[int] = 4,
|
| 19 |
+
intermediate_size: Optional[int] = None,
|
| 20 |
+
num_hidden_layers: int = 24,
|
| 21 |
+
num_heads: int = 4,
|
| 22 |
+
num_kv_heads: Optional[int] = None,
|
| 23 |
+
feature_map: Optional[str] = None,
|
| 24 |
+
attn_mode: str = "chunk",
|
| 25 |
+
use_short_conv: bool = False,
|
| 26 |
+
conv_size: int = 4,
|
| 27 |
+
use_output_gate: bool = True,
|
| 28 |
+
clamp_min: Optional[float] = None,
|
| 29 |
+
hidden_act: str = "swish",
|
| 30 |
+
max_position_embeddings: int = 2048,
|
| 31 |
+
elementwise_affine: Optional[bool] = True,
|
| 32 |
+
norm_eps: float = 1e-6,
|
| 33 |
+
use_gk: bool = True,
|
| 34 |
+
use_gv: bool = False,
|
| 35 |
+
attn: Optional[Dict] = None,
|
| 36 |
+
use_cache: bool = True,
|
| 37 |
+
pad_token_id: int = None,
|
| 38 |
+
bos_token_id: int = 1,
|
| 39 |
+
eos_token_id: int = 2,
|
| 40 |
+
tie_word_embeddings: bool = False,
|
| 41 |
+
initializer_range: float = 0.006,
|
| 42 |
+
fuse_norm: bool = True,
|
| 43 |
+
fuse_swiglu: bool = True,
|
| 44 |
+
fuse_cross_entropy: bool = True,
|
| 45 |
+
vocab_size: int = 32000,
|
| 46 |
+
**kwargs
|
| 47 |
+
):
|
| 48 |
+
self.hidden_size = hidden_size
|
| 49 |
+
self.expand_k = expand_k
|
| 50 |
+
self.expand_v = expand_v
|
| 51 |
+
self.hidden_ratio = hidden_ratio
|
| 52 |
+
self.intermediate_size = intermediate_size
|
| 53 |
+
self.num_hidden_layers = num_hidden_layers
|
| 54 |
+
self.num_heads = num_heads
|
| 55 |
+
self.num_kv_heads = num_kv_heads
|
| 56 |
+
self.feature_map = feature_map
|
| 57 |
+
self.attn_mode = attn_mode
|
| 58 |
+
self.use_short_conv = use_short_conv
|
| 59 |
+
self.conv_size = conv_size
|
| 60 |
+
self.use_output_gate = use_output_gate
|
| 61 |
+
self.clamp_min = clamp_min
|
| 62 |
+
self.hidden_act = hidden_act
|
| 63 |
+
self.max_position_embeddings = max_position_embeddings
|
| 64 |
+
self.elementwise_affine = elementwise_affine
|
| 65 |
+
self.norm_eps = norm_eps
|
| 66 |
+
self.use_gk = use_gk
|
| 67 |
+
self.use_gv = use_gv
|
| 68 |
+
self.attn = attn
|
| 69 |
+
self.use_cache = use_cache
|
| 70 |
+
self.initializer_range = initializer_range
|
| 71 |
+
|
| 72 |
+
self.fuse_norm = fuse_norm
|
| 73 |
+
self.fuse_swiglu = fuse_swiglu
|
| 74 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 75 |
+
self.vocab_size = vocab_size
|
| 76 |
+
|
| 77 |
+
if attn is not None:
|
| 78 |
+
if not isinstance(attn, Dict):
|
| 79 |
+
raise ValueError("attn must be a dictionary")
|
| 80 |
+
if 'layers' not in attn:
|
| 81 |
+
raise ValueError("Layer indices must be provided to initialize hybrid attention layers")
|
| 82 |
+
if 'num_heads' not in attn:
|
| 83 |
+
raise ValueError("Number of heads must be provided to initialize hybrid attention layers")
|
| 84 |
+
attn['num_kv_heads'] = attn.get('num_kv_heads', attn['num_heads'])
|
| 85 |
+
attn['qkv_bias'] = attn.get('qkv_bias', False)
|
| 86 |
+
attn['window_size'] = attn.get('window_size', None)
|
| 87 |
+
attn['rope_theta'] = attn.get('rope_theta', 10000.)
|
| 88 |
+
|
| 89 |
+
super().__init__(
|
| 90 |
+
pad_token_id=pad_token_id,
|
| 91 |
+
bos_token_id=bos_token_id,
|
| 92 |
+
eos_token_id=eos_token_id,
|
| 93 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 94 |
+
**kwargs,
|
| 95 |
+
)
|
fla/models/gla/modeling_gla.py
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.utils.checkpoint
|
| 12 |
+
from transformers.generation import GenerationMixin
|
| 13 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 14 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 15 |
+
from transformers.utils import logging
|
| 16 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 17 |
+
|
| 18 |
+
from fla.layers.attn import Attention
|
| 19 |
+
from fla.layers.gla import GatedLinearAttention
|
| 20 |
+
from fla.models.gla.configuration_gla import GLAConfig
|
| 21 |
+
from fla.models.utils import Cache
|
| 22 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 23 |
+
from fla.modules import GatedMLP as GLAMLP
|
| 24 |
+
from fla.modules import RMSNorm
|
| 25 |
+
|
| 26 |
+
if TYPE_CHECKING:
|
| 27 |
+
from transformers.processing_utils import Unpack
|
| 28 |
+
|
| 29 |
+
logger = logging.get_logger(__name__)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class GLABlock(nn.Module):
|
| 33 |
+
def __init__(self, config: GLAConfig, layer_idx: int):
|
| 34 |
+
super().__init__()
|
| 35 |
+
|
| 36 |
+
self.config = config
|
| 37 |
+
self.layer_idx = layer_idx
|
| 38 |
+
|
| 39 |
+
self.attn_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 40 |
+
if config.attn is not None and layer_idx in config.attn['layers']:
|
| 41 |
+
self.attn = Attention(
|
| 42 |
+
hidden_size=config.hidden_size,
|
| 43 |
+
num_heads=config.attn['num_heads'],
|
| 44 |
+
num_kv_heads=config.attn['num_kv_heads'],
|
| 45 |
+
qkv_bias=config.attn['qkv_bias'],
|
| 46 |
+
window_size=config.attn['window_size'],
|
| 47 |
+
rope_theta=config.attn['rope_theta'],
|
| 48 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 49 |
+
layer_idx=layer_idx
|
| 50 |
+
)
|
| 51 |
+
else:
|
| 52 |
+
self.attn = GatedLinearAttention(
|
| 53 |
+
mode=config.attn_mode,
|
| 54 |
+
hidden_size=config.hidden_size,
|
| 55 |
+
expand_k=config.expand_k,
|
| 56 |
+
expand_v=config.expand_v,
|
| 57 |
+
num_heads=config.num_heads,
|
| 58 |
+
num_kv_heads=config.num_kv_heads,
|
| 59 |
+
feature_map=config.feature_map,
|
| 60 |
+
use_short_conv=config.use_short_conv,
|
| 61 |
+
conv_size=config.conv_size,
|
| 62 |
+
use_output_gate=config.use_output_gate,
|
| 63 |
+
gate_fn=config.hidden_act,
|
| 64 |
+
elementwise_affine=config.elementwise_affine,
|
| 65 |
+
norm_eps=config.norm_eps,
|
| 66 |
+
clamp_min=config.clamp_min,
|
| 67 |
+
fuse_norm=config.fuse_norm,
|
| 68 |
+
layer_idx=layer_idx
|
| 69 |
+
)
|
| 70 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 71 |
+
self.mlp = GLAMLP(
|
| 72 |
+
hidden_size=config.hidden_size,
|
| 73 |
+
hidden_ratio=config.hidden_ratio,
|
| 74 |
+
intermediate_size=config.intermediate_size,
|
| 75 |
+
hidden_act=config.hidden_act,
|
| 76 |
+
fuse_swiglu=config.fuse_swiglu
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def forward(
|
| 80 |
+
self,
|
| 81 |
+
hidden_states: torch.Tensor,
|
| 82 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 83 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 84 |
+
use_cache: Optional[bool] = False,
|
| 85 |
+
output_attentions: Optional[bool] = False,
|
| 86 |
+
**kwargs: Unpack[Dict]
|
| 87 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 88 |
+
residual = hidden_states
|
| 89 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 90 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 91 |
+
hidden_states=hidden_states,
|
| 92 |
+
attention_mask=attention_mask,
|
| 93 |
+
past_key_values=past_key_values,
|
| 94 |
+
use_cache=use_cache,
|
| 95 |
+
output_attentions=output_attentions,
|
| 96 |
+
**kwargs
|
| 97 |
+
)
|
| 98 |
+
if self.config.fuse_norm:
|
| 99 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 100 |
+
else:
|
| 101 |
+
hidden_states = residual + hidden_states
|
| 102 |
+
residual = hidden_states
|
| 103 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 104 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 105 |
+
hidden_states = residual + hidden_states
|
| 106 |
+
|
| 107 |
+
outputs = (hidden_states, attentions, past_key_values)
|
| 108 |
+
|
| 109 |
+
return outputs
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class GLAPreTrainedModel(PreTrainedModel):
|
| 113 |
+
|
| 114 |
+
config_class = GLAConfig
|
| 115 |
+
base_model_prefix = 'model'
|
| 116 |
+
supports_gradient_checkpointing = True
|
| 117 |
+
_no_split_modules = ['GLABlock']
|
| 118 |
+
_supports_cache_class = True
|
| 119 |
+
|
| 120 |
+
def __init__(self, *inputs, **kwargs):
|
| 121 |
+
super().__init__(*inputs, **kwargs)
|
| 122 |
+
|
| 123 |
+
def _init_weights(
|
| 124 |
+
self,
|
| 125 |
+
module: nn.Module,
|
| 126 |
+
prenorm_residual_strategy: Optional[str] = 'rescale',
|
| 127 |
+
num_residuals_per_layer: int = 2,
|
| 128 |
+
):
|
| 129 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 130 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 131 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 132 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 133 |
+
if module.bias is not None:
|
| 134 |
+
nn.init.zeros_(module.bias)
|
| 135 |
+
elif isinstance(module, nn.Embedding):
|
| 136 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 137 |
+
elif hasattr(module, 'reset_parameters'):
|
| 138 |
+
module.reset_parameters()
|
| 139 |
+
|
| 140 |
+
if prenorm_residual_strategy is not None:
|
| 141 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 142 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 143 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 144 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 145 |
+
#
|
| 146 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 147 |
+
p = None
|
| 148 |
+
if hasattr(module, 'o_proj'):
|
| 149 |
+
p = module.o_proj.weight
|
| 150 |
+
elif hasattr(module, 'down_proj'):
|
| 151 |
+
p = module.down_proj.weight
|
| 152 |
+
if p is not None:
|
| 153 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 154 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 155 |
+
# We need to reinit p since this code could be called multiple times
|
| 156 |
+
# Having just p *= scale would repeatedly scale it down
|
| 157 |
+
if prenorm_residual_strategy == 'rescale':
|
| 158 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 159 |
+
with torch.no_grad():
|
| 160 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 161 |
+
elif prenorm_residual_strategy == 'zero':
|
| 162 |
+
nn.init.zeros_(p)
|
| 163 |
+
else:
|
| 164 |
+
raise ValueError(f"Invalid prenorm_residual_strategy: {prenorm_residual_strategy}")
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
class GLAModel(GLAPreTrainedModel):
|
| 168 |
+
|
| 169 |
+
def __init__(self, config: GLAConfig):
|
| 170 |
+
super().__init__(config)
|
| 171 |
+
self.padding_idx = config.pad_token_id
|
| 172 |
+
self.vocab_size = config.vocab_size
|
| 173 |
+
|
| 174 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 175 |
+
self.layers = nn.ModuleList([GLABlock(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 176 |
+
self.norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 177 |
+
|
| 178 |
+
self.gradient_checkpointing = False
|
| 179 |
+
|
| 180 |
+
self.post_init()
|
| 181 |
+
|
| 182 |
+
def get_input_embeddings(self):
|
| 183 |
+
return self.embeddings
|
| 184 |
+
|
| 185 |
+
def set_input_embeddings(self, value):
|
| 186 |
+
self.embeddings = value
|
| 187 |
+
|
| 188 |
+
def forward(
|
| 189 |
+
self,
|
| 190 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 191 |
+
attention_mask: Optional[torch.Tensor] = None, # noqa
|
| 192 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 193 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 194 |
+
use_cache: Optional[bool] = None,
|
| 195 |
+
output_attentions: Optional[bool] = None,
|
| 196 |
+
output_hidden_states: Optional[bool] = None,
|
| 197 |
+
return_dict: Optional[bool] = None,
|
| 198 |
+
**kwargs: Unpack[Dict]
|
| 199 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 200 |
+
if output_attentions:
|
| 201 |
+
warnings.warn("`GLAModel` does not `output_attentions` now, setting it to `False`.")
|
| 202 |
+
output_attentions = False
|
| 203 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 204 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 205 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 206 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 207 |
+
|
| 208 |
+
# retrieve input_ids and inputs_embeds
|
| 209 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 210 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 211 |
+
if input_ids is None and inputs_embeds is None:
|
| 212 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 213 |
+
|
| 214 |
+
if inputs_embeds is None:
|
| 215 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 216 |
+
hidden_states = inputs_embeds
|
| 217 |
+
|
| 218 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 219 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 220 |
+
|
| 221 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 222 |
+
logger.warning_once("`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...")
|
| 223 |
+
use_cache = False
|
| 224 |
+
|
| 225 |
+
all_hidden_states = () if output_hidden_states else None
|
| 226 |
+
all_attns = () if output_attentions else None
|
| 227 |
+
for layer in self.layers:
|
| 228 |
+
if output_hidden_states:
|
| 229 |
+
all_hidden_states += (hidden_states,)
|
| 230 |
+
|
| 231 |
+
if self.gradient_checkpointing and self.training:
|
| 232 |
+
hidden_states, attentions, past_key_values = self._gradient_checkpointing_func(
|
| 233 |
+
layer.__call__,
|
| 234 |
+
hidden_states,
|
| 235 |
+
attention_mask,
|
| 236 |
+
past_key_values,
|
| 237 |
+
use_cache,
|
| 238 |
+
output_attentions,
|
| 239 |
+
**kwargs
|
| 240 |
+
)
|
| 241 |
+
else:
|
| 242 |
+
hidden_states, attentions, past_key_values = layer(
|
| 243 |
+
hidden_states,
|
| 244 |
+
attention_mask=attention_mask,
|
| 245 |
+
past_key_values=past_key_values,
|
| 246 |
+
use_cache=use_cache,
|
| 247 |
+
output_attentions=output_attentions,
|
| 248 |
+
**kwargs
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
if output_attentions:
|
| 252 |
+
all_attns += (attentions,)
|
| 253 |
+
|
| 254 |
+
hidden_states = self.norm(hidden_states)
|
| 255 |
+
|
| 256 |
+
# add hidden states from the last decoder layer
|
| 257 |
+
if output_hidden_states:
|
| 258 |
+
all_hidden_states += (hidden_states,)
|
| 259 |
+
|
| 260 |
+
if not return_dict:
|
| 261 |
+
return tuple(i for i in [hidden_states, past_key_values, all_hidden_states, all_attns] if i is not None)
|
| 262 |
+
return BaseModelOutputWithPast(
|
| 263 |
+
last_hidden_state=hidden_states,
|
| 264 |
+
past_key_values=past_key_values,
|
| 265 |
+
hidden_states=all_hidden_states,
|
| 266 |
+
attentions=all_attns
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
class GLAForCausalLM(GLAPreTrainedModel, GenerationMixin):
|
| 271 |
+
|
| 272 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 273 |
+
|
| 274 |
+
def __init__(self, config):
|
| 275 |
+
super().__init__(config)
|
| 276 |
+
self.model = GLAModel(config)
|
| 277 |
+
self.vocab_size = config.vocab_size
|
| 278 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 279 |
+
self.criterion = None
|
| 280 |
+
|
| 281 |
+
# Initialize weights and apply final processing
|
| 282 |
+
self.post_init()
|
| 283 |
+
|
| 284 |
+
def get_input_embeddings(self):
|
| 285 |
+
return self.model.embeddings
|
| 286 |
+
|
| 287 |
+
def set_input_embeddings(self, value):
|
| 288 |
+
self.model.embeddings = value
|
| 289 |
+
|
| 290 |
+
def get_output_embeddings(self):
|
| 291 |
+
return self.lm_head
|
| 292 |
+
|
| 293 |
+
def set_output_embeddings(self, new_embeddings):
|
| 294 |
+
self.lm_head = new_embeddings
|
| 295 |
+
|
| 296 |
+
def set_decoder(self, decoder):
|
| 297 |
+
self.model = decoder
|
| 298 |
+
|
| 299 |
+
def get_decoder(self):
|
| 300 |
+
return self.model
|
| 301 |
+
|
| 302 |
+
def generate(self, *args, **kwargs):
|
| 303 |
+
try:
|
| 304 |
+
return super().generate(*args, **kwargs)
|
| 305 |
+
except AttributeError as exception:
|
| 306 |
+
if 'past_key_values' in str(exception):
|
| 307 |
+
raise AttributeError(
|
| 308 |
+
f"You tried to call `generate` with a decoding strategy that manipulates `past_key_values`, "
|
| 309 |
+
f"which is not supported for {self.__class__.__name__}. "
|
| 310 |
+
f"Try another generation strategy instead. "
|
| 311 |
+
f"For the available generation strategies, check this doc: "
|
| 312 |
+
f"https://huggingface.co/docs/transformers/en/generation_strategies#decoding-strategies"
|
| 313 |
+
)
|
| 314 |
+
else:
|
| 315 |
+
raise exception
|
| 316 |
+
|
| 317 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 318 |
+
def prepare_inputs_for_generation(
|
| 319 |
+
self,
|
| 320 |
+
input_ids: torch.LongTensor = None,
|
| 321 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 322 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 323 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 324 |
+
use_cache: bool = True,
|
| 325 |
+
logits_to_keep: Optional[int] = None,
|
| 326 |
+
**kwargs
|
| 327 |
+
):
|
| 328 |
+
# only last token for `inputs_ids` if the `past_key_values` is not empty.
|
| 329 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 330 |
+
input_ids = input_ids[:, -1:]
|
| 331 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 332 |
+
if inputs_embeds is not None and len(past_key_values) == 0:
|
| 333 |
+
model_inputs = {'inputs_embeds': inputs_embeds}
|
| 334 |
+
else:
|
| 335 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 336 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 337 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 338 |
+
# TODO: use `next_tokens` directly instead.
|
| 339 |
+
model_inputs = {'input_ids': input_ids.contiguous()}
|
| 340 |
+
|
| 341 |
+
if logits_to_keep is not None:
|
| 342 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 343 |
+
|
| 344 |
+
model_inputs.update({
|
| 345 |
+
'past_key_values': past_key_values,
|
| 346 |
+
'use_cache': use_cache,
|
| 347 |
+
'attention_mask': attention_mask,
|
| 348 |
+
})
|
| 349 |
+
return model_inputs
|
| 350 |
+
|
| 351 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 352 |
+
def forward(
|
| 353 |
+
self,
|
| 354 |
+
input_ids: torch.LongTensor = None,
|
| 355 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 356 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 357 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 358 |
+
labels: Optional[torch.LongTensor] = None,
|
| 359 |
+
use_cache: Optional[bool] = None,
|
| 360 |
+
output_attentions: Optional[bool] = None,
|
| 361 |
+
output_hidden_states: Optional[bool] = None,
|
| 362 |
+
return_dict: Optional[bool] = None,
|
| 363 |
+
logits_to_keep: Optional[int] = 0,
|
| 364 |
+
**kwargs: Unpack[Dict]
|
| 365 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 366 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 367 |
+
output_hidden_states = (
|
| 368 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 369 |
+
)
|
| 370 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 371 |
+
|
| 372 |
+
outputs = self.model(
|
| 373 |
+
input_ids=input_ids,
|
| 374 |
+
attention_mask=attention_mask,
|
| 375 |
+
inputs_embeds=inputs_embeds,
|
| 376 |
+
past_key_values=past_key_values,
|
| 377 |
+
use_cache=use_cache,
|
| 378 |
+
output_attentions=output_attentions,
|
| 379 |
+
output_hidden_states=output_hidden_states,
|
| 380 |
+
return_dict=return_dict,
|
| 381 |
+
**kwargs
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
hidden_states = outputs[0]
|
| 385 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 386 |
+
|
| 387 |
+
loss, logits = None, None
|
| 388 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 389 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 390 |
+
if labels is not None:
|
| 391 |
+
if getattr(self, 'criterion', None) is None:
|
| 392 |
+
if fuse_linear_and_cross_entropy:
|
| 393 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 394 |
+
elif self.config.fuse_cross_entropy:
|
| 395 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 396 |
+
else:
|
| 397 |
+
criterion = nn.CrossEntropyLoss()
|
| 398 |
+
else:
|
| 399 |
+
criterion = self.criterion
|
| 400 |
+
labels = labels.to(hidden_states.device)
|
| 401 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 402 |
+
if fuse_linear_and_cross_entropy:
|
| 403 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 404 |
+
else:
|
| 405 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 406 |
+
|
| 407 |
+
if not return_dict:
|
| 408 |
+
output = (logits,) + outputs[1:]
|
| 409 |
+
return (loss,) + output if loss is not None else output
|
| 410 |
+
|
| 411 |
+
return CausalLMOutputWithPast(
|
| 412 |
+
loss=loss,
|
| 413 |
+
logits=logits,
|
| 414 |
+
past_key_values=outputs.past_key_values,
|
| 415 |
+
hidden_states=outputs.hidden_states,
|
| 416 |
+
attentions=outputs.attentions,
|
| 417 |
+
)
|
fla/models/linear_attn/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.linear_attn.configuration_linear_attn import LinearAttentionConfig
|
| 6 |
+
from fla.models.linear_attn.modeling_linear_attn import LinearAttentionForCausalLM, LinearAttentionModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(LinearAttentionConfig.model_type, LinearAttentionConfig)
|
| 9 |
+
AutoModel.register(LinearAttentionConfig, LinearAttentionModel)
|
| 10 |
+
AutoModelForCausalLM.register(LinearAttentionConfig, LinearAttentionForCausalLM)
|
| 11 |
+
|
| 12 |
+
__all__ = ['LinearAttentionConfig', 'LinearAttentionForCausalLM', 'LinearAttentionModel']
|
fla/models/linear_attn/configuration_linear_attn.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Dict, Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class LinearAttentionConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'linear_attn'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
attn_mode: str = "fused_chunk",
|
| 16 |
+
hidden_size: int = 2048,
|
| 17 |
+
expand_k: int = 1,
|
| 18 |
+
expand_v: int = 1,
|
| 19 |
+
hidden_ratio: Optional[int] = 4,
|
| 20 |
+
intermediate_size: Optional[int] = None,
|
| 21 |
+
num_hidden_layers: int = 24,
|
| 22 |
+
num_heads: int = 4,
|
| 23 |
+
num_kv_heads: Optional[int] = None,
|
| 24 |
+
feature_map: str = "elementwise_product",
|
| 25 |
+
tie_feature_map_qk: bool = False,
|
| 26 |
+
norm_q: bool = False,
|
| 27 |
+
norm_k: bool = False,
|
| 28 |
+
norm_feature_map: bool = False,
|
| 29 |
+
hidden_act: str = "swish",
|
| 30 |
+
max_position_embeddings: int = 2048,
|
| 31 |
+
elementwise_affine: Optional[bool] = True,
|
| 32 |
+
norm_eps: float = 1e-6,
|
| 33 |
+
attn: Optional[Dict] = None,
|
| 34 |
+
use_cache: bool = True,
|
| 35 |
+
pad_token_id: int = None,
|
| 36 |
+
bos_token_id: int = 1,
|
| 37 |
+
eos_token_id: int = 2,
|
| 38 |
+
tie_word_embeddings: bool = False,
|
| 39 |
+
initializer_range: float = 0.006,
|
| 40 |
+
fuse_norm: bool = True,
|
| 41 |
+
fuse_swiglu: bool = True,
|
| 42 |
+
fuse_cross_entropy: bool = True,
|
| 43 |
+
vocab_size: int = 32000,
|
| 44 |
+
**kwargs
|
| 45 |
+
):
|
| 46 |
+
self.attn_mode = attn_mode
|
| 47 |
+
self.hidden_size = hidden_size
|
| 48 |
+
self.expand_k = expand_k
|
| 49 |
+
self.expand_v = expand_v
|
| 50 |
+
self.hidden_ratio = hidden_ratio
|
| 51 |
+
self.intermediate_size = intermediate_size
|
| 52 |
+
self.num_hidden_layers = num_hidden_layers
|
| 53 |
+
self.num_heads = num_heads
|
| 54 |
+
self.num_kv_heads = num_kv_heads
|
| 55 |
+
self.feature_map = feature_map
|
| 56 |
+
self.tie_feature_map_qk = tie_feature_map_qk
|
| 57 |
+
self.norm_q = norm_q
|
| 58 |
+
self.norm_k = norm_k
|
| 59 |
+
self.norm_feature_map = norm_feature_map
|
| 60 |
+
self.hidden_act = hidden_act
|
| 61 |
+
self.max_position_embeddings = max_position_embeddings
|
| 62 |
+
self.elementwise_affine = elementwise_affine
|
| 63 |
+
self.norm_eps = norm_eps
|
| 64 |
+
self.attn = attn
|
| 65 |
+
self.use_cache = use_cache
|
| 66 |
+
self.initializer_range = initializer_range
|
| 67 |
+
|
| 68 |
+
self.fuse_norm = fuse_norm
|
| 69 |
+
self.fuse_swiglu = fuse_swiglu
|
| 70 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 71 |
+
self.vocab_size = vocab_size
|
| 72 |
+
|
| 73 |
+
if attn is not None:
|
| 74 |
+
if not isinstance(attn, Dict):
|
| 75 |
+
raise ValueError("attn must be a dictionary")
|
| 76 |
+
if 'layers' not in attn:
|
| 77 |
+
raise ValueError("Layer indices must be provided to initialize hybrid attention layers")
|
| 78 |
+
if 'num_heads' not in attn:
|
| 79 |
+
raise ValueError("Number of heads must be provided to initialize hybrid attention layers")
|
| 80 |
+
attn['num_kv_heads'] = attn.get('num_kv_heads', attn['num_heads'])
|
| 81 |
+
attn['qkv_bias'] = attn.get('qkv_bias', False)
|
| 82 |
+
attn['window_size'] = attn.get('window_size', None)
|
| 83 |
+
attn['rope_theta'] = attn.get('rope_theta', 10000.)
|
| 84 |
+
|
| 85 |
+
super().__init__(
|
| 86 |
+
pad_token_id=pad_token_id,
|
| 87 |
+
bos_token_id=bos_token_id,
|
| 88 |
+
eos_token_id=eos_token_id,
|
| 89 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 90 |
+
**kwargs,
|
| 91 |
+
)
|
fla/models/linear_attn/modeling_linear_attn.py
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import List, Optional, Tuple, Union
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.utils.checkpoint
|
| 12 |
+
from transformers.generation import GenerationMixin
|
| 13 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 14 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 15 |
+
from transformers.utils import logging
|
| 16 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 17 |
+
|
| 18 |
+
from fla.layers.attn import Attention
|
| 19 |
+
from fla.layers.linear_attn import LinearAttention
|
| 20 |
+
from fla.models.linear_attn.configuration_linear_attn import LinearAttentionConfig
|
| 21 |
+
from fla.models.utils import Cache
|
| 22 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 23 |
+
from fla.modules import GatedMLP as LinearAttentionMLP
|
| 24 |
+
from fla.modules import RMSNorm
|
| 25 |
+
|
| 26 |
+
logger = logging.get_logger(__name__)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class LinearAttentionBlock(nn.Module):
|
| 30 |
+
def __init__(self, config: LinearAttentionConfig, layer_idx: int):
|
| 31 |
+
super().__init__()
|
| 32 |
+
|
| 33 |
+
self.config = config
|
| 34 |
+
self.layer_idx = layer_idx
|
| 35 |
+
|
| 36 |
+
self.attn_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 37 |
+
if config.attn is not None and layer_idx in config.attn['layers']:
|
| 38 |
+
self.attn = Attention(
|
| 39 |
+
hidden_size=config.hidden_size,
|
| 40 |
+
num_heads=config.attn['num_heads'],
|
| 41 |
+
num_kv_heads=config.attn['num_kv_heads'],
|
| 42 |
+
qkv_bias=config.attn['qkv_bias'],
|
| 43 |
+
window_size=config.attn['window_size'],
|
| 44 |
+
rope_theta=config.attn['rope_theta'],
|
| 45 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 46 |
+
layer_idx=layer_idx
|
| 47 |
+
)
|
| 48 |
+
else:
|
| 49 |
+
self.attn = LinearAttention(
|
| 50 |
+
mode=config.attn_mode,
|
| 51 |
+
hidden_size=config.hidden_size,
|
| 52 |
+
expand_k=config.expand_k,
|
| 53 |
+
expand_v=config.expand_v,
|
| 54 |
+
num_heads=config.num_heads,
|
| 55 |
+
num_kv_heads=config.num_kv_heads,
|
| 56 |
+
feature_map=config.feature_map,
|
| 57 |
+
tie_feature_map_qk=config.tie_feature_map_qk,
|
| 58 |
+
norm_q=config.norm_q,
|
| 59 |
+
norm_k=config.norm_k,
|
| 60 |
+
do_feature_map_norm=config.norm_feature_map,
|
| 61 |
+
elementwise_affine=config.elementwise_affine,
|
| 62 |
+
norm_eps=config.norm_eps,
|
| 63 |
+
layer_idx=layer_idx
|
| 64 |
+
)
|
| 65 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 66 |
+
self.mlp = LinearAttentionMLP(
|
| 67 |
+
hidden_size=config.hidden_size,
|
| 68 |
+
hidden_ratio=config.hidden_ratio,
|
| 69 |
+
intermediate_size=config.intermediate_size,
|
| 70 |
+
hidden_act=config.hidden_act,
|
| 71 |
+
fuse_swiglu=config.fuse_swiglu
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
def forward(
|
| 75 |
+
self,
|
| 76 |
+
hidden_states: torch.Tensor,
|
| 77 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 78 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 79 |
+
use_cache: Optional[bool] = False,
|
| 80 |
+
output_attentions: Optional[bool] = False,
|
| 81 |
+
**kwargs,
|
| 82 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 83 |
+
residual = hidden_states
|
| 84 |
+
# currently not supported
|
| 85 |
+
attentions, past_key_values = None, None
|
| 86 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 87 |
+
hidden_states = self.attn(hidden_states=hidden_states, **kwargs)
|
| 88 |
+
if self.config.fuse_norm:
|
| 89 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 90 |
+
else:
|
| 91 |
+
hidden_states = residual + hidden_states
|
| 92 |
+
residual = hidden_states
|
| 93 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 94 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 95 |
+
hidden_states = residual + hidden_states
|
| 96 |
+
|
| 97 |
+
outputs = (hidden_states, attentions, past_key_values)
|
| 98 |
+
|
| 99 |
+
return outputs
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class LinearAttentionPreTrainedModel(PreTrainedModel):
|
| 103 |
+
|
| 104 |
+
config_class = LinearAttentionConfig
|
| 105 |
+
base_model_prefix = 'model'
|
| 106 |
+
supports_gradient_checkpointing = True
|
| 107 |
+
_no_split_modules = ['LinearAttentionBlock']
|
| 108 |
+
_supports_cache_class = True
|
| 109 |
+
|
| 110 |
+
def __init__(self, *inputs, **kwargs):
|
| 111 |
+
super().__init__(*inputs, **kwargs)
|
| 112 |
+
|
| 113 |
+
def _init_weights(
|
| 114 |
+
self,
|
| 115 |
+
module: nn.Module,
|
| 116 |
+
prenorm_residual_strategy: Optional[str] = 'rescale',
|
| 117 |
+
num_residuals_per_layer: int = 2,
|
| 118 |
+
):
|
| 119 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 120 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 121 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 122 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 123 |
+
if module.bias is not None:
|
| 124 |
+
nn.init.zeros_(module.bias)
|
| 125 |
+
elif isinstance(module, nn.Embedding):
|
| 126 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 127 |
+
elif hasattr(module, 'reset_parameters'):
|
| 128 |
+
module.reset_parameters()
|
| 129 |
+
|
| 130 |
+
if prenorm_residual_strategy is not None:
|
| 131 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 132 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 133 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 134 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 135 |
+
#
|
| 136 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 137 |
+
p = None
|
| 138 |
+
if hasattr(module, 'o_proj'):
|
| 139 |
+
p = module.o_proj.weight
|
| 140 |
+
elif hasattr(module, 'down_proj'):
|
| 141 |
+
p = module.down_proj.weight
|
| 142 |
+
if p is not None:
|
| 143 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 144 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 145 |
+
# We need to reinit p since this code could be called multiple times
|
| 146 |
+
# Having just p *= scale would repeatedly scale it down
|
| 147 |
+
if prenorm_residual_strategy == 'rescale':
|
| 148 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 149 |
+
with torch.no_grad():
|
| 150 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 151 |
+
elif prenorm_residual_strategy == 'zero':
|
| 152 |
+
nn.init.zeros_(p)
|
| 153 |
+
else:
|
| 154 |
+
raise ValueError(f"Invalid prenorm_residual_strategy: {prenorm_residual_strategy}")
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
class LinearAttentionModel(LinearAttentionPreTrainedModel):
|
| 158 |
+
|
| 159 |
+
def __init__(self, config: LinearAttentionConfig):
|
| 160 |
+
super().__init__(config)
|
| 161 |
+
self.padding_idx = config.pad_token_id
|
| 162 |
+
self.vocab_size = config.vocab_size
|
| 163 |
+
|
| 164 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 165 |
+
self.layers = nn.ModuleList([LinearAttentionBlock(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 166 |
+
self.norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 167 |
+
|
| 168 |
+
self.gradient_checkpointing = False
|
| 169 |
+
|
| 170 |
+
self.post_init()
|
| 171 |
+
|
| 172 |
+
def get_input_embeddings(self):
|
| 173 |
+
return self.embeddings
|
| 174 |
+
|
| 175 |
+
def set_input_embeddings(self, value):
|
| 176 |
+
self.embeddings = value
|
| 177 |
+
|
| 178 |
+
def forward(
|
| 179 |
+
self,
|
| 180 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 181 |
+
attention_mask: Optional[torch.Tensor] = None, # noqa
|
| 182 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 183 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 184 |
+
use_cache: Optional[bool] = None,
|
| 185 |
+
output_attentions: Optional[bool] = None,
|
| 186 |
+
output_hidden_states: Optional[bool] = None,
|
| 187 |
+
return_dict: Optional[bool] = None
|
| 188 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 189 |
+
if output_attentions:
|
| 190 |
+
warnings.warn(
|
| 191 |
+
"`LinearAttentionModel` does not support output attention weights now, "
|
| 192 |
+
"so `output_attentions` is set to `False`."
|
| 193 |
+
)
|
| 194 |
+
output_attentions = False
|
| 195 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 196 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 197 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 198 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 199 |
+
|
| 200 |
+
# retrieve input_ids and inputs_embeds
|
| 201 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 202 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 203 |
+
if input_ids is None and inputs_embeds is None:
|
| 204 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 205 |
+
|
| 206 |
+
if inputs_embeds is None:
|
| 207 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 208 |
+
hidden_states = inputs_embeds
|
| 209 |
+
|
| 210 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 211 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 212 |
+
|
| 213 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 214 |
+
logger.warning_once("`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...")
|
| 215 |
+
use_cache = False
|
| 216 |
+
|
| 217 |
+
all_hidden_states = () if output_hidden_states else None
|
| 218 |
+
all_attns = () if output_attentions else None
|
| 219 |
+
|
| 220 |
+
for i, layer in enumerate(self.layers):
|
| 221 |
+
if output_hidden_states:
|
| 222 |
+
all_hidden_states += (hidden_states,)
|
| 223 |
+
|
| 224 |
+
if self.gradient_checkpointing and self.training:
|
| 225 |
+
hidden_states, attentions, past_key_values = self._gradient_checkpointing_func(
|
| 226 |
+
layer.__call__,
|
| 227 |
+
hidden_states,
|
| 228 |
+
attention_mask,
|
| 229 |
+
past_key_values,
|
| 230 |
+
use_cache,
|
| 231 |
+
output_attentions,
|
| 232 |
+
)
|
| 233 |
+
else:
|
| 234 |
+
hidden_states, attentions, past_key_values = layer(
|
| 235 |
+
hidden_states,
|
| 236 |
+
attention_mask=attention_mask,
|
| 237 |
+
past_key_values=past_key_values,
|
| 238 |
+
use_cache=use_cache,
|
| 239 |
+
output_attentions=output_attentions
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
if output_attentions:
|
| 243 |
+
all_attns += (attentions,)
|
| 244 |
+
|
| 245 |
+
hidden_states = self.norm(hidden_states)
|
| 246 |
+
|
| 247 |
+
# add hidden states from the last decoder layer
|
| 248 |
+
if output_hidden_states:
|
| 249 |
+
all_hidden_states += (hidden_states,)
|
| 250 |
+
|
| 251 |
+
if not return_dict:
|
| 252 |
+
return tuple(i for i in [hidden_states, past_key_values, all_hidden_states, all_attns] if i is not None)
|
| 253 |
+
return BaseModelOutputWithPast(
|
| 254 |
+
last_hidden_state=hidden_states,
|
| 255 |
+
past_key_values=past_key_values,
|
| 256 |
+
hidden_states=all_hidden_states,
|
| 257 |
+
attentions=all_attns
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
class LinearAttentionForCausalLM(LinearAttentionPreTrainedModel, GenerationMixin):
|
| 262 |
+
|
| 263 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 264 |
+
|
| 265 |
+
def __init__(self, config):
|
| 266 |
+
super().__init__(config)
|
| 267 |
+
self.model = LinearAttentionModel(config)
|
| 268 |
+
self.vocab_size = config.vocab_size
|
| 269 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 270 |
+
self.criterion = None
|
| 271 |
+
|
| 272 |
+
# Initialize weights and apply final processing
|
| 273 |
+
self.post_init()
|
| 274 |
+
|
| 275 |
+
def get_input_embeddings(self):
|
| 276 |
+
return self.model.embeddings
|
| 277 |
+
|
| 278 |
+
def set_input_embeddings(self, value):
|
| 279 |
+
self.model.embeddings = value
|
| 280 |
+
|
| 281 |
+
def get_output_embeddings(self):
|
| 282 |
+
return self.lm_head
|
| 283 |
+
|
| 284 |
+
def set_output_embeddings(self, new_embeddings):
|
| 285 |
+
self.lm_head = new_embeddings
|
| 286 |
+
|
| 287 |
+
def set_decoder(self, decoder):
|
| 288 |
+
self.model = decoder
|
| 289 |
+
|
| 290 |
+
def get_decoder(self):
|
| 291 |
+
return self.model
|
| 292 |
+
|
| 293 |
+
def generate(self, *args, **kwargs):
|
| 294 |
+
try:
|
| 295 |
+
return super().generate(*args, **kwargs)
|
| 296 |
+
except AttributeError as exception:
|
| 297 |
+
if 'past_key_values' in str(exception):
|
| 298 |
+
raise AttributeError(
|
| 299 |
+
f"You tried to call `generate` with a decoding strategy that manipulates `past_key_values`, "
|
| 300 |
+
f"which is not supported for {self.__class__.__name__}. "
|
| 301 |
+
f"Try another generation strategy instead. "
|
| 302 |
+
f"For the available generation strategies, check this doc: "
|
| 303 |
+
f"https://huggingface.co/docs/transformers/en/generation_strategies#decoding-strategies"
|
| 304 |
+
)
|
| 305 |
+
else:
|
| 306 |
+
raise exception
|
| 307 |
+
|
| 308 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 309 |
+
def prepare_inputs_for_generation(
|
| 310 |
+
self,
|
| 311 |
+
input_ids: torch.LongTensor = None,
|
| 312 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 313 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 314 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 315 |
+
use_cache: bool = True,
|
| 316 |
+
logits_to_keep: Optional[int] = None,
|
| 317 |
+
**kwargs
|
| 318 |
+
):
|
| 319 |
+
# only last token for `inputs_ids` if the `past_key_values` is not empty.
|
| 320 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 321 |
+
input_ids = input_ids[:, -1:]
|
| 322 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 323 |
+
if inputs_embeds is not None and len(past_key_values) == 0:
|
| 324 |
+
model_inputs = {'inputs_embeds': inputs_embeds}
|
| 325 |
+
else:
|
| 326 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 327 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 328 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 329 |
+
# TODO: use `next_tokens` directly instead.
|
| 330 |
+
model_inputs = {'input_ids': input_ids.contiguous()}
|
| 331 |
+
|
| 332 |
+
if logits_to_keep is not None:
|
| 333 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 334 |
+
|
| 335 |
+
model_inputs.update({
|
| 336 |
+
'past_key_values': past_key_values,
|
| 337 |
+
'use_cache': use_cache,
|
| 338 |
+
'attention_mask': attention_mask,
|
| 339 |
+
})
|
| 340 |
+
return model_inputs
|
| 341 |
+
|
| 342 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 343 |
+
def forward(
|
| 344 |
+
self,
|
| 345 |
+
input_ids: torch.LongTensor = None,
|
| 346 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 347 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 348 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 349 |
+
labels: Optional[torch.LongTensor] = None,
|
| 350 |
+
use_cache: Optional[bool] = None,
|
| 351 |
+
output_attentions: Optional[bool] = None,
|
| 352 |
+
output_hidden_states: Optional[bool] = None,
|
| 353 |
+
return_dict: Optional[bool] = None,
|
| 354 |
+
logits_to_keep: Optional[int] = 0
|
| 355 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 356 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 357 |
+
output_hidden_states = (
|
| 358 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 359 |
+
)
|
| 360 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 361 |
+
|
| 362 |
+
outputs = self.model(
|
| 363 |
+
input_ids=input_ids,
|
| 364 |
+
attention_mask=attention_mask,
|
| 365 |
+
inputs_embeds=inputs_embeds,
|
| 366 |
+
past_key_values=past_key_values,
|
| 367 |
+
use_cache=use_cache,
|
| 368 |
+
output_attentions=output_attentions,
|
| 369 |
+
output_hidden_states=output_hidden_states,
|
| 370 |
+
return_dict=return_dict
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
hidden_states = outputs[0]
|
| 374 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 375 |
+
|
| 376 |
+
loss, logits = None, None
|
| 377 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 378 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 379 |
+
if labels is not None:
|
| 380 |
+
if getattr(self, 'criterion', None) is None:
|
| 381 |
+
if fuse_linear_and_cross_entropy:
|
| 382 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 383 |
+
elif self.config.fuse_cross_entropy:
|
| 384 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 385 |
+
else:
|
| 386 |
+
criterion = nn.CrossEntropyLoss()
|
| 387 |
+
else:
|
| 388 |
+
criterion = self.criterion
|
| 389 |
+
labels = labels.to(hidden_states.device)
|
| 390 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 391 |
+
if fuse_linear_and_cross_entropy:
|
| 392 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 393 |
+
else:
|
| 394 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 395 |
+
|
| 396 |
+
if not return_dict:
|
| 397 |
+
output = (logits,) + outputs[1:]
|
| 398 |
+
return (loss,) + output if loss is not None else output
|
| 399 |
+
|
| 400 |
+
return CausalLMOutputWithPast(
|
| 401 |
+
loss=loss,
|
| 402 |
+
logits=logits,
|
| 403 |
+
past_key_values=outputs.past_key_values,
|
| 404 |
+
hidden_states=outputs.hidden_states,
|
| 405 |
+
attentions=outputs.attentions,
|
| 406 |
+
)
|
fla/models/mamba2/modeling_mamba2.py
ADDED
|
@@ -0,0 +1,1093 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 state-spaces/mamba2 org and HuggingFace Inc. team.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""PyTorch MAMBA2 model."""
|
| 15 |
+
|
| 16 |
+
import math
|
| 17 |
+
import warnings
|
| 18 |
+
from dataclasses import dataclass
|
| 19 |
+
from typing import Optional, Tuple, Union
|
| 20 |
+
|
| 21 |
+
import torch
|
| 22 |
+
import torch.utils.checkpoint
|
| 23 |
+
from torch import nn
|
| 24 |
+
from transformers.activations import ACT2FN
|
| 25 |
+
from transformers.generation import GenerationMixin
|
| 26 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 27 |
+
from transformers.utils import ModelOutput, logging
|
| 28 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 29 |
+
|
| 30 |
+
from fla.models.mamba2.configuration_mamba2 import Mamba2Config
|
| 31 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss, RMSNorm
|
| 32 |
+
from fla.modules.layernorm_gated import RMSNormGated
|
| 33 |
+
|
| 34 |
+
logger = logging.get_logger(__name__)
|
| 35 |
+
|
| 36 |
+
with warnings.catch_warnings():
|
| 37 |
+
warnings.simplefilter('ignore')
|
| 38 |
+
try:
|
| 39 |
+
from mamba_ssm.ops.triton.selective_state_update import selective_state_update
|
| 40 |
+
from mamba_ssm.ops.triton.ssd_combined import mamba_chunk_scan_combined, mamba_split_conv1d_scan_combined
|
| 41 |
+
except ImportError:
|
| 42 |
+
(
|
| 43 |
+
selective_state_update,
|
| 44 |
+
mamba_chunk_scan_combined,
|
| 45 |
+
mamba_split_conv1d_scan_combined,
|
| 46 |
+
) = (None, None, None)
|
| 47 |
+
try:
|
| 48 |
+
from causal_conv1d import causal_conv1d_fn, causal_conv1d_update
|
| 49 |
+
except ImportError:
|
| 50 |
+
causal_conv1d_update, causal_conv1d_fn = None, None
|
| 51 |
+
is_fast_path_available = all((
|
| 52 |
+
selective_state_update,
|
| 53 |
+
causal_conv1d_fn,
|
| 54 |
+
causal_conv1d_update
|
| 55 |
+
))
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def pad_tensor_by_size(input_tensor: torch.Tensor, pad_size: int):
|
| 59 |
+
"""
|
| 60 |
+
Padding x tensor with `pad_size` on the seq_len dim (dim=1)
|
| 61 |
+
|
| 62 |
+
Assumes that we only have tensors of either size 4 or 3
|
| 63 |
+
"""
|
| 64 |
+
pad_shape = (0, 0, 0, 0, 0, pad_size, 0, 0) if len(input_tensor.shape) == 4 else (0, 0, 0, pad_size, 0, 0)
|
| 65 |
+
|
| 66 |
+
return torch.nn.functional.pad(input_tensor, pad_shape, mode="constant", value=0)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def reshape_into_chunks(input_tensor, pad_size, chunk_size):
|
| 70 |
+
"""
|
| 71 |
+
Padding input_tensor with `pad_size` on the seq_len dim (dim=1) and
|
| 72 |
+
simultaneously splitting it into chunk sequences.
|
| 73 |
+
|
| 74 |
+
Assumes that we only have tensors of either size 4 or 3
|
| 75 |
+
"""
|
| 76 |
+
# [bsz, seq_len, ...] -> [bsz, seq_len multiple of chunk_size, ...]
|
| 77 |
+
input_tensor = pad_tensor_by_size(input_tensor, pad_size)
|
| 78 |
+
|
| 79 |
+
if len(input_tensor.shape) == 3:
|
| 80 |
+
# [bsz, seq_len multiple of chunk_size, num_heads] -> [bsz, -1, chunk_size, num_heads]
|
| 81 |
+
return input_tensor.reshape(input_tensor.shape[0], -1, chunk_size, input_tensor.shape[2])
|
| 82 |
+
else:
|
| 83 |
+
# [bsz, seq_len multiple of chunk_size, num_heads, head_dim or state_size] ->
|
| 84 |
+
# [bsz, -1, chunk_size, num_heads, head_dim or state_size]
|
| 85 |
+
return input_tensor.reshape(
|
| 86 |
+
input_tensor.shape[0], -1, chunk_size, input_tensor.shape[2], input_tensor.shape[3]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def segment_sum(input_tensor):
|
| 91 |
+
"""
|
| 92 |
+
More stable segment sum calculation. Uses cumulative sums and masking instead of direct subtractions.
|
| 93 |
+
"""
|
| 94 |
+
chunk_size = input_tensor.size(-1)
|
| 95 |
+
# 1. expand input tensor to have an additional dimension and repeat along that dimension
|
| 96 |
+
# [..., chunk_size] -> [..., chunk_size, chunk_size]
|
| 97 |
+
input_tensor = input_tensor[..., None].expand(*input_tensor.size(), chunk_size)
|
| 98 |
+
# 2. create a lower triangular mask with the diagonal set to 0 to 0 out elements above diag
|
| 99 |
+
mask = torch.tril(torch.ones(chunk_size, chunk_size, device=input_tensor.device, dtype=torch.bool), diagonal=-1)
|
| 100 |
+
input_tensor = input_tensor.masked_fill(~mask, 0)
|
| 101 |
+
# 3. compute actual cumsum
|
| 102 |
+
tensor_segsum = torch.cumsum(input_tensor, dim=-2)
|
| 103 |
+
|
| 104 |
+
# 4. apply mask to keep only the lower triangular part of the cumulative sum result (incl diagonal this time)
|
| 105 |
+
mask = torch.tril(torch.ones(chunk_size, chunk_size, device=input_tensor.device, dtype=torch.bool), diagonal=0)
|
| 106 |
+
tensor_segsum = tensor_segsum.masked_fill(~mask, -torch.inf)
|
| 107 |
+
return tensor_segsum
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def apply_mask_to_padding_states(hidden_states, attention_mask):
|
| 111 |
+
"""
|
| 112 |
+
Tunes out the hidden states for padding tokens, see https://github.com/state-spaces/mamba/issues/66
|
| 113 |
+
"""
|
| 114 |
+
if attention_mask is not None and attention_mask.shape[1] > 1 and attention_mask.shape[0] > 1:
|
| 115 |
+
dtype = hidden_states.dtype
|
| 116 |
+
hidden_states = (hidden_states * attention_mask[:, :, None]).to(dtype)
|
| 117 |
+
|
| 118 |
+
return hidden_states
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
class Mamba2Cache:
|
| 122 |
+
"""
|
| 123 |
+
Arguments:
|
| 124 |
+
config: Mamba2Config
|
| 125 |
+
batch_size: int
|
| 126 |
+
dtype: torch.dtype
|
| 127 |
+
device: torch.device
|
| 128 |
+
|
| 129 |
+
Attributes:
|
| 130 |
+
dtype: (`torch.dtype`):
|
| 131 |
+
The default `dtype` used to initializing the cache.
|
| 132 |
+
conv_kernel_size: (`int`):
|
| 133 |
+
Model's convolution kernel size taken from config.
|
| 134 |
+
n_groups: (`int`):
|
| 135 |
+
Model's number of groups taken from the config - similar to tensor parallel in Transformer.
|
| 136 |
+
state_size: (`int`):
|
| 137 |
+
Model's SSM state size taken from config.
|
| 138 |
+
num_heads: (`int`):
|
| 139 |
+
The number of heads used in the linear attention / SSM.
|
| 140 |
+
head_dim: (`int`):
|
| 141 |
+
The respective dimension of the heads used in the linear attention / SSM.
|
| 142 |
+
intermediate_size: (`int`):
|
| 143 |
+
Model's intermediate_size based on (expand * hidden_dim) from config.
|
| 144 |
+
conv_states: (`torch.Tensor`):
|
| 145 |
+
A tensor of shape `[num_layers, batch_size, conv_kernel_size, intermediate_size + 2 * n_groups * state_size]`
|
| 146 |
+
that holds convolutional states.
|
| 147 |
+
ssm_states: (`torch.Tensor`):
|
| 148 |
+
A tensor of shape `[num_layers, batch_size, num_heads, head_dim, state_size]` that holds ssm states.
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
def __init__(
|
| 152 |
+
self,
|
| 153 |
+
config: Mamba2Config,
|
| 154 |
+
batch_size: int,
|
| 155 |
+
dtype: torch.dtype = torch.float16,
|
| 156 |
+
device: Optional[str] = None,
|
| 157 |
+
):
|
| 158 |
+
self.dtype = dtype
|
| 159 |
+
self.conv_kernel_size = config.conv_kernel
|
| 160 |
+
self.n_groups = config.n_groups
|
| 161 |
+
self.state_size = config.state_size
|
| 162 |
+
self.num_heads = config.num_heads
|
| 163 |
+
self.head_dim = config.head_dim
|
| 164 |
+
self.intermediate_size = int(config.expand * config.hidden_size)
|
| 165 |
+
|
| 166 |
+
self.conv_states = torch.zeros(
|
| 167 |
+
config.num_hidden_layers,
|
| 168 |
+
batch_size,
|
| 169 |
+
self.intermediate_size + 2 * self.n_groups * self.state_size,
|
| 170 |
+
self.conv_kernel_size,
|
| 171 |
+
device=device,
|
| 172 |
+
dtype=dtype,
|
| 173 |
+
)
|
| 174 |
+
self.ssm_states = torch.zeros(
|
| 175 |
+
config.num_hidden_layers,
|
| 176 |
+
batch_size,
|
| 177 |
+
self.num_heads,
|
| 178 |
+
self.head_dim,
|
| 179 |
+
self.state_size,
|
| 180 |
+
device=device,
|
| 181 |
+
dtype=dtype,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
def update_conv_state(
|
| 185 |
+
self,
|
| 186 |
+
layer_idx: int,
|
| 187 |
+
new_conv_state: torch.Tensor,
|
| 188 |
+
cache_init: bool = False
|
| 189 |
+
) -> torch.Tensor:
|
| 190 |
+
if cache_init:
|
| 191 |
+
self.conv_states[layer_idx] = new_conv_state.to(self.conv_states.device)
|
| 192 |
+
else:
|
| 193 |
+
self.conv_states[layer_idx] = self.conv_states[layer_idx].roll(shifts=-1, dims=-1)
|
| 194 |
+
self.conv_states[layer_idx][:, :, -1] = new_conv_state[:, 0, :].to(self.conv_states.device)
|
| 195 |
+
return self.conv_states[layer_idx]
|
| 196 |
+
|
| 197 |
+
def update_ssm_state(self, layer_idx: int, new_ssm_state: torch.Tensor):
|
| 198 |
+
self.ssm_states[layer_idx] = new_ssm_state.to(self.ssm_states.device)
|
| 199 |
+
return self.ssm_states[layer_idx]
|
| 200 |
+
|
| 201 |
+
def reset(self):
|
| 202 |
+
self.conv_states.zero_()
|
| 203 |
+
self.ssm_states.zero_()
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
class Mamba2Mixer(nn.Module):
|
| 207 |
+
"""
|
| 208 |
+
Compute ∆, A, B, C, and D the state space parameters and compute the `contextualized_states`.
|
| 209 |
+
A, D are input independent (see Mamba paper [1] Section 3.5.2 "Interpretation of A" for why A isn't selective)
|
| 210 |
+
∆, B, C are input-dependent (this is a key difference between Mamba and the linear time invariant S4,
|
| 211 |
+
and is why Mamba is called **selective** state spaces)
|
| 212 |
+
"""
|
| 213 |
+
|
| 214 |
+
def __init__(self, config: Mamba2Config, layer_idx: int):
|
| 215 |
+
super().__init__()
|
| 216 |
+
self.num_heads = config.num_heads
|
| 217 |
+
self.hidden_size = config.hidden_size
|
| 218 |
+
self.ssm_state_size = config.state_size
|
| 219 |
+
self.conv_kernel_size = config.conv_kernel
|
| 220 |
+
self.intermediate_size = int(config.expand * self.hidden_size)
|
| 221 |
+
self.time_step_rank = int(config.time_step_rank)
|
| 222 |
+
self.layer_idx = layer_idx
|
| 223 |
+
self.use_conv_bias = config.use_conv_bias
|
| 224 |
+
self.activation = config.hidden_act
|
| 225 |
+
self.act = ACT2FN[config.hidden_act]
|
| 226 |
+
|
| 227 |
+
self.layer_norm_epsilon = config.layer_norm_epsilon
|
| 228 |
+
self.rms_norm = config.rms_norm
|
| 229 |
+
|
| 230 |
+
self.n_groups = config.n_groups
|
| 231 |
+
self.head_dim = config.head_dim
|
| 232 |
+
self.chunk_size = config.chunk_size
|
| 233 |
+
|
| 234 |
+
self.time_step_limit = config.time_step_limit
|
| 235 |
+
self.time_step_min = config.time_step_min
|
| 236 |
+
self.time_step_max = config.time_step_max
|
| 237 |
+
|
| 238 |
+
self.conv_dim = self.intermediate_size + 2 * self.n_groups * self.ssm_state_size
|
| 239 |
+
self.conv1d = nn.Conv1d(
|
| 240 |
+
in_channels=self.conv_dim,
|
| 241 |
+
out_channels=self.conv_dim,
|
| 242 |
+
bias=config.use_conv_bias,
|
| 243 |
+
kernel_size=config.conv_kernel,
|
| 244 |
+
groups=self.conv_dim,
|
| 245 |
+
padding=config.conv_kernel - 1,
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# projection of the input hidden states
|
| 249 |
+
projection_size = self.intermediate_size + self.conv_dim + self.num_heads
|
| 250 |
+
self.in_proj = nn.Linear(
|
| 251 |
+
self.hidden_size,
|
| 252 |
+
projection_size,
|
| 253 |
+
bias=config.use_bias,
|
| 254 |
+
)
|
| 255 |
+
# selective projection used to make dt, B and C input dependant
|
| 256 |
+
|
| 257 |
+
# time step projection (discretization)
|
| 258 |
+
# instantiate once and copy inv_dt in init_weights of PretrainedModel
|
| 259 |
+
self.dt_bias = nn.Parameter(torch.ones(self.num_heads))
|
| 260 |
+
|
| 261 |
+
# S4D real initialization. These are not discretized!
|
| 262 |
+
# The core is to load them, compute the discrete states, then write the updated state. Keeps the memory bounded
|
| 263 |
+
A = torch.arange(1, self.num_heads + 1)
|
| 264 |
+
self.A_log = nn.Parameter(torch.log(A))
|
| 265 |
+
self.A_log._no_weight_decay = True
|
| 266 |
+
self.norm = RMSNormGated(
|
| 267 |
+
self.intermediate_size, eps=self.layer_norm_epsilon, norm_before_gate=False
|
| 268 |
+
)
|
| 269 |
+
self.D = nn.Parameter(torch.ones(self.num_heads))
|
| 270 |
+
self.D._no_weight_decay = True
|
| 271 |
+
|
| 272 |
+
self.out_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=config.use_bias)
|
| 273 |
+
self.use_bias = config.use_bias
|
| 274 |
+
|
| 275 |
+
if not is_fast_path_available:
|
| 276 |
+
logger.warning_once(
|
| 277 |
+
"The fast path is not available because one of "
|
| 278 |
+
"`(selective_state_update, causal_conv1d_fn, causal_conv1d_update)` is None. "
|
| 279 |
+
"Falling back to the naive implementation. "
|
| 280 |
+
"To install follow https://github.com/state-spaces/mamba/#installation and"
|
| 281 |
+
"https://github.com/Dao-AILab/causal-conv1d"
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
def cuda_kernels_forward(
|
| 285 |
+
self,
|
| 286 |
+
hidden_states: torch.Tensor,
|
| 287 |
+
cache_params: Optional[Mamba2Cache] = None,
|
| 288 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 289 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 290 |
+
):
|
| 291 |
+
# 1. Gated MLP's linear projection
|
| 292 |
+
hidden_states = apply_mask_to_padding_states(hidden_states, attention_mask)
|
| 293 |
+
projected_states = self.in_proj(hidden_states)
|
| 294 |
+
|
| 295 |
+
# Set up dimensions for reshapes later
|
| 296 |
+
batch_size, seq_len, _ = hidden_states.shape
|
| 297 |
+
groups_time_state_size = self.n_groups * self.ssm_state_size
|
| 298 |
+
d_mlp = (
|
| 299 |
+
projected_states.shape[-1]
|
| 300 |
+
- 2 * self.intermediate_size
|
| 301 |
+
- 2 * self.n_groups * self.ssm_state_size
|
| 302 |
+
- self.num_heads
|
| 303 |
+
) // 2
|
| 304 |
+
|
| 305 |
+
# Single step calculations via cache
|
| 306 |
+
if cache_params is not None and cache_position is not None and cache_position[0] > 0:
|
| 307 |
+
_, _, gate, hidden_states_B_C, dt = projected_states.squeeze(1).split(
|
| 308 |
+
[d_mlp, d_mlp, self.intermediate_size, self.conv_dim, self.num_heads], dim=-1
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
# 2. Convolution sequence transformation
|
| 312 |
+
hidden_states_B_C = causal_conv1d_update(
|
| 313 |
+
hidden_states_B_C,
|
| 314 |
+
cache_params.conv_states[self.layer_idx],
|
| 315 |
+
self.conv1d.weight.squeeze(1),
|
| 316 |
+
self.conv1d.bias,
|
| 317 |
+
self.activation,
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
hidden_states, B, C = torch.split(
|
| 321 |
+
hidden_states_B_C,
|
| 322 |
+
[
|
| 323 |
+
self.intermediate_size,
|
| 324 |
+
groups_time_state_size,
|
| 325 |
+
groups_time_state_size,
|
| 326 |
+
],
|
| 327 |
+
dim=-1,
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
# 3. SSM transformation
|
| 331 |
+
A = -torch.exp(self.A_log.float()) # (nheads,)
|
| 332 |
+
A = A[:, None, ...][:, :, None].expand(-1, self.head_dim, self.ssm_state_size).to(dtype=torch.float32)
|
| 333 |
+
dt = dt[:, :, None].expand(-1, -1, self.head_dim)
|
| 334 |
+
dt_bias = self.dt_bias[:, None, ...].expand(-1, self.head_dim)
|
| 335 |
+
D = self.D[:, None, ...].expand(-1, self.head_dim)
|
| 336 |
+
B = B.view(batch_size, self.n_groups, B.shape[1] // self.n_groups)
|
| 337 |
+
C = C.view(batch_size, self.n_groups, C.shape[1] // self.n_groups)
|
| 338 |
+
hidden_states_reshaped = hidden_states.view(batch_size, self.num_heads, self.head_dim)
|
| 339 |
+
|
| 340 |
+
hidden_states = selective_state_update(
|
| 341 |
+
cache_params.ssm_states[self.layer_idx],
|
| 342 |
+
hidden_states_reshaped,
|
| 343 |
+
dt,
|
| 344 |
+
A,
|
| 345 |
+
B,
|
| 346 |
+
C,
|
| 347 |
+
D,
|
| 348 |
+
z=None,
|
| 349 |
+
dt_bias=dt_bias,
|
| 350 |
+
dt_softplus=True,
|
| 351 |
+
)
|
| 352 |
+
hidden_states = hidden_states.view(batch_size, self.num_heads * self.head_dim)
|
| 353 |
+
hidden_states = self.norm(hidden_states, gate)
|
| 354 |
+
|
| 355 |
+
# 4. Final linear projection
|
| 356 |
+
out = self.out_proj(hidden_states)[:, None, ...]
|
| 357 |
+
|
| 358 |
+
# Fused calculations or step by step if no initialized cache is found
|
| 359 |
+
else:
|
| 360 |
+
A = -torch.exp(self.A_log.float()) # (num_heads) or (intermediate_size, state_size)
|
| 361 |
+
dt_limit_kwargs = {} if self.time_step_limit == (0.0, float("inf")) else {"dt_limit": self.time_step_limit}
|
| 362 |
+
|
| 363 |
+
# 2-4. Fused kernel for conv1d, SSM, and the final projection
|
| 364 |
+
if self.training and cache_params is None:
|
| 365 |
+
out = mamba_split_conv1d_scan_combined(
|
| 366 |
+
projected_states,
|
| 367 |
+
self.conv1d.weight.squeeze(1),
|
| 368 |
+
self.conv1d.bias,
|
| 369 |
+
self.dt_bias,
|
| 370 |
+
A,
|
| 371 |
+
D=self.D,
|
| 372 |
+
chunk_size=self.chunk_size,
|
| 373 |
+
seq_idx=None, # was seq_idx
|
| 374 |
+
activation=self.activation,
|
| 375 |
+
rmsnorm_weight=self.norm.weight,
|
| 376 |
+
rmsnorm_eps=self.norm.eps,
|
| 377 |
+
outproj_weight=self.out_proj.weight,
|
| 378 |
+
outproj_bias=self.out_proj.bias,
|
| 379 |
+
headdim=self.head_dim,
|
| 380 |
+
ngroups=self.n_groups,
|
| 381 |
+
norm_before_gate=False,
|
| 382 |
+
return_final_states=False,
|
| 383 |
+
**dt_limit_kwargs,
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
else:
|
| 387 |
+
_, _, gate, hidden_states_B_C, dt = projected_states.split(
|
| 388 |
+
[d_mlp, d_mlp, self.intermediate_size, self.conv_dim, self.num_heads], dim=-1
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
+
# 2. Convolution sequence transformation
|
| 392 |
+
# Init cache
|
| 393 |
+
if cache_params is not None:
|
| 394 |
+
hidden_states_B_C_transposed = hidden_states_B_C.transpose(1, 2)
|
| 395 |
+
conv_states = nn.functional.pad(
|
| 396 |
+
hidden_states_B_C_transposed,
|
| 397 |
+
(cache_params.conv_kernel_size - hidden_states_B_C_transposed.shape[-1], 0),
|
| 398 |
+
)
|
| 399 |
+
cache_params.update_conv_state(
|
| 400 |
+
layer_idx=self.layer_idx, new_conv_state=conv_states, cache_init=True
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
if self.activation not in ["silu", "swish"]:
|
| 404 |
+
hidden_states_B_C = self.act(
|
| 405 |
+
self.conv1d(hidden_states_B_C.transpose(1, 2))[..., :seq_len].transpose(1, 2)
|
| 406 |
+
)
|
| 407 |
+
else:
|
| 408 |
+
hidden_states_B_C = causal_conv1d_fn(
|
| 409 |
+
x=hidden_states_B_C.transpose(1, 2),
|
| 410 |
+
weight=self.conv1d.weight.squeeze(1),
|
| 411 |
+
bias=self.conv1d.bias,
|
| 412 |
+
activation=self.activation,
|
| 413 |
+
).transpose(1, 2)
|
| 414 |
+
|
| 415 |
+
hidden_states_B_C = apply_mask_to_padding_states(hidden_states_B_C, attention_mask)
|
| 416 |
+
hidden_states, B, C = torch.split(
|
| 417 |
+
hidden_states_B_C,
|
| 418 |
+
[self.intermediate_size, groups_time_state_size, groups_time_state_size],
|
| 419 |
+
dim=-1,
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
# 3. SSM transformation
|
| 423 |
+
scan_output, ssm_state = mamba_chunk_scan_combined(
|
| 424 |
+
hidden_states.view(batch_size, seq_len, -1, self.head_dim),
|
| 425 |
+
dt,
|
| 426 |
+
A,
|
| 427 |
+
B.view(batch_size, seq_len, self.n_groups, -1),
|
| 428 |
+
C.view(batch_size, seq_len, self.n_groups, -1),
|
| 429 |
+
chunk_size=self.chunk_size,
|
| 430 |
+
D=self.D,
|
| 431 |
+
z=None,
|
| 432 |
+
seq_idx=None,
|
| 433 |
+
return_final_states=True,
|
| 434 |
+
dt_bias=self.dt_bias,
|
| 435 |
+
dt_softplus=True,
|
| 436 |
+
**dt_limit_kwargs,
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
# Init cache
|
| 440 |
+
if ssm_state is not None and cache_params is not None:
|
| 441 |
+
cache_params.update_ssm_state(layer_idx=self.layer_idx, new_ssm_state=ssm_state)
|
| 442 |
+
|
| 443 |
+
scan_output = scan_output.view(batch_size, seq_len, -1)
|
| 444 |
+
# Multiply "gate" branch and apply extra normalization layer
|
| 445 |
+
scan_output = self.norm(scan_output, gate)
|
| 446 |
+
|
| 447 |
+
# 4. Final linear projection
|
| 448 |
+
out = self.out_proj(scan_output)
|
| 449 |
+
return out
|
| 450 |
+
|
| 451 |
+
# fmt: off
|
| 452 |
+
def torch_forward(
|
| 453 |
+
self,
|
| 454 |
+
input_states,
|
| 455 |
+
cache_params: Optional[Mamba2Cache] = None,
|
| 456 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 457 |
+
attention_mask: Optional[torch.Tensor] = None
|
| 458 |
+
):
|
| 459 |
+
batch_size, seq_len, _ = input_states.shape
|
| 460 |
+
dtype = input_states.dtype
|
| 461 |
+
|
| 462 |
+
# 1. Gated MLP's linear projection
|
| 463 |
+
input_states = apply_mask_to_padding_states(input_states, attention_mask)
|
| 464 |
+
projected_states = self.in_proj(input_states)
|
| 465 |
+
d_mlp = (projected_states.shape[-1] - 2 * self.intermediate_size -
|
| 466 |
+
2 * self.n_groups * self.ssm_state_size - self.num_heads) // 2
|
| 467 |
+
_, _, gate, hidden_states_B_C, dt = projected_states.split(
|
| 468 |
+
[d_mlp, d_mlp, self.intermediate_size, self.conv_dim, self.num_heads], dim=-1
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
# 2. Convolution sequence transformation
|
| 472 |
+
if cache_params is not None and cache_position is not None and cache_position[0] > 0:
|
| 473 |
+
cache_params.update_conv_state(layer_idx=self.layer_idx, new_conv_state=hidden_states_B_C, cache_init=False)
|
| 474 |
+
|
| 475 |
+
# We need to guarantee that anything regarding the cache is on the same device
|
| 476 |
+
conv_states = cache_params.conv_states[self.layer_idx].to(device=self.conv1d.weight.device)
|
| 477 |
+
|
| 478 |
+
hidden_states_B_C = torch.sum(
|
| 479 |
+
conv_states * self.conv1d.weight.squeeze(1), dim=-1
|
| 480 |
+
)
|
| 481 |
+
if self.use_conv_bias:
|
| 482 |
+
hidden_states_B_C = hidden_states_B_C + self.conv1d.bias
|
| 483 |
+
hidden_states_B_C = self.act(hidden_states_B_C)
|
| 484 |
+
else:
|
| 485 |
+
# Init cache
|
| 486 |
+
if cache_params is not None:
|
| 487 |
+
hidden_states_B_C_transposed = hidden_states_B_C.transpose(1, 2)
|
| 488 |
+
conv_states = nn.functional.pad(
|
| 489 |
+
hidden_states_B_C_transposed, (cache_params.conv_kernel_size - hidden_states_B_C_transposed.shape[-1], 0)
|
| 490 |
+
)
|
| 491 |
+
cache_params.update_conv_state(layer_idx=self.layer_idx, new_conv_state=conv_states, cache_init=True)
|
| 492 |
+
|
| 493 |
+
hidden_states_B_C = self.act(self.conv1d(hidden_states_B_C.transpose(1, 2))[..., :seq_len].transpose(1, 2))
|
| 494 |
+
|
| 495 |
+
hidden_states_B_C = apply_mask_to_padding_states(hidden_states_B_C, attention_mask)
|
| 496 |
+
hidden_states, B, C = torch.split(
|
| 497 |
+
hidden_states_B_C,
|
| 498 |
+
[self.intermediate_size, self.n_groups * self.ssm_state_size, self.n_groups * self.ssm_state_size],
|
| 499 |
+
dim=-1
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
+
# 3. SSM transformation
|
| 503 |
+
A = -torch.exp(self.A_log.float()) # [num_heads]
|
| 504 |
+
if cache_params is not None and cache_position is not None and cache_position[0] > 0:
|
| 505 |
+
# We need to guarantee that anything regarding the cache is on the same device
|
| 506 |
+
cache_device = cache_params.ssm_states.device
|
| 507 |
+
|
| 508 |
+
# Note: there is no need to pad parameter matrices here, as there is just one new token
|
| 509 |
+
# for batched generation
|
| 510 |
+
dt = dt[:, 0, :][:, None, ...]
|
| 511 |
+
dt = dt.transpose(1, 2).expand(batch_size, dt.shape[-1], self.head_dim)
|
| 512 |
+
# [num_heads] -> [num_heads, head_dim]
|
| 513 |
+
dt_bias = self.dt_bias[..., None].expand(self.dt_bias.shape[0], self.head_dim)
|
| 514 |
+
|
| 515 |
+
dt = torch.nn.functional.softplus(dt + dt_bias.to(dt.dtype))
|
| 516 |
+
dt = torch.clamp(dt, self.time_step_limit[0], self.time_step_limit[1])
|
| 517 |
+
A = A[..., None, None].expand(self.num_heads, self.head_dim, self.ssm_state_size).to(dtype=torch.float32)
|
| 518 |
+
# [bsz, num_heads, head_dim, state_size]
|
| 519 |
+
dA = (torch.exp(dt[..., None] * A)).to(device=cache_device)
|
| 520 |
+
|
| 521 |
+
# Discretize B
|
| 522 |
+
# [bsz, n_groups * state_size] -> [bsz, n_groups, 1, state_size] ->
|
| 523 |
+
# -> [bsz, n_groups, group to head repetition factor, state_size] -> [bsz, num_heads, state_size]
|
| 524 |
+
B = B.reshape(batch_size, self.n_groups, -1)[..., None, :]
|
| 525 |
+
B = B.expand(batch_size, self.n_groups, self.num_heads // self.n_groups, B.shape[-1]).contiguous()
|
| 526 |
+
B = B.reshape(batch_size, -1, B.shape[-1])
|
| 527 |
+
# [bsz, num_heads, head_dim, state_size]
|
| 528 |
+
dB = dt[..., None] * B[..., None, :]
|
| 529 |
+
|
| 530 |
+
# Discretize x into dB
|
| 531 |
+
# [bsz, intermediate_size] -> [bsz, num_heads, head_dim]
|
| 532 |
+
hidden_states = hidden_states.reshape(batch_size, -1, self.head_dim)
|
| 533 |
+
dBx = (dB * hidden_states[..., None]).to(device=cache_device)
|
| 534 |
+
|
| 535 |
+
# State calculation
|
| 536 |
+
cache_params.update_ssm_state(
|
| 537 |
+
layer_idx=self.layer_idx,
|
| 538 |
+
new_ssm_state=cache_params.ssm_states[self.layer_idx] * dA + dBx
|
| 539 |
+
)
|
| 540 |
+
|
| 541 |
+
# Subsequent output
|
| 542 |
+
# [bsz, n_groups * state_size] -> [bsz, num_heads, state_size]
|
| 543 |
+
C = C.reshape(batch_size, self.n_groups, -1)[..., None, :]
|
| 544 |
+
C = C.expand(batch_size, self.n_groups, self.num_heads // self.n_groups, C.shape[-1]).contiguous()
|
| 545 |
+
C = C.reshape(batch_size, -1, C.shape[-1])
|
| 546 |
+
# [bsz, num_heads, head_dim]
|
| 547 |
+
|
| 548 |
+
ssm_states = cache_params.ssm_states[self.layer_idx].to(device=C.device, dtype=C.dtype) # Shape: [b, h, d, n]
|
| 549 |
+
# Reshape ssm_states to merge the first two dimensions
|
| 550 |
+
# Shape: [b*h, d, n]
|
| 551 |
+
ssm_states_reshaped = ssm_states.view(batch_size * self.num_heads, self.head_dim, self.ssm_state_size)
|
| 552 |
+
C_reshaped = C.view(batch_size * self.num_heads, self.ssm_state_size, 1) # Shape: [b*h, n, 1]
|
| 553 |
+
y = torch.bmm(ssm_states_reshaped, C_reshaped)
|
| 554 |
+
y = y.view(batch_size, self.num_heads, self.head_dim)
|
| 555 |
+
|
| 556 |
+
# D skip connection
|
| 557 |
+
# [num_heads] -> [num_heads, head_dim]
|
| 558 |
+
D = self.D[..., None].expand(self.D.shape[0], self.head_dim)
|
| 559 |
+
y = (y + hidden_states * D).to(y.dtype)
|
| 560 |
+
|
| 561 |
+
# [bsz, num_heads, head_dim] -> [bsz, 1, intermediate_size]
|
| 562 |
+
y = y.reshape(batch_size, -1)[:, None, ...]
|
| 563 |
+
else:
|
| 564 |
+
# begin ssd naive implementation without einsums
|
| 565 |
+
dt = nn.functional.softplus(dt + self.dt_bias)
|
| 566 |
+
dt = torch.clamp(dt, self.time_step_limit[0], self.time_step_limit[1])
|
| 567 |
+
hidden_states = hidden_states.reshape(batch_size, seq_len, -1, self.head_dim).float()
|
| 568 |
+
B = B.reshape(batch_size, seq_len, -1, self.ssm_state_size).float()
|
| 569 |
+
C = C.reshape(batch_size, seq_len, -1, self.ssm_state_size).float()
|
| 570 |
+
B = B.repeat(1, 1, self.num_heads // self.n_groups, 1)
|
| 571 |
+
C = C.repeat(1, 1, self.num_heads // self.n_groups, 1)
|
| 572 |
+
pad_size = (self.chunk_size - seq_len % self.chunk_size) % self.chunk_size
|
| 573 |
+
|
| 574 |
+
D_residual = self.D[..., None] * pad_tensor_by_size(hidden_states, pad_size)
|
| 575 |
+
|
| 576 |
+
# Discretize x and A
|
| 577 |
+
hidden_states = hidden_states * dt[..., None]
|
| 578 |
+
A = A.to(hidden_states.dtype) * dt
|
| 579 |
+
|
| 580 |
+
# Rearrange into blocks/chunks
|
| 581 |
+
hidden_states, A, B, C = [reshape_into_chunks(t, pad_size, self.chunk_size) for t in (hidden_states, A, B, C)]
|
| 582 |
+
|
| 583 |
+
# [bsz, -1, chunk_size, num_heads] -> [bsz, num_heads, -1, chunk_size]
|
| 584 |
+
A = A.permute(0, 3, 1, 2)
|
| 585 |
+
A_cumsum = torch.cumsum(A, dim=-1)
|
| 586 |
+
|
| 587 |
+
# 1. Compute the output for each intra-chunk (diagonal blocks)
|
| 588 |
+
# This is the analog of a causal mask
|
| 589 |
+
L = torch.exp(segment_sum(A))
|
| 590 |
+
|
| 591 |
+
# Contraction of C and B to get G (attention-weights like)
|
| 592 |
+
# shape: (b, c, l, s, h, n)
|
| 593 |
+
G_intermediate = C[:, :, :, None, :, :] * B[:, :, None, :, :, :]
|
| 594 |
+
G = G_intermediate.sum(dim=-1) # shape: (b, c, l, s, h)
|
| 595 |
+
|
| 596 |
+
# Compute M, equivalent to applying attention mask to weights
|
| 597 |
+
M_intermediate = G[..., None] * L.permute(0, 2, 3, 4, 1)[..., None]
|
| 598 |
+
M = M_intermediate.sum(dim=-1)
|
| 599 |
+
|
| 600 |
+
# Compute Y_diag (apply to values)
|
| 601 |
+
Y_diag = (M[..., None] * hidden_states[:, :, None]).sum(dim=3)
|
| 602 |
+
|
| 603 |
+
# 2. Compute the state for each intra-chunk
|
| 604 |
+
# (right term of low-rank factorization of off-diagonal blocks; B terms)
|
| 605 |
+
decay_states = torch.exp((A_cumsum[:, :, :, -1:] - A_cumsum))
|
| 606 |
+
B_decay = B * decay_states.permute(0, -2, -1, 1)[..., None]
|
| 607 |
+
states = (B_decay[..., None, :] * hidden_states[..., None]).sum(dim=2)
|
| 608 |
+
|
| 609 |
+
# 3. Compute the inter-chunk SSM recurrence; produces correct SSM states at chunk boundaries
|
| 610 |
+
# (middle term of factorization of off-diag blocks; A terms)
|
| 611 |
+
if cache_params is not None and cache_position is not None and cache_position[0] > 0:
|
| 612 |
+
previous_states = cache_params.ssm_states[self.layer_idx][:, None, ...].to(device=states.device)
|
| 613 |
+
else:
|
| 614 |
+
previous_states = torch.zeros_like(states[:, :1])
|
| 615 |
+
states = torch.cat([previous_states, states], dim=1)
|
| 616 |
+
decay_chunk = torch.exp(segment_sum(nn.functional.pad(A_cumsum[:, :, :, -1], (1, 0))))
|
| 617 |
+
decay_chunk = decay_chunk.transpose(1, 3)
|
| 618 |
+
new_states = (decay_chunk[..., None, None] * states[:, :, None, ...]).sum(dim=1)
|
| 619 |
+
states, ssm_state = new_states[:, :-1], new_states[:, -1]
|
| 620 |
+
|
| 621 |
+
# 4. Compute state -> output conversion per chunk
|
| 622 |
+
# (left term of low-rank factorization of off-diagonal blocks; C terms)
|
| 623 |
+
state_decay_out = torch.exp(A_cumsum)
|
| 624 |
+
C_times_states = (C[..., None, :] * states[:, :, None, ...])
|
| 625 |
+
state_decay_out_permuted = state_decay_out.permute(0, 2, 3, 1)
|
| 626 |
+
Y_off = (C_times_states.sum(-1) * state_decay_out_permuted[..., None])
|
| 627 |
+
|
| 628 |
+
# Add output of intra-chunk and inter-chunk terms (diagonal and off-diagonal blocks)
|
| 629 |
+
y = Y_diag + Y_off
|
| 630 |
+
# [bsz, -1, self.chunk_size, num_heads, head_dim] -> [bsz, (padded) seq_len, num_heads, head_dim]
|
| 631 |
+
y = y.reshape(batch_size, -1, self.num_heads, self.head_dim)
|
| 632 |
+
|
| 633 |
+
y = y + D_residual
|
| 634 |
+
# Cutting off padded chunks
|
| 635 |
+
if pad_size > 0:
|
| 636 |
+
y = y[:, :seq_len, :, :]
|
| 637 |
+
y = y.reshape(batch_size, seq_len, -1)
|
| 638 |
+
|
| 639 |
+
# Init cache
|
| 640 |
+
if ssm_state is not None and cache_params is not None:
|
| 641 |
+
cache_params.update_ssm_state(layer_idx=self.layer_idx, new_ssm_state=ssm_state)
|
| 642 |
+
|
| 643 |
+
scan_output = self.norm(y, gate)
|
| 644 |
+
|
| 645 |
+
# end ssd naive
|
| 646 |
+
|
| 647 |
+
# 4. Final linear projection
|
| 648 |
+
contextualized_states = self.out_proj(scan_output.to(dtype)) # [batch, seq_len, hidden_size]
|
| 649 |
+
return contextualized_states
|
| 650 |
+
# fmt: on
|
| 651 |
+
|
| 652 |
+
def forward(
|
| 653 |
+
self,
|
| 654 |
+
hidden_states,
|
| 655 |
+
cache_params: Optional[Mamba2Cache] = None,
|
| 656 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 657 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 658 |
+
):
|
| 659 |
+
if is_fast_path_available and "cuda" in self.in_proj.weight.device.type:
|
| 660 |
+
return self.cuda_kernels_forward(hidden_states, cache_params, cache_position, attention_mask)
|
| 661 |
+
dtype = hidden_states.dtype
|
| 662 |
+
if attention_mask is not None and attention_mask.shape[1] > 1 and attention_mask.shape[0] > 1:
|
| 663 |
+
# tune out hidden states for pad tokens, see https://github.com/state-spaces/mamba/issues/66
|
| 664 |
+
hidden_states = (hidden_states * attention_mask[:, :, None]).to(dtype)
|
| 665 |
+
|
| 666 |
+
return self.torch_forward(hidden_states, cache_params, cache_position, attention_mask)
|
| 667 |
+
|
| 668 |
+
|
| 669 |
+
class Mamba2Block(nn.Module):
|
| 670 |
+
def __init__(self, config, layer_idx):
|
| 671 |
+
super().__init__()
|
| 672 |
+
self.config = config
|
| 673 |
+
self.layer_idx = layer_idx
|
| 674 |
+
self.residual_in_fp32 = config.residual_in_fp32
|
| 675 |
+
self.norm = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon)
|
| 676 |
+
self.mixer = Mamba2Mixer(config, layer_idx=layer_idx)
|
| 677 |
+
|
| 678 |
+
def forward(
|
| 679 |
+
self,
|
| 680 |
+
hidden_states,
|
| 681 |
+
cache_params: Optional[Mamba2Cache] = None,
|
| 682 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 683 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 684 |
+
):
|
| 685 |
+
residual = hidden_states
|
| 686 |
+
hidden_states = self.norm(hidden_states)
|
| 687 |
+
if self.residual_in_fp32:
|
| 688 |
+
residual = residual.to(torch.float32)
|
| 689 |
+
|
| 690 |
+
hidden_states = self.mixer(
|
| 691 |
+
hidden_states,
|
| 692 |
+
cache_params=cache_params,
|
| 693 |
+
cache_position=cache_position,
|
| 694 |
+
attention_mask=attention_mask,
|
| 695 |
+
)
|
| 696 |
+
hidden_states = residual + hidden_states
|
| 697 |
+
if self.residual_in_fp32:
|
| 698 |
+
hidden_states = hidden_states.to(dtype=self.norm.weight.dtype)
|
| 699 |
+
return hidden_states
|
| 700 |
+
|
| 701 |
+
|
| 702 |
+
class Mamba2PreTrainedModel(PreTrainedModel, GenerationMixin):
|
| 703 |
+
"""
|
| 704 |
+
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
|
| 705 |
+
models.
|
| 706 |
+
"""
|
| 707 |
+
|
| 708 |
+
config_class = Mamba2Config
|
| 709 |
+
base_model_prefix = "backbone"
|
| 710 |
+
_no_split_modules = ["Mamba2Block"]
|
| 711 |
+
supports_gradient_checkpointing = True
|
| 712 |
+
_is_stateful = True
|
| 713 |
+
|
| 714 |
+
def _init_weights(
|
| 715 |
+
self,
|
| 716 |
+
module: nn.Module,
|
| 717 |
+
num_residuals_per_layer: int = 1,
|
| 718 |
+
):
|
| 719 |
+
"""Initialize the weights."""
|
| 720 |
+
if isinstance(module, Mamba2Mixer):
|
| 721 |
+
|
| 722 |
+
# --- A_log ---
|
| 723 |
+
A = torch.arange(1, module.num_heads + 1)
|
| 724 |
+
with torch.no_grad():
|
| 725 |
+
if not isinstance(module.A_log, torch.distributed.tensor.DTensor):
|
| 726 |
+
module.A_log.copy_(torch.log(A))
|
| 727 |
+
else:
|
| 728 |
+
logger.warning_once("`A_log` is a DTensor, skipping initialization")
|
| 729 |
+
module.A_log._no_weight_decay = True
|
| 730 |
+
|
| 731 |
+
# --- D ---
|
| 732 |
+
nn.init.ones_(module.D)
|
| 733 |
+
module.D._no_weight_decay = True
|
| 734 |
+
|
| 735 |
+
# --- dt_bias ---
|
| 736 |
+
dt = torch.exp(
|
| 737 |
+
torch.rand(self.config.num_heads)
|
| 738 |
+
* (math.log(self.config.time_step_max) - math.log(self.config.time_step_min))
|
| 739 |
+
+ math.log(self.config.time_step_min)
|
| 740 |
+
).clamp(min=self.config.time_step_floor)
|
| 741 |
+
|
| 742 |
+
# Inverse of softplus: https://github.com/pytorch/pytorch/issues/72759
|
| 743 |
+
inv_dt = dt + torch.log(-torch.expm1(-dt))
|
| 744 |
+
with torch.no_grad():
|
| 745 |
+
if not isinstance(module.dt_bias, torch.distributed.tensor.DTensor):
|
| 746 |
+
module.dt_bias.copy_(inv_dt)
|
| 747 |
+
else:
|
| 748 |
+
logger.warning_once("`dt_bias` is a DTensor, skipping initialization")
|
| 749 |
+
module.dt_bias._no_reinit = True
|
| 750 |
+
|
| 751 |
+
elif isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 752 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 753 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 754 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 755 |
+
if module.bias is not None:
|
| 756 |
+
nn.init.zeros_(module.bias)
|
| 757 |
+
# guard against deprecated behavior
|
| 758 |
+
if hasattr(module.bias, "_no_reinit"):
|
| 759 |
+
raise ValueError("This is not supposed to happen")
|
| 760 |
+
elif isinstance(module, nn.Embedding):
|
| 761 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 762 |
+
elif hasattr(module, 'reset_parameters'):
|
| 763 |
+
module.reset_parameters()
|
| 764 |
+
|
| 765 |
+
if self.config.rescale_prenorm_residual:
|
| 766 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 767 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 768 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 769 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 770 |
+
#
|
| 771 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 772 |
+
p = None
|
| 773 |
+
if hasattr(module, 'o_proj'):
|
| 774 |
+
# p = module.o_proj.weight
|
| 775 |
+
# guard against deprecated behavior
|
| 776 |
+
raise ValueError("This is not supposed to happen")
|
| 777 |
+
elif hasattr(module, 'out_proj'):
|
| 778 |
+
p = module.out_proj.weight
|
| 779 |
+
elif hasattr(module, 'down_proj'):
|
| 780 |
+
p = module.down_proj.weight
|
| 781 |
+
if p is not None:
|
| 782 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 783 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 784 |
+
# We need to reinit p since this code could be called multiple times
|
| 785 |
+
# Having just p *= scale would repeatedly scale it down
|
| 786 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 787 |
+
with torch.no_grad():
|
| 788 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 789 |
+
|
| 790 |
+
|
| 791 |
+
@dataclass
|
| 792 |
+
# Copied from transformers.models.mamba.modeling_mamba.MambaOutput with MAMBA->MAMBA2,Mamba->Mamba2
|
| 793 |
+
class Mamba2Output(ModelOutput):
|
| 794 |
+
"""
|
| 795 |
+
Class for the MAMBA2 model outputs.
|
| 796 |
+
|
| 797 |
+
Args:
|
| 798 |
+
last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
|
| 799 |
+
Sequence of hidden-states at the output of the last layer of the model.
|
| 800 |
+
cache_params (`Mamba2Cache`):
|
| 801 |
+
The state of the model at the last time step. Can be used in a forward method with the next `input_ids` to
|
| 802 |
+
avoid providing the old `input_ids`.
|
| 803 |
+
|
| 804 |
+
Includes both the State space model state matrices after the selective scan, and the Convolutional states
|
| 805 |
+
hidden_states (`tuple(torch.FloatTensor)`, *optional*,
|
| 806 |
+
returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
|
| 807 |
+
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
|
| 808 |
+
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
|
| 809 |
+
|
| 810 |
+
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
|
| 811 |
+
"""
|
| 812 |
+
|
| 813 |
+
last_hidden_state: Optional[torch.FloatTensor] = None
|
| 814 |
+
cache_params: Optional[Mamba2Cache] = None
|
| 815 |
+
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
|
| 816 |
+
|
| 817 |
+
|
| 818 |
+
@dataclass
|
| 819 |
+
# Copied from transformers.models.mamba.modeling_mamba.MambaCausalLMOutput with Mamba->Mamba2
|
| 820 |
+
class Mamba2CausalLMOutput(ModelOutput):
|
| 821 |
+
"""
|
| 822 |
+
Base class for causal language model (or autoregressive) outputs.
|
| 823 |
+
|
| 824 |
+
Args:
|
| 825 |
+
loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
|
| 826 |
+
Language modeling loss (for next-token prediction).
|
| 827 |
+
logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
|
| 828 |
+
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
|
| 829 |
+
cache_params (`Mamba2Cache`):
|
| 830 |
+
The state of the model at the last time step. Can be used in a forward method with the next `input_ids` to
|
| 831 |
+
avoid providing the old `input_ids`.
|
| 832 |
+
|
| 833 |
+
Includes both the State space model state matrices after the selective scan, and the Convolutional states
|
| 834 |
+
hidden_states (`tuple(torch.FloatTensor)`, *optional*,
|
| 835 |
+
returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
|
| 836 |
+
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
|
| 837 |
+
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
|
| 838 |
+
|
| 839 |
+
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
|
| 840 |
+
"""
|
| 841 |
+
|
| 842 |
+
loss: Optional[torch.FloatTensor] = None
|
| 843 |
+
logits: Optional[torch.FloatTensor] = None
|
| 844 |
+
cache_params: Optional[Mamba2Cache] = None
|
| 845 |
+
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
|
| 846 |
+
|
| 847 |
+
|
| 848 |
+
class Mamba2Model(Mamba2PreTrainedModel):
|
| 849 |
+
def __init__(self, config):
|
| 850 |
+
super().__init__(config)
|
| 851 |
+
|
| 852 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 853 |
+
self.layers = nn.ModuleList([Mamba2Block(config, layer_idx=idx) for idx in range(config.num_hidden_layers)])
|
| 854 |
+
|
| 855 |
+
self.gradient_checkpointing = False
|
| 856 |
+
self.norm_f = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon)
|
| 857 |
+
# Initialize weights and apply final processing
|
| 858 |
+
self._register_load_state_dict_pre_hook(self.load_hook)
|
| 859 |
+
self.post_init()
|
| 860 |
+
|
| 861 |
+
def load_hook(self, state_dict, prefix, *args):
|
| 862 |
+
for k in state_dict:
|
| 863 |
+
if "embedding." in k:
|
| 864 |
+
state_dict[k.replace("embedding.", "embeddings.")] = state_dict.pop(k)
|
| 865 |
+
break
|
| 866 |
+
|
| 867 |
+
def get_input_embeddings(self):
|
| 868 |
+
return self.embeddings
|
| 869 |
+
|
| 870 |
+
def set_input_embeddings(self, new_embeddings):
|
| 871 |
+
self.embeddings = new_embeddings
|
| 872 |
+
|
| 873 |
+
def forward(
|
| 874 |
+
self,
|
| 875 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 876 |
+
inputs_embeds: Optional[torch.LongTensor] = None,
|
| 877 |
+
cache_params: Optional[Mamba2Cache] = None,
|
| 878 |
+
use_cache: Optional[bool] = None,
|
| 879 |
+
output_hidden_states: Optional[bool] = None,
|
| 880 |
+
return_dict: Optional[bool] = None,
|
| 881 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 882 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 883 |
+
**kwargs,
|
| 884 |
+
) -> Union[Tuple, Mamba2Output]:
|
| 885 |
+
output_hidden_states = (
|
| 886 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 887 |
+
)
|
| 888 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 889 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 890 |
+
|
| 891 |
+
if (input_ids is None) ^ (inputs_embeds is not None): # ^ is python for xor
|
| 892 |
+
raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
|
| 893 |
+
|
| 894 |
+
if inputs_embeds is None:
|
| 895 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 896 |
+
|
| 897 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 898 |
+
use_cache = False
|
| 899 |
+
|
| 900 |
+
if use_cache:
|
| 901 |
+
if cache_params is None:
|
| 902 |
+
cache_params = Mamba2Cache(
|
| 903 |
+
self.config, inputs_embeds.size(0), device=inputs_embeds.device, dtype=inputs_embeds.dtype
|
| 904 |
+
)
|
| 905 |
+
cache_position = torch.arange(0, self.config.conv_kernel, device=inputs_embeds.device)
|
| 906 |
+
elif cache_position is None:
|
| 907 |
+
# cases when we do manual forward instead of using `model.generate` which will initiate
|
| 908 |
+
# `cache_position` and makes sure it is not None, throw error here instead of doing some
|
| 909 |
+
# hack to conjecture the current cache position
|
| 910 |
+
raise ValueError(
|
| 911 |
+
"You have to specify the `cache_position` manually when `use_cache=True` and `cache_params` is passed, "
|
| 912 |
+
"you don't have to pass a `cache_params` if you are in prefilling stage because in that case it will "
|
| 913 |
+
"be initialized for you automatically"
|
| 914 |
+
)
|
| 915 |
+
else:
|
| 916 |
+
cache_params = None
|
| 917 |
+
|
| 918 |
+
hidden_states = inputs_embeds
|
| 919 |
+
all_hidden_states = () if output_hidden_states else None
|
| 920 |
+
for mixer_block in self.layers:
|
| 921 |
+
if self.gradient_checkpointing and self.training:
|
| 922 |
+
hidden_states = self._gradient_checkpointing_func(
|
| 923 |
+
mixer_block.__call__,
|
| 924 |
+
hidden_states,
|
| 925 |
+
cache_params,
|
| 926 |
+
cache_position,
|
| 927 |
+
attention_mask,
|
| 928 |
+
)
|
| 929 |
+
else:
|
| 930 |
+
hidden_states = mixer_block(
|
| 931 |
+
hidden_states,
|
| 932 |
+
cache_params=cache_params,
|
| 933 |
+
cache_position=cache_position,
|
| 934 |
+
attention_mask=attention_mask,
|
| 935 |
+
)
|
| 936 |
+
|
| 937 |
+
if output_hidden_states:
|
| 938 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
| 939 |
+
|
| 940 |
+
hidden_states = self.norm_f(hidden_states)
|
| 941 |
+
|
| 942 |
+
if output_hidden_states:
|
| 943 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
| 944 |
+
|
| 945 |
+
if not return_dict:
|
| 946 |
+
return tuple(v for v in [hidden_states, cache_params, all_hidden_states] if v is not None)
|
| 947 |
+
|
| 948 |
+
return Mamba2Output(
|
| 949 |
+
last_hidden_state=hidden_states,
|
| 950 |
+
cache_params=cache_params if use_cache else None,
|
| 951 |
+
hidden_states=all_hidden_states,
|
| 952 |
+
)
|
| 953 |
+
|
| 954 |
+
|
| 955 |
+
class Mamba2ForCausalLM(Mamba2PreTrainedModel):
|
| 956 |
+
_tied_weights_keys = []
|
| 957 |
+
|
| 958 |
+
def __init__(self, config):
|
| 959 |
+
super().__init__(config)
|
| 960 |
+
self.backbone = Mamba2Model(config)
|
| 961 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 962 |
+
self.criterion = None
|
| 963 |
+
|
| 964 |
+
# Initialize weights and apply final processing
|
| 965 |
+
self.post_init()
|
| 966 |
+
|
| 967 |
+
def get_output_embeddings(self):
|
| 968 |
+
return self.lm_head
|
| 969 |
+
|
| 970 |
+
def set_output_embeddings(self, new_embeddings):
|
| 971 |
+
self.lm_head = new_embeddings
|
| 972 |
+
|
| 973 |
+
def get_input_embeddings(self):
|
| 974 |
+
return self.backbone.get_input_embeddings()
|
| 975 |
+
|
| 976 |
+
def set_input_embeddings(self, new_embeddings):
|
| 977 |
+
return self.backbone.set_input_embeddings(new_embeddings)
|
| 978 |
+
|
| 979 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 980 |
+
def prepare_inputs_for_generation(
|
| 981 |
+
self,
|
| 982 |
+
input_ids,
|
| 983 |
+
inputs_embeds=None,
|
| 984 |
+
use_cache=None,
|
| 985 |
+
cache_params: Optional[Mamba2Cache] = None,
|
| 986 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 987 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 988 |
+
logits_to_keep: Optional[int] = None,
|
| 989 |
+
**kwargs,
|
| 990 |
+
):
|
| 991 |
+
if use_cache:
|
| 992 |
+
# `cache_position` should have been initialized in `generate`
|
| 993 |
+
if cache_position is None:
|
| 994 |
+
raise ValueError(
|
| 995 |
+
"`cache_position` should not be None as it should have been initialized in "
|
| 996 |
+
"`model.generate`, you are responsible for passing in a valid `cache_position` if "
|
| 997 |
+
"you are calling `prepare_inputs_for_generation` directly with `use_cache=True`"
|
| 998 |
+
)
|
| 999 |
+
if cache_position[0] > 0:
|
| 1000 |
+
input_ids = input_ids[:, -1][..., None]
|
| 1001 |
+
|
| 1002 |
+
if attention_mask is not None:
|
| 1003 |
+
attention_mask = None
|
| 1004 |
+
else:
|
| 1005 |
+
# we initialize the `cache_position` to full size of `conv_states` at prefill stage
|
| 1006 |
+
# considering padding will be applied when input length is shorter, and truncation
|
| 1007 |
+
# will be applied when it is longer, so it will be equivalent to always have it match
|
| 1008 |
+
# the length of `cache_params.conv_states`, which is `config.conv_kernel`
|
| 1009 |
+
cache_position = torch.arange(0, self.config.conv_kernel, device=input_ids.device)
|
| 1010 |
+
|
| 1011 |
+
if inputs_embeds is not None and cache_params is None:
|
| 1012 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
| 1013 |
+
else:
|
| 1014 |
+
model_inputs = {"input_ids": input_ids}
|
| 1015 |
+
|
| 1016 |
+
if logits_to_keep is not None:
|
| 1017 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 1018 |
+
|
| 1019 |
+
model_inputs.update({
|
| 1020 |
+
'attention_mask': attention_mask,
|
| 1021 |
+
'cache_params': cache_params,
|
| 1022 |
+
'use_cache': use_cache,
|
| 1023 |
+
'cache_position': cache_position,
|
| 1024 |
+
'logits_to_keep': logits_to_keep
|
| 1025 |
+
})
|
| 1026 |
+
return model_inputs
|
| 1027 |
+
|
| 1028 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 1029 |
+
def forward(
|
| 1030 |
+
self,
|
| 1031 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 1032 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 1033 |
+
cache_params: Optional[Mamba2Cache] = None,
|
| 1034 |
+
labels: Optional[torch.LongTensor] = None,
|
| 1035 |
+
output_hidden_states: Optional[bool] = None,
|
| 1036 |
+
return_dict: Optional[bool] = None,
|
| 1037 |
+
use_cache: Optional[bool] = None,
|
| 1038 |
+
cache_position: Optional[torch.Tensor] = None,
|
| 1039 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 1040 |
+
logits_to_keep: Optional[int] = 0,
|
| 1041 |
+
**kwargs, # for now we need this for generation
|
| 1042 |
+
) -> Union[Tuple, Mamba2CausalLMOutput]:
|
| 1043 |
+
r"""
|
| 1044 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
| 1045 |
+
Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
|
| 1046 |
+
`labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
|
| 1047 |
+
are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
|
| 1048 |
+
"""
|
| 1049 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 1050 |
+
|
| 1051 |
+
outputs = self.backbone(
|
| 1052 |
+
input_ids,
|
| 1053 |
+
cache_params=cache_params,
|
| 1054 |
+
inputs_embeds=inputs_embeds,
|
| 1055 |
+
output_hidden_states=output_hidden_states,
|
| 1056 |
+
return_dict=return_dict,
|
| 1057 |
+
use_cache=use_cache,
|
| 1058 |
+
cache_position=cache_position,
|
| 1059 |
+
attention_mask=attention_mask,
|
| 1060 |
+
)
|
| 1061 |
+
hidden_states = outputs[0]
|
| 1062 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 1063 |
+
|
| 1064 |
+
loss, logits = None, None
|
| 1065 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 1066 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 1067 |
+
if labels is not None:
|
| 1068 |
+
if getattr(self, 'criterion', None) is None:
|
| 1069 |
+
if fuse_linear_and_cross_entropy:
|
| 1070 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 1071 |
+
elif self.config.fuse_cross_entropy:
|
| 1072 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 1073 |
+
else:
|
| 1074 |
+
criterion = nn.CrossEntropyLoss()
|
| 1075 |
+
else:
|
| 1076 |
+
criterion = self.criterion
|
| 1077 |
+
labels = labels.to(hidden_states.device)
|
| 1078 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 1079 |
+
if fuse_linear_and_cross_entropy:
|
| 1080 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 1081 |
+
else:
|
| 1082 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 1083 |
+
|
| 1084 |
+
if not return_dict:
|
| 1085 |
+
output = (logits,) + outputs[1:]
|
| 1086 |
+
return (loss,) + output if loss is not None else output
|
| 1087 |
+
|
| 1088 |
+
return Mamba2CausalLMOutput(
|
| 1089 |
+
loss=loss,
|
| 1090 |
+
logits=logits,
|
| 1091 |
+
cache_params=outputs.cache_params,
|
| 1092 |
+
hidden_states=outputs.hidden_states,
|
| 1093 |
+
)
|
fla/models/nsa/configuration_nsa.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class NSAConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'nsa'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
hidden_size: int = 2048,
|
| 16 |
+
num_hidden_layers: int = 24,
|
| 17 |
+
num_heads: int = 64,
|
| 18 |
+
num_kv_heads: int = 4,
|
| 19 |
+
head_dim: int = 32,
|
| 20 |
+
qkv_bias: bool = False,
|
| 21 |
+
block_size: int = 64,
|
| 22 |
+
block_counts: Optional[int] = 16,
|
| 23 |
+
window_size: Optional[int] = 512,
|
| 24 |
+
rope_theta: Optional[float] = 10000.,
|
| 25 |
+
max_position_embeddings: int = 2048,
|
| 26 |
+
hidden_ratio: Optional[int] = 4,
|
| 27 |
+
intermediate_size: Optional[int] = None,
|
| 28 |
+
hidden_act: str = "swish",
|
| 29 |
+
initializer_range: float = 0.006,
|
| 30 |
+
elementwise_affine: Optional[bool] = True,
|
| 31 |
+
norm_eps: float = 1e-6,
|
| 32 |
+
use_cache: bool = True,
|
| 33 |
+
pad_token_id: int = None,
|
| 34 |
+
bos_token_id: int = 1,
|
| 35 |
+
eos_token_id: int = 2,
|
| 36 |
+
tie_word_embeddings: bool = False,
|
| 37 |
+
fuse_norm: bool = True,
|
| 38 |
+
fuse_swiglu: bool = True,
|
| 39 |
+
fuse_cross_entropy: bool = True,
|
| 40 |
+
vocab_size: int = 32000,
|
| 41 |
+
**kwargs,
|
| 42 |
+
):
|
| 43 |
+
self.hidden_size = hidden_size
|
| 44 |
+
self.num_hidden_layers = num_hidden_layers
|
| 45 |
+
self.num_heads = num_heads
|
| 46 |
+
self.num_kv_heads = num_kv_heads
|
| 47 |
+
self.head_dim = head_dim
|
| 48 |
+
self.qkv_bias = qkv_bias
|
| 49 |
+
self.block_size = block_size
|
| 50 |
+
self.block_counts = block_counts
|
| 51 |
+
self.window_size = window_size
|
| 52 |
+
self.rope_theta = rope_theta
|
| 53 |
+
self.max_position_embeddings = max_position_embeddings
|
| 54 |
+
|
| 55 |
+
self.hidden_ratio = hidden_ratio
|
| 56 |
+
self.intermediate_size = intermediate_size
|
| 57 |
+
self.hidden_act = hidden_act
|
| 58 |
+
|
| 59 |
+
self.initializer_range = initializer_range
|
| 60 |
+
self.elementwise_affine = elementwise_affine
|
| 61 |
+
self.norm_eps = norm_eps
|
| 62 |
+
self.use_cache = use_cache
|
| 63 |
+
|
| 64 |
+
self.fuse_norm = fuse_norm
|
| 65 |
+
self.fuse_swiglu = fuse_swiglu
|
| 66 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 67 |
+
self.vocab_size = vocab_size
|
| 68 |
+
|
| 69 |
+
super().__init__(
|
| 70 |
+
pad_token_id=pad_token_id,
|
| 71 |
+
bos_token_id=bos_token_id,
|
| 72 |
+
eos_token_id=eos_token_id,
|
| 73 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 74 |
+
**kwargs,
|
| 75 |
+
)
|
fla/models/retnet/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.retnet.configuration_retnet import RetNetConfig
|
| 6 |
+
from fla.models.retnet.modeling_retnet import RetNetForCausalLM, RetNetModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(RetNetConfig.model_type, RetNetConfig)
|
| 9 |
+
AutoModel.register(RetNetConfig, RetNetModel)
|
| 10 |
+
AutoModelForCausalLM.register(RetNetConfig, RetNetForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['RetNetConfig', 'RetNetForCausalLM', 'RetNetModel']
|
fla/models/rwkv6/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.rwkv6.configuration_rwkv6 import RWKV6Config
|
| 6 |
+
from fla.models.rwkv6.modeling_rwkv6 import RWKV6ForCausalLM, RWKV6Model
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(RWKV6Config.model_type, RWKV6Config, True)
|
| 9 |
+
AutoModel.register(RWKV6Config, RWKV6Model, True)
|
| 10 |
+
AutoModelForCausalLM.register(RWKV6Config, RWKV6ForCausalLM, True)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['RWKV6Config', 'RWKV6ForCausalLM', 'RWKV6Model']
|
fla/models/rwkv7/modeling_rwkv7.py
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.utils.checkpoint
|
| 12 |
+
from transformers.generation import GenerationMixin
|
| 13 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 14 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 15 |
+
from transformers.utils import logging
|
| 16 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 17 |
+
|
| 18 |
+
from fla.layers.attn import Attention
|
| 19 |
+
from fla.layers.rwkv7 import RWKV7Attention
|
| 20 |
+
from fla.models.rwkv7.configuration_rwkv7 import RWKV7Config
|
| 21 |
+
from fla.models.utils import Cache
|
| 22 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss, LayerNorm
|
| 23 |
+
from fla.modules.activations import ACT2FN
|
| 24 |
+
|
| 25 |
+
if TYPE_CHECKING:
|
| 26 |
+
from transformers.processing_utils import Unpack
|
| 27 |
+
|
| 28 |
+
logger = logging.get_logger(__name__)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class RWKV7FeedForward(nn.Module):
|
| 32 |
+
|
| 33 |
+
def __init__(
|
| 34 |
+
self,
|
| 35 |
+
hidden_size: int,
|
| 36 |
+
hidden_ratio: Optional[int] = None,
|
| 37 |
+
intermediate_size: Optional[int] = None,
|
| 38 |
+
hidden_act: str = 'sqrelu',
|
| 39 |
+
layer_idx: int = None
|
| 40 |
+
) -> RWKV7FeedForward:
|
| 41 |
+
super().__init__()
|
| 42 |
+
|
| 43 |
+
self.hidden_size = hidden_size
|
| 44 |
+
if hidden_ratio is None:
|
| 45 |
+
hidden_ratio = 4
|
| 46 |
+
if intermediate_size is None:
|
| 47 |
+
intermediate_size = int(hidden_size * hidden_ratio)
|
| 48 |
+
intermediate_size = 32 * ((intermediate_size + 32 - 1) // 32)
|
| 49 |
+
self.hidden_ratio = hidden_ratio
|
| 50 |
+
self.intermediate_size = intermediate_size
|
| 51 |
+
|
| 52 |
+
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
|
| 53 |
+
|
| 54 |
+
self.x_k = nn.Parameter(torch.zeros(hidden_size))
|
| 55 |
+
|
| 56 |
+
self.key = nn.Linear(hidden_size, intermediate_size, bias=False)
|
| 57 |
+
self.value = nn.Linear(intermediate_size, hidden_size, bias=False)
|
| 58 |
+
self.act_fn = ACT2FN[hidden_act]
|
| 59 |
+
|
| 60 |
+
self.layer_idx = layer_idx
|
| 61 |
+
|
| 62 |
+
def forward(
|
| 63 |
+
self,
|
| 64 |
+
x: torch.Tensor,
|
| 65 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 66 |
+
state: Optional[Cache] = None
|
| 67 |
+
) -> torch.Tensor:
|
| 68 |
+
if attention_mask is not None:
|
| 69 |
+
x = x.mul(attention_mask[:, -x.shape[-2]:, None])
|
| 70 |
+
if x.shape[1] == 1 and state is not None and state[self.layer_idx]['ffn_state'] is not None:
|
| 71 |
+
shifted = state[self.layer_idx]['ffn_state'].unsqueeze(1)
|
| 72 |
+
else:
|
| 73 |
+
shifted = self.time_shift(x)
|
| 74 |
+
if state is not None and state[self.layer_idx]['ffn_state'] is not None:
|
| 75 |
+
shifted[:, 0] = state[self.layer_idx]['ffn_state'][-1]
|
| 76 |
+
if state is not None:
|
| 77 |
+
# no need to update the offset twice
|
| 78 |
+
state.update(ffn_state=x[:, -1], layer_idx=self.layer_idx, offset=0)
|
| 79 |
+
return self.value(self.act_fn(self.key(x.addcmul(shifted - x, self.x_k)))), state
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class RWKV7Block(nn.Module):
|
| 83 |
+
|
| 84 |
+
def __init__(
|
| 85 |
+
self,
|
| 86 |
+
config: RWKV7Config,
|
| 87 |
+
layer_idx: int
|
| 88 |
+
) -> RWKV7Block:
|
| 89 |
+
super().__init__()
|
| 90 |
+
|
| 91 |
+
self.config = config
|
| 92 |
+
self.layer_idx = layer_idx
|
| 93 |
+
|
| 94 |
+
if config.norm_first and layer_idx == 0:
|
| 95 |
+
self.pre_norm = (LayerNorm if config.fuse_norm else nn.LayerNorm)(
|
| 96 |
+
config.hidden_size,
|
| 97 |
+
bias=config.norm_bias,
|
| 98 |
+
eps=config.norm_eps
|
| 99 |
+
)
|
| 100 |
+
self.attn_norm = (LayerNorm if config.fuse_norm else nn.LayerNorm)(
|
| 101 |
+
config.hidden_size,
|
| 102 |
+
bias=config.norm_bias,
|
| 103 |
+
eps=config.norm_eps
|
| 104 |
+
)
|
| 105 |
+
if config.attn is not None and layer_idx in config.attn['layers']:
|
| 106 |
+
self.attn = Attention(
|
| 107 |
+
hidden_size=config.hidden_size,
|
| 108 |
+
num_heads=config.attn['num_heads'],
|
| 109 |
+
num_kv_heads=config.attn['num_kv_heads'],
|
| 110 |
+
qkv_bias=config.attn['qkv_bias'],
|
| 111 |
+
window_size=config.attn['window_size'],
|
| 112 |
+
rope_theta=config.attn['rope_theta'],
|
| 113 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 114 |
+
layer_idx=layer_idx
|
| 115 |
+
)
|
| 116 |
+
else:
|
| 117 |
+
self.attn = RWKV7Attention(
|
| 118 |
+
mode=config.attn_mode,
|
| 119 |
+
hidden_size=config.hidden_size,
|
| 120 |
+
head_dim=config.head_dim,
|
| 121 |
+
num_heads=config.num_heads,
|
| 122 |
+
decay_low_rank_dim=config.decay_low_rank_dim,
|
| 123 |
+
gate_low_rank_dim=config.gate_low_rank_dim,
|
| 124 |
+
a_low_rank_dim=config.a_low_rank_dim,
|
| 125 |
+
v_low_rank_dim=config.v_low_rank_dim,
|
| 126 |
+
norm_eps=config.norm_eps,
|
| 127 |
+
fuse_norm=config.fuse_norm,
|
| 128 |
+
layer_idx=layer_idx,
|
| 129 |
+
value_dim=config.value_dim[layer_idx]
|
| 130 |
+
)
|
| 131 |
+
self.ffn_norm = (LayerNorm if config.fuse_norm else nn.LayerNorm)(
|
| 132 |
+
config.hidden_size,
|
| 133 |
+
bias=config.norm_bias,
|
| 134 |
+
eps=config.norm_eps
|
| 135 |
+
)
|
| 136 |
+
self.ffn = RWKV7FeedForward(
|
| 137 |
+
hidden_size=config.hidden_size,
|
| 138 |
+
hidden_ratio=config.hidden_ratio,
|
| 139 |
+
intermediate_size=config.intermediate_size,
|
| 140 |
+
hidden_act=config.hidden_act,
|
| 141 |
+
layer_idx=layer_idx
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
def forward(
|
| 145 |
+
self,
|
| 146 |
+
hidden_states: torch.Tensor,
|
| 147 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 148 |
+
past_key_values: Optional[Cache] = None,
|
| 149 |
+
use_cache: Optional[bool] = False,
|
| 150 |
+
output_attentions: Optional[bool] = False,
|
| 151 |
+
v_first: torch.Tensor = None,
|
| 152 |
+
**kwargs,
|
| 153 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 154 |
+
residual = self.pre_norm(hidden_states) if hasattr(self, 'pre_norm') else hidden_states
|
| 155 |
+
hidden_states = self.attn_norm(residual)
|
| 156 |
+
hidden_states, attentions, past_key_values, v_first = self.attn(
|
| 157 |
+
hidden_states=hidden_states,
|
| 158 |
+
attention_mask=attention_mask,
|
| 159 |
+
past_key_values=past_key_values,
|
| 160 |
+
use_cache=use_cache,
|
| 161 |
+
output_attentions=output_attentions,
|
| 162 |
+
v_first=v_first,
|
| 163 |
+
**kwargs
|
| 164 |
+
)
|
| 165 |
+
if self.config.fuse_norm:
|
| 166 |
+
hidden_states, residual = self.ffn_norm(hidden_states, residual, True)
|
| 167 |
+
else:
|
| 168 |
+
hidden_states = residual + hidden_states
|
| 169 |
+
residual = hidden_states
|
| 170 |
+
hidden_states = self.ffn_norm(hidden_states)
|
| 171 |
+
hidden_states, past_key_values = self.ffn(hidden_states, attention_mask, past_key_values)
|
| 172 |
+
hidden_states = residual + hidden_states
|
| 173 |
+
|
| 174 |
+
outputs = (hidden_states, attentions, past_key_values, v_first)
|
| 175 |
+
|
| 176 |
+
return outputs
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
class RWKV7PreTrainedModel(PreTrainedModel):
|
| 180 |
+
|
| 181 |
+
config_class = RWKV7Config
|
| 182 |
+
base_model_prefix = 'model'
|
| 183 |
+
supports_gradient_checkpointing = True
|
| 184 |
+
_no_split_modules = ['RWKV7Block']
|
| 185 |
+
_supports_cache_class = True
|
| 186 |
+
_skip_keys_device_placement = ["past_key_values"]
|
| 187 |
+
|
| 188 |
+
def __init__(self, *inputs, **kwargs):
|
| 189 |
+
super().__init__(*inputs, **kwargs)
|
| 190 |
+
|
| 191 |
+
def _init_weights(
|
| 192 |
+
self,
|
| 193 |
+
module: nn.Module,
|
| 194 |
+
rescale_prenorm_residual: bool = True,
|
| 195 |
+
num_residuals_per_layer: int = 2,
|
| 196 |
+
):
|
| 197 |
+
warnings.warn(
|
| 198 |
+
"RWKV-7 employs a carefully designed initialization strategy tailored to its architecture. "
|
| 199 |
+
"The detailed initialization scheme is currently not implemented here but can be found in the "
|
| 200 |
+
"official code repository. We emphasize that using the recommended initialization is essential "
|
| 201 |
+
"for replicating the results in RWKV-7 paper. Deviations from the prescribed initialization "
|
| 202 |
+
"may lead to performance degradation.\n"
|
| 203 |
+
"Alternatively, please generate initial weights from the official RWKV code repository, and "
|
| 204 |
+
"convert the PyTorch checkpoint into FLA supported format."
|
| 205 |
+
)
|
| 206 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 207 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 208 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 209 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 210 |
+
if module.bias is not None:
|
| 211 |
+
nn.init.zeros_(module.bias)
|
| 212 |
+
elif isinstance(module, nn.Parameter):
|
| 213 |
+
nn.init.normal_(module, mean=0.0, std=self.config.initializer_range)
|
| 214 |
+
elif isinstance(module, nn.Embedding):
|
| 215 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 216 |
+
elif hasattr(module, 'reset_parameters'):
|
| 217 |
+
module.reset_parameters()
|
| 218 |
+
|
| 219 |
+
if rescale_prenorm_residual:
|
| 220 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 221 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 222 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 223 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 224 |
+
#
|
| 225 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 226 |
+
p = None
|
| 227 |
+
if hasattr(module, 'o_proj'):
|
| 228 |
+
p = module.o_proj.weight
|
| 229 |
+
elif hasattr(module, 'down_proj'):
|
| 230 |
+
p = module.down_proj.weight
|
| 231 |
+
if p is not None:
|
| 232 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 233 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 234 |
+
# We need to reinit p since this code could be called multiple times
|
| 235 |
+
# Having just p *= scale would repeatedly scale it down
|
| 236 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 237 |
+
with torch.no_grad():
|
| 238 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
class RWKV7Model(RWKV7PreTrainedModel):
|
| 242 |
+
|
| 243 |
+
def __init__(self, config: RWKV7Config):
|
| 244 |
+
super().__init__(config)
|
| 245 |
+
self.padding_idx = config.pad_token_id
|
| 246 |
+
self.vocab_size = config.vocab_size
|
| 247 |
+
|
| 248 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 249 |
+
self.layers = nn.ModuleList([RWKV7Block(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 250 |
+
self.norm = (LayerNorm if config.fuse_norm else nn.LayerNorm)(
|
| 251 |
+
config.hidden_size,
|
| 252 |
+
bias=config.norm_bias,
|
| 253 |
+
eps=config.norm_eps
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
self.gradient_checkpointing = False
|
| 257 |
+
|
| 258 |
+
self.post_init()
|
| 259 |
+
|
| 260 |
+
def get_input_embeddings(self):
|
| 261 |
+
return self.embeddings
|
| 262 |
+
|
| 263 |
+
def set_input_embeddings(self, value):
|
| 264 |
+
self.embeddings = value
|
| 265 |
+
|
| 266 |
+
def forward(
|
| 267 |
+
self,
|
| 268 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 269 |
+
attention_mask: Optional[torch.Tensor] = None, # noqa
|
| 270 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 271 |
+
past_key_values: Optional[Cache] = None,
|
| 272 |
+
use_cache: Optional[bool] = None,
|
| 273 |
+
output_attentions: Optional[bool] = None,
|
| 274 |
+
output_hidden_states: Optional[bool] = None,
|
| 275 |
+
return_dict: Optional[bool] = None,
|
| 276 |
+
**kwargs: Unpack[Dict]
|
| 277 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 278 |
+
if output_attentions:
|
| 279 |
+
warnings.warn("`RWKV7Model` does not `output_attentions` now, setting it to `False`.")
|
| 280 |
+
output_attentions = False
|
| 281 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 282 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 283 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 284 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 285 |
+
|
| 286 |
+
# retrieve input_ids and inputs_embeds
|
| 287 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 288 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 289 |
+
if input_ids is None and inputs_embeds is None:
|
| 290 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 291 |
+
|
| 292 |
+
if inputs_embeds is None:
|
| 293 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 294 |
+
hidden_states = inputs_embeds
|
| 295 |
+
|
| 296 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 297 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 298 |
+
|
| 299 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 300 |
+
logger.warning_once("`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...")
|
| 301 |
+
use_cache = False
|
| 302 |
+
|
| 303 |
+
all_hidden_states = () if output_hidden_states else None
|
| 304 |
+
all_attns = () if output_attentions else None
|
| 305 |
+
|
| 306 |
+
v_first = torch.zeros_like(hidden_states)
|
| 307 |
+
for layer in self.layers:
|
| 308 |
+
if output_hidden_states:
|
| 309 |
+
all_hidden_states += (hidden_states,)
|
| 310 |
+
|
| 311 |
+
if self.gradient_checkpointing and self.training:
|
| 312 |
+
hidden_states, attentions, past_key_values, v_first = self._gradient_checkpointing_func(
|
| 313 |
+
layer.__call__,
|
| 314 |
+
hidden_states,
|
| 315 |
+
attention_mask,
|
| 316 |
+
past_key_values,
|
| 317 |
+
use_cache,
|
| 318 |
+
output_attentions,
|
| 319 |
+
v_first,
|
| 320 |
+
**kwargs
|
| 321 |
+
)
|
| 322 |
+
else:
|
| 323 |
+
hidden_states, attentions, past_key_values, v_first = layer(
|
| 324 |
+
hidden_states,
|
| 325 |
+
attention_mask=attention_mask,
|
| 326 |
+
past_key_values=past_key_values,
|
| 327 |
+
use_cache=use_cache,
|
| 328 |
+
output_attentions=output_attentions,
|
| 329 |
+
v_first=v_first,
|
| 330 |
+
**kwargs
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
if output_attentions:
|
| 334 |
+
all_attns += (attentions,)
|
| 335 |
+
|
| 336 |
+
hidden_states = self.norm(hidden_states)
|
| 337 |
+
|
| 338 |
+
# add hidden states from the last decoder layer
|
| 339 |
+
if output_hidden_states:
|
| 340 |
+
all_hidden_states += (hidden_states,)
|
| 341 |
+
|
| 342 |
+
if not return_dict:
|
| 343 |
+
return tuple(i for i in [hidden_states, past_key_values, all_hidden_states, all_attns] if i is not None)
|
| 344 |
+
return BaseModelOutputWithPast(
|
| 345 |
+
last_hidden_state=hidden_states,
|
| 346 |
+
past_key_values=past_key_values,
|
| 347 |
+
hidden_states=all_hidden_states,
|
| 348 |
+
attentions=all_attns
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
class RWKV7ForCausalLM(RWKV7PreTrainedModel, GenerationMixin):
|
| 353 |
+
|
| 354 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 355 |
+
|
| 356 |
+
def __init__(self, config):
|
| 357 |
+
super().__init__(config)
|
| 358 |
+
self.model = RWKV7Model(config)
|
| 359 |
+
self.vocab_size = config.vocab_size
|
| 360 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 361 |
+
self.criterion = None
|
| 362 |
+
|
| 363 |
+
# Initialize weights and apply final processing
|
| 364 |
+
self.post_init()
|
| 365 |
+
|
| 366 |
+
def get_input_embeddings(self):
|
| 367 |
+
return self.model.embeddings
|
| 368 |
+
|
| 369 |
+
def set_input_embeddings(self, value):
|
| 370 |
+
self.model.embeddings = value
|
| 371 |
+
|
| 372 |
+
def get_output_embeddings(self):
|
| 373 |
+
return self.lm_head
|
| 374 |
+
|
| 375 |
+
def set_output_embeddings(self, new_embeddings):
|
| 376 |
+
self.lm_head = new_embeddings
|
| 377 |
+
|
| 378 |
+
def set_decoder(self, decoder):
|
| 379 |
+
self.model = decoder
|
| 380 |
+
|
| 381 |
+
def get_decoder(self):
|
| 382 |
+
return self.model
|
| 383 |
+
|
| 384 |
+
def generate(self, *args, **kwargs):
|
| 385 |
+
try:
|
| 386 |
+
return super().generate(*args, **kwargs)
|
| 387 |
+
except AttributeError as exception:
|
| 388 |
+
if 'past_key_values' in str(exception):
|
| 389 |
+
raise AttributeError(
|
| 390 |
+
f"You tried to call `generate` with a decoding strategy that manipulates `past_key_values`, "
|
| 391 |
+
f"which is not supported for {self.__class__.__name__}. "
|
| 392 |
+
f"Try another generation strategy instead. "
|
| 393 |
+
f"For the available generation strategies, check this doc: "
|
| 394 |
+
f"https://huggingface.co/docs/transformers/en/generation_strategies#decoding-strategies"
|
| 395 |
+
)
|
| 396 |
+
else:
|
| 397 |
+
raise exception
|
| 398 |
+
|
| 399 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 400 |
+
def prepare_inputs_for_generation(
|
| 401 |
+
self,
|
| 402 |
+
input_ids: torch.LongTensor = None,
|
| 403 |
+
past_key_values: Optional[Cache] = None,
|
| 404 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 405 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 406 |
+
use_cache: bool = True,
|
| 407 |
+
logits_to_keep: Optional[int] = None,
|
| 408 |
+
**kwargs
|
| 409 |
+
):
|
| 410 |
+
# only last token for `inputs_ids` if the `past_key_values` is not empty.
|
| 411 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 412 |
+
input_ids = input_ids[:, -1:]
|
| 413 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 414 |
+
if inputs_embeds is not None and len(past_key_values) == 0:
|
| 415 |
+
model_inputs = {'inputs_embeds': inputs_embeds}
|
| 416 |
+
else:
|
| 417 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 418 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 419 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 420 |
+
# TODO: use `next_tokens` directly instead.
|
| 421 |
+
model_inputs = {'input_ids': input_ids.contiguous()}
|
| 422 |
+
|
| 423 |
+
if logits_to_keep is not None:
|
| 424 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 425 |
+
|
| 426 |
+
model_inputs.update({
|
| 427 |
+
'past_key_values': past_key_values,
|
| 428 |
+
'use_cache': use_cache,
|
| 429 |
+
'attention_mask': attention_mask,
|
| 430 |
+
})
|
| 431 |
+
return model_inputs
|
| 432 |
+
|
| 433 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 434 |
+
def forward(
|
| 435 |
+
self,
|
| 436 |
+
input_ids: torch.LongTensor = None,
|
| 437 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 438 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 439 |
+
past_key_values: Optional[Cache] = None,
|
| 440 |
+
labels: Optional[torch.LongTensor] = None,
|
| 441 |
+
shift_labels: Optional[torch.LongTensor] = None,
|
| 442 |
+
use_cache: Optional[bool] = None,
|
| 443 |
+
output_attentions: Optional[bool] = None,
|
| 444 |
+
output_hidden_states: Optional[bool] = None,
|
| 445 |
+
return_dict: Optional[bool] = None,
|
| 446 |
+
logits_to_keep: Optional[int] = 0,
|
| 447 |
+
**kwargs: Unpack[Dict]
|
| 448 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 449 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 450 |
+
output_hidden_states = (
|
| 451 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 452 |
+
)
|
| 453 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 454 |
+
|
| 455 |
+
outputs = self.model(
|
| 456 |
+
input_ids=input_ids,
|
| 457 |
+
attention_mask=attention_mask,
|
| 458 |
+
inputs_embeds=inputs_embeds,
|
| 459 |
+
past_key_values=past_key_values,
|
| 460 |
+
use_cache=use_cache,
|
| 461 |
+
output_attentions=output_attentions,
|
| 462 |
+
output_hidden_states=output_hidden_states,
|
| 463 |
+
return_dict=return_dict,
|
| 464 |
+
**kwargs
|
| 465 |
+
)
|
| 466 |
+
|
| 467 |
+
hidden_states = outputs[0]
|
| 468 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 469 |
+
|
| 470 |
+
loss, logits = None, None
|
| 471 |
+
has_labels = (labels is not None) or (shift_labels is not None)
|
| 472 |
+
if not (fuse_linear_and_cross_entropy and has_labels):
|
| 473 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 474 |
+
if has_labels:
|
| 475 |
+
if getattr(self, 'criterion', None) is None:
|
| 476 |
+
if fuse_linear_and_cross_entropy:
|
| 477 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 478 |
+
elif self.config.fuse_cross_entropy:
|
| 479 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 480 |
+
else:
|
| 481 |
+
criterion = nn.CrossEntropyLoss()
|
| 482 |
+
else:
|
| 483 |
+
criterion = self.criterion
|
| 484 |
+
|
| 485 |
+
# shift_labels: See https://github.com/huggingface/transformers/pull/36607/files.
|
| 486 |
+
if shift_labels is None:
|
| 487 |
+
shift_labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 488 |
+
shift_labels = shift_labels.to(hidden_states.device)
|
| 489 |
+
|
| 490 |
+
if fuse_linear_and_cross_entropy:
|
| 491 |
+
loss = criterion(hidden_states, shift_labels, self.lm_head.weight, self.lm_head.bias)
|
| 492 |
+
else:
|
| 493 |
+
loss = criterion(logits.view(shift_labels.numel(), -1), shift_labels.view(-1))
|
| 494 |
+
|
| 495 |
+
if not return_dict:
|
| 496 |
+
output = (logits,) + outputs[1:]
|
| 497 |
+
return (loss,) + output if loss is not None else output
|
| 498 |
+
|
| 499 |
+
return CausalLMOutputWithPast(
|
| 500 |
+
loss=loss,
|
| 501 |
+
logits=logits,
|
| 502 |
+
past_key_values=outputs.past_key_values,
|
| 503 |
+
hidden_states=outputs.hidden_states,
|
| 504 |
+
attentions=outputs.attentions,
|
| 505 |
+
)
|
fla/models/samba/configuration_samba.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
from typing import Dict, Optional
|
| 5 |
+
|
| 6 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class SambaConfig(PretrainedConfig):
|
| 10 |
+
|
| 11 |
+
model_type = "samba"
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
hidden_size: int = 2304,
|
| 16 |
+
state_size: int = 16,
|
| 17 |
+
num_hidden_layers: int = 18,
|
| 18 |
+
norm_eps=1e-5,
|
| 19 |
+
pad_token_id: int = 0,
|
| 20 |
+
bos_token_id: int = 1,
|
| 21 |
+
eos_token_id: int = 2,
|
| 22 |
+
expand: int = 2,
|
| 23 |
+
conv_kernel: int = 4,
|
| 24 |
+
use_bias: bool = False,
|
| 25 |
+
use_conv_bias: bool = True,
|
| 26 |
+
hidden_act: str = "swish",
|
| 27 |
+
initializer_range: str = 0.02,
|
| 28 |
+
residual_in_fp32: bool = False,
|
| 29 |
+
time_step_rank: str = "auto",
|
| 30 |
+
time_step_scale: float = 1.0,
|
| 31 |
+
time_step_min: float = 0.001,
|
| 32 |
+
time_step_max: float = 0.1,
|
| 33 |
+
time_step_init_scheme: str = "random",
|
| 34 |
+
time_step_floor: float = 1e-4,
|
| 35 |
+
max_position_embeddings: int = 2048,
|
| 36 |
+
attn: Optional[Dict] = {
|
| 37 |
+
'layers': (1, 3, 5, 7, 9, 11, 13, 15, 17),
|
| 38 |
+
'num_heads': 18,
|
| 39 |
+
'num_kv_heads': 18,
|
| 40 |
+
'qkv_bias': False,
|
| 41 |
+
'window_size': 2048,
|
| 42 |
+
'rope_theta': 10000.
|
| 43 |
+
},
|
| 44 |
+
hidden_ratio: Optional[int] = 4,
|
| 45 |
+
rescale_prenorm_residual: bool = False,
|
| 46 |
+
use_cache: bool = True,
|
| 47 |
+
fuse_norm: bool = True,
|
| 48 |
+
fuse_swiglu: bool = True,
|
| 49 |
+
fuse_cross_entropy: bool = True,
|
| 50 |
+
vocab_size: int = 32000,
|
| 51 |
+
tie_word_embeddings: bool = False,
|
| 52 |
+
**kwargs,
|
| 53 |
+
):
|
| 54 |
+
self.hidden_size = hidden_size
|
| 55 |
+
self.state_size = state_size
|
| 56 |
+
self.num_hidden_layers = num_hidden_layers
|
| 57 |
+
self.norm_eps = norm_eps
|
| 58 |
+
self.conv_kernel = conv_kernel
|
| 59 |
+
self.expand = expand
|
| 60 |
+
self.intermediate_size = int(expand * self.hidden_size)
|
| 61 |
+
self.bos_token_id = bos_token_id
|
| 62 |
+
self.eos_token_id = eos_token_id
|
| 63 |
+
self.pad_token_id = pad_token_id
|
| 64 |
+
self.use_bias = use_bias
|
| 65 |
+
self.use_conv_bias = use_conv_bias
|
| 66 |
+
self.hidden_act = hidden_act
|
| 67 |
+
self.initializer_range = initializer_range
|
| 68 |
+
self.time_step_rank = math.ceil(self.hidden_size / 16) if time_step_rank == "auto" else time_step_rank
|
| 69 |
+
self.time_step_scale = time_step_scale
|
| 70 |
+
self.time_step_min = time_step_min
|
| 71 |
+
self.time_step_max = time_step_max
|
| 72 |
+
self.time_step_init_scheme = time_step_init_scheme
|
| 73 |
+
self.time_step_floor = time_step_floor
|
| 74 |
+
self.max_position_embeddings = max_position_embeddings
|
| 75 |
+
self.attn = attn
|
| 76 |
+
self.hidden_ratio = hidden_ratio
|
| 77 |
+
self.rescale_prenorm_residual = rescale_prenorm_residual
|
| 78 |
+
self.residual_in_fp32 = residual_in_fp32
|
| 79 |
+
self.use_cache = use_cache
|
| 80 |
+
|
| 81 |
+
self.fuse_norm = fuse_norm
|
| 82 |
+
self.fuse_swiglu = fuse_swiglu
|
| 83 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 84 |
+
self.vocab_size = vocab_size
|
| 85 |
+
|
| 86 |
+
super().__init__(
|
| 87 |
+
bos_token_id=bos_token_id,
|
| 88 |
+
eos_token_id=eos_token_id,
|
| 89 |
+
pad_token_id=pad_token_id,
|
| 90 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 91 |
+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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
from dataclasses import dataclass
|
| 7 |
+
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.utils.checkpoint
|
| 11 |
+
from torch import nn
|
| 12 |
+
from transformers.generation import GenerationMixin
|
| 13 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 14 |
+
from transformers.utils import ModelOutput, logging
|
| 15 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 16 |
+
|
| 17 |
+
from fla.layers.attn import Attention
|
| 18 |
+
from fla.models.mamba.modeling_mamba import MambaCache, MambaMixer
|
| 19 |
+
from fla.models.samba.configuration_samba import SambaConfig
|
| 20 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 21 |
+
from fla.modules import GatedMLP as SambaMLP
|
| 22 |
+
from fla.modules import RMSNorm
|
| 23 |
+
|
| 24 |
+
if TYPE_CHECKING:
|
| 25 |
+
from transformers.processing_utils import Unpack
|
| 26 |
+
|
| 27 |
+
logger = logging.get_logger(__name__)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class SambaBlock(nn.Module):
|
| 31 |
+
def __init__(self, config, layer_idx):
|
| 32 |
+
super().__init__()
|
| 33 |
+
|
| 34 |
+
self.config = config
|
| 35 |
+
self.layer_idx = layer_idx
|
| 36 |
+
|
| 37 |
+
self.mixer_norm = RMSNorm(hidden_size=config.hidden_size, eps=config.norm_eps)
|
| 38 |
+
if config.attn is not None and layer_idx in config.attn['layers']:
|
| 39 |
+
self.mixer = Attention(
|
| 40 |
+
hidden_size=config.hidden_size,
|
| 41 |
+
num_heads=config.attn['num_heads'],
|
| 42 |
+
num_kv_heads=config.attn['num_kv_heads'],
|
| 43 |
+
qkv_bias=config.attn['qkv_bias'],
|
| 44 |
+
window_size=config.attn['window_size'],
|
| 45 |
+
rope_theta=config.attn['rope_theta'],
|
| 46 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 47 |
+
layer_idx=layer_idx
|
| 48 |
+
)
|
| 49 |
+
else:
|
| 50 |
+
self.mixer = MambaMixer(config, layer_idx=layer_idx)
|
| 51 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 52 |
+
self.mlp = SambaMLP(
|
| 53 |
+
hidden_size=config.hidden_size,
|
| 54 |
+
hidden_ratio=config.hidden_ratio,
|
| 55 |
+
hidden_act=config.hidden_act,
|
| 56 |
+
fuse_swiglu=config.fuse_swiglu
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def forward(
|
| 60 |
+
self,
|
| 61 |
+
hidden_states: torch.Tensor,
|
| 62 |
+
cache_params: Optional[Tuple[torch.Tensor]] = None,
|
| 63 |
+
**kwargs: Unpack[Dict]
|
| 64 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 65 |
+
|
| 66 |
+
residual = hidden_states
|
| 67 |
+
hidden_states = self.mixer_norm(hidden_states)
|
| 68 |
+
if isinstance(self.mixer, MambaMixer):
|
| 69 |
+
hidden_states = self.mixer(hidden_states, cache_params=cache_params, **kwargs)
|
| 70 |
+
else:
|
| 71 |
+
hidden_states, _, cache_params = self.mixer(hidden_states=hidden_states, past_key_values=cache_params, **kwargs)
|
| 72 |
+
if self.config.fuse_norm:
|
| 73 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 74 |
+
else:
|
| 75 |
+
hidden_states = residual + hidden_states
|
| 76 |
+
residual = hidden_states
|
| 77 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 78 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 79 |
+
hidden_states = residual + hidden_states
|
| 80 |
+
return hidden_states
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
class SambaPreTrainedModel(PreTrainedModel):
|
| 84 |
+
"""
|
| 85 |
+
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
|
| 86 |
+
models.
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
config_class = SambaConfig
|
| 90 |
+
base_model_prefix = "backbone"
|
| 91 |
+
_no_split_modules = ["SambaBlock"]
|
| 92 |
+
supports_gradient_checkpointing = True
|
| 93 |
+
|
| 94 |
+
def _init_weights(self, module):
|
| 95 |
+
"""Initialize the weights."""
|
| 96 |
+
if isinstance(module, nn.Linear):
|
| 97 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 98 |
+
if module.bias is not None:
|
| 99 |
+
if not getattr(module.bias, "_no_reinit", False):
|
| 100 |
+
nn.init.zeros_(module.bias)
|
| 101 |
+
elif isinstance(module, MambaMixer):
|
| 102 |
+
module.A_log._no_weight_decay = True
|
| 103 |
+
module.D._no_weight_decay = True
|
| 104 |
+
|
| 105 |
+
dt_init_std = self.config.time_step_rank**-0.5 * self.config.time_step_scale
|
| 106 |
+
if self.config.time_step_init_scheme == "constant":
|
| 107 |
+
nn.init.constant_(module.dt_proj.weight, dt_init_std)
|
| 108 |
+
elif self.config.time_step_init_scheme == "random":
|
| 109 |
+
nn.init.uniform_(module.dt_proj.weight, -dt_init_std, dt_init_std)
|
| 110 |
+
|
| 111 |
+
dt = torch.exp(
|
| 112 |
+
torch.rand(self.config.intermediate_size)
|
| 113 |
+
* (math.log(self.config.time_step_max) - math.log(self.config.time_step_min))
|
| 114 |
+
+ math.log(self.config.time_step_min)
|
| 115 |
+
).clamp(min=self.config.time_step_floor)
|
| 116 |
+
# # Inverse of softplus: https://github.com/pytorch/pytorch/issues/72759
|
| 117 |
+
inv_dt = dt + torch.log(-torch.expm1(-dt))
|
| 118 |
+
with torch.no_grad():
|
| 119 |
+
module.dt_proj.bias.data = nn.Parameter(inv_dt.to(module.dt_proj.bias.device))
|
| 120 |
+
module.dt_proj.bias._no_reinit = True
|
| 121 |
+
elif isinstance(module, nn.Embedding):
|
| 122 |
+
nn.init.normal_(module.weight, std=self.config.initializer_range)
|
| 123 |
+
elif hasattr(module, 'reset_parameters'):
|
| 124 |
+
module.reset_parameters()
|
| 125 |
+
|
| 126 |
+
if self.config.rescale_prenorm_residual:
|
| 127 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 128 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 129 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 130 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 131 |
+
#
|
| 132 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 133 |
+
for name, p in module.named_parameters():
|
| 134 |
+
if name in ["out_proj.weight"]:
|
| 135 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 136 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 137 |
+
# We need to reinit p since this code could be called multiple times
|
| 138 |
+
# Having just p *= scale would repeatedly scale it down
|
| 139 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 140 |
+
with torch.no_grad():
|
| 141 |
+
p /= math.sqrt(self.config.num_layers)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
@dataclass
|
| 145 |
+
class SambaOutput(ModelOutput):
|
| 146 |
+
"""
|
| 147 |
+
Class for the Samba model outputs.
|
| 148 |
+
|
| 149 |
+
Args:
|
| 150 |
+
last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
|
| 151 |
+
Sequence of hidden-states at the output of the last layer of the model.
|
| 152 |
+
cache_params (`MambaCache`):
|
| 153 |
+
The state of the model at the last time step. Can be used in a forward method with the next `input_ids` to
|
| 154 |
+
avoid providing the old `input_ids`.
|
| 155 |
+
|
| 156 |
+
Includes both the State space model state matrices after the selective scan, and the Convolutional states
|
| 157 |
+
hidden_states (`tuple(torch.FloatTensor)`, *optional*,
|
| 158 |
+
returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
|
| 159 |
+
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
|
| 160 |
+
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
|
| 161 |
+
|
| 162 |
+
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
|
| 163 |
+
"""
|
| 164 |
+
|
| 165 |
+
last_hidden_state: Optional[torch.FloatTensor] = None
|
| 166 |
+
cache_params: Optional[MambaCache] = None
|
| 167 |
+
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@dataclass
|
| 171 |
+
class SambaCausalLMOutput(ModelOutput):
|
| 172 |
+
"""
|
| 173 |
+
Base class for causal language model (or autoregressive) outputs.
|
| 174 |
+
|
| 175 |
+
Args:
|
| 176 |
+
loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
|
| 177 |
+
Language modeling loss (for next-token prediction).
|
| 178 |
+
logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
|
| 179 |
+
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
|
| 180 |
+
cache_params (`MambaCache`):
|
| 181 |
+
The state of the model at the last time step. Can be used in a forward method with the next `input_ids` to
|
| 182 |
+
avoid providing the old `input_ids`.
|
| 183 |
+
|
| 184 |
+
Includes both the State space model state matrices after the selective scan, and the Convolutional states
|
| 185 |
+
hidden_states (`tuple(torch.FloatTensor)`, *optional*,
|
| 186 |
+
returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
|
| 187 |
+
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
|
| 188 |
+
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
|
| 189 |
+
|
| 190 |
+
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
|
| 191 |
+
"""
|
| 192 |
+
|
| 193 |
+
loss: Optional[torch.FloatTensor] = None
|
| 194 |
+
logits: Optional[torch.FloatTensor] = None
|
| 195 |
+
cache_params: Optional[MambaCache] = None
|
| 196 |
+
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
class SambaModel(SambaPreTrainedModel):
|
| 200 |
+
def __init__(self, config):
|
| 201 |
+
super().__init__(config)
|
| 202 |
+
|
| 203 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 204 |
+
self.layers = nn.ModuleList([SambaBlock(config, layer_idx=idx) for idx in range(config.num_hidden_layers)])
|
| 205 |
+
|
| 206 |
+
self.gradient_checkpointing = False
|
| 207 |
+
self.norm_f = RMSNorm(config.hidden_size, eps=config.norm_eps)
|
| 208 |
+
# Initialize weights and apply final processing
|
| 209 |
+
self.post_init()
|
| 210 |
+
|
| 211 |
+
def get_input_embeddings(self):
|
| 212 |
+
return self.embeddings
|
| 213 |
+
|
| 214 |
+
def set_input_embeddings(self, new_embeddings):
|
| 215 |
+
self.embeddings = new_embeddings
|
| 216 |
+
|
| 217 |
+
def forward(
|
| 218 |
+
self,
|
| 219 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 220 |
+
inputs_embeds: Optional[torch.LongTensor] = None,
|
| 221 |
+
cache_params: Optional[MambaCache] = None,
|
| 222 |
+
use_cache: Optional[bool] = None,
|
| 223 |
+
output_hidden_states: Optional[bool] = None,
|
| 224 |
+
return_dict: Optional[bool] = None,
|
| 225 |
+
**kwargs: Unpack[Dict]
|
| 226 |
+
) -> Union[Tuple, SambaOutput]:
|
| 227 |
+
output_hidden_states = (
|
| 228 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 229 |
+
)
|
| 230 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 231 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 232 |
+
|
| 233 |
+
if (input_ids is None) ^ (inputs_embeds is not None): # ^ is python for xor
|
| 234 |
+
raise ValueError(
|
| 235 |
+
"You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one"
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
if inputs_embeds is None:
|
| 239 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 240 |
+
|
| 241 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 242 |
+
use_cache = False
|
| 243 |
+
|
| 244 |
+
if cache_params is None and use_cache:
|
| 245 |
+
cache_params = MambaCache(
|
| 246 |
+
self.config, inputs_embeds.size(0), device=inputs_embeds.device, dtype=inputs_embeds.dtype
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
hidden_states = inputs_embeds
|
| 250 |
+
all_hidden_states = () if output_hidden_states else None
|
| 251 |
+
for mixer_block in self.layers:
|
| 252 |
+
if self.gradient_checkpointing and self.training:
|
| 253 |
+
hidden_states = self._gradient_checkpointing_func(
|
| 254 |
+
mixer_block.__call__,
|
| 255 |
+
hidden_states,
|
| 256 |
+
cache_params,
|
| 257 |
+
**kwargs
|
| 258 |
+
)
|
| 259 |
+
else:
|
| 260 |
+
hidden_states = mixer_block(
|
| 261 |
+
hidden_states,
|
| 262 |
+
cache_params=cache_params,
|
| 263 |
+
**kwargs
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
if output_hidden_states:
|
| 267 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
| 268 |
+
|
| 269 |
+
if use_cache:
|
| 270 |
+
cache_params.seqlen_offset += inputs_embeds.shape[1]
|
| 271 |
+
|
| 272 |
+
hidden_states = self.norm_f(hidden_states)
|
| 273 |
+
|
| 274 |
+
if output_hidden_states:
|
| 275 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
| 276 |
+
|
| 277 |
+
if not return_dict:
|
| 278 |
+
return tuple(v for v in [hidden_states, cache_params, all_hidden_states] if v is not None)
|
| 279 |
+
|
| 280 |
+
return SambaOutput(
|
| 281 |
+
last_hidden_state=hidden_states,
|
| 282 |
+
cache_params=cache_params if use_cache else None,
|
| 283 |
+
hidden_states=all_hidden_states,
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
class SambaForCausalLM(SambaPreTrainedModel, GenerationMixin):
|
| 288 |
+
|
| 289 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 290 |
+
|
| 291 |
+
def __init__(self, config):
|
| 292 |
+
super().__init__(config)
|
| 293 |
+
self.backbone = SambaModel(config)
|
| 294 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 295 |
+
self.criterion = None
|
| 296 |
+
|
| 297 |
+
# Initialize weights and apply final processing
|
| 298 |
+
self.post_init()
|
| 299 |
+
|
| 300 |
+
def get_output_embeddings(self):
|
| 301 |
+
return self.lm_head
|
| 302 |
+
|
| 303 |
+
def set_output_embeddings(self, new_embeddings):
|
| 304 |
+
self.lm_head = new_embeddings
|
| 305 |
+
|
| 306 |
+
def get_input_embeddings(self):
|
| 307 |
+
return self.backbone.get_input_embeddings()
|
| 308 |
+
|
| 309 |
+
def set_input_embeddings(self, new_embeddings):
|
| 310 |
+
return self.backbone.set_input_embeddings(new_embeddings)
|
| 311 |
+
|
| 312 |
+
def _update_model_kwargs_for_generation(
|
| 313 |
+
self, outputs: ModelOutput, model_kwargs: Dict[str, Any], **kwargs
|
| 314 |
+
) -> Dict[str, Any]:
|
| 315 |
+
model_kwargs["cache_params"] = outputs.get("cache_params", None)
|
| 316 |
+
return model_kwargs
|
| 317 |
+
|
| 318 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 319 |
+
def prepare_inputs_for_generation(
|
| 320 |
+
self,
|
| 321 |
+
input_ids,
|
| 322 |
+
cache_params:
|
| 323 |
+
Optional[MambaCache] = None,
|
| 324 |
+
inputs_embeds=None,
|
| 325 |
+
attention_mask=None,
|
| 326 |
+
use_cache: Optional[bool] = True,
|
| 327 |
+
logits_to_keep: Optional[int] = None,
|
| 328 |
+
**kwargs: Unpack[Dict]
|
| 329 |
+
):
|
| 330 |
+
# only last token for inputs_ids if the state is passed along.
|
| 331 |
+
if cache_params is not None:
|
| 332 |
+
input_ids = input_ids[:, -1].unsqueeze(-1)
|
| 333 |
+
|
| 334 |
+
if inputs_embeds is not None and cache_params is None:
|
| 335 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
| 336 |
+
else:
|
| 337 |
+
model_inputs = {"input_ids": input_ids}
|
| 338 |
+
|
| 339 |
+
if logits_to_keep is not None:
|
| 340 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 341 |
+
|
| 342 |
+
model_inputs.update({
|
| 343 |
+
'cache_params': cache_params,
|
| 344 |
+
'use_cache': use_cache,
|
| 345 |
+
'attention_mask': attention_mask,
|
| 346 |
+
'logits_to_keep': logits_to_keep,
|
| 347 |
+
})
|
| 348 |
+
return model_inputs
|
| 349 |
+
|
| 350 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 351 |
+
def forward(
|
| 352 |
+
self,
|
| 353 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 354 |
+
attention_mask: Optional[torch.Tensor] = None, # noqa
|
| 355 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 356 |
+
cache_params: Optional[MambaCache] = None,
|
| 357 |
+
labels: Optional[torch.LongTensor] = None,
|
| 358 |
+
output_hidden_states: Optional[bool] = None,
|
| 359 |
+
return_dict: Optional[bool] = None,
|
| 360 |
+
use_cache: Optional[bool] = None,
|
| 361 |
+
logits_to_keep: Optional[int] = 0,
|
| 362 |
+
**kwargs: Unpack[Dict]
|
| 363 |
+
) -> Union[Tuple, SambaCausalLMOutput]:
|
| 364 |
+
r"""
|
| 365 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
| 366 |
+
Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
|
| 367 |
+
`labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
|
| 368 |
+
are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
|
| 369 |
+
"""
|
| 370 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 371 |
+
|
| 372 |
+
outputs = self.backbone(
|
| 373 |
+
input_ids,
|
| 374 |
+
cache_params=cache_params,
|
| 375 |
+
inputs_embeds=inputs_embeds,
|
| 376 |
+
output_hidden_states=output_hidden_states,
|
| 377 |
+
return_dict=return_dict,
|
| 378 |
+
use_cache=use_cache,
|
| 379 |
+
**kwargs
|
| 380 |
+
)
|
| 381 |
+
hidden_states = outputs[0]
|
| 382 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 383 |
+
|
| 384 |
+
loss, logits = None, None
|
| 385 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 386 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 387 |
+
if labels is not None:
|
| 388 |
+
if getattr(self, 'criterion', None) is None:
|
| 389 |
+
if fuse_linear_and_cross_entropy:
|
| 390 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 391 |
+
elif self.config.fuse_cross_entropy:
|
| 392 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 393 |
+
else:
|
| 394 |
+
criterion = nn.CrossEntropyLoss()
|
| 395 |
+
else:
|
| 396 |
+
criterion = self.criterion
|
| 397 |
+
labels = labels.to(hidden_states.device)
|
| 398 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 399 |
+
if fuse_linear_and_cross_entropy:
|
| 400 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 401 |
+
else:
|
| 402 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 403 |
+
|
| 404 |
+
if not return_dict:
|
| 405 |
+
output = (logits,) + outputs[1:]
|
| 406 |
+
return (loss,) + output if loss is not None else output
|
| 407 |
+
|
| 408 |
+
return SambaCausalLMOutput(
|
| 409 |
+
loss=loss,
|
| 410 |
+
logits=logits,
|
| 411 |
+
cache_params=outputs.cache_params,
|
| 412 |
+
hidden_states=outputs.hidden_states,
|
| 413 |
+
)
|
fla/models/transformer/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.transformer.configuration_transformer import TransformerConfig
|
| 6 |
+
from fla.models.transformer.modeling_transformer import TransformerForCausalLM, TransformerModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(TransformerConfig.model_type, TransformerConfig)
|
| 9 |
+
AutoModel.register(TransformerConfig, TransformerModel)
|
| 10 |
+
AutoModelForCausalLM.register(TransformerConfig, TransformerForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['TransformerConfig', 'TransformerForCausalLM', 'TransformerModel']
|
fla/models/transformer/modeling_transformer.py
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.utils.checkpoint
|
| 12 |
+
from transformers.generation import GenerationMixin
|
| 13 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 14 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 15 |
+
from transformers.utils import logging
|
| 16 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 17 |
+
|
| 18 |
+
from fla.layers.attn import Attention
|
| 19 |
+
from fla.models.transformer.configuration_transformer import TransformerConfig
|
| 20 |
+
from fla.models.utils import Cache
|
| 21 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 22 |
+
from fla.modules import GatedMLP as TransformerMLP
|
| 23 |
+
from fla.modules import RMSNorm
|
| 24 |
+
|
| 25 |
+
if TYPE_CHECKING:
|
| 26 |
+
from transformers.processing_utils import Unpack
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
logger = logging.get_logger(__name__)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class TransformerBlock(nn.Module):
|
| 33 |
+
|
| 34 |
+
def __init__(self, config: TransformerConfig, layer_idx: int):
|
| 35 |
+
super().__init__()
|
| 36 |
+
|
| 37 |
+
self.config = config
|
| 38 |
+
self.layer_idx = layer_idx
|
| 39 |
+
|
| 40 |
+
self.attn_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 41 |
+
self.attn = Attention(
|
| 42 |
+
hidden_size=config.hidden_size,
|
| 43 |
+
num_heads=config.num_heads,
|
| 44 |
+
num_kv_heads=config.num_kv_heads,
|
| 45 |
+
qkv_bias=config.qkv_bias,
|
| 46 |
+
qk_norm=config.qk_norm,
|
| 47 |
+
window_size=config.window_size,
|
| 48 |
+
rope_theta=config.rope_theta,
|
| 49 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 50 |
+
layer_idx=layer_idx
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 54 |
+
self.mlp = TransformerMLP(
|
| 55 |
+
hidden_size=config.hidden_size,
|
| 56 |
+
hidden_ratio=config.hidden_ratio,
|
| 57 |
+
intermediate_size=config.intermediate_size,
|
| 58 |
+
hidden_act=config.hidden_act,
|
| 59 |
+
fuse_swiglu=config.fuse_swiglu
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
def forward(
|
| 63 |
+
self,
|
| 64 |
+
hidden_states: torch.Tensor,
|
| 65 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 66 |
+
past_key_values: Optional[Tuple[torch.Tensor]] = None,
|
| 67 |
+
output_attentions: Optional[bool] = False,
|
| 68 |
+
use_cache: Optional[bool] = False,
|
| 69 |
+
**kwargs: Unpack[Any]
|
| 70 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 71 |
+
|
| 72 |
+
residual = hidden_states
|
| 73 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 74 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 75 |
+
hidden_states=hidden_states,
|
| 76 |
+
attention_mask=attention_mask,
|
| 77 |
+
past_key_values=past_key_values,
|
| 78 |
+
use_cache=use_cache,
|
| 79 |
+
output_attentions=output_attentions,
|
| 80 |
+
**kwargs
|
| 81 |
+
)
|
| 82 |
+
if self.config.fuse_norm:
|
| 83 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 84 |
+
else:
|
| 85 |
+
hidden_states = residual + hidden_states
|
| 86 |
+
residual = hidden_states
|
| 87 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 88 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 89 |
+
hidden_states = residual + hidden_states
|
| 90 |
+
|
| 91 |
+
outputs = (hidden_states,)
|
| 92 |
+
|
| 93 |
+
if output_attentions:
|
| 94 |
+
outputs += (attentions,)
|
| 95 |
+
|
| 96 |
+
if use_cache:
|
| 97 |
+
outputs += (past_key_values,)
|
| 98 |
+
|
| 99 |
+
return outputs
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class TransformerPreTrainedModel(PreTrainedModel):
|
| 103 |
+
|
| 104 |
+
config_class = TransformerConfig
|
| 105 |
+
base_model_prefix = 'model'
|
| 106 |
+
supports_gradient_checkpointing = True
|
| 107 |
+
_no_split_modules = ['TransformerBlock']
|
| 108 |
+
_supports_cache_class = True
|
| 109 |
+
|
| 110 |
+
def __init__(self, *inputs, **kwargs):
|
| 111 |
+
super().__init__(*inputs, **kwargs)
|
| 112 |
+
|
| 113 |
+
def _init_weights(
|
| 114 |
+
self,
|
| 115 |
+
module: nn.Module,
|
| 116 |
+
rescale_prenorm_residual: bool = False,
|
| 117 |
+
num_residuals_per_layer: int = 2,
|
| 118 |
+
):
|
| 119 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 120 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 121 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 122 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 123 |
+
if module.bias is not None:
|
| 124 |
+
nn.init.zeros_(module.bias)
|
| 125 |
+
elif isinstance(module, nn.Embedding):
|
| 126 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 127 |
+
elif hasattr(module, 'reset_parameters'):
|
| 128 |
+
module.reset_parameters()
|
| 129 |
+
|
| 130 |
+
if rescale_prenorm_residual:
|
| 131 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 132 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 133 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 134 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 135 |
+
#
|
| 136 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 137 |
+
p = None
|
| 138 |
+
if hasattr(module, 'o_proj'):
|
| 139 |
+
p = module.o_proj.weight
|
| 140 |
+
elif hasattr(module, 'down_proj'):
|
| 141 |
+
p = module.down_proj.weight
|
| 142 |
+
if p is not None:
|
| 143 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 144 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 145 |
+
# We need to reinit p since this code could be called multiple times
|
| 146 |
+
# Having just p *= scale would repeatedly scale it down
|
| 147 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 148 |
+
with torch.no_grad():
|
| 149 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
class TransformerModel(TransformerPreTrainedModel):
|
| 153 |
+
|
| 154 |
+
def __init__(
|
| 155 |
+
self,
|
| 156 |
+
config: TransformerConfig
|
| 157 |
+
) -> TransformerModel:
|
| 158 |
+
super().__init__(config)
|
| 159 |
+
self.padding_idx = config.pad_token_id
|
| 160 |
+
self.vocab_size = config.vocab_size
|
| 161 |
+
|
| 162 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 163 |
+
self.layers = nn.ModuleList([TransformerBlock(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 164 |
+
self.norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 165 |
+
|
| 166 |
+
self.gradient_checkpointing = False
|
| 167 |
+
|
| 168 |
+
self.post_init()
|
| 169 |
+
|
| 170 |
+
def get_input_embeddings(self):
|
| 171 |
+
return self.embeddings
|
| 172 |
+
|
| 173 |
+
def set_input_embeddings(self, value):
|
| 174 |
+
self.embeddings = value
|
| 175 |
+
|
| 176 |
+
def forward(
|
| 177 |
+
self,
|
| 178 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 179 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 180 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
| 181 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 182 |
+
use_cache: Optional[bool] = None,
|
| 183 |
+
output_attentions: Optional[bool] = None,
|
| 184 |
+
output_hidden_states: Optional[bool] = None,
|
| 185 |
+
return_dict: Optional[bool] = None,
|
| 186 |
+
**kwargs: Unpack[Any]
|
| 187 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 188 |
+
if output_attentions:
|
| 189 |
+
warnings.warn(
|
| 190 |
+
"`TransformerModel` does not support output attention weights now, so `output_attentions` is set to `False`."
|
| 191 |
+
)
|
| 192 |
+
output_attentions = False
|
| 193 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 194 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 195 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 196 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 197 |
+
|
| 198 |
+
# retrieve input_ids and inputs_embeds
|
| 199 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 200 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 201 |
+
elif input_ids is None and inputs_embeds is None:
|
| 202 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 203 |
+
|
| 204 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 205 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 206 |
+
|
| 207 |
+
if inputs_embeds is None:
|
| 208 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 209 |
+
|
| 210 |
+
# embed positions
|
| 211 |
+
hidden_states = inputs_embeds
|
| 212 |
+
|
| 213 |
+
if self.gradient_checkpointing and self.training:
|
| 214 |
+
if use_cache:
|
| 215 |
+
logger.warning_once(
|
| 216 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
| 217 |
+
)
|
| 218 |
+
use_cache = False
|
| 219 |
+
|
| 220 |
+
all_hidden_states = () if output_hidden_states else None
|
| 221 |
+
all_attns = () if output_attentions else None
|
| 222 |
+
next_cache = None
|
| 223 |
+
|
| 224 |
+
for layer in self.layers:
|
| 225 |
+
if output_hidden_states:
|
| 226 |
+
all_hidden_states += (hidden_states,)
|
| 227 |
+
|
| 228 |
+
if self.gradient_checkpointing and self.training:
|
| 229 |
+
layer_outputs = self._gradient_checkpointing_func(
|
| 230 |
+
layer.__call__,
|
| 231 |
+
hidden_states,
|
| 232 |
+
attention_mask,
|
| 233 |
+
past_key_values,
|
| 234 |
+
output_attentions,
|
| 235 |
+
use_cache,
|
| 236 |
+
**kwargs
|
| 237 |
+
)
|
| 238 |
+
else:
|
| 239 |
+
layer_outputs = layer(
|
| 240 |
+
hidden_states,
|
| 241 |
+
attention_mask=attention_mask,
|
| 242 |
+
past_key_values=past_key_values,
|
| 243 |
+
output_attentions=output_attentions,
|
| 244 |
+
use_cache=use_cache,
|
| 245 |
+
**kwargs
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
hidden_states = layer_outputs[0]
|
| 249 |
+
|
| 250 |
+
if use_cache:
|
| 251 |
+
next_cache = layer_outputs[2 if output_attentions else 1]
|
| 252 |
+
|
| 253 |
+
if output_attentions:
|
| 254 |
+
all_attns += (layer_outputs[1],)
|
| 255 |
+
|
| 256 |
+
hidden_states = self.norm(hidden_states)
|
| 257 |
+
|
| 258 |
+
# add hidden states from the last decoder layer
|
| 259 |
+
if output_hidden_states:
|
| 260 |
+
all_hidden_states += (hidden_states,)
|
| 261 |
+
|
| 262 |
+
if not return_dict:
|
| 263 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_attns] if v is not None)
|
| 264 |
+
|
| 265 |
+
return BaseModelOutputWithPast(
|
| 266 |
+
last_hidden_state=hidden_states,
|
| 267 |
+
past_key_values=next_cache,
|
| 268 |
+
hidden_states=all_hidden_states,
|
| 269 |
+
attentions=all_attns
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
class TransformerForCausalLM(TransformerPreTrainedModel, GenerationMixin):
|
| 274 |
+
|
| 275 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 276 |
+
|
| 277 |
+
def __init__(self, config):
|
| 278 |
+
super().__init__(config)
|
| 279 |
+
self.model = TransformerModel(config)
|
| 280 |
+
self.vocab_size = config.vocab_size
|
| 281 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 282 |
+
self.criterion = None
|
| 283 |
+
|
| 284 |
+
# Initialize weights and apply final processing
|
| 285 |
+
self.post_init()
|
| 286 |
+
|
| 287 |
+
def get_input_embeddings(self):
|
| 288 |
+
return self.model.embeddings
|
| 289 |
+
|
| 290 |
+
def set_input_embeddings(self, value):
|
| 291 |
+
self.model.embeddings = value
|
| 292 |
+
|
| 293 |
+
def get_output_embeddings(self):
|
| 294 |
+
return self.lm_head
|
| 295 |
+
|
| 296 |
+
def set_output_embeddings(self, new_embeddings):
|
| 297 |
+
self.lm_head = new_embeddings
|
| 298 |
+
|
| 299 |
+
def set_decoder(self, decoder):
|
| 300 |
+
self.model = decoder
|
| 301 |
+
|
| 302 |
+
def get_decoder(self):
|
| 303 |
+
return self.model
|
| 304 |
+
|
| 305 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 306 |
+
def prepare_inputs_for_generation(
|
| 307 |
+
self,
|
| 308 |
+
input_ids: torch.LongTensor = None,
|
| 309 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 310 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 311 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 312 |
+
use_cache: bool = True,
|
| 313 |
+
logits_to_keep: Optional[int] = None,
|
| 314 |
+
**kwargs
|
| 315 |
+
):
|
| 316 |
+
# only last token for `inputs_ids` if the `past_key_values` is not empty.
|
| 317 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 318 |
+
input_ids = input_ids[:, -1:]
|
| 319 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 320 |
+
if inputs_embeds is not None and len(past_key_values) == 0:
|
| 321 |
+
model_inputs = {'inputs_embeds': inputs_embeds}
|
| 322 |
+
else:
|
| 323 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 324 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 325 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 326 |
+
# TODO: use `next_tokens` directly instead.
|
| 327 |
+
model_inputs = {'input_ids': input_ids.contiguous()}
|
| 328 |
+
|
| 329 |
+
if logits_to_keep is not None:
|
| 330 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 331 |
+
|
| 332 |
+
model_inputs.update({
|
| 333 |
+
'past_key_values': past_key_values,
|
| 334 |
+
'use_cache': use_cache,
|
| 335 |
+
'attention_mask': attention_mask,
|
| 336 |
+
})
|
| 337 |
+
return model_inputs
|
| 338 |
+
|
| 339 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 340 |
+
def forward(
|
| 341 |
+
self,
|
| 342 |
+
input_ids: torch.LongTensor = None,
|
| 343 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 344 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 345 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 346 |
+
labels: Optional[torch.LongTensor] = None,
|
| 347 |
+
use_cache: Optional[bool] = None,
|
| 348 |
+
output_attentions: Optional[bool] = None,
|
| 349 |
+
output_hidden_states: Optional[bool] = None,
|
| 350 |
+
return_dict: Optional[bool] = None,
|
| 351 |
+
logits_to_keep: Optional[int] = 0,
|
| 352 |
+
**kwargs: Unpack[Any]
|
| 353 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 354 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 355 |
+
output_hidden_states = (
|
| 356 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 357 |
+
)
|
| 358 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 359 |
+
|
| 360 |
+
outputs = self.model(
|
| 361 |
+
input_ids=input_ids,
|
| 362 |
+
attention_mask=attention_mask,
|
| 363 |
+
past_key_values=past_key_values,
|
| 364 |
+
inputs_embeds=inputs_embeds,
|
| 365 |
+
use_cache=use_cache,
|
| 366 |
+
output_attentions=output_attentions,
|
| 367 |
+
output_hidden_states=output_hidden_states,
|
| 368 |
+
return_dict=return_dict,
|
| 369 |
+
**kwargs
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
+
hidden_states = outputs[0]
|
| 373 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 374 |
+
logits = None if fuse_linear_and_cross_entropy else self.lm_head(hidden_states[:, -logits_to_keep:])
|
| 375 |
+
|
| 376 |
+
loss = None
|
| 377 |
+
if labels is not None:
|
| 378 |
+
if getattr(self, 'criterion', None) is None:
|
| 379 |
+
if fuse_linear_and_cross_entropy:
|
| 380 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 381 |
+
elif self.config.fuse_cross_entropy:
|
| 382 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 383 |
+
else:
|
| 384 |
+
criterion = nn.CrossEntropyLoss()
|
| 385 |
+
else:
|
| 386 |
+
criterion = self.criterion
|
| 387 |
+
# Enable model parallelism
|
| 388 |
+
labels = labels.to(hidden_states.device)
|
| 389 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 390 |
+
labels = labels[..., :hidden_states.shape[1]].contiguous()
|
| 391 |
+
if fuse_linear_and_cross_entropy:
|
| 392 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 393 |
+
else:
|
| 394 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 395 |
+
|
| 396 |
+
if not return_dict:
|
| 397 |
+
output = (logits,) + outputs[1:]
|
| 398 |
+
return (loss,) + output if loss is not None else output
|
| 399 |
+
|
| 400 |
+
return CausalLMOutputWithPast(
|
| 401 |
+
loss=loss,
|
| 402 |
+
logits=logits,
|
| 403 |
+
past_key_values=outputs.past_key_values,
|
| 404 |
+
hidden_states=outputs.hidden_states,
|
| 405 |
+
attentions=outputs.attentions,
|
| 406 |
+
)
|
fla/models/transformer_top/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.transformer_top.configuration_transformer import TOPTransformerConfig
|
| 6 |
+
from fla.models.transformer_top.modeling_transformer import TOPTransformerForCausalLM, TOPTransformerModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(TOPTransformerConfig.model_type, TOPTransformerConfig)
|
| 9 |
+
AutoModel.register(TOPTransformerConfig, TOPTransformerModel)
|
| 10 |
+
AutoModelForCausalLM.register(TOPTransformerConfig, TOPTransformerForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['TOPTransformerConfig', 'TOPTransformerForCausalLM', 'TOPTransformerModel']
|
fla/models/transformer_top/configuration_transformer.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TOPTransformerConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'top_transformer'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
hidden_size: int = 2048,
|
| 16 |
+
num_hidden_layers: int = 24,
|
| 17 |
+
num_heads: int = 32,
|
| 18 |
+
num_kv_heads: int = None,
|
| 19 |
+
qkv_bias: bool = False,
|
| 20 |
+
qk_norm: bool = False,
|
| 21 |
+
window_size: Optional[int] = None,
|
| 22 |
+
rope_theta: Optional[float] = 10000.,
|
| 23 |
+
max_position_embeddings: int = 2048,
|
| 24 |
+
hidden_ratio: Optional[int] = 4,
|
| 25 |
+
intermediate_size: Optional[int] = None,
|
| 26 |
+
hidden_act: str = "swish",
|
| 27 |
+
initializer_range: float = 0.006,
|
| 28 |
+
elementwise_affine: Optional[bool] = True,
|
| 29 |
+
norm_eps: float = 1e-6,
|
| 30 |
+
use_cache: bool = True,
|
| 31 |
+
pad_token_id: int = None,
|
| 32 |
+
bos_token_id: int = 1,
|
| 33 |
+
eos_token_id: int = 2,
|
| 34 |
+
tie_word_embeddings: bool = False,
|
| 35 |
+
fuse_norm: bool = True,
|
| 36 |
+
fuse_swiglu: bool = True,
|
| 37 |
+
fuse_cross_entropy: bool = True,
|
| 38 |
+
vocab_size: int = 32000,
|
| 39 |
+
use_top_loss: bool = False,
|
| 40 |
+
top_window_size: Optional[int] = None,
|
| 41 |
+
**kwargs,
|
| 42 |
+
):
|
| 43 |
+
self.hidden_size = hidden_size
|
| 44 |
+
self.num_hidden_layers = num_hidden_layers
|
| 45 |
+
self.num_heads = num_heads
|
| 46 |
+
self.num_kv_heads = num_kv_heads
|
| 47 |
+
self.qkv_bias = qkv_bias
|
| 48 |
+
self.qk_norm = qk_norm
|
| 49 |
+
self.window_size = window_size
|
| 50 |
+
self.rope_theta = rope_theta
|
| 51 |
+
self.max_position_embeddings = max_position_embeddings
|
| 52 |
+
|
| 53 |
+
self.hidden_ratio = hidden_ratio
|
| 54 |
+
self.intermediate_size = intermediate_size
|
| 55 |
+
self.hidden_act = hidden_act
|
| 56 |
+
|
| 57 |
+
self.initializer_range = initializer_range
|
| 58 |
+
self.elementwise_affine = elementwise_affine
|
| 59 |
+
self.norm_eps = norm_eps
|
| 60 |
+
self.use_cache = use_cache
|
| 61 |
+
|
| 62 |
+
self.fuse_norm = fuse_norm
|
| 63 |
+
self.fuse_swiglu = fuse_swiglu
|
| 64 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 65 |
+
self.vocab_size = vocab_size
|
| 66 |
+
|
| 67 |
+
self.use_top_loss = use_top_loss
|
| 68 |
+
self.top_window_size = top_window_size if top_window_size is not None else max_position_embeddings
|
| 69 |
+
|
| 70 |
+
super().__init__(
|
| 71 |
+
pad_token_id=pad_token_id,
|
| 72 |
+
bos_token_id=bos_token_id,
|
| 73 |
+
eos_token_id=eos_token_id,
|
| 74 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 75 |
+
**kwargs,
|
| 76 |
+
)
|
fla/models/transformer_top/modeling_transformer.py
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.nn.functional as F
|
| 12 |
+
import torch.utils.checkpoint
|
| 13 |
+
from dataclasses import dataclass
|
| 14 |
+
from transformers.generation import GenerationMixin
|
| 15 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 16 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 17 |
+
from transformers.utils import logging
|
| 18 |
+
from transformers.utils.deprecation import deprecate_kwarg
|
| 19 |
+
|
| 20 |
+
import triton
|
| 21 |
+
import triton.language as tl
|
| 22 |
+
|
| 23 |
+
from fla.layers.attn import Attention
|
| 24 |
+
from fla.models.transformer_top.configuration_transformer import TOPTransformerConfig
|
| 25 |
+
from fla.models.utils import Cache
|
| 26 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss, FusedLinearListNetLoss
|
| 27 |
+
from fla.modules import GatedMLP as TransformerMLP
|
| 28 |
+
from fla.modules import RMSNorm
|
| 29 |
+
from fla.modules.seq_to_top import seq_to_top
|
| 30 |
+
|
| 31 |
+
if TYPE_CHECKING:
|
| 32 |
+
from transformers.processing_utils import Unpack
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
logger = logging.get_logger(__name__)
|
| 36 |
+
|
| 37 |
+
@dataclass
|
| 38 |
+
class TOPLMOutputWithPast(CausalLMOutputWithPast):
|
| 39 |
+
ntp_loss: Optional[torch.FloatTensor] = None
|
| 40 |
+
top_loss: Optional[torch.FloatTensor] = None
|
| 41 |
+
|
| 42 |
+
class TOPTransformerBlock(nn.Module):
|
| 43 |
+
|
| 44 |
+
def __init__(self, config: TOPTransformerConfig, layer_idx: int):
|
| 45 |
+
super().__init__()
|
| 46 |
+
|
| 47 |
+
self.config = config
|
| 48 |
+
self.layer_idx = layer_idx
|
| 49 |
+
|
| 50 |
+
self.attn_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 51 |
+
self.attn = Attention(
|
| 52 |
+
hidden_size=config.hidden_size,
|
| 53 |
+
num_heads=config.num_heads,
|
| 54 |
+
num_kv_heads=config.num_kv_heads,
|
| 55 |
+
qkv_bias=config.qkv_bias,
|
| 56 |
+
qk_norm=config.qk_norm,
|
| 57 |
+
window_size=config.window_size,
|
| 58 |
+
rope_theta=config.rope_theta,
|
| 59 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 60 |
+
layer_idx=layer_idx
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 64 |
+
self.mlp = TransformerMLP(
|
| 65 |
+
hidden_size=config.hidden_size,
|
| 66 |
+
hidden_ratio=config.hidden_ratio,
|
| 67 |
+
intermediate_size=config.intermediate_size,
|
| 68 |
+
hidden_act=config.hidden_act,
|
| 69 |
+
fuse_swiglu=config.fuse_swiglu
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
def forward(
|
| 73 |
+
self,
|
| 74 |
+
hidden_states: torch.Tensor,
|
| 75 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 76 |
+
past_key_values: Optional[Tuple[torch.Tensor]] = None,
|
| 77 |
+
output_attentions: Optional[bool] = False,
|
| 78 |
+
use_cache: Optional[bool] = False,
|
| 79 |
+
**kwargs: Unpack[Any]
|
| 80 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 81 |
+
|
| 82 |
+
residual = hidden_states
|
| 83 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 84 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 85 |
+
hidden_states=hidden_states,
|
| 86 |
+
attention_mask=attention_mask,
|
| 87 |
+
past_key_values=past_key_values,
|
| 88 |
+
use_cache=use_cache,
|
| 89 |
+
output_attentions=output_attentions,
|
| 90 |
+
**kwargs
|
| 91 |
+
)
|
| 92 |
+
if self.config.fuse_norm:
|
| 93 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 94 |
+
else:
|
| 95 |
+
hidden_states = residual + hidden_states
|
| 96 |
+
residual = hidden_states
|
| 97 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 98 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 99 |
+
hidden_states = residual + hidden_states
|
| 100 |
+
|
| 101 |
+
outputs = (hidden_states,)
|
| 102 |
+
|
| 103 |
+
if output_attentions:
|
| 104 |
+
outputs += (attentions,)
|
| 105 |
+
|
| 106 |
+
if use_cache:
|
| 107 |
+
outputs += (past_key_values,)
|
| 108 |
+
|
| 109 |
+
return outputs
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class TOPTransformerPreTrainedModel(PreTrainedModel):
|
| 113 |
+
|
| 114 |
+
config_class = TOPTransformerConfig
|
| 115 |
+
base_model_prefix = 'model'
|
| 116 |
+
supports_gradient_checkpointing = True
|
| 117 |
+
_no_split_modules = ['TOPTransformerBlock']
|
| 118 |
+
_supports_cache_class = True
|
| 119 |
+
|
| 120 |
+
def __init__(self, *inputs, **kwargs):
|
| 121 |
+
super().__init__(*inputs, **kwargs)
|
| 122 |
+
|
| 123 |
+
def _init_weights(
|
| 124 |
+
self,
|
| 125 |
+
module: nn.Module,
|
| 126 |
+
rescale_prenorm_residual: bool = False,
|
| 127 |
+
num_residuals_per_layer: int = 2,
|
| 128 |
+
):
|
| 129 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 130 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 131 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 132 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 133 |
+
if module.bias is not None:
|
| 134 |
+
nn.init.zeros_(module.bias)
|
| 135 |
+
elif isinstance(module, nn.Embedding):
|
| 136 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 137 |
+
elif hasattr(module, 'reset_parameters'):
|
| 138 |
+
module.reset_parameters()
|
| 139 |
+
|
| 140 |
+
if rescale_prenorm_residual:
|
| 141 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 142 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 143 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 144 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 145 |
+
#
|
| 146 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 147 |
+
p = None
|
| 148 |
+
if hasattr(module, 'o_proj'):
|
| 149 |
+
p = module.o_proj.weight
|
| 150 |
+
elif hasattr(module, 'down_proj'):
|
| 151 |
+
p = module.down_proj.weight
|
| 152 |
+
if p is not None:
|
| 153 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per TOPTransformer Block
|
| 154 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 155 |
+
# We need to reinit p since this code could be called multiple times
|
| 156 |
+
# Having just p *= scale would repeatedly scale it down
|
| 157 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 158 |
+
with torch.no_grad():
|
| 159 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
class TOPTransformerModel(TOPTransformerPreTrainedModel):
|
| 163 |
+
|
| 164 |
+
def __init__(
|
| 165 |
+
self,
|
| 166 |
+
config: TOPTransformerConfig
|
| 167 |
+
) -> TOPTransformerModel:
|
| 168 |
+
super().__init__(config)
|
| 169 |
+
self.padding_idx = config.pad_token_id
|
| 170 |
+
self.vocab_size = config.vocab_size
|
| 171 |
+
|
| 172 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 173 |
+
self.layers = nn.ModuleList([TOPTransformerBlock(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 174 |
+
self.norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 175 |
+
|
| 176 |
+
self.gradient_checkpointing = False
|
| 177 |
+
|
| 178 |
+
self.post_init()
|
| 179 |
+
|
| 180 |
+
def get_input_embeddings(self):
|
| 181 |
+
return self.embeddings
|
| 182 |
+
|
| 183 |
+
def set_input_embeddings(self, value):
|
| 184 |
+
self.embeddings = value
|
| 185 |
+
|
| 186 |
+
def forward(
|
| 187 |
+
self,
|
| 188 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 189 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 190 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
| 191 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 192 |
+
use_cache: Optional[bool] = None,
|
| 193 |
+
output_attentions: Optional[bool] = None,
|
| 194 |
+
output_hidden_states: Optional[bool] = None,
|
| 195 |
+
return_dict: Optional[bool] = None,
|
| 196 |
+
**kwargs: Unpack[Any]
|
| 197 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 198 |
+
if output_attentions:
|
| 199 |
+
warnings.warn(
|
| 200 |
+
"`TOPTransformerModel` does not support output attention weights now, so `output_attentions` is set to `False`."
|
| 201 |
+
)
|
| 202 |
+
output_attentions = False
|
| 203 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 204 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 205 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 206 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 207 |
+
|
| 208 |
+
# retrieve input_ids and inputs_embeds
|
| 209 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 210 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 211 |
+
elif input_ids is None and inputs_embeds is None:
|
| 212 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 213 |
+
|
| 214 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 215 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 216 |
+
|
| 217 |
+
if inputs_embeds is None:
|
| 218 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 219 |
+
|
| 220 |
+
# embed positions
|
| 221 |
+
hidden_states = inputs_embeds
|
| 222 |
+
|
| 223 |
+
if self.gradient_checkpointing and self.training:
|
| 224 |
+
if use_cache:
|
| 225 |
+
logger.warning_once(
|
| 226 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
| 227 |
+
)
|
| 228 |
+
use_cache = False
|
| 229 |
+
|
| 230 |
+
all_hidden_states = () if output_hidden_states else None
|
| 231 |
+
all_attns = () if output_attentions else None
|
| 232 |
+
next_cache = None
|
| 233 |
+
|
| 234 |
+
for layer in self.layers:
|
| 235 |
+
if output_hidden_states:
|
| 236 |
+
all_hidden_states += (hidden_states,)
|
| 237 |
+
|
| 238 |
+
if self.gradient_checkpointing and self.training:
|
| 239 |
+
layer_outputs = self._gradient_checkpointing_func(
|
| 240 |
+
layer.__call__,
|
| 241 |
+
hidden_states,
|
| 242 |
+
attention_mask,
|
| 243 |
+
past_key_values,
|
| 244 |
+
output_attentions,
|
| 245 |
+
use_cache,
|
| 246 |
+
**kwargs
|
| 247 |
+
)
|
| 248 |
+
else:
|
| 249 |
+
layer_outputs = layer(
|
| 250 |
+
hidden_states,
|
| 251 |
+
attention_mask=attention_mask,
|
| 252 |
+
past_key_values=past_key_values,
|
| 253 |
+
output_attentions=output_attentions,
|
| 254 |
+
use_cache=use_cache,
|
| 255 |
+
**kwargs
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
hidden_states = layer_outputs[0]
|
| 259 |
+
|
| 260 |
+
if use_cache:
|
| 261 |
+
next_cache = layer_outputs[2 if output_attentions else 1]
|
| 262 |
+
|
| 263 |
+
if output_attentions:
|
| 264 |
+
all_attns += (layer_outputs[1],)
|
| 265 |
+
|
| 266 |
+
hidden_states = self.norm(hidden_states)
|
| 267 |
+
|
| 268 |
+
# add hidden states from the last decoder layer
|
| 269 |
+
if output_hidden_states:
|
| 270 |
+
all_hidden_states += (hidden_states,)
|
| 271 |
+
|
| 272 |
+
if not return_dict:
|
| 273 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_attns] if v is not None)
|
| 274 |
+
|
| 275 |
+
return BaseModelOutputWithPast(
|
| 276 |
+
last_hidden_state=hidden_states,
|
| 277 |
+
past_key_values=next_cache,
|
| 278 |
+
hidden_states=all_hidden_states,
|
| 279 |
+
attentions=all_attns
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
class TOPTransformerForCausalLM(TOPTransformerPreTrainedModel, GenerationMixin):
|
| 284 |
+
|
| 285 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 286 |
+
|
| 287 |
+
def __init__(self, config):
|
| 288 |
+
super().__init__(config)
|
| 289 |
+
self.model = TOPTransformerModel(config)
|
| 290 |
+
self.vocab_size = config.vocab_size
|
| 291 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 292 |
+
if config.use_top_loss:
|
| 293 |
+
self.top_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 294 |
+
self.top_criterion = FusedLinearListNetLoss()
|
| 295 |
+
self.top_window_size = config.top_window_size
|
| 296 |
+
self.criterion = None
|
| 297 |
+
self.pad_token_id = config.pad_token_id
|
| 298 |
+
|
| 299 |
+
# Initialize weights and apply final processing
|
| 300 |
+
self.post_init()
|
| 301 |
+
|
| 302 |
+
def get_input_embeddings(self):
|
| 303 |
+
return self.model.embeddings
|
| 304 |
+
|
| 305 |
+
def set_input_embeddings(self, value):
|
| 306 |
+
self.model.embeddings = value
|
| 307 |
+
|
| 308 |
+
def get_output_embeddings(self):
|
| 309 |
+
return self.lm_head
|
| 310 |
+
|
| 311 |
+
def set_output_embeddings(self, new_embeddings):
|
| 312 |
+
self.lm_head = new_embeddings
|
| 313 |
+
|
| 314 |
+
def set_decoder(self, decoder):
|
| 315 |
+
self.model = decoder
|
| 316 |
+
|
| 317 |
+
def get_decoder(self):
|
| 318 |
+
return self.model
|
| 319 |
+
|
| 320 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 321 |
+
def prepare_inputs_for_generation(
|
| 322 |
+
self,
|
| 323 |
+
input_ids: torch.LongTensor = None,
|
| 324 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 325 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 326 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 327 |
+
use_cache: bool = True,
|
| 328 |
+
logits_to_keep: Optional[int] = None,
|
| 329 |
+
**kwargs
|
| 330 |
+
):
|
| 331 |
+
# only last token for `inputs_ids` if the `past_key_values` is not empty.
|
| 332 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 333 |
+
input_ids = input_ids[:, -1:]
|
| 334 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 335 |
+
if inputs_embeds is not None and len(past_key_values) == 0:
|
| 336 |
+
model_inputs = {'inputs_embeds': inputs_embeds}
|
| 337 |
+
else:
|
| 338 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 339 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 340 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 341 |
+
# TODO: use `next_tokens` directly instead.
|
| 342 |
+
model_inputs = {'input_ids': input_ids.contiguous()}
|
| 343 |
+
|
| 344 |
+
if logits_to_keep is not None:
|
| 345 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 346 |
+
|
| 347 |
+
model_inputs.update({
|
| 348 |
+
'past_key_values': past_key_values,
|
| 349 |
+
'use_cache': use_cache,
|
| 350 |
+
'attention_mask': attention_mask,
|
| 351 |
+
})
|
| 352 |
+
return model_inputs
|
| 353 |
+
|
| 354 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 355 |
+
def forward(
|
| 356 |
+
self,
|
| 357 |
+
input_ids: torch.LongTensor = None,
|
| 358 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 359 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 360 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 361 |
+
labels: Optional[torch.LongTensor] = None,
|
| 362 |
+
use_cache: Optional[bool] = None,
|
| 363 |
+
output_attentions: Optional[bool] = None,
|
| 364 |
+
output_hidden_states: Optional[bool] = None,
|
| 365 |
+
return_dict: Optional[bool] = None,
|
| 366 |
+
logits_to_keep: Optional[int] = 0,
|
| 367 |
+
**kwargs: Unpack[Any]
|
| 368 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 369 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 370 |
+
output_hidden_states = (
|
| 371 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 372 |
+
)
|
| 373 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 374 |
+
|
| 375 |
+
outputs = self.model(
|
| 376 |
+
input_ids=input_ids,
|
| 377 |
+
attention_mask=attention_mask,
|
| 378 |
+
past_key_values=past_key_values,
|
| 379 |
+
inputs_embeds=inputs_embeds,
|
| 380 |
+
use_cache=use_cache,
|
| 381 |
+
output_attentions=output_attentions,
|
| 382 |
+
output_hidden_states=output_hidden_states,
|
| 383 |
+
return_dict=return_dict,
|
| 384 |
+
**kwargs
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
hidden_states = outputs[0]
|
| 388 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 389 |
+
logits = None if fuse_linear_and_cross_entropy else self.lm_head(hidden_states[:, -logits_to_keep:])
|
| 390 |
+
|
| 391 |
+
loss = None
|
| 392 |
+
ntp_loss = None
|
| 393 |
+
top_loss = None
|
| 394 |
+
if labels is not None:
|
| 395 |
+
if getattr(self, 'criterion', None) is None:
|
| 396 |
+
if fuse_linear_and_cross_entropy:
|
| 397 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 398 |
+
elif self.config.fuse_cross_entropy:
|
| 399 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 400 |
+
else:
|
| 401 |
+
criterion = nn.CrossEntropyLoss()
|
| 402 |
+
else:
|
| 403 |
+
criterion = self.criterion
|
| 404 |
+
# Enable model parallelism
|
| 405 |
+
labels = labels.to(hidden_states.device)
|
| 406 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 407 |
+
ntp_labels = labels[..., :hidden_states.shape[1]].contiguous()
|
| 408 |
+
if fuse_linear_and_cross_entropy:
|
| 409 |
+
ntp_loss = criterion(hidden_states, ntp_labels, self.lm_head.weight, self.lm_head.bias)
|
| 410 |
+
else:
|
| 411 |
+
ntp_loss = criterion(logits.view(ntp_labels.numel(), -1), ntp_labels.reshape(-1))
|
| 412 |
+
|
| 413 |
+
if self.config.use_top_loss:
|
| 414 |
+
top_labels = seq_to_top(labels, vocab_size=self.vocab_size, window_size=self.top_window_size, pad_token_id=self.pad_token_id).contiguous()
|
| 415 |
+
top_loss = self.top_criterion(hidden_states, top_labels, self.top_head.weight, self.top_head.bias)
|
| 416 |
+
# print(f"NTP Loss: {ntp_loss.item()}, TOP Loss: {top_loss.item()}")
|
| 417 |
+
# For debugging, get the index where the top label is the highest and print the corresponding logits
|
| 418 |
+
# idx_max = torch.argmax(top_labels.view(-1, self.vocab_size), dim=1)
|
| 419 |
+
# # Print the labels and logits at that index
|
| 420 |
+
# print(f"Labels: {top_labels.view(-1, self.vocab_size)[0, idx_max[0]-3:idx_max[0]+3]}")
|
| 421 |
+
# print(f"Logits: {F.sigmoid(top_logits).view(-1, self.vocab_size)[0, idx_max[0]-3:idx_max[0]+3]}")
|
| 422 |
+
loss = ntp_loss + top_loss
|
| 423 |
+
else:
|
| 424 |
+
loss = ntp_loss
|
| 425 |
+
|
| 426 |
+
if not return_dict:
|
| 427 |
+
output = (logits,) + outputs[1:]
|
| 428 |
+
return (loss,) + output if loss is not None else output
|
| 429 |
+
|
| 430 |
+
return TOPLMOutputWithPast(
|
| 431 |
+
loss=loss,
|
| 432 |
+
ntp_loss=ntp_loss,
|
| 433 |
+
top_loss=top_loss,
|
| 434 |
+
logits=logits,
|
| 435 |
+
past_key_values=outputs.past_key_values,
|
| 436 |
+
hidden_states=outputs.hidden_states,
|
| 437 |
+
attentions=outputs.attentions,
|
| 438 |
+
)
|
fla/models/utils.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import transformers
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Cache(transformers.cache_utils.Cache):
|
| 12 |
+
"""
|
| 13 |
+
A cache used for storing hidden states produced by flash linear attention models.
|
| 14 |
+
|
| 15 |
+
It stores the states of each layer as the tensor of shape `[batch_size, key_dim, value_dim]`.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
is_compileable = True
|
| 19 |
+
|
| 20 |
+
def __init__(
|
| 21 |
+
self,
|
| 22 |
+
seen_tokens: int = 0
|
| 23 |
+
) -> Cache:
|
| 24 |
+
super().__init__()
|
| 25 |
+
|
| 26 |
+
self.states: List[Dict[str, Any]] = []
|
| 27 |
+
|
| 28 |
+
self._seen_tokens = seen_tokens # Used in `generate` to keep tally of how many tokens the cache has seen
|
| 29 |
+
|
| 30 |
+
def __getitem__(self, layer_idx: int) -> Dict[str, Any]:
|
| 31 |
+
if layer_idx < len(self):
|
| 32 |
+
return self.states[layer_idx]
|
| 33 |
+
else:
|
| 34 |
+
raise KeyError(f"Cache only has {len(self)} layers, attempted to access layer with index {layer_idx}")
|
| 35 |
+
|
| 36 |
+
def __iter__(self):
|
| 37 |
+
for state in self.states:
|
| 38 |
+
yield state
|
| 39 |
+
|
| 40 |
+
def __len__(self):
|
| 41 |
+
return len(self.states)
|
| 42 |
+
|
| 43 |
+
def update(
|
| 44 |
+
self,
|
| 45 |
+
recurrent_state: torch.Tensor = None,
|
| 46 |
+
attn_state: Tuple[torch.Tensor, torch.Tensor] = None,
|
| 47 |
+
conv_state: Tuple[torch.Tensor] = None,
|
| 48 |
+
ffn_state: torch.Tensor = None,
|
| 49 |
+
layer_idx: int = 0,
|
| 50 |
+
offset: Optional[int] = 1,
|
| 51 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 52 |
+
) -> Dict[str, Any]:
|
| 53 |
+
"""
|
| 54 |
+
Updates the cache with the new `recurrent_state`/`attn_state`/`conv_state` for the layer `layer_idx`.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
recurrent_state (`torch.Tensor`, `optional`):
|
| 58 |
+
The new recurrent state to cache.
|
| 59 |
+
attn_state (`Tuple[torch.Tensor, torch.Tensor]`, `optional`):
|
| 60 |
+
The new attention key/value states to cache.
|
| 61 |
+
conv_state (`Tuple[torch.Tensor]`, `optional`):
|
| 62 |
+
The new convolution state to cache.
|
| 63 |
+
layer_idx (`int`, defaults to 0):
|
| 64 |
+
The index of the layer to cache the states for.
|
| 65 |
+
offset (`int`, `optional`, defaults to 1):
|
| 66 |
+
The number of new tokens being processed.
|
| 67 |
+
cache_kwargs (`Dict[str, Any]`, `optional`):
|
| 68 |
+
Additional arguments for the cache subclass.
|
| 69 |
+
|
| 70 |
+
Return:
|
| 71 |
+
Dictionary of the updated state.
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
# Update the number of seen tokens
|
| 75 |
+
if layer_idx == 0:
|
| 76 |
+
self._seen_tokens += offset
|
| 77 |
+
|
| 78 |
+
if attn_state is not None:
|
| 79 |
+
input_size = attn_state[0].shape[-2]
|
| 80 |
+
window_size = cache_kwargs.get('window_size', None)
|
| 81 |
+
if not isinstance(attn_state, Tuple) or len(attn_state) != 2:
|
| 82 |
+
raise ValueError("`attn_state` must be a tuple of two tensors for key/value states")
|
| 83 |
+
if len(self.states) <= layer_idx:
|
| 84 |
+
if attn_state is not None:
|
| 85 |
+
if window_size is not None and input_size > window_size:
|
| 86 |
+
attn_state = (attn_state[0][..., -window_size:, :].contiguous(),
|
| 87 |
+
attn_state[1][..., -window_size:, :].contiguous())
|
| 88 |
+
state = dict(
|
| 89 |
+
recurrent_state=recurrent_state,
|
| 90 |
+
attn_state=attn_state,
|
| 91 |
+
conv_state=conv_state,
|
| 92 |
+
ffn_state=ffn_state
|
| 93 |
+
)
|
| 94 |
+
self.states.append(state)
|
| 95 |
+
else:
|
| 96 |
+
state = self.states[layer_idx]
|
| 97 |
+
if recurrent_state is not None:
|
| 98 |
+
state['recurrent_state'] = recurrent_state
|
| 99 |
+
if attn_state is not None:
|
| 100 |
+
key_state, value_state = state['attn_state']
|
| 101 |
+
if window_size is not None and key_state.shape[-2] == window_size:
|
| 102 |
+
# DO NOT allocate new memory if the cache is full
|
| 103 |
+
# roll the key/value states to the left by `input_size`
|
| 104 |
+
key_state = key_state.roll(-input_size, -2)
|
| 105 |
+
value_state = value_state.roll(-input_size, -2)
|
| 106 |
+
# replace the last `input_size` tokens with the new key/value states
|
| 107 |
+
key_state[..., -input_size:, :] = attn_state[0]
|
| 108 |
+
value_state[..., -input_size:, :] = attn_state[1]
|
| 109 |
+
attn_state = (key_state, value_state)
|
| 110 |
+
else:
|
| 111 |
+
attn_state = (torch.cat([key_state, attn_state[0]], -2),
|
| 112 |
+
torch.cat([value_state, attn_state[1]], -2),)
|
| 113 |
+
state['attn_state'] = attn_state
|
| 114 |
+
if conv_state is not None:
|
| 115 |
+
state['conv_state'] = conv_state
|
| 116 |
+
if ffn_state is not None:
|
| 117 |
+
state['ffn_state'] = ffn_state
|
| 118 |
+
|
| 119 |
+
return state
|
| 120 |
+
|
| 121 |
+
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
|
| 122 |
+
"""Returns the sequence length of the cached states. A layer index can be optionally passed."""
|
| 123 |
+
if len(self.states) <= layer_idx:
|
| 124 |
+
return 0
|
| 125 |
+
return self._seen_tokens
|
| 126 |
+
|
| 127 |
+
def get_max_length(self) -> Optional[int]:
|
| 128 |
+
"""Returns the maximum sequence length of the cached states. Cache does not have a maximum length."""
|
| 129 |
+
return None
|
| 130 |
+
|
| 131 |
+
def to_legacy_cache(self) -> Tuple:
|
| 132 |
+
return tuple(self.states)
|
| 133 |
+
|
| 134 |
+
@classmethod
|
| 135 |
+
@torch.compiler.disable
|
| 136 |
+
def from_legacy_cache(
|
| 137 |
+
cls,
|
| 138 |
+
past_key_values: Optional[Tuple] = None,
|
| 139 |
+
seen_tokens: int = 0
|
| 140 |
+
) -> Cache:
|
| 141 |
+
"""Converts a cache in the legacy cache format into an equivalent `Cache`."""
|
| 142 |
+
|
| 143 |
+
cache = cls(seen_tokens)
|
| 144 |
+
if isinstance(past_key_values, list):
|
| 145 |
+
for layer_idx in range(len(past_key_values)):
|
| 146 |
+
cache.states.append(past_key_values[layer_idx])
|
| 147 |
+
return cache
|
fla/modules/convolution.py
ADDED
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# from https://github.com/HazyResearch/zoology/blob/main/zoology/mixers/convolution.py
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import warnings
|
| 7 |
+
from typing import Optional, Tuple
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import torch.nn.functional as F
|
| 12 |
+
import triton
|
| 13 |
+
import triton.language as tl
|
| 14 |
+
from einops import rearrange
|
| 15 |
+
|
| 16 |
+
from fla.modules.activations import ACT2FN
|
| 17 |
+
from fla.ops.common.utils import prepare_position_ids, prepare_sequence_ids
|
| 18 |
+
from fla.utils import checkpoint, input_guard
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
from causal_conv1d import causal_conv1d_fn, causal_conv1d_update
|
| 22 |
+
except ImportError:
|
| 23 |
+
causal_conv1d_fn = None
|
| 24 |
+
causal_conv1d_update = None
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def fft_conv(u, k, dropout_mask, gelu=True, k_rev=None):
|
| 28 |
+
seqlen = u.shape[-1]
|
| 29 |
+
fft_size = 2 * seqlen
|
| 30 |
+
k_f = torch.fft.rfft(k, n=fft_size) / fft_size
|
| 31 |
+
if k_rev is not None:
|
| 32 |
+
k_rev_f = torch.fft.rfft(k_rev, n=fft_size) / fft_size
|
| 33 |
+
k_f = k_f + k_rev_f.conj()
|
| 34 |
+
u_f = torch.fft.rfft(u.to(dtype=k.dtype), n=fft_size)
|
| 35 |
+
|
| 36 |
+
if len(u.shape) > 3:
|
| 37 |
+
k_f = k_f.unsqueeze(1)
|
| 38 |
+
y = torch.fft.irfft(u_f * k_f, n=fft_size, norm="forward")[..., :seqlen]
|
| 39 |
+
|
| 40 |
+
out = y + u
|
| 41 |
+
if gelu:
|
| 42 |
+
out = F.gelu(out)
|
| 43 |
+
if dropout_mask is not None:
|
| 44 |
+
return (out * rearrange(dropout_mask, "b H -> b H 1")).to(dtype=u.dtype)
|
| 45 |
+
else:
|
| 46 |
+
return out.to(dtype=u.dtype)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@checkpoint
|
| 50 |
+
def proj_then_conv1d(
|
| 51 |
+
x: torch.Tensor,
|
| 52 |
+
proj_weight: torch.Tensor,
|
| 53 |
+
conv1d_weight: torch.Tensor,
|
| 54 |
+
conv1d_bias: Optional[torch.Tensor] = None,
|
| 55 |
+
cache: Optional[torch.Tensor] = None
|
| 56 |
+
) -> torch.Tensor:
|
| 57 |
+
# We do matmul and transpose BLH -> HBL at the same time
|
| 58 |
+
x = rearrange(proj_weight @ rearrange(x, "b t d -> d (b t)"), "d (b t) -> b d t", t=x.shape[-2])
|
| 59 |
+
|
| 60 |
+
if causal_conv1d_fn is None:
|
| 61 |
+
raise ImportError("`causal_conv1d_fn` is not available. Please install `causal-conv1d` first.")
|
| 62 |
+
if cache is None:
|
| 63 |
+
x = causal_conv1d_fn(
|
| 64 |
+
x=x,
|
| 65 |
+
weight=rearrange(conv1d_weight, "d 1 w -> d w"),
|
| 66 |
+
bias=conv1d_bias,
|
| 67 |
+
activation="silu",
|
| 68 |
+
).transpose(1, 2)
|
| 69 |
+
else:
|
| 70 |
+
assert x.shape[-1] == 1, "Only support decoding with 1 token at a time for now"
|
| 71 |
+
x = x.squeeze(-1)
|
| 72 |
+
x = causal_conv1d_update(
|
| 73 |
+
x=x,
|
| 74 |
+
weight=rearrange(conv1d_weight, "d 1 w -> d w"),
|
| 75 |
+
bias=conv1d_bias,
|
| 76 |
+
cache=cache,
|
| 77 |
+
activation="silu",
|
| 78 |
+
)
|
| 79 |
+
return x
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@triton.jit
|
| 83 |
+
def causal_conv1d_varlen_states_fwd_kernel(
|
| 84 |
+
x,
|
| 85 |
+
cache,
|
| 86 |
+
offsets,
|
| 87 |
+
D,
|
| 88 |
+
W,
|
| 89 |
+
BD: tl.constexpr,
|
| 90 |
+
BW: tl.constexpr
|
| 91 |
+
):
|
| 92 |
+
i_d, i_w, i_n = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 93 |
+
eos = tl.load(offsets + i_n + 1)
|
| 94 |
+
bos = tl.maximum(tl.load(offsets + i_n), eos - W)
|
| 95 |
+
o_t = eos - (i_w + 1) * BW + tl.arange(0, BW)
|
| 96 |
+
o_d = i_d * BD + tl.arange(0, BD)
|
| 97 |
+
o_w = W - (i_w + 1) * BW + tl.arange(0, BW)
|
| 98 |
+
|
| 99 |
+
b_x = tl.load(x + o_t * D + o_d[:, None], mask=(o_t >= bos) & (o_d[:, None] < D), other=0)
|
| 100 |
+
tl.store(cache + i_n * D*W + o_d[:, None] * W + o_w, b_x, mask=(o_d[:, None] < D) & (o_w >= 0))
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@input_guard
|
| 104 |
+
def causal_conv1d_varlen_states_fwd(
|
| 105 |
+
x: torch.Tensor,
|
| 106 |
+
cache: torch.Tensor,
|
| 107 |
+
cu_seqlens: torch.Tensor,
|
| 108 |
+
state_len: int
|
| 109 |
+
) -> torch.Tensor:
|
| 110 |
+
N, D, W = len(cu_seqlens) - 1, x.shape[-1], state_len
|
| 111 |
+
cache = torch.empty(N, D, W, dtype=x.dtype, device=x.device) if cache is None else cache
|
| 112 |
+
BD = min(triton.next_power_of_2(D), 256)
|
| 113 |
+
BW = min(triton.next_power_of_2(state_len), 16)
|
| 114 |
+
grid = (triton.cdiv(D, BD), triton.cdiv(W, BW), N)
|
| 115 |
+
with torch.cuda.device(x.device.index):
|
| 116 |
+
causal_conv1d_varlen_states_fwd_kernel[grid](
|
| 117 |
+
x=x,
|
| 118 |
+
cache=cache,
|
| 119 |
+
offsets=cu_seqlens,
|
| 120 |
+
D=D,
|
| 121 |
+
W=W,
|
| 122 |
+
BW=BW,
|
| 123 |
+
BD=BD
|
| 124 |
+
)
|
| 125 |
+
return cache
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
class ShortConvolution(nn.Conv1d):
|
| 129 |
+
"""
|
| 130 |
+
Simple wrapper around `nn.Conv1d` that accepts dimension last.
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
def __init__(
|
| 134 |
+
self,
|
| 135 |
+
hidden_size: int,
|
| 136 |
+
kernel_size: int,
|
| 137 |
+
bias: bool = False,
|
| 138 |
+
activation: Optional[str] = 'silu',
|
| 139 |
+
use_fast_conv1d: Optional[bool] = True,
|
| 140 |
+
device: Optional[torch.device] = None,
|
| 141 |
+
dtype: Optional[torch.dtype] = None,
|
| 142 |
+
):
|
| 143 |
+
super().__init__(
|
| 144 |
+
in_channels=hidden_size,
|
| 145 |
+
out_channels=hidden_size,
|
| 146 |
+
kernel_size=kernel_size,
|
| 147 |
+
groups=hidden_size,
|
| 148 |
+
bias=bias,
|
| 149 |
+
padding=kernel_size - 1,
|
| 150 |
+
device=device,
|
| 151 |
+
dtype=dtype,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
self.hidden_size = hidden_size
|
| 155 |
+
self.activation = None
|
| 156 |
+
if activation is not None:
|
| 157 |
+
assert activation in ['silu', 'swish'], f"Activation `{activation}` not supported yet."
|
| 158 |
+
self.activation = activation
|
| 159 |
+
|
| 160 |
+
if causal_conv1d_fn is None:
|
| 161 |
+
if use_fast_conv1d:
|
| 162 |
+
raise RuntimeError(
|
| 163 |
+
"Please either install `causal-conv1d>=1.4.0` to enable fast causal short convolution CUDA kernel "
|
| 164 |
+
"or set `use_fast_conv1d` to False"
|
| 165 |
+
)
|
| 166 |
+
else:
|
| 167 |
+
warnings.warn(
|
| 168 |
+
"The naive Pytorch verison is very slow in practice, "
|
| 169 |
+
"please run `pip install causal-conv1d>=1.4.0` to install fast causal short convolution CUDA kernel",
|
| 170 |
+
category=ImportWarning
|
| 171 |
+
)
|
| 172 |
+
self.use_fast_conv1d = use_fast_conv1d
|
| 173 |
+
|
| 174 |
+
def extra_repr(self):
|
| 175 |
+
s = ('{in_channels}, {out_channels}, kernel_size={kernel_size}'
|
| 176 |
+
', stride={stride}')
|
| 177 |
+
if self.padding != (0,) * len(self.padding):
|
| 178 |
+
s += ', padding={padding}'
|
| 179 |
+
if self.dilation != (1,) * len(self.dilation):
|
| 180 |
+
s += ', dilation={dilation}'
|
| 181 |
+
if self.output_padding != (0,) * len(self.output_padding):
|
| 182 |
+
s += ', output_padding={output_padding}'
|
| 183 |
+
if self.groups != 1:
|
| 184 |
+
s += ', groups={groups}'
|
| 185 |
+
if self.bias is None:
|
| 186 |
+
s += ', bias=False'
|
| 187 |
+
if self.padding_mode != 'zeros':
|
| 188 |
+
s += ', padding_mode={padding_mode}'
|
| 189 |
+
if self.activation is not None:
|
| 190 |
+
s += ', activation={activation}'
|
| 191 |
+
if not self.use_fast_conv1d:
|
| 192 |
+
s += ', use_fast_conv1d={use_fast_conv1d}'
|
| 193 |
+
return s.format(**self.__dict__)
|
| 194 |
+
|
| 195 |
+
def forward(
|
| 196 |
+
self,
|
| 197 |
+
x: torch.Tensor,
|
| 198 |
+
mask: Optional[torch.Tensor] = None,
|
| 199 |
+
cache: Optional[torch.Tensor] = None,
|
| 200 |
+
output_final_state: bool = False,
|
| 201 |
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
| 202 |
+
**kwargs,
|
| 203 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 204 |
+
"""
|
| 205 |
+
Args:
|
| 206 |
+
x (`torch.Tensor`):
|
| 207 |
+
Tensor of shape `[B, T, D]`.
|
| 208 |
+
If `seq_idx` is provided, `B` must be 1.
|
| 209 |
+
mask (`Optional[torch.Tensor]`):
|
| 210 |
+
Attention mask dealing with padded positions.
|
| 211 |
+
cache (`Optional[torch.Tensor]`):
|
| 212 |
+
Previous cache tensor of shape `[N, D, W]`, where `W` is the kernel size.
|
| 213 |
+
If provided, the cache is updated **inplace**.
|
| 214 |
+
output_final_state (Optional[bool]):
|
| 215 |
+
Whether to output the final state of shape `[N, D, W]`. Default: `False`.
|
| 216 |
+
cu_seqlens (Optional[torch.LongTensor]):
|
| 217 |
+
Cumulative sequence lengths for each batch. Used for varlen. Default: `None`.
|
| 218 |
+
Shape: [B+1]
|
| 219 |
+
|
| 220 |
+
Returns:
|
| 221 |
+
Tensor of shape `[B, T, D]`.
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
B, T, D, W = *x.shape, self.kernel_size[0]
|
| 225 |
+
N = B if cu_seqlens is None else len(cu_seqlens) - 1
|
| 226 |
+
if mask is not None:
|
| 227 |
+
if cu_seqlens is not None:
|
| 228 |
+
raise ValueError("`mask` and `cu_seqlens` cannot be provided at the same time")
|
| 229 |
+
x = x.mul_(mask.unsqueeze(-1))
|
| 230 |
+
if output_final_state and cache is None:
|
| 231 |
+
cache = x.new_zeros(N, D, W)
|
| 232 |
+
# during the decoding phase, we assume the batch is composed of sequences of length 1
|
| 233 |
+
if cache is not None and B * T == N:
|
| 234 |
+
return self.step(x, cache, cu_seqlens)
|
| 235 |
+
|
| 236 |
+
if cache is not None:
|
| 237 |
+
if cu_seqlens is not None:
|
| 238 |
+
cache = causal_conv1d_varlen_states_fwd(x, cache, cu_seqlens, W)
|
| 239 |
+
else:
|
| 240 |
+
cache[:, :, -min(W, T):].copy_(rearrange(x[..., -min(W, T):, :], 'n w d -> n d w'))
|
| 241 |
+
|
| 242 |
+
x = rearrange(x, 'b t d -> b d t')
|
| 243 |
+
if self.use_fast_conv1d:
|
| 244 |
+
# Sequence index for each token. Used for varlen.
|
| 245 |
+
# Suppose a batch consists of two sequences with lengths 3 and 4,
|
| 246 |
+
# seq_idx=[0, 0, 0, 1, 1, 1, 1] for this batch.
|
| 247 |
+
# NOTE: No need to provide this arg if `cu_seqlens` is passed.
|
| 248 |
+
# This arg is just for BC, and will be removed in the future.
|
| 249 |
+
# [B, T]
|
| 250 |
+
seq_idx = kwargs.get('seq_idx', None)
|
| 251 |
+
if cu_seqlens is not None and seq_idx is None:
|
| 252 |
+
seq_idx = prepare_sequence_ids(prepare_position_ids(cu_seqlens)).to(torch.int32).unsqueeze(0)
|
| 253 |
+
x = causal_conv1d_fn(
|
| 254 |
+
x=x,
|
| 255 |
+
weight=rearrange(self.weight, "d 1 w -> d w"),
|
| 256 |
+
bias=self.bias,
|
| 257 |
+
activation=self.activation,
|
| 258 |
+
seq_idx=seq_idx,
|
| 259 |
+
)
|
| 260 |
+
else:
|
| 261 |
+
if cu_seqlens is not None:
|
| 262 |
+
raise ValueError("`cu_seqlens` is not supported for the naive Pytorch version")
|
| 263 |
+
x = self._conv_forward(x, self.weight, self.bias)[..., :x.shape[-1]]
|
| 264 |
+
if self.activation is not None:
|
| 265 |
+
x = ACT2FN[self.activation](x)
|
| 266 |
+
return rearrange(x, "b d t -> b t d"), cache
|
| 267 |
+
|
| 268 |
+
def step(
|
| 269 |
+
self,
|
| 270 |
+
x: torch.Tensor,
|
| 271 |
+
cache: torch.Tensor,
|
| 272 |
+
cu_seqlens: Optional[torch.LongTensor] = None
|
| 273 |
+
):
|
| 274 |
+
shape = x.shape
|
| 275 |
+
x = x.squeeze(0) if cu_seqlens is not None else x.squeeze(1)
|
| 276 |
+
if self.use_fast_conv1d:
|
| 277 |
+
x = causal_conv1d_update(
|
| 278 |
+
x=x,
|
| 279 |
+
conv_state=cache,
|
| 280 |
+
weight=rearrange(self.weight, "d 1 w -> d w"),
|
| 281 |
+
bias=self.bias,
|
| 282 |
+
activation=self.activation,
|
| 283 |
+
)
|
| 284 |
+
else:
|
| 285 |
+
dtype = x.dtype
|
| 286 |
+
# we follow the fast mode that updates the cache in-place
|
| 287 |
+
cache.copy_(cache.roll(shifts=-1, dims=-1))
|
| 288 |
+
cache[:, :, -1] = x
|
| 289 |
+
x = torch.sum(cache * rearrange(self.weight, "d 1 w -> d w"), dim=-1)
|
| 290 |
+
if self.bias is not None:
|
| 291 |
+
x = x + self.bias
|
| 292 |
+
if self.activation is not None:
|
| 293 |
+
x = ACT2FN[self.activation](x).to(dtype=dtype)
|
| 294 |
+
return x.view(shape), cache
|
| 295 |
+
|
| 296 |
+
@property
|
| 297 |
+
def state_size(self) -> int:
|
| 298 |
+
return self.hidden_size * self.kernel_size
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
class LongConvolution(nn.Module):
|
| 302 |
+
"""
|
| 303 |
+
LongConvolution applies a convolution operation on the input tensor using a fixed
|
| 304 |
+
filter of length max_len.
|
| 305 |
+
The filter is learned during training and is applied using FFT convolution.
|
| 306 |
+
Args:
|
| 307 |
+
hidden_size (int): The number of expected features in the input and output.
|
| 308 |
+
max_len (int): The maximum sequence length.
|
| 309 |
+
Returns:
|
| 310 |
+
y: [batch_size, seq_len, hidden_size] tensor
|
| 311 |
+
"""
|
| 312 |
+
|
| 313 |
+
def __init__(
|
| 314 |
+
self,
|
| 315 |
+
hidden_size: int,
|
| 316 |
+
max_len: int,
|
| 317 |
+
**kwargs,
|
| 318 |
+
):
|
| 319 |
+
"""
|
| 320 |
+
Initializes the LongConvolution module.
|
| 321 |
+
Args:
|
| 322 |
+
hidden_size (int): The number of expected features in the input and output.
|
| 323 |
+
max_len (int): The maximum sequence length.
|
| 324 |
+
"""
|
| 325 |
+
super().__init__()
|
| 326 |
+
self.hidden_size = hidden_size
|
| 327 |
+
self.filter = nn.Parameter(torch.randn(self.hidden_size, max_len), requires_grad=True)
|
| 328 |
+
|
| 329 |
+
def forward(self, x: torch.Tensor, *args, **kwargs):
|
| 330 |
+
"""
|
| 331 |
+
Applies the LongConvolution operation on the input tensor.
|
| 332 |
+
Args:
|
| 333 |
+
x: [batch_size, seq_len, hidden_size] tensor
|
| 334 |
+
Returns:
|
| 335 |
+
y: [batch_size, seq_len, hidden_size] tensor
|
| 336 |
+
"""
|
| 337 |
+
x = x.transpose(1, 2)
|
| 338 |
+
y = fft_conv(x, self.filter, dropout_mask=None, gelu=False)
|
| 339 |
+
y = y.transpose(1, 2)
|
| 340 |
+
return y.to(dtype=x.dtype)
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
class PositionalEmbedding(nn.Module):
|
| 344 |
+
def __init__(self, emb_dim: int, seq_len: int, **kwargs):
|
| 345 |
+
"""Complex exponential positional embeddings for implicit long convolution filters."""
|
| 346 |
+
super().__init__()
|
| 347 |
+
|
| 348 |
+
self.seq_len = seq_len
|
| 349 |
+
# The time embedding fed to the filteres is normalized so that t_f = 1
|
| 350 |
+
t = torch.linspace(0, 1, self.seq_len)[None, :, None] # 1, L, 1
|
| 351 |
+
|
| 352 |
+
if emb_dim > 1:
|
| 353 |
+
bands = (emb_dim - 1) // 2
|
| 354 |
+
# To compute the right embeddings we use the "proper" linspace
|
| 355 |
+
t_rescaled = torch.linspace(0, seq_len - 1, seq_len)[None, :, None]
|
| 356 |
+
w = 2 * math.pi * t_rescaled / seq_len # 1, L, 1
|
| 357 |
+
|
| 358 |
+
f = torch.linspace(1e-4, bands - 1, bands)[None, None]
|
| 359 |
+
z = torch.exp(-1j * f * w)
|
| 360 |
+
z = torch.cat([t, z.real, z.imag], dim=-1)
|
| 361 |
+
self.z = nn.Parameter(z, requires_grad=False)
|
| 362 |
+
|
| 363 |
+
def forward(self, L):
|
| 364 |
+
return self.z[:, :L]
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
class ImplicitLongConvolution(nn.Module):
|
| 368 |
+
"""
|
| 369 |
+
Long convolution with implicit filter parameterized by an MLP.
|
| 370 |
+
|
| 371 |
+
Args:
|
| 372 |
+
hidden_size (int):
|
| 373 |
+
The number of expected features in the input and output.
|
| 374 |
+
max_len (int):
|
| 375 |
+
The maximum sequence length.
|
| 376 |
+
d_emb (Optional[int]):
|
| 377 |
+
The dimension of the positional embeddings. Must be odd and greater or equal to 3 (time, sine and cosine).
|
| 378 |
+
Defaults to 3.
|
| 379 |
+
d_hidden (Optional[int]):
|
| 380 |
+
The number of features in the hidden layer of the MLP. Defaults to 16.
|
| 381 |
+
|
| 382 |
+
Attributes:
|
| 383 |
+
pos_emb (`PositionalEmbedding`): The positional embedding layer.
|
| 384 |
+
mlp (`nn.Sequential`): The MLP that parameterizes the implicit filter.
|
| 385 |
+
|
| 386 |
+
"""
|
| 387 |
+
|
| 388 |
+
def __init__(
|
| 389 |
+
self,
|
| 390 |
+
hidden_size: int,
|
| 391 |
+
max_len: int,
|
| 392 |
+
d_emb: int = 3,
|
| 393 |
+
d_hidden: int = 16,
|
| 394 |
+
**kwargs,
|
| 395 |
+
):
|
| 396 |
+
"""
|
| 397 |
+
Long convolution with implicit filter parameterized by an MLP.
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
"""
|
| 401 |
+
super().__init__()
|
| 402 |
+
self.hidden_size = hidden_size
|
| 403 |
+
self.d_emb = d_emb
|
| 404 |
+
|
| 405 |
+
assert (
|
| 406 |
+
d_emb % 2 != 0 and d_emb >= 3
|
| 407 |
+
), "d_emb must be odd and greater or equal to 3 (time, sine and cosine)"
|
| 408 |
+
self.pos_emb = PositionalEmbedding(d_emb, max_len)
|
| 409 |
+
|
| 410 |
+
# final linear layer
|
| 411 |
+
self.mlp = nn.Sequential(
|
| 412 |
+
nn.Linear(d_emb, d_hidden),
|
| 413 |
+
torch.nn.ReLU(),
|
| 414 |
+
nn.Linear(d_hidden, hidden_size),
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
def filter(self, seq_len: int, *args, **kwargs):
|
| 418 |
+
k = self.mlp(self.pos_emb(seq_len))
|
| 419 |
+
|
| 420 |
+
return k.transpose(1, 2)
|
| 421 |
+
|
| 422 |
+
def forward(self, x: torch.Tensor, *args, **kwargs):
|
| 423 |
+
"""
|
| 424 |
+
Args:
|
| 425 |
+
x: [batch_size, seq_len, hidden_size] tensor
|
| 426 |
+
Returns:
|
| 427 |
+
y: [batch_size, seq_len, hidden_size] tensor
|
| 428 |
+
"""
|
| 429 |
+
x = x.transpose(1, 2)
|
| 430 |
+
k = self.filter(x.shape[-1])
|
| 431 |
+
y = fft_conv(x, k, dropout_mask=None, gelu=False)
|
| 432 |
+
|
| 433 |
+
y = y.transpose(1, 2)
|
| 434 |
+
return y.to(dtype=x.dtype)
|
fla/modules/feature_map.py
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
from typing import Optional
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
from torch import nn
|
| 11 |
+
|
| 12 |
+
from fla.modules.activations import fast_gelu_impl, sigmoid, sqrelu, swish
|
| 13 |
+
from fla.modules.layernorm import layer_norm
|
| 14 |
+
from fla.utils import checkpoint
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@checkpoint
|
| 18 |
+
def flatten_diag_outer_product(x, y):
|
| 19 |
+
z = torch.einsum("...i,...j->...ij", x, y)
|
| 20 |
+
N = z.size(-1)
|
| 21 |
+
indicies = torch.triu_indices(N, N)
|
| 22 |
+
return z[..., indicies[0], indicies[1]]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@checkpoint
|
| 26 |
+
def flatten_diag_outer_product_off1(x, y):
|
| 27 |
+
z = torch.einsum("...i,...j->...ij", x, y)
|
| 28 |
+
N = z.size(-1)
|
| 29 |
+
indicies = torch.triu_indices(N, N, 1)
|
| 30 |
+
indices2 = torch.arange(0, N)
|
| 31 |
+
return z[..., indicies[0], indicies[1]], z[..., indices2, indices2]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def is_power_of_2(n):
|
| 35 |
+
return (n & (n - 1) == 0) and n != 0
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class HedgehogFeatureMap(nn.Module):
|
| 39 |
+
|
| 40 |
+
r"""
|
| 41 |
+
Hedgehog feature map as introduced in
|
| 42 |
+
`The Hedgehog & the Porcupine: Expressive Linear Attentions with Softmax Mimicry <https://arxiv.org/abs/2402.04347>`_
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
def __init__(
|
| 46 |
+
self,
|
| 47 |
+
head_dim: int
|
| 48 |
+
) -> HedgehogFeatureMap:
|
| 49 |
+
super().__init__()
|
| 50 |
+
# Trainable map
|
| 51 |
+
self.layer = nn.Linear(head_dim, head_dim)
|
| 52 |
+
self.init_weights_()
|
| 53 |
+
|
| 54 |
+
def init_weights_(self):
|
| 55 |
+
"""Initialize trainable map as identity"""
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
identity = torch.eye(*self.layer.weight.shape[-2:], dtype=torch.float)
|
| 58 |
+
self.layer.weight.copy_(identity.to(self.layer.weight))
|
| 59 |
+
nn.init.zeros_(self.layer.bias)
|
| 60 |
+
|
| 61 |
+
def forward(self, x: torch.Tensor):
|
| 62 |
+
x = self.layer(x) # shape b, h, l, d
|
| 63 |
+
return torch.cat([2*x, -2*x], dim=-1).softmax(-1)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class T2RFeatureMap(nn.Module):
|
| 67 |
+
|
| 68 |
+
r"""
|
| 69 |
+
Simple linear mapping feature map as in
|
| 70 |
+
`Finetuning Pretrained Transformers into RNNs <https://arxiv.org/abs/2103.13076>`_
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
def __init__(
|
| 74 |
+
self,
|
| 75 |
+
head_dim: int,
|
| 76 |
+
dot_dim: int = None,
|
| 77 |
+
bias: Optional[bool] = False
|
| 78 |
+
) -> T2RFeatureMap:
|
| 79 |
+
super().__init__()
|
| 80 |
+
# Trainable map
|
| 81 |
+
if dot_dim is None:
|
| 82 |
+
dot_dim = head_dim
|
| 83 |
+
|
| 84 |
+
self.head_dim = head_dim
|
| 85 |
+
self.dot_dim = dot_dim
|
| 86 |
+
self.bias = bias
|
| 87 |
+
|
| 88 |
+
self.layer = nn.Linear(head_dim, dot_dim, bias=bias)
|
| 89 |
+
|
| 90 |
+
def __repr__(self) -> str:
|
| 91 |
+
return f"{self.__class__.__name__}(head_dim={self.head_dim}, dot_dim={self.dot_dim}, bias={self.bias})"
|
| 92 |
+
|
| 93 |
+
def forward(self, x: torch.Tensor):
|
| 94 |
+
return self.layer(x).relu()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class DPFPFeatureMap(nn.Module):
|
| 98 |
+
|
| 99 |
+
r"""
|
| 100 |
+
Deterministic Parameter-Free Projection (DPFP) feature map in
|
| 101 |
+
`Linear Transformers Are Secretly Fast Weight Programmers <https://arxiv.org/abs/2102.11174>`_
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
def __init__(
|
| 105 |
+
self,
|
| 106 |
+
head_dim: int,
|
| 107 |
+
nu: int = 4
|
| 108 |
+
) -> DPFPFeatureMap:
|
| 109 |
+
super().__init__()
|
| 110 |
+
self.nu = nu
|
| 111 |
+
|
| 112 |
+
def forward(self, x: torch.Tensor):
|
| 113 |
+
x = torch.cat([x.relu(), -x.relu()], dim=-1)
|
| 114 |
+
x_rolled = torch.cat([x.roll(shifts=j, dims=-1) for j in range(1, self.nu+1)], dim=-1)
|
| 115 |
+
x_repeat = torch.cat([x] * self.nu, dim=-1)
|
| 116 |
+
return x_repeat * x_rolled
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
class HadamardFeatureMap(nn.Module):
|
| 120 |
+
def __init__(
|
| 121 |
+
self,
|
| 122 |
+
head_dim: int
|
| 123 |
+
) -> HadamardFeatureMap:
|
| 124 |
+
super().__init__()
|
| 125 |
+
# Trainable map
|
| 126 |
+
self.layer1 = nn.Linear(head_dim, head_dim)
|
| 127 |
+
self.layer2 = nn.Linear(head_dim, head_dim)
|
| 128 |
+
|
| 129 |
+
def forward(self, x: torch.Tensor):
|
| 130 |
+
return self.layer1(x) * self.layer2(x)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
class LearnableOuterProductFeatureMap(nn.Module):
|
| 134 |
+
def __init__(
|
| 135 |
+
self,
|
| 136 |
+
head_dim: int,
|
| 137 |
+
feature_dim: int
|
| 138 |
+
) -> LearnableOuterProductFeatureMap:
|
| 139 |
+
super().__init__()
|
| 140 |
+
# Trainable map
|
| 141 |
+
self.layer1 = nn.Linear(head_dim, feature_dim, bias=False)
|
| 142 |
+
self.layer2 = nn.Linear(head_dim, feature_dim, bias=False)
|
| 143 |
+
self.normalizer = feature_dim ** -0.5
|
| 144 |
+
|
| 145 |
+
def forward(self, x: torch.Tensor):
|
| 146 |
+
return flatten_diag_outer_product(self.layer1(x), self.layer2(x))
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class LearnablePolySketchNonNegativeFeatureMap(nn.Module):
|
| 150 |
+
|
| 151 |
+
def __init__(
|
| 152 |
+
self,
|
| 153 |
+
head_dim: int,
|
| 154 |
+
sketch_size: Optional[int] = None,
|
| 155 |
+
degree: Optional[int] = 2
|
| 156 |
+
) -> LearnablePolySketchNonNegativeFeatureMap:
|
| 157 |
+
super().__init__()
|
| 158 |
+
|
| 159 |
+
assert is_power_of_2(degree) and degree >= 2, f"The degree {degree} must be a power of 2"
|
| 160 |
+
|
| 161 |
+
self.head_dim = head_dim
|
| 162 |
+
self.sketch_size = sketch_size if sketch_size is not None else head_dim
|
| 163 |
+
self.degree = degree
|
| 164 |
+
|
| 165 |
+
self.gamma = nn.Parameter(torch.ones(head_dim))
|
| 166 |
+
self.beta = nn.Parameter(torch.zeros(head_dim))
|
| 167 |
+
# NOTE: the sketch layers defined here are quite different from the original paper
|
| 168 |
+
# currently we simply use linear layers without any non-linear activations
|
| 169 |
+
self.sketches1 = nn.ModuleList([
|
| 170 |
+
nn.Linear(head_dim, sketch_size, bias=False),
|
| 171 |
+
*[nn.Linear(sketch_size, sketch_size, bias=False) for _ in range(int(math.log2(self.degree)) - 2)]
|
| 172 |
+
])
|
| 173 |
+
self.sketches2 = nn.ModuleList([
|
| 174 |
+
nn.Linear(head_dim, sketch_size, bias=False),
|
| 175 |
+
*[nn.Linear(sketch_size, sketch_size, bias=False) for _ in range(int(math.log2(self.degree)) - 2)]
|
| 176 |
+
])
|
| 177 |
+
|
| 178 |
+
def forward(self, x: torch.Tensor):
|
| 179 |
+
# Section 2.1
|
| 180 |
+
x = layer_norm(x, self.gamma, self.beta)
|
| 181 |
+
# first map the input to sketch size with learnable parameters
|
| 182 |
+
x = self.sketches1[0](x) * self.sketches2[0](x) * self.head_dim ** -0.5
|
| 183 |
+
for i in range(1, int(math.log2(self.degree)) - 1):
|
| 184 |
+
x = self.sketches1[i](x) * self.sketches2[i](x) * self.head_dim ** -0.5
|
| 185 |
+
# do sketch mapping for log2(p) - 1 times in total
|
| 186 |
+
# do p=2 mapping to ensure non-negativity
|
| 187 |
+
return flatten_diag_outer_product(x, x)
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
class TaylorFeatureMap(nn.Module):
|
| 191 |
+
def __init__(
|
| 192 |
+
self,
|
| 193 |
+
head_dim: int
|
| 194 |
+
) -> TaylorFeatureMap:
|
| 195 |
+
super().__init__()
|
| 196 |
+
self.head_dim = head_dim
|
| 197 |
+
self.r2 = math.sqrt(2)
|
| 198 |
+
self.rd = math.sqrt(self.head_dim)
|
| 199 |
+
self.rrd = math.sqrt(self.rd)
|
| 200 |
+
|
| 201 |
+
def forward(self, x: torch.Tensor):
|
| 202 |
+
x2_1, x2_2 = flatten_diag_outer_product_off1(x, x)
|
| 203 |
+
return torch.cat([torch.ones_like(x[..., 0:1]), x / self.rrd, x2_2 / (self.rd * self.r2), x2_1 / self.rd], dim=-1)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
class RebasedFeatureMap(nn.Module):
|
| 207 |
+
|
| 208 |
+
def __init__(
|
| 209 |
+
self,
|
| 210 |
+
head_dim: int,
|
| 211 |
+
use_gamma: Optional[bool] = True,
|
| 212 |
+
use_beta: Optional[bool] = True,
|
| 213 |
+
normalize: Optional[bool] = True
|
| 214 |
+
) -> RebasedFeatureMap:
|
| 215 |
+
super().__init__()
|
| 216 |
+
|
| 217 |
+
self.head_dim = head_dim
|
| 218 |
+
self.use_gamma = use_gamma
|
| 219 |
+
self.use_beta = use_beta
|
| 220 |
+
self.normalize = normalize
|
| 221 |
+
|
| 222 |
+
self.gamma = None
|
| 223 |
+
self.beta = None
|
| 224 |
+
if use_gamma:
|
| 225 |
+
self.gamma = nn.Parameter(torch.ones(head_dim))
|
| 226 |
+
if use_beta:
|
| 227 |
+
self.beta = nn.Parameter(torch.zeros(head_dim))
|
| 228 |
+
|
| 229 |
+
def forward(self, x: torch.Tensor, flatten: Optional[bool] = True):
|
| 230 |
+
if self.use_beta and self.use_gamma and self.normalize:
|
| 231 |
+
x = layer_norm(x, self.gamma, self.beta)
|
| 232 |
+
elif self.normalize:
|
| 233 |
+
x = F.layer_norm(x, (self.head_dim,), self.gamma, self.beta)
|
| 234 |
+
elif self.use_gamma and self.use_beta:
|
| 235 |
+
x = torch.addcmul(self.beta, x, self.gamma)
|
| 236 |
+
elif self.use_gamma:
|
| 237 |
+
x = x.mul(self.gamma)
|
| 238 |
+
else:
|
| 239 |
+
raise RuntimeError(f"Not supported combination of `use_gamma`, `use_beta` and `normalize`, "
|
| 240 |
+
f"which is currentlt set as (`{self.use_gamma}`, `{self.use_beta}`, `{self.normalize}`)")
|
| 241 |
+
if not flatten:
|
| 242 |
+
return x
|
| 243 |
+
x2_1, x2_2 = flatten_diag_outer_product_off1(x, x)
|
| 244 |
+
# rebased use learnable parameters to approximate any quadratic function
|
| 245 |
+
return torch.cat([x2_2 * self.head_dim ** -0.5, x2_1 * (2 / self.head_dim) ** 0.5], dim=-1)
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
class ReLUFeatureMap(nn.Module):
|
| 249 |
+
|
| 250 |
+
def __init__(
|
| 251 |
+
self,
|
| 252 |
+
) -> ReLUFeatureMap:
|
| 253 |
+
super().__init__()
|
| 254 |
+
|
| 255 |
+
def forward(self, x: torch.Tensor):
|
| 256 |
+
return F.relu(x)
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
class SquaredReLUFeatureMap(nn.Module):
|
| 260 |
+
|
| 261 |
+
def __init__(
|
| 262 |
+
self,
|
| 263 |
+
) -> SquaredReLUFeatureMap:
|
| 264 |
+
super().__init__()
|
| 265 |
+
|
| 266 |
+
def forward(self, x: torch.Tensor):
|
| 267 |
+
return sqrelu(x)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
class GELUFeatureMap(nn.Module):
|
| 271 |
+
|
| 272 |
+
def __init__(
|
| 273 |
+
self,
|
| 274 |
+
) -> GELUFeatureMap:
|
| 275 |
+
super().__init__()
|
| 276 |
+
|
| 277 |
+
def forward(self, x: torch.Tensor):
|
| 278 |
+
return fast_gelu_impl(x)
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
class SwishFeatureMap(nn.Module):
|
| 282 |
+
|
| 283 |
+
def __init__(
|
| 284 |
+
self,
|
| 285 |
+
) -> SwishFeatureMap:
|
| 286 |
+
super().__init__()
|
| 287 |
+
|
| 288 |
+
def forward(self, x: torch.Tensor):
|
| 289 |
+
return swish(x)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
class SigmoidFeatureMap(nn.Module):
|
| 293 |
+
|
| 294 |
+
def __init__(
|
| 295 |
+
self,
|
| 296 |
+
) -> SigmoidFeatureMap:
|
| 297 |
+
super().__init__()
|
| 298 |
+
|
| 299 |
+
def forward(self, x: torch.Tensor):
|
| 300 |
+
return sigmoid(x)
|
fla/modules/fused_kl_div.py
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Tuple
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
import triton
|
| 9 |
+
import triton.language as tl
|
| 10 |
+
|
| 11 |
+
from fla.ops.utils.op import exp, log
|
| 12 |
+
from fla.utils import input_guard
|
| 13 |
+
|
| 14 |
+
# The hard limit of TRITON_MAX_TENSOR_NUMEL is 1048576
|
| 15 |
+
# https://github.com/triton-lang/triton/blob/ba42a5c68fd0505f8c42f4202d53be0f8d9a5fe0/python/triton/language/core.py#L19
|
| 16 |
+
# However, setting limit as 65536 as in LayerNorm tutorial is faster because of less register spilling
|
| 17 |
+
# The optimal maximum block size depends on your hardware, your kernel, and your dtype
|
| 18 |
+
MAX_FUSED_SIZE = 65536 // 2
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@triton.jit
|
| 22 |
+
def kl_div_kernel(
|
| 23 |
+
logits,
|
| 24 |
+
target_logits,
|
| 25 |
+
loss,
|
| 26 |
+
s_logits,
|
| 27 |
+
s_loss,
|
| 28 |
+
reduction: tl.constexpr,
|
| 29 |
+
N: tl.constexpr,
|
| 30 |
+
V: tl.constexpr,
|
| 31 |
+
BV: tl.constexpr
|
| 32 |
+
):
|
| 33 |
+
# https://github.com/triton-lang/triton/issues/1058
|
| 34 |
+
# If N*V is too large, i_n * stride will overflow out of int32, so we convert to int64
|
| 35 |
+
i_n = tl.program_id(0).to(tl.int64)
|
| 36 |
+
|
| 37 |
+
logits += i_n * s_logits
|
| 38 |
+
target_logits += i_n * s_logits
|
| 39 |
+
|
| 40 |
+
# m is the max value. use the notation from the paper
|
| 41 |
+
sm = float('-inf')
|
| 42 |
+
tm = float('-inf')
|
| 43 |
+
# d is the sum. use the notation from the paper
|
| 44 |
+
sd, td = 0.0, 0.0
|
| 45 |
+
|
| 46 |
+
NV = tl.cdiv(V, BV)
|
| 47 |
+
for iv in range(0, NV):
|
| 48 |
+
o_x = iv * BV + tl.arange(0, BV)
|
| 49 |
+
# for student
|
| 50 |
+
b_sl = tl.load(logits + o_x, mask=o_x < V, other=float('-inf'))
|
| 51 |
+
b_sm = tl.max(b_sl)
|
| 52 |
+
m_new = tl.maximum(sm, b_sm)
|
| 53 |
+
sd = sd * exp(sm - m_new) + tl.sum(exp(b_sl - m_new))
|
| 54 |
+
sm = m_new
|
| 55 |
+
# for teacher
|
| 56 |
+
b_tl = tl.load(target_logits + o_x, mask=o_x < V, other=float('-inf'))
|
| 57 |
+
b_tm = tl.max(b_tl)
|
| 58 |
+
m_new = tl.maximum(tm, b_tm)
|
| 59 |
+
td = td * exp(tm - m_new) + tl.sum(exp(b_tl - m_new))
|
| 60 |
+
tm = m_new
|
| 61 |
+
|
| 62 |
+
b_loss = 0.
|
| 63 |
+
# KL(y_true || y) = exp(y_true) * (log(y_true) - log(y))
|
| 64 |
+
for iv in range(0, NV):
|
| 65 |
+
o_x = iv * BV + tl.arange(0, BV)
|
| 66 |
+
b_sl = tl.load(logits + o_x, mask=o_x < V, other=float('-inf'))
|
| 67 |
+
b_tl = tl.load(target_logits + o_x, mask=o_x < V, other=float('-inf'))
|
| 68 |
+
b_sp_log = b_sl - sm - log(sd)
|
| 69 |
+
b_tp_log = b_tl - tm - log(td)
|
| 70 |
+
b_sp = exp(b_sp_log)
|
| 71 |
+
b_tp = exp(b_tp_log)
|
| 72 |
+
b_kl = tl.where(o_x < V, b_tp * (b_tp_log - b_sp_log), 0)
|
| 73 |
+
b_dl = -b_tp + b_sp
|
| 74 |
+
b_loss += tl.sum(b_kl)
|
| 75 |
+
if reduction == 'batchmean':
|
| 76 |
+
b_dl = b_dl / N
|
| 77 |
+
tl.store(logits + o_x, b_dl, mask=o_x < V)
|
| 78 |
+
|
| 79 |
+
# Normalize the loss by the number of elements if reduction is 'batchmean'
|
| 80 |
+
if reduction == 'batchmean':
|
| 81 |
+
b_loss = b_loss / N
|
| 82 |
+
|
| 83 |
+
tl.store(loss + i_n * s_loss, b_loss)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
@triton.jit
|
| 87 |
+
def elementwise_mul_kernel(
|
| 88 |
+
x,
|
| 89 |
+
g,
|
| 90 |
+
N: tl.constexpr,
|
| 91 |
+
B: tl.constexpr
|
| 92 |
+
):
|
| 93 |
+
"""
|
| 94 |
+
This function multiplies each element of the tensor pointed by x with the value pointed by g.
|
| 95 |
+
The multiplication is performed in-place on the tensor pointed by x.
|
| 96 |
+
|
| 97 |
+
Parameters:
|
| 98 |
+
x:
|
| 99 |
+
Pointer to the input tensor.
|
| 100 |
+
g:
|
| 101 |
+
Pointer to the gradient output value.
|
| 102 |
+
N (int):
|
| 103 |
+
The number of columns in the input tensor.
|
| 104 |
+
B (int):
|
| 105 |
+
The block size for Triton operations.
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
# Get the program ID and convert it to int64 to avoid overflow
|
| 109 |
+
i_x = tl.program_id(0).to(tl.int64)
|
| 110 |
+
o_x = i_x * B + tl.arange(0, B)
|
| 111 |
+
|
| 112 |
+
# Load the gradient output value
|
| 113 |
+
b_g = tl.load(g)
|
| 114 |
+
b_x = tl.load(x + o_x, mask=o_x < N)
|
| 115 |
+
tl.store(x + o_x, b_x * b_g, mask=o_x < N)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def fused_kl_div_forward(
|
| 119 |
+
x: torch.Tensor,
|
| 120 |
+
target_x: torch.Tensor,
|
| 121 |
+
weight: torch.Tensor,
|
| 122 |
+
target_weight: torch.Tensor,
|
| 123 |
+
reduction: str = 'batchmean'
|
| 124 |
+
):
|
| 125 |
+
device = x.device
|
| 126 |
+
|
| 127 |
+
# ideally, we would like to achieve the same memory consumption as [N, H],
|
| 128 |
+
# so the expected chunk size should be:
|
| 129 |
+
# NC = ceil(V / H)
|
| 130 |
+
# C = ceil(N / NC)
|
| 131 |
+
# for ex: N = 4096*4, V = 32000, H = 4096 ==> NC = 8, C = ceil(N / NC) = 2048
|
| 132 |
+
N, H, V = *x.shape, weight.shape[0]
|
| 133 |
+
BV = min(MAX_FUSED_SIZE, triton.next_power_of_2(V))
|
| 134 |
+
# TODO: in real cases, we may need to limit the number of chunks NC to
|
| 135 |
+
# ensure the precisions of accumulated gradients
|
| 136 |
+
NC = min(8, triton.cdiv(V, H))
|
| 137 |
+
C = triton.next_power_of_2(triton.cdiv(N, NC))
|
| 138 |
+
NC = triton.cdiv(N, C)
|
| 139 |
+
|
| 140 |
+
dx = torch.zeros_like(x, device=device)
|
| 141 |
+
dw = torch.zeros_like(weight, device=device) if weight is not None else None
|
| 142 |
+
# we use fp32 for loss accumulator
|
| 143 |
+
loss = torch.zeros(N, dtype=torch.float32, device=device)
|
| 144 |
+
|
| 145 |
+
for ic in range(NC):
|
| 146 |
+
start, end = ic * C, min((ic + 1) * C, N)
|
| 147 |
+
# [C, N]
|
| 148 |
+
c_sx = x[start:end]
|
| 149 |
+
c_tx = target_x[start:end]
|
| 150 |
+
# when doing matmul, use the original precision
|
| 151 |
+
# [C, V]
|
| 152 |
+
c_sl = F.linear(c_sx, weight)
|
| 153 |
+
c_tl = F.linear(c_tx, target_weight)
|
| 154 |
+
|
| 155 |
+
# unreduced loss
|
| 156 |
+
c_loss = loss[start:end]
|
| 157 |
+
|
| 158 |
+
# Here we calculate the gradient of c_sx in place so we can save memory.
|
| 159 |
+
kl_div_kernel[(c_sx.shape[0],)](
|
| 160 |
+
logits=c_sl,
|
| 161 |
+
target_logits=c_tl,
|
| 162 |
+
loss=c_loss,
|
| 163 |
+
s_logits=c_sl.stride(-2),
|
| 164 |
+
s_loss=c_loss.stride(-1),
|
| 165 |
+
reduction=reduction,
|
| 166 |
+
N=N,
|
| 167 |
+
V=V,
|
| 168 |
+
BV=BV,
|
| 169 |
+
num_warps=32
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# gradient of logits is computed in-place by the above triton kernel and is of shape: C x V
|
| 173 |
+
# thus dx[start: end] should be of shape: C x H
|
| 174 |
+
# additionally, since we are chunking the inputs, observe that the loss and gradients are calculated only
|
| 175 |
+
# on `n_non_ignore` tokens. However, the gradient of the input should be calculated for all tokens.
|
| 176 |
+
# Thus, we need an additional scaling factor of (n_non_ignore/total) to scale the gradients.
|
| 177 |
+
# [C, H]
|
| 178 |
+
|
| 179 |
+
dx[start:end] = torch.mm(c_sl, weight)
|
| 180 |
+
|
| 181 |
+
if weight is not None:
|
| 182 |
+
torch.addmm(input=dw, mat1=c_sl.t(), mat2=c_sx, out=dw)
|
| 183 |
+
|
| 184 |
+
loss = loss.sum()
|
| 185 |
+
return loss, dx, dw
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def fused_kl_div_backward(
|
| 189 |
+
do: torch.Tensor,
|
| 190 |
+
dx: torch.Tensor,
|
| 191 |
+
dw: torch.Tensor
|
| 192 |
+
):
|
| 193 |
+
# If cross entropy is the last layer, do is 1.0. Skip the mul to save time
|
| 194 |
+
if torch.ne(do, torch.tensor(1.0, device=do.device)):
|
| 195 |
+
# We use a Triton kernel instead of a PyTorch operation because modifying inputs in-place
|
| 196 |
+
# for gradient storage and backward multiple times causes anomalies with PyTorch but not with Triton.
|
| 197 |
+
N, H = dx.shape
|
| 198 |
+
B = min(MAX_FUSED_SIZE, triton.next_power_of_2(H))
|
| 199 |
+
|
| 200 |
+
elementwise_mul_kernel[(triton.cdiv(N * H, B),)](
|
| 201 |
+
x=dx,
|
| 202 |
+
g=do,
|
| 203 |
+
N=N*H,
|
| 204 |
+
B=B,
|
| 205 |
+
num_warps=32,
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
# handle dw
|
| 209 |
+
if dw is not None:
|
| 210 |
+
V, H = dw.shape
|
| 211 |
+
elementwise_mul_kernel[(triton.cdiv(V * H, B),)](
|
| 212 |
+
x=dw,
|
| 213 |
+
g=do,
|
| 214 |
+
N=V*H,
|
| 215 |
+
B=B,
|
| 216 |
+
num_warps=32,
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
return dx, dw
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
class FusedKLDivLossFunction(torch.autograd.Function):
|
| 223 |
+
|
| 224 |
+
@staticmethod
|
| 225 |
+
@input_guard
|
| 226 |
+
def forward(
|
| 227 |
+
ctx,
|
| 228 |
+
x: torch.Tensor,
|
| 229 |
+
target_x: torch.Tensor,
|
| 230 |
+
weight: torch.Tensor,
|
| 231 |
+
target_weight: torch.Tensor,
|
| 232 |
+
reduction: str
|
| 233 |
+
):
|
| 234 |
+
loss, dx, dw = fused_kl_div_forward(
|
| 235 |
+
x=x,
|
| 236 |
+
target_x=target_x,
|
| 237 |
+
weight=weight,
|
| 238 |
+
target_weight=target_weight,
|
| 239 |
+
reduction=reduction
|
| 240 |
+
)
|
| 241 |
+
ctx.save_for_backward(dx, dw)
|
| 242 |
+
return loss
|
| 243 |
+
|
| 244 |
+
@staticmethod
|
| 245 |
+
@input_guard
|
| 246 |
+
def backward(ctx, do):
|
| 247 |
+
dx, dw = ctx.saved_tensors
|
| 248 |
+
dx, dw = fused_kl_div_backward(do, dx, dw)
|
| 249 |
+
return dx, None, dw, None, None
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def fused_kl_div_loss(
|
| 253 |
+
x: torch.Tensor,
|
| 254 |
+
target_x: torch.Tensor,
|
| 255 |
+
weight: torch.Tensor,
|
| 256 |
+
target_weight: torch.Tensor,
|
| 257 |
+
reduction: str = 'batchmean'
|
| 258 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 259 |
+
"""
|
| 260 |
+
Args:
|
| 261 |
+
x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
| 262 |
+
target_x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
| 263 |
+
weight (torch.Tensor): [vocab_size, hidden_size]
|
| 264 |
+
where `vocab_size` is the number of classes.
|
| 265 |
+
target_weight (torch.Tensor): [vocab_size, hidden_size]
|
| 266 |
+
where `vocab_size` is the number of classes.
|
| 267 |
+
reduction:
|
| 268 |
+
Specifies the reduction to apply to the output: 'batchmean'. Default: 'batchmean'.
|
| 269 |
+
Returns:
|
| 270 |
+
loss
|
| 271 |
+
"""
|
| 272 |
+
return FusedKLDivLossFunction.apply(
|
| 273 |
+
x,
|
| 274 |
+
target_x,
|
| 275 |
+
weight,
|
| 276 |
+
target_weight,
|
| 277 |
+
reduction
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
class FusedKLDivLoss(nn.Module):
|
| 282 |
+
|
| 283 |
+
def __init__(
|
| 284 |
+
self,
|
| 285 |
+
reduction: str = 'batchmean'
|
| 286 |
+
):
|
| 287 |
+
"""
|
| 288 |
+
Args:
|
| 289 |
+
reduction:
|
| 290 |
+
Specifies the reduction to apply to the output: 'batchmean'. Default: 'batchmean'.
|
| 291 |
+
"""
|
| 292 |
+
super().__init__()
|
| 293 |
+
|
| 294 |
+
assert reduction in ['batchmean'], f"reduction: {reduction} is not supported"
|
| 295 |
+
|
| 296 |
+
self.reduction = reduction
|
| 297 |
+
|
| 298 |
+
def forward(
|
| 299 |
+
self,
|
| 300 |
+
x: torch.Tensor,
|
| 301 |
+
target_x: torch.Tensor,
|
| 302 |
+
weight: torch.Tensor,
|
| 303 |
+
target_weight: torch.Tensor
|
| 304 |
+
):
|
| 305 |
+
"""
|
| 306 |
+
Args:
|
| 307 |
+
x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
| 308 |
+
target_x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
| 309 |
+
weight (torch.Tensor): [vocab_size, hidden_size]
|
| 310 |
+
where `vocab_size` is the number of classes.
|
| 311 |
+
target_weight (torch.Tensor): [vocab_size, hidden_size]
|
| 312 |
+
where `vocab_size` is the number of classes.
|
| 313 |
+
Returns:
|
| 314 |
+
loss
|
| 315 |
+
"""
|
| 316 |
+
loss = fused_kl_div_loss(
|
| 317 |
+
x=x,
|
| 318 |
+
target_x=target_x,
|
| 319 |
+
weight=weight,
|
| 320 |
+
target_weight=target_weight,
|
| 321 |
+
reduction=self.reduction
|
| 322 |
+
)
|
| 323 |
+
return loss
|
fla/modules/l2norm.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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3 |
+
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import triton
|
| 8 |
+
import triton.language as tl
|
| 9 |
+
|
| 10 |
+
from fla.utils import input_guard
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@triton.autotune(
|
| 14 |
+
configs=[
|
| 15 |
+
triton.Config({}, num_warps=num_warps)
|
| 16 |
+
for num_warps in [1, 2, 4, 8, 16, 32]
|
| 17 |
+
],
|
| 18 |
+
key=['N']
|
| 19 |
+
)
|
| 20 |
+
@triton.jit
|
| 21 |
+
def l2norm_fwd_kernel(
|
| 22 |
+
X,
|
| 23 |
+
Y,
|
| 24 |
+
N,
|
| 25 |
+
eps,
|
| 26 |
+
BLOCK_N: tl.constexpr,
|
| 27 |
+
):
|
| 28 |
+
i_m = tl.program_id(0)
|
| 29 |
+
X += i_m * N
|
| 30 |
+
Y += i_m * N
|
| 31 |
+
# Compute mean and variance
|
| 32 |
+
cols = tl.arange(0, BLOCK_N)
|
| 33 |
+
mask = cols < N
|
| 34 |
+
x = tl.load(X + cols, mask=mask, other=0.0).to(tl.float32)
|
| 35 |
+
xbar = tl.where(mask, x, 0.0)
|
| 36 |
+
var = tl.sum(xbar * xbar, axis=0)
|
| 37 |
+
rstd = 1 / tl.sqrt(var + eps)
|
| 38 |
+
# tl.store(Rstd + i_m, rstd)
|
| 39 |
+
# Normalize and apply linear transformation
|
| 40 |
+
y = x * rstd
|
| 41 |
+
# Write output
|
| 42 |
+
tl.store(Y + cols, y, mask=mask)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@triton.autotune(
|
| 46 |
+
configs=[
|
| 47 |
+
triton.Config({}, num_warps=num_warps)
|
| 48 |
+
for num_warps in [1, 2, 4, 8, 16, 32]
|
| 49 |
+
],
|
| 50 |
+
key=['N']
|
| 51 |
+
)
|
| 52 |
+
@triton.jit
|
| 53 |
+
def l2norm_bwd_kernel(
|
| 54 |
+
X,
|
| 55 |
+
DY,
|
| 56 |
+
DX,
|
| 57 |
+
N,
|
| 58 |
+
eps,
|
| 59 |
+
BLOCK_N: tl.constexpr,
|
| 60 |
+
):
|
| 61 |
+
i_m = tl.program_id(0)
|
| 62 |
+
X += i_m * N
|
| 63 |
+
DX += i_m * N
|
| 64 |
+
DY += i_m * N
|
| 65 |
+
|
| 66 |
+
# Y += i_m * stride_y_row
|
| 67 |
+
cols = tl.arange(0, BLOCK_N)
|
| 68 |
+
mask = cols < N
|
| 69 |
+
x = tl.load(X + cols, mask=mask, other=0.0).to(tl.float32)
|
| 70 |
+
x = tl.where(mask, x, 0.0)
|
| 71 |
+
var = tl.sum(x * x)
|
| 72 |
+
rstd = 1 / tl.sqrt(var + eps)
|
| 73 |
+
# tl.store(Rstd + i_m, rstd)
|
| 74 |
+
# Normalize and apply linear transformation
|
| 75 |
+
# y = x * rstd
|
| 76 |
+
dy = tl.load(DY + cols, mask=mask, other=0.0).to(tl.float32)
|
| 77 |
+
dy = tl.where(mask, dy, 0.0)
|
| 78 |
+
dx = dy * rstd - tl.sum(dy * x) * (1 / (var+eps)) * rstd * x
|
| 79 |
+
tl.store(DX + cols, dx, mask=mask)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def l2norm_fwd(
|
| 83 |
+
x: torch.Tensor,
|
| 84 |
+
eps: float = 1e-6,
|
| 85 |
+
output_dtype: Optional[torch.dtype] = None
|
| 86 |
+
):
|
| 87 |
+
x_shape_og = x.shape
|
| 88 |
+
x = x.reshape(-1, x.shape[-1])
|
| 89 |
+
# allocate output
|
| 90 |
+
if output_dtype is None:
|
| 91 |
+
y = torch.empty_like(x)
|
| 92 |
+
else:
|
| 93 |
+
y = torch.empty_like(x, dtype=output_dtype)
|
| 94 |
+
assert y.stride(-1) == 1
|
| 95 |
+
N = x.shape[-1]
|
| 96 |
+
M = x.shape[0]
|
| 97 |
+
# rstd = torch.empty((M,), dtype=torch.float32, device=x.device)
|
| 98 |
+
# Less than 64KB per feature: enqueue fused kernel
|
| 99 |
+
MAX_FUSED_SIZE = 65536 // x.element_size()
|
| 100 |
+
BLOCK_N = min(MAX_FUSED_SIZE, triton.next_power_of_2(N))
|
| 101 |
+
if N > BLOCK_N:
|
| 102 |
+
raise RuntimeError("This layer norm doesn't support feature dim >= 64KB.")
|
| 103 |
+
# heuristics for number of warps
|
| 104 |
+
l2norm_fwd_kernel[(M,)](
|
| 105 |
+
x,
|
| 106 |
+
y,
|
| 107 |
+
N,
|
| 108 |
+
eps,
|
| 109 |
+
BLOCK_N,
|
| 110 |
+
)
|
| 111 |
+
return y.reshape(x_shape_og)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def l2norm_bwd(
|
| 115 |
+
x: torch.Tensor,
|
| 116 |
+
dy: torch.Tensor,
|
| 117 |
+
eps: float = 1e-5
|
| 118 |
+
):
|
| 119 |
+
x_shape_og = x.shape
|
| 120 |
+
x = x.reshape(-1, dy.shape[-1])
|
| 121 |
+
dy = dy.reshape(-1, dy.shape[-1])
|
| 122 |
+
if dy.stride(-1) != 1:
|
| 123 |
+
dy = dy.contiguous()
|
| 124 |
+
assert dy.shape == x.shape
|
| 125 |
+
# allocate output
|
| 126 |
+
dx = torch.empty_like(x)
|
| 127 |
+
M = x.shape[0]
|
| 128 |
+
N = x.shape[-1]
|
| 129 |
+
# rstd = torch.empty((M,), dtype=torch.float32, device=x.device)
|
| 130 |
+
# Less than 64KB per feature: enqueue fused kernel
|
| 131 |
+
MAX_FUSED_SIZE = 65536 // x.element_size()
|
| 132 |
+
BLOCK_N = min(MAX_FUSED_SIZE, triton.next_power_of_2(N))
|
| 133 |
+
if N > BLOCK_N:
|
| 134 |
+
raise RuntimeError("This layer norm doesn't support feature dim >= 64KB.")
|
| 135 |
+
# heuristics for number of warps
|
| 136 |
+
l2norm_bwd_kernel[(M,)](
|
| 137 |
+
x,
|
| 138 |
+
dy,
|
| 139 |
+
dx,
|
| 140 |
+
N,
|
| 141 |
+
eps,
|
| 142 |
+
BLOCK_N,
|
| 143 |
+
)
|
| 144 |
+
return dx.reshape(x_shape_og)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
class L2NormFunction(torch.autograd.Function):
|
| 148 |
+
|
| 149 |
+
@staticmethod
|
| 150 |
+
@input_guard
|
| 151 |
+
def forward(
|
| 152 |
+
ctx,
|
| 153 |
+
x,
|
| 154 |
+
eps=1e-6,
|
| 155 |
+
output_dtype=None
|
| 156 |
+
):
|
| 157 |
+
y = l2norm_fwd(x, eps, output_dtype)
|
| 158 |
+
ctx.eps = eps
|
| 159 |
+
ctx.x_dtype = x.dtype
|
| 160 |
+
ctx.save_for_backward(x)
|
| 161 |
+
return y
|
| 162 |
+
|
| 163 |
+
@staticmethod
|
| 164 |
+
@input_guard
|
| 165 |
+
def backward(ctx, dy):
|
| 166 |
+
x, = ctx.saved_tensors
|
| 167 |
+
dx = l2norm_bwd(x, dy, ctx.eps)
|
| 168 |
+
return dx, None, None
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def l2_norm(
|
| 172 |
+
x: torch.Tensor,
|
| 173 |
+
eps: float = 1e-6,
|
| 174 |
+
output_dtype: Optional[torch.dtype] = None
|
| 175 |
+
) -> torch.Tensor:
|
| 176 |
+
return L2NormFunction.apply(x, eps, output_dtype)
|
fla/ops/based/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (286 Bytes). View file
|
|
|
fla/ops/based/__pycache__/parallel.cpython-312.pyc
ADDED
|
Binary file (22.6 kB). View file
|
|
|
fla/ops/common/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
fla/ops/common/__pycache__/chunk_delta_h.cpython-312.pyc
ADDED
|
Binary file (23.9 kB). View file
|
|
|
fla/ops/common/__pycache__/chunk_scaled_dot_kkt.cpython-312.pyc
ADDED
|
Binary file (6.74 kB). View file
|
|
|
fla/ops/common/__pycache__/utils.cpython-312.pyc
ADDED
|
Binary file (4.42 kB). View file
|
|
|
fla/ops/common/chunk_delta_h.py
ADD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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from typing import Optional, Tuple
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import triton
|
| 8 |
+
import triton.language as tl
|
| 9 |
+
|
| 10 |
+
from fla.ops.common.utils import prepare_chunk_offsets
|
| 11 |
+
from fla.ops.utils.op import exp
|
| 12 |
+
from fla.utils import check_shared_mem, is_nvidia_hopper, use_cuda_graph
|
| 13 |
+
|
| 14 |
+
NUM_WARPS = [2, 4] if is_nvidia_hopper else [2, 4, 8, 16]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@triton.heuristics({
|
| 18 |
+
'USE_G': lambda args: args['g'] is not None,
|
| 19 |
+
'USE_INITIAL_STATE': lambda args: args['h0'] is not None,
|
| 20 |
+
'STORE_FINAL_STATE': lambda args: args['ht'] is not None,
|
| 21 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None,
|
| 22 |
+
})
|
| 23 |
+
@triton.autotune(
|
| 24 |
+
configs=[
|
| 25 |
+
triton.Config({}, num_warps=num_warps, num_stages=num_stages)
|
| 26 |
+
for num_warps in NUM_WARPS
|
| 27 |
+
for num_stages in [2, 3, 4]
|
| 28 |
+
],
|
| 29 |
+
key=['H', 'K', 'V', 'BT', 'BK', 'BV', 'USE_G'],
|
| 30 |
+
use_cuda_graph=use_cuda_graph,
|
| 31 |
+
)
|
| 32 |
+
@triton.jit(do_not_specialize=['T'])
|
| 33 |
+
def chunk_gated_delta_rule_fwd_kernel_h(
|
| 34 |
+
k,
|
| 35 |
+
v,
|
| 36 |
+
d,
|
| 37 |
+
v_new,
|
| 38 |
+
g,
|
| 39 |
+
h,
|
| 40 |
+
h0,
|
| 41 |
+
ht,
|
| 42 |
+
offsets,
|
| 43 |
+
chunk_offsets,
|
| 44 |
+
T,
|
| 45 |
+
H: tl.constexpr,
|
| 46 |
+
K: tl.constexpr,
|
| 47 |
+
V: tl.constexpr,
|
| 48 |
+
BT: tl.constexpr,
|
| 49 |
+
BC: tl.constexpr,
|
| 50 |
+
BK: tl.constexpr,
|
| 51 |
+
BV: tl.constexpr,
|
| 52 |
+
NT: tl.constexpr,
|
| 53 |
+
USE_G: tl.constexpr,
|
| 54 |
+
USE_INITIAL_STATE: tl.constexpr,
|
| 55 |
+
STORE_FINAL_STATE: tl.constexpr,
|
| 56 |
+
USE_OFFSETS: tl.constexpr,
|
| 57 |
+
HEAD_FIRST: tl.constexpr,
|
| 58 |
+
):
|
| 59 |
+
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 60 |
+
i_n, i_h = i_nh // H, i_nh % H
|
| 61 |
+
if USE_OFFSETS:
|
| 62 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 63 |
+
T = eos - bos
|
| 64 |
+
NT = tl.cdiv(T, BT)
|
| 65 |
+
boh = tl.load(chunk_offsets + i_n).to(tl.int32)
|
| 66 |
+
else:
|
| 67 |
+
bos, eos = i_n * T, i_n * T + T
|
| 68 |
+
NT = tl.cdiv(T, BT)
|
| 69 |
+
boh = i_n * NT
|
| 70 |
+
|
| 71 |
+
# [BK, BV]
|
| 72 |
+
b_h = tl.zeros([BK, BV], dtype=tl.float32)
|
| 73 |
+
if USE_INITIAL_STATE:
|
| 74 |
+
p_h0 = tl.make_block_ptr(h0 + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 75 |
+
b_h = tl.load(p_h0, boundary_check=(0, 1)).to(tl.float32)
|
| 76 |
+
|
| 77 |
+
for i_t in range(NT):
|
| 78 |
+
if HEAD_FIRST:
|
| 79 |
+
p_h = tl.make_block_ptr(h + (i_nh * NT + i_t) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 80 |
+
else:
|
| 81 |
+
p_h = tl.make_block_ptr(h + ((boh + i_t) * H + i_h) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 82 |
+
tl.store(p_h, b_h.to(p_h.dtype.element_ty), boundary_check=(0, 1))
|
| 83 |
+
b_hc = tl.zeros([BK, BV], dtype=tl.float32)
|
| 84 |
+
if USE_G:
|
| 85 |
+
last_idx = min((i_t + 1) * BT, T) - 1
|
| 86 |
+
if HEAD_FIRST:
|
| 87 |
+
b_g_last = tl.load(g + i_nh * T + last_idx)
|
| 88 |
+
else:
|
| 89 |
+
b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
|
| 90 |
+
else:
|
| 91 |
+
b_g_last = None
|
| 92 |
+
last_idx = None
|
| 93 |
+
# since we need to make all DK in the SRAM. we face serve SRAM memory burden. By subchunking we allievate such burden
|
| 94 |
+
for i_c in range(tl.cdiv(min(BT, T - i_t * BT), BC)):
|
| 95 |
+
if HEAD_FIRST:
|
| 96 |
+
p_k = tl.make_block_ptr(k + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT + i_c * BC), (BK, BC), (0, 1))
|
| 97 |
+
p_d = tl.make_block_ptr(d + i_nh * T*K, (T, K), (K, 1), (i_t * BT + i_c * BC, i_k * BK), (BC, BK), (1, 0))
|
| 98 |
+
p_v = tl.make_block_ptr(v + i_nh * T*V, (T, V), (V, 1), (i_t * BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 99 |
+
p_v_new = tl.make_block_ptr(v_new+i_nh*T*V, (T, V), (V, 1), (i_t * BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 100 |
+
p_g = tl.make_block_ptr(g + i_nh * T, (T,), (1,), (i_t * BT + i_c * BC,), (BC,), (0,)) if USE_G else None
|
| 101 |
+
else:
|
| 102 |
+
p_k = tl.make_block_ptr(k+(bos*H+i_h)*K, (K, T), (1, H*K), (i_k * BK, i_t * BT + i_c * BC), (BK, BC), (0, 1))
|
| 103 |
+
p_d = tl.make_block_ptr(d+(bos*H+i_h)*K, (T, K), (H*K, 1), (i_t * BT + i_c * BC, i_k * BK), (BC, BK), (1, 0))
|
| 104 |
+
p_v = tl.make_block_ptr(v+(bos*H+i_h)*V, (T, V), (H*V, 1), (i_t * BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 105 |
+
p_v_new = tl.make_block_ptr(v_new+(bos*H+i_h)*V, (T, V), (H*V, 1), (i_t*BT+i_c*BC, i_v * BV), (BC, BV), (1, 0))
|
| 106 |
+
p_g = tl.make_block_ptr(g+bos*H+i_h, (T,), (H,), (i_t*BT+i_c*BC, ), (BC,), (0,)) if USE_G else None
|
| 107 |
+
b_g = tl.load(p_g, boundary_check=(0, )) if USE_G else None
|
| 108 |
+
# [BK, BC]
|
| 109 |
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
| 110 |
+
b_k = (b_k * exp(b_g_last - b_g)[None, :]).to(b_k.dtype) if USE_G else b_k
|
| 111 |
+
# [BC, BK]
|
| 112 |
+
b_d = tl.load(p_d, boundary_check=(0, 1))
|
| 113 |
+
b_d = (b_d * exp(b_g)[:, None]).to(b_d.dtype) if USE_G else b_d
|
| 114 |
+
# [BC, BV]
|
| 115 |
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
| 116 |
+
b_v2 = b_v - tl.dot(b_d, b_h.to(b_d.dtype))
|
| 117 |
+
# [BK, BV]
|
| 118 |
+
tl.store(p_v_new, b_v2.to(p_v_new.dtype.element_ty), boundary_check=(0, 1))
|
| 119 |
+
b_hc += tl.dot(b_k, b_v2.to(b_k.dtype), allow_tf32=False)
|
| 120 |
+
b_h *= exp(b_g_last) if USE_G else 1
|
| 121 |
+
b_h += b_hc
|
| 122 |
+
|
| 123 |
+
if STORE_FINAL_STATE:
|
| 124 |
+
p_ht = tl.make_block_ptr(ht + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 125 |
+
tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
@triton.heuristics({
|
| 129 |
+
'USE_G': lambda args: args['g'] is not None,
|
| 130 |
+
'USE_INITIAL_STATE': lambda args: args['dh0'] is not None,
|
| 131 |
+
'USE_FINAL_STATE_GRADIENT': lambda args: args['dht'] is not None,
|
| 132 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None,
|
| 133 |
+
})
|
| 134 |
+
@triton.autotune(
|
| 135 |
+
configs=[
|
| 136 |
+
triton.Config({}, num_warps=num_warps, num_stages=num_stages)
|
| 137 |
+
for num_warps in NUM_WARPS
|
| 138 |
+
for num_stages in [2, 3, 4]
|
| 139 |
+
],
|
| 140 |
+
key=['BT', 'BK', 'BV', 'USE_G'],
|
| 141 |
+
use_cuda_graph=use_cuda_graph,
|
| 142 |
+
)
|
| 143 |
+
@triton.jit(do_not_specialize=['T'])
|
| 144 |
+
def chunk_gated_delta_rule_bwd_kernel_dhu(
|
| 145 |
+
q,
|
| 146 |
+
k,
|
| 147 |
+
d,
|
| 148 |
+
g,
|
| 149 |
+
dht,
|
| 150 |
+
dh0,
|
| 151 |
+
do,
|
| 152 |
+
dh,
|
| 153 |
+
dv,
|
| 154 |
+
dv2,
|
| 155 |
+
offsets,
|
| 156 |
+
chunk_offsets,
|
| 157 |
+
scale,
|
| 158 |
+
T,
|
| 159 |
+
H: tl.constexpr,
|
| 160 |
+
K: tl.constexpr,
|
| 161 |
+
V: tl.constexpr,
|
| 162 |
+
BT: tl.constexpr,
|
| 163 |
+
BC: tl.constexpr,
|
| 164 |
+
BK: tl.constexpr,
|
| 165 |
+
BV: tl.constexpr,
|
| 166 |
+
USE_G: tl.constexpr,
|
| 167 |
+
USE_INITIAL_STATE: tl.constexpr,
|
| 168 |
+
USE_FINAL_STATE_GRADIENT: tl.constexpr,
|
| 169 |
+
USE_OFFSETS: tl.constexpr,
|
| 170 |
+
HEAD_FIRST: tl.constexpr
|
| 171 |
+
):
|
| 172 |
+
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 173 |
+
i_n, i_h = i_nh // H, i_nh % H
|
| 174 |
+
if USE_OFFSETS:
|
| 175 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 176 |
+
T = eos - bos
|
| 177 |
+
NT = tl.cdiv(T, BT)
|
| 178 |
+
boh = tl.load(chunk_offsets + i_n).to(tl.int32)
|
| 179 |
+
else:
|
| 180 |
+
bos, eos = i_n * T, i_n * T + T
|
| 181 |
+
NT = tl.cdiv(T, BT)
|
| 182 |
+
boh = i_n * NT
|
| 183 |
+
|
| 184 |
+
# [BK, BV]
|
| 185 |
+
b_dh = tl.zeros([BK, BV], dtype=tl.float32)
|
| 186 |
+
if USE_FINAL_STATE_GRADIENT:
|
| 187 |
+
p_dht = tl.make_block_ptr(dht + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 188 |
+
b_dh += tl.load(p_dht, boundary_check=(0, 1))
|
| 189 |
+
|
| 190 |
+
for i_t in range(NT - 1, -1, -1):
|
| 191 |
+
if HEAD_FIRST:
|
| 192 |
+
p_dh = tl.make_block_ptr(dh + (i_nh * NT + i_t) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 193 |
+
else:
|
| 194 |
+
p_dh = tl.make_block_ptr(dh + ((boh+i_t) * H + i_h) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 195 |
+
tl.store(p_dh, b_dh.to(p_dh.dtype.element_ty), boundary_check=(0, 1))
|
| 196 |
+
b_dh_tmp = tl.zeros([BK, BV], dtype=tl.float32)
|
| 197 |
+
if USE_G:
|
| 198 |
+
last_idx = min((i_t + 1) * BT, T) - 1
|
| 199 |
+
if HEAD_FIRST:
|
| 200 |
+
bg_last = tl.load(g + i_nh * T + last_idx)
|
| 201 |
+
else:
|
| 202 |
+
bg_last = tl.load(g + (bos + last_idx) * H + i_h)
|
| 203 |
+
else:
|
| 204 |
+
bg_last = None
|
| 205 |
+
last_idx = None
|
| 206 |
+
for i_c in range(tl.cdiv(BT, BC) - 1, -1, -1):
|
| 207 |
+
if HEAD_FIRST:
|
| 208 |
+
p_q = tl.make_block_ptr(q + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT + i_c * BC), (BK, BC), (0, 1))
|
| 209 |
+
p_k = tl.make_block_ptr(k + i_nh * T*K, (T, K), (K, 1), (i_t * BT + i_c * BC, i_k * BK), (BC, BK), (1, 0))
|
| 210 |
+
p_d = tl.make_block_ptr(d + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT + i_c * BC), (BK, BC), (0, 1))
|
| 211 |
+
p_dv = tl.make_block_ptr(dv + i_nh * T*V, (T, V), (V, 1), (i_t * BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 212 |
+
p_do = tl.make_block_ptr(do + i_nh * T*V, (T, V), (V, 1), (i_t * BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 213 |
+
p_g = tl.make_block_ptr(g + i_nh * T, (T,), (1,), (i_t * BT + i_c * BC,), (BC,), (0,)) if USE_G else None
|
| 214 |
+
p_dv2 = tl.make_block_ptr(dv2 + i_nh * T*V, (T, V), (V, 1), (i_t * BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 215 |
+
else:
|
| 216 |
+
p_q = tl.make_block_ptr(q+(bos*H+i_h)*K, (K, T), (1, H*K), (i_k * BK, i_t * BT + i_c * BC), (BK, BC), (0, 1))
|
| 217 |
+
p_k = tl.make_block_ptr(k+(bos*H+i_h)*K, (T, K), (H*K, 1), (i_t * BT + i_c * BC, i_k * BK), (BC, BK), (1, 0))
|
| 218 |
+
p_d = tl.make_block_ptr(d+(bos*H+i_h)*K, (K, T), (1, H*K), (i_k * BK, i_t * BT + i_c * BC), (BK, BC), (0, 1))
|
| 219 |
+
p_dv = tl.make_block_ptr(dv+(bos*H+i_h)*V, (T, V), (H*V, 1), (i_t*BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 220 |
+
p_do = tl.make_block_ptr(do+(bos*H+i_h)*V, (T, V), (H*V, 1), (i_t*BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 221 |
+
p_g = tl.make_block_ptr(g+bos*H+i_h, (T,), (H,), (i_t*BT + i_c * BC,), (BC,), (0,)) if USE_G else None
|
| 222 |
+
p_dv2 = tl.make_block_ptr(dv2+(bos*H+i_h)*V, (T, V), (H*V, 1), (i_t*BT + i_c * BC, i_v * BV), (BC, BV), (1, 0))
|
| 223 |
+
b_g = tl.load(p_g, boundary_check=(0,)) if USE_G else None
|
| 224 |
+
# [BK, BT]
|
| 225 |
+
b_q = tl.load(p_q, boundary_check=(0, 1))
|
| 226 |
+
b_q = (b_q * scale * exp(b_g)[None, :]).to(b_q.dtype) if USE_G else (b_q * scale).to(b_q.dtype)
|
| 227 |
+
# [BT, BK]
|
| 228 |
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
| 229 |
+
b_d = tl.load(p_d, boundary_check=(0, 1))
|
| 230 |
+
b_k = (b_k * exp(bg_last - b_g)[:, None]).to(b_k.dtype) if USE_G else b_k
|
| 231 |
+
b_d = (b_d * exp(b_g)[None, :]).to(b_d.dtype) if USE_G else b_d
|
| 232 |
+
# [BT, V]
|
| 233 |
+
b_do = tl.load(p_do, boundary_check=(0, 1))
|
| 234 |
+
b_dv = tl.load(p_dv, boundary_check=(0, 1))
|
| 235 |
+
b_dv2 = b_dv + tl.dot(b_k, b_dh.to(b_k.dtype), allow_tf32=False)
|
| 236 |
+
tl.store(p_dv2, b_dv2.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
|
| 237 |
+
# [BK, BV]
|
| 238 |
+
b_dh_tmp += tl.dot(b_q, b_do.to(b_q.dtype), allow_tf32=False)
|
| 239 |
+
b_dh_tmp -= tl.dot(b_d, b_dv2.to(b_q.dtype), allow_tf32=False)
|
| 240 |
+
b_dh *= exp(bg_last) if USE_G else 1
|
| 241 |
+
b_dh += b_dh_tmp
|
| 242 |
+
|
| 243 |
+
if USE_INITIAL_STATE:
|
| 244 |
+
p_dh0 = tl.make_block_ptr(dh0 + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 245 |
+
tl.store(p_dh0, b_dh.to(p_dh0.dtype.element_ty), boundary_check=(0, 1))
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def chunk_gated_delta_rule_fwd_h(
|
| 249 |
+
k: torch.Tensor,
|
| 250 |
+
w: torch.Tensor,
|
| 251 |
+
u: torch.Tensor,
|
| 252 |
+
g: Optional[torch.Tensor] = None,
|
| 253 |
+
initial_state: Optional[torch.Tensor] = None,
|
| 254 |
+
output_final_state: bool = False,
|
| 255 |
+
offsets: Optional[torch.LongTensor] = None,
|
| 256 |
+
indices: Optional[torch.LongTensor] = None,
|
| 257 |
+
head_first: bool = True,
|
| 258 |
+
chunk_size: int = 64
|
| 259 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 260 |
+
if head_first:
|
| 261 |
+
B, H, T, K, V = *k.shape, u.shape[-1]
|
| 262 |
+
else:
|
| 263 |
+
B, T, H, K, V = *k.shape, u.shape[-1]
|
| 264 |
+
BT = min(chunk_size, max(triton.next_power_of_2(T), 16))
|
| 265 |
+
# N: the actual number of sequences in the batch with either equal or variable lengths
|
| 266 |
+
if offsets is None:
|
| 267 |
+
N, NT, chunk_offsets = B, triton.cdiv(T, BT), None
|
| 268 |
+
else:
|
| 269 |
+
N, NT, chunk_offsets = len(offsets) - 1, len(indices), prepare_chunk_offsets(offsets, BT)
|
| 270 |
+
BK = triton.next_power_of_2(K)
|
| 271 |
+
assert BK <= 256, "current kernel does not support head dimension larger than 256."
|
| 272 |
+
# H100 can have larger block size
|
| 273 |
+
if check_shared_mem('hopper', k.device.index):
|
| 274 |
+
BV = 64
|
| 275 |
+
BC = 64 if K <= 128 else 32
|
| 276 |
+
# A100
|
| 277 |
+
elif check_shared_mem('ampere', k.device.index):
|
| 278 |
+
BV = 32
|
| 279 |
+
BC = 64
|
| 280 |
+
else:
|
| 281 |
+
BV = 32
|
| 282 |
+
BC = 32 if K <= 128 else 16
|
| 283 |
+
BC = min(BT, BC)
|
| 284 |
+
NK = triton.cdiv(K, BK)
|
| 285 |
+
NV = triton.cdiv(V, BV)
|
| 286 |
+
assert NK == 1, 'NK > 1 is not supported because it involves time-consuming synchronization'
|
| 287 |
+
|
| 288 |
+
if head_first:
|
| 289 |
+
h = k.new_empty(B, H, NT, K, V)
|
| 290 |
+
else:
|
| 291 |
+
h = k.new_empty(B, NT, H, K, V)
|
| 292 |
+
final_state = k.new_empty(N, H, K, V, dtype=torch.float32) if output_final_state else None
|
| 293 |
+
|
| 294 |
+
v_new = torch.empty_like(u)
|
| 295 |
+
grid = (NK, NV, N * H)
|
| 296 |
+
|
| 297 |
+
chunk_gated_delta_rule_fwd_kernel_h[grid](
|
| 298 |
+
k=k,
|
| 299 |
+
v=u,
|
| 300 |
+
d=w,
|
| 301 |
+
v_new=v_new,
|
| 302 |
+
g=g,
|
| 303 |
+
h=h,
|
| 304 |
+
h0=initial_state,
|
| 305 |
+
ht=final_state,
|
| 306 |
+
offsets=offsets,
|
| 307 |
+
chunk_offsets=chunk_offsets,
|
| 308 |
+
T=T,
|
| 309 |
+
H=H,
|
| 310 |
+
K=K,
|
| 311 |
+
V=V,
|
| 312 |
+
BT=BT,
|
| 313 |
+
BC=BC,
|
| 314 |
+
BK=BK,
|
| 315 |
+
BV=BV,
|
| 316 |
+
NT=NT,
|
| 317 |
+
HEAD_FIRST=head_first
|
| 318 |
+
)
|
| 319 |
+
return h, v_new, final_state
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def chunk_gated_delta_rule_bwd_dhu(
|
| 323 |
+
q: torch.Tensor,
|
| 324 |
+
k: torch.Tensor,
|
| 325 |
+
w: torch.Tensor,
|
| 326 |
+
g: torch.Tensor,
|
| 327 |
+
h0: torch.Tensor,
|
| 328 |
+
dht: Optional[torch.Tensor],
|
| 329 |
+
do: torch.Tensor,
|
| 330 |
+
dv: torch.Tensor,
|
| 331 |
+
scale: float,
|
| 332 |
+
offsets: Optional[torch.LongTensor] = None,
|
| 333 |
+
indices: Optional[torch.LongTensor] = None,
|
| 334 |
+
head_first: bool = True,
|
| 335 |
+
chunk_size: int = 64
|
| 336 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
| 337 |
+
if head_first:
|
| 338 |
+
B, H, T, K, V = *q.shape, do.shape[-1]
|
| 339 |
+
else:
|
| 340 |
+
B, T, H, K, V = *q.shape, do.shape[-1]
|
| 341 |
+
BT = min(chunk_size, max(triton.next_power_of_2(T), 16))
|
| 342 |
+
# N: the actual number of sequences in the batch with either equal or variable lengths
|
| 343 |
+
if offsets is None:
|
| 344 |
+
N, NT, chunk_offsets = B, triton.cdiv(T, BT), None
|
| 345 |
+
else:
|
| 346 |
+
N, NT, chunk_offsets = len(offsets) - 1, len(indices), prepare_chunk_offsets(offsets, BT)
|
| 347 |
+
|
| 348 |
+
BK = triton.next_power_of_2(K)
|
| 349 |
+
assert BK <= 256, "current kernel does not support head dimension being larger than 256."
|
| 350 |
+
|
| 351 |
+
# H100
|
| 352 |
+
if check_shared_mem('hopper', q.device.index):
|
| 353 |
+
BV = 64
|
| 354 |
+
BC = 64 if K <= 128 else 32
|
| 355 |
+
# A100
|
| 356 |
+
elif check_shared_mem('ampere', q.device.index):
|
| 357 |
+
BV = 32
|
| 358 |
+
BC = 64 if K <= 128 else 32
|
| 359 |
+
else:
|
| 360 |
+
BV = 32 if K <= 128 else 16
|
| 361 |
+
BC = 16
|
| 362 |
+
|
| 363 |
+
BC = min(BT, BC)
|
| 364 |
+
NK, NV = triton.cdiv(K, BK), triton.cdiv(V, BV)
|
| 365 |
+
assert NK == 1, 'NK > 1 is not supported because it involves time-consuming synchronization'
|
| 366 |
+
|
| 367 |
+
if head_first:
|
| 368 |
+
dh = q.new_empty(B, H, NT, K, V)
|
| 369 |
+
else:
|
| 370 |
+
dh = q.new_empty(B, NT, H, K, V)
|
| 371 |
+
dh0 = torch.empty_like(h0, dtype=torch.float32) if h0 is not None else None
|
| 372 |
+
dv2 = torch.empty_like(dv)
|
| 373 |
+
|
| 374 |
+
grid = (NK, NV, N * H)
|
| 375 |
+
chunk_gated_delta_rule_bwd_kernel_dhu[grid](
|
| 376 |
+
q=q,
|
| 377 |
+
k=k,
|
| 378 |
+
d=w,
|
| 379 |
+
g=g,
|
| 380 |
+
dht=dht,
|
| 381 |
+
dh0=dh0,
|
| 382 |
+
do=do,
|
| 383 |
+
dh=dh,
|
| 384 |
+
dv=dv,
|
| 385 |
+
dv2=dv2,
|
| 386 |
+
offsets=offsets,
|
| 387 |
+
chunk_offsets=chunk_offsets,
|
| 388 |
+
scale=scale,
|
| 389 |
+
T=T,
|
| 390 |
+
H=H,
|
| 391 |
+
K=K,
|
| 392 |
+
V=V,
|
| 393 |
+
BT=BT,
|
| 394 |
+
BC=BC,
|
| 395 |
+
BK=BK,
|
| 396 |
+
BV=BV,
|
| 397 |
+
HEAD_FIRST=head_first
|
| 398 |
+
)
|
| 399 |
+
return dh, dh0, dv2
|
fla/ops/common/chunk_h.py
ADDED
|
@@ -0,0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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from typing import Optional, Tuple
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import triton
|
| 8 |
+
import triton.language as tl
|
| 9 |
+
|
| 10 |
+
from fla.ops.common.utils import prepare_chunk_offsets
|
| 11 |
+
from fla.ops.utils.op import exp
|
| 12 |
+
from fla.utils import check_shared_mem
|
| 13 |
+
|
| 14 |
+
BKV_LIST = [32, 64] if check_shared_mem() else [16, 32]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@triton.heuristics({
|
| 18 |
+
'USE_INITIAL_STATE': lambda args: args['h0'] is not None,
|
| 19 |
+
'STORE_FINAL_STATE': lambda args: args['ht'] is not None,
|
| 20 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 21 |
+
})
|
| 22 |
+
@triton.autotune(
|
| 23 |
+
configs=[
|
| 24 |
+
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
|
| 25 |
+
for BK in BKV_LIST
|
| 26 |
+
for BV in BKV_LIST
|
| 27 |
+
for num_warps in [1, 2, 4, 8]
|
| 28 |
+
for num_stages in [2, 3, 4]
|
| 29 |
+
],
|
| 30 |
+
key=['BT', 'USE_G', 'USE_GK', 'USE_GV']
|
| 31 |
+
)
|
| 32 |
+
@triton.jit(do_not_specialize=['T'])
|
| 33 |
+
def chunk_fwd_kernel_h(
|
| 34 |
+
k,
|
| 35 |
+
v,
|
| 36 |
+
h,
|
| 37 |
+
g,
|
| 38 |
+
gk,
|
| 39 |
+
gv,
|
| 40 |
+
h0,
|
| 41 |
+
ht,
|
| 42 |
+
offsets,
|
| 43 |
+
split_offsets,
|
| 44 |
+
T,
|
| 45 |
+
H: tl.constexpr,
|
| 46 |
+
K: tl.constexpr,
|
| 47 |
+
V: tl.constexpr,
|
| 48 |
+
BT: tl.constexpr,
|
| 49 |
+
BS: tl.constexpr,
|
| 50 |
+
BK: tl.constexpr,
|
| 51 |
+
BV: tl.constexpr,
|
| 52 |
+
USE_G: tl.constexpr,
|
| 53 |
+
USE_GK: tl.constexpr,
|
| 54 |
+
USE_GV: tl.constexpr,
|
| 55 |
+
USE_INITIAL_STATE: tl.constexpr,
|
| 56 |
+
STORE_FINAL_STATE: tl.constexpr,
|
| 57 |
+
USE_OFFSETS: tl.constexpr,
|
| 58 |
+
HEAD_FIRST: tl.constexpr
|
| 59 |
+
):
|
| 60 |
+
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 61 |
+
i_n, i_h = i_nh // H, i_nh % H
|
| 62 |
+
if USE_OFFSETS:
|
| 63 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 64 |
+
T = eos - bos
|
| 65 |
+
NT = tl.cdiv(T, BT)
|
| 66 |
+
NS = tl.cdiv(T, BS)
|
| 67 |
+
boh = tl.load(split_offsets + i_n).to(tl.int32)
|
| 68 |
+
else:
|
| 69 |
+
bos, eos = i_n * T, i_n * T + T
|
| 70 |
+
NT = tl.cdiv(T, BT)
|
| 71 |
+
NS = tl.cdiv(T, BS)
|
| 72 |
+
boh = i_n * NS
|
| 73 |
+
|
| 74 |
+
# [BK, BV]
|
| 75 |
+
b_h = tl.zeros([BK, BV], dtype=tl.float32)
|
| 76 |
+
if USE_INITIAL_STATE:
|
| 77 |
+
p_h0 = tl.make_block_ptr(h0 + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 78 |
+
b_h = tl.load(p_h0, boundary_check=(0, 1)).to(tl.float32)
|
| 79 |
+
|
| 80 |
+
for i_t in range(NT):
|
| 81 |
+
i_s = i_t // (BS // BT)
|
| 82 |
+
if HEAD_FIRST:
|
| 83 |
+
p_k = tl.make_block_ptr(k + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 84 |
+
p_v = tl.make_block_ptr(v + i_nh * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 85 |
+
|
| 86 |
+
o_h = (i_nh * NS + i_s).to(tl.int64) * K*V
|
| 87 |
+
p_h = tl.make_block_ptr(h + o_h, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 88 |
+
else:
|
| 89 |
+
p_k = tl.make_block_ptr(k + (bos*H + i_h) * K, (K, T), (1, H*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 90 |
+
p_v = tl.make_block_ptr(v + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 91 |
+
|
| 92 |
+
o_h = ((boh + i_s) * H + i_h).to(tl.int64) * K*V
|
| 93 |
+
p_h = tl.make_block_ptr(h + o_h, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 94 |
+
|
| 95 |
+
if i_t % (BS // BT) == 0:
|
| 96 |
+
tl.store(p_h, b_h.to(p_h.dtype.element_ty), boundary_check=(0, 1))
|
| 97 |
+
# [BK, BT]
|
| 98 |
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
| 99 |
+
# [BT, BV]
|
| 100 |
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
| 101 |
+
last_idx = min((i_t + 1) * BT, T) - 1
|
| 102 |
+
|
| 103 |
+
# scalar decay
|
| 104 |
+
if USE_G:
|
| 105 |
+
if HEAD_FIRST:
|
| 106 |
+
b_g_last = tl.load(g + i_nh * T + last_idx)
|
| 107 |
+
p_g = g + i_nh * T + i_t * BT + tl.arange(0, BT)
|
| 108 |
+
p_g = tl.max_contiguous(tl.multiple_of(p_g, BT), BT)
|
| 109 |
+
else:
|
| 110 |
+
b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
|
| 111 |
+
p_g = g + bos*H + (i_t * BT + tl.arange(0, BT)) * H + i_h
|
| 112 |
+
b_h *= exp(b_g_last)
|
| 113 |
+
b_g = tl.load(p_g, mask=(i_t * BT + tl.arange(0, BT) < T), other=0.)
|
| 114 |
+
b_v = (b_v * exp(b_g_last - b_g)[:, None]).to(b_v.dtype)
|
| 115 |
+
|
| 116 |
+
# vector decay, h = Diag(gk) @ h
|
| 117 |
+
if USE_GK:
|
| 118 |
+
if HEAD_FIRST:
|
| 119 |
+
p_gk = tl.make_block_ptr(gk + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 120 |
+
p_gk_last = gk + i_nh * T*K + last_idx * K + i_k * BK + tl.arange(0, BK)
|
| 121 |
+
p_gk_last = tl.max_contiguous(tl.multiple_of(p_gk_last, BK), BK)
|
| 122 |
+
else:
|
| 123 |
+
p_gk = tl.make_block_ptr(gk + (bos*H + i_h) * K, (K, T), (1, H*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 124 |
+
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
|
| 125 |
+
|
| 126 |
+
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
|
| 127 |
+
b_h *= exp(b_gk_last)[:, None]
|
| 128 |
+
|
| 129 |
+
b_gk = tl.load(p_gk, boundary_check=(0, 1))
|
| 130 |
+
b_k = (b_k * exp(b_gk_last[:, None] - b_gk)).to(b_k.dtype)
|
| 131 |
+
|
| 132 |
+
# vector decay, h = h @ Diag(gv)
|
| 133 |
+
if USE_GV:
|
| 134 |
+
if HEAD_FIRST:
|
| 135 |
+
p_gv = tl.make_block_ptr(gv + i_nh * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 136 |
+
p_gv_last = gv + i_nh * T*V + last_idx * V + i_v * BV + tl.arange(0, BV)
|
| 137 |
+
p_gv_last = tl.max_contiguous(tl.multiple_of(p_gv_last, BV), BV)
|
| 138 |
+
else:
|
| 139 |
+
p_gv = tl.make_block_ptr(gv + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 140 |
+
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
|
| 141 |
+
|
| 142 |
+
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
|
| 143 |
+
b_h *= exp(b_gv_last)[None, :]
|
| 144 |
+
|
| 145 |
+
b_gv = tl.load(p_gv, boundary_check=(0, 1))
|
| 146 |
+
b_v = (b_v * exp(b_gv_last[None, :] - b_gv)).to(b_v.dtype)
|
| 147 |
+
|
| 148 |
+
b_h += tl.dot(b_k, b_v)
|
| 149 |
+
|
| 150 |
+
if STORE_FINAL_STATE:
|
| 151 |
+
p_ht = tl.make_block_ptr(ht + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 152 |
+
tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
@triton.heuristics({
|
| 156 |
+
'STORE_INITIAL_STATE_GRADIENT': lambda args: args['dh0'] is not None,
|
| 157 |
+
'USE_FINAL_STATE_GRADIENT': lambda args: args['dht'] is not None,
|
| 158 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 159 |
+
})
|
| 160 |
+
@triton.autotune(
|
| 161 |
+
configs=[
|
| 162 |
+
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
|
| 163 |
+
for BK in BKV_LIST
|
| 164 |
+
for BV in BKV_LIST
|
| 165 |
+
for num_warps in [1, 2, 4, 8]
|
| 166 |
+
for num_stages in [2, 3, 4]
|
| 167 |
+
],
|
| 168 |
+
key=['BT', 'USE_G', 'USE_GK', 'USE_GV']
|
| 169 |
+
)
|
| 170 |
+
@triton.jit(do_not_specialize=['T'])
|
| 171 |
+
def chunk_bwd_kernel_dh(
|
| 172 |
+
q,
|
| 173 |
+
g,
|
| 174 |
+
gk,
|
| 175 |
+
gv,
|
| 176 |
+
do,
|
| 177 |
+
dh,
|
| 178 |
+
dht,
|
| 179 |
+
dh0,
|
| 180 |
+
offsets,
|
| 181 |
+
split_offsets,
|
| 182 |
+
scale,
|
| 183 |
+
T,
|
| 184 |
+
HQ: tl.constexpr,
|
| 185 |
+
H: tl.constexpr,
|
| 186 |
+
K: tl.constexpr,
|
| 187 |
+
V: tl.constexpr,
|
| 188 |
+
BT: tl.constexpr,
|
| 189 |
+
BS: tl.constexpr,
|
| 190 |
+
BK: tl.constexpr,
|
| 191 |
+
BV: tl.constexpr,
|
| 192 |
+
NG: tl.constexpr,
|
| 193 |
+
USE_G: tl.constexpr,
|
| 194 |
+
USE_GK: tl.constexpr,
|
| 195 |
+
USE_GV: tl.constexpr,
|
| 196 |
+
STORE_INITIAL_STATE_GRADIENT: tl.constexpr,
|
| 197 |
+
USE_FINAL_STATE_GRADIENT: tl.constexpr,
|
| 198 |
+
USE_OFFSETS: tl.constexpr,
|
| 199 |
+
HEAD_FIRST: tl.constexpr
|
| 200 |
+
):
|
| 201 |
+
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 202 |
+
i_bg = i_nh // NG
|
| 203 |
+
i_n, i_hq = i_nh // HQ, i_nh % HQ
|
| 204 |
+
i_h = i_hq // NG
|
| 205 |
+
if USE_OFFSETS:
|
| 206 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 207 |
+
T = eos - bos
|
| 208 |
+
NT = tl.cdiv(T, BT)
|
| 209 |
+
NS = tl.cdiv(T, BS)
|
| 210 |
+
boh = tl.load(split_offsets + i_n).to(tl.int32)
|
| 211 |
+
else:
|
| 212 |
+
bos, eos = i_n * T, i_n * T + T
|
| 213 |
+
NT = tl.cdiv(T, BT)
|
| 214 |
+
NS = tl.cdiv(T, BS)
|
| 215 |
+
boh = i_n * NS
|
| 216 |
+
|
| 217 |
+
# [BK, BV]
|
| 218 |
+
b_dh = tl.zeros([BK, BV], dtype=tl.float32)
|
| 219 |
+
if USE_FINAL_STATE_GRADIENT:
|
| 220 |
+
p_dht = tl.make_block_ptr(dht + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 221 |
+
b_dh += tl.load(p_dht, boundary_check=(0, 1)).to(tl.float32)
|
| 222 |
+
|
| 223 |
+
for i_t in range(NT - 1, -1, -1):
|
| 224 |
+
i_s = i_t // (BS // BT)
|
| 225 |
+
if HEAD_FIRST:
|
| 226 |
+
o_dh = (i_nh * NS + i_s).to(tl.int64) * K*V
|
| 227 |
+
p_dh = tl.make_block_ptr(dh + o_dh, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 228 |
+
else:
|
| 229 |
+
o_dh = ((boh + i_s) * H + i_h).to(tl.int64) * K*V
|
| 230 |
+
p_dh = tl.make_block_ptr(dh + o_dh, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 231 |
+
|
| 232 |
+
if i_t % (BS // BT) == 0:
|
| 233 |
+
tl.store(p_dh, b_dh.to(p_dh.dtype.element_ty), boundary_check=(0, 1))
|
| 234 |
+
last_idx = min(i_t * BT + BT, T) - 1
|
| 235 |
+
# [BK, BT]
|
| 236 |
+
if HEAD_FIRST:
|
| 237 |
+
p_q = tl.make_block_ptr(q + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 238 |
+
p_do = tl.make_block_ptr(do + i_nh * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 239 |
+
else:
|
| 240 |
+
p_q = tl.make_block_ptr(q + (bos*HQ + i_hq) * K, (K, T), (1, HQ*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 241 |
+
p_do = tl.make_block_ptr(do + (bos*HQ + i_hq) * V, (T, V), (HQ*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 242 |
+
b_q = tl.load(p_q, boundary_check=(0, 1))
|
| 243 |
+
b_q = (b_q * scale).to(b_q.dtype)
|
| 244 |
+
# [BT, BV]
|
| 245 |
+
b_do = tl.load(p_do, boundary_check=(0, 1))
|
| 246 |
+
|
| 247 |
+
if USE_G:
|
| 248 |
+
if HEAD_FIRST:
|
| 249 |
+
p_g = g + i_bg * T + i_t * BT + tl.arange(0, BT)
|
| 250 |
+
p_g = tl.max_contiguous(tl.multiple_of(p_g, BT), BT)
|
| 251 |
+
b_g_last = tl.load(g + i_bg * T + last_idx)
|
| 252 |
+
else:
|
| 253 |
+
p_g = g + (bos + i_t * BT + tl.arange(0, BT)) * H + i_h
|
| 254 |
+
b_g_last = tl.load(g + (bos + last_idx) * H + i_h)
|
| 255 |
+
b_g = tl.load(p_g, mask=(i_t * BT + tl.arange(0, BT) < T), other=0.)
|
| 256 |
+
b_q = (b_q * exp(b_g)[None, :]).to(b_q.dtype)
|
| 257 |
+
|
| 258 |
+
b_dh *= exp(b_g_last)
|
| 259 |
+
|
| 260 |
+
if USE_GK:
|
| 261 |
+
if HEAD_FIRST:
|
| 262 |
+
p_gk = tl.make_block_ptr(gk + i_bg * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 263 |
+
p_gk_last = gk + (i_bg * T + last_idx) * K + i_k * BK + tl.arange(0, BK)
|
| 264 |
+
p_gk_last = tl.max_contiguous(tl.multiple_of(p_gk_last, BK), BK)
|
| 265 |
+
else:
|
| 266 |
+
p_gk = tl.make_block_ptr(gk + (bos*H + i_h) * K, (K, T), (1, H*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 267 |
+
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
|
| 268 |
+
|
| 269 |
+
b_gk = tl.load(p_gk, boundary_check=(0, 1))
|
| 270 |
+
b_q = (b_q * exp(b_gk)).to(b_q.dtype)
|
| 271 |
+
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
|
| 272 |
+
b_dh *= exp(b_gk_last)[:, None]
|
| 273 |
+
|
| 274 |
+
if USE_GV:
|
| 275 |
+
if HEAD_FIRST:
|
| 276 |
+
p_gv = tl.make_block_ptr(gv + i_bg * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 277 |
+
p_gv_last = gv + (i_bg * T + last_idx) * V + i_v * BV + tl.arange(0, BV)
|
| 278 |
+
p_gv_last = tl.max_contiguous(tl.multiple_of(p_gv_last, BV), BV)
|
| 279 |
+
else:
|
| 280 |
+
p_gv = tl.make_block_ptr(gv + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 281 |
+
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
|
| 282 |
+
|
| 283 |
+
b_gv = tl.load(p_gv, boundary_check=(0, 1))
|
| 284 |
+
b_do = (b_do * exp(b_gv)).to(b_do.dtype)
|
| 285 |
+
|
| 286 |
+
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
|
| 287 |
+
b_dh *= exp(b_gv_last)[None, :]
|
| 288 |
+
|
| 289 |
+
b_dh += tl.dot(b_q, b_do)
|
| 290 |
+
|
| 291 |
+
if STORE_INITIAL_STATE_GRADIENT:
|
| 292 |
+
p_dh0 = tl.make_block_ptr(dh0 + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 293 |
+
tl.store(p_dh0, b_dh.to(p_dh0.dtype.element_ty), boundary_check=(0, 1))
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def chunk_fwd_h(
|
| 297 |
+
k: torch.Tensor,
|
| 298 |
+
v: torch.Tensor,
|
| 299 |
+
g: torch.Tensor,
|
| 300 |
+
gk: torch.Tensor,
|
| 301 |
+
gv: torch.Tensor,
|
| 302 |
+
h0: torch.Tensor,
|
| 303 |
+
output_final_state: bool,
|
| 304 |
+
offsets: Optional[torch.Tensor] = None,
|
| 305 |
+
head_first: bool = True,
|
| 306 |
+
chunk_size: int = 64,
|
| 307 |
+
split_size: Optional[int] = None,
|
| 308 |
+
states_in_fp32: bool = False
|
| 309 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 310 |
+
if head_first:
|
| 311 |
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
| 312 |
+
else:
|
| 313 |
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
| 314 |
+
BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
|
| 315 |
+
BS = BT if split_size is None else min(split_size, max(16, triton.next_power_of_2(T)))
|
| 316 |
+
assert BS % BT == 0, f"The `split_size` (got {BS}) must be a multiple of `chunk_size` {BT}"
|
| 317 |
+
# N: the actual number of sequences in the batch with either equal or variable lengths
|
| 318 |
+
if offsets is None:
|
| 319 |
+
split_offsets, N, NS = None, B, triton.cdiv(T, BS)
|
| 320 |
+
else:
|
| 321 |
+
split_offsets = prepare_chunk_offsets(offsets, BS)
|
| 322 |
+
N, NS = len(offsets) - 1, split_offsets[-1]
|
| 323 |
+
|
| 324 |
+
if head_first:
|
| 325 |
+
h = k.new_empty(B, H, NS, K, V, dtype=k.dtype if not states_in_fp32 else torch.float)
|
| 326 |
+
else:
|
| 327 |
+
h = k.new_empty(B, NS, H, K, V, dtype=k.dtype if not states_in_fp32 else torch.float)
|
| 328 |
+
ht = k.new_empty(N, H, K, V, dtype=torch.float) if output_final_state else None
|
| 329 |
+
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), N * H)
|
| 330 |
+
chunk_fwd_kernel_h[grid](
|
| 331 |
+
k=k,
|
| 332 |
+
v=v,
|
| 333 |
+
h=h,
|
| 334 |
+
g=g,
|
| 335 |
+
gk=gk,
|
| 336 |
+
gv=gv,
|
| 337 |
+
h0=h0,
|
| 338 |
+
ht=ht,
|
| 339 |
+
offsets=offsets,
|
| 340 |
+
split_offsets=split_offsets,
|
| 341 |
+
T=T,
|
| 342 |
+
H=H,
|
| 343 |
+
K=K,
|
| 344 |
+
V=V,
|
| 345 |
+
BT=BT,
|
| 346 |
+
BS=BS,
|
| 347 |
+
USE_G=g is not None,
|
| 348 |
+
USE_GK=gk is not None,
|
| 349 |
+
USE_GV=gv is not None,
|
| 350 |
+
HEAD_FIRST=head_first
|
| 351 |
+
)
|
| 352 |
+
return h, ht
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
def chunk_bwd_dh(
|
| 356 |
+
q: torch.Tensor,
|
| 357 |
+
k: torch.Tensor,
|
| 358 |
+
v: torch.Tensor,
|
| 359 |
+
g: torch.Tensor,
|
| 360 |
+
gk: torch.Tensor,
|
| 361 |
+
gv: torch.Tensor,
|
| 362 |
+
do: torch.Tensor,
|
| 363 |
+
h0: torch.Tensor,
|
| 364 |
+
dht: torch.Tensor,
|
| 365 |
+
scale: float,
|
| 366 |
+
offsets: Optional[torch.Tensor] = None,
|
| 367 |
+
head_first: bool = True,
|
| 368 |
+
chunk_size: int = 64,
|
| 369 |
+
split_size: Optional[int] = None,
|
| 370 |
+
states_in_fp32: bool = False
|
| 371 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 372 |
+
if head_first:
|
| 373 |
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
| 374 |
+
HQ = q.shape[1]
|
| 375 |
+
else:
|
| 376 |
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
| 377 |
+
HQ = q.shape[2]
|
| 378 |
+
BT = min(chunk_size, max(16, triton.next_power_of_2(T)))
|
| 379 |
+
BS = BT if split_size is None else min(split_size, max(16, triton.next_power_of_2(T)))
|
| 380 |
+
assert BS % BT == 0, f"The `split_size` (got {BS}) must be a multiple of `chunk_size` {BT}"
|
| 381 |
+
# N: the actual number of sequences in the batch with either equal or variable lengths
|
| 382 |
+
# NG: number of groups in GQA
|
| 383 |
+
if offsets is None:
|
| 384 |
+
split_offsets, N, NS = None, B, triton.cdiv(T, BS)
|
| 385 |
+
else:
|
| 386 |
+
split_offsets = prepare_chunk_offsets(offsets, BS)
|
| 387 |
+
N, NS = len(offsets) - 1, split_offsets[-1]
|
| 388 |
+
NG = HQ // H
|
| 389 |
+
|
| 390 |
+
if head_first:
|
| 391 |
+
dh = k.new_empty(B, HQ, NS, K, V, dtype=k.dtype if not states_in_fp32 else torch.float)
|
| 392 |
+
else:
|
| 393 |
+
dh = k.new_empty(B, NS, HQ, K, V, dtype=k.dtype if not states_in_fp32 else torch.float)
|
| 394 |
+
dh0 = torch.empty_like(h0, dtype=torch.float) if h0 is not None else None
|
| 395 |
+
|
| 396 |
+
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), N * H)
|
| 397 |
+
chunk_bwd_kernel_dh[grid](
|
| 398 |
+
q=q,
|
| 399 |
+
g=g,
|
| 400 |
+
gk=gk,
|
| 401 |
+
gv=gv,
|
| 402 |
+
do=do,
|
| 403 |
+
dh=dh,
|
| 404 |
+
dht=dht,
|
| 405 |
+
dh0=dh0,
|
| 406 |
+
offsets=offsets,
|
| 407 |
+
split_offsets=split_offsets,
|
| 408 |
+
scale=scale,
|
| 409 |
+
T=T,
|
| 410 |
+
HQ=HQ,
|
| 411 |
+
H=H,
|
| 412 |
+
K=K,
|
| 413 |
+
V=V,
|
| 414 |
+
BT=BT,
|
| 415 |
+
BS=BS,
|
| 416 |
+
NG=NG,
|
| 417 |
+
USE_G=g is not None,
|
| 418 |
+
USE_GK=gk is not None,
|
| 419 |
+
USE_GV=gv is not None,
|
| 420 |
+
HEAD_FIRST=head_first
|
| 421 |
+
)
|
| 422 |
+
return dh, dh0
|
fla/ops/common/chunk_h_split.py
ADDED
|
@@ -0,0 +1,677 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from typing import Optional, Tuple
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import triton
|
| 8 |
+
import triton.language as tl
|
| 9 |
+
|
| 10 |
+
from fla.ops.utils.op import exp
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@triton.heuristics({
|
| 14 |
+
'USE_INITIAL_STATE': lambda args: args['h0'] is not None,
|
| 15 |
+
'STORE_FINAL_STATE': lambda args: args['ht'] is not None,
|
| 16 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 17 |
+
})
|
| 18 |
+
@triton.autotune(
|
| 19 |
+
configs=[
|
| 20 |
+
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
|
| 21 |
+
for BK in [32, 64]
|
| 22 |
+
for BV in [32, 64]
|
| 23 |
+
for num_warps in [2, 4, 8]
|
| 24 |
+
for num_stages in [2, 3]
|
| 25 |
+
],
|
| 26 |
+
key=['BT', 'USE_G', 'USE_GK', 'USE_GV'],
|
| 27 |
+
)
|
| 28 |
+
@triton.jit(do_not_specialize=['T'])
|
| 29 |
+
def chunk_fwd_kernel_h_split(
|
| 30 |
+
k,
|
| 31 |
+
v,
|
| 32 |
+
g,
|
| 33 |
+
gk,
|
| 34 |
+
gv,
|
| 35 |
+
hs,
|
| 36 |
+
hr,
|
| 37 |
+
h0,
|
| 38 |
+
ht,
|
| 39 |
+
offsets,
|
| 40 |
+
split_indices,
|
| 41 |
+
T,
|
| 42 |
+
S: tl.constexpr,
|
| 43 |
+
H: tl.constexpr,
|
| 44 |
+
K: tl.constexpr,
|
| 45 |
+
V: tl.constexpr,
|
| 46 |
+
BT: tl.constexpr,
|
| 47 |
+
BK: tl.constexpr,
|
| 48 |
+
BV: tl.constexpr,
|
| 49 |
+
USE_G: tl.constexpr,
|
| 50 |
+
USE_GK: tl.constexpr,
|
| 51 |
+
USE_GV: tl.constexpr,
|
| 52 |
+
USE_INITIAL_STATE: tl.constexpr,
|
| 53 |
+
STORE_FINAL_STATE: tl.constexpr,
|
| 54 |
+
USE_OFFSETS: tl.constexpr,
|
| 55 |
+
HEAD_FIRST: tl.constexpr
|
| 56 |
+
):
|
| 57 |
+
# handle one split at a time
|
| 58 |
+
# i_h: head index
|
| 59 |
+
# i_n: sequence index
|
| 60 |
+
# i_s: local split index inside a sequence
|
| 61 |
+
i_k, i_v, i_sh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 62 |
+
i_ss, i_h = i_sh // H, i_sh % H
|
| 63 |
+
if USE_OFFSETS:
|
| 64 |
+
i_n, i_s = tl.load(split_indices + i_ss * 2).to(tl.int32), tl.load(split_indices + i_ss * 2 + 1).to(tl.int32)
|
| 65 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 66 |
+
T = eos - bos
|
| 67 |
+
NS = tl.cdiv(T, S)
|
| 68 |
+
else:
|
| 69 |
+
NS = tl.cdiv(T, S)
|
| 70 |
+
i_n, i_s = i_ss // NS, i_ss % NS
|
| 71 |
+
bos, eos = i_n * T, i_n * T + T
|
| 72 |
+
i_nh = i_n * H + i_h
|
| 73 |
+
|
| 74 |
+
# [BK, BV]
|
| 75 |
+
b_h = tl.zeros([BK, BV], dtype=tl.float32)
|
| 76 |
+
# for the first split, we directly store the state as the final result
|
| 77 |
+
if i_s == 0:
|
| 78 |
+
if USE_INITIAL_STATE:
|
| 79 |
+
p_h0 = tl.make_block_ptr(h0 + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 80 |
+
b_h += tl.load(p_h0, boundary_check=(0, 1)).to(tl.float32)
|
| 81 |
+
p_hr = tl.make_block_ptr(hr + i_sh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 82 |
+
tl.store(p_hr, b_h.to(p_hr.dtype.element_ty), boundary_check=(0, 1))
|
| 83 |
+
for i_t in range(tl.cdiv(i_s * S, BT), tl.cdiv(min(i_s * S + S, T), BT)):
|
| 84 |
+
if HEAD_FIRST:
|
| 85 |
+
p_k = tl.make_block_ptr(k + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 86 |
+
p_v = tl.make_block_ptr(v + i_nh * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 87 |
+
else:
|
| 88 |
+
p_k = tl.make_block_ptr(k + (bos*H + i_h) * K, (K, T), (1, H*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 89 |
+
p_v = tl.make_block_ptr(v + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 90 |
+
# [BK, BT]
|
| 91 |
+
b_k = tl.load(p_k, boundary_check=(0, 1))
|
| 92 |
+
# [BT, BV]
|
| 93 |
+
b_v = tl.load(p_v, boundary_check=(0, 1))
|
| 94 |
+
last_idx = min(i_t * BT + BT, T) - 1
|
| 95 |
+
|
| 96 |
+
# scalar decay
|
| 97 |
+
if USE_G:
|
| 98 |
+
if HEAD_FIRST:
|
| 99 |
+
b_g_last = tl.load(g + i_nh * T + last_idx)
|
| 100 |
+
p_g = g + i_nh * T + i_t * BT + tl.arange(0, BT)
|
| 101 |
+
p_g = tl.max_contiguous(tl.multiple_of(p_g, BT), BT)
|
| 102 |
+
else:
|
| 103 |
+
b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
|
| 104 |
+
p_g = g + bos*H + (i_t * BT + tl.arange(0, BT)) * H + i_h
|
| 105 |
+
b_h *= exp(b_g_last)
|
| 106 |
+
b_g = tl.load(p_g, mask=(i_t * BT + tl.arange(0, BT) < T), other=0.)
|
| 107 |
+
b_v = (b_v * exp(b_g_last - b_g)[:, None]).to(b_v.dtype)
|
| 108 |
+
|
| 109 |
+
# vector decay, h = Diag(gk) @ h
|
| 110 |
+
if USE_GK:
|
| 111 |
+
if HEAD_FIRST:
|
| 112 |
+
p_gk = tl.make_block_ptr(gk + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 113 |
+
p_gk_last = gk + i_nh * T*K + last_idx * K + i_k * BK + tl.arange(0, BK)
|
| 114 |
+
p_gk_last = tl.max_contiguous(tl.multiple_of(p_gk_last, BK), BK)
|
| 115 |
+
else:
|
| 116 |
+
p_gk = tl.make_block_ptr(gk + (bos*H + i_h) * K, (K, T), (1, H*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 117 |
+
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
|
| 118 |
+
|
| 119 |
+
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
|
| 120 |
+
b_h *= exp(b_gk_last)[:, None]
|
| 121 |
+
|
| 122 |
+
b_gk = tl.load(p_gk, boundary_check=(0, 1))
|
| 123 |
+
b_k = (b_k * exp(b_gk_last[:, None] - b_gk)).to(b_k.dtype)
|
| 124 |
+
|
| 125 |
+
# vector decay, h = h @ Diag(gv)
|
| 126 |
+
if USE_GV:
|
| 127 |
+
if HEAD_FIRST:
|
| 128 |
+
p_gv = tl.make_block_ptr(gv + i_nh * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 129 |
+
p_gv_last = gv + i_nh * T*V + last_idx * V + i_v * BV + tl.arange(0, BV)
|
| 130 |
+
p_gv_last = tl.max_contiguous(tl.multiple_of(p_gv_last, BV), BV)
|
| 131 |
+
else:
|
| 132 |
+
p_gv = tl.make_block_ptr(gv + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 133 |
+
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
|
| 134 |
+
|
| 135 |
+
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
|
| 136 |
+
b_h *= exp(b_gv_last)[None, :]
|
| 137 |
+
|
| 138 |
+
b_gv = tl.load(p_gv, boundary_check=(0, 1))
|
| 139 |
+
b_v = (b_v * exp(b_gv_last[None, :] - b_gv)).to(b_v.dtype)
|
| 140 |
+
|
| 141 |
+
b_h += tl.dot(b_k, b_v)
|
| 142 |
+
|
| 143 |
+
# if there are more than one splits, we store the result to (unreduced) hs
|
| 144 |
+
# otherwise, we store the result to ht as the final state
|
| 145 |
+
if NS > 1:
|
| 146 |
+
p_hs = tl.make_block_ptr(hs + i_sh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 147 |
+
tl.store(p_hs, b_h.to(p_hs.dtype.element_ty), boundary_check=(0, 1))
|
| 148 |
+
elif STORE_FINAL_STATE:
|
| 149 |
+
p_ht = tl.make_block_ptr(ht + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 150 |
+
tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
@triton.heuristics({
|
| 154 |
+
'STORE_FINAL_STATE': lambda args: args['ht'] is not None,
|
| 155 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 156 |
+
})
|
| 157 |
+
@triton.autotune(
|
| 158 |
+
configs=[
|
| 159 |
+
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
|
| 160 |
+
for BK in [32, 64]
|
| 161 |
+
for BV in [32, 64]
|
| 162 |
+
for num_warps in [2, 4, 8]
|
| 163 |
+
for num_stages in [2, 3, 4]
|
| 164 |
+
],
|
| 165 |
+
key=['BT', 'USE_G', 'USE_GK', 'USE_GV'],
|
| 166 |
+
)
|
| 167 |
+
@triton.jit(do_not_specialize=['T'])
|
| 168 |
+
def chunk_fwd_kernel_h_reduction(
|
| 169 |
+
g,
|
| 170 |
+
gk,
|
| 171 |
+
gv,
|
| 172 |
+
hs,
|
| 173 |
+
hr,
|
| 174 |
+
ht,
|
| 175 |
+
offsets,
|
| 176 |
+
split_offsets,
|
| 177 |
+
T,
|
| 178 |
+
S: tl.constexpr,
|
| 179 |
+
H: tl.constexpr,
|
| 180 |
+
K: tl.constexpr,
|
| 181 |
+
V: tl.constexpr,
|
| 182 |
+
BT: tl.constexpr,
|
| 183 |
+
BK: tl.constexpr,
|
| 184 |
+
BV: tl.constexpr,
|
| 185 |
+
USE_G: tl.constexpr,
|
| 186 |
+
USE_GK: tl.constexpr,
|
| 187 |
+
USE_GV: tl.constexpr,
|
| 188 |
+
STORE_FINAL_STATE: tl.constexpr,
|
| 189 |
+
USE_OFFSETS: tl.constexpr,
|
| 190 |
+
HEAD_FIRST: tl.constexpr
|
| 191 |
+
):
|
| 192 |
+
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 193 |
+
i_n, i_h = i_nh // H, i_nh % H
|
| 194 |
+
if USE_OFFSETS:
|
| 195 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 196 |
+
T = eos - bos
|
| 197 |
+
NS = tl.cdiv(T, S)
|
| 198 |
+
boh = tl.load(split_offsets + i_n).to(tl.int32)
|
| 199 |
+
else:
|
| 200 |
+
bos, eos = i_n * T, i_n * T + T
|
| 201 |
+
NS = tl.cdiv(T, S)
|
| 202 |
+
boh = i_n * NS
|
| 203 |
+
|
| 204 |
+
b_h = tl.zeros([BK, BV], dtype=tl.float32)
|
| 205 |
+
# skip the first split
|
| 206 |
+
for i_s in range(1, NS):
|
| 207 |
+
p_hs = tl.make_block_ptr(hs + ((boh + i_s-1) * H + i_h) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 208 |
+
p_hr = tl.make_block_ptr(hr + ((boh + i_s) * H + i_h) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 209 |
+
b_h += tl.load(p_hs, boundary_check=(0, 1)).to(tl.float32)
|
| 210 |
+
tl.store(p_hr, b_h.to(p_hr.dtype.element_ty), boundary_check=(0, 1))
|
| 211 |
+
|
| 212 |
+
for i_t in range(tl.cdiv(i_s * S, BT), tl.cdiv(min(i_s * S + S, T), BT)):
|
| 213 |
+
last_idx = min(i_t * BT + BT, T) - 1
|
| 214 |
+
# scalar decay
|
| 215 |
+
if USE_G:
|
| 216 |
+
if HEAD_FIRST:
|
| 217 |
+
b_g_last = tl.load(g + i_nh * T + last_idx)
|
| 218 |
+
else:
|
| 219 |
+
b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
|
| 220 |
+
b_h *= exp(b_g_last)
|
| 221 |
+
|
| 222 |
+
# vector decay, h = Diag(gk) @ h
|
| 223 |
+
if USE_GK:
|
| 224 |
+
if HEAD_FIRST:
|
| 225 |
+
p_gk_last = gk + i_nh * T*K + last_idx * K + i_k * BK + tl.arange(0, BK)
|
| 226 |
+
p_gk_last = tl.max_contiguous(tl.multiple_of(p_gk_last, BK), BK)
|
| 227 |
+
else:
|
| 228 |
+
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
|
| 229 |
+
|
| 230 |
+
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
|
| 231 |
+
b_h *= exp(b_gk_last)[:, None]
|
| 232 |
+
|
| 233 |
+
# vector decay, h = h @ Diag(gv)
|
| 234 |
+
if USE_GV:
|
| 235 |
+
if HEAD_FIRST:
|
| 236 |
+
p_gv_last = gv + i_nh * T*V + last_idx * V + i_v * BV + tl.arange(0, BV)
|
| 237 |
+
p_gv_last = tl.max_contiguous(tl.multiple_of(p_gv_last, BV), BV)
|
| 238 |
+
else:
|
| 239 |
+
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
|
| 240 |
+
|
| 241 |
+
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
|
| 242 |
+
b_h *= exp(b_gv_last)[None, :]
|
| 243 |
+
|
| 244 |
+
if NS > 1:
|
| 245 |
+
if STORE_FINAL_STATE:
|
| 246 |
+
p_hs = tl.make_block_ptr(hs + ((boh + NS-1) * H + i_h)*K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 247 |
+
p_ht = tl.make_block_ptr(ht + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 248 |
+
b_h += tl.load(p_hs, boundary_check=(0, 1)).to(tl.float32)
|
| 249 |
+
tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
@triton.heuristics({
|
| 253 |
+
'USE_FINAL_STATE_GRADIENT': lambda args: args['dht'] is not None,
|
| 254 |
+
'STORE_INITIAL_STATE_GRADIENT': lambda args: args['dh0'] is not None,
|
| 255 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 256 |
+
})
|
| 257 |
+
@triton.autotune(
|
| 258 |
+
configs=[
|
| 259 |
+
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
|
| 260 |
+
for BK in [32, 64]
|
| 261 |
+
for BV in [32, 64]
|
| 262 |
+
for num_warps in [2, 4, 8]
|
| 263 |
+
for num_stages in [2, 3]
|
| 264 |
+
],
|
| 265 |
+
key=['BT', 'USE_G', 'USE_GK', 'USE_GV'],
|
| 266 |
+
)
|
| 267 |
+
@triton.jit(do_not_specialize=['T'])
|
| 268 |
+
def chunk_bwd_kernel_dh_split(
|
| 269 |
+
q,
|
| 270 |
+
g,
|
| 271 |
+
gk,
|
| 272 |
+
gv,
|
| 273 |
+
do,
|
| 274 |
+
dht,
|
| 275 |
+
dhs,
|
| 276 |
+
dhr,
|
| 277 |
+
dh0,
|
| 278 |
+
offsets,
|
| 279 |
+
split_indices,
|
| 280 |
+
scale,
|
| 281 |
+
T,
|
| 282 |
+
S: tl.constexpr,
|
| 283 |
+
HQ: tl.constexpr,
|
| 284 |
+
H: tl.constexpr,
|
| 285 |
+
K: tl.constexpr,
|
| 286 |
+
V: tl.constexpr,
|
| 287 |
+
BT: tl.constexpr,
|
| 288 |
+
BK: tl.constexpr,
|
| 289 |
+
BV: tl.constexpr,
|
| 290 |
+
NG: tl.constexpr,
|
| 291 |
+
USE_G: tl.constexpr,
|
| 292 |
+
USE_GK: tl.constexpr,
|
| 293 |
+
USE_GV: tl.constexpr,
|
| 294 |
+
USE_FINAL_STATE_GRADIENT: tl.constexpr,
|
| 295 |
+
STORE_INITIAL_STATE_GRADIENT: tl.constexpr,
|
| 296 |
+
USE_OFFSETS: tl.constexpr,
|
| 297 |
+
HEAD_FIRST: tl.constexpr
|
| 298 |
+
):
|
| 299 |
+
# handle one split at a time
|
| 300 |
+
# i_h: head index
|
| 301 |
+
# i_n: sequence index
|
| 302 |
+
# i_s: local split index inside a sequence
|
| 303 |
+
i_k, i_v, i_sh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 304 |
+
i_ss, i_hq = i_sh // HQ, i_sh % HQ
|
| 305 |
+
if USE_OFFSETS:
|
| 306 |
+
i_n, i_s = tl.load(split_indices + i_ss * 2).to(tl.int32), tl.load(split_indices + i_ss * 2 + 1).to(tl.int32)
|
| 307 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 308 |
+
T = eos - bos
|
| 309 |
+
NS = tl.cdiv(T, S)
|
| 310 |
+
else:
|
| 311 |
+
NS = tl.cdiv(T, S)
|
| 312 |
+
i_n, i_s = i_ss // NS, i_ss % NS
|
| 313 |
+
bos, eos = i_n * T, i_n * T + T
|
| 314 |
+
i_nh = i_n * HQ + i_hq
|
| 315 |
+
i_ng, i_h = i_nh // NG, i_hq // NG
|
| 316 |
+
|
| 317 |
+
# [BK, BV]
|
| 318 |
+
b_dh = tl.zeros([BK, BV], dtype=tl.float32)
|
| 319 |
+
if i_s == NS - 1:
|
| 320 |
+
if USE_FINAL_STATE_GRADIENT:
|
| 321 |
+
p_dht = tl.make_block_ptr(dht + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 322 |
+
b_dh += tl.load(p_dht, boundary_check=(0, 1)).to(tl.float32)
|
| 323 |
+
p_dhr = tl.make_block_ptr(dhr + i_sh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 324 |
+
tl.store(p_dhr, b_dh.to(p_dhr.dtype.element_ty), boundary_check=(0, 1))
|
| 325 |
+
|
| 326 |
+
for i_t in range(tl.cdiv(min(i_s * S + S, T), BT) - 1, tl.cdiv(i_s * S, BT) - 1, -1):
|
| 327 |
+
if HEAD_FIRST:
|
| 328 |
+
p_q = tl.make_block_ptr(q + i_nh * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 329 |
+
p_do = tl.make_block_ptr(do + i_nh * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 330 |
+
else:
|
| 331 |
+
p_q = tl.make_block_ptr(q + (bos*HQ + i_hq) * K, (K, T), (1, HQ*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 332 |
+
p_do = tl.make_block_ptr(do + (bos*HQ + i_hq) * V, (T, V), (HQ*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 333 |
+
|
| 334 |
+
b_q = tl.load(p_q, boundary_check=(0, 1))
|
| 335 |
+
b_q = (b_q * scale).to(b_q.dtype)
|
| 336 |
+
# [BT, BV]
|
| 337 |
+
b_do = tl.load(p_do, boundary_check=(0, 1))
|
| 338 |
+
|
| 339 |
+
last_idx = min(i_t * BT + BT, T) - 1
|
| 340 |
+
if USE_G:
|
| 341 |
+
if HEAD_FIRST:
|
| 342 |
+
p_g = g + i_ng * T + i_t * BT + tl.arange(0, BT)
|
| 343 |
+
p_g = tl.max_contiguous(tl.multiple_of(p_g, BT), BT)
|
| 344 |
+
b_g_last = tl.load(g + i_ng * T + last_idx)
|
| 345 |
+
else:
|
| 346 |
+
p_g = g + (bos + i_t * BT + tl.arange(0, BT)) * H + i_h
|
| 347 |
+
b_g_last = tl.load(g + (bos + last_idx) * H + i_h)
|
| 348 |
+
b_g = tl.load(p_g, mask=(i_t * BT + tl.arange(0, BT) < T), other=0.)
|
| 349 |
+
b_q = (b_q * exp(b_g)[None, :]).to(b_q.dtype)
|
| 350 |
+
b_dh *= exp(b_g_last)
|
| 351 |
+
|
| 352 |
+
if USE_GK:
|
| 353 |
+
if HEAD_FIRST:
|
| 354 |
+
p_gk = tl.make_block_ptr(gk + i_ng * T*K, (K, T), (1, K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 355 |
+
p_gk_last = gk + (i_ng * T + last_idx) * K + i_k * BK + tl.arange(0, BK)
|
| 356 |
+
p_gk_last = tl.max_contiguous(tl.multiple_of(p_gk_last, BK), BK)
|
| 357 |
+
else:
|
| 358 |
+
p_gk = tl.make_block_ptr(gk + (bos*H + i_h) * K, (K, T), (1, H*K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
|
| 359 |
+
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
|
| 360 |
+
|
| 361 |
+
b_gk = tl.load(p_gk, boundary_check=(0, 1))
|
| 362 |
+
b_q = (b_q * exp(b_gk)).to(b_q.dtype)
|
| 363 |
+
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
|
| 364 |
+
b_dh *= exp(b_gk_last)[:, None]
|
| 365 |
+
|
| 366 |
+
if USE_GV:
|
| 367 |
+
if HEAD_FIRST:
|
| 368 |
+
p_gv = tl.make_block_ptr(gv + i_ng * T*V, (T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 369 |
+
p_gv_last = gv + (i_ng * T + last_idx) * V + i_v * BV + tl.arange(0, BV)
|
| 370 |
+
p_gv_last = tl.max_contiguous(tl.multiple_of(p_gv_last, BV), BV)
|
| 371 |
+
else:
|
| 372 |
+
p_gv = tl.make_block_ptr(gv + (bos*H + i_h) * V, (T, V), (H*V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
|
| 373 |
+
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
|
| 374 |
+
|
| 375 |
+
b_gv = tl.load(p_gv, boundary_check=(0, 1))
|
| 376 |
+
b_do = (b_do * exp(b_gv)).to(b_do.dtype)
|
| 377 |
+
|
| 378 |
+
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
|
| 379 |
+
b_dh *= exp(b_gv_last)[None, :]
|
| 380 |
+
|
| 381 |
+
b_dh += tl.dot(b_q, b_do)
|
| 382 |
+
|
| 383 |
+
if NS > 1:
|
| 384 |
+
p_dhs = tl.make_block_ptr(dhs + i_sh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 385 |
+
tl.store(p_dhs, b_dh.to(p_dhs.dtype.element_ty), boundary_check=(0, 1))
|
| 386 |
+
elif STORE_INITIAL_STATE_GRADIENT:
|
| 387 |
+
p_dh0 = tl.make_block_ptr(dh0 + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 388 |
+
tl.store(p_dh0, b_dh.to(p_dh0.dtype.element_ty), boundary_check=(0, 1))
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
@triton.heuristics({
|
| 392 |
+
'STORE_INITIAL_STATE_GRADIENT': lambda args: args['dh0'] is not None,
|
| 393 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 394 |
+
})
|
| 395 |
+
@triton.autotune(
|
| 396 |
+
configs=[
|
| 397 |
+
triton.Config({'BK': BK, 'BV': BV}, num_warps=num_warps, num_stages=num_stages)
|
| 398 |
+
for BK in [32, 64]
|
| 399 |
+
for BV in [32, 64]
|
| 400 |
+
for num_warps in [2, 4, 8]
|
| 401 |
+
for num_stages in [2, 3, 4]
|
| 402 |
+
],
|
| 403 |
+
key=['BT', 'USE_G', 'USE_GK', 'USE_GV'],
|
| 404 |
+
)
|
| 405 |
+
@triton.jit(do_not_specialize=['T'])
|
| 406 |
+
def chunk_bwd_kernel_dh_reduction(
|
| 407 |
+
g,
|
| 408 |
+
gk,
|
| 409 |
+
gv,
|
| 410 |
+
dhs,
|
| 411 |
+
dhr,
|
| 412 |
+
dh0,
|
| 413 |
+
offsets,
|
| 414 |
+
split_offsets,
|
| 415 |
+
T,
|
| 416 |
+
S: tl.constexpr,
|
| 417 |
+
H: tl.constexpr,
|
| 418 |
+
HQ: tl.constexpr,
|
| 419 |
+
K: tl.constexpr,
|
| 420 |
+
V: tl.constexpr,
|
| 421 |
+
BT: tl.constexpr,
|
| 422 |
+
BK: tl.constexpr,
|
| 423 |
+
BV: tl.constexpr,
|
| 424 |
+
NG: tl.constexpr,
|
| 425 |
+
USE_G: tl.constexpr,
|
| 426 |
+
USE_GK: tl.constexpr,
|
| 427 |
+
USE_GV: tl.constexpr,
|
| 428 |
+
STORE_INITIAL_STATE_GRADIENT: tl.constexpr,
|
| 429 |
+
USE_OFFSETS: tl.constexpr,
|
| 430 |
+
HEAD_FIRST: tl.constexpr
|
| 431 |
+
):
|
| 432 |
+
i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 433 |
+
i_n, i_hq = i_nh // HQ, i_nh % HQ
|
| 434 |
+
i_ng, i_h = i_nh // NG, i_hq // NG
|
| 435 |
+
if USE_OFFSETS:
|
| 436 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets + i_n + 1).to(tl.int32)
|
| 437 |
+
T = eos - bos
|
| 438 |
+
NS = tl.cdiv(T, S)
|
| 439 |
+
boh = tl.load(split_offsets + i_n).to(tl.int32)
|
| 440 |
+
else:
|
| 441 |
+
bos, eos = i_n * T, i_n * T + T
|
| 442 |
+
NS = tl.cdiv(T, S)
|
| 443 |
+
boh = i_n * NS
|
| 444 |
+
|
| 445 |
+
b_dh = tl.zeros([BK, BV], dtype=tl.float32)
|
| 446 |
+
for i_s in range(NS - 2, -1, -1):
|
| 447 |
+
p_dhs = tl.make_block_ptr(dhs + ((boh+i_s+1) * H + i_h) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 448 |
+
p_dhr = tl.make_block_ptr(dhr + ((boh+i_s) * H + i_h) * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 449 |
+
b_dh += tl.load(p_dhs, boundary_check=(0, 1)).to(tl.float32)
|
| 450 |
+
tl.store(p_dhr, b_dh.to(p_dhr.dtype.element_ty), boundary_check=(0, 1))
|
| 451 |
+
|
| 452 |
+
for i_t in range(tl.cdiv(min(i_s * S + S, T), BT) - 1, tl.cdiv(i_s * S, BT) - 1, -1):
|
| 453 |
+
last_idx = min(i_t * BT + BT, T) - 1
|
| 454 |
+
# scalar decay
|
| 455 |
+
if USE_G:
|
| 456 |
+
if HEAD_FIRST:
|
| 457 |
+
b_g_last = tl.load(g + i_ng * T + last_idx)
|
| 458 |
+
else:
|
| 459 |
+
b_g_last = tl.load(g + (bos + last_idx) * H + i_h)
|
| 460 |
+
b_dh *= exp(b_g_last)
|
| 461 |
+
|
| 462 |
+
if USE_GK:
|
| 463 |
+
if HEAD_FIRST:
|
| 464 |
+
p_gk_last = gk + (i_ng * T + last_idx) * K + i_k * BK + tl.arange(0, BK)
|
| 465 |
+
p_gk_last = tl.max_contiguous(tl.multiple_of(p_gk_last, BK), BK)
|
| 466 |
+
else:
|
| 467 |
+
p_gk_last = gk + (bos + last_idx) * H*K + i_h * K + i_k * BK + tl.arange(0, BK)
|
| 468 |
+
|
| 469 |
+
b_gk_last = tl.load(p_gk_last, mask=(i_k * BK + tl.arange(0, BK) < K), other=0.)
|
| 470 |
+
b_dh *= exp(b_gk_last)[:, None]
|
| 471 |
+
|
| 472 |
+
if USE_GV:
|
| 473 |
+
if HEAD_FIRST:
|
| 474 |
+
p_gv_last = gv + (i_ng * T + last_idx) * V + i_v * BV + tl.arange(0, BV)
|
| 475 |
+
p_gv_last = tl.max_contiguous(tl.multiple_of(p_gv_last, BV), BV)
|
| 476 |
+
else:
|
| 477 |
+
p_gv_last = gv + (bos + last_idx) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
|
| 478 |
+
|
| 479 |
+
b_gv_last = tl.load(p_gv_last, mask=(i_v * BV + tl.arange(0, BV) < V), other=0.)
|
| 480 |
+
b_dh *= exp(b_gv_last)[None, :]
|
| 481 |
+
|
| 482 |
+
if NS > 1:
|
| 483 |
+
if STORE_INITIAL_STATE_GRADIENT:
|
| 484 |
+
p_dhs = tl.make_block_ptr(dhs + (boh * H + i_h)*K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 485 |
+
p_dh0 = tl.make_block_ptr(dh0 + i_nh * K*V, (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
|
| 486 |
+
b_dh += tl.load(p_dhs, boundary_check=(0, 1)).to(tl.float32)
|
| 487 |
+
tl.store(p_dh0, b_dh.to(p_dh0.dtype.element_ty), boundary_check=(0, 1))
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
def chunk_fwd_h(
|
| 491 |
+
k: torch.Tensor,
|
| 492 |
+
v: torch.Tensor,
|
| 493 |
+
g: torch.Tensor,
|
| 494 |
+
gk: torch.Tensor,
|
| 495 |
+
gv: torch.Tensor,
|
| 496 |
+
h0: torch.Tensor,
|
| 497 |
+
output_final_state: bool,
|
| 498 |
+
offsets: Optional[torch.LongTensor] = None,
|
| 499 |
+
split_offsets: Optional[torch.LongTensor] = None,
|
| 500 |
+
split_indices: Optional[torch.LongTensor] = None,
|
| 501 |
+
head_first: bool = True,
|
| 502 |
+
chunk_size: int = 64,
|
| 503 |
+
split_size: int = 256,
|
| 504 |
+
states_in_fp32: bool = True
|
| 505 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 506 |
+
if head_first:
|
| 507 |
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
| 508 |
+
else:
|
| 509 |
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
| 510 |
+
# B: batch size
|
| 511 |
+
# N: the actual number of sequences in the batch
|
| 512 |
+
# H: number of heads
|
| 513 |
+
# T: sequence length, can be variable across sequences
|
| 514 |
+
# S: split size, a multiple of chunk size
|
| 515 |
+
# BT: chunk size
|
| 516 |
+
S, BT = split_size, chunk_size
|
| 517 |
+
assert S % BT == 0, f"The `split_size` (got {S}) must be a multiple of `chunk_size` {BT}"
|
| 518 |
+
if offsets is None:
|
| 519 |
+
N = B
|
| 520 |
+
NS = N * triton.cdiv(T, S)
|
| 521 |
+
else:
|
| 522 |
+
N = len(offsets) - 1
|
| 523 |
+
NS = split_offsets[-1]
|
| 524 |
+
|
| 525 |
+
# unreduced kv states per split
|
| 526 |
+
hs = k.new_empty(NS, H, K, V, dtype=torch.float)
|
| 527 |
+
# reduced states per split
|
| 528 |
+
hr = k.new_empty(NS, H, K, V, dtype=torch.float if states_in_fp32 else k.dtype)
|
| 529 |
+
ht = k.new_empty(N, H, K, V, dtype=torch.float) if output_final_state else None
|
| 530 |
+
# parallelized over splits
|
| 531 |
+
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), NS * H)
|
| 532 |
+
chunk_fwd_kernel_h_split[grid](
|
| 533 |
+
k=k,
|
| 534 |
+
v=v,
|
| 535 |
+
g=g,
|
| 536 |
+
gk=gk,
|
| 537 |
+
gv=gv,
|
| 538 |
+
hs=hs,
|
| 539 |
+
hr=hr,
|
| 540 |
+
h0=h0,
|
| 541 |
+
ht=ht,
|
| 542 |
+
offsets=offsets,
|
| 543 |
+
split_indices=split_indices,
|
| 544 |
+
T=T,
|
| 545 |
+
S=S,
|
| 546 |
+
H=H,
|
| 547 |
+
K=K,
|
| 548 |
+
V=V,
|
| 549 |
+
BT=BT,
|
| 550 |
+
USE_G=g is not None,
|
| 551 |
+
USE_GK=gk is not None,
|
| 552 |
+
USE_GV=gv is not None,
|
| 553 |
+
HEAD_FIRST=head_first
|
| 554 |
+
)
|
| 555 |
+
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), N * H)
|
| 556 |
+
chunk_fwd_kernel_h_reduction[grid](
|
| 557 |
+
g=g,
|
| 558 |
+
gk=gk,
|
| 559 |
+
gv=gv,
|
| 560 |
+
hs=hs,
|
| 561 |
+
hr=hr,
|
| 562 |
+
ht=ht,
|
| 563 |
+
offsets=offsets,
|
| 564 |
+
split_offsets=split_offsets,
|
| 565 |
+
T=T,
|
| 566 |
+
S=S,
|
| 567 |
+
H=H,
|
| 568 |
+
K=K,
|
| 569 |
+
V=V,
|
| 570 |
+
BT=BT,
|
| 571 |
+
USE_G=g is not None,
|
| 572 |
+
USE_GK=gk is not None,
|
| 573 |
+
USE_GV=gv is not None,
|
| 574 |
+
HEAD_FIRST=head_first
|
| 575 |
+
)
|
| 576 |
+
return hr, ht
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
def chunk_bwd_dh(
|
| 580 |
+
q: torch.Tensor,
|
| 581 |
+
k: torch.Tensor,
|
| 582 |
+
v: torch.Tensor,
|
| 583 |
+
g: torch.Tensor,
|
| 584 |
+
gk: torch.Tensor,
|
| 585 |
+
gv: torch.Tensor,
|
| 586 |
+
do: torch.Tensor,
|
| 587 |
+
h0: torch.Tensor,
|
| 588 |
+
dht: torch.Tensor,
|
| 589 |
+
scale: float,
|
| 590 |
+
offsets: Optional[torch.Tensor] = None,
|
| 591 |
+
split_offsets: Optional[torch.Tensor] = None,
|
| 592 |
+
split_indices: Optional[torch.Tensor] = None,
|
| 593 |
+
head_first: bool = True,
|
| 594 |
+
chunk_size: int = 64,
|
| 595 |
+
split_size: int = 256,
|
| 596 |
+
states_in_fp32: bool = True
|
| 597 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 598 |
+
if head_first:
|
| 599 |
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
| 600 |
+
HQ = q.shape[1]
|
| 601 |
+
else:
|
| 602 |
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
| 603 |
+
HQ = q.shape[2]
|
| 604 |
+
# B: batch size
|
| 605 |
+
# N: the actual number of sequences in the batch
|
| 606 |
+
# H: number of heads
|
| 607 |
+
# T: sequence length, can be variable across sequences
|
| 608 |
+
# S: split size, a multiple of chunk size
|
| 609 |
+
# BT: chunk size
|
| 610 |
+
S, BT = max(chunk_size, min(split_size, triton.next_power_of_2(T))), chunk_size
|
| 611 |
+
assert S % BT == 0, f"The `split_size` (got {S}) must be a multiple of `chunk_size` {BT}"
|
| 612 |
+
if offsets is None:
|
| 613 |
+
N = B
|
| 614 |
+
NS = N * triton.cdiv(T, S)
|
| 615 |
+
else:
|
| 616 |
+
N = len(offsets) - 1
|
| 617 |
+
NS = split_offsets[-1]
|
| 618 |
+
# number of groups in GQA
|
| 619 |
+
NG = HQ // H
|
| 620 |
+
|
| 621 |
+
dhs = q.new_empty(NS, HQ, K, V, dtype=torch.float)
|
| 622 |
+
dhr = q.new_empty(NS, HQ, K, V, dtype=torch.float if states_in_fp32 else k.dtype)
|
| 623 |
+
dh0 = torch.empty_like(h0, dtype=torch.float) if h0 is not None else None
|
| 624 |
+
|
| 625 |
+
# parallelized over splits
|
| 626 |
+
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), NS * HQ)
|
| 627 |
+
chunk_bwd_kernel_dh_split[grid](
|
| 628 |
+
q=q,
|
| 629 |
+
g=g,
|
| 630 |
+
gk=gk,
|
| 631 |
+
gv=gv,
|
| 632 |
+
do=do,
|
| 633 |
+
dht=dht,
|
| 634 |
+
dhs=dhs,
|
| 635 |
+
dhr=dhr,
|
| 636 |
+
dh0=dh0,
|
| 637 |
+
offsets=offsets,
|
| 638 |
+
split_indices=split_indices,
|
| 639 |
+
scale=scale,
|
| 640 |
+
T=T,
|
| 641 |
+
S=S,
|
| 642 |
+
HQ=HQ,
|
| 643 |
+
H=H,
|
| 644 |
+
K=K,
|
| 645 |
+
V=V,
|
| 646 |
+
BT=BT,
|
| 647 |
+
NG=NG,
|
| 648 |
+
USE_G=g is not None,
|
| 649 |
+
USE_GK=gk is not None,
|
| 650 |
+
USE_GV=gv is not None,
|
| 651 |
+
HEAD_FIRST=head_first,
|
| 652 |
+
)
|
| 653 |
+
|
| 654 |
+
def grid(meta): return (triton.cdiv(K, meta['BK']), triton.cdiv(V, meta['BV']), N * HQ)
|
| 655 |
+
chunk_bwd_kernel_dh_reduction[grid](
|
| 656 |
+
g=g,
|
| 657 |
+
gk=gk,
|
| 658 |
+
gv=gv,
|
| 659 |
+
dhs=dhs,
|
| 660 |
+
dhr=dhr,
|
| 661 |
+
dh0=dh0,
|
| 662 |
+
offsets=offsets,
|
| 663 |
+
split_offsets=split_offsets,
|
| 664 |
+
T=T,
|
| 665 |
+
S=S,
|
| 666 |
+
HQ=HQ,
|
| 667 |
+
H=H,
|
| 668 |
+
K=K,
|
| 669 |
+
V=V,
|
| 670 |
+
BT=BT,
|
| 671 |
+
NG=NG,
|
| 672 |
+
USE_G=g is not None,
|
| 673 |
+
USE_GK=gk is not None,
|
| 674 |
+
USE_GV=gv is not None,
|
| 675 |
+
HEAD_FIRST=head_first
|
| 676 |
+
)
|
| 677 |
+
return dhr, dh0
|
fla/ops/delta_rule/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (361 Bytes). View file
|
|
|
fla/ops/delta_rule/__pycache__/chunk.cpython-312.pyc
ADDED
|
Binary file (13.3 kB). View file
|
|
|
fla/ops/delta_rule/__pycache__/wy_fast.cpython-312.pyc
ADDED
|
Binary file (20.5 kB). View file
|
|
|
fla/ops/delta_rule/fused_recurrent.py
ADDED
|
@@ -0,0 +1,607 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from typing import Optional, Tuple
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import triton
|
| 8 |
+
import triton.language as tl
|
| 9 |
+
from einops import rearrange
|
| 10 |
+
|
| 11 |
+
from fla.modules.l2norm import l2norm_bwd, l2norm_fwd
|
| 12 |
+
from fla.utils import input_guard
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@triton.heuristics({
|
| 16 |
+
'USE_INITIAL_STATE': lambda args: args['h0'] is not None,
|
| 17 |
+
'STORE_FINAL_STATE': lambda args: args['ht'] is not None,
|
| 18 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 19 |
+
})
|
| 20 |
+
@triton.jit(do_not_specialize=['T'])
|
| 21 |
+
def fused_recurrent_delta_rule_fwd_kernel(
|
| 22 |
+
q,
|
| 23 |
+
k,
|
| 24 |
+
v,
|
| 25 |
+
u,
|
| 26 |
+
beta,
|
| 27 |
+
o,
|
| 28 |
+
h0,
|
| 29 |
+
ht,
|
| 30 |
+
offsets,
|
| 31 |
+
scale,
|
| 32 |
+
T,
|
| 33 |
+
B: tl.constexpr,
|
| 34 |
+
H: tl.constexpr,
|
| 35 |
+
K: tl.constexpr,
|
| 36 |
+
V: tl.constexpr,
|
| 37 |
+
BK: tl.constexpr,
|
| 38 |
+
BV: tl.constexpr,
|
| 39 |
+
USE_INITIAL_STATE: tl.constexpr,
|
| 40 |
+
STORE_FINAL_STATE: tl.constexpr,
|
| 41 |
+
IS_BETA_HEADWISE: tl.constexpr,
|
| 42 |
+
USE_OFFSETS: tl.constexpr,
|
| 43 |
+
HEAD_FIRST: tl.constexpr
|
| 44 |
+
):
|
| 45 |
+
i_v, i_k, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 46 |
+
i_n, i_h = i_nh // H, i_nh % H
|
| 47 |
+
if USE_OFFSETS:
|
| 48 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int64), tl.load(offsets + i_n + 1).to(tl.int64)
|
| 49 |
+
all = T
|
| 50 |
+
T = eos - bos
|
| 51 |
+
else:
|
| 52 |
+
bos, eos = i_n * T, i_n * T + T
|
| 53 |
+
all = B * T
|
| 54 |
+
|
| 55 |
+
if HEAD_FIRST:
|
| 56 |
+
p_q = q + i_nh * T*K + i_k * BK + tl.arange(0, BK)
|
| 57 |
+
p_k = k + i_nh * T*K + i_k * BK + tl.arange(0, BK)
|
| 58 |
+
p_v = v + i_nh * T*V + i_v * BV + tl.arange(0, BV)
|
| 59 |
+
p_u = u + i_nh * T*V + i_v * BV + tl.arange(0, BV)
|
| 60 |
+
if IS_BETA_HEADWISE:
|
| 61 |
+
p_beta = beta + i_nh * T*V + i_v * BV + tl.arange(0, BV)
|
| 62 |
+
else:
|
| 63 |
+
p_beta = beta + i_nh * T
|
| 64 |
+
p_o = o + (i_k * B*H + i_nh) * T*V + i_v * BV + tl.arange(0, BV)
|
| 65 |
+
else:
|
| 66 |
+
p_q = q + (bos * H + i_h) * K + i_k * BK + tl.arange(0, BK)
|
| 67 |
+
p_k = k + (bos * H + i_h) * K + i_k * BK + tl.arange(0, BK)
|
| 68 |
+
p_v = v + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 69 |
+
p_u = u + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 70 |
+
if IS_BETA_HEADWISE:
|
| 71 |
+
p_beta = beta + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 72 |
+
else:
|
| 73 |
+
p_beta = beta + bos * H + i_h
|
| 74 |
+
p_o = o + ((i_k * all + bos) * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 75 |
+
|
| 76 |
+
mask_k = (i_k * BK + tl.arange(0, BK)) < K
|
| 77 |
+
mask_v = (i_v * BV + tl.arange(0, BV)) < V
|
| 78 |
+
mask_h = mask_k[None, :] & mask_v[:, None]
|
| 79 |
+
|
| 80 |
+
b_h = tl.zeros([BV, BK], dtype=tl.float32)
|
| 81 |
+
if USE_INITIAL_STATE:
|
| 82 |
+
p_h0 = h0 + i_nh * K * V + (i_k * BK + tl.arange(0, BK)[None, :]) * V + (i_v * BV + tl.arange(0, BV)[:, None])
|
| 83 |
+
b_h += tl.load(p_h0, mask=mask_h, other=0).to(tl.float32)
|
| 84 |
+
|
| 85 |
+
for _ in range(0, T):
|
| 86 |
+
b_k = tl.load(p_k, mask=mask_k, other=0).to(tl.float32)
|
| 87 |
+
b_v = tl.load(p_v, mask=mask_v, other=0).to(tl.float32)
|
| 88 |
+
b_q = tl.load(p_q, mask=mask_k, other=0).to(tl.float32) * scale
|
| 89 |
+
b_v_minus = tl.sum(b_h * b_k[None, :], axis=1)
|
| 90 |
+
b_v -= b_v_minus
|
| 91 |
+
if IS_BETA_HEADWISE:
|
| 92 |
+
b_beta = tl.load(p_beta, mask=mask_v, other=0).to(tl.float32)
|
| 93 |
+
else:
|
| 94 |
+
b_beta = tl.load(p_beta).to(tl.float32)
|
| 95 |
+
tl.store(p_u, b_v.to(p_v.dtype.element_ty), mask=mask_v)
|
| 96 |
+
b_v *= b_beta
|
| 97 |
+
b_h += b_k[None, :] * b_v[:, None]
|
| 98 |
+
b_o = b_h * b_q[None, :]
|
| 99 |
+
b_o = tl.sum(b_o, axis=1)
|
| 100 |
+
tl.store(p_o, b_o.to(p_o.dtype.element_ty), mask=mask_v)
|
| 101 |
+
|
| 102 |
+
p_q += K if HEAD_FIRST else H*K
|
| 103 |
+
p_k += K if HEAD_FIRST else H*K
|
| 104 |
+
p_o += V if HEAD_FIRST else H*V
|
| 105 |
+
p_v += V if HEAD_FIRST else H*V
|
| 106 |
+
p_u += V if HEAD_FIRST else H*V
|
| 107 |
+
p_beta += (1 if HEAD_FIRST else H) * (V if IS_BETA_HEADWISE else 1)
|
| 108 |
+
|
| 109 |
+
if STORE_FINAL_STATE:
|
| 110 |
+
p_ht = ht + i_nh * K * V + (i_k * BK + tl.arange(0, BK)[None, :]) * V + (i_v * BV + tl.arange(0, BV)[:, None])
|
| 111 |
+
tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), mask=mask_h)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@triton.heuristics({
|
| 115 |
+
'USE_INITIAL_STATE': lambda args: args['h0'] is not None,
|
| 116 |
+
'USE_FINAL_STATE_GRADIENT': lambda args: args['dht'] is not None,
|
| 117 |
+
'USE_OFFSETS': lambda args: args['offsets'] is not None
|
| 118 |
+
})
|
| 119 |
+
@triton.jit(do_not_specialize=['T'])
|
| 120 |
+
def fused_recurrent_delta_rule_bwd_kernel(
|
| 121 |
+
q,
|
| 122 |
+
k,
|
| 123 |
+
v,
|
| 124 |
+
beta,
|
| 125 |
+
h0,
|
| 126 |
+
dh0,
|
| 127 |
+
dht,
|
| 128 |
+
do,
|
| 129 |
+
dq,
|
| 130 |
+
dk,
|
| 131 |
+
dv,
|
| 132 |
+
db,
|
| 133 |
+
offsets,
|
| 134 |
+
scale,
|
| 135 |
+
B: tl.constexpr,
|
| 136 |
+
T,
|
| 137 |
+
H: tl.constexpr,
|
| 138 |
+
K: tl.constexpr,
|
| 139 |
+
V: tl.constexpr,
|
| 140 |
+
BK: tl.constexpr,
|
| 141 |
+
BV: tl.constexpr,
|
| 142 |
+
NK: tl.constexpr,
|
| 143 |
+
IS_BETA_HEADWISE: tl.constexpr, # whether beta is headwise vector or scalar
|
| 144 |
+
USE_INITIAL_STATE: tl.constexpr, # whether to use dh0
|
| 145 |
+
USE_FINAL_STATE_GRADIENT: tl.constexpr, # whether to use dht
|
| 146 |
+
USE_OFFSETS: tl.constexpr,
|
| 147 |
+
HEAD_FIRST: tl.constexpr
|
| 148 |
+
):
|
| 149 |
+
i_v, i_k, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
| 150 |
+
i_n, i_h = i_nh // H, i_nh % H
|
| 151 |
+
if USE_OFFSETS:
|
| 152 |
+
bos, eos = tl.load(offsets + i_n).to(tl.int64), tl.load(offsets + i_n + 1).to(tl.int64)
|
| 153 |
+
all = T
|
| 154 |
+
T = eos - bos
|
| 155 |
+
else:
|
| 156 |
+
bos, eos = i_n * T, i_n * T + T
|
| 157 |
+
all = B * T
|
| 158 |
+
|
| 159 |
+
mask_k = i_k * BK + tl.arange(0, BK) < K
|
| 160 |
+
mask_v = i_v * BV + tl.arange(0, BV) < V
|
| 161 |
+
|
| 162 |
+
if HEAD_FIRST:
|
| 163 |
+
p_q = q + i_nh * T*K + i_k * BK + tl.arange(0, BK) + (T - 1) * K
|
| 164 |
+
p_k = k + i_nh * T*K + i_k * BK + tl.arange(0, BK) + (T - 1) * K
|
| 165 |
+
p_v = v + i_nh * T*V + i_v * BV + tl.arange(0, BV) + (T - 1) * V
|
| 166 |
+
p_do = do + i_nh * T*V + i_v * BV + tl.arange(0, BV) + (T - 1) * V
|
| 167 |
+
p_dk = dk + (i_v * B*H + i_nh) * T*K + i_k * BK + tl.arange(0, BK) + (T - 1) * K
|
| 168 |
+
p_dv = dv + (i_k * B*H + i_nh) * T*V + i_v * BV + tl.arange(0, BV) + (T - 1) * V
|
| 169 |
+
if IS_BETA_HEADWISE:
|
| 170 |
+
p_beta = beta + i_nh * T*V + i_v * BV + tl.arange(0, BV) + (T - 1) * V
|
| 171 |
+
p_dbeta = db + (i_v * NK*B*H + i_k * B*H + i_nh) * T*V + tl.arange(0, BV) + (T - 1) * V
|
| 172 |
+
else:
|
| 173 |
+
p_beta = beta + i_nh * T + T - 1
|
| 174 |
+
p_dbeta = db + (i_v * B*H + i_nh) * T + T - 1
|
| 175 |
+
else:
|
| 176 |
+
p_q = q + (bos * H + i_h) * K + i_k * BK + tl.arange(0, BK) + (T - 1) * H*K
|
| 177 |
+
p_k = k + (bos * H + i_h) * K + i_k * BK + tl.arange(0, BK) + (T - 1) * H*K
|
| 178 |
+
p_v = v + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV) + (T - 1) * H*V
|
| 179 |
+
p_do = do + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV) + (T - 1) * H*V
|
| 180 |
+
p_dk = dk + ((i_v * all + bos) * H + i_h) * K + i_k * BK + tl.arange(0, BK) + (T - 1) * H*K
|
| 181 |
+
p_dv = dv + ((i_k * all + bos) * H + i_h) * V + i_v * BV + tl.arange(0, BV) + (T - 1) * H*V
|
| 182 |
+
if IS_BETA_HEADWISE:
|
| 183 |
+
p_beta = beta + (bos + T - 1) * H*V + i_h * V + i_v * BV + tl.arange(0, BV)
|
| 184 |
+
p_dbeta = db + ((i_v * NK + i_k) * all + bos + T - 1) * H*V + i_h * V + tl.arange(0, BV)
|
| 185 |
+
else:
|
| 186 |
+
p_beta = beta + (bos + T - 1) * H + i_h
|
| 187 |
+
p_dbeta = db + (i_v * all + bos + T - 1) * H + i_h
|
| 188 |
+
|
| 189 |
+
b_dh = tl.zeros([BK, BV], dtype=tl.float32)
|
| 190 |
+
if USE_FINAL_STATE_GRADIENT:
|
| 191 |
+
p_ht = dht + i_nh * K * V + (i_k * BK + tl.arange(0, BK)[:, None]) * V + (i_v * BV + tl.arange(0, BV)[None, :])
|
| 192 |
+
b_dh += tl.load(p_ht, mask=mask_k[:, None] & mask_v[None, :], other=0).to(tl.float32)
|
| 193 |
+
|
| 194 |
+
for _ in range(T):
|
| 195 |
+
b_q = tl.load(p_q, mask=mask_k, other=0).to(tl.float32) * scale
|
| 196 |
+
b_k = tl.load(p_k, mask=mask_k, other=0).to(tl.float32)
|
| 197 |
+
b_v = tl.load(p_v, mask=mask_v, other=0).to(tl.float32)
|
| 198 |
+
b_do = tl.load(p_do, mask=mask_v, other=0).to(tl.float32)
|
| 199 |
+
if IS_BETA_HEADWISE:
|
| 200 |
+
b_beta = tl.load(p_beta, mask=mask_v, other=0).to(tl.float32)
|
| 201 |
+
else:
|
| 202 |
+
b_beta = tl.load(p_beta).to(tl.float32)
|
| 203 |
+
b_dh += b_q[:, None] * b_do[None, :]
|
| 204 |
+
b_dk = tl.sum(b_dh * (b_v * b_beta)[None, :], axis=1)
|
| 205 |
+
b_dv = tl.sum(b_dh * b_k[:, None], axis=0)
|
| 206 |
+
|
| 207 |
+
b_db = b_dv * b_v if IS_BETA_HEADWISE else tl.sum(b_dv * b_v)
|
| 208 |
+
b_dv = b_dv * b_beta
|
| 209 |
+
|
| 210 |
+
tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), mask=mask_k)
|
| 211 |
+
tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), mask=mask_v)
|
| 212 |
+
if IS_BETA_HEADWISE:
|
| 213 |
+
tl.store(p_dbeta, b_db.to(p_dbeta.dtype.element_ty), mask=mask_v)
|
| 214 |
+
else:
|
| 215 |
+
tl.store(p_dbeta, b_db.to(p_dbeta.dtype.element_ty))
|
| 216 |
+
|
| 217 |
+
b_dh -= b_k[:, None] * b_dv[None, :]
|
| 218 |
+
|
| 219 |
+
p_q -= K if HEAD_FIRST else H*K
|
| 220 |
+
p_k -= K if HEAD_FIRST else H*K
|
| 221 |
+
p_v -= V if HEAD_FIRST else H*V
|
| 222 |
+
p_do -= V if HEAD_FIRST else H*V
|
| 223 |
+
p_dk -= K if HEAD_FIRST else H*K
|
| 224 |
+
p_dv -= V if HEAD_FIRST else H*V
|
| 225 |
+
p_dbeta -= (1 if HEAD_FIRST else H) * (V if IS_BETA_HEADWISE else 1)
|
| 226 |
+
p_beta -= (1 if HEAD_FIRST else H) * (V if IS_BETA_HEADWISE else 1)
|
| 227 |
+
|
| 228 |
+
if USE_INITIAL_STATE:
|
| 229 |
+
p_dh0 = dh0 + i_nh * K * V + (i_k * BK + tl.arange(0, BK)[:, None]) * V + (i_v * BV + tl.arange(0, BV)[None, :])
|
| 230 |
+
tl.store(p_dh0, b_dh.to(p_dh0.dtype.element_ty), mask=mask_k[:, None] & mask_v[None, :])
|
| 231 |
+
|
| 232 |
+
tl.debug_barrier()
|
| 233 |
+
|
| 234 |
+
b_h = tl.zeros([BK, BV], dtype=tl.float32)
|
| 235 |
+
|
| 236 |
+
if HEAD_FIRST:
|
| 237 |
+
p_q = q + i_nh * T*K + i_k * BK + tl.arange(0, BK)
|
| 238 |
+
p_k = k + i_nh * T*K + i_k * BK + tl.arange(0, BK)
|
| 239 |
+
p_v = v + i_nh * T*V + i_v * BV + tl.arange(0, BV)
|
| 240 |
+
if IS_BETA_HEADWISE:
|
| 241 |
+
p_beta = beta + i_nh * T*V + i_v * BV + tl.arange(0, BV)
|
| 242 |
+
else:
|
| 243 |
+
p_beta = beta + i_nh * T
|
| 244 |
+
p_do = do + i_nh * T*V + i_v * BV + tl.arange(0, BV)
|
| 245 |
+
p_dq = dq + (i_v * B*H + i_nh) * T*K + i_k * BK + tl.arange(0, BK)
|
| 246 |
+
p_dk = dk + (i_v * B*H + i_nh) * T*K + i_k * BK + tl.arange(0, BK)
|
| 247 |
+
p_dv = dv + (i_k * B*H + i_nh) * T*V + i_v * BV + tl.arange(0, BV)
|
| 248 |
+
else:
|
| 249 |
+
p_q = q + (bos * H + i_h) * K + i_k * BK + tl.arange(0, BK)
|
| 250 |
+
p_k = k + (bos * H + i_h) * K + i_k * BK + tl.arange(0, BK)
|
| 251 |
+
p_v = v + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 252 |
+
if IS_BETA_HEADWISE:
|
| 253 |
+
p_beta = beta + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 254 |
+
else:
|
| 255 |
+
p_beta = beta + bos * H + i_h
|
| 256 |
+
p_do = do + (bos * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 257 |
+
p_dq = dq + ((i_v * all + bos) * H + i_h) * K + i_k * BK + tl.arange(0, BK)
|
| 258 |
+
p_dk = dk + ((i_v * all + bos) * H + i_h) * K + i_k * BK + tl.arange(0, BK)
|
| 259 |
+
p_dv = dv + ((i_k * all + bos) * H + i_h) * V + i_v * BV + tl.arange(0, BV)
|
| 260 |
+
|
| 261 |
+
if USE_INITIAL_STATE:
|
| 262 |
+
mask_h = mask_k[:, None] & mask_v[None, :]
|
| 263 |
+
p_h0 = h0 + i_nh * K * V + (i_k * BK + tl.arange(0, BK)[:, None]) * V + (i_v * BV + tl.arange(0, BV)[None, :])
|
| 264 |
+
b_h += tl.load(p_h0, mask=mask_h, other=0).to(tl.float32)
|
| 265 |
+
|
| 266 |
+
for _ in range(0, T):
|
| 267 |
+
b_dk = tl.load(p_dk, mask=mask_k, other=0).to(tl.float32)
|
| 268 |
+
b_dv = tl.load(p_dv, mask=mask_v, other=0).to(tl.float32)
|
| 269 |
+
b_dk -= tl.sum(b_dv[None, :] * b_h, axis=1)
|
| 270 |
+
tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), mask=mask_k)
|
| 271 |
+
|
| 272 |
+
b_k = tl.load(p_k, mask=mask_k, other=0).to(tl.float32)
|
| 273 |
+
b_v = tl.load(p_v, mask=mask_v, other=0).to(tl.float32)
|
| 274 |
+
b_do = tl.load(p_do, mask=mask_v, other=0).to(tl.float32)
|
| 275 |
+
if IS_BETA_HEADWISE:
|
| 276 |
+
b_beta = tl.load(p_beta, mask=mask_v, other=0).to(tl.float32)
|
| 277 |
+
else:
|
| 278 |
+
b_beta = tl.load(p_beta).to(tl.float32)
|
| 279 |
+
b_v *= b_beta
|
| 280 |
+
|
| 281 |
+
b_h += b_k[:, None] * b_v[None, :]
|
| 282 |
+
b_dq = b_h * b_do[None, :]
|
| 283 |
+
d_q = tl.sum(b_dq, axis=1) * scale
|
| 284 |
+
tl.store(p_dq, d_q.to(p_dq.dtype.element_ty), mask=mask_k)
|
| 285 |
+
|
| 286 |
+
p_k += K if HEAD_FIRST else H*K
|
| 287 |
+
p_v += V if HEAD_FIRST else H*V
|
| 288 |
+
p_do += V if HEAD_FIRST else H*V
|
| 289 |
+
p_dq += K if HEAD_FIRST else H*K
|
| 290 |
+
p_dk += K if HEAD_FIRST else H*K
|
| 291 |
+
p_dv += V if HEAD_FIRST else H*V
|
| 292 |
+
p_beta += (1 if HEAD_FIRST else H) * (V if IS_BETA_HEADWISE else 1)
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
def fused_recurrent_delta_rule_fwd(
|
| 296 |
+
q: torch.Tensor,
|
| 297 |
+
k: torch.Tensor,
|
| 298 |
+
v: torch.Tensor,
|
| 299 |
+
beta: torch.Tensor,
|
| 300 |
+
scale: float,
|
| 301 |
+
initial_state: torch.Tensor,
|
| 302 |
+
output_final_state: bool,
|
| 303 |
+
offsets: Optional[torch.LongTensor] = None,
|
| 304 |
+
head_first: bool = True
|
| 305 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 306 |
+
if head_first:
|
| 307 |
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
| 308 |
+
else:
|
| 309 |
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
| 310 |
+
N = B if offsets is None else len(offsets) - 1
|
| 311 |
+
BK, BV = triton.next_power_of_2(K), min(triton.next_power_of_2(V), 8)
|
| 312 |
+
NK, NV = triton.cdiv(K, BK), triton.cdiv(V, BV)
|
| 313 |
+
assert NK == 1, "NK > 1 is not supported yet"
|
| 314 |
+
num_stages = 1
|
| 315 |
+
num_warps = 1
|
| 316 |
+
|
| 317 |
+
o = q.new_empty(NK, *v.shape)
|
| 318 |
+
if output_final_state:
|
| 319 |
+
final_state = q.new_empty(N, H, K, V, dtype=torch.float32)
|
| 320 |
+
else:
|
| 321 |
+
final_state = None
|
| 322 |
+
|
| 323 |
+
grid = (NV, NK, N * H)
|
| 324 |
+
u = torch.empty_like(v)
|
| 325 |
+
fused_recurrent_delta_rule_fwd_kernel[grid](
|
| 326 |
+
q,
|
| 327 |
+
k,
|
| 328 |
+
v,
|
| 329 |
+
u,
|
| 330 |
+
beta,
|
| 331 |
+
o,
|
| 332 |
+
initial_state,
|
| 333 |
+
final_state,
|
| 334 |
+
offsets,
|
| 335 |
+
scale,
|
| 336 |
+
T=T,
|
| 337 |
+
B=B,
|
| 338 |
+
H=H,
|
| 339 |
+
K=K,
|
| 340 |
+
V=V,
|
| 341 |
+
BK=BK,
|
| 342 |
+
BV=BV,
|
| 343 |
+
IS_BETA_HEADWISE=beta.ndim == v.ndim,
|
| 344 |
+
HEAD_FIRST=head_first,
|
| 345 |
+
num_warps=num_warps,
|
| 346 |
+
num_stages=num_stages,
|
| 347 |
+
)
|
| 348 |
+
o = o.squeeze(0)
|
| 349 |
+
return o, u, final_state
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def fused_recurrent_delta_rule_bwd(
|
| 353 |
+
q: torch.Tensor,
|
| 354 |
+
k: torch.Tensor,
|
| 355 |
+
v: torch.Tensor,
|
| 356 |
+
beta: torch.Tensor,
|
| 357 |
+
dht: torch.Tensor,
|
| 358 |
+
do: torch.Tensor,
|
| 359 |
+
scale: float,
|
| 360 |
+
initial_state: torch.Tensor,
|
| 361 |
+
offsets: Optional[torch.LongTensor] = None,
|
| 362 |
+
head_first: bool = True
|
| 363 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
| 364 |
+
if head_first:
|
| 365 |
+
B, H, T, K, V = *k.shape, v.shape[-1]
|
| 366 |
+
else:
|
| 367 |
+
B, T, H, K, V = *k.shape, v.shape[-1]
|
| 368 |
+
N = B if offsets is None else len(offsets) - 1
|
| 369 |
+
BK, BV = triton.next_power_of_2(K), min(triton.next_power_of_2(V), 32)
|
| 370 |
+
NK, NV = triton.cdiv(K, BK), triton.cdiv(V, BV)
|
| 371 |
+
assert NK == 1, "NK > 1 is not supported yet"
|
| 372 |
+
num_stages = 1
|
| 373 |
+
num_warps = 2
|
| 374 |
+
|
| 375 |
+
beta_vector = beta.ndim == v.ndim
|
| 376 |
+
|
| 377 |
+
dq = q.new_empty(NV, *q.shape)
|
| 378 |
+
dk = q.new_empty(NV, *k.shape)
|
| 379 |
+
dv = q.new_empty(NK, *v.shape)
|
| 380 |
+
if beta_vector:
|
| 381 |
+
db = q.new_empty(NV, NK, B, H, T, V) if head_first else q.new_empty(NV, NK, B, T, H, V)
|
| 382 |
+
else:
|
| 383 |
+
db = q.new_empty(NV, B, H, T) if head_first else q.new_empty(NV, B, T, H)
|
| 384 |
+
grid = (NV, NK, N * H)
|
| 385 |
+
|
| 386 |
+
if initial_state is not None and initial_state.requires_grad:
|
| 387 |
+
dh0 = torch.empty_like(initial_state, dtype=torch.float32)
|
| 388 |
+
else:
|
| 389 |
+
dh0 = None
|
| 390 |
+
|
| 391 |
+
fused_recurrent_delta_rule_bwd_kernel[grid](
|
| 392 |
+
q,
|
| 393 |
+
k,
|
| 394 |
+
v,
|
| 395 |
+
beta,
|
| 396 |
+
initial_state,
|
| 397 |
+
dh0,
|
| 398 |
+
dht,
|
| 399 |
+
do,
|
| 400 |
+
dq,
|
| 401 |
+
dk,
|
| 402 |
+
dv,
|
| 403 |
+
db,
|
| 404 |
+
offsets,
|
| 405 |
+
scale,
|
| 406 |
+
T=T,
|
| 407 |
+
B=B,
|
| 408 |
+
H=H,
|
| 409 |
+
K=K,
|
| 410 |
+
V=V,
|
| 411 |
+
BK=BK,
|
| 412 |
+
BV=BV,
|
| 413 |
+
NK=NK,
|
| 414 |
+
IS_BETA_HEADWISE=beta_vector,
|
| 415 |
+
HEAD_FIRST=head_first,
|
| 416 |
+
num_warps=num_warps,
|
| 417 |
+
num_stages=num_stages
|
| 418 |
+
)
|
| 419 |
+
dq = dq.sum(0)
|
| 420 |
+
dk = dk.sum(0)
|
| 421 |
+
dv = dv.sum(0)
|
| 422 |
+
db = db.sum((0, 1)) if beta_vector else db.sum(0)
|
| 423 |
+
|
| 424 |
+
return dq, dk, dv, db, dh0
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
class FusedRecurrentFunction(torch.autograd.Function):
|
| 428 |
+
|
| 429 |
+
@staticmethod
|
| 430 |
+
@input_guard
|
| 431 |
+
def forward(
|
| 432 |
+
ctx,
|
| 433 |
+
q: torch.Tensor,
|
| 434 |
+
k: torch.Tensor,
|
| 435 |
+
v: torch.Tensor,
|
| 436 |
+
beta: torch.Tensor,
|
| 437 |
+
scale: float,
|
| 438 |
+
initial_state: torch.Tensor,
|
| 439 |
+
output_final_state: bool,
|
| 440 |
+
offsets: Optional[torch.LongTensor] = None,
|
| 441 |
+
head_first: bool = True,
|
| 442 |
+
use_qk_l2norm_in_kernel: bool = False
|
| 443 |
+
):
|
| 444 |
+
q_orig = q
|
| 445 |
+
k_orig = k
|
| 446 |
+
|
| 447 |
+
if use_qk_l2norm_in_kernel:
|
| 448 |
+
q = l2norm_fwd(q)
|
| 449 |
+
k = l2norm_fwd(k)
|
| 450 |
+
|
| 451 |
+
o, u, final_state = fused_recurrent_delta_rule_fwd(
|
| 452 |
+
q=q,
|
| 453 |
+
k=k,
|
| 454 |
+
v=v,
|
| 455 |
+
beta=beta,
|
| 456 |
+
scale=scale,
|
| 457 |
+
initial_state=initial_state,
|
| 458 |
+
output_final_state=output_final_state,
|
| 459 |
+
offsets=offsets,
|
| 460 |
+
head_first=head_first
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
ctx.save_for_backward(q_orig, k_orig, u, beta, initial_state)
|
| 464 |
+
ctx.scale = scale
|
| 465 |
+
ctx.offsets = offsets
|
| 466 |
+
ctx.head_first = head_first
|
| 467 |
+
ctx.use_qk_l2norm_in_kernel = use_qk_l2norm_in_kernel
|
| 468 |
+
return o, final_state
|
| 469 |
+
|
| 470 |
+
@staticmethod
|
| 471 |
+
@input_guard
|
| 472 |
+
def backward(ctx, do, dht):
|
| 473 |
+
q, k, v, beta, initial_state = ctx.saved_tensors
|
| 474 |
+
if ctx.use_qk_l2norm_in_kernel:
|
| 475 |
+
q, q_orig = l2norm_fwd(q), q
|
| 476 |
+
k, k_orig = l2norm_fwd(k), k
|
| 477 |
+
dq, dk, dv, db, dh0 = fused_recurrent_delta_rule_bwd(
|
| 478 |
+
q=q,
|
| 479 |
+
k=k,
|
| 480 |
+
v=v,
|
| 481 |
+
beta=beta,
|
| 482 |
+
dht=dht,
|
| 483 |
+
do=do,
|
| 484 |
+
scale=ctx.scale,
|
| 485 |
+
initial_state=initial_state,
|
| 486 |
+
offsets=ctx.offsets,
|
| 487 |
+
head_first=ctx.head_first
|
| 488 |
+
)
|
| 489 |
+
if ctx.use_qk_l2norm_in_kernel:
|
| 490 |
+
dq, dk = l2norm_bwd(q_orig, dq), l2norm_bwd(k_orig, dk)
|
| 491 |
+
return dq.to(q), dk.to(k), dv.to(v), db.to(beta), None, dh0, None, None, None, None
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
@torch.compiler.disable
|
| 495 |
+
def fused_recurrent_delta_rule(
|
| 496 |
+
q: torch.Tensor,
|
| 497 |
+
k: torch.Tensor,
|
| 498 |
+
v: torch.Tensor,
|
| 499 |
+
beta: torch.Tensor = None,
|
| 500 |
+
scale: float = None,
|
| 501 |
+
initial_state: torch.Tensor = None,
|
| 502 |
+
output_final_state: bool = False,
|
| 503 |
+
cu_seqlens: Optional[torch.LongTensor] = None,
|
| 504 |
+
head_first: bool = True,
|
| 505 |
+
use_qk_l2norm_in_kernel: bool = False
|
| 506 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 507 |
+
r"""
|
| 508 |
+
Args:
|
| 509 |
+
q (torch.Tensor):
|
| 510 |
+
queries of shape `[B, T, H, K]` if `head_first=False` else `[B, H, T, K]`.
|
| 511 |
+
k (torch.Tensor):
|
| 512 |
+
keys of shape `[B, T, H, K]` if `head_first=False` else `[B, H, T, K]`.
|
| 513 |
+
v (torch.Tensor):
|
| 514 |
+
values of shape `[B, T, H, V]` if `head_first=False` else `[B, H, T, V]`.
|
| 515 |
+
beta (torch.Tensor):
|
| 516 |
+
betas of shape `[B, T, H]` if `head_first=False` else `(B, H, T)`.
|
| 517 |
+
scale (Optional[int]):
|
| 518 |
+
Scale factor for the RetNet attention scores.
|
| 519 |
+
If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
|
| 520 |
+
initial_state (Optional[torch.Tensor]):
|
| 521 |
+
Initial state of shape `[N, H, K, V]` for `N` input sequences.
|
| 522 |
+
For equal-length input sequences, `N` equals the batch size `B`.
|
| 523 |
+
Default: `None`.
|
| 524 |
+
output_final_state (Optional[bool]):
|
| 525 |
+
Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
|
| 526 |
+
cu_seqlens (torch.LongTensor):
|
| 527 |
+
Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
|
| 528 |
+
consistent with the FlashAttention API.
|
| 529 |
+
head_first (Optional[bool]):
|
| 530 |
+
Whether the inputs are in the head-first format, which is not supported for variable-length inputs.
|
| 531 |
+
Default: `False`.
|
| 532 |
+
|
| 533 |
+
Returns:
|
| 534 |
+
o (torch.Tensor):
|
| 535 |
+
Outputs of shape `[B, T, H, V]` if `head_first=False` else `[B, H, T, V]`.
|
| 536 |
+
final_state (torch.Tensor):
|
| 537 |
+
Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.
|
| 538 |
+
|
| 539 |
+
Examples::
|
| 540 |
+
>>> import torch
|
| 541 |
+
>>> import torch.nn.functional as F
|
| 542 |
+
>>> from einops import rearrange
|
| 543 |
+
>>> from fla.ops.delta_rule import fused_recurrent_delta_rule
|
| 544 |
+
# inputs with equal lengths
|
| 545 |
+
>>> B, T, H, K, V = 4, 2048, 4, 512, 512
|
| 546 |
+
>>> q = torch.randn(B, T, H, K, device='cuda')
|
| 547 |
+
>>> k = F.normalize(torch.randn(B, T, H, K, device='cuda'), p=2, dim=-1)
|
| 548 |
+
>>> v = torch.randn(B, T, H, V, device='cuda')
|
| 549 |
+
>>> beta = torch.rand(B, T, H, device='cuda').sigmoid()
|
| 550 |
+
>>> h0 = torch.randn(B, H, K, V, device='cuda')
|
| 551 |
+
>>> o, ht = fused_recurrent_delta_rule(
|
| 552 |
+
q, k, v, beta,
|
| 553 |
+
initial_state=h0,
|
| 554 |
+
output_final_state=True
|
| 555 |
+
)
|
| 556 |
+
# for variable-length inputs, the batch size `B` is expected to be 1 and `cu_seqlens` is required
|
| 557 |
+
>>> q, k, v, beta = map(lambda x: rearrange(x, 'b t ... -> 1 (b t) ...'), (q, k, v, beta))
|
| 558 |
+
# for a batch with 4 sequences, `cu_seqlens` with 5 start/end positions are expected
|
| 559 |
+
>>> cu_seqlens = q.new_tensor([0, 2048, 4096, 6144, 8192], dtype=torch.long)
|
| 560 |
+
>>> o_var, ht_var = fused_recurrent_delta_rule(
|
| 561 |
+
q, k, v, beta,
|
| 562 |
+
initial_state=h0,
|
| 563 |
+
output_final_state=True,
|
| 564 |
+
cu_seqlens=cu_seqlens
|
| 565 |
+
)
|
| 566 |
+
>>> assert o.allclose(o_var.view(o.shape))
|
| 567 |
+
>>> assert ht.allclose(ht_var)
|
| 568 |
+
"""
|
| 569 |
+
if cu_seqlens is not None:
|
| 570 |
+
if q.shape[0] != 1:
|
| 571 |
+
raise ValueError(
|
| 572 |
+
f"The batch size is expected to be 1 rather than {q.shape[0]} when using `cu_seqlens`."
|
| 573 |
+
f"Please flatten variable-length inputs before processing."
|
| 574 |
+
)
|
| 575 |
+
if head_first:
|
| 576 |
+
raise RuntimeError(
|
| 577 |
+
"Sequences with variable lengths are not supported for head-first mode"
|
| 578 |
+
)
|
| 579 |
+
if initial_state is not None and initial_state.shape[0] != len(cu_seqlens) - 1:
|
| 580 |
+
raise ValueError(
|
| 581 |
+
f"The number of initial states is expected to be equal to the number of input sequences, "
|
| 582 |
+
f"i.e., {len(cu_seqlens) - 1} rather than {initial_state.shape[0]}."
|
| 583 |
+
)
|
| 584 |
+
if scale is None:
|
| 585 |
+
scale = k.shape[-1] ** -0.5
|
| 586 |
+
else:
|
| 587 |
+
assert scale > 0, "scale must be positive"
|
| 588 |
+
if beta is None:
|
| 589 |
+
beta = torch.ones_like(q[..., 0])
|
| 590 |
+
if head_first:
|
| 591 |
+
q, k, v = map(lambda x: rearrange(x, 'b h t d -> b t h d'), (q, k, v))
|
| 592 |
+
beta = rearrange(beta, 'b h t -> b t h')
|
| 593 |
+
o, final_state = FusedRecurrentFunction.apply(
|
| 594 |
+
q,
|
| 595 |
+
k,
|
| 596 |
+
v,
|
| 597 |
+
beta,
|
| 598 |
+
scale,
|
| 599 |
+
initial_state,
|
| 600 |
+
output_final_state,
|
| 601 |
+
cu_seqlens,
|
| 602 |
+
False,
|
| 603 |
+
use_qk_l2norm_in_kernel
|
| 604 |
+
)
|
| 605 |
+
if head_first:
|
| 606 |
+
o = rearrange(o, 'b t h v -> b h t v')
|
| 607 |
+
return o, final_state
|
fla/ops/forgetting_attn/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (242 Bytes). View file
|
|
|
fla/ops/forgetting_attn/__pycache__/parallel.cpython-312.pyc
ADDED
|
Binary file (39 kB). View file
|
|
|