|
|
@ -28,27 +28,17 @@ from torch import nn
|
|
|
|
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
|
|
|
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
|
|
|
|
|
|
|
|
|
|
|
from transformers.activations import ACT2FN
|
|
|
|
from transformers.activations import ACT2FN
|
|
|
|
from transformers.modeling_outputs import (
|
|
|
|
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
|
|
|
|
BaseModelOutputWithPast,
|
|
|
|
|
|
|
|
CausalLMOutputWithPast,
|
|
|
|
|
|
|
|
SequenceClassifierOutputWithPast,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
from transformers.modeling_utils import PreTrainedModel
|
|
|
|
from transformers.modeling_utils import PreTrainedModel
|
|
|
|
from transformers.generation.streamers import BaseStreamer
|
|
|
|
from transformers.generation.streamers import BaseStreamer
|
|
|
|
from transformers.utils import (
|
|
|
|
from transformers.utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
|
|
|
|
add_start_docstrings,
|
|
|
|
from .configuration_internlm import InternLMConfig
|
|
|
|
add_start_docstrings_to_model_forward,
|
|
|
|
|
|
|
|
logging,
|
|
|
|
|
|
|
|
replace_return_docstrings,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
from configuration_internlm import InternLMConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.get_logger(__name__)
|
|
|
|
logger = logging.get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
_CONFIG_FOR_DOC = "InternLMConfig"
|
|
|
|
_CONFIG_FOR_DOC = "InternLMConfig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
|
|
|
|
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
|
|
|
|
def _make_causal_mask(
|
|
|
|
def _make_causal_mask(
|
|
|
|
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
|
|
|
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
|
|
@ -106,7 +96,7 @@ class InternLMRotaryEmbedding(torch.nn.Module):
|
|
|
|
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
|
|
|
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
|
|
|
super().__init__()
|
|
|
|
super().__init__()
|
|
|
|
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
|
|
|
|
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
|
|
|
|
self.register_buffer("inv_freq", inv_freq)
|
|
|
|
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
|
|
|
|
|
|
|
|
|
|
|
# Build here to make `torch.jit.trace` work.
|
|
|
|
# Build here to make `torch.jit.trace` work.
|
|
|
|
self.max_seq_len_cached = max_position_embeddings
|
|
|
|
self.max_seq_len_cached = max_position_embeddings
|
|
|
@ -332,11 +322,9 @@ INTERNLM_START_DOCSTRING = r"""
|
|
|
|
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
|
|
|
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
|
|
|
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
|
|
|
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
|
|
|
etc.)
|
|
|
|
etc.)
|
|
|
|
|
|
|
|
|
|
|
|
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
|
|
|
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
|
|
|
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
|
|
|
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
|
|
|
and behavior.
|
|
|
|
and behavior.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
Parameters:
|
|
|
|
config ([`InternLMConfig`]):
|
|
|
|
config ([`InternLMConfig`]):
|
|
|
|
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
|
|
|
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
|
|
@ -377,44 +365,33 @@ INTERNLM_INPUTS_DOCSTRING = r"""
|
|
|
|
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
|
|
|
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
|
|
|
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
|
|
|
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
|
|
|
it.
|
|
|
|
it.
|
|
|
|
|
|
|
|
|
|
|
|
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
|
|
|
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
|
|
|
[`PreTrainedTokenizer.__call__`] for details.
|
|
|
|
[`PreTrainedTokenizer.__call__`] for details.
|
|
|
|
|
|
|
|
|
|
|
|
[What are input IDs?](../glossary#input-ids)
|
|
|
|
[What are input IDs?](../glossary#input-ids)
|
|
|
|
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
|
|
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
|
|
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
|
|
|
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
|
|
|
|
|
|
|
|
|
|
|
- 1 for tokens that are **not masked**,
|
|
|
|
- 1 for tokens that are **not masked**,
|
|
|
|
- 0 for tokens that are **masked**.
|
|
|
|
- 0 for tokens that are **masked**.
|
|
|
|
|
|
|
|
|
|
|
|
[What are attention masks?](../glossary#attention-mask)
|
|
|
|
[What are attention masks?](../glossary#attention-mask)
|
|
|
|
|
|
|
|
|
|
|
|
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
|
|
|
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
|
|
|
[`PreTrainedTokenizer.__call__`] for details.
|
|
|
|
[`PreTrainedTokenizer.__call__`] for details.
|
|
|
|
|
|
|
|
|
|
|
|
If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
|
|
|
|
If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
|
|
|
|
`past_key_values`).
|
|
|
|
`past_key_values`).
|
|
|
|
|
|
|
|
|
|
|
|
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
|
|
|
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
|
|
|
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
|
|
|
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
|
|
|
information on the default strategy.
|
|
|
|
information on the default strategy.
|
|
|
|
|
|
|
|
|
|
|
|
- 1 indicates the head is **not masked**,
|
|
|
|
- 1 indicates the head is **not masked**,
|
|
|
|
- 0 indicates the head is **masked**.
|
|
|
|
- 0 indicates the head is **masked**.
|
|
|
|
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
|
|
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
|
|
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
|
|
|
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
|
|
|
config.n_positions - 1]`.
|
|
|
|
config.n_positions - 1]`.
|
|
|
|
|
|
|
|
|
|
|
|
[What are position IDs?](../glossary#position-ids)
|
|
|
|
[What are position IDs?](../glossary#position-ids)
|
|
|
|
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
|
|
|
|
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
|
|
|
|
Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
|
|
|
|
Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
|
|
|
|
`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
|
|
|
|
`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
|
|
|
|
`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
|
|
|
|
`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
|
|
|
|
|
|
|
|
|
|
|
|
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
|
|
|
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
|
|
|
blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
|
|
|
|
blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
|
|
|
|
|
|
|
|
|
|
|
|
If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
|
|
|
|
If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
|
|
|
|
don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
|
|
|
|
don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
|
|
|
|
`decoder_input_ids` of shape `(batch_size, sequence_length)`.
|
|
|
|
`decoder_input_ids` of shape `(batch_size, sequence_length)`.
|
|
|
@ -443,11 +420,9 @@ INTERNLM_INPUTS_DOCSTRING = r"""
|
|
|
|
class InternLMModel(InternLMPreTrainedModel):
|
|
|
|
class InternLMModel(InternLMPreTrainedModel):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`InternLMDecoderLayer`]
|
|
|
|
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`InternLMDecoderLayer`]
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
config: InternLMConfig
|
|
|
|
config: InternLMConfig
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
_auto_class = "AutoModel"
|
|
|
|
_auto_class = "AutoModel"
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config: InternLMConfig):
|
|
|
|
def __init__(self, config: InternLMConfig):
|
|
|
@ -673,20 +648,14 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
|
|
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
|
|
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
|
|
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
|
|
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
|
|
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
|
|
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
>>> from transformers import AutoTokenizer, InternLMForCausalLM
|
|
|
|
>>> from transformers import AutoTokenizer, InternLMForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
>>> model = InternLMForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
|
|
|
>>> model = InternLMForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
|
|
|
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
|
|
|
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
|
|
|
|
|
|
|
|
|
|
|
>>> prompt = "Hey, are you consciours? Can you talk to me?"
|
|
|
|
>>> prompt = "Hey, are you consciours? Can you talk to me?"
|
|
|
|
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
>>> # Generate
|
|
|
|
>>> # Generate
|
|
|
|
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
|
|
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
|
|
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
|
|
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
|
@ -780,15 +749,12 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
|
|
def build_inputs(self, tokenizer, query: str, history: List[Tuple[str, str]] = []):
|
|
|
|
def build_inputs(self, tokenizer, query: str, history: List[Tuple[str, str]] = []):
|
|
|
|
prompt = ""
|
|
|
|
prompt = ""
|
|
|
|
for record in history:
|
|
|
|
for record in history:
|
|
|
|
prompt += f"""<s><|User|>:{record[0]}<eoh>\n<|Bot|>:{record[1]}<eoa>\n"""
|
|
|
|
prompt += f"""<|User|>:{record[0]}<eoh>\n<|Bot|>:{record[1]}<eoa>\n"""
|
|
|
|
if len(prompt) == 0:
|
|
|
|
|
|
|
|
prompt += "<s>"
|
|
|
|
|
|
|
|
prompt += f"""<|User|>:{query}<eoh>\n<|Bot|>:"""
|
|
|
|
prompt += f"""<|User|>:{query}<eoh>\n<|Bot|>:"""
|
|
|
|
return tokenizer([prompt], return_tensors="pt")
|
|
|
|
return tokenizer([prompt], return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
@torch.no_grad()
|
|
|
|
def chat(
|
|
|
|
def chat(self,
|
|
|
|
self,
|
|
|
|
|
|
|
|
tokenizer,
|
|
|
|
tokenizer,
|
|
|
|
query: str,
|
|
|
|
query: str,
|
|
|
|
history: List[Tuple[str, str]] = [],
|
|
|
|
history: List[Tuple[str, str]] = [],
|
|
|
@ -797,28 +763,24 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
|
|
do_sample: bool = True,
|
|
|
|
do_sample: bool = True,
|
|
|
|
temperature: float = 0.8,
|
|
|
|
temperature: float = 0.8,
|
|
|
|
top_p: float = 0.8,
|
|
|
|
top_p: float = 0.8,
|
|
|
|
**kwargs,
|
|
|
|
**kwargs):
|
|
|
|
):
|
|
|
|
|
|
|
|
inputs = self.build_inputs(tokenizer, query, history)
|
|
|
|
inputs = self.build_inputs(tokenizer, query, history)
|
|
|
|
inputs = {k: v.to(self.device) for k, v in inputs.items() if torch.is_tensor(v)}
|
|
|
|
inputs = {k: v.to(self.device) for k, v in inputs.items() if torch.is_tensor(v)}
|
|
|
|
outputs = self.generate(
|
|
|
|
outputs = self.generate(**inputs,
|
|
|
|
**inputs,
|
|
|
|
|
|
|
|
streamer=streamer,
|
|
|
|
streamer=streamer,
|
|
|
|
max_new_tokens=max_new_tokens,
|
|
|
|
max_new_tokens=max_new_tokens,
|
|
|
|
do_sample=do_sample,
|
|
|
|
do_sample=do_sample,
|
|
|
|
temperature=temperature,
|
|
|
|
temperature=temperature,
|
|
|
|
top_p=top_p,
|
|
|
|
top_p=top_p,
|
|
|
|
**kwargs,
|
|
|
|
**kwargs)
|
|
|
|
)
|
|
|
|
outputs = outputs[0].cpu().tolist()[len(inputs["input_ids"][0]):]
|
|
|
|
outputs = outputs[0].cpu().tolist()[len(inputs["input_ids"][0]) :]
|
|
|
|
|
|
|
|
response = tokenizer.decode(outputs, skip_special_tokens=True)
|
|
|
|
response = tokenizer.decode(outputs, skip_special_tokens=True)
|
|
|
|
response = response.split("<eoa>")[0]
|
|
|
|
response = response.split("<eoa>")[0]
|
|
|
|
history = history + [(query, response)]
|
|
|
|
history = history + [(query, response)]
|
|
|
|
return response, history
|
|
|
|
return response, history
|
|
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
@torch.no_grad()
|
|
|
|
def stream_chat(
|
|
|
|
def stream_chat(self,
|
|
|
|
self,
|
|
|
|
|
|
|
|
tokenizer,
|
|
|
|
tokenizer,
|
|
|
|
query: str,
|
|
|
|
query: str,
|
|
|
|
history: List[Tuple[str, str]] = [],
|
|
|
|
history: List[Tuple[str, str]] = [],
|
|
|
@ -826,8 +788,7 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
|
|
do_sample: bool = True,
|
|
|
|
do_sample: bool = True,
|
|
|
|
temperature: float = 0.8,
|
|
|
|
temperature: float = 0.8,
|
|
|
|
top_p: float = 0.8,
|
|
|
|
top_p: float = 0.8,
|
|
|
|
**kwargs,
|
|
|
|
**kwargs):
|
|
|
|
):
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Return a generator in format: (response, history)
|
|
|
|
Return a generator in format: (response, history)
|
|
|
|
Eg.
|
|
|
|
Eg.
|
|
|
@ -878,7 +839,7 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
|
|
do_sample=do_sample,
|
|
|
|
do_sample=do_sample,
|
|
|
|
temperature=temperature,
|
|
|
|
temperature=temperature,
|
|
|
|
top_p=top_p,
|
|
|
|
top_p=top_p,
|
|
|
|
**kwargs,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def consumer():
|
|
|
|
def consumer():
|
|
|
@ -886,7 +847,7 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
|
|
producer.start()
|
|
|
|
producer.start()
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
res = response_queue.get()
|
|
|
|
res = response_queue.get()
|
|
|
|
if res is not None:
|
|
|
|
if res is None:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
yield res
|
|
|
|
yield res
|
|
|
|
|
|
|
|
|
|
|
@ -896,10 +857,8 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
|
|
@add_start_docstrings(
|
|
|
|
@add_start_docstrings(
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
The InternLM Model transformer with a sequence classification head on top (linear layer).
|
|
|
|
The InternLM Model transformer with a sequence classification head on top (linear layer).
|
|
|
|
|
|
|
|
|
|
|
|
[`InternLMForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
|
|
|
[`InternLMForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
|
|
|
(e.g. GPT-2) do.
|
|
|
|
(e.g. GPT-2) do.
|
|
|
|
|
|
|
|
|
|
|
|
Since it does classification on the last token, it requires to know the position of the last token. If a
|
|
|
|
Since it does classification on the last token, it requires to know the position of the last token. If a
|
|
|
|
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
|
|
|
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
|
|
|
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
|
|
|
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|