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/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/layers/__pycache__/hgrn.cpython-312.pyc +0 -0
- fla/layers/abc.py +218 -0
- fla/layers/based.py +96 -0
- fla/layers/forgetting_attn.py +109 -0
- fla/layers/gsa.py +227 -0
- fla/layers/hgrn2.py +211 -0
- fla/layers/linear_attn.py +166 -0
- fla/layers/multiscale_retention.py +298 -0
- fla/layers/nsa.py +138 -0
- fla/layers/rebased.py +133 -0
- fla/layers/rwkv6.py +307 -0
- fla/layers/simple_gla.py +261 -0
- fla/models/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/abc/__init__.py +13 -0
- fla/models/abc/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/abc/__pycache__/configuration_abc.cpython-312.pyc +0 -0
- fla/models/abc/__pycache__/modeling_abc.cpython-312.pyc +0 -0
- fla/models/abc/configuration_abc.py +91 -0
- fla/models/bitnet/__init__.py +13 -0
- fla/models/bitnet/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/bitnet/__pycache__/configuration_bitnet.cpython-312.pyc +0 -0
- fla/models/bitnet/configuration_bitnet.py +67 -0
- fla/models/delta_net/__init__.py +12 -0
- fla/models/delta_net/configuration_delta_net.py +91 -0
- fla/models/forgetting_transformer/__init__.py +16 -0
- fla/models/forgetting_transformer/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/forgetting_transformer/__pycache__/modeling_forgetting_transformer.cpython-312.pyc +0 -0
- fla/models/forgetting_transformer/configuration_forgetting_transformer.py +68 -0
- fla/models/forgetting_transformer/modeling_forgetting_transformer.py +408 -0
- fla/models/gated_deltanet/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/gated_deltanet/__pycache__/modeling_gated_deltanet.cpython-312.pyc +0 -0
- fla/models/gated_deltanet/modeling_gated_deltanet.py +412 -0
- fla/models/gated_deltaproduct/__init__.py +14 -0
- fla/models/gated_deltaproduct/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/gated_deltaproduct/__pycache__/configuration_gated_deltaproduct.cpython-312.pyc +0 -0
- fla/models/gated_deltaproduct/__pycache__/modeling_gated_deltaproduct.cpython-312.pyc +0 -0
- fla/models/gated_deltaproduct/modeling_gated_deltaproduct.py +520 -0
- fla/models/gla/__init__.py +13 -0
- fla/models/gla/__pycache__/configuration_gla.cpython-312.pyc +0 -0
- fla/models/gla/__pycache__/modeling_gla.cpython-312.pyc +0 -0
- fla/models/gla/modeling_gla.py +417 -0
- fla/models/gsa/__init__.py +13 -0
- fla/models/gsa/__pycache__/__init__.cpython-312.pyc +0 -0
- fla/models/gsa/modeling_gsa.py +420 -0
- fla/models/hgrn/__init__.py +13 -0
- fla/models/hgrn/configuration_hgrn.py +81 -0
- fla/models/hgrn/modeling_hgrn.py +420 -0
- fla/models/hgrn2/__init__.py +13 -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_ewbp5xc1/attempt_0/0/stderr.log filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
tb/20250901-0749/wandb/run-20250901_074914-top_transformer-top.code.1B.batch16.seqlen4096.context4096.warmup400.update1.steps40000.lr5e-5.cosine-202509010747/run-top_transformer-top.code.1B.batch16.seqlen4096.context4096.warmup400.update1.steps40000.lr5e-5.cosine-202509010747.wandb filter=lfs diff=lfs merge=lfs -text
|
fla/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (1.89 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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 |
+
from einops import rearrange
|
| 12 |
+
|
| 13 |
+
from fla.modules import FusedRMSNormGated, RMSNorm, RotaryEmbedding, ShortConvolution
|
| 14 |
+
from fla.modules.activations import swiglu, swish
|
| 15 |
+
from fla.ops.abc.chunk import chunk_abc
|
| 16 |
+
|
| 17 |
+
if TYPE_CHECKING:
|
| 18 |
+
from fla.models.utils import Cache
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class ABCAttention(nn.Module):
|
| 22 |
+
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
hidden_size: int = 1024,
|
| 26 |
+
expand_k: float = 0.5,
|
| 27 |
+
expand_v: float = 1.0,
|
| 28 |
+
num_heads: int = 4,
|
| 29 |
+
use_short_conv: bool = False,
|
| 30 |
+
conv_size: int = 4,
|
| 31 |
+
conv_bias: bool = False,
|
| 32 |
+
num_slots: Optional[int] = None,
|
| 33 |
+
elementwise_affine: Optional[bool] = True,
|
| 34 |
+
norm_eps: float = 1e-5,
|
| 35 |
+
gate_low_rank_dim: int = 16,
|
| 36 |
+
gate_logit_normalizer: int = 16,
|
| 37 |
+
use_rope: bool = True,
|
| 38 |
+
use_input_gate: bool = False,
|
| 39 |
+
use_output_gate: bool = True,
|
| 40 |
+
use_norm: bool = True,
|
| 41 |
+
clamp_min: Optional[float] = -32,
|
| 42 |
+
clamp_max: Optional[float] = 32,
|
| 43 |
+
layer_idx: Optional[int] = None,
|
| 44 |
+
**kwargs
|
| 45 |
+
) -> ABCAttention:
|
| 46 |
+
super().__init__()
|
| 47 |
+
|
| 48 |
+
self.hidden_size = hidden_size
|
| 49 |
+
self.expand_k = expand_k
|
| 50 |
+
self.expand_v = expand_v
|
| 51 |
+
self.num_heads = num_heads
|
| 52 |
+
self.key_dim = int(self.hidden_size * self.expand_k)
|
| 53 |
+
self.value_dim = int(self.hidden_size * self.expand_v)
|
| 54 |
+
self.head_k_dim = self.key_dim // self.num_heads
|
| 55 |
+
self.head_v_dim = self.value_dim // self.num_heads
|
| 56 |
+
|
| 57 |
+
self.use_short_conv = use_short_conv
|
| 58 |
+
self.conv_size = conv_size
|
| 59 |
+
self.conv_bias = conv_bias
|
| 60 |
+
|
| 61 |
+
self.gate_low_rank_dim = gate_low_rank_dim
|
| 62 |
+
self.gate_logit_normalizer = gate_logit_normalizer
|
| 63 |
+
|
| 64 |
+
self.use_rope = use_rope
|
| 65 |
+
self.use_input_gate = use_input_gate
|
| 66 |
+
self.use_output_gate = use_output_gate
|
| 67 |
+
self.use_norm = use_norm
|
| 68 |
+
|
| 69 |
+
if num_slots is None:
|
| 70 |
+
num_slots = self.head_k_dim
|
| 71 |
+
self.num_slots = num_slots
|
| 72 |
+
|
| 73 |
+
self.norm_eps = norm_eps
|
| 74 |
+
|
| 75 |
+
self.clamp_min = clamp_min
|
| 76 |
+
self.clamp_max = clamp_max
|
| 77 |
+
self.layer_idx = layer_idx
|
| 78 |
+
|
| 79 |
+
if layer_idx is None:
|
| 80 |
+
warnings.warn(
|
| 81 |
+
f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
|
| 82 |
+
"to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
|
| 83 |
+
"when creating this class."
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
self.q_proj = nn.Linear(self.hidden_size, self.key_dim, bias=False)
|
| 87 |
+
self.k_proj = nn.Linear(self.hidden_size, self.key_dim, bias=False)
|
| 88 |
+
self.v_proj = nn.Linear(self.hidden_size, self.value_dim, bias=False)
|
| 89 |
+
|
| 90 |
+
if use_output_gate:
|
| 91 |
+
self.g_proj = nn.Linear(self.hidden_size, self.value_dim, bias=False)
|
| 92 |
+
self.s_proj = nn.Linear(self.hidden_size, self.num_heads * self.num_slots, bias=False)
|
| 93 |
+
self.o_proj = nn.Linear(self.value_dim, self.hidden_size, bias=False)
|
| 94 |
+
|
| 95 |
+
if use_short_conv:
|
| 96 |
+
self.conv_size = conv_size
|
| 97 |
+
self.q_conv1d = ShortConvolution(self.key_dim, conv_size, activation='silu')
|
| 98 |
+
self.k_conv1d = ShortConvolution(self.key_dim, conv_size, activation='silu')
|
| 99 |
+
self.v_conv1d = ShortConvolution(self.value_dim, conv_size, activation='silu')
|
| 100 |
+
|
| 101 |
+
if self.use_norm:
|
| 102 |
+
if self.use_output_gate:
|
| 103 |
+
self.g_norm = FusedRMSNormGated(
|
| 104 |
+
hidden_size=self.head_v_dim,
|
| 105 |
+
elementwise_affine=elementwise_affine,
|
| 106 |
+
eps=norm_eps
|
| 107 |
+
)
|
| 108 |
+
else:
|
| 109 |
+
self.g_norm = RMSNorm(
|
| 110 |
+
hidden_size=self.head_v_dim,
|
| 111 |
+
elementwise_affine=elementwise_affine,
|
| 112 |
+
eps=norm_eps
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
if self.use_rope:
|
| 116 |
+
self.rotary = RotaryEmbedding(self.head_k_dim)
|
| 117 |
+
|
| 118 |
+
def forward(
|
| 119 |
+
self,
|
| 120 |
+
hidden_states: torch.Tensor,
|
| 121 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 122 |
+
past_key_values: Optional[Cache] = None,
|
| 123 |
+
use_cache: Optional[bool] = False,
|
| 124 |
+
output_attentions: Optional[bool] = False,
|
| 125 |
+
**kwargs
|
| 126 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]:
|
| 127 |
+
if attention_mask is not None:
|
| 128 |
+
assert len(attention_mask.shape) == 2, (
|
| 129 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 130 |
+
"for padding purposes (0 indicating padding). "
|
| 131 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
last_state = None
|
| 135 |
+
if past_key_values is not None and len(past_key_values) > self.layer_idx:
|
| 136 |
+
last_state = past_key_values[self.layer_idx]
|
| 137 |
+
|
| 138 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 139 |
+
if cu_seqlens is not None:
|
| 140 |
+
raise NotImplementedError("Training with cu_seqlens is not supported yet for ABCAttention")
|
| 141 |
+
if self.use_short_conv:
|
| 142 |
+
conv_state_q, conv_state_k, conv_state_v = None, None, None
|
| 143 |
+
if last_state is not None:
|
| 144 |
+
conv_state_q, conv_state_k, conv_state_v = last_state['conv_state']
|
| 145 |
+
conv_mask = attention_mask[:, -hidden_states.shape[1]:] if attention_mask is not None else None
|
| 146 |
+
q, conv_state_q = self.q_conv1d(
|
| 147 |
+
x=self.q_proj(hidden_states),
|
| 148 |
+
mask=conv_mask,
|
| 149 |
+
cache=conv_state_q,
|
| 150 |
+
output_final_state=use_cache,
|
| 151 |
+
cu_seqlens=cu_seqlens
|
| 152 |
+
)
|
| 153 |
+
k, conv_state_k = self.k_conv1d(
|
| 154 |
+
x=self.k_proj(hidden_states),
|
| 155 |
+
mask=conv_mask,
|
| 156 |
+
cache=conv_state_k,
|
| 157 |
+
output_final_state=use_cache,
|
| 158 |
+
cu_seqlens=cu_seqlens
|
| 159 |
+
)
|
| 160 |
+
v, conv_state_v = self.v_conv1d(
|
| 161 |
+
x=self.v_proj(hidden_states),
|
| 162 |
+
mask=conv_mask,
|
| 163 |
+
cache=conv_state_v,
|
| 164 |
+
output_final_state=use_cache,
|
| 165 |
+
cu_seqlens=cu_seqlens
|
| 166 |
+
)
|
| 167 |
+
else:
|
| 168 |
+
q = self.q_proj(hidden_states)
|
| 169 |
+
k = self.k_proj(hidden_states)
|
| 170 |
+
v = self.v_proj(hidden_states)
|
| 171 |
+
|
| 172 |
+
if self.use_input_gate:
|
| 173 |
+
q, k, v = map(lambda x: swish(x), (q, k, v))
|
| 174 |
+
# dealing with left-padding
|
| 175 |
+
if attention_mask is not None:
|
| 176 |
+
v = v.mul_(attention_mask[:, -v.shape[-2]:, None])
|
| 177 |
+
|
| 178 |
+
q, k = map(lambda x: rearrange(x, '... (h d) -> ... h d', d=self.head_k_dim), (q, k))
|
| 179 |
+
v = rearrange(v, '... (h d) -> ... h d', d=self.head_v_dim)
|
| 180 |
+
if self.use_rope:
|
| 181 |
+
seqlen_offset = 0
|
| 182 |
+
if past_key_values is not None:
|
| 183 |
+
seqlen_offset = past_key_values.get_seq_length(self.layer_idx)
|
| 184 |
+
q, k = self.rotary(q, k, seqlen_offset=seqlen_offset)
|
| 185 |
+
|
| 186 |
+
s = rearrange(self.s_proj(hidden_states), '... (h m) -> ... h m', m=self.num_slots)
|
| 187 |
+
s = s.clamp_(self.clamp_min, self.clamp_max)
|
| 188 |
+
|
| 189 |
+
recurrent_state = last_state['recurrent_state'] if last_state is not None else None
|
| 190 |
+
o, recurrent_state = chunk_abc(
|
| 191 |
+
q=q,
|
| 192 |
+
k=k,
|
| 193 |
+
v=v,
|
| 194 |
+
s=s,
|
| 195 |
+
initial_state=recurrent_state,
|
| 196 |
+
output_final_state=use_cache,
|
| 197 |
+
head_first=False
|
| 198 |
+
)
|
| 199 |
+
if past_key_values is not None:
|
| 200 |
+
past_key_values.update(
|
| 201 |
+
recurrent_state=recurrent_state,
|
| 202 |
+
conv_state=(conv_state_q, conv_state_k, conv_state_v) if self.use_short_conv else None,
|
| 203 |
+
layer_idx=self.layer_idx,
|
| 204 |
+
offset=q.shape[1]
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
if self.use_norm and not self.use_output_gate:
|
| 208 |
+
o = self.g_norm(o)
|
| 209 |
+
elif self.use_output_gate:
|
| 210 |
+
g = rearrange(self.g_proj(hidden_states), '... (h d) -> ... h d', d=self.head_v_dim)
|
| 211 |
+
o = self.g_norm(o, g) if self.use_norm else swiglu(g, o)
|
| 212 |
+
o = rearrange(o, '... h d -> ... (h d)')
|
| 213 |
+
o = self.o_proj(o)
|
| 214 |
+
|
| 215 |
+
return o, None, past_key_values
|
| 216 |
+
|
| 217 |
+
def state_size(self, seq_len: int = 2048):
|
| 218 |
+
return 2 * self.num_slots * self.hidden_size
|
fla/layers/based.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
Linear attention in Based.
|
| 6 |
+
https://github.com/HazyResearch/zoology/blob/main/zoology/mixers/based.py
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
from einops import rearrange
|
| 12 |
+
|
| 13 |
+
from fla.modules.feature_map import TaylorFeatureMap
|
| 14 |
+
from fla.ops.based import parallel_based
|
| 15 |
+
from fla.ops.linear_attn import chunk_linear_attn, fused_chunk_linear_attn
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class BasedLinearAttention(nn.Module):
|
| 19 |
+
|
| 20 |
+
def __init__(
|
| 21 |
+
self,
|
| 22 |
+
hidden_size: int,
|
| 23 |
+
feature_dim: int = 16,
|
| 24 |
+
num_key_value_heads: int = 12,
|
| 25 |
+
num_heads: int = 12,
|
| 26 |
+
feature_name: str = "taylor_exp",
|
| 27 |
+
eps: float = 1e-12,
|
| 28 |
+
causal: bool = True,
|
| 29 |
+
mode: str = "parallel",
|
| 30 |
+
):
|
| 31 |
+
super().__init__()
|
| 32 |
+
|
| 33 |
+
self.hidden_size = hidden_size
|
| 34 |
+
self.mode = mode
|
| 35 |
+
self.feature_name = feature_name
|
| 36 |
+
self.feature_dim = feature_dim
|
| 37 |
+
self.num_key_value_heads = num_key_value_heads
|
| 38 |
+
self.num_heads = num_heads
|
| 39 |
+
self.head_dim = self.hidden_size // self.num_key_value_heads
|
| 40 |
+
assert self.hidden_size % self.head_dim == 0
|
| 41 |
+
self.causal = causal
|
| 42 |
+
|
| 43 |
+
self.q_proj = nn.Linear(self.hidden_size, self.feature_dim * self.num_heads, bias=False)
|
| 44 |
+
self.k_proj = nn.Linear(self.hidden_size, self.feature_dim * self.num_heads, bias=False)
|
| 45 |
+
self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
|
| 46 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
| 47 |
+
self.dropout = nn.Identity()
|
| 48 |
+
self.feature_map = TaylorFeatureMap(feature_dim)
|
| 49 |
+
self.eps = eps
|
| 50 |
+
|
| 51 |
+
def forward(self, hidden_states: torch.Tensor, **kwargs):
|
| 52 |
+
mode = self.mode
|
| 53 |
+
q, k, v = self.q_proj(hidden_states), self.k_proj(hidden_states), self.v_proj(hidden_states)
|
| 54 |
+
q, k, v = map(lambda x: rearrange(x, "... (h d) -> ... h d", d=self.head_dim), [q, k, v])
|
| 55 |
+
if mode == "fused_chunk":
|
| 56 |
+
q, k = self.feature_map(q), self.feature_map(k)
|
| 57 |
+
o, _ = fused_chunk_linear_attn(q, k, v, normalize=True, scale=1, head_first=False)
|
| 58 |
+
elif mode == 'chunk':
|
| 59 |
+
q, k = self.feature_map(q), self.feature_map(k)
|
| 60 |
+
o, _ = chunk_linear_attn(q, k, v, normalize=True, scale=1, head_first=False)
|
| 61 |
+
elif mode == 'parallel':
|
| 62 |
+
assert q.shape[-1] <= 128
|
| 63 |
+
o = parallel_based(q, k, v, scale=1, use_norm=True, head_first=False)
|
| 64 |
+
o = rearrange(o, 'b t h d -> b t (h d)')
|
| 65 |
+
o = self.o_proj(o)
|
| 66 |
+
o = self.dropout(o)
|
| 67 |
+
return o
|
| 68 |
+
|
| 69 |
+
# https://github.com/HazyResearch/zoology/blob/main/zoology/mixers/based.py#L119
|
| 70 |
+
|
| 71 |
+
def forward_reference(self, hidden_states: torch.Tensor, filters: torch.Tensor = None, *args, **kwargs):
|
| 72 |
+
"""
|
| 73 |
+
x (torch.Tensor): tensor of shape (b, d, t)
|
| 74 |
+
y (torch.Tensor): tensor of shape (b, d, t)
|
| 75 |
+
"""
|
| 76 |
+
# hidden_states = hidden_states.transpose(1, 2)
|
| 77 |
+
b, t, _ = hidden_states.size()
|
| 78 |
+
q, k, v = self.q_proj(hidden_states), self.k_proj(hidden_states), self.v_proj(hidden_states)
|
| 79 |
+
|
| 80 |
+
q = q.view(b, t, self.num_heads, self.feature_dim).transpose(1, 2)
|
| 81 |
+
k = k.view(b, t, self.num_key_value_heads, self.feature_dim).transpose(1, 2)
|
| 82 |
+
v = v.view(b, t, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
| 83 |
+
|
| 84 |
+
# Linear attention
|
| 85 |
+
q, k = self.feature_map(q), self.feature_map(k)
|
| 86 |
+
q, k, v = q.unsqueeze(-2), k.unsqueeze(-2), v.unsqueeze(-1)
|
| 87 |
+
|
| 88 |
+
# Compute attention
|
| 89 |
+
if self.causal:
|
| 90 |
+
y = ((q * (k * v).cumsum(2)).sum(-1) / ((q * k.cumsum(2)).sum(-1) + self.eps))
|
| 91 |
+
else:
|
| 92 |
+
y = ((q * (k * v).sum(2, True)).sum(-1) / ((q * k.sum(2, True)).sum(-1) + self.eps))
|
| 93 |
+
y = rearrange(y, 'b h t d -> b t (h d)')
|
| 94 |
+
y = self.o_proj(y.to(hidden_states.dtype))
|
| 95 |
+
y = self.dropout(y)
|
| 96 |
+
return y.to(hidden_states.dtype)
|
fla/layers/forgetting_attn.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import torch.nn.functional as F
|
| 11 |
+
import torch.utils.checkpoint
|
| 12 |
+
from einops import rearrange
|
| 13 |
+
from transformers.utils import logging
|
| 14 |
+
|
| 15 |
+
from fla.modules import GroupNorm
|
| 16 |
+
from fla.ops.forgetting_attn.parallel import parallel_forgetting_attn
|
| 17 |
+
|
| 18 |
+
if TYPE_CHECKING:
|
| 19 |
+
from fla.models.utils import Cache
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
logger = logging.get_logger(__name__)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class ForgettingAttention(nn.Module):
|
| 26 |
+
|
| 27 |
+
def __init__(
|
| 28 |
+
self,
|
| 29 |
+
hidden_size: int = 2048,
|
| 30 |
+
num_heads: int = 32,
|
| 31 |
+
num_kv_heads: Optional[int] = None,
|
| 32 |
+
qkv_bias: bool = False,
|
| 33 |
+
qk_norm: bool = False,
|
| 34 |
+
window_size: Optional[int] = None,
|
| 35 |
+
use_output_gate: bool = False,
|
| 36 |
+
layer_idx: int = None
|
| 37 |
+
):
|
| 38 |
+
super().__init__()
|
| 39 |
+
|
| 40 |
+
self.hidden_size = hidden_size
|
| 41 |
+
self.num_heads = num_heads
|
| 42 |
+
if num_kv_heads is None:
|
| 43 |
+
self.num_kv_heads = self.num_heads
|
| 44 |
+
else:
|
| 45 |
+
self.num_kv_heads = num_kv_heads
|
| 46 |
+
self.num_kv_groups = num_heads // self.num_kv_heads
|
| 47 |
+
self.head_dim = self.hidden_size // self.num_heads
|
| 48 |
+
self.kv_dim = self.num_kv_heads * self.head_dim
|
| 49 |
+
self.qkv_bias = qkv_bias
|
| 50 |
+
self.qk_norm = qk_norm
|
| 51 |
+
|
| 52 |
+
self.window_size = window_size
|
| 53 |
+
self.use_output_gate = use_output_gate
|
| 54 |
+
self.layer_idx = layer_idx
|
| 55 |
+
|
| 56 |
+
self.q_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=self.qkv_bias)
|
| 57 |
+
self.k_proj = nn.Linear(self.hidden_size, self.kv_dim, bias=self.qkv_bias)
|
| 58 |
+
self.v_proj = nn.Linear(self.hidden_size, self.kv_dim, bias=self.qkv_bias)
|
| 59 |
+
self.f_proj = nn.Linear(self.hidden_size, self.num_heads, bias=True)
|
| 60 |
+
|
| 61 |
+
if use_output_gate:
|
| 62 |
+
self.g_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
|
| 63 |
+
self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
|
| 64 |
+
|
| 65 |
+
if qk_norm:
|
| 66 |
+
self.q_norm = GroupNorm(
|
| 67 |
+
num_groups=self.num_heads,
|
| 68 |
+
hidden_size=self.hidden_size,
|
| 69 |
+
is_rms_norm=True,
|
| 70 |
+
)
|
| 71 |
+
self.k_norm = GroupNorm(
|
| 72 |
+
num_groups=self.num_kv_heads,
|
| 73 |
+
hidden_size=self.kv_dim,
|
| 74 |
+
is_rms_norm=True,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
def forward(
|
| 78 |
+
self,
|
| 79 |
+
hidden_states: torch.Tensor,
|
| 80 |
+
attention_mask: Optional[torch.LongTensor] = None,
|
| 81 |
+
past_key_values: Optional[Cache] = None,
|
| 82 |
+
output_attentions: bool = False,
|
| 83 |
+
use_cache: bool = False,
|
| 84 |
+
**kwargs,
|
| 85 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
| 86 |
+
if attention_mask is not None:
|
| 87 |
+
assert len(attention_mask.shape) == 2, (
|
| 88 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 89 |
+
"for padding purposes (0 indicating padding). "
|
| 90 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 94 |
+
q, k, v = self.q_proj(hidden_states), self.k_proj(hidden_states), self.v_proj(hidden_states)
|
| 95 |
+
f = F.logsigmoid(self.f_proj(hidden_states).float())
|
| 96 |
+
if self.qk_norm:
|
| 97 |
+
q, k = self.q_norm(q), self.k_norm(k)
|
| 98 |
+
|
| 99 |
+
q = rearrange(q, '... (h d) -> ... h d', d=self.head_dim)
|
| 100 |
+
k = rearrange(k, '... (h d) -> ... h d', d=self.head_dim)
|
| 101 |
+
v = rearrange(v, '... (h d) -> ... h d', d=self.head_dim)
|
| 102 |
+
|
| 103 |
+
o = parallel_forgetting_attn(q, k, v, f, cu_seqlens=cu_seqlens)
|
| 104 |
+
o = rearrange(o, '... h d -> ... (h d)')
|
| 105 |
+
if self.use_output_gate:
|
| 106 |
+
o = self.g_proj(hidden_states).sigmoid() * o
|
| 107 |
+
o = self.o_proj(o)
|
| 108 |
+
|
| 109 |
+
return o, None, past_key_values
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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, 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/hgrn2.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
# "HGRN2: Gated Linear RNNs with State Expansion"[https://arxiv.org/abs/2404.07904]
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from typing import TYPE_CHECKING, Dict, Optional, Tuple
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
import torch.nn.functional as F
|
| 13 |
+
from einops import rearrange
|
| 14 |
+
|
| 15 |
+
from fla.modules import RMSNorm, ShortConvolution
|
| 16 |
+
from fla.modules.activations import swish
|
| 17 |
+
from fla.modules.layernorm import rms_norm_linear
|
| 18 |
+
from fla.ops.gla import chunk_gla, fused_chunk_gla, fused_recurrent_gla
|
| 19 |
+
|
| 20 |
+
if TYPE_CHECKING:
|
| 21 |
+
from transformers.processing_utils import Unpack
|
| 22 |
+
|
| 23 |
+
from fla.models.utils import Cache
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class HGRN2Attention(nn.Module):
|
| 27 |
+
|
| 28 |
+
def __init__(
|
| 29 |
+
self,
|
| 30 |
+
mode: str = 'chunk',
|
| 31 |
+
hidden_size: int = 1024,
|
| 32 |
+
num_heads: Optional[int] = None,
|
| 33 |
+
expand_ratio: Optional[int] = 128,
|
| 34 |
+
use_short_conv: bool = False,
|
| 35 |
+
conv_size: int = 4,
|
| 36 |
+
conv_bias: bool = False,
|
| 37 |
+
elementwise_affine: Optional[bool] = True,
|
| 38 |
+
norm_eps: float = 1e-5,
|
| 39 |
+
layer_idx: int = None
|
| 40 |
+
) -> HGRN2Attention:
|
| 41 |
+
super().__init__()
|
| 42 |
+
|
| 43 |
+
self.mode = mode
|
| 44 |
+
self.hidden_size = hidden_size
|
| 45 |
+
|
| 46 |
+
if expand_ratio is None and num_heads is not None:
|
| 47 |
+
expand_ratio = hidden_size // num_heads
|
| 48 |
+
elif expand_ratio is not None and num_heads is None:
|
| 49 |
+
num_heads = hidden_size // expand_ratio
|
| 50 |
+
elif expand_ratio is None and num_heads is None:
|
| 51 |
+
raise RuntimeError("One of `expand_ratio` or `num_heads` should be provided.")
|
| 52 |
+
self.num_heads = num_heads
|
| 53 |
+
self.expand_ratio = expand_ratio
|
| 54 |
+
|
| 55 |
+
self.use_short_conv = use_short_conv
|
| 56 |
+
self.conv_size = conv_size
|
| 57 |
+
self.conv_bias = conv_bias
|
| 58 |
+
|
| 59 |
+
self.forget_dim = int(self.num_heads * self.expand_ratio)
|
| 60 |
+
self.input_dim = hidden_size
|
| 61 |
+
self.layer_idx = layer_idx
|
| 62 |
+
|
| 63 |
+
assert mode in ['chunk', 'fused_recurrent', 'fused_chunk'], f"Not suppoerted mode `{mode}`."
|
| 64 |
+
assert self.forget_dim % num_heads == 0, f"forget dim must be divisible by num_heads of {num_heads}"
|
| 65 |
+
assert self.input_dim % num_heads == 0, f"input dim must be divisible by num_heads of {num_heads}"
|
| 66 |
+
|
| 67 |
+
self.head_f_dim = self.expand_ratio
|
| 68 |
+
self.head_i_dim = self.hidden_size // num_heads
|
| 69 |
+
|
| 70 |
+
self.q_proj = nn.Linear(hidden_size, self.forget_dim, bias=False)
|
| 71 |
+
self.f_proj = nn.Linear(hidden_size, self.forget_dim, bias=False)
|
| 72 |
+
self.i_proj = nn.Linear(hidden_size, self.input_dim, bias=False)
|
| 73 |
+
|
| 74 |
+
if use_short_conv:
|
| 75 |
+
self.conv_size = conv_size
|
| 76 |
+
self.q_conv1d = ShortConvolution(self.forget_dim, conv_size, activation=None)
|
| 77 |
+
self.f_conv1d = ShortConvolution(self.forget_dim, conv_size, activation=None)
|
| 78 |
+
self.i_conv1d = ShortConvolution(self.input_dim, conv_size, activation=None)
|
| 79 |
+
|
| 80 |
+
self.g_norm = RMSNorm(hidden_size=self.hidden_size, elementwise_affine=elementwise_affine, eps=norm_eps)
|
| 81 |
+
self.o_proj = nn.Linear(self.input_dim, hidden_size, bias=False)
|
| 82 |
+
|
| 83 |
+
def forward(
|
| 84 |
+
self,
|
| 85 |
+
hidden_states: torch.Tensor,
|
| 86 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 87 |
+
past_key_values: Optional[Cache] = None,
|
| 88 |
+
use_cache: Optional[bool] = False,
|
| 89 |
+
output_attentions: Optional[bool] = False,
|
| 90 |
+
lower_bound: Optional[torch.Tensor] = None,
|
| 91 |
+
**kwargs: Unpack[Dict]
|
| 92 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]:
|
| 93 |
+
if attention_mask is not None:
|
| 94 |
+
assert len(attention_mask.shape) == 2, (
|
| 95 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 96 |
+
"for padding purposes (0 indicating padding). "
|
| 97 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# launching the triton kernel for just one token will actually be slower
|
| 101 |
+
mode = 'fused_recurrent' if hidden_states.shape[1] <= 64 else self.mode
|
| 102 |
+
|
| 103 |
+
last_state = None
|
| 104 |
+
if past_key_values is not None and len(past_key_values) > self.layer_idx:
|
| 105 |
+
last_state = past_key_values[self.layer_idx]
|
| 106 |
+
|
| 107 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 108 |
+
if self.use_short_conv:
|
| 109 |
+
conv_state_q, conv_state_f, conv_state_i = None, None, None
|
| 110 |
+
if last_state is not None:
|
| 111 |
+
conv_state_q, conv_state_f, conv_state_i = last_state['conv_state']
|
| 112 |
+
conv_mask = attention_mask[:, -hidden_states.shape[1]:] if attention_mask is not None else None
|
| 113 |
+
q, conv_state_q = self.q_conv1d(
|
| 114 |
+
x=self.q_proj(hidden_states),
|
| 115 |
+
mask=conv_mask,
|
| 116 |
+
cache=conv_state_q,
|
| 117 |
+
output_final_state=use_cache,
|
| 118 |
+
cu_seqlens=cu_seqlens
|
| 119 |
+
)
|
| 120 |
+
f, conv_state_f = self.f_conv1d(
|
| 121 |
+
x=self.f_proj(hidden_states),
|
| 122 |
+
mask=conv_mask,
|
| 123 |
+
cache=conv_state_f,
|
| 124 |
+
output_final_state=use_cache,
|
| 125 |
+
cu_seqlens=cu_seqlens
|
| 126 |
+
)
|
| 127 |
+
i, conv_state_i = self.i_conv1d(
|
| 128 |
+
x=self.i_proj(hidden_states),
|
| 129 |
+
mask=conv_mask,
|
| 130 |
+
cache=conv_state_i,
|
| 131 |
+
output_final_state=use_cache,
|
| 132 |
+
cu_seqlens=cu_seqlens
|
| 133 |
+
)
|
| 134 |
+
else:
|
| 135 |
+
q = self.q_proj(hidden_states)
|
| 136 |
+
f = self.f_proj(hidden_states)
|
| 137 |
+
i = self.i_proj(hidden_states)
|
| 138 |
+
|
| 139 |
+
# dealing with left-padding
|
| 140 |
+
if attention_mask is not None:
|
| 141 |
+
i = i.mul_(attention_mask[:, -i.shape[-2]:, None])
|
| 142 |
+
|
| 143 |
+
q = swish(q)
|
| 144 |
+
|
| 145 |
+
# improve precision
|
| 146 |
+
f = f.float()
|
| 147 |
+
|
| 148 |
+
# the lower bound for the first layer is zero
|
| 149 |
+
if lower_bound is None or self.layer_idx == 0:
|
| 150 |
+
k, g = 1 - f.sigmoid(), F.logsigmoid(f)
|
| 151 |
+
else:
|
| 152 |
+
g = lower_bound + (1 - lower_bound) * f.sigmoid()
|
| 153 |
+
k, g = 1 - g, g.log()
|
| 154 |
+
|
| 155 |
+
q, k, g = map(lambda x: rearrange(x, '... (h d) -> ... h d', d=self.head_f_dim), (q, k.to(i), g))
|
| 156 |
+
i = rearrange(i, '... (h d) -> ... h d', d=self.head_i_dim)
|
| 157 |
+
|
| 158 |
+
recurrent_state = last_state['recurrent_state'] if last_state is not None else None
|
| 159 |
+
if mode == 'fused_recurrent':
|
| 160 |
+
o, recurrent_state = fused_recurrent_gla(
|
| 161 |
+
q=q,
|
| 162 |
+
k=k,
|
| 163 |
+
v=i,
|
| 164 |
+
gk=g,
|
| 165 |
+
initial_state=recurrent_state,
|
| 166 |
+
output_final_state=use_cache,
|
| 167 |
+
cu_seqlens=cu_seqlens,
|
| 168 |
+
head_first=False
|
| 169 |
+
)
|
| 170 |
+
elif mode == 'fused_chunk':
|
| 171 |
+
o, recurrent_state = fused_chunk_gla(
|
| 172 |
+
q=q,
|
| 173 |
+
k=k,
|
| 174 |
+
v=i,
|
| 175 |
+
g=g,
|
| 176 |
+
initial_state=recurrent_state,
|
| 177 |
+
output_final_state=use_cache,
|
| 178 |
+
head_first=False
|
| 179 |
+
)
|
| 180 |
+
elif mode == 'chunk':
|
| 181 |
+
o, recurrent_state = chunk_gla(
|
| 182 |
+
q=q,
|
| 183 |
+
k=k,
|
| 184 |
+
v=i,
|
| 185 |
+
g=g,
|
| 186 |
+
initial_state=recurrent_state,
|
| 187 |
+
output_final_state=use_cache,
|
| 188 |
+
cu_seqlens=cu_seqlens,
|
| 189 |
+
head_first=False
|
| 190 |
+
)
|
| 191 |
+
else:
|
| 192 |
+
raise NotImplementedError(f"Not supported mode `{mode}`.")
|
| 193 |
+
|
| 194 |
+
if past_key_values is not None:
|
| 195 |
+
past_key_values.update(
|
| 196 |
+
recurrent_state=recurrent_state,
|
| 197 |
+
conv_state=(conv_state_q, conv_state_f, conv_state_i) if self.use_short_conv else None,
|
| 198 |
+
layer_idx=self.layer_idx,
|
| 199 |
+
offset=q.shape[1]
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
o = rearrange(o, '... h d -> ... (h d)')
|
| 203 |
+
o = rms_norm_linear(o, self.g_norm.weight, self.g_norm.bias, self.o_proj.weight, self.o_proj.bias)
|
| 204 |
+
return o, None, past_key_values
|
| 205 |
+
|
| 206 |
+
def state_size(self, **kwargs) -> int:
|
| 207 |
+
state_size = self.forget_dim * self.head_i_dim
|
| 208 |
+
for module in self.children():
|
| 209 |
+
if isinstance(module, ShortConvolution):
|
| 210 |
+
state_size += module.state_size
|
| 211 |
+
return state_size
|
fla/layers/linear_attn.py
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
from einops import rearrange, repeat
|
| 10 |
+
|
| 11 |
+
from fla.modules import RMSNorm
|
| 12 |
+
from fla.modules.feature_map import DPFPFeatureMap, HadamardFeatureMap, HedgehogFeatureMap, T2RFeatureMap
|
| 13 |
+
from fla.ops.linear_attn import chunk_linear_attn, fused_chunk_linear_attn, fused_recurrent_linear_attn
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class LinearAttention(nn.Module):
|
| 17 |
+
|
| 18 |
+
def __init__(
|
| 19 |
+
self,
|
| 20 |
+
mode: str = 'chunk',
|
| 21 |
+
hidden_size: str = 1024,
|
| 22 |
+
expand_k: int = 1.0,
|
| 23 |
+
expand_v: int = 1.0,
|
| 24 |
+
num_heads: int = 8,
|
| 25 |
+
num_kv_heads: Optional[int] = None,
|
| 26 |
+
feature_map: str = 'elementwise_product',
|
| 27 |
+
tie_feature_map_qk: bool = False,
|
| 28 |
+
output_norm: str = 'rmsnorm',
|
| 29 |
+
norm_q: bool = False,
|
| 30 |
+
norm_k: bool = False,
|
| 31 |
+
do_feature_map_norm: bool = False,
|
| 32 |
+
elementwise_affine: bool = True,
|
| 33 |
+
norm_eps: float = 1e-5,
|
| 34 |
+
**kwargs
|
| 35 |
+
):
|
| 36 |
+
super().__init__()
|
| 37 |
+
|
| 38 |
+
self.hidden_size = hidden_size
|
| 39 |
+
self.mode = mode
|
| 40 |
+
self.num_heads = num_heads
|
| 41 |
+
self.num_kv_heads = num_kv_heads if num_kv_heads is not None else num_heads
|
| 42 |
+
self.num_kv_groups = self.num_heads // self.num_kv_heads
|
| 43 |
+
self.key_dim = int(hidden_size * expand_k)
|
| 44 |
+
self.value_dim = int(hidden_size * expand_v)
|
| 45 |
+
self.key_dim_per_group = self.key_dim // self.num_kv_groups
|
| 46 |
+
self.value_dim_per_group = self.value_dim // self.num_kv_groups
|
| 47 |
+
|
| 48 |
+
assert mode in ['chunk', 'fused_chunk', 'fused_recurrent'], f"Not suppoerted mode `{mode}`."
|
| 49 |
+
assert self.key_dim % num_heads == 0, f"key dim must be divisible by num_heads of {num_heads}"
|
| 50 |
+
assert self.value_dim % num_heads == 0, f"value dim must be divisible by num_heads of {num_heads}"
|
| 51 |
+
|
| 52 |
+
self.head_k_dim = self.key_dim // num_heads
|
| 53 |
+
self.head_v_dim = self.value_dim // num_heads
|
| 54 |
+
self.do_feature_map_norm = do_feature_map_norm
|
| 55 |
+
|
| 56 |
+
if feature_map == 'hedgehog':
|
| 57 |
+
if tie_feature_map_qk:
|
| 58 |
+
self.feature_map_q = self.feature_map_k = HedgehogFeatureMap(head_dim=self.head_k_dim)
|
| 59 |
+
else:
|
| 60 |
+
self.feature_map_q = HedgehogFeatureMap(head_dim=self.head_k_dim)
|
| 61 |
+
self.feature_map_k = HedgehogFeatureMap(head_dim=self.head_k_dim)
|
| 62 |
+
|
| 63 |
+
elif feature_map == 't2r':
|
| 64 |
+
if tie_feature_map_qk:
|
| 65 |
+
self.feature_map_q = self.feature_map_k = T2RFeatureMap(head_dim=self.head_k_dim)
|
| 66 |
+
else:
|
| 67 |
+
self.feature_map_q = T2RFeatureMap(head_dim=self.head_k_dim)
|
| 68 |
+
self.feature_map_k = T2RFeatureMap(head_dim=self.head_k_dim)
|
| 69 |
+
|
| 70 |
+
elif feature_map == 'elementwise_product':
|
| 71 |
+
if tie_feature_map_qk:
|
| 72 |
+
self.feature_map_q = self.feature_map_k = HadamardFeatureMap(head_dim=self.head_k_dim)
|
| 73 |
+
else:
|
| 74 |
+
self.feature_map_q = HadamardFeatureMap(head_dim=self.head_k_dim)
|
| 75 |
+
self.feature_map_k = HadamardFeatureMap(head_dim=self.head_k_dim)
|
| 76 |
+
|
| 77 |
+
elif feature_map == 'dpfp':
|
| 78 |
+
self.feature_map_q = DPFPFeatureMap(head_dim=self.head_k_dim)
|
| 79 |
+
self.feature_map_k = DPFPFeatureMap(head_dim=self.head_k_dim)
|
| 80 |
+
|
| 81 |
+
elif feature_map == 'elu':
|
| 82 |
+
def elu(x):
|
| 83 |
+
return F.elu(x) + 1
|
| 84 |
+
self.feature_map_q = elu
|
| 85 |
+
self.feature_map_k = elu
|
| 86 |
+
|
| 87 |
+
elif feature_map == 'relu':
|
| 88 |
+
self.feature_map_q = nn.ReLU()
|
| 89 |
+
self.feature_map_k = nn.ReLU()
|
| 90 |
+
|
| 91 |
+
elif feature_map == 'identity':
|
| 92 |
+
self.feature_map_q = nn.Identity()
|
| 93 |
+
self.feature_map_k = nn.Identity()
|
| 94 |
+
else:
|
| 95 |
+
raise NotImplementedError(f"Not supported feature map `{feature_map}`.")
|
| 96 |
+
|
| 97 |
+
self.q_proj = nn.Linear(hidden_size, self.key_dim, bias=False)
|
| 98 |
+
self.k_proj = nn.Linear(hidden_size, self.key_dim_per_group, bias=False)
|
| 99 |
+
self.v_proj = nn.Linear(hidden_size, self.value_dim_per_group, bias=False)
|
| 100 |
+
|
| 101 |
+
if output_norm == 'rmsnorm':
|
| 102 |
+
self.norm = RMSNorm(hidden_size=self.head_v_dim, elementwise_affine=elementwise_affine, eps=norm_eps)
|
| 103 |
+
elif output_norm == 'identity':
|
| 104 |
+
self.norm = nn.Identity()
|
| 105 |
+
else:
|
| 106 |
+
raise NotImplementedError(f"Not supported output norm `{output_norm}`.")
|
| 107 |
+
|
| 108 |
+
self.o_proj = nn.Linear(self.value_dim, hidden_size, bias=False)
|
| 109 |
+
|
| 110 |
+
self.norm_q = norm_q
|
| 111 |
+
self.norm_k = norm_k
|
| 112 |
+
|
| 113 |
+
def forward(
|
| 114 |
+
self,
|
| 115 |
+
hidden_states: torch.Tensor,
|
| 116 |
+
**kwargs
|
| 117 |
+
) -> torch.Tensor:
|
| 118 |
+
mode = self.mode
|
| 119 |
+
q = self.q_proj(hidden_states)
|
| 120 |
+
k = self.k_proj(hidden_states)
|
| 121 |
+
v = self.v_proj(hidden_states)
|
| 122 |
+
|
| 123 |
+
q = rearrange(q, '... (h d) -> ... h d', d=self.head_k_dim)
|
| 124 |
+
if self.num_kv_groups > 1:
|
| 125 |
+
k = repeat(k, '... (h d) -> ... (h g) d', d=self.head_k_dim, g=self.num_kv_groups)
|
| 126 |
+
v = repeat(v, '... (h d) -> ... (h g) d', d=self.head_v_dim, g=self.num_kv_groups)
|
| 127 |
+
else:
|
| 128 |
+
k = rearrange(k, '... (h d) -> ... h d', d=self.head_k_dim)
|
| 129 |
+
v = rearrange(v, '... (h d) -> ... h d', d=self.head_v_dim)
|
| 130 |
+
|
| 131 |
+
q = self.feature_map_q(q)
|
| 132 |
+
k = self.feature_map_k(k)
|
| 133 |
+
|
| 134 |
+
if self.norm_q:
|
| 135 |
+
q = q / (q.sum(-1, True) + 1e-4)
|
| 136 |
+
if self.norm_k:
|
| 137 |
+
k = k / (k.sum(-1, True) + 1e-4)
|
| 138 |
+
|
| 139 |
+
if mode == 'chunk':
|
| 140 |
+
o, final_state = chunk_linear_attn(
|
| 141 |
+
q=q,
|
| 142 |
+
k=k,
|
| 143 |
+
v=v,
|
| 144 |
+
normalize=self.do_feature_map_norm,
|
| 145 |
+
head_first=False
|
| 146 |
+
)
|
| 147 |
+
elif mode == 'fused_chunk':
|
| 148 |
+
o, final_state = fused_chunk_linear_attn(
|
| 149 |
+
q=q,
|
| 150 |
+
k=k,
|
| 151 |
+
v=v,
|
| 152 |
+
normalize=self.do_feature_map_norm,
|
| 153 |
+
)
|
| 154 |
+
elif mode == 'fused_recurrent':
|
| 155 |
+
o, final_state = fused_recurrent_linear_attn(
|
| 156 |
+
q=q,
|
| 157 |
+
k=k,
|
| 158 |
+
v=v,
|
| 159 |
+
normalize=self.do_feature_map_norm,
|
| 160 |
+
)
|
| 161 |
+
else:
|
| 162 |
+
raise NotImplementedError
|
| 163 |
+
o = self.norm(o)
|
| 164 |
+
o = rearrange(o, '... h d -> ... (h d)')
|
| 165 |
+
o = self.o_proj(o)
|
| 166 |
+
return o
|
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/layers/nsa.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, Union
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
from einops import rearrange
|
| 11 |
+
from transformers.utils import logging
|
| 12 |
+
|
| 13 |
+
from fla.modules import RotaryEmbedding
|
| 14 |
+
from fla.ops.nsa.parallel import parallel_nsa
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from fla.models.utils import Cache
|
| 18 |
+
|
| 19 |
+
logger = logging.get_logger(__name__)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class NativeSparseAttention(nn.Module):
|
| 23 |
+
|
| 24 |
+
def __init__(
|
| 25 |
+
self,
|
| 26 |
+
hidden_size: int = 2048,
|
| 27 |
+
num_heads: int = 64,
|
| 28 |
+
num_kv_heads: Optional[int] = 4,
|
| 29 |
+
head_dim: int = 64,
|
| 30 |
+
qkv_bias: bool = False,
|
| 31 |
+
block_size: Optional[int] = 64,
|
| 32 |
+
block_counts: Optional[Union[torch.LongTensor, int]] = 16,
|
| 33 |
+
window_size: Optional[int] = 512,
|
| 34 |
+
rope_theta: Optional[float] = 10000.,
|
| 35 |
+
max_position_embeddings: Optional[int] = None,
|
| 36 |
+
layer_idx: int = None
|
| 37 |
+
):
|
| 38 |
+
super().__init__()
|
| 39 |
+
|
| 40 |
+
self.hidden_size = hidden_size
|
| 41 |
+
self.num_heads = num_heads
|
| 42 |
+
if num_kv_heads is None:
|
| 43 |
+
self.num_kv_heads = self.num_heads
|
| 44 |
+
else:
|
| 45 |
+
self.num_kv_heads = num_kv_heads
|
| 46 |
+
self.num_kv_groups = num_heads // self.num_kv_heads
|
| 47 |
+
self.head_dim = head_dim
|
| 48 |
+
self.kv_dim = self.num_kv_heads * self.head_dim
|
| 49 |
+
self.qkv_bias = qkv_bias
|
| 50 |
+
|
| 51 |
+
self.block_size = block_size
|
| 52 |
+
self.block_counts = block_counts
|
| 53 |
+
self.window_size = window_size
|
| 54 |
+
self.rope_theta = rope_theta
|
| 55 |
+
self.max_position_embeddings = max_position_embeddings
|
| 56 |
+
self.layer_idx = layer_idx
|
| 57 |
+
|
| 58 |
+
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=self.qkv_bias)
|
| 59 |
+
self.k_proj = nn.Linear(self.hidden_size, self.kv_dim, bias=self.qkv_bias)
|
| 60 |
+
self.v_proj = nn.Linear(self.hidden_size, self.kv_dim, bias=self.qkv_bias)
|
| 61 |
+
self.g_proj = nn.Linear(self.hidden_size, self.num_heads * 3, bias=False)
|
| 62 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
| 63 |
+
|
| 64 |
+
self.rotary = RotaryEmbedding(dim=self.head_dim, base=self.rope_theta)
|
| 65 |
+
|
| 66 |
+
def forward(
|
| 67 |
+
self,
|
| 68 |
+
hidden_states: torch.Tensor,
|
| 69 |
+
attention_mask: Optional[torch.LongTensor] = None,
|
| 70 |
+
past_key_values: Optional[Cache] = None,
|
| 71 |
+
output_attentions: bool = False,
|
| 72 |
+
use_cache: bool = False,
|
| 73 |
+
**kwargs,
|
| 74 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
| 75 |
+
if attention_mask is not None:
|
| 76 |
+
assert len(attention_mask.shape) == 2, (
|
| 77 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 78 |
+
"for padding purposes (0 indicating padding). "
|
| 79 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
batch_size, seq_len, _ = hidden_states.size()
|
| 83 |
+
|
| 84 |
+
q = rearrange(self.q_proj(hidden_states), '... (h d) -> ... h d', d=self.head_dim)
|
| 85 |
+
k = rearrange(self.k_proj(hidden_states), '... (h d) -> ... h d', d=self.head_dim)
|
| 86 |
+
v = rearrange(self.v_proj(hidden_states), '... (h d) -> ... h d', d=self.head_dim)
|
| 87 |
+
g = rearrange(self.g_proj(hidden_states), '... (h d) -> ... h d', d=3)
|
| 88 |
+
g_cmp, g_slc, g_swa = g.sigmoid().unbind(-1)
|
| 89 |
+
|
| 90 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 91 |
+
|
| 92 |
+
seqlen_offset, max_seqlen = 0, seq_len
|
| 93 |
+
if past_key_values is not None:
|
| 94 |
+
seqlen_offset = past_key_values.get_seq_length(self.layer_idx)
|
| 95 |
+
max_seqlen = q.shape[1] + seqlen_offset
|
| 96 |
+
|
| 97 |
+
if attention_mask is not None:
|
| 98 |
+
# to deliminate the offsets of padding tokens
|
| 99 |
+
seqlen_offset = seqlen_offset + attention_mask.sum(-1) - attention_mask.shape[-1]
|
| 100 |
+
max_seqlen = q.shape[1] + max(seqlen_offset)
|
| 101 |
+
|
| 102 |
+
if self.max_position_embeddings is not None:
|
| 103 |
+
max_seqlen = max(max_seqlen, self.max_position_embeddings)
|
| 104 |
+
q, k = self.rotary(q, k, seqlen_offset=seqlen_offset, max_seqlen=max_seqlen, cu_seqlens=cu_seqlens)
|
| 105 |
+
|
| 106 |
+
if past_key_values is not None:
|
| 107 |
+
cache_has_content = past_key_values.get_seq_length(self.layer_idx) > 0
|
| 108 |
+
k_cached, v_cached = past_key_values.update(
|
| 109 |
+
attn_state=(k.flatten(-2, -1), v.flatten(-2, -1)),
|
| 110 |
+
layer_idx=self.layer_idx,
|
| 111 |
+
offset=seq_len,
|
| 112 |
+
cache_kwargs=dict(window_size=self.window_size)
|
| 113 |
+
)['attn_state']
|
| 114 |
+
if cache_has_content:
|
| 115 |
+
k, v = k_cached, v_cached
|
| 116 |
+
k = rearrange(k, '... (h d) -> ... h d', d=self.head_dim)
|
| 117 |
+
v = rearrange(v, '... (h d) -> ... h d', d=self.head_dim)
|
| 118 |
+
|
| 119 |
+
o = parallel_nsa(
|
| 120 |
+
q=q,
|
| 121 |
+
k=k,
|
| 122 |
+
v=v,
|
| 123 |
+
g_cmp=g_cmp,
|
| 124 |
+
g_slc=g_slc,
|
| 125 |
+
g_swa=g_swa,
|
| 126 |
+
block_size=self.block_size,
|
| 127 |
+
block_counts=self.block_counts,
|
| 128 |
+
window_size=self.window_size,
|
| 129 |
+
cu_seqlens=cu_seqlens,
|
| 130 |
+
head_first=False
|
| 131 |
+
)
|
| 132 |
+
o = o.reshape(batch_size, seq_len, -1)
|
| 133 |
+
o = self.o_proj(o)
|
| 134 |
+
|
| 135 |
+
if not output_attentions:
|
| 136 |
+
attentions = None
|
| 137 |
+
|
| 138 |
+
return o, attentions, past_key_values
|
fla/layers/rebased.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
https://github.com/corl-team/rebased/blob/main/flash_linear_attention/fla/layers/rebased_fast.py
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
from typing import Optional
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
from einops import rearrange
|
| 15 |
+
|
| 16 |
+
from fla.modules.feature_map import RebasedFeatureMap
|
| 17 |
+
from fla.ops.linear_attn import chunk_linear_attn, fused_chunk_linear_attn
|
| 18 |
+
from fla.ops.rebased import parallel_rebased
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class ReBasedLinearAttention(nn.Module):
|
| 22 |
+
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
hidden_size: int,
|
| 26 |
+
l_max: int = 2048,
|
| 27 |
+
feature_dim: int = 16,
|
| 28 |
+
num_key_value_heads: int = 16,
|
| 29 |
+
num_heads: int = 16,
|
| 30 |
+
use_gamma: Optional[bool] = True,
|
| 31 |
+
use_beta: Optional[bool] = True,
|
| 32 |
+
normalize: Optional[bool] = True,
|
| 33 |
+
causal: bool = True,
|
| 34 |
+
eps: float = 1e-5,
|
| 35 |
+
mode: str = "parallel",
|
| 36 |
+
layer_idx: Optional[int] = None,
|
| 37 |
+
**kwargs
|
| 38 |
+
) -> ReBasedLinearAttention:
|
| 39 |
+
super().__init__()
|
| 40 |
+
self.hidden_size = hidden_size
|
| 41 |
+
self.l_max = l_max
|
| 42 |
+
self.mode = mode
|
| 43 |
+
assert self.mode in ["fused_chunk", "parallel", 'chunk']
|
| 44 |
+
|
| 45 |
+
self.feature_dim = feature_dim
|
| 46 |
+
self.num_key_value_heads = num_key_value_heads
|
| 47 |
+
self.num_heads = num_heads
|
| 48 |
+
self.head_dim = self.hidden_size // self.num_key_value_heads
|
| 49 |
+
self.use_gamma = use_gamma
|
| 50 |
+
self.use_beta = use_beta
|
| 51 |
+
self.normalize = normalize
|
| 52 |
+
self.causal = causal
|
| 53 |
+
self.eps = eps
|
| 54 |
+
self.mode = mode
|
| 55 |
+
self.layer_idx = layer_idx
|
| 56 |
+
|
| 57 |
+
self.feature_map = RebasedFeatureMap(self.feature_dim, use_gamma, use_beta, normalize)
|
| 58 |
+
self.q_proj = nn.Linear(self.hidden_size, self.feature_dim * self.num_heads, bias=False)
|
| 59 |
+
self.k_proj = nn.Linear(self.hidden_size, self.feature_dim * self.num_heads, bias=False)
|
| 60 |
+
self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
|
| 61 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
| 62 |
+
self.dropout = nn.Identity()
|
| 63 |
+
|
| 64 |
+
def forward(self, hidden_states: torch.Tensor, **kwargs):
|
| 65 |
+
mode = self.mode
|
| 66 |
+
q, k, v = self.q_proj(hidden_states), self.k_proj(hidden_states), self.v_proj(hidden_states)
|
| 67 |
+
q, k, v = map(lambda x: rearrange(x, "... (h d) -> ... h d", d=self.head_dim), [q, k, v])
|
| 68 |
+
q, k = self.feature_map(q, flatten=(mode != 'parallel')), self.feature_map(k, flatten=(mode != 'parallel'))
|
| 69 |
+
if mode == "fused_chunk":
|
| 70 |
+
o = fused_chunk_linear_attn(
|
| 71 |
+
q=q,
|
| 72 |
+
k=k,
|
| 73 |
+
v=v,
|
| 74 |
+
normalize=True,
|
| 75 |
+
scale=1,
|
| 76 |
+
head_first=False
|
| 77 |
+
)
|
| 78 |
+
elif mode == 'chunk':
|
| 79 |
+
o = chunk_linear_attn(
|
| 80 |
+
q=q,
|
| 81 |
+
k=k,
|
| 82 |
+
v=v,
|
| 83 |
+
normalize=True,
|
| 84 |
+
scale=1,
|
| 85 |
+
head_first=False
|
| 86 |
+
)
|
| 87 |
+
elif mode == 'parallel':
|
| 88 |
+
assert q.shape[-1] <= 128
|
| 89 |
+
o = parallel_rebased(
|
| 90 |
+
q=q,
|
| 91 |
+
k=k,
|
| 92 |
+
v=v,
|
| 93 |
+
eps=self.eps,
|
| 94 |
+
use_scale=True,
|
| 95 |
+
use_normalize=True,
|
| 96 |
+
head_first=False
|
| 97 |
+
)
|
| 98 |
+
o = self.o_proj(o)
|
| 99 |
+
o = self.dropout(o)
|
| 100 |
+
return o
|
| 101 |
+
|
| 102 |
+
# https://github.com/HazyResearch/zoology/blob/main/zoology/mixers/based.py#L119
|
| 103 |
+
def forward_reference(
|
| 104 |
+
self,
|
| 105 |
+
hidden_states: torch.Tensor,
|
| 106 |
+
filters: torch.Tensor = None,
|
| 107 |
+
*args,
|
| 108 |
+
**kwargs
|
| 109 |
+
):
|
| 110 |
+
"""
|
| 111 |
+
x (torch.Tensor): tensor of shape (b, d, t)
|
| 112 |
+
y (torch.Tensor): tensor of shape (b, d, t)
|
| 113 |
+
"""
|
| 114 |
+
b, t, _ = hidden_states.size()
|
| 115 |
+
q, k, v = self.q_proj(hidden_states), self.k_proj(hidden_states), self.v_proj(hidden_states)
|
| 116 |
+
|
| 117 |
+
q = q.view(b, t, -1, self.feature_dim).transpose(1, 2)
|
| 118 |
+
k = k.view(b, t, -1, self.feature_dim).transpose(1, 2)
|
| 119 |
+
v = v.view(b, t, -1, self.head_dim).transpose(1, 2)
|
| 120 |
+
|
| 121 |
+
# Linear attention
|
| 122 |
+
q, k = self.feature_map(q), self.feature_map(k)
|
| 123 |
+
q, k, v = q.unsqueeze(-2), k.unsqueeze(-2), v.unsqueeze(-1)
|
| 124 |
+
|
| 125 |
+
# Compute attention
|
| 126 |
+
if self.causal:
|
| 127 |
+
y = ((q * (k * v).cumsum(2)).sum(-1) / ((q * k.cumsum(2)).sum(-1) + self.eps))
|
| 128 |
+
else:
|
| 129 |
+
y = ((q * (k * v).sum(2, True)).sum(-1) / ((q * k.sum(2, True)).sum(-1) + self.eps))
|
| 130 |
+
y = rearrange(y, 'b h t d -> b t (h d)')
|
| 131 |
+
y = self.o_proj(y.to(hidden_states.dtype))
|
| 132 |
+
y = self.dropout(y)
|
| 133 |
+
return y.to(hidden_states.dtype)
|
fla/layers/rwkv6.py
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
| 3 |
+
|
| 4 |
+
# "Eagle and Finch: RWKV with Matrix-Valued States and Dynamic Recurrence"[https://arxiv.org/abs/2404.05892]
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from typing import TYPE_CHECKING, Optional, Tuple
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
from einops import rearrange
|
| 13 |
+
|
| 14 |
+
from fla.modules import GroupNorm
|
| 15 |
+
from fla.modules.activations import ACT2FN
|
| 16 |
+
from fla.ops.rwkv6 import chunk_rwkv6, fused_recurrent_rwkv6
|
| 17 |
+
|
| 18 |
+
if TYPE_CHECKING:
|
| 19 |
+
from fla.models.utils import Cache
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class RWKV6Attention(nn.Module):
|
| 23 |
+
|
| 24 |
+
def __init__(
|
| 25 |
+
self,
|
| 26 |
+
mode: str = 'chunk',
|
| 27 |
+
hidden_size: int = 1024,
|
| 28 |
+
expand_k: float = 0.5,
|
| 29 |
+
expand_v: float = 1.0,
|
| 30 |
+
num_heads: int = 4,
|
| 31 |
+
gate_fn: str = 'swish',
|
| 32 |
+
proj_low_rank_dim: int = 32,
|
| 33 |
+
gate_low_rank_dim: int = 64,
|
| 34 |
+
fuse_norm: bool = True,
|
| 35 |
+
elementwise_affine: Optional[bool] = True,
|
| 36 |
+
norm_eps: float = 1e-5,
|
| 37 |
+
layer_idx: int = None,
|
| 38 |
+
**kwargs
|
| 39 |
+
) -> RWKV6Attention:
|
| 40 |
+
super().__init__()
|
| 41 |
+
|
| 42 |
+
self.mode = mode
|
| 43 |
+
self.hidden_size = hidden_size
|
| 44 |
+
self.expand_k = expand_k
|
| 45 |
+
self.expand_v = expand_v
|
| 46 |
+
self.num_heads = num_heads
|
| 47 |
+
self.proj_low_rank_dim = proj_low_rank_dim
|
| 48 |
+
self.gate_low_rank_dim = gate_low_rank_dim
|
| 49 |
+
|
| 50 |
+
self.key_dim = int(hidden_size * expand_k)
|
| 51 |
+
self.value_dim = int(hidden_size * expand_v)
|
| 52 |
+
self.layer_idx = layer_idx
|
| 53 |
+
|
| 54 |
+
assert mode in ['chunk', 'fused_recurrent'], f"Not suppoerted mode `{mode}`."
|
| 55 |
+
assert self.key_dim % num_heads == 0, f"key dim must be divisible by num_heads of {num_heads}"
|
| 56 |
+
assert self.value_dim % num_heads == 0, f"value dim must be divisible by num_heads of {num_heads}"
|
| 57 |
+
|
| 58 |
+
self.head_k_dim = self.key_dim // num_heads
|
| 59 |
+
self.head_v_dim = self.value_dim // num_heads
|
| 60 |
+
|
| 61 |
+
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
|
| 62 |
+
self.x_proj = nn.Sequential(
|
| 63 |
+
LerpLinear(hidden_size, proj_low_rank_dim * 5),
|
| 64 |
+
nn.Tanh(),
|
| 65 |
+
nn.Linear(proj_low_rank_dim * 5, hidden_size, bias=False)
|
| 66 |
+
)
|
| 67 |
+
self.x_bias = nn.Parameter(torch.zeros(5, hidden_size))
|
| 68 |
+
|
| 69 |
+
self.r_proj = DDLerpLinear(hidden_size, self.key_dim)
|
| 70 |
+
self.w_proj = DDLerpLinear(hidden_size, self.key_dim, low_rank_dim=gate_low_rank_dim)
|
| 71 |
+
self.k_proj = DDLerpLinear(hidden_size, self.key_dim)
|
| 72 |
+
self.v_proj = DDLerpLinear(hidden_size, self.value_dim)
|
| 73 |
+
self.g_proj = DDLerpLinear(hidden_size, self.value_dim)
|
| 74 |
+
self.bonus = nn.Parameter(torch.zeros(num_heads, self.head_k_dim))
|
| 75 |
+
|
| 76 |
+
# TODO: fuse GroupNorm and output gate
|
| 77 |
+
self.g_norm = GroupNorm(self.num_heads, self.value_dim, elementwise_affine=elementwise_affine, bias=True, eps=norm_eps)
|
| 78 |
+
self.o_proj = nn.Linear(self.value_dim, hidden_size, bias=False)
|
| 79 |
+
self.gate_fn = ACT2FN[gate_fn]
|
| 80 |
+
|
| 81 |
+
self.apply(self._initialize_weights)
|
| 82 |
+
|
| 83 |
+
def _initialize_weights(self, module: nn.Module):
|
| 84 |
+
if getattr(module, "_is_hf_initialized", False):
|
| 85 |
+
return
|
| 86 |
+
if isinstance(module, nn.Linear):
|
| 87 |
+
nn.init.xavier_uniform_(module.weight, gain=2 ** -2.5)
|
| 88 |
+
if module.bias is not None:
|
| 89 |
+
nn.init.zeros_(module.bias)
|
| 90 |
+
if isinstance(module, nn.Parameter):
|
| 91 |
+
nn.init.xavier_uniform_(module, gain=2 ** -2.5)
|
| 92 |
+
module._is_hf_initialized = True
|
| 93 |
+
|
| 94 |
+
def forward(
|
| 95 |
+
self,
|
| 96 |
+
hidden_states: torch.Tensor,
|
| 97 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 98 |
+
past_key_values: Optional[Cache] = None,
|
| 99 |
+
use_cache: Optional[bool] = False,
|
| 100 |
+
output_attentions: Optional[bool] = False,
|
| 101 |
+
**kwargs
|
| 102 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]:
|
| 103 |
+
if attention_mask is not None:
|
| 104 |
+
assert len(attention_mask.shape) == 2, (
|
| 105 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 106 |
+
"for padding purposes (0 indicating padding). "
|
| 107 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
batch_size, seq_len, hidden_size = hidden_states.shape
|
| 111 |
+
# launching the triton kernel for just one token will actually be slower
|
| 112 |
+
mode = 'fused_recurrent' if hidden_states.shape[1] <= 64 else self.mode
|
| 113 |
+
|
| 114 |
+
last_state = None
|
| 115 |
+
if past_key_values is not None and len(past_key_values) > self.layer_idx:
|
| 116 |
+
last_state = past_key_values[self.layer_idx]
|
| 117 |
+
|
| 118 |
+
if attention_mask is not None:
|
| 119 |
+
hidden_states = hidden_states.mul_(attention_mask[:, -hidden_states.shape[-2]:, None])
|
| 120 |
+
if hidden_states.shape[1] == 1 and last_state is not None:
|
| 121 |
+
shifted = last_state['conv_state'].unsqueeze(1)
|
| 122 |
+
else:
|
| 123 |
+
shifted = self.time_shift(hidden_states)
|
| 124 |
+
if last_state is not None:
|
| 125 |
+
shifted[:, 0] = last_state['conv_state']
|
| 126 |
+
|
| 127 |
+
delta = shifted - hidden_states
|
| 128 |
+
x = self.x_proj[0](hidden_states, delta).view(batch_size, seq_len, -1, self.proj_low_rank_dim)
|
| 129 |
+
x = torch.einsum('b t n r, h n r-> b t n h', self.x_proj[1](x), self.x_proj[2].weight.view(hidden_size, 5, -1))
|
| 130 |
+
|
| 131 |
+
r, w, k, v, g = x.add_(self.x_bias).unbind(-2)
|
| 132 |
+
r = self.r_proj(hidden_states, r, delta)
|
| 133 |
+
w = self.w_proj(hidden_states, w, delta)
|
| 134 |
+
k = self.k_proj(hidden_states, k, delta)
|
| 135 |
+
v = self.v_proj(hidden_states, v, delta)
|
| 136 |
+
g = self.g_proj(hidden_states, g, delta)
|
| 137 |
+
|
| 138 |
+
# dealing with left-padding
|
| 139 |
+
if attention_mask is not None:
|
| 140 |
+
v = v.mul_(attention_mask[:, -v.shape[-2]:, None])
|
| 141 |
+
r, w, k = map(lambda x: rearrange(x, 'b t (h d) -> b t h d', d=self.head_k_dim), (r, w, k))
|
| 142 |
+
v = rearrange(v, 'b t (h d) -> b t h d', d=self.head_v_dim)
|
| 143 |
+
w = -torch.exp(w)
|
| 144 |
+
u = self.bonus
|
| 145 |
+
|
| 146 |
+
recurrent_state = last_state['recurrent_state'] if last_state is not None else None
|
| 147 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 148 |
+
if mode == 'fused_recurrent':
|
| 149 |
+
o, recurrent_state = fused_recurrent_rwkv6(
|
| 150 |
+
r=r,
|
| 151 |
+
k=k,
|
| 152 |
+
v=v,
|
| 153 |
+
w=w,
|
| 154 |
+
u=u,
|
| 155 |
+
scale=1.,
|
| 156 |
+
initial_state=recurrent_state,
|
| 157 |
+
output_final_state=use_cache,
|
| 158 |
+
cu_seqlens=cu_seqlens,
|
| 159 |
+
head_first=False
|
| 160 |
+
)
|
| 161 |
+
elif mode == 'chunk':
|
| 162 |
+
o, recurrent_state = chunk_rwkv6(
|
| 163 |
+
q=r,
|
| 164 |
+
k=k,
|
| 165 |
+
v=v,
|
| 166 |
+
g=w,
|
| 167 |
+
u=u,
|
| 168 |
+
scale=1.,
|
| 169 |
+
initial_state=recurrent_state,
|
| 170 |
+
output_final_state=use_cache,
|
| 171 |
+
cu_seqlens=cu_seqlens,
|
| 172 |
+
head_first=False
|
| 173 |
+
)
|
| 174 |
+
else:
|
| 175 |
+
raise NotImplementedError(f"Not supported mode `{mode}`.")
|
| 176 |
+
|
| 177 |
+
if past_key_values is not None:
|
| 178 |
+
past_key_values.update(
|
| 179 |
+
recurrent_state=recurrent_state,
|
| 180 |
+
conv_state=hidden_states[:, -1],
|
| 181 |
+
layer_idx=self.layer_idx,
|
| 182 |
+
offset=r.shape[2]
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
o = self.g_norm(rearrange(o, '... h d -> ... (h d)')) * self.gate_fn(g)
|
| 186 |
+
o = self.o_proj(o)
|
| 187 |
+
|
| 188 |
+
return o, None, past_key_values
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class LoRA(nn.Module):
|
| 192 |
+
|
| 193 |
+
def __init__(
|
| 194 |
+
self,
|
| 195 |
+
input_dim: int,
|
| 196 |
+
output_dim: int,
|
| 197 |
+
low_rank_dim: int,
|
| 198 |
+
bias: Optional[bool] = True,
|
| 199 |
+
activation: Optional[str] = 'tanh'
|
| 200 |
+
):
|
| 201 |
+
super().__init__()
|
| 202 |
+
|
| 203 |
+
self.input_dim = input_dim
|
| 204 |
+
self.output_dim = output_dim
|
| 205 |
+
self.low_rank_dim = low_rank_dim
|
| 206 |
+
self.bias = bias
|
| 207 |
+
|
| 208 |
+
if activation is None:
|
| 209 |
+
self.activation = nn.Identity()
|
| 210 |
+
elif activation == 'sigmoid':
|
| 211 |
+
self.activation = nn.Sigmoid()
|
| 212 |
+
elif activation == 'tanh':
|
| 213 |
+
self.activation = nn.Tanh()
|
| 214 |
+
elif activation == 'relu':
|
| 215 |
+
self.activation = nn.ReLU()
|
| 216 |
+
else:
|
| 217 |
+
raise ValueError(f"Not supported activation `{activation}`.")
|
| 218 |
+
|
| 219 |
+
self.lora = nn.Sequential(
|
| 220 |
+
nn.Linear(input_dim, low_rank_dim, bias=False),
|
| 221 |
+
self.activation,
|
| 222 |
+
nn.Linear(low_rank_dim, output_dim, bias=bias)
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
def __repr__(self) -> str:
|
| 226 |
+
s = f"{self.__class__.__name__}("
|
| 227 |
+
s += f"input_dim={self.input_dim}, low_rank_dim={self.low_rank_dim}, output_dim={self.output_dim}"
|
| 228 |
+
if not self.bias:
|
| 229 |
+
s += f", bias={self.bias}"
|
| 230 |
+
s += ")"
|
| 231 |
+
return s
|
| 232 |
+
|
| 233 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 234 |
+
return self.lora(x)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
class LerpLinear(nn.Module):
|
| 238 |
+
|
| 239 |
+
def __init__(
|
| 240 |
+
self,
|
| 241 |
+
input_dim: int,
|
| 242 |
+
output_dim: int,
|
| 243 |
+
low_rank_dim: Optional[int] = None
|
| 244 |
+
):
|
| 245 |
+
super().__init__()
|
| 246 |
+
|
| 247 |
+
self.input_dim = input_dim
|
| 248 |
+
self.output_dim = output_dim
|
| 249 |
+
self.low_rank_dim = low_rank_dim
|
| 250 |
+
|
| 251 |
+
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
|
| 252 |
+
if low_rank_dim is None:
|
| 253 |
+
self.linear = nn.Linear(input_dim, output_dim, bias=False)
|
| 254 |
+
else:
|
| 255 |
+
self.linear = LoRA(input_dim, output_dim, low_rank_dim)
|
| 256 |
+
self.mu = nn.Parameter(torch.zeros(input_dim))
|
| 257 |
+
|
| 258 |
+
def __repr__(self) -> str:
|
| 259 |
+
s = f"{self.__class__.__name__}({self.input_dim}, {self.output_dim}"
|
| 260 |
+
if self.low_rank_dim is not None:
|
| 261 |
+
s += f", low_rank_dim={self.low_rank_dim}"
|
| 262 |
+
s += ")"
|
| 263 |
+
return s
|
| 264 |
+
|
| 265 |
+
def forward(self, x: torch.Tensor, delta: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 266 |
+
if delta is None:
|
| 267 |
+
shifted = self.time_shift(x)
|
| 268 |
+
if len(shifted.shape) == 2:
|
| 269 |
+
shifted = shifted.unsqueeze(1)
|
| 270 |
+
delta = shifted - x
|
| 271 |
+
return self.linear(x + delta * self.mu)
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
class DDLerpLinear(nn.Module):
|
| 275 |
+
|
| 276 |
+
def __init__(
|
| 277 |
+
self,
|
| 278 |
+
input_dim: int,
|
| 279 |
+
output_dim: int,
|
| 280 |
+
low_rank_dim: Optional[int] = None
|
| 281 |
+
):
|
| 282 |
+
super().__init__()
|
| 283 |
+
|
| 284 |
+
self.input_dim = input_dim
|
| 285 |
+
self.output_dim = output_dim
|
| 286 |
+
self.low_rank_dim = low_rank_dim
|
| 287 |
+
|
| 288 |
+
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
|
| 289 |
+
if low_rank_dim is None:
|
| 290 |
+
self.linear = nn.Linear(input_dim, output_dim, bias=False)
|
| 291 |
+
else:
|
| 292 |
+
self.linear = LoRA(input_dim, output_dim, low_rank_dim)
|
| 293 |
+
|
| 294 |
+
def __repr__(self) -> str:
|
| 295 |
+
s = f"{self.__class__.__name__}({self.input_dim}, {self.output_dim}"
|
| 296 |
+
if self.low_rank_dim is not None:
|
| 297 |
+
s += f", low_rank_dim={self.low_rank_dim}"
|
| 298 |
+
s += ")"
|
| 299 |
+
return s
|
| 300 |
+
|
| 301 |
+
def forward(self, x: torch.Tensor, mu: torch.Tensor, delta: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 302 |
+
if delta is None:
|
| 303 |
+
shifted = self.time_shift(x)
|
| 304 |
+
if len(shifted.shape) == 2:
|
| 305 |
+
shifted = shifted.unsqueeze(1)
|
| 306 |
+
delta = shifted - x
|
| 307 |
+
return self.linear(x + delta * mu)
|
fla/layers/simple_gla.py
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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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 |
+
import torch.nn.functional as F
|
| 11 |
+
from einops import rearrange, repeat
|
| 12 |
+
|
| 13 |
+
from fla.modules import FusedRMSNormGated, RMSNorm, ShortConvolution
|
| 14 |
+
from fla.modules.activations import ACT2FN
|
| 15 |
+
from fla.ops.simple_gla import chunk_simple_gla, fused_recurrent_simple_gla
|
| 16 |
+
|
| 17 |
+
if TYPE_CHECKING:
|
| 18 |
+
from fla.models.utils import Cache
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class SimpleGatedLinearAttention(nn.Module):
|
| 22 |
+
r"""
|
| 23 |
+
The layer implementaion for [Gated Linear Attention Transformers with Hardware-Efficient Training](https://arxiv.org/abs/2312.06635). # noqa
|
| 24 |
+
This layer calls the simplified GLA kernel in which the gating is head-wise instead of elementwise.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
mode (str, Optional):
|
| 28 |
+
Which GLA kernel to use.
|
| 29 |
+
Currently available: `chunk`.
|
| 30 |
+
Default: `chunk`.
|
| 31 |
+
hidden_size (int, Optional):
|
| 32 |
+
The hidden size of the input. Default: 1024.
|
| 33 |
+
expand_k (float, Optional):
|
| 34 |
+
The expansion ratio for the key dim. Default: 1.0.
|
| 35 |
+
expand_v (float, Optional):
|
| 36 |
+
The expansion ratio for the value dim. Default: 1.0.
|
| 37 |
+
num_heads (int, Optional):
|
| 38 |
+
The number of heads. Default: 4.
|
| 39 |
+
num_kv_heads (int, Optional):
|
| 40 |
+
The number of key/value heads, used for MQA. Default: None.
|
| 41 |
+
feature_map (str, Optional):
|
| 42 |
+
Feature map function applied to queries/keys. Default: None.
|
| 43 |
+
use_short_conv (bool, Optional):
|
| 44 |
+
Whether to use short convolutions. Default: `False`.
|
| 45 |
+
conv_size (int, Optional):
|
| 46 |
+
The kernel size of the short convolution, only used when `use_short_conv` is `True`. Default: 4.
|
| 47 |
+
conv_bias (bool, Optional):
|
| 48 |
+
Whether to use bias in the short convolution, only used when `use_short_conv` is `True`. Default: `False`.
|
| 49 |
+
gate_fn (str, Optional):
|
| 50 |
+
The activation function for the output gate. Default: `swish`.
|
| 51 |
+
elementwise_affine (bool, Optional):
|
| 52 |
+
If `True`, applies elementwise affine to LayerNorm with learnable parameters. Default: `True`.
|
| 53 |
+
norm_eps (float, Optional):
|
| 54 |
+
The epsilon value for the layernorm/rmsnorm layer. Default: 1e-5.
|
| 55 |
+
gate_logit_normalizer (int, Optional):
|
| 56 |
+
The normalizer for the gate logits, appied after `logsigmoid`. Default: 16.
|
| 57 |
+
fuse_norm (bool, Optional):
|
| 58 |
+
Whether to fuse the norm and the output gate for better memory footprint. Default: `True`.
|
| 59 |
+
layer_idx (int, Optional):
|
| 60 |
+
The index of the layer. Default: None.
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
def __init__(
|
| 64 |
+
self,
|
| 65 |
+
mode: str = 'chunk',
|
| 66 |
+
hidden_size: int = 1024,
|
| 67 |
+
expand_k: float = 1.,
|
| 68 |
+
expand_v: float = 1.,
|
| 69 |
+
num_heads: int = 4,
|
| 70 |
+
num_kv_heads: Optional[int] = None,
|
| 71 |
+
feature_map: Optional[str] = None,
|
| 72 |
+
use_short_conv: bool = True,
|
| 73 |
+
conv_size: int = 4,
|
| 74 |
+
conv_bias: bool = False,
|
| 75 |
+
gate_fn: str = 'swish',
|
| 76 |
+
elementwise_affine: Optional[bool] = True,
|
| 77 |
+
norm_eps: float = 1e-5,
|
| 78 |
+
gate_logit_normalizer: int = 16,
|
| 79 |
+
fuse_norm: bool = True,
|
| 80 |
+
layer_idx: int = None,
|
| 81 |
+
) -> SimpleGatedLinearAttention:
|
| 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 |
+
|
| 97 |
+
self.key_dim = int(hidden_size * expand_k)
|
| 98 |
+
self.value_dim = int(hidden_size * expand_v)
|
| 99 |
+
self.key_dim_per_group = self.key_dim // self.num_kv_groups
|
| 100 |
+
self.value_dim_per_group = self.value_dim // self.num_kv_groups
|
| 101 |
+
self.layer_idx = layer_idx
|
| 102 |
+
|
| 103 |
+
assert mode in ['chunk', "fused_recurrent"], f"Not suppoerted mode `{mode}`."
|
| 104 |
+
assert self.key_dim % num_heads == 0, f"key dim must be divisible by num_heads of {num_heads}"
|
| 105 |
+
assert self.value_dim % num_heads == 0, f"value dim must be divisible by num_heads of {num_heads}"
|
| 106 |
+
|
| 107 |
+
self.head_k_dim = self.key_dim // num_heads
|
| 108 |
+
self.head_v_dim = self.value_dim // num_heads
|
| 109 |
+
|
| 110 |
+
self.q_proj = nn.Linear(hidden_size, self.key_dim, bias=False)
|
| 111 |
+
self.k_proj = nn.Linear(hidden_size, self.key_dim_per_group, bias=False)
|
| 112 |
+
self.v_proj = nn.Linear(hidden_size, self.value_dim_per_group, bias=False)
|
| 113 |
+
self.g_proj = nn.Linear(hidden_size, self.value_dim, bias=False)
|
| 114 |
+
|
| 115 |
+
if use_short_conv:
|
| 116 |
+
self.conv_size = conv_size
|
| 117 |
+
self.q_conv1d = ShortConvolution(self.key_dim, conv_size, activation='silu')
|
| 118 |
+
self.k_conv1d = ShortConvolution(self.key_dim_per_group, conv_size, activation='silu')
|
| 119 |
+
self.v_conv1d = ShortConvolution(self.value_dim_per_group, conv_size, activation='silu')
|
| 120 |
+
|
| 121 |
+
self.gk_proj = nn.Linear(hidden_size, self.num_heads)
|
| 122 |
+
|
| 123 |
+
if gate_fn == 'swish' and fuse_norm:
|
| 124 |
+
self.g_norm_swish_gate = FusedRMSNormGated(
|
| 125 |
+
hidden_size=self.head_v_dim,
|
| 126 |
+
elementwise_affine=elementwise_affine,
|
| 127 |
+
eps=norm_eps
|
| 128 |
+
)
|
| 129 |
+
self.fuse_norm_and_gate = True
|
| 130 |
+
else:
|
| 131 |
+
self.fuse_norm_and_gate = False
|
| 132 |
+
self.g_norm = RMSNorm(
|
| 133 |
+
hidden_size=self.head_v_dim,
|
| 134 |
+
elementwise_affine=elementwise_affine,
|
| 135 |
+
eps=norm_eps
|
| 136 |
+
)
|
| 137 |
+
self.gate_fn = ACT2FN[gate_fn]
|
| 138 |
+
self.o_proj = nn.Linear(self.value_dim, hidden_size, bias=False)
|
| 139 |
+
|
| 140 |
+
self.gate_logit_normalizer = gate_logit_normalizer
|
| 141 |
+
|
| 142 |
+
def forward(
|
| 143 |
+
self,
|
| 144 |
+
hidden_states: torch.Tensor,
|
| 145 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 146 |
+
past_key_values: Optional[Cache] = None,
|
| 147 |
+
use_cache: Optional[bool] = False,
|
| 148 |
+
output_attentions: Optional[bool] = False,
|
| 149 |
+
**kwargs
|
| 150 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]:
|
| 151 |
+
if attention_mask is not None:
|
| 152 |
+
assert len(attention_mask.shape) == 2, (
|
| 153 |
+
"Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
|
| 154 |
+
"for padding purposes (0 indicating padding). "
|
| 155 |
+
"Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# launching the triton kernel for just one token will actually be slower
|
| 159 |
+
mode = 'fused_recurrent' if hidden_states.shape[1] <= 64 else self.mode
|
| 160 |
+
|
| 161 |
+
last_state = None
|
| 162 |
+
if past_key_values is not None and len(past_key_values) > self.layer_idx:
|
| 163 |
+
last_state = past_key_values[self.layer_idx]
|
| 164 |
+
|
| 165 |
+
cu_seqlens = kwargs.get('cu_seqlens', None)
|
| 166 |
+
if self.use_short_conv:
|
| 167 |
+
conv_state_q, conv_state_k, conv_state_v = None, None, None
|
| 168 |
+
if last_state is not None:
|
| 169 |
+
conv_state_q, conv_state_k, conv_state_v = last_state['conv_state']
|
| 170 |
+
conv_mask = attention_mask[:, -hidden_states.shape[1]:] if attention_mask is not None else None
|
| 171 |
+
q, conv_state_q = self.q_conv1d(
|
| 172 |
+
x=self.q_proj(hidden_states),
|
| 173 |
+
mask=conv_mask,
|
| 174 |
+
cache=conv_state_q,
|
| 175 |
+
output_final_state=use_cache,
|
| 176 |
+
cu_seqlens=cu_seqlens
|
| 177 |
+
)
|
| 178 |
+
k, conv_state_k = self.k_conv1d(
|
| 179 |
+
x=self.k_proj(hidden_states),
|
| 180 |
+
mask=conv_mask,
|
| 181 |
+
cache=conv_state_k,
|
| 182 |
+
output_final_state=use_cache,
|
| 183 |
+
cu_seqlens=cu_seqlens
|
| 184 |
+
)
|
| 185 |
+
v, conv_state_v = self.v_conv1d(
|
| 186 |
+
x=self.v_proj(hidden_states),
|
| 187 |
+
mask=conv_mask,
|
| 188 |
+
cache=conv_state_v,
|
| 189 |
+
output_final_state=use_cache,
|
| 190 |
+
cu_seqlens=cu_seqlens
|
| 191 |
+
)
|
| 192 |
+
else:
|
| 193 |
+
q = self.q_proj(hidden_states)
|
| 194 |
+
k = self.k_proj(hidden_states)
|
| 195 |
+
v = self.v_proj(hidden_states)
|
| 196 |
+
gk = self.gk_proj(hidden_states)
|
| 197 |
+
|
| 198 |
+
if self.feature_map_fn is not None:
|
| 199 |
+
q, k = map(self.feature_map_fn, (q, k))
|
| 200 |
+
# dealing with left-padding
|
| 201 |
+
if attention_mask is not None:
|
| 202 |
+
v = v.mul_(attention_mask[:, -v.shape[-2]:, None])
|
| 203 |
+
q = rearrange(q, '... (h d) -> ... h d', h=self.num_heads)
|
| 204 |
+
if self.num_kv_groups > 1:
|
| 205 |
+
k, v = (repeat(x, '... (h d) -> ... (h g) d', h=self.num_kv_heads, g=self.num_kv_groups) for x in (k, v))
|
| 206 |
+
else:
|
| 207 |
+
k, v = (rearrange(x, '... (h d) -> ... h d', h=self.num_kv_heads) for x in (k, v))
|
| 208 |
+
gk = F.logsigmoid(gk) / self.gate_logit_normalizer
|
| 209 |
+
|
| 210 |
+
recurrent_state = last_state['recurrent_state'] if last_state is not None else None
|
| 211 |
+
if mode == 'chunk':
|
| 212 |
+
o, recurrent_state = chunk_simple_gla(
|
| 213 |
+
q=q,
|
| 214 |
+
k=k,
|
| 215 |
+
v=v,
|
| 216 |
+
gk=gk,
|
| 217 |
+
initial_state=recurrent_state,
|
| 218 |
+
output_final_state=use_cache,
|
| 219 |
+
cu_seqlens=cu_seqlens,
|
| 220 |
+
head_first=False
|
| 221 |
+
)
|
| 222 |
+
elif mode == 'fused_recurrent':
|
| 223 |
+
o, recurrent_state = fused_recurrent_simple_gla(
|
| 224 |
+
q=q,
|
| 225 |
+
k=k,
|
| 226 |
+
v=v,
|
| 227 |
+
gk=gk,
|
| 228 |
+
initial_state=recurrent_state,
|
| 229 |
+
output_final_state=use_cache,
|
| 230 |
+
cu_seqlens=cu_seqlens,
|
| 231 |
+
head_first=False
|
| 232 |
+
)
|
| 233 |
+
else:
|
| 234 |
+
raise NotImplementedError(f"Not supported mode `{mode}`.")
|
| 235 |
+
|
| 236 |
+
if past_key_values is not None:
|
| 237 |
+
past_key_values.update(
|
| 238 |
+
recurrent_state=recurrent_state,
|
| 239 |
+
conv_state=(conv_state_q, conv_state_k, conv_state_v) if self.use_short_conv else None,
|
| 240 |
+
layer_idx=self.layer_idx,
|
| 241 |
+
offset=q.shape[1]
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
g = self.g_proj(hidden_states)
|
| 245 |
+
if self.fuse_norm_and_gate:
|
| 246 |
+
g = rearrange(g, 'b t (h d) -> b t h d', h=self.num_heads)
|
| 247 |
+
o = self.g_norm_swish_gate(o, g)
|
| 248 |
+
o = rearrange(o, 'b t h d -> b t (h d)')
|
| 249 |
+
else:
|
| 250 |
+
o = rearrange(self.g_norm(o), 'b t h d -> b t (h d)')
|
| 251 |
+
o = o * self.gate_fn(g)
|
| 252 |
+
o = self.o_proj(o)
|
| 253 |
+
|
| 254 |
+
return o, None, past_key_values
|
| 255 |
+
|
| 256 |
+
def state_size(self, **kwargs) -> int:
|
| 257 |
+
state_size = self.key_dim * self.head_v_dim
|
| 258 |
+
for module in self.children():
|
| 259 |
+
if isinstance(module, ShortConvolution):
|
| 260 |
+
state_size += module.state_size
|
| 261 |
+
return state_size
|
fla/models/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (3.1 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/abc/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (689 Bytes). View file
|
|
|
fla/models/abc/__pycache__/configuration_abc.cpython-312.pyc
ADDED
|
Binary file (3.64 kB). View file
|
|
|
fla/models/abc/__pycache__/modeling_abc.cpython-312.pyc
ADDED
|
Binary file (18.4 kB). View file
|
|
|
fla/models/abc/configuration_abc.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 ABCConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'abc'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
hidden_size: int = 2048,
|
| 16 |
+
gate_low_rank_dim: int = 16,
|
| 17 |
+
clamp_min: float = -32,
|
| 18 |
+
clamp_max: float = 32,
|
| 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_slots: Optional[int] = 64,
|
| 24 |
+
use_short_conv: bool = False,
|
| 25 |
+
conv_size: int = 4,
|
| 26 |
+
exapnd_k: float = 0.5,
|
| 27 |
+
exapnd_v: float = 1,
|
| 28 |
+
hidden_act: str = "swish",
|
| 29 |
+
max_position_embeddings: int = 2048,
|
| 30 |
+
elementwise_affine: Optional[bool] = True,
|
| 31 |
+
norm_eps: float = 1e-6,
|
| 32 |
+
use_rope: bool = True,
|
| 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.hidden_size = hidden_size
|
| 47 |
+
self.gate_low_rank_dim = gate_low_rank_dim
|
| 48 |
+
self.clamp_min = clamp_min
|
| 49 |
+
self.clamp_max = clamp_max
|
| 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_slots = num_slots
|
| 55 |
+
self.use_short_conv = use_short_conv
|
| 56 |
+
self.conv_size = conv_size
|
| 57 |
+
self.expand_k = exapnd_k
|
| 58 |
+
self.expand_v = exapnd_v
|
| 59 |
+
self.hidden_act = hidden_act
|
| 60 |
+
self.max_position_embeddings = max_position_embeddings
|
| 61 |
+
self.elementwise_affine = elementwise_affine
|
| 62 |
+
self.norm_eps = norm_eps
|
| 63 |
+
self.use_rope = use_rope
|
| 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/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/bitnet/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (714 Bytes). View file
|
|
|
fla/models/bitnet/__pycache__/configuration_bitnet.cpython-312.pyc
ADDED
|
Binary file (2.4 kB). View file
|
|
|
fla/models/bitnet/configuration_bitnet.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BitNetConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'bitnet'
|
| 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 |
+
window_size: Optional[int] = None,
|
| 20 |
+
rope_theta: Optional[float] = 10000.,
|
| 21 |
+
max_position_embeddings: int = 2048,
|
| 22 |
+
hidden_ratio: Optional[int] = 4,
|
| 23 |
+
intermediate_size: Optional[int] = None,
|
| 24 |
+
hidden_act: str = "swish",
|
| 25 |
+
initializer_range: float = 0.006,
|
| 26 |
+
elementwise_affine: Optional[bool] = True,
|
| 27 |
+
norm_eps: float = 1e-6,
|
| 28 |
+
use_cache: bool = True,
|
| 29 |
+
pad_token_id: int = None,
|
| 30 |
+
bos_token_id: int = 1,
|
| 31 |
+
eos_token_id: int = 2,
|
| 32 |
+
tie_word_embeddings: bool = False,
|
| 33 |
+
fuse_norm: bool = True,
|
| 34 |
+
fuse_swiglu: bool = True,
|
| 35 |
+
fuse_cross_entropy: bool = True,
|
| 36 |
+
vocab_size: int = 32000,
|
| 37 |
+
**kwargs,
|
| 38 |
+
):
|
| 39 |
+
self.hidden_size = hidden_size
|
| 40 |
+
self.num_hidden_layers = num_hidden_layers
|
| 41 |
+
self.num_heads = num_heads
|
| 42 |
+
self.num_kv_heads = num_kv_heads
|
| 43 |
+
self.window_size = window_size
|
| 44 |
+
self.rope_theta = rope_theta
|
| 45 |
+
self.max_position_embeddings = max_position_embeddings
|
| 46 |
+
|
| 47 |
+
self.hidden_ratio = hidden_ratio
|
| 48 |
+
self.intermediate_size = intermediate_size
|
| 49 |
+
self.hidden_act = hidden_act
|
| 50 |
+
|
| 51 |
+
self.initializer_range = initializer_range
|
| 52 |
+
self.elementwise_affine = elementwise_affine
|
| 53 |
+
self.norm_eps = norm_eps
|
| 54 |
+
self.use_cache = use_cache
|
| 55 |
+
|
| 56 |
+
self.fuse_norm = fuse_norm
|
| 57 |
+
self.fuse_swiglu = fuse_swiglu
|
| 58 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 59 |
+
self.vocab_size = vocab_size
|
| 60 |
+
|
| 61 |
+
super().__init__(
|
| 62 |
+
pad_token_id=pad_token_id,
|
| 63 |
+
bos_token_id=bos_token_id,
|
| 64 |
+
eos_token_id=eos_token_id,
|
| 65 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 66 |
+
**kwargs,
|
| 67 |
+
)
|
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/delta_net/configuration_delta_net.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 DeltaNetConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'delta_net'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
attn_mode: str = "chunk",
|
| 16 |
+
hidden_size: int = 2048,
|
| 17 |
+
expand_k: int = 1,
|
| 18 |
+
expand_v: int = 1,
|
| 19 |
+
use_gate: bool = False,
|
| 20 |
+
use_short_conv: bool = True,
|
| 21 |
+
conv_size: int = 4,
|
| 22 |
+
use_beta: bool = True,
|
| 23 |
+
use_output_norm: bool = True,
|
| 24 |
+
num_heads: int = 16,
|
| 25 |
+
qk_norm: str = 'l2',
|
| 26 |
+
qk_activation: str = 'silu',
|
| 27 |
+
max_position_embeddings: int = 2048,
|
| 28 |
+
hidden_ratio: Optional[int] = 4,
|
| 29 |
+
intermediate_size: Optional[int] = None,
|
| 30 |
+
hidden_act: str = "swish",
|
| 31 |
+
num_hidden_layers: int = 24,
|
| 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.use_gate = use_gate
|
| 51 |
+
self.use_short_conv = use_short_conv
|
| 52 |
+
self.conv_size = conv_size
|
| 53 |
+
self.use_beta = use_beta
|
| 54 |
+
self.use_output_norm = use_output_norm
|
| 55 |
+
self.num_heads = num_heads
|
| 56 |
+
self.qk_norm = qk_norm
|
| 57 |
+
self.qk_activation = qk_activation
|
| 58 |
+
self.max_position_embeddings = max_position_embeddings
|
| 59 |
+
|
| 60 |
+
self.hidden_ratio = hidden_ratio
|
| 61 |
+
self.intermediate_size = intermediate_size
|
| 62 |
+
self.hidden_act = hidden_act
|
| 63 |
+
self.num_hidden_layers = num_hidden_layers
|
| 64 |
+
self.norm_eps = norm_eps
|
| 65 |
+
self.attn = attn
|
| 66 |
+
self.use_cache = use_cache
|
| 67 |
+
self.initializer_range = initializer_range
|
| 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/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/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (849 Bytes). View file
|
|
|
fla/models/forgetting_transformer/__pycache__/modeling_forgetting_transformer.cpython-312.pyc
ADDED
|
Binary file (17.3 kB). View file
|
|
|
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/forgetting_transformer/modeling_forgetting_transformer.py
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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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.forgetting_attn import ForgettingAttention
|
| 19 |
+
from fla.models.forgetting_transformer.configuration_forgetting_transformer import ForgettingTransformerConfig
|
| 20 |
+
from fla.models.utils import Cache
|
| 21 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 22 |
+
from fla.modules import GatedMLP as ForgettingTransformerMLP
|
| 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 ForgettingTransformerBlock(nn.Module):
|
| 33 |
+
|
| 34 |
+
def __init__(self, config: ForgettingTransformerConfig, 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 = ForgettingAttention(
|
| 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 |
+
use_output_gate=config.use_output_gate,
|
| 49 |
+
layer_idx=layer_idx
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 53 |
+
self.mlp = ForgettingTransformerMLP(
|
| 54 |
+
hidden_size=config.hidden_size,
|
| 55 |
+
hidden_ratio=config.hidden_ratio,
|
| 56 |
+
intermediate_size=config.intermediate_size,
|
| 57 |
+
hidden_act=config.hidden_act,
|
| 58 |
+
fuse_swiglu=config.fuse_swiglu
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
def forward(
|
| 62 |
+
self,
|
| 63 |
+
hidden_states: torch.Tensor,
|
| 64 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 65 |
+
past_key_values: Optional[Tuple[torch.Tensor]] = None,
|
| 66 |
+
output_attentions: Optional[bool] = False,
|
| 67 |
+
use_cache: Optional[bool] = False,
|
| 68 |
+
**kwargs: Unpack[Any]
|
| 69 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 70 |
+
|
| 71 |
+
residual = hidden_states
|
| 72 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 73 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 74 |
+
hidden_states=hidden_states,
|
| 75 |
+
attention_mask=attention_mask,
|
| 76 |
+
past_key_values=past_key_values,
|
| 77 |
+
use_cache=use_cache,
|
| 78 |
+
output_attentions=output_attentions,
|
| 79 |
+
**kwargs
|
| 80 |
+
)
|
| 81 |
+
if self.config.fuse_norm:
|
| 82 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 83 |
+
else:
|
| 84 |
+
hidden_states = residual + hidden_states
|
| 85 |
+
residual = hidden_states
|
| 86 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 87 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 88 |
+
hidden_states = residual + hidden_states
|
| 89 |
+
|
| 90 |
+
outputs = (hidden_states,)
|
| 91 |
+
|
| 92 |
+
if output_attentions:
|
| 93 |
+
outputs += (attentions,)
|
| 94 |
+
|
| 95 |
+
if use_cache:
|
| 96 |
+
outputs += (past_key_values,)
|
| 97 |
+
|
| 98 |
+
return outputs
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class ForgettingTransformerPreTrainedModel(PreTrainedModel):
|
| 102 |
+
|
| 103 |
+
config_class = ForgettingTransformerConfig
|
| 104 |
+
base_model_prefix = 'model'
|
| 105 |
+
supports_gradient_checkpointing = True
|
| 106 |
+
_no_split_modules = ['ForgettingTransformerBlock']
|
| 107 |
+
_supports_cache_class = True
|
| 108 |
+
|
| 109 |
+
def __init__(self, *inputs, **kwargs):
|
| 110 |
+
super().__init__(*inputs, **kwargs)
|
| 111 |
+
|
| 112 |
+
def _init_weights(
|
| 113 |
+
self,
|
| 114 |
+
module: nn.Module,
|
| 115 |
+
rescale_prenorm_residual: bool = False,
|
| 116 |
+
num_residuals_per_layer: int = 2,
|
| 117 |
+
):
|
| 118 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 119 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 120 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 121 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 122 |
+
if module.bias is not None:
|
| 123 |
+
nn.init.zeros_(module.bias)
|
| 124 |
+
elif isinstance(module, nn.Embedding):
|
| 125 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 126 |
+
elif hasattr(module, 'reset_parameters'):
|
| 127 |
+
module.reset_parameters()
|
| 128 |
+
|
| 129 |
+
if rescale_prenorm_residual:
|
| 130 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 131 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 132 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 133 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 134 |
+
#
|
| 135 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 136 |
+
p = None
|
| 137 |
+
if hasattr(module, 'o_proj'):
|
| 138 |
+
p = module.o_proj.weight
|
| 139 |
+
elif hasattr(module, 'down_proj'):
|
| 140 |
+
p = module.down_proj.weight
|
| 141 |
+
if p is not None:
|
| 142 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per ForgettingTransformer Block
|
| 143 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 144 |
+
# We need to reinit p since this code could be called multiple times
|
| 145 |
+
# Having just p *= scale would repeatedly scale it down
|
| 146 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 147 |
+
with torch.no_grad():
|
| 148 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
class ForgettingTransformerModel(ForgettingTransformerPreTrainedModel):
|
| 152 |
+
|
| 153 |
+
def __init__(
|
| 154 |
+
self,
|
| 155 |
+
config: ForgettingTransformerConfig
|
| 156 |
+
) -> ForgettingTransformerModel:
|
| 157 |
+
super().__init__(config)
|
| 158 |
+
self.padding_idx = config.pad_token_id
|
| 159 |
+
self.vocab_size = config.vocab_size
|
| 160 |
+
|
| 161 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 162 |
+
self.layers = nn.ModuleList([
|
| 163 |
+
ForgettingTransformerBlock(config, layer_idx)
|
| 164 |
+
for layer_idx in range(config.num_hidden_layers)
|
| 165 |
+
])
|
| 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,
|
| 182 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
| 183 |
+
inputs_embeds: Optional[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 |
+
**kwargs: Unpack[Any]
|
| 189 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 190 |
+
if output_attentions:
|
| 191 |
+
warnings.warn(
|
| 192 |
+
"`ForgettingTransformerModel` does not support output attention weights now, "
|
| 193 |
+
"so `output_attentions` is set to `False`."
|
| 194 |
+
)
|
| 195 |
+
output_attentions = False
|
| 196 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 197 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 198 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 199 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 200 |
+
|
| 201 |
+
# retrieve input_ids and inputs_embeds
|
| 202 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 203 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 204 |
+
elif input_ids is None and inputs_embeds is None:
|
| 205 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 206 |
+
|
| 207 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 208 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 209 |
+
|
| 210 |
+
if inputs_embeds is None:
|
| 211 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 212 |
+
|
| 213 |
+
# embed positions
|
| 214 |
+
hidden_states = inputs_embeds
|
| 215 |
+
|
| 216 |
+
if self.gradient_checkpointing and self.training:
|
| 217 |
+
if use_cache:
|
| 218 |
+
logger.warning_once(
|
| 219 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
| 220 |
+
)
|
| 221 |
+
use_cache = False
|
| 222 |
+
|
| 223 |
+
all_hidden_states = () if output_hidden_states else None
|
| 224 |
+
all_attns = () if output_attentions else None
|
| 225 |
+
next_cache = None
|
| 226 |
+
|
| 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 |
+
layer_outputs = self._gradient_checkpointing_func(
|
| 233 |
+
layer.__call__,
|
| 234 |
+
hidden_states,
|
| 235 |
+
attention_mask,
|
| 236 |
+
past_key_values,
|
| 237 |
+
output_attentions,
|
| 238 |
+
use_cache,
|
| 239 |
+
**kwargs
|
| 240 |
+
)
|
| 241 |
+
else:
|
| 242 |
+
layer_outputs = layer(
|
| 243 |
+
hidden_states,
|
| 244 |
+
attention_mask=attention_mask,
|
| 245 |
+
past_key_values=past_key_values,
|
| 246 |
+
output_attentions=output_attentions,
|
| 247 |
+
use_cache=use_cache,
|
| 248 |
+
**kwargs
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
hidden_states = layer_outputs[0]
|
| 252 |
+
|
| 253 |
+
if use_cache:
|
| 254 |
+
next_cache = layer_outputs[2 if output_attentions else 1]
|
| 255 |
+
|
| 256 |
+
if output_attentions:
|
| 257 |
+
all_attns += (layer_outputs[1],)
|
| 258 |
+
|
| 259 |
+
hidden_states = self.norm(hidden_states)
|
| 260 |
+
|
| 261 |
+
# add hidden states from the last decoder layer
|
| 262 |
+
if output_hidden_states:
|
| 263 |
+
all_hidden_states += (hidden_states,)
|
| 264 |
+
|
| 265 |
+
if not return_dict:
|
| 266 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_attns] if v is not None)
|
| 267 |
+
|
| 268 |
+
return BaseModelOutputWithPast(
|
| 269 |
+
last_hidden_state=hidden_states,
|
| 270 |
+
past_key_values=next_cache,
|
| 271 |
+
hidden_states=all_hidden_states,
|
| 272 |
+
attentions=all_attns
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
class ForgettingTransformerForCausalLM(ForgettingTransformerPreTrainedModel, GenerationMixin):
|
| 277 |
+
|
| 278 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 279 |
+
|
| 280 |
+
def __init__(self, config):
|
| 281 |
+
super().__init__(config)
|
| 282 |
+
self.model = ForgettingTransformerModel(config)
|
| 283 |
+
self.vocab_size = config.vocab_size
|
| 284 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 285 |
+
self.criterion = None
|
| 286 |
+
|
| 287 |
+
# Initialize weights and apply final processing
|
| 288 |
+
self.post_init()
|
| 289 |
+
|
| 290 |
+
def get_input_embeddings(self):
|
| 291 |
+
return self.model.embeddings
|
| 292 |
+
|
| 293 |
+
def set_input_embeddings(self, value):
|
| 294 |
+
self.model.embeddings = value
|
| 295 |
+
|
| 296 |
+
def get_output_embeddings(self):
|
| 297 |
+
return self.lm_head
|
| 298 |
+
|
| 299 |
+
def set_output_embeddings(self, new_embeddings):
|
| 300 |
+
self.lm_head = new_embeddings
|
| 301 |
+
|
| 302 |
+
def set_decoder(self, decoder):
|
| 303 |
+
self.model = decoder
|
| 304 |
+
|
| 305 |
+
def get_decoder(self):
|
| 306 |
+
return self.model
|
| 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 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 348 |
+
inputs_embeds: Optional[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 |
+
**kwargs: Unpack[Any]
|
| 356 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 357 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 358 |
+
output_hidden_states = (
|
| 359 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 360 |
+
)
|
| 361 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 362 |
+
|
| 363 |
+
outputs = self.model(
|
| 364 |
+
input_ids=input_ids,
|
| 365 |
+
attention_mask=attention_mask,
|
| 366 |
+
past_key_values=past_key_values,
|
| 367 |
+
inputs_embeds=inputs_embeds,
|
| 368 |
+
use_cache=use_cache,
|
| 369 |
+
output_attentions=output_attentions,
|
| 370 |
+
output_hidden_states=output_hidden_states,
|
| 371 |
+
return_dict=return_dict,
|
| 372 |
+
**kwargs
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
hidden_states = outputs[0]
|
| 376 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 377 |
+
logits = None if fuse_linear_and_cross_entropy else self.lm_head(hidden_states[:, -logits_to_keep:])
|
| 378 |
+
|
| 379 |
+
loss = None
|
| 380 |
+
if labels is not None:
|
| 381 |
+
if getattr(self, 'criterion', None) is None:
|
| 382 |
+
if fuse_linear_and_cross_entropy:
|
| 383 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 384 |
+
elif self.config.fuse_cross_entropy:
|
| 385 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 386 |
+
else:
|
| 387 |
+
criterion = nn.CrossEntropyLoss()
|
| 388 |
+
else:
|
| 389 |
+
criterion = self.criterion
|
| 390 |
+
# Enable model parallelism
|
| 391 |
+
labels = labels.to(hidden_states.device)
|
| 392 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 393 |
+
if fuse_linear_and_cross_entropy:
|
| 394 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 395 |
+
else:
|
| 396 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 397 |
+
|
| 398 |
+
if not return_dict:
|
| 399 |
+
output = (logits,) + outputs[1:]
|
| 400 |
+
return (loss,) + output if loss is not None else output
|
| 401 |
+
|
| 402 |
+
return CausalLMOutputWithPast(
|
| 403 |
+
loss=loss,
|
| 404 |
+
logits=logits,
|
| 405 |
+
past_key_values=outputs.past_key_values,
|
| 406 |
+
hidden_states=outputs.hidden_states,
|
| 407 |
+
attentions=outputs.attentions,
|
| 408 |
+
)
|
fla/models/gated_deltanet/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (778 Bytes). View file
|
|
|
fla/models/gated_deltanet/__pycache__/modeling_gated_deltanet.cpython-312.pyc
ADDED
|
Binary file (18.5 kB). View file
|
|
|
fla/models/gated_deltanet/modeling_gated_deltanet.py
ADDED
|
@@ -0,0 +1,412 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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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.gated_deltanet import GatedDeltaNet
|
| 20 |
+
from fla.models.gated_deltanet.configuration_gated_deltanet import GatedDeltaNetConfig
|
| 21 |
+
from fla.models.utils import Cache
|
| 22 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 23 |
+
from fla.modules import GatedMLP as GatedDeltaNetMLP
|
| 24 |
+
from fla.modules import RMSNorm
|
| 25 |
+
|
| 26 |
+
if TYPE_CHECKING:
|
| 27 |
+
from transformers.processing_utils import Unpack
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
logger = logging.get_logger(__name__)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class GatedDeltaNetBlock(nn.Module):
|
| 34 |
+
def __init__(self, config: GatedDeltaNetConfig, 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 |
+
if config.attn is not None and layer_idx in config.attn['layers']:
|
| 42 |
+
self.attn = Attention(
|
| 43 |
+
hidden_size=config.hidden_size,
|
| 44 |
+
num_heads=config.attn['num_heads'],
|
| 45 |
+
num_kv_heads=config.attn['num_kv_heads'],
|
| 46 |
+
qkv_bias=config.attn['qkv_bias'],
|
| 47 |
+
window_size=config.attn['window_size'],
|
| 48 |
+
rope_theta=config.attn['rope_theta'],
|
| 49 |
+
max_position_embeddings=config.max_position_embeddings,
|
| 50 |
+
layer_idx=layer_idx
|
| 51 |
+
)
|
| 52 |
+
else:
|
| 53 |
+
self.attn = GatedDeltaNet(
|
| 54 |
+
mode=config.attn_mode,
|
| 55 |
+
hidden_size=config.hidden_size,
|
| 56 |
+
expand_v=config.expand_v,
|
| 57 |
+
head_dim=config.head_dim,
|
| 58 |
+
num_heads=config.num_heads,
|
| 59 |
+
use_gate=config.use_gate,
|
| 60 |
+
use_short_conv=config.use_short_conv,
|
| 61 |
+
conv_size=config.conv_size,
|
| 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 = GatedDeltaNetMLP(
|
| 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: Unpack[Dict]
|
| 82 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 83 |
+
residual = hidden_states
|
| 84 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 85 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 86 |
+
hidden_states=hidden_states,
|
| 87 |
+
attention_mask=attention_mask,
|
| 88 |
+
past_key_values=past_key_values,
|
| 89 |
+
use_cache=use_cache,
|
| 90 |
+
output_attentions=output_attentions,
|
| 91 |
+
**kwargs
|
| 92 |
+
)
|
| 93 |
+
if self.config.fuse_norm:
|
| 94 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 95 |
+
else:
|
| 96 |
+
hidden_states = residual + hidden_states
|
| 97 |
+
residual = hidden_states
|
| 98 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 99 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 100 |
+
hidden_states = residual + hidden_states
|
| 101 |
+
|
| 102 |
+
outputs = (hidden_states, attentions, past_key_values)
|
| 103 |
+
|
| 104 |
+
return outputs
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class GatedDeltaNetPreTrainedModel(PreTrainedModel):
|
| 108 |
+
|
| 109 |
+
config_class = GatedDeltaNetConfig
|
| 110 |
+
base_model_prefix = 'model'
|
| 111 |
+
supports_gradient_checkpointing = True
|
| 112 |
+
_no_split_modules = ['GatedDeltaNetBlock']
|
| 113 |
+
_supports_cache_class = True
|
| 114 |
+
|
| 115 |
+
def __init__(self, *inputs, **kwargs):
|
| 116 |
+
super().__init__(*inputs, **kwargs)
|
| 117 |
+
|
| 118 |
+
def _init_weights(
|
| 119 |
+
self,
|
| 120 |
+
module: nn.Module,
|
| 121 |
+
prenorm_residual_strategy: Optional[str] = 'rescale',
|
| 122 |
+
num_residuals_per_layer: int = 2,
|
| 123 |
+
):
|
| 124 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 125 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 126 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 127 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 128 |
+
if module.bias is not None:
|
| 129 |
+
nn.init.zeros_(module.bias)
|
| 130 |
+
elif isinstance(module, nn.Embedding):
|
| 131 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 132 |
+
elif hasattr(module, 'reset_parameters'):
|
| 133 |
+
module.reset_parameters()
|
| 134 |
+
|
| 135 |
+
if prenorm_residual_strategy is not None:
|
| 136 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 137 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 138 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 139 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 140 |
+
#
|
| 141 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 142 |
+
p = None
|
| 143 |
+
if hasattr(module, 'o_proj'):
|
| 144 |
+
p = module.o_proj.weight
|
| 145 |
+
elif hasattr(module, 'down_proj'):
|
| 146 |
+
p = module.down_proj.weight
|
| 147 |
+
if p is not None:
|
| 148 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 149 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 150 |
+
# We need to reinit p since this code could be called multiple times
|
| 151 |
+
# Having just p *= scale would repeatedly scale it down
|
| 152 |
+
if prenorm_residual_strategy == 'rescale':
|
| 153 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 154 |
+
with torch.no_grad():
|
| 155 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 156 |
+
elif prenorm_residual_strategy == 'zero':
|
| 157 |
+
nn.init.zeros_(p)
|
| 158 |
+
else:
|
| 159 |
+
raise ValueError(f"Invalid prenorm_residual_strategy: {prenorm_residual_strategy}")
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
class GatedDeltaNetModel(GatedDeltaNetPreTrainedModel):
|
| 163 |
+
|
| 164 |
+
def __init__(self, config: GatedDeltaNetConfig):
|
| 165 |
+
super().__init__(config)
|
| 166 |
+
self.padding_idx = config.pad_token_id
|
| 167 |
+
self.vocab_size = config.vocab_size
|
| 168 |
+
|
| 169 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 170 |
+
self.layers = nn.ModuleList([GatedDeltaNetBlock(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 171 |
+
self.norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 172 |
+
|
| 173 |
+
self.gradient_checkpointing = False
|
| 174 |
+
|
| 175 |
+
self.post_init()
|
| 176 |
+
|
| 177 |
+
def get_input_embeddings(self):
|
| 178 |
+
return self.embeddings
|
| 179 |
+
|
| 180 |
+
def set_input_embeddings(self, value):
|
| 181 |
+
self.embeddings = value
|
| 182 |
+
|
| 183 |
+
def forward(
|
| 184 |
+
self,
|
| 185 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 186 |
+
attention_mask: Optional[torch.Tensor] = None, # noqa
|
| 187 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 188 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 189 |
+
use_cache: Optional[bool] = None,
|
| 190 |
+
output_attentions: Optional[bool] = None,
|
| 191 |
+
output_hidden_states: Optional[bool] = None,
|
| 192 |
+
return_dict: Optional[bool] = None,
|
| 193 |
+
**kwargs: Unpack[Dict]
|
| 194 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 195 |
+
if output_attentions:
|
| 196 |
+
warnings.warn("`GatedDeltaNetModel` does not `output_attentions` now, setting it to `False`.")
|
| 197 |
+
output_attentions = False
|
| 198 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 199 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 200 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 201 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 202 |
+
|
| 203 |
+
# retrieve input_ids and inputs_embeds
|
| 204 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 205 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 206 |
+
if input_ids is None and inputs_embeds is None:
|
| 207 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 208 |
+
|
| 209 |
+
if inputs_embeds is None:
|
| 210 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 211 |
+
hidden_states = inputs_embeds
|
| 212 |
+
|
| 213 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 214 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 215 |
+
|
| 216 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 217 |
+
logger.warning_once("`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...")
|
| 218 |
+
use_cache = False
|
| 219 |
+
|
| 220 |
+
all_hidden_states = () if output_hidden_states else None
|
| 221 |
+
all_attns = () if output_attentions else None
|
| 222 |
+
for layer in self.layers:
|
| 223 |
+
if output_hidden_states:
|
| 224 |
+
all_hidden_states += (hidden_states,)
|
| 225 |
+
|
| 226 |
+
if self.gradient_checkpointing and self.training:
|
| 227 |
+
hidden_states, attentions, past_key_values = self._gradient_checkpointing_func(
|
| 228 |
+
layer.__call__,
|
| 229 |
+
hidden_states,
|
| 230 |
+
attention_mask,
|
| 231 |
+
past_key_values,
|
| 232 |
+
use_cache,
|
| 233 |
+
output_attentions,
|
| 234 |
+
**kwargs
|
| 235 |
+
)
|
| 236 |
+
else:
|
| 237 |
+
hidden_states, attentions, past_key_values = layer(
|
| 238 |
+
hidden_states,
|
| 239 |
+
attention_mask=attention_mask,
|
| 240 |
+
past_key_values=past_key_values,
|
| 241 |
+
use_cache=use_cache,
|
| 242 |
+
output_attentions=output_attentions,
|
| 243 |
+
**kwargs
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
if output_attentions:
|
| 247 |
+
all_attns += (attentions,)
|
| 248 |
+
|
| 249 |
+
hidden_states = self.norm(hidden_states)
|
| 250 |
+
|
| 251 |
+
# add hidden states from the last decoder layer
|
| 252 |
+
if output_hidden_states:
|
| 253 |
+
all_hidden_states += (hidden_states,)
|
| 254 |
+
|
| 255 |
+
if not return_dict:
|
| 256 |
+
return tuple(i for i in [hidden_states, past_key_values, all_hidden_states, all_attns] if i is not None)
|
| 257 |
+
return BaseModelOutputWithPast(
|
| 258 |
+
last_hidden_state=hidden_states,
|
| 259 |
+
past_key_values=past_key_values,
|
| 260 |
+
hidden_states=all_hidden_states,
|
| 261 |
+
attentions=all_attns
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
class GatedDeltaNetForCausalLM(GatedDeltaNetPreTrainedModel, GenerationMixin):
|
| 266 |
+
|
| 267 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 268 |
+
|
| 269 |
+
def __init__(self, config):
|
| 270 |
+
super().__init__(config)
|
| 271 |
+
self.model = GatedDeltaNetModel(config)
|
| 272 |
+
self.vocab_size = config.vocab_size
|
| 273 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 274 |
+
self.criterion = None
|
| 275 |
+
|
| 276 |
+
# Initialize weights and apply final processing
|
| 277 |
+
self.post_init()
|
| 278 |
+
|
| 279 |
+
def get_input_embeddings(self):
|
| 280 |
+
return self.model.embeddings
|
| 281 |
+
|
| 282 |
+
def set_input_embeddings(self, value):
|
| 283 |
+
self.model.embeddings = value
|
| 284 |
+
|
| 285 |
+
def get_output_embeddings(self):
|
| 286 |
+
return self.lm_head
|
| 287 |
+
|
| 288 |
+
def set_output_embeddings(self, new_embeddings):
|
| 289 |
+
self.lm_head = new_embeddings
|
| 290 |
+
|
| 291 |
+
def set_decoder(self, decoder):
|
| 292 |
+
self.model = decoder
|
| 293 |
+
|
| 294 |
+
def get_decoder(self):
|
| 295 |
+
return self.model
|
| 296 |
+
|
| 297 |
+
def generate(self, *args, **kwargs):
|
| 298 |
+
try:
|
| 299 |
+
return super().generate(*args, **kwargs)
|
| 300 |
+
except AttributeError as exception:
|
| 301 |
+
if 'past_key_values' in str(exception):
|
| 302 |
+
raise AttributeError(
|
| 303 |
+
f"You tried to call `generate` with a decoding strategy that manipulates `past_key_values`, "
|
| 304 |
+
f"which is not supported for {self.__class__.__name__}. "
|
| 305 |
+
f"Try another generation strategy instead. "
|
| 306 |
+
f"For the available generation strategies, check this doc: "
|
| 307 |
+
f"https://huggingface.co/docs/transformers/en/generation_strategies#decoding-strategies"
|
| 308 |
+
)
|
| 309 |
+
else:
|
| 310 |
+
raise exception
|
| 311 |
+
|
| 312 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 313 |
+
def prepare_inputs_for_generation(
|
| 314 |
+
self,
|
| 315 |
+
input_ids: torch.LongTensor = None,
|
| 316 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 317 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 318 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 319 |
+
use_cache: bool = True,
|
| 320 |
+
logits_to_keep: Optional[int] = None,
|
| 321 |
+
**kwargs
|
| 322 |
+
):
|
| 323 |
+
# only last token for `inputs_ids` if the `past_key_values` is not empty.
|
| 324 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 325 |
+
input_ids = input_ids[:, -1:]
|
| 326 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 327 |
+
if inputs_embeds is not None and len(past_key_values) == 0:
|
| 328 |
+
model_inputs = {'inputs_embeds': inputs_embeds}
|
| 329 |
+
else:
|
| 330 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 331 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 332 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 333 |
+
# TODO: use `next_tokens` directly instead.
|
| 334 |
+
model_inputs = {'input_ids': input_ids.contiguous()}
|
| 335 |
+
|
| 336 |
+
if logits_to_keep is not None:
|
| 337 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 338 |
+
|
| 339 |
+
model_inputs.update({
|
| 340 |
+
'past_key_values': past_key_values,
|
| 341 |
+
'use_cache': use_cache,
|
| 342 |
+
'attention_mask': attention_mask,
|
| 343 |
+
})
|
| 344 |
+
return model_inputs
|
| 345 |
+
|
| 346 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 347 |
+
def forward(
|
| 348 |
+
self,
|
| 349 |
+
input_ids: torch.LongTensor = None,
|
| 350 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 351 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 352 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 353 |
+
labels: Optional[torch.LongTensor] = None,
|
| 354 |
+
use_cache: Optional[bool] = None,
|
| 355 |
+
output_attentions: Optional[bool] = None,
|
| 356 |
+
output_hidden_states: Optional[bool] = None,
|
| 357 |
+
return_dict: Optional[bool] = None,
|
| 358 |
+
logits_to_keep: Optional[int] = 0,
|
| 359 |
+
**kwargs: Unpack[Dict]
|
| 360 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 361 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 362 |
+
output_hidden_states = (
|
| 363 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 364 |
+
)
|
| 365 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 366 |
+
|
| 367 |
+
outputs = self.model(
|
| 368 |
+
input_ids=input_ids,
|
| 369 |
+
attention_mask=attention_mask,
|
| 370 |
+
inputs_embeds=inputs_embeds,
|
| 371 |
+
past_key_values=past_key_values,
|
| 372 |
+
use_cache=use_cache,
|
| 373 |
+
output_attentions=output_attentions,
|
| 374 |
+
output_hidden_states=output_hidden_states,
|
| 375 |
+
return_dict=return_dict,
|
| 376 |
+
**kwargs
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
hidden_states = outputs[0]
|
| 380 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 381 |
+
|
| 382 |
+
loss, logits = None, None
|
| 383 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 384 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 385 |
+
if labels is not None:
|
| 386 |
+
if getattr(self, 'criterion', None) is None:
|
| 387 |
+
if fuse_linear_and_cross_entropy:
|
| 388 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 389 |
+
elif self.config.fuse_cross_entropy:
|
| 390 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 391 |
+
else:
|
| 392 |
+
criterion = nn.CrossEntropyLoss()
|
| 393 |
+
else:
|
| 394 |
+
criterion = self.criterion
|
| 395 |
+
labels = labels.to(hidden_states.device)
|
| 396 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 397 |
+
if fuse_linear_and_cross_entropy:
|
| 398 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 399 |
+
else:
|
| 400 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 401 |
+
|
| 402 |
+
if not return_dict:
|
| 403 |
+
output = (logits,) + outputs[1:]
|
| 404 |
+
return (loss,) + output if loss is not None else output
|
| 405 |
+
|
| 406 |
+
return CausalLMOutputWithPast(
|
| 407 |
+
loss=loss,
|
| 408 |
+
logits=logits,
|
| 409 |
+
past_key_values=outputs.past_key_values,
|
| 410 |
+
hidden_states=outputs.hidden_states,
|
| 411 |
+
attentions=outputs.attentions,
|
| 412 |
+
)
|
fla/models/gated_deltaproduct/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 2 |
+
|
| 3 |
+
from fla.models.gated_deltaproduct.configuration_gated_deltaproduct import GatedDeltaProductConfig
|
| 4 |
+
from fla.models.gated_deltaproduct.modeling_gated_deltaproduct import GatedDeltaProductForCausalLM, GatedDeltaProductModel
|
| 5 |
+
|
| 6 |
+
AutoConfig.register(GatedDeltaProductConfig.model_type, GatedDeltaProductConfig)
|
| 7 |
+
AutoModel.register(GatedDeltaProductConfig, GatedDeltaProductModel)
|
| 8 |
+
AutoModelForCausalLM.register(GatedDeltaProductConfig, GatedDeltaProductForCausalLM)
|
| 9 |
+
|
| 10 |
+
__all__ = [
|
| 11 |
+
"GatedDeltaProductConfig",
|
| 12 |
+
"GatedDeltaProductForCausalLM",
|
| 13 |
+
"GatedDeltaProductModel",
|
| 14 |
+
]
|
fla/models/gated_deltaproduct/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (809 Bytes). View file
|
|
|
fla/models/gated_deltaproduct/__pycache__/configuration_gated_deltaproduct.cpython-312.pyc
ADDED
|
Binary file (3.41 kB). View file
|
|
|
fla/models/gated_deltaproduct/__pycache__/modeling_gated_deltaproduct.cpython-312.pyc
ADDED
|
Binary file (20.7 kB). View file
|
|
|
fla/models/gated_deltaproduct/modeling_gated_deltaproduct.py
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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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/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.gla.configuration_gla import GLAConfig
|
| 6 |
+
from fla.models.gla.modeling_gla import GLAForCausalLM, GLAModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(GLAConfig.model_type, GLAConfig)
|
| 9 |
+
AutoModel.register(GLAConfig, GLAModel)
|
| 10 |
+
AutoModelForCausalLM.register(GLAConfig, GLAForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['GLAConfig', 'GLAForCausalLM', 'GLAModel']
|
fla/models/gla/__pycache__/configuration_gla.cpython-312.pyc
ADDED
|
Binary file (3.76 kB). View file
|
|
|
fla/models/gla/__pycache__/modeling_gla.cpython-312.pyc
ADDED
|
Binary file (18.6 kB). View file
|
|
|
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/gsa/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.gsa.configuration_gsa import GSAConfig
|
| 6 |
+
from fla.models.gsa.modeling_gsa import GSAForCausalLM, GSAModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(GSAConfig.model_type, GSAConfig)
|
| 9 |
+
AutoModel.register(GSAConfig, GSAModel)
|
| 10 |
+
AutoModelForCausalLM.register(GSAConfig, GSAForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['GSAConfig', 'GSAForCausalLM', 'GSAModel']
|
fla/models/gsa/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (689 Bytes). View file
|
|
|
fla/models/gsa/modeling_gsa.py
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
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.gsa import GatedSlotAttention
|
| 20 |
+
from fla.models.gsa.configuration_gsa import GSAConfig
|
| 21 |
+
from fla.models.utils import Cache
|
| 22 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 23 |
+
from fla.modules import GatedMLP as GSAMLP
|
| 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 GSABlock(nn.Module):
|
| 33 |
+
def __init__(self, config: GSAConfig, 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 = GatedSlotAttention(
|
| 53 |
+
hidden_size=config.hidden_size,
|
| 54 |
+
expand_k=config.expand_k,
|
| 55 |
+
expand_v=config.expand_v,
|
| 56 |
+
num_heads=config.num_heads,
|
| 57 |
+
num_kv_heads=config.num_kv_heads,
|
| 58 |
+
num_slots=config.num_slots,
|
| 59 |
+
use_short_conv=config.use_short_conv,
|
| 60 |
+
conv_size=config.conv_size,
|
| 61 |
+
feature_map=config.feature_map,
|
| 62 |
+
use_output_gate=config.use_output_gate,
|
| 63 |
+
use_norm=config.use_norm,
|
| 64 |
+
gate_fn=config.hidden_act,
|
| 65 |
+
gate_logit_normalizer=config.gate_logit_normalizer,
|
| 66 |
+
elementwise_affine=config.elementwise_affine,
|
| 67 |
+
norm_eps=config.norm_eps,
|
| 68 |
+
fuse_norm=config.fuse_norm,
|
| 69 |
+
layer_idx=layer_idx
|
| 70 |
+
)
|
| 71 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 72 |
+
self.mlp = GSAMLP(
|
| 73 |
+
hidden_size=config.hidden_size,
|
| 74 |
+
hidden_ratio=config.hidden_ratio,
|
| 75 |
+
intermediate_size=config.intermediate_size,
|
| 76 |
+
hidden_act=config.hidden_act,
|
| 77 |
+
fuse_swiglu=config.fuse_swiglu
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
def forward(
|
| 81 |
+
self,
|
| 82 |
+
hidden_states: torch.Tensor,
|
| 83 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 84 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 85 |
+
use_cache: Optional[bool] = False,
|
| 86 |
+
output_attentions: Optional[bool] = False,
|
| 87 |
+
**kwargs: Unpack[Dict]
|
| 88 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 89 |
+
residual = hidden_states
|
| 90 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 91 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 92 |
+
hidden_states=hidden_states,
|
| 93 |
+
attention_mask=attention_mask,
|
| 94 |
+
past_key_values=past_key_values,
|
| 95 |
+
use_cache=use_cache,
|
| 96 |
+
output_attentions=output_attentions,
|
| 97 |
+
**kwargs
|
| 98 |
+
)
|
| 99 |
+
if self.config.fuse_norm:
|
| 100 |
+
hidden_states, residual = self.mlp_norm(hidden_states, residual, True)
|
| 101 |
+
else:
|
| 102 |
+
hidden_states = residual + hidden_states
|
| 103 |
+
residual = hidden_states
|
| 104 |
+
hidden_states = self.mlp_norm(hidden_states)
|
| 105 |
+
hidden_states = self.mlp(hidden_states, **kwargs)
|
| 106 |
+
hidden_states = residual + hidden_states
|
| 107 |
+
|
| 108 |
+
outputs = (hidden_states, attentions, past_key_values)
|
| 109 |
+
|
| 110 |
+
return outputs
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
class GSAPreTrainedModel(PreTrainedModel):
|
| 114 |
+
|
| 115 |
+
config_class = GSAConfig
|
| 116 |
+
base_model_prefix = 'model'
|
| 117 |
+
supports_gradient_checkpointing = True
|
| 118 |
+
_no_split_modules = ['GSABlock']
|
| 119 |
+
_supports_cache_class = True
|
| 120 |
+
|
| 121 |
+
def __init__(self, *inputs, **kwargs):
|
| 122 |
+
super().__init__(*inputs, **kwargs)
|
| 123 |
+
|
| 124 |
+
def _init_weights(
|
| 125 |
+
self,
|
| 126 |
+
module: nn.Module,
|
| 127 |
+
prenorm_residual_strategy: Optional[str] = 'rescale',
|
| 128 |
+
num_residuals_per_layer: int = 2,
|
| 129 |
+
):
|
| 130 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 131 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 132 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 133 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 134 |
+
if module.bias is not None:
|
| 135 |
+
nn.init.zeros_(module.bias)
|
| 136 |
+
elif isinstance(module, nn.Embedding):
|
| 137 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 138 |
+
elif hasattr(module, 'reset_parameters'):
|
| 139 |
+
module.reset_parameters()
|
| 140 |
+
|
| 141 |
+
if prenorm_residual_strategy is not None:
|
| 142 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 143 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 144 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 145 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 146 |
+
#
|
| 147 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 148 |
+
p = None
|
| 149 |
+
if hasattr(module, 'o_proj'):
|
| 150 |
+
p = module.o_proj.weight
|
| 151 |
+
elif hasattr(module, 'down_proj'):
|
| 152 |
+
p = module.down_proj.weight
|
| 153 |
+
if p is not None:
|
| 154 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 155 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 156 |
+
# We need to reinit p since this code could be called multiple times
|
| 157 |
+
# Having just p *= scale would repeatedly scale it down
|
| 158 |
+
if prenorm_residual_strategy == 'rescale':
|
| 159 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 160 |
+
with torch.no_grad():
|
| 161 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 162 |
+
elif prenorm_residual_strategy == 'zero':
|
| 163 |
+
nn.init.zeros_(p)
|
| 164 |
+
else:
|
| 165 |
+
raise ValueError(f"Invalid prenorm_residual_strategy: {prenorm_residual_strategy}")
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
class GSAModel(GSAPreTrainedModel):
|
| 169 |
+
|
| 170 |
+
def __init__(self, config: GSAConfig):
|
| 171 |
+
super().__init__(config)
|
| 172 |
+
self.padding_idx = config.pad_token_id
|
| 173 |
+
self.vocab_size = config.vocab_size
|
| 174 |
+
|
| 175 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 176 |
+
self.layers = nn.ModuleList([GSABlock(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 177 |
+
self.norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 178 |
+
|
| 179 |
+
self.gradient_checkpointing = False
|
| 180 |
+
|
| 181 |
+
self.post_init()
|
| 182 |
+
|
| 183 |
+
def get_input_embeddings(self):
|
| 184 |
+
return self.embeddings
|
| 185 |
+
|
| 186 |
+
def set_input_embeddings(self, value):
|
| 187 |
+
self.embeddings = value
|
| 188 |
+
|
| 189 |
+
def forward(
|
| 190 |
+
self,
|
| 191 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 192 |
+
attention_mask: Optional[torch.Tensor] = None, # noqa
|
| 193 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 194 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 195 |
+
use_cache: Optional[bool] = None,
|
| 196 |
+
output_attentions: Optional[bool] = None,
|
| 197 |
+
output_hidden_states: Optional[bool] = None,
|
| 198 |
+
return_dict: Optional[bool] = None,
|
| 199 |
+
**kwargs: Unpack[Dict]
|
| 200 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 201 |
+
if output_attentions:
|
| 202 |
+
warnings.warn("`GSAModel` does not `output_attentions` now, setting it to `False`.")
|
| 203 |
+
output_attentions = False
|
| 204 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 205 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 206 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 207 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 208 |
+
|
| 209 |
+
# retrieve input_ids and inputs_embeds
|
| 210 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 211 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 212 |
+
if input_ids is None and inputs_embeds is None:
|
| 213 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 214 |
+
|
| 215 |
+
if inputs_embeds is None:
|
| 216 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 217 |
+
hidden_states = inputs_embeds
|
| 218 |
+
|
| 219 |
+
if use_cache and not isinstance(past_key_values, Cache):
|
| 220 |
+
past_key_values = Cache.from_legacy_cache(past_key_values)
|
| 221 |
+
|
| 222 |
+
if self.gradient_checkpointing and self.training and use_cache:
|
| 223 |
+
logger.warning_once("`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...")
|
| 224 |
+
use_cache = False
|
| 225 |
+
|
| 226 |
+
all_hidden_states = () if output_hidden_states else None
|
| 227 |
+
all_attns = () if output_attentions else None
|
| 228 |
+
for layer in self.layers:
|
| 229 |
+
if output_hidden_states:
|
| 230 |
+
all_hidden_states += (hidden_states,)
|
| 231 |
+
|
| 232 |
+
if self.gradient_checkpointing and self.training:
|
| 233 |
+
hidden_states, attentions, past_key_values = self._gradient_checkpointing_func(
|
| 234 |
+
layer.__call__,
|
| 235 |
+
hidden_states,
|
| 236 |
+
attention_mask,
|
| 237 |
+
past_key_values,
|
| 238 |
+
use_cache,
|
| 239 |
+
output_attentions,
|
| 240 |
+
**kwargs
|
| 241 |
+
)
|
| 242 |
+
else:
|
| 243 |
+
hidden_states, attentions, past_key_values = layer(
|
| 244 |
+
hidden_states,
|
| 245 |
+
attention_mask=attention_mask,
|
| 246 |
+
past_key_values=past_key_values,
|
| 247 |
+
use_cache=use_cache,
|
| 248 |
+
output_attentions=output_attentions,
|
| 249 |
+
**kwargs
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
if output_attentions:
|
| 253 |
+
all_attns += (attentions,)
|
| 254 |
+
|
| 255 |
+
hidden_states = self.norm(hidden_states)
|
| 256 |
+
|
| 257 |
+
# add hidden states from the last decoder layer
|
| 258 |
+
if output_hidden_states:
|
| 259 |
+
all_hidden_states += (hidden_states,)
|
| 260 |
+
|
| 261 |
+
if not return_dict:
|
| 262 |
+
return tuple(i for i in [hidden_states, past_key_values, all_hidden_states, all_attns] if i is not None)
|
| 263 |
+
return BaseModelOutputWithPast(
|
| 264 |
+
last_hidden_state=hidden_states,
|
| 265 |
+
past_key_values=past_key_values,
|
| 266 |
+
hidden_states=all_hidden_states,
|
| 267 |
+
attentions=all_attns
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
class GSAForCausalLM(GSAPreTrainedModel, GenerationMixin):
|
| 272 |
+
|
| 273 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 274 |
+
|
| 275 |
+
def __init__(self, config):
|
| 276 |
+
|
| 277 |
+
super().__init__(config)
|
| 278 |
+
self.model = GSAModel(config)
|
| 279 |
+
self.vocab_size = config.vocab_size
|
| 280 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 281 |
+
self.criterion = None
|
| 282 |
+
|
| 283 |
+
# Initialize weights and apply final processing
|
| 284 |
+
self.post_init()
|
| 285 |
+
|
| 286 |
+
def get_input_embeddings(self):
|
| 287 |
+
return self.model.embeddings
|
| 288 |
+
|
| 289 |
+
def set_input_embeddings(self, value):
|
| 290 |
+
self.model.embeddings = value
|
| 291 |
+
|
| 292 |
+
def get_output_embeddings(self):
|
| 293 |
+
return self.lm_head
|
| 294 |
+
|
| 295 |
+
def set_output_embeddings(self, new_embeddings):
|
| 296 |
+
self.lm_head = new_embeddings
|
| 297 |
+
|
| 298 |
+
def set_decoder(self, decoder):
|
| 299 |
+
self.model = decoder
|
| 300 |
+
|
| 301 |
+
def get_decoder(self):
|
| 302 |
+
return self.model
|
| 303 |
+
|
| 304 |
+
def generate(self, *args, **kwargs):
|
| 305 |
+
try:
|
| 306 |
+
return super().generate(*args, **kwargs)
|
| 307 |
+
except AttributeError as exception:
|
| 308 |
+
if 'past_key_values' in str(exception):
|
| 309 |
+
raise AttributeError(
|
| 310 |
+
f"You tried to call `generate` with a decoding strategy that manipulates `past_key_values`, "
|
| 311 |
+
f"which is not supported for {self.__class__.__name__}. "
|
| 312 |
+
f"Try another generation strategy instead. "
|
| 313 |
+
f"For the available generation strategies, check this doc: "
|
| 314 |
+
f"https://huggingface.co/docs/transformers/en/generation_strategies#decoding-strategies"
|
| 315 |
+
)
|
| 316 |
+
else:
|
| 317 |
+
raise exception
|
| 318 |
+
|
| 319 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 320 |
+
def prepare_inputs_for_generation(
|
| 321 |
+
self,
|
| 322 |
+
input_ids: torch.LongTensor = None,
|
| 323 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 324 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 325 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 326 |
+
use_cache: bool = True,
|
| 327 |
+
logits_to_keep: Optional[int] = None,
|
| 328 |
+
**kwargs
|
| 329 |
+
):
|
| 330 |
+
# only last token for `inputs_ids` if the `past_key_values` is not empty.
|
| 331 |
+
if past_key_values is not None and len(past_key_values) > 0:
|
| 332 |
+
input_ids = input_ids[:, -1:]
|
| 333 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 334 |
+
if inputs_embeds is not None and len(past_key_values) == 0:
|
| 335 |
+
model_inputs = {'inputs_embeds': inputs_embeds}
|
| 336 |
+
else:
|
| 337 |
+
# The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
|
| 338 |
+
# recompiles graphs as the stride of the inputs is a guard.
|
| 339 |
+
# Ref: https://github.com/huggingface/transformers/pull/29114
|
| 340 |
+
# TODO: use `next_tokens` directly instead.
|
| 341 |
+
model_inputs = {'input_ids': input_ids.contiguous()}
|
| 342 |
+
|
| 343 |
+
if logits_to_keep is not None:
|
| 344 |
+
model_inputs['logits_to_keep'] = logits_to_keep
|
| 345 |
+
|
| 346 |
+
model_inputs.update({
|
| 347 |
+
'past_key_values': past_key_values,
|
| 348 |
+
'use_cache': use_cache,
|
| 349 |
+
'attention_mask': attention_mask,
|
| 350 |
+
})
|
| 351 |
+
return model_inputs
|
| 352 |
+
|
| 353 |
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
| 354 |
+
def forward(
|
| 355 |
+
self,
|
| 356 |
+
input_ids: torch.LongTensor = None,
|
| 357 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 358 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 359 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 360 |
+
labels: Optional[torch.LongTensor] = None,
|
| 361 |
+
use_cache: Optional[bool] = None,
|
| 362 |
+
output_attentions: Optional[bool] = None,
|
| 363 |
+
output_hidden_states: Optional[bool] = None,
|
| 364 |
+
return_dict: Optional[bool] = None,
|
| 365 |
+
logits_to_keep: Optional[int] = 0,
|
| 366 |
+
**kwargs: Unpack[Dict]
|
| 367 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 368 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 369 |
+
output_hidden_states = (
|
| 370 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 371 |
+
)
|
| 372 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 373 |
+
|
| 374 |
+
outputs = self.model(
|
| 375 |
+
input_ids=input_ids,
|
| 376 |
+
attention_mask=attention_mask,
|
| 377 |
+
inputs_embeds=inputs_embeds,
|
| 378 |
+
past_key_values=past_key_values,
|
| 379 |
+
use_cache=use_cache,
|
| 380 |
+
output_attentions=output_attentions,
|
| 381 |
+
output_hidden_states=output_hidden_states,
|
| 382 |
+
return_dict=return_dict,
|
| 383 |
+
**kwargs
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
hidden_states = outputs[0]
|
| 387 |
+
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
|
| 388 |
+
|
| 389 |
+
loss, logits = None, None
|
| 390 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 391 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 392 |
+
if labels is not None:
|
| 393 |
+
if getattr(self, 'criterion', None) is None:
|
| 394 |
+
if fuse_linear_and_cross_entropy:
|
| 395 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 396 |
+
elif self.config.fuse_cross_entropy:
|
| 397 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 398 |
+
else:
|
| 399 |
+
criterion = nn.CrossEntropyLoss()
|
| 400 |
+
else:
|
| 401 |
+
criterion = self.criterion
|
| 402 |
+
# Enable model parallelism
|
| 403 |
+
labels = labels.to(hidden_states.device)
|
| 404 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 405 |
+
if fuse_linear_and_cross_entropy:
|
| 406 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 407 |
+
else:
|
| 408 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 409 |
+
|
| 410 |
+
if not return_dict:
|
| 411 |
+
output = (logits,) + outputs[1:]
|
| 412 |
+
return (loss,) + output if loss is not None else output
|
| 413 |
+
|
| 414 |
+
return CausalLMOutputWithPast(
|
| 415 |
+
loss=loss,
|
| 416 |
+
logits=logits,
|
| 417 |
+
past_key_values=outputs.past_key_values,
|
| 418 |
+
hidden_states=outputs.hidden_states,
|
| 419 |
+
attentions=outputs.attentions,
|
| 420 |
+
)
|
fla/models/hgrn/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.hgrn.configuration_hgrn import HGRNConfig
|
| 6 |
+
from fla.models.hgrn.modeling_hgrn import HGRNForCausalLM, HGRNModel
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(HGRNConfig.model_type, HGRNConfig)
|
| 9 |
+
AutoModel.register(HGRNConfig, HGRNModel)
|
| 10 |
+
AutoModelForCausalLM.register(HGRNConfig, HGRNForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['HGRNConfig', 'HGRNForCausalLM', 'HGRNModel']
|
fla/models/hgrn/configuration_hgrn.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from typing import Dict, Optional
|
| 4 |
+
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class HGRNConfig(PretrainedConfig):
|
| 9 |
+
|
| 10 |
+
model_type = 'hgrn'
|
| 11 |
+
keys_to_ignore_at_inference = ['past_key_values']
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
attn_mode: str = "fused_recurrent",
|
| 16 |
+
hidden_size: int = 2048,
|
| 17 |
+
num_hidden_layers: int = 24,
|
| 18 |
+
expand_ratio: Optional[int] = 1,
|
| 19 |
+
use_short_conv: bool = False,
|
| 20 |
+
conv_size: int = 4,
|
| 21 |
+
use_lower_bound: bool = True,
|
| 22 |
+
max_position_embeddings: int = 2048,
|
| 23 |
+
hidden_ratio: Optional[int] = 4,
|
| 24 |
+
intermediate_size: Optional[int] = None,
|
| 25 |
+
hidden_act: str = "swish",
|
| 26 |
+
elementwise_affine: Optional[bool] = True,
|
| 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.num_hidden_layers = num_hidden_layers
|
| 44 |
+
self.expand_ratio = expand_ratio
|
| 45 |
+
self.use_short_conv = use_short_conv
|
| 46 |
+
self.conv_size = conv_size
|
| 47 |
+
self.use_lower_bound = use_lower_bound
|
| 48 |
+
self.max_position_embeddings = max_position_embeddings
|
| 49 |
+
self.hidden_ratio = hidden_ratio
|
| 50 |
+
self.intermediate_size = intermediate_size
|
| 51 |
+
self.elementwise_affine = elementwise_affine
|
| 52 |
+
self.attn = attn
|
| 53 |
+
self.norm_eps = norm_eps
|
| 54 |
+
self.hidden_act = hidden_act
|
| 55 |
+
self.use_cache = use_cache
|
| 56 |
+
self.initializer_range = initializer_range
|
| 57 |
+
|
| 58 |
+
self.fuse_norm = fuse_norm
|
| 59 |
+
self.fuse_swiglu = fuse_swiglu
|
| 60 |
+
self.fuse_cross_entropy = fuse_cross_entropy
|
| 61 |
+
self.vocab_size = vocab_size
|
| 62 |
+
|
| 63 |
+
if attn is not None:
|
| 64 |
+
if not isinstance(attn, Dict):
|
| 65 |
+
raise ValueError("attn must be a dictionary")
|
| 66 |
+
if 'layers' not in attn:
|
| 67 |
+
raise ValueError("Layer indices must be provided to initialize hybrid attention layers")
|
| 68 |
+
if 'num_heads' not in attn:
|
| 69 |
+
raise ValueError("Number of heads must be provided to initialize hybrid attention layers")
|
| 70 |
+
attn['num_kv_heads'] = attn.get('num_kv_heads', attn['num_heads'])
|
| 71 |
+
attn['qkv_bias'] = attn.get('qkv_bias', False)
|
| 72 |
+
attn['window_size'] = attn.get('window_size', None)
|
| 73 |
+
attn['rope_theta'] = attn.get('rope_theta', 10000.)
|
| 74 |
+
|
| 75 |
+
super().__init__(
|
| 76 |
+
pad_token_id=pad_token_id,
|
| 77 |
+
bos_token_id=bos_token_id,
|
| 78 |
+
eos_token_id=eos_token_id,
|
| 79 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 80 |
+
**kwargs,
|
| 81 |
+
)
|
fla/models/hgrn/modeling_hgrn.py
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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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.hgrn import HGRNAttention
|
| 20 |
+
from fla.models.hgrn.configuration_hgrn import HGRNConfig
|
| 21 |
+
from fla.models.utils import Cache
|
| 22 |
+
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss
|
| 23 |
+
from fla.modules import GatedMLP as HGRNMLP
|
| 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 HGRNBlock(nn.Module):
|
| 33 |
+
def __init__(self, config: HGRNConfig, 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 = HGRNAttention(
|
| 53 |
+
mode=config.attn_mode,
|
| 54 |
+
hidden_size=config.hidden_size,
|
| 55 |
+
expand_ratio=config.expand_ratio,
|
| 56 |
+
use_short_conv=config.use_short_conv,
|
| 57 |
+
conv_size=config.conv_size,
|
| 58 |
+
elementwise_affine=config.elementwise_affine,
|
| 59 |
+
norm_eps=config.norm_eps,
|
| 60 |
+
layer_idx=layer_idx
|
| 61 |
+
)
|
| 62 |
+
self.mlp_norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 63 |
+
self.mlp = HGRNMLP(
|
| 64 |
+
hidden_size=config.hidden_size,
|
| 65 |
+
hidden_ratio=config.hidden_ratio,
|
| 66 |
+
intermediate_size=config.intermediate_size,
|
| 67 |
+
hidden_act=config.hidden_act,
|
| 68 |
+
fuse_swiglu=config.fuse_swiglu
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
def forward(
|
| 72 |
+
self,
|
| 73 |
+
hidden_states: torch.Tensor,
|
| 74 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 75 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 76 |
+
use_cache: Optional[bool] = False,
|
| 77 |
+
output_attentions: Optional[bool] = False,
|
| 78 |
+
lower_bound: Optional[torch.Tensor] = False,
|
| 79 |
+
**kwargs: Unpack[Dict]
|
| 80 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 81 |
+
residual = hidden_states
|
| 82 |
+
hidden_states = self.attn_norm(hidden_states)
|
| 83 |
+
hidden_states, attentions, past_key_values = self.attn(
|
| 84 |
+
hidden_states=hidden_states,
|
| 85 |
+
attention_mask=attention_mask,
|
| 86 |
+
past_key_values=past_key_values,
|
| 87 |
+
use_cache=use_cache,
|
| 88 |
+
output_attentions=output_attentions,
|
| 89 |
+
lower_bound=lower_bound,
|
| 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, attentions, past_key_values)
|
| 102 |
+
|
| 103 |
+
return outputs
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class HGRNPreTrainedModel(PreTrainedModel):
|
| 107 |
+
|
| 108 |
+
config_class = HGRNConfig
|
| 109 |
+
base_model_prefix = 'model'
|
| 110 |
+
supports_gradient_checkpointing = True
|
| 111 |
+
_no_split_modules = ['HGRNBlock']
|
| 112 |
+
_supports_cache_class = True
|
| 113 |
+
|
| 114 |
+
def __init__(self, *inputs, **kwargs):
|
| 115 |
+
super().__init__(*inputs, **kwargs)
|
| 116 |
+
|
| 117 |
+
def _init_weights(
|
| 118 |
+
self,
|
| 119 |
+
module: nn.Module,
|
| 120 |
+
prenorm_residual_strategy: Optional[str] = 'rescale',
|
| 121 |
+
num_residuals_per_layer: int = 2,
|
| 122 |
+
):
|
| 123 |
+
if isinstance(module, (nn.Linear, nn.Conv1d)):
|
| 124 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
| 125 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
| 126 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 127 |
+
if module.bias is not None:
|
| 128 |
+
nn.init.zeros_(module.bias)
|
| 129 |
+
elif isinstance(module, nn.Embedding):
|
| 130 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 131 |
+
elif hasattr(module, 'reset_parameters'):
|
| 132 |
+
module.reset_parameters()
|
| 133 |
+
|
| 134 |
+
if prenorm_residual_strategy is not None:
|
| 135 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
| 136 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
| 137 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
| 138 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
| 139 |
+
#
|
| 140 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
| 141 |
+
p = None
|
| 142 |
+
if hasattr(module, 'o_proj'):
|
| 143 |
+
p = module.o_proj.weight
|
| 144 |
+
elif hasattr(module, 'down_proj'):
|
| 145 |
+
p = module.down_proj.weight
|
| 146 |
+
if p is not None:
|
| 147 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
| 148 |
+
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
|
| 149 |
+
# We need to reinit p since this code could be called multiple times
|
| 150 |
+
# Having just p *= scale would repeatedly scale it down
|
| 151 |
+
if prenorm_residual_strategy == 'rescale':
|
| 152 |
+
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
|
| 153 |
+
with torch.no_grad():
|
| 154 |
+
p /= math.sqrt(num_residuals_per_layer * self.config.num_hidden_layers)
|
| 155 |
+
elif prenorm_residual_strategy == 'zero':
|
| 156 |
+
nn.init.zeros_(p)
|
| 157 |
+
else:
|
| 158 |
+
raise ValueError(f"Invalid prenorm_residual_strategy: {prenorm_residual_strategy}")
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
class HGRNModel(HGRNPreTrainedModel):
|
| 162 |
+
|
| 163 |
+
def __init__(self, config: HGRNConfig):
|
| 164 |
+
super().__init__(config)
|
| 165 |
+
self.padding_idx = config.pad_token_id
|
| 166 |
+
self.vocab_size = config.vocab_size
|
| 167 |
+
|
| 168 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 169 |
+
if config.use_lower_bound:
|
| 170 |
+
self.lower_bounds = nn.Parameter(torch.zeros(config.num_hidden_layers, config.hidden_size))
|
| 171 |
+
self.layers = nn.ModuleList([HGRNBlock(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 172 |
+
self.norm = (RMSNorm if config.fuse_norm else nn.RMSNorm)(config.hidden_size, eps=config.norm_eps)
|
| 173 |
+
|
| 174 |
+
self.gradient_checkpointing = False
|
| 175 |
+
|
| 176 |
+
self.post_init()
|
| 177 |
+
|
| 178 |
+
def get_input_embeddings(self):
|
| 179 |
+
return self.embeddings
|
| 180 |
+
|
| 181 |
+
def set_input_embeddings(self, value):
|
| 182 |
+
self.embeddings = value
|
| 183 |
+
|
| 184 |
+
def forward(
|
| 185 |
+
self,
|
| 186 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 187 |
+
attention_mask: Optional[torch.Tensor] = None, # noqa
|
| 188 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 189 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 190 |
+
use_cache: Optional[bool] = None,
|
| 191 |
+
output_attentions: Optional[bool] = None,
|
| 192 |
+
output_hidden_states: Optional[bool] = None,
|
| 193 |
+
return_dict: Optional[bool] = None,
|
| 194 |
+
**kwargs: Unpack[Dict]
|
| 195 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 196 |
+
if output_attentions:
|
| 197 |
+
warnings.warn("`HGRNModel` does not `output_attentions` now, setting it to `False`.")
|
| 198 |
+
output_attentions = False
|
| 199 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 200 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 201 |
+
use_cache = use_cache if use_cache is not None else (self.config.use_cache if not self.training else False)
|
| 202 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 203 |
+
|
| 204 |
+
# retrieve input_ids and inputs_embeds
|
| 205 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 206 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 207 |
+
if input_ids is None and inputs_embeds is None:
|
| 208 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 209 |
+
|
| 210 |
+
if inputs_embeds is None:
|
| 211 |
+
inputs_embeds = self.embeddings(input_ids)
|
| 212 |
+
hidden_states = 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 self.gradient_checkpointing and self.training and use_cache:
|
| 218 |
+
logger.warning_once("`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...")
|
| 219 |
+
use_cache = False
|
| 220 |
+
|
| 221 |
+
all_hidden_states = () if output_hidden_states else None
|
| 222 |
+
all_attns = () if output_attentions else None
|
| 223 |
+
|
| 224 |
+
if self.config.use_lower_bound:
|
| 225 |
+
lower_bounds = self.lower_bounds.softmax(0)
|
| 226 |
+
lower_bounds = lower_bounds.cumsum(0) - lower_bounds[0]
|
| 227 |
+
for i, layer in enumerate(self.layers):
|
| 228 |
+
if output_hidden_states:
|
| 229 |
+
all_hidden_states += (hidden_states,)
|
| 230 |
+
|
| 231 |
+
lower_bound = lower_bounds[i] if self.config.use_lower_bound else None
|
| 232 |
+
if self.gradient_checkpointing and self.training:
|
| 233 |
+
hidden_states, attentions, past_key_values = self._gradient_checkpointing_func(
|
| 234 |
+
layer.__call__,
|
| 235 |
+
hidden_states,
|
| 236 |
+
attention_mask,
|
| 237 |
+
past_key_values,
|
| 238 |
+
use_cache,
|
| 239 |
+
output_attentions,
|
| 240 |
+
lower_bound,
|
| 241 |
+
**kwargs
|
| 242 |
+
)
|
| 243 |
+
else:
|
| 244 |
+
hidden_states, attentions, past_key_values = layer(
|
| 245 |
+
hidden_states,
|
| 246 |
+
attention_mask=attention_mask,
|
| 247 |
+
past_key_values=past_key_values,
|
| 248 |
+
use_cache=use_cache,
|
| 249 |
+
output_attentions=output_attentions,
|
| 250 |
+
lower_bound=lower_bound,
|
| 251 |
+
**kwargs
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
if output_attentions:
|
| 255 |
+
all_attns += (attentions,)
|
| 256 |
+
|
| 257 |
+
hidden_states = self.norm(hidden_states)
|
| 258 |
+
|
| 259 |
+
# add hidden states from the last decoder layer
|
| 260 |
+
if output_hidden_states:
|
| 261 |
+
all_hidden_states += (hidden_states,)
|
| 262 |
+
|
| 263 |
+
if not return_dict:
|
| 264 |
+
return tuple(i for i in [hidden_states, past_key_values, all_hidden_states, all_attns] if i is not None)
|
| 265 |
+
return BaseModelOutputWithPast(
|
| 266 |
+
last_hidden_state=hidden_states,
|
| 267 |
+
past_key_values=past_key_values,
|
| 268 |
+
hidden_states=all_hidden_states,
|
| 269 |
+
attentions=all_attns
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
class HGRNForCausalLM(HGRNPreTrainedModel, GenerationMixin):
|
| 274 |
+
|
| 275 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 276 |
+
|
| 277 |
+
def __init__(self, config):
|
| 278 |
+
super().__init__(config)
|
| 279 |
+
self.model = HGRNModel(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 |
+
def generate(self, *args, **kwargs):
|
| 306 |
+
try:
|
| 307 |
+
return super().generate(*args, **kwargs)
|
| 308 |
+
except AttributeError as exception:
|
| 309 |
+
if 'past_key_values' in str(exception):
|
| 310 |
+
raise AttributeError(
|
| 311 |
+
f"You tried to call `generate` with a decoding strategy that manipulates `past_key_values`, "
|
| 312 |
+
f"which is not supported for {self.__class__.__name__}. "
|
| 313 |
+
f"Try another generation strategy instead. "
|
| 314 |
+
f"For the available generation strategies, check this doc: "
|
| 315 |
+
f"https://huggingface.co/docs/transformers/en/generation_strategies#decoding-strategies"
|
| 316 |
+
)
|
| 317 |
+
else:
|
| 318 |
+
raise exception
|
| 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: Unpack[Dict]
|
| 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 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 360 |
+
past_key_values: Optional[Union[Cache, List[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[Dict]
|
| 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 |
+
inputs_embeds=inputs_embeds,
|
| 379 |
+
past_key_values=past_key_values,
|
| 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 |
+
|
| 390 |
+
loss, logits = None, None
|
| 391 |
+
if not fuse_linear_and_cross_entropy or labels is None:
|
| 392 |
+
logits = self.lm_head(hidden_states if logits_to_keep is None else hidden_states[:, -logits_to_keep:])
|
| 393 |
+
if labels is not None:
|
| 394 |
+
if getattr(self, 'criterion', None) is None:
|
| 395 |
+
if fuse_linear_and_cross_entropy:
|
| 396 |
+
criterion = FusedLinearCrossEntropyLoss()
|
| 397 |
+
elif self.config.fuse_cross_entropy:
|
| 398 |
+
criterion = FusedCrossEntropyLoss(inplace_backward=True)
|
| 399 |
+
else:
|
| 400 |
+
criterion = nn.CrossEntropyLoss()
|
| 401 |
+
else:
|
| 402 |
+
criterion = self.criterion
|
| 403 |
+
labels = labels.to(hidden_states.device)
|
| 404 |
+
labels = torch.cat((labels[..., 1:], torch.full_like(labels[:, :1], criterion.ignore_index)), 1)
|
| 405 |
+
if fuse_linear_and_cross_entropy:
|
| 406 |
+
loss = criterion(hidden_states, labels, self.lm_head.weight, self.lm_head.bias)
|
| 407 |
+
else:
|
| 408 |
+
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
|
| 409 |
+
|
| 410 |
+
if not return_dict:
|
| 411 |
+
output = (logits,) + outputs[1:]
|
| 412 |
+
return (loss,) + output if loss is not None else output
|
| 413 |
+
|
| 414 |
+
return CausalLMOutputWithPast(
|
| 415 |
+
loss=loss,
|
| 416 |
+
logits=logits,
|
| 417 |
+
past_key_values=outputs.past_key_values,
|
| 418 |
+
hidden_states=outputs.hidden_states,
|
| 419 |
+
attentions=outputs.attentions,
|
| 420 |
+
)
|
fla/models/hgrn2/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
from fla.models.hgrn2.configuration_hgrn2 import HGRN2Config
|
| 6 |
+
from fla.models.hgrn2.modeling_hgrn2 import HGRN2ForCausalLM, HGRN2Model
|
| 7 |
+
|
| 8 |
+
AutoConfig.register(HGRN2Config.model_type, HGRN2Config)
|
| 9 |
+
AutoModel.register(HGRN2Config, HGRN2Model)
|
| 10 |
+
AutoModelForCausalLM.register(HGRN2Config, HGRN2ForCausalLM)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = ['HGRN2Config', 'HGRN2ForCausalLM', 'HGRN2Model']
|