File size: 19,256 Bytes
b64301c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | import math
import warnings
from typing import Optional, Tuple, Union
import torch
from torch import nn
from transformers.modeling_utils import PreTrainedModel
from transformers.utils import logging
from transformers.utils.deprecation import deprecate_kwarg
from fla.layers.log_linear_mamba2 import LogLinearAttentionState, LogLinearMamba2
from fla.models.log_linear_mamba2.configuration_log_linear_mamba2 import LogLinearMamba2Config
from fla.models.mamba2.modeling_mamba2 import Mamba2Cache, Mamba2CausalLMOutput, Mamba2Output
from fla.models.utils import FLAGenerationMixin
from fla.modules import FusedCrossEntropyLoss, FusedLinearCrossEntropyLoss, GatedMLP, RMSNorm
logger = logging.get_logger(__name__)
class LogLinearMamba2Cache(Mamba2Cache):
def __init__(
self,
config: LogLinearMamba2Config,
batch_size: int,
dtype: torch.dtype = torch.float16,
device: Optional[str] = None,
):
self.dtype = dtype
self.conv_kernel_size = config.conv_kernel
self.n_groups = config.n_groups
self.state_size = config.state_size
self.num_heads = config.num_heads
self.head_dim = config.head_dim
self.intermediate_size = int(config.expand * config.hidden_size)
self.conv_states = {
i: torch.zeros(
batch_size,
self.intermediate_size + 2 * config.n_groups * config.state_size,
self.conv_kernel_size,
device=device,
dtype=dtype,
)
for i in range(config.num_hidden_layers)
}
self.hssm_states = {
i: None for i in range(config.num_hidden_layers)
}
def update_conv_state(
self, layer_idx: int, new_conv_state: torch.Tensor, cache_init: bool = False
) -> torch.Tensor:
if new_conv_state.dtype != self.conv_states[layer_idx].dtype:
warnings.warn(
f"`new_conv_state.dtype` ({new_conv_state.dtype}) does not match the cache's dtype "
f"({self.conv_states[layer_idx].dtype}), casting.",
stacklevel=2
)
new_conv_state = new_conv_state.to(dtype=self.conv_states[layer_idx].dtype)
if cache_init:
self.conv_states[layer_idx] = new_conv_state.to(
self.conv_states[layer_idx].device
)
else:
self.conv_states[layer_idx] = self.conv_states[layer_idx].roll(
shifts=-1, dims=-1
)
self.conv_states[layer_idx][:, :, -1] = new_conv_state[:, 0, :].to(
self.conv_states[layer_idx].device
)
return self.conv_states[layer_idx]
def update_hssm_state(
self, layer_idx: int, new_hssm_state: LogLinearAttentionState
) -> LogLinearAttentionState:
self.hssm_states[layer_idx] = new_hssm_state
return self.hssm_states[layer_idx]
def reset(self) -> None:
for k in self.conv_states.keys():
self.conv_states[k].zero_()
for k in self.hssm_states.keys():
self.hssm_states[k].reset_states()
class LogLinearMamba2Block(nn.Module):
def __init__(self, config: LogLinearMamba2Config, layer_idx: int) -> None:
super().__init__()
if config.residual_in_fp32:
raise NotImplementedError
self.config = config
self.layer_idx = layer_idx
self.mixer_norm = RMSNorm(config.hidden_size, eps=config.norm_eps)
self.mlp_norm = RMSNorm(config.hidden_size, eps=config.norm_eps)
self.mixer = LogLinearMamba2(
num_heads=config.num_heads,
head_dim=config.head_dim,
hidden_size=config.hidden_size,
state_size=config.state_size,
expand=config.expand,
n_groups=config.n_groups,
conv_kernel=config.conv_kernel,
use_conv_bias=config.use_conv_bias,
hidden_act=config.hidden_act,
rms_norm=config.rms_norm,
chunk_size=config.chunk_size,
time_step_rank=config.time_step_rank,
time_step_limit=config.time_step_limit,
time_step_min=config.time_step_min,
time_step_max=config.time_step_max,
use_bias=config.use_bias,
norm_eps=config.norm_eps,
layer_idx=layer_idx,
)
self.mlp = GatedMLP(
hidden_size=config.hidden_size,
hidden_ratio=4,
intermediate_size=None,
hidden_act="swish",
fuse_swiglu=True,
)
def forward(
self,
hidden_states,
cache_params: Optional[LogLinearMamba2Cache] = None,
cache_position: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
):
residual = hidden_states
hidden_states = self.mixer_norm(hidden_states)
hidden_states = self.mixer(
hidden_states,
cache_params=cache_params,
cache_position=cache_position,
attention_mask=attention_mask,
)
if self.config.fuse_norm:
hidden_states, residual = self.mlp_norm(
hidden_states, residual=residual, prenorm=True
)
else:
hidden_states = residual + hidden_states
residual = hidden_states
hidden_states = self.mlp_norm(hidden_states)
hidden_states = self.mlp(hidden_states)
hidden_states = residual + hidden_states
return hidden_states
class LogLinearMamba2PreTrainedModel(PreTrainedModel, FLAGenerationMixin):
"""
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
models.
"""
config_class = LogLinearMamba2Config
base_model_prefix = "backbone"
_no_split_modules = ["LogLinearMamba2Block"]
supports_gradient_checkpointing = True
_is_stateful = True
def _init_weights(
self,
module: nn.Module,
num_residuals_per_layer: int = 2, # HAttention + MLP
):
"""Initialize the weights."""
if isinstance(module, LogLinearMamba2):
# --- A_log ---
A = torch.arange(1, module.num_heads + 1)
with torch.no_grad():
if not isinstance(module.A_log, torch.distributed.tensor.DTensor):
module.A_log.copy_(torch.log(A))
else:
logger.warning_once("`A_log` is a DTensor, skipping initialization")
module.A_log._no_weight_decay = True
# --- D ---
nn.init.ones_(module.D)
module.D._no_weight_decay = True
# --- L ---
nn.init.ones_(module.L)
module.L._no_weight_decay = True
# --- dt_bias ---
dt = torch.exp(
torch.rand(self.config.num_heads)
* (
math.log(self.config.time_step_max)
- math.log(self.config.time_step_min)
)
+ math.log(self.config.time_step_min)
).clamp(min=self.config.time_step_floor)
# Inverse of softplus: https://github.com/pytorch/pytorch/issues/72759
inv_dt = dt + torch.log(-torch.expm1(-dt))
with torch.no_grad():
if not isinstance(module.dt_bias, torch.distributed.tensor.DTensor):
module.dt_bias.copy_(inv_dt)
else:
logger.warning_once(
"`dt_bias` is a DTensor, skipping initialization"
)
module.dt_bias._no_reinit = True
elif isinstance(module, (nn.Linear, nn.Conv1d)):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
if module.bias is not None:
nn.init.zeros_(module.bias)
# guard against deprecated behavior
if hasattr(module.bias, "_no_reinit"):
raise ValueError("This is not supposed to happen")
elif isinstance(module, nn.Embedding):
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
elif hasattr(module, "reset_parameters"):
module.reset_parameters()
if self.config.rescale_prenorm_residual:
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
#
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
p = None
if hasattr(module, "o_proj"):
# p = module.o_proj.weight
# guard against deprecated behavior
raise ValueError("This is not supposed to happen")
elif hasattr(module, "out_proj"):
p = module.out_proj.weight
elif hasattr(module, "down_proj"):
p = module.down_proj.weight
if p is not None:
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
# Following Pytorch init, except scale by 1/sqrt(2 * n_layer)
# We need to reinit p since this code could be called multiple times
# Having just p *= scale would repeatedly scale it down
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
with torch.no_grad():
p /= math.sqrt(
num_residuals_per_layer * self.config.num_hidden_layers
)
class LogLinearMamba2Model(LogLinearMamba2PreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size)
self.layers = nn.ModuleList(
[
LogLinearMamba2Block(config, layer_idx=idx)
for idx in range(config.num_hidden_layers)
]
)
self.gradient_checkpointing = False
self.norm_f = RMSNorm(config.hidden_size, eps=config.norm_eps)
# Initialize weights and apply final processing
self._register_load_state_dict_pre_hook(self.load_hook)
self.post_init()
def load_hook(self, state_dict, prefix, *args):
for k in state_dict:
if "embedding." in k:
state_dict[k.replace("embedding.", "embeddings.")] = state_dict.pop(k)
break
def get_input_embeddings(self):
return self.embeddings
def set_input_embeddings(self, new_embeddings):
self.embeddings = new_embeddings
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
inputs_embeds: Optional[torch.LongTensor] = None,
cache_params: Optional[LogLinearMamba2Cache] = None,
use_cache: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
cache_position: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
**kwargs,
) -> Union[Tuple, Mamba2Output]:
output_hidden_states = (
output_hidden_states
if output_hidden_states is not None
else self.config.output_hidden_states
)
use_cache = (
use_cache
if use_cache is not None
else (self.config.use_cache if not self.training else False)
)
return_dict = (
return_dict if return_dict is not None else self.config.use_return_dict
)
if (input_ids is None) ^ (inputs_embeds is not None): # ^ is python for xor
raise ValueError(
"You must specify exactly one of input_ids or inputs_embeds"
)
if inputs_embeds is None:
inputs_embeds = self.embeddings(input_ids)
if self.gradient_checkpointing and self.training and use_cache:
use_cache = False
if use_cache:
if cache_params is None:
cache_params = LogLinearMamba2Cache(
self.config,
inputs_embeds.size(0),
device=inputs_embeds.device,
dtype=inputs_embeds.dtype,
)
cache_position = torch.arange(
0, self.config.conv_kernel, device=inputs_embeds.device
)
elif cache_position is None:
# cases when we do manual forward instead of using `model.generate` which will initiate
# `cache_position` and makes sure it is not None, throw error here instead of doing some
# hack to conjecture the current cache position
raise ValueError(
"You have to specify the `cache_position` manually when `use_cache=True` and `cache_params` is passed, "
"you don't have to pass a `cache_params` if you are in prefilling stage because in that case it will "
"be initialized for you automatically"
)
else:
cache_params = None
hidden_states = inputs_embeds
all_hidden_states = () if output_hidden_states else None
for mixer_block in self.layers:
if self.gradient_checkpointing and self.training:
hidden_states = self._gradient_checkpointing_func(
mixer_block.__call__,
hidden_states,
cache_params,
cache_position,
attention_mask,
)
else:
hidden_states = mixer_block(
hidden_states,
cache_params=cache_params,
cache_position=cache_position,
attention_mask=attention_mask,
)
if output_hidden_states:
all_hidden_states = all_hidden_states + (hidden_states,)
hidden_states = self.norm_f(hidden_states)
if output_hidden_states:
all_hidden_states = all_hidden_states + (hidden_states,)
if not return_dict:
return tuple(
v
for v in [hidden_states, cache_params, all_hidden_states]
if v is not None
)
return Mamba2Output(
last_hidden_state=hidden_states,
cache_params=cache_params if use_cache else None,
hidden_states=all_hidden_states,
)
class LogLinearMamba2ForCausalLM(LogLinearMamba2PreTrainedModel):
_tied_weights_keys = []
def __init__(self, config):
super().__init__(config)
self.backbone = LogLinearMamba2Model(config)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
self.criterion = None
# Initialize weights and apply final processing
self.post_init()
def get_output_embeddings(self):
return self.lm_head
def set_output_embeddings(self, new_embeddings):
self.lm_head = new_embeddings
def get_input_embeddings(self):
return self.backbone.get_input_embeddings()
def set_input_embeddings(self, new_embeddings):
return self.backbone.set_input_embeddings(new_embeddings)
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
cache_params: Optional[LogLinearMamba2Cache] = None,
labels: Optional[torch.LongTensor] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
use_cache: Optional[bool] = None,
cache_position: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
logits_to_keep: Optional[int] = 0,
**kwargs, # for now we need this for generation
) -> Union[Tuple, Mamba2CausalLMOutput]:
r"""
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
`labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
"""
return_dict = (
return_dict if return_dict is not None else self.config.use_return_dict
)
outputs = self.backbone(
input_ids,
cache_params=cache_params,
inputs_embeds=inputs_embeds,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
use_cache=use_cache,
cache_position=cache_position,
attention_mask=attention_mask,
)
hidden_states = outputs[0]
fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training
loss, logits = None, None
if not fuse_linear_and_cross_entropy or labels is None:
logits = self.lm_head(
hidden_states
if logits_to_keep is None
else hidden_states[:, -logits_to_keep:]
)
if labels is not None:
if getattr(self, "criterion", None) is None:
if fuse_linear_and_cross_entropy:
criterion = FusedLinearCrossEntropyLoss()
elif self.config.fuse_cross_entropy:
criterion = FusedCrossEntropyLoss(inplace_backward=True)
else:
criterion = nn.CrossEntropyLoss()
else:
criterion = self.criterion
labels = labels.to(hidden_states.device)
labels = torch.cat(
(
labels[..., 1:],
torch.full_like(labels[:, :1], criterion.ignore_index),
),
1,
)
if fuse_linear_and_cross_entropy:
loss = criterion(
hidden_states, labels, self.lm_head.weight, self.lm_head.bias
)
else:
loss = criterion(logits.view(labels.numel(), -1), labels.view(-1))
if not return_dict:
output = (logits,) + outputs[1:]
return (loss,) + output if loss is not None else output
return Mamba2CausalLMOutput(
loss=loss,
logits=logits,
cache_params=outputs.cache_params,
hidden_states=outputs.hidden_states,
)
|