2024-01-03 20:26:26 +08:00
|
|
|
import copy
|
|
|
|
import math
|
2024-01-07 16:15:27 +08:00
|
|
|
import inspect
|
2024-01-20 20:04:45 +08:00
|
|
|
import os
|
|
|
|
import gc
|
|
|
|
from tqdm import auto as tqdm_lib
|
|
|
|
import json
|
2024-01-03 20:26:26 +08:00
|
|
|
from typing import TYPE_CHECKING, Optional, Tuple, Union, Callable, List, Any, Generator
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch.nn.functional as F
|
|
|
|
import torch.utils.checkpoint
|
|
|
|
|
|
|
|
from torch.nn import CrossEntropyLoss
|
|
|
|
from transformers import PreTrainedTokenizer, GenerationConfig, StoppingCriteriaList
|
|
|
|
from transformers.generation.logits_process import LogitsProcessorList
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from transformers.generation.streamers import BaseStreamer
|
|
|
|
from transformers.generation.utils import GenerateOutput
|
|
|
|
from transformers.modeling_outputs import (
|
|
|
|
BaseModelOutputWithPast,
|
|
|
|
CausalLMOutputWithPast,
|
|
|
|
)
|
|
|
|
from transformers.modeling_utils import PreTrainedModel
|
|
|
|
from transformers.utils import logging
|
|
|
|
|
|
|
|
from torch import nn
|
2024-01-07 22:49:21 +08:00
|
|
|
from einops import rearrange
|
2024-01-03 20:26:26 +08:00
|
|
|
|
2024-01-03 21:03:27 +08:00
|
|
|
from configuration_qwen import QWenConfig
|
|
|
|
from qwen_generation_utils import (
|
2024-01-03 20:26:26 +08:00
|
|
|
HistoryType,
|
|
|
|
make_context,
|
|
|
|
decode_tokens,
|
|
|
|
StopWordsLogitsProcessor,
|
|
|
|
)
|
|
|
|
|
2024-01-20 20:04:45 +08:00
|
|
|
from safetensors import safe_open
|
|
|
|
from safetensors.torch import load_file as safe_load_file
|
|
|
|
from safetensors.torch import save_file as safe_save_file
|
|
|
|
|
|
|
|
|
2024-01-13 16:50:25 +08:00
|
|
|
import sys
|
2024-01-13 17:16:43 +08:00
|
|
|
|
2024-01-13 16:50:25 +08:00
|
|
|
sys.path.append("..")
|
|
|
|
from tools import show
|
2024-01-19 14:54:48 +08:00
|
|
|
from tools import mem_tracker
|
2024-01-13 16:50:25 +08:00
|
|
|
|
2024-01-19 14:54:48 +08:00
|
|
|
# tracker = mem_tracker.MemTracker()
|
|
|
|
# tracker.track()
|
2024-01-03 20:26:26 +08:00
|
|
|
|
2024-01-07 21:54:37 +08:00
|
|
|
|
2024-01-03 20:26:26 +08:00
|
|
|
class QWenAttention(nn.Module):
|
2024-01-13 16:50:25 +08:00
|
|
|
def __init__(self, config, index):
|
2024-01-03 20:26:26 +08:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.register_buffer("masked_bias", torch.tensor(-1e4), persistent=False)
|
|
|
|
self.seq_length = config.seq_length
|
|
|
|
|
|
|
|
self.hidden_size = config.hidden_size
|
|
|
|
self.split_size = config.hidden_size
|
|
|
|
self.num_heads = config.num_attention_heads
|
|
|
|
self.head_dim = self.hidden_size // self.num_heads
|
|
|
|
|
|
|
|
self.scale_attn_weights = True
|
|
|
|
|
|
|
|
self.projection_size = config.kv_channels * config.num_attention_heads
|
|
|
|
|
|
|
|
assert self.projection_size % config.num_attention_heads == 0
|
2024-01-07 21:54:37 +08:00
|
|
|
self.hidden_size_per_attention_head = self.projection_size // config.num_attention_heads
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
self.c_attn = nn.Linear(config.hidden_size, 3 * self.projection_size)
|
2024-01-07 21:54:37 +08:00
|
|
|
self.c_proj = nn.Linear(config.hidden_size, self.projection_size, bias=not config.no_bias)
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
self.use_dynamic_ntk = config.use_dynamic_ntk
|
|
|
|
|
2024-01-07 21:54:37 +08:00
|
|
|
logn_list = [math.log(i, self.seq_length) if i > self.seq_length else 1 for i in range(1, 32768)]
|
2024-01-03 20:26:26 +08:00
|
|
|
logn_tensor = torch.tensor(logn_list)[None, :, None, None]
|
|
|
|
self.register_buffer("logn_tensor", logn_tensor, persistent=False)
|
|
|
|
|
|
|
|
self.attn_dropout = nn.Dropout(config.attn_dropout_prob)
|
2024-01-07 21:54:37 +08:00
|
|
|
self.softmax_in_fp32 = config.softmax_in_fp32 if hasattr(config, "softmax_in_fp32") else False
|
2024-01-03 20:26:26 +08:00
|
|
|
cache_dtype = torch.float
|
|
|
|
self.cache_qmax = torch.tensor(torch.iinfo(torch.uint8).max, dtype=cache_dtype)
|
|
|
|
self.cache_qmin = torch.tensor(torch.iinfo(torch.uint8).min, dtype=cache_dtype)
|
2024-01-13 16:50:25 +08:00
|
|
|
self.index = index
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
def _split_heads(self, tensor, num_heads, attn_head_size):
|
|
|
|
new_shape = tensor.size()[:-1] + (num_heads, attn_head_size)
|
|
|
|
tensor = tensor.view(new_shape)
|
|
|
|
return tensor
|
|
|
|
|
|
|
|
def _merge_heads(self, tensor, num_heads, attn_head_size):
|
|
|
|
tensor = tensor.contiguous()
|
|
|
|
new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
|
|
|
|
return tensor.view(new_shape)
|
|
|
|
|
|
|
|
def forward(
|
|
|
|
self,
|
|
|
|
hidden_states: Optional[Tuple[torch.FloatTensor]],
|
|
|
|
rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None,
|
|
|
|
):
|
|
|
|
mixed_x_layer = self.c_attn(hidden_states)
|
|
|
|
query, key, value = mixed_x_layer.split(self.split_size, dim=2)
|
|
|
|
query = self._split_heads(query, self.num_heads, self.head_dim)
|
|
|
|
key = self._split_heads(key, self.num_heads, self.head_dim)
|
|
|
|
value = self._split_heads(value, self.num_heads, self.head_dim)
|
|
|
|
|
2024-01-07 22:36:55 +08:00
|
|
|
rotary_pos_emb = rotary_pos_emb_list[0]
|
|
|
|
rotary_pos_emb = [i[:, -query.shape[1] :, :, :] for i in rotary_pos_emb]
|
|
|
|
rotary_pos_emb = (rotary_pos_emb,) * 2
|
|
|
|
q_pos_emb, k_pos_emb = rotary_pos_emb
|
|
|
|
# Slice the pos emb for current inference
|
|
|
|
query = apply_rotary_pos_emb(query, q_pos_emb)
|
|
|
|
key = apply_rotary_pos_emb(key, k_pos_emb)
|
2024-01-03 20:26:26 +08:00
|
|
|
|
2024-01-07 16:53:53 +08:00
|
|
|
key_size = key.size(1)
|
2024-01-07 22:49:21 +08:00
|
|
|
if key_size > self.seq_length and not self.training:
|
2024-01-07 16:53:53 +08:00
|
|
|
seq_start = key.size(1) - query.size(1)
|
|
|
|
seq_end = key.size(1)
|
2024-01-03 20:26:26 +08:00
|
|
|
logn_tensor = self.logn_tensor[:, seq_start:seq_end, :, :].type_as(query)
|
|
|
|
query = query * logn_tensor.expand_as(query)
|
|
|
|
|
2024-01-07 16:53:53 +08:00
|
|
|
key_size = key.size(1)
|
2024-01-20 18:08:20 +08:00
|
|
|
causal_mask = torch.tril(torch.ones((key_size, key_size), dtype=torch.bool, device=query.device)).view(
|
|
|
|
1, 1, key_size, key_size
|
|
|
|
)
|
2024-01-07 16:22:41 +08:00
|
|
|
query = query.permute(0, 2, 1, 3)
|
2024-01-07 16:53:53 +08:00
|
|
|
key = key.permute(0, 2, 1, 3)
|
|
|
|
value = value.permute(0, 2, 1, 3)
|
|
|
|
|
2024-01-13 16:50:25 +08:00
|
|
|
# qk = query @ key.transpose(-2, -1)
|
|
|
|
# qk = qk[0]
|
2024-01-20 18:08:20 +08:00
|
|
|
# prePath = "../generated/query_matmul_key/img/"
|
|
|
|
# show.DumpTensorToImage(
|
|
|
|
# qk, prePath + "q_matmul_k_sequence_" + str(key_size) + "_layer_" + str(self.index) + ".png"
|
|
|
|
# )
|
2024-01-13 16:50:25 +08:00
|
|
|
|
2024-01-20 18:08:20 +08:00
|
|
|
attn_output = F.scaled_dot_product_attention(query, key, value, attn_mask=causal_mask).transpose(1, 2)
|
2024-01-07 16:23:04 +08:00
|
|
|
context_layer = self._merge_heads(attn_output, self.num_heads, self.head_dim)
|
2024-01-03 20:26:26 +08:00
|
|
|
attn_output = self.c_proj(context_layer)
|
2024-01-19 14:54:48 +08:00
|
|
|
|
|
|
|
return attn_output
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
class QWenMLP(nn.Module):
|
|
|
|
def __init__(self, config):
|
|
|
|
super().__init__()
|
|
|
|
ff_dim_in = config.intermediate_size // 2
|
2024-01-07 22:36:55 +08:00
|
|
|
self.w1 = nn.Linear(config.hidden_size, ff_dim_in, bias=not config.no_bias)
|
|
|
|
self.w2 = nn.Linear(config.hidden_size, ff_dim_in, bias=not config.no_bias)
|
2024-01-03 20:26:26 +08:00
|
|
|
self.c_proj = nn.Linear(ff_dim_in, config.hidden_size, bias=not config.no_bias)
|
|
|
|
|
|
|
|
def forward(self, hidden_states):
|
|
|
|
a1 = self.w1(hidden_states)
|
|
|
|
a2 = self.w2(hidden_states)
|
|
|
|
intermediate_parallel = a1 * F.silu(a2)
|
|
|
|
output = self.c_proj(intermediate_parallel)
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
class QWenBlock(nn.Module):
|
2024-01-13 16:50:25 +08:00
|
|
|
def __init__(self, config, index):
|
2024-01-03 20:26:26 +08:00
|
|
|
super().__init__()
|
|
|
|
hidden_size = config.hidden_size
|
|
|
|
|
|
|
|
self.ln_1 = RMSNorm(
|
|
|
|
hidden_size,
|
|
|
|
eps=config.layer_norm_epsilon,
|
|
|
|
)
|
2024-01-13 16:50:25 +08:00
|
|
|
self.attn = QWenAttention(config, index)
|
2024-01-03 20:26:26 +08:00
|
|
|
self.ln_2 = RMSNorm(
|
|
|
|
hidden_size,
|
|
|
|
eps=config.layer_norm_epsilon,
|
|
|
|
)
|
|
|
|
self.mlp = QWenMLP(config)
|
2024-01-13 16:50:25 +08:00
|
|
|
self.index = index
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
def forward(
|
|
|
|
self,
|
|
|
|
hidden_states: Optional[Tuple[torch.FloatTensor]],
|
|
|
|
rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None,
|
|
|
|
):
|
|
|
|
layernorm_output = self.ln_1(hidden_states)
|
|
|
|
|
2024-01-20 18:08:20 +08:00
|
|
|
attn_outputs = self.attn(layernorm_output, rotary_pos_emb_list)
|
2024-01-03 20:26:26 +08:00
|
|
|
attn_output = attn_outputs[0]
|
|
|
|
residual = hidden_states
|
|
|
|
layernorm_input = attn_output + residual
|
|
|
|
|
|
|
|
layernorm_output = self.ln_2(layernorm_input)
|
|
|
|
residual = layernorm_input
|
|
|
|
mlp_output = self.mlp(layernorm_output)
|
|
|
|
hidden_states = residual + mlp_output
|
2024-01-19 14:54:48 +08:00
|
|
|
return hidden_states
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
|
2024-01-20 20:04:45 +08:00
|
|
|
class QWenPreTrainedModel(nn.Module):
|
2024-01-03 20:26:26 +08:00
|
|
|
config_class = QWenConfig
|
|
|
|
base_model_prefix = "transformer"
|
|
|
|
is_parallelizable = False
|
|
|
|
supports_gradient_checkpointing = True
|
|
|
|
_no_split_modules = ["QWenBlock"]
|
|
|
|
|
|
|
|
def __init__(self, *inputs, **kwargs):
|
2024-01-20 20:04:45 +08:00
|
|
|
super().__init__()
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
|
2024-01-07 21:54:37 +08:00
|
|
|
class QWenModel(QWenPreTrainedModel):
|
2024-01-03 20:26:26 +08:00
|
|
|
def __init__(self, config):
|
|
|
|
super().__init__(config)
|
|
|
|
self.vocab_size = config.vocab_size
|
|
|
|
self.num_hidden_layers = config.num_hidden_layers
|
|
|
|
self.embed_dim = config.hidden_size
|
|
|
|
|
|
|
|
self.use_dynamic_ntk = config.use_dynamic_ntk
|
|
|
|
self.seq_length = config.seq_length
|
|
|
|
|
|
|
|
self.wte = nn.Embedding(self.vocab_size, self.embed_dim)
|
|
|
|
|
|
|
|
self.drop = nn.Dropout(config.emb_dropout_prob)
|
|
|
|
|
|
|
|
if config.rotary_pct == 1.0:
|
|
|
|
self.rotary_ndims = None
|
|
|
|
else:
|
|
|
|
assert config.rotary_pct < 1
|
2024-01-07 16:23:04 +08:00
|
|
|
self.rotary_ndims = int(config.kv_channels * config.rotary_pct)
|
|
|
|
dim = self.rotary_ndims if self.rotary_ndims is not None else config.kv_channels
|
2024-01-03 20:26:26 +08:00
|
|
|
self.rotary_emb = RotaryEmbedding(dim, base=config.rotary_emb_base)
|
|
|
|
|
2024-01-13 16:50:25 +08:00
|
|
|
self.h = nn.ModuleList([QWenBlock(config, i) for i in range(config.num_hidden_layers)])
|
2024-01-03 20:26:26 +08:00
|
|
|
self.ln_f = RMSNorm(
|
|
|
|
self.embed_dim,
|
|
|
|
eps=config.layer_norm_epsilon,
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_ntk_alpha(self, true_seq_len):
|
|
|
|
context_value = math.log(true_seq_len / self.seq_length, 2) + 1
|
|
|
|
ntk_alpha = 2 ** math.ceil(context_value) - 1
|
|
|
|
ntk_alpha = max(ntk_alpha, 1)
|
|
|
|
return ntk_alpha
|
|
|
|
|
|
|
|
def forward(
|
|
|
|
self,
|
|
|
|
input_ids: Optional[torch.LongTensor] = None,
|
|
|
|
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
|
|
):
|
|
|
|
if input_ids is not None and inputs_embeds is not None:
|
2024-01-07 21:54:37 +08:00
|
|
|
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
2024-01-03 20:26:26 +08:00
|
|
|
elif input_ids is not None:
|
|
|
|
input_shape = input_ids.size()
|
|
|
|
input_ids = input_ids.view(-1, input_shape[-1])
|
|
|
|
batch_size = input_ids.shape[0]
|
|
|
|
elif inputs_embeds is not None:
|
|
|
|
input_shape = inputs_embeds.size()[:-1]
|
|
|
|
batch_size = inputs_embeds.shape[0]
|
|
|
|
else:
|
|
|
|
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
|
|
|
|
|
|
|
if inputs_embeds is None:
|
|
|
|
inputs_embeds = self.wte(input_ids)
|
|
|
|
hidden_states = inputs_embeds
|
|
|
|
|
|
|
|
kv_seq_len = hidden_states.size()[1]
|
|
|
|
|
|
|
|
if self.training or not self.use_dynamic_ntk:
|
|
|
|
ntk_alpha_list = [1.0]
|
|
|
|
elif kv_seq_len != hidden_states.size()[1]:
|
|
|
|
ntk_alpha_list = self.rotary_emb._ntk_alpha_cached_list
|
|
|
|
else:
|
|
|
|
ntk_alpha_list = []
|
2024-01-20 18:08:20 +08:00
|
|
|
ntk_alpha = self.get_ntk_alpha(kv_seq_len)
|
|
|
|
ntk_alpha_list.append(ntk_alpha)
|
2024-01-03 20:26:26 +08:00
|
|
|
self.rotary_emb._ntk_alpha_cached_list = ntk_alpha_list
|
2024-01-07 21:54:37 +08:00
|
|
|
rotary_pos_emb_list = [self.rotary_emb(kv_seq_len, ntk_alpha=ntk_alpha) for ntk_alpha in ntk_alpha_list]
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
hidden_states = self.drop(hidden_states)
|
|
|
|
output_shape = input_shape + (hidden_states.size(-1),)
|
|
|
|
|
2024-01-07 21:43:02 +08:00
|
|
|
all_hidden_states = None
|
2024-01-19 14:54:48 +08:00
|
|
|
for block in self.h:
|
2024-01-20 18:08:20 +08:00
|
|
|
hidden_states = block(hidden_states, rotary_pos_emb_list=rotary_pos_emb_list)
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
hidden_states = self.ln_f(hidden_states)
|
|
|
|
hidden_states = hidden_states.view(output_shape)
|
2024-01-18 20:23:21 +08:00
|
|
|
return BaseModelOutputWithPast(last_hidden_state=hidden_states, hidden_states=all_hidden_states)
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
|
2024-01-20 20:04:45 +08:00
|
|
|
class QWenLMHeadModel(nn.Module):
|
2024-01-03 20:26:26 +08:00
|
|
|
def __init__(self, config):
|
2024-01-20 20:04:45 +08:00
|
|
|
super().__init__()
|
|
|
|
self.config = config
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
self.transformer = QWenModel(config)
|
|
|
|
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
2024-01-20 20:04:45 +08:00
|
|
|
self.generation_config = GenerationConfig.from_model_config(config)
|
2024-01-03 20:26:26 +08:00
|
|
|
|
2024-01-18 20:23:21 +08:00
|
|
|
def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, **kwargs):
|
|
|
|
model_inputs = {"input_ids": input_ids}
|
2024-01-03 20:26:26 +08:00
|
|
|
return model_inputs
|
|
|
|
|
|
|
|
def forward(
|
|
|
|
self,
|
|
|
|
input_ids: Optional[torch.LongTensor] = None,
|
|
|
|
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
|
|
labels: Optional[torch.LongTensor] = None,
|
|
|
|
) -> Union[Tuple, CausalLMOutputWithPast]:
|
|
|
|
transformer_outputs = self.transformer(
|
|
|
|
input_ids,
|
|
|
|
inputs_embeds=inputs_embeds,
|
|
|
|
)
|
|
|
|
hidden_states = transformer_outputs[0]
|
|
|
|
|
|
|
|
lm_logits = self.lm_head(hidden_states)
|
|
|
|
|
|
|
|
loss = None
|
|
|
|
if labels is not None:
|
|
|
|
labels = labels.to(lm_logits.device)
|
|
|
|
shift_logits = lm_logits[..., :-1, :].contiguous()
|
|
|
|
shift_labels = labels[..., 1:].contiguous()
|
|
|
|
loss_fct = CrossEntropyLoss()
|
2024-01-07 21:54:37 +08:00
|
|
|
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
|
2024-01-03 20:26:26 +08:00
|
|
|
|
2024-01-07 17:28:15 +08:00
|
|
|
# shift_labels = torch.ones([1,19]).to(lm_logits.device).to(torch.int64)
|
|
|
|
# shift_logits = lm_logits[..., :-1, :].contiguous()
|
|
|
|
# loss_fct = CrossEntropyLoss()
|
|
|
|
# loss = loss_fct(
|
|
|
|
# shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)
|
|
|
|
# )
|
|
|
|
# loss.backward()
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
return CausalLMOutputWithPast(
|
|
|
|
loss=loss,
|
|
|
|
logits=lm_logits,
|
|
|
|
hidden_states=transformer_outputs.hidden_states,
|
|
|
|
attentions=transformer_outputs.attentions,
|
|
|
|
)
|
|
|
|
|
2024-01-20 20:04:45 +08:00
|
|
|
def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.PathLike]]):
|
|
|
|
load_in_8bit = False
|
|
|
|
load_in_4bit = False
|
|
|
|
|
|
|
|
pretrained_model_name_or_path = str(pretrained_model_name_or_path)
|
|
|
|
resolved_archive_file = os.path.join(pretrained_model_name_or_path, "model.safetensors.index.json")
|
|
|
|
print(f"loading weights file {resolved_archive_file}")
|
|
|
|
with open(resolved_archive_file, "r") as f:
|
|
|
|
index = json.loads(f.read())
|
|
|
|
shard_filenames = sorted(set(index["weight_map"].values()))
|
|
|
|
resolved_archive_file = [os.path.join(pretrained_model_name_or_path, f) for f in shard_filenames]
|
|
|
|
model = cls._load_pretrained_model(resolved_archive_file)
|
|
|
|
model.is_loaded_in_4bit = load_in_4bit
|
|
|
|
model.is_loaded_in_8bit = load_in_8bit
|
|
|
|
return model
|
|
|
|
|
|
|
|
def _load_state_dict_into_model(self, model_to_load, state_dict, start_prefix):
|
|
|
|
metadata = getattr(state_dict, "_metadata", None)
|
|
|
|
state_dict = state_dict.copy()
|
|
|
|
if metadata is not None:
|
|
|
|
state_dict._metadata = metadata
|
|
|
|
error_msgs = []
|
|
|
|
|
|
|
|
def load(module: nn.Module, state_dict, prefix=""):
|
|
|
|
local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {})
|
|
|
|
args = (state_dict, prefix, local_metadata, True, [], [], error_msgs)
|
|
|
|
if len([key for key in state_dict if key.startswith(prefix)]) > 0:
|
|
|
|
module._load_from_state_dict(*args)
|
|
|
|
|
|
|
|
for name, child in module._modules.items():
|
|
|
|
if child is not None:
|
|
|
|
load(child, state_dict, prefix + name + ".")
|
|
|
|
|
|
|
|
load(model_to_load, state_dict, prefix=start_prefix)
|
|
|
|
del state_dict
|
|
|
|
return error_msgs
|
|
|
|
|
|
|
|
def _load_pretrained_model(cls, resolved_archive_file):
|
|
|
|
start_prefix = ""
|
|
|
|
model_to_load = cls
|
|
|
|
error_msgs = []
|
|
|
|
if len(resolved_archive_file) > 1:
|
|
|
|
resolved_archive_file = tqdm_lib.tqdm(resolved_archive_file, desc="Loading checkpoint shards")
|
|
|
|
for shard_file in resolved_archive_file:
|
|
|
|
state_dict = safe_load_file(shard_file)
|
|
|
|
|
|
|
|
error_msgs += cls._load_state_dict_into_model(model_to_load, state_dict, start_prefix)
|
|
|
|
del state_dict # force memory release
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
print(f"All model checkpoint weights were used when initializing {cls.__class__.__name__}.\n")
|
|
|
|
return cls
|
|
|
|
|
2024-01-19 14:54:48 +08:00
|
|
|
@torch.no_grad()
|
2024-01-03 20:26:26 +08:00
|
|
|
def chat(
|
|
|
|
self,
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
|
|
|
query: str,
|
2024-01-10 19:35:46 +08:00
|
|
|
query_assistant: str,
|
2024-01-03 20:26:26 +08:00
|
|
|
history: Optional[HistoryType],
|
|
|
|
system: str = "You are a helpful assistant.",
|
|
|
|
**kwargs,
|
|
|
|
) -> Tuple[str, HistoryType]:
|
2024-01-10 21:16:54 +08:00
|
|
|
generation_config = self.generation_config
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
if history is None:
|
|
|
|
history = []
|
|
|
|
else:
|
|
|
|
history = copy.deepcopy(history)
|
|
|
|
|
2024-01-10 21:16:54 +08:00
|
|
|
stop_words_ids = []
|
2024-01-03 20:26:26 +08:00
|
|
|
|
2024-01-20 20:04:45 +08:00
|
|
|
raw_text, context_tokens = make_context(tokenizer, query, query_assistant, history=history, system=system)
|
2024-01-03 20:26:26 +08:00
|
|
|
|
2024-01-10 21:16:54 +08:00
|
|
|
stop_words_ids.extend([[tokenizer.im_end_id], [tokenizer.im_start_id]])
|
2024-01-20 20:04:45 +08:00
|
|
|
input_ids = torch.tensor([context_tokens]).to(next(self.parameters()).device)
|
2024-01-03 20:26:26 +08:00
|
|
|
outputs = self.generate(
|
2024-01-07 16:23:04 +08:00
|
|
|
input_ids,
|
|
|
|
stop_words_ids=stop_words_ids,
|
2024-01-20 18:08:20 +08:00
|
|
|
tokenizer=tokenizer,
|
2024-01-07 16:23:04 +08:00
|
|
|
**kwargs,
|
|
|
|
)
|
2024-01-10 19:35:46 +08:00
|
|
|
decoded, response, end_reason = decode_tokens(
|
2024-01-03 20:26:26 +08:00
|
|
|
outputs[0],
|
|
|
|
tokenizer,
|
|
|
|
raw_text_len=len(raw_text),
|
|
|
|
context_length=len(context_tokens),
|
2024-01-07 16:23:04 +08:00
|
|
|
errors="replace",
|
2024-01-03 20:26:26 +08:00
|
|
|
)
|
|
|
|
history.append((query, response))
|
2024-01-10 19:35:46 +08:00
|
|
|
return response, history, decoded
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
def generate(
|
|
|
|
self,
|
2024-01-20 20:04:45 +08:00
|
|
|
input_ids: Optional[torch.Tensor] = None,
|
2024-01-13 17:16:43 +08:00
|
|
|
stop_words_ids=[],
|
2024-01-20 18:08:20 +08:00
|
|
|
tokenizer=None,
|
2024-01-07 21:54:37 +08:00
|
|
|
prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None,
|
2024-01-03 20:26:26 +08:00
|
|
|
**kwargs,
|
|
|
|
) -> Union[GenerateOutput, torch.LongTensor]:
|
2024-01-10 21:16:54 +08:00
|
|
|
generation_config = self.generation_config
|
2024-01-07 16:15:27 +08:00
|
|
|
generation_config = copy.deepcopy(generation_config)
|
2024-01-07 21:54:37 +08:00
|
|
|
model_kwargs = generation_config.update(**kwargs) # All unused kwargs must be model kwargs
|
2024-01-07 16:15:27 +08:00
|
|
|
generation_config.validate()
|
|
|
|
|
2024-01-13 17:16:43 +08:00
|
|
|
pad_token_id = generation_config.pad_token_id
|
|
|
|
eos_token_id_tensor = torch.tensor([generation_config.eos_token_id]).to(input_ids.device)
|
|
|
|
|
2024-01-07 17:32:24 +08:00
|
|
|
scores = None
|
2024-01-07 16:15:27 +08:00
|
|
|
# keep track of which sequences are already finished
|
2024-01-07 21:54:37 +08:00
|
|
|
unfinished_sequences = torch.ones(input_ids.shape[0], dtype=torch.long, device=input_ids.device)
|
2024-01-07 16:15:27 +08:00
|
|
|
|
2024-01-07 22:36:55 +08:00
|
|
|
this_peer_finished = False
|
2024-01-07 16:15:27 +08:00
|
|
|
# auto-regressive generation
|
|
|
|
while True:
|
|
|
|
model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
|
|
|
|
|
|
|
|
# forward pass to get next token
|
2024-01-07 22:36:55 +08:00
|
|
|
outputs = self(**model_inputs)
|
2024-01-11 15:00:18 +08:00
|
|
|
next_token_scores = outputs.logits[:, -1, :]
|
2024-01-07 16:15:27 +08:00
|
|
|
|
2024-01-20 20:20:18 +08:00
|
|
|
# repetition_penalty
|
2024-01-20 20:04:45 +08:00
|
|
|
penalty = self.config.repetition_penalty
|
|
|
|
score = torch.gather(next_token_scores, 1, input_ids)
|
|
|
|
# if score < 0 then repetition penalty has to be multiplied to reduce the token probabilities
|
|
|
|
score = torch.where(score < 0, score * penalty, score / penalty)
|
|
|
|
next_token_scores = next_token_scores.scatter_(1, input_ids, score)
|
|
|
|
|
2024-01-20 20:20:18 +08:00
|
|
|
# top_p
|
2024-01-20 20:04:45 +08:00
|
|
|
top_p = self.config.top_p
|
|
|
|
filter_value = -float("Inf")
|
|
|
|
min_tokens_to_keep = 1
|
|
|
|
sorted_logits, sorted_indices = torch.sort(next_token_scores, descending=False)
|
|
|
|
cumulative_probs = sorted_logits.softmax(dim=-1).cumsum(dim=-1)
|
|
|
|
# Remove tokens with cumulative top_p above the threshold (token with 0 are kept)
|
|
|
|
sorted_indices_to_remove = cumulative_probs <= (1 - top_p)
|
|
|
|
# Keep at least min_tokens_to_keep
|
|
|
|
sorted_indices_to_remove[..., -min_tokens_to_keep:] = 0
|
|
|
|
# scatter sorted tensors to original indexing
|
|
|
|
indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
|
|
|
|
next_token_scores = next_token_scores.masked_fill(indices_to_remove, filter_value)
|
2024-01-07 16:15:27 +08:00
|
|
|
|
|
|
|
# sample
|
|
|
|
probs = nn.functional.softmax(next_token_scores, dim=-1)
|
|
|
|
next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1)
|
|
|
|
|
2024-01-13 16:50:25 +08:00
|
|
|
next_tokens = next_tokens * unfinished_sequences + pad_token_id * (1 - unfinished_sequences)
|
2024-01-07 16:15:27 +08:00
|
|
|
|
|
|
|
# update generated ids, model inputs, and length for next step
|
|
|
|
input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1)
|
|
|
|
|
2024-01-13 16:50:25 +08:00
|
|
|
unfinished_sequences = unfinished_sequences.mul(
|
|
|
|
next_tokens.tile(eos_token_id_tensor.shape[0], 1).ne(eos_token_id_tensor.unsqueeze(1)).prod(dim=0)
|
|
|
|
)
|
2024-01-07 16:15:27 +08:00
|
|
|
|
2024-01-20 18:08:20 +08:00
|
|
|
# decoded, response, end_reason = decode_tokens(
|
|
|
|
# next_tokens,
|
|
|
|
# tokenizer,
|
|
|
|
# raw_text_len=0,
|
|
|
|
# context_length=0,
|
|
|
|
# errors="replace",
|
|
|
|
# )
|
|
|
|
# print(decoded)
|
|
|
|
|
2024-01-13 16:50:25 +08:00
|
|
|
# stop when each sentence is finished
|
|
|
|
if unfinished_sequences.max() == 0:
|
|
|
|
this_peer_finished = True
|
2024-01-07 16:15:27 +08:00
|
|
|
|
2024-01-07 16:53:53 +08:00
|
|
|
if this_peer_finished:
|
2024-01-07 16:15:27 +08:00
|
|
|
break
|
|
|
|
return input_ids
|
|
|
|
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
class RotaryEmbedding(torch.nn.Module):
|
|
|
|
def __init__(self, dim, base=10000):
|
|
|
|
super().__init__()
|
|
|
|
self.dim = dim
|
|
|
|
self.base = base
|
|
|
|
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
|
|
|
|
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
|
|
|
self._rotary_pos_emb_cache = None
|
|
|
|
self._seq_len_cached = 0
|
|
|
|
self._ntk_alpha_cached = 1.0
|
|
|
|
self._ntk_alpha_cached_list = [1.0]
|
|
|
|
|
|
|
|
def update_rotary_pos_emb_cache(self, seqlen, ntk_alpha=1.0):
|
|
|
|
if seqlen > self._seq_len_cached or ntk_alpha != self._ntk_alpha_cached:
|
|
|
|
base = self.base * ntk_alpha ** (self.dim / (self.dim - 2))
|
|
|
|
self.inv_freq = 1.0 / (
|
2024-01-07 21:54:37 +08:00
|
|
|
base ** (torch.arange(0, self.dim, 2, device=self.inv_freq.device).float() / self.dim)
|
2024-01-03 20:26:26 +08:00
|
|
|
)
|
|
|
|
self._seq_len_cached = max(2 * seqlen, 16)
|
|
|
|
self._ntk_alpha_cached = ntk_alpha
|
|
|
|
seq = torch.arange(self._seq_len_cached, device=self.inv_freq.device)
|
|
|
|
freqs = torch.outer(seq.type_as(self.inv_freq), self.inv_freq)
|
|
|
|
|
|
|
|
emb = torch.cat((freqs, freqs), dim=-1)
|
|
|
|
emb = rearrange(emb, "n d -> 1 n 1 d")
|
|
|
|
|
|
|
|
cos, sin = emb.cos(), emb.sin()
|
|
|
|
self._rotary_pos_emb_cache = [cos, sin]
|
|
|
|
|
|
|
|
def forward(self, max_seq_len, ntk_alpha=1.0):
|
|
|
|
self.update_rotary_pos_emb_cache(max_seq_len, ntk_alpha)
|
|
|
|
cos, sin = self._rotary_pos_emb_cache
|
|
|
|
return [cos[:, :max_seq_len], sin[:, :max_seq_len]]
|
|
|
|
|
|
|
|
|
|
|
|
def _rotate_half(x):
|
|
|
|
x = rearrange(x, "... (j d) -> ... j d", j=2)
|
|
|
|
x1, x2 = x.unbind(dim=-2)
|
|
|
|
return torch.cat((-x2, x1), dim=-1)
|
|
|
|
|
|
|
|
|
|
|
|
def apply_rotary_pos_emb(t, freqs):
|
|
|
|
rot_dim = freqs[0].shape[-1]
|
|
|
|
cos, sin = freqs
|
|
|
|
t_float = t.float()
|
2024-01-07 16:53:53 +08:00
|
|
|
t_rot, t_pass = t_float[..., :rot_dim], t_float[..., rot_dim:]
|
|
|
|
t_rot = (t_rot * cos) + (_rotate_half(t_rot) * sin)
|
|
|
|
return torch.cat((t_rot, t_pass), dim=-1).type_as(t)
|
2024-01-03 20:26:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RMSNorm(torch.nn.Module):
|
|
|
|
def __init__(self, dim: int, eps: float = 1e-6):
|
|
|
|
super().__init__()
|
|
|
|
self.eps = eps
|
|
|
|
self.weight = nn.Parameter(torch.ones(dim))
|
|
|
|
|
|
|
|
def _norm(self, x):
|
|
|
|
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
|
|
|
|
|
|
|
def forward(self, x):
|
2024-01-07 16:53:53 +08:00
|
|
|
output = self._norm(x.float()).type_as(x)
|
|
|
|
return output * self.weight
|