|
|
""" PyTorch SRV1 model.""" |
|
|
import sys |
|
|
import os |
|
|
from os import path |
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
|
|
print(sys.path) |
|
|
import math |
|
|
from typing import List, Optional, Tuple, Union |
|
|
|
|
|
import torch |
|
|
import torch.utils.checkpoint |
|
|
from torch import nn |
|
|
from torch.nn import CrossEntropyLoss |
|
|
from transformers.activations import ACT2FN |
|
|
from transformers import AutoTokenizer, AutoConfig |
|
|
from .configuration_srv1 import SRV1Config |
|
|
|
|
|
from transformers.modeling_outputs import ( |
|
|
BaseModelOutputWithPast, |
|
|
CausalLMOutputWithPast, |
|
|
) |
|
|
from transformers.modeling_utils import PreTrainedModel |
|
|
from transformers.utils import ( |
|
|
add_start_docstrings, |
|
|
add_start_docstrings_to_model_forward, |
|
|
logging, |
|
|
replace_return_docstrings, |
|
|
) |
|
|
|
|
|
from .layers import ( |
|
|
TensorParallelColumnLinear, |
|
|
TensorParallelEmbedding, |
|
|
TensorParallelHead, |
|
|
TensorParallelRowLinear, |
|
|
load_layer_norm_no_bias, |
|
|
) |
|
|
from .dist import initialize_torch_distributed |
|
|
from .weights import Weights |
|
|
|
|
|
logger = logging.get_logger(__name__) |
|
|
|
|
|
_CONFIG_FOR_DOC = SRV1Config |
|
|
|
|
|
|
|
|
|
|
|
def _make_causal_mask( |
|
|
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0 |
|
|
): |
|
|
""" |
|
|
Make causal mask used for bi-directional self-attention. |
|
|
""" |
|
|
bsz, tgt_len = input_ids_shape |
|
|
mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device) |
|
|
mask_cond = torch.arange(mask.size(-1), device=device) |
|
|
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0) |
|
|
mask = mask.to(dtype) |
|
|
|
|
|
if past_key_values_length > 0: |
|
|
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1) |
|
|
return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length) |
|
|
|
|
|
|
|
|
|
|
|
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None): |
|
|
""" |
|
|
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`. |
|
|
""" |
|
|
bsz, src_len = mask.size() |
|
|
tgt_len = tgt_len if tgt_len is not None else src_len |
|
|
|
|
|
expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype) |
|
|
|
|
|
inverted_mask = 1.0 - expanded_mask |
|
|
|
|
|
return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min) |
|
|
|
|
|
|
|
|
class SRV1RMSNorm(nn.Module): |
|
|
def __init__(self, hidden_size, eps=1e-6): |
|
|
""" |
|
|
SRV1RMSNorm is equivalent to T5LayerNorm |
|
|
""" |
|
|
super().__init__() |
|
|
self.weight = nn.Parameter(torch.ones(hidden_size)) |
|
|
self.variance_epsilon = eps |
|
|
|
|
|
def forward(self, hidden_states): |
|
|
input_dtype = hidden_states.dtype |
|
|
hidden_states = hidden_states.to(torch.float32) |
|
|
variance = hidden_states.pow(2).mean(-1, keepdim=True) |
|
|
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) |
|
|
return self.weight * hidden_states.to(input_dtype) |
|
|
|
|
|
|
|
|
SRV1RMSNorm.load_no_bias = load_layer_norm_no_bias |
|
|
|
|
|
|
|
|
class SRV1RotaryEmbedding(torch.nn.Module): |
|
|
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None): |
|
|
super().__init__() |
|
|
|
|
|
self.dim = dim |
|
|
self.max_position_embeddings = max_position_embeddings |
|
|
self.base = base |
|
|
self.inv_freq = self._create_inv_freq(dim=dim, base=base, device=device) |
|
|
|
|
|
|
|
|
self._set_cos_sin_cache( |
|
|
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype() |
|
|
) |
|
|
|
|
|
def _set_cos_sin_cache(self, seq_len, device, dtype): |
|
|
self.max_seq_len_cached = seq_len |
|
|
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype) |
|
|
|
|
|
freqs = torch.einsum("i,j->ij", t, self.inv_freq) |
|
|
|
|
|
emb = torch.cat((freqs, freqs), dim=-1) |
|
|
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False) |
|
|
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False) |
|
|
|
|
|
def forward(self, x, seq_len=None): |
|
|
|
|
|
if seq_len > self.max_seq_len_cached: |
|
|
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype) |
|
|
|
|
|
return ( |
|
|
self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype), |
|
|
self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype), |
|
|
) |
|
|
|
|
|
def _create_inv_freq(self, dim, base, device): |
|
|
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, device=device, dtype=torch.float32) / dim)) |
|
|
return inv_freq |
|
|
|
|
|
class SRV1RotaryEmbedding(SRV1RotaryEmbedding): |
|
|
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0): |
|
|
self.scaling_factor = scaling_factor |
|
|
super().__init__(dim, max_position_embeddings, base, device) |
|
|
def _set_cos_sin_cache(self, seq_len, device, dtype): |
|
|
self.max_seq_len_cached = seq_len |
|
|
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype) |
|
|
t = t / self.scaling_factor |
|
|
|
|
|
freqs = torch.einsum("i,j->ij", t, self.inv_freq) |
|
|
|
|
|
|
|
|
emb = torch.cat((freqs, freqs), dim=-1) |
|
|
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False) |
|
|
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False) |
|
|
|
|
|
def rotate_half(x): |
|
|
"""Rotates half the hidden dims of the input.""" |
|
|
x1 = x[..., : x.shape[-1] // 2] |
|
|
x2 = x[..., x.shape[-1] // 2 :] |
|
|
return torch.cat((-x2, x1), dim=-1) |
|
|
|
|
|
|
|
|
def apply_rotary_pos_emb(q, k, cos, sin, position_ids): |
|
|
|
|
|
cos = cos.squeeze(1).squeeze(0) |
|
|
sin = sin.squeeze(1).squeeze(0) |
|
|
cos = cos[position_ids].unsqueeze(1) |
|
|
sin = sin[position_ids].unsqueeze(1) |
|
|
q_embed = (q * cos) + (rotate_half(q) * sin) |
|
|
k_embed = (k * cos) + (rotate_half(k) * sin) |
|
|
return q_embed, k_embed |
|
|
|
|
|
|
|
|
class SRV1MLP(nn.Module): |
|
|
def __init__(self, prefix, config: SRV1Config, weigths): |
|
|
super().__init__() |
|
|
self.gate_proj = TensorParallelColumnLinear.load( |
|
|
config=config, prefix=f"{prefix}.gate_proj", weights=weigths, bias=False |
|
|
) |
|
|
self.up_proj = TensorParallelColumnLinear.load( |
|
|
config=config, prefix=f"{prefix}.up_proj", weights=weigths, bias=False |
|
|
) |
|
|
self.down_proj = TensorParallelRowLinear.load( |
|
|
config=config, prefix=f"{prefix}.down_proj", weights=weigths, bias=False |
|
|
) |
|
|
self.act_fn = ACT2FN[config.hidden_act] |
|
|
|
|
|
def forward(self, x): |
|
|
down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x)) |
|
|
return down_proj |
|
|
|
|
|
|
|
|
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: |
|
|
""" |
|
|
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, |
|
|
num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) |
|
|
""" |
|
|
batch, num_key_value_heads, slen, head_dim = hidden_states.shape |
|
|
if n_rep == 1: |
|
|
return hidden_states |
|
|
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) |
|
|
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) |
|
|
|
|
|
|
|
|
class SRV1Attention(nn.Module): |
|
|
"""Multi-headed attention from 'Attention Is All You Need' paper""" |
|
|
|
|
|
def __init__(self, prefix, config: SRV1Config, weights): |
|
|
super().__init__() |
|
|
self.config = config |
|
|
self.hidden_size = config.hidden_size |
|
|
self.num_heads = config.num_attention_heads |
|
|
self.head_dim = self.hidden_size // self.num_heads |
|
|
self.num_key_value_heads = config.num_key_value_heads |
|
|
self.num_key_value_groups = self.num_heads // self.num_key_value_heads |
|
|
self.max_position_embeddings = config.max_position_embeddings |
|
|
self.rope_theta = getattr(config, "rope_theta", 10000) |
|
|
|
|
|
if (self.head_dim * self.num_heads) != self.hidden_size: |
|
|
raise ValueError( |
|
|
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}" |
|
|
f" and `num_heads`: {self.num_heads})." |
|
|
) |
|
|
|
|
|
|
|
|
process_group = weights.process_group |
|
|
self.hidden_size = self.hidden_size // process_group.size() |
|
|
self.num_heads = self.num_heads // process_group.size() |
|
|
self.num_key_value_heads = self.num_key_value_heads // process_group.size() |
|
|
|
|
|
self.q_proj = TensorParallelColumnLinear.load(config, prefix=f"{prefix}.q_proj", weights=weights, bias=False) |
|
|
self.k_proj = TensorParallelColumnLinear.load(config, prefix=f"{prefix}.k_proj", weights=weights, bias=False) |
|
|
self.v_proj = TensorParallelColumnLinear.load(config, prefix=f"{prefix}.v_proj", weights=weights, bias=False) |
|
|
self.o_proj = TensorParallelRowLinear.load(config, prefix=f"{prefix}.o_proj", weights=weights, bias=False) |
|
|
if self.config.rope_scaling is not None and self.config.rope_scaling['type'] == "linear": |
|
|
|
|
|
|
|
|
self.rotary_emb = SRV1RotaryEmbedding( |
|
|
self.head_dim, self.max_position_embeddings, base=self.rope_theta, scaling_factor=self.config.rope_scaling['factor'] |
|
|
) |
|
|
else: |
|
|
self.rotary_emb = SRV1RotaryEmbedding( |
|
|
self.head_dim, self.max_position_embeddings, base=self.rope_theta |
|
|
) |
|
|
|
|
|
def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int): |
|
|
return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous() |
|
|
|
|
|
def forward( |
|
|
self, |
|
|
hidden_states: torch.Tensor, |
|
|
attention_mask: Optional[torch.Tensor] = None, |
|
|
position_ids: Optional[torch.LongTensor] = None, |
|
|
past_key_value: Optional[Tuple[torch.Tensor]] = None, |
|
|
output_attentions: bool = False, |
|
|
use_cache: bool = False, |
|
|
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: |
|
|
bsz, q_len, _ = hidden_states.size() |
|
|
|
|
|
query_states = self.q_proj(hidden_states) |
|
|
key_states = self.k_proj(hidden_states) |
|
|
value_states = self.v_proj(hidden_states) |
|
|
|
|
|
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) |
|
|
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) |
|
|
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) |
|
|
|
|
|
kv_seq_len = key_states.shape[-2] |
|
|
if past_key_value is not None: |
|
|
kv_seq_len += past_key_value[0].shape[-2] |
|
|
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len) |
|
|
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) |
|
|
|
|
|
if past_key_value is not None: |
|
|
|
|
|
key_states = torch.cat([past_key_value[0], key_states], dim=2) |
|
|
value_states = torch.cat([past_key_value[1], value_states], dim=2) |
|
|
|
|
|
past_key_value = (key_states, value_states) if use_cache else None |
|
|
|
|
|
|
|
|
key_states = repeat_kv(key_states, self.num_key_value_groups) |
|
|
value_states = repeat_kv(value_states, self.num_key_value_groups) |
|
|
|
|
|
attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim) |
|
|
|
|
|
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len): |
|
|
raise ValueError( |
|
|
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is" |
|
|
f" {attn_weights.size()}" |
|
|
) |
|
|
|
|
|
if attention_mask is not None: |
|
|
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len): |
|
|
raise ValueError( |
|
|
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}" |
|
|
) |
|
|
attn_weights = attn_weights + attention_mask |
|
|
|
|
|
|
|
|
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype) |
|
|
attn_output = torch.matmul(attn_weights, value_states) |
|
|
|
|
|
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): |
|
|
raise ValueError( |
|
|
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" |
|
|
f" {attn_output.size()}" |
|
|
) |
|
|
|
|
|
attn_output = attn_output.transpose(1, 2).contiguous() |
|
|
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) |
|
|
attn_output = self.o_proj(attn_output) |
|
|
|
|
|
if not output_attentions: |
|
|
attn_weights = None |
|
|
|
|
|
return attn_output, attn_weights, past_key_value |
|
|
|
|
|
|