Upload 11 files
Browse files- added_tokens.json +10 -0
- config.json +31 -0
- configuration_aquila.py +128 -0
- generation_config.json +7 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- modeling_aquila.py +1146 -0
- special_tokens_map.json +30 -0
- tokenizer.json +0 -0
- tokenizer_config.json +85 -0
- vocab.json +0 -0
added_tokens.json
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"</s>": 100007,
|
3 |
+
"<|LDWANG|>": 100002,
|
4 |
+
"<|endofpiece|>": 100001,
|
5 |
+
"<|startofpiece|>": 100000,
|
6 |
+
"[CLS]": 100006,
|
7 |
+
"[MASK]": 100003,
|
8 |
+
"[gMASK]": 100004,
|
9 |
+
"[sMASK]": 100005
|
10 |
+
}
|
config.json
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "/home/ea/work/my_optimum_intel/optimum-intel/aquila-2-7b",
|
3 |
+
"architectures": [
|
4 |
+
"AquilaForCausalLM"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "configuration_aquila.AquilaConfig",
|
8 |
+
"AutoModelForCausalLM": "modeling_aquila.AquilaForCausalLM"
|
9 |
+
},
|
10 |
+
"bos_token_id": 100006,
|
11 |
+
"eos_token_id": 100007,
|
12 |
+
"hidden_act": "silu",
|
13 |
+
"hidden_size": 32,
|
14 |
+
"initializer_range": 0.02,
|
15 |
+
"intermediate_size": 86,
|
16 |
+
"max_position_embeddings": 64,
|
17 |
+
"model_type": "aquila",
|
18 |
+
"num_attention_heads": 2,
|
19 |
+
"num_hidden_layers": 2,
|
20 |
+
"num_key_value_heads": 2,
|
21 |
+
"pad_token_id": 0,
|
22 |
+
"pretraining_tp": 1,
|
23 |
+
"rms_norm_eps": 1e-05,
|
24 |
+
"rope_scaling": null,
|
25 |
+
"rope_theta": 10000.0,
|
26 |
+
"tie_word_embeddings": false,
|
27 |
+
"torch_dtype": "float32",
|
28 |
+
"transformers_version": "4.40.1",
|
29 |
+
"use_cache": true,
|
30 |
+
"vocab_size": 100008
|
31 |
+
}
|
configuration_aquila.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2023 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
""" Aquila model configuration"""
|
21 |
+
|
22 |
+
from transformers import PretrainedConfig
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
class AquilaConfig(PretrainedConfig):
|
27 |
+
r"""
|
28 |
+
This is the configuration class to store the configuration of a [`AquilaModel`]. It is used to instantiate an Aquila
|
29 |
+
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
|
30 |
+
defaults will yield a similar configuration to that of the Aquila-7B.
|
31 |
+
|
32 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
33 |
+
documentation from [`PretrainedConfig`] for more information.
|
34 |
+
|
35 |
+
|
36 |
+
Args:
|
37 |
+
vocab_size (`int`, *optional*, defaults to 32000):
|
38 |
+
Vocabulary size of the Aquila model. Defines the number of different tokens that can be represented by the
|
39 |
+
`inputs_ids` passed when calling [`AquilaModel`]
|
40 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
41 |
+
Dimension of the hidden representations.
|
42 |
+
intermediate_size (`int`, *optional*, defaults to 11008):
|
43 |
+
Dimension of the MLP representations.
|
44 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
45 |
+
Number of hidden layers in the Transformer encoder.
|
46 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
47 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
48 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
49 |
+
The non-linear activation function (function or string) in the decoder.
|
50 |
+
max_position_embeddings (`int`, *optional*, defaults to 2048):
|
51 |
+
The maximum sequence length that this model might ever be used with. Typically set this to something large
|
52 |
+
just in case (e.g., 512 or 1024 or 2048).
|
53 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
54 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
55 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-12):
|
56 |
+
The epsilon used by the rms normalization layers.
|
57 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
58 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
59 |
+
relevant if `config.is_decoder=True`.
|
60 |
+
tie_word_embeddings(`bool`, *optional*, defaults to `False`):
|
61 |
+
Whether to tie weight embeddings
|
62 |
+
Example:
|
63 |
+
|
64 |
+
```python
|
65 |
+
>>> from transformers import AquilaModel, AquilaConfig
|
66 |
+
|
67 |
+
>>> # Initializing a Aquila aquila-7b style configuration
|
68 |
+
>>> configuration = AquilaConfig()
|
69 |
+
|
70 |
+
>>> # Initializing a model from the aquila-7b style configuration
|
71 |
+
>>> model = AquilaModel(configuration)
|
72 |
+
|
73 |
+
>>> # Accessing the model configuration
|
74 |
+
>>> configuration = model.config
|
75 |
+
```"""
|
76 |
+
model_type = "aquila"
|
77 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
78 |
+
|
79 |
+
def __init__(
|
80 |
+
self,
|
81 |
+
vocab_size=100008,
|
82 |
+
hidden_size=4096,
|
83 |
+
intermediate_size=11008,
|
84 |
+
num_hidden_layers=32,
|
85 |
+
num_attention_heads=32,
|
86 |
+
num_key_value_heads=None,
|
87 |
+
hidden_act="silu",
|
88 |
+
max_position_embeddings=2048,
|
89 |
+
initializer_range=0.02,
|
90 |
+
rms_norm_eps=1e-6,
|
91 |
+
use_cache=True,
|
92 |
+
pad_token_id=0,
|
93 |
+
bos_token_id=1,
|
94 |
+
eos_token_id=2,
|
95 |
+
pretraining_tp=1,
|
96 |
+
tie_word_embeddings=False,
|
97 |
+
rope_theta=10000.0,
|
98 |
+
rope_scaling=None,
|
99 |
+
**kwargs,
|
100 |
+
):
|
101 |
+
self.vocab_size = vocab_size
|
102 |
+
self.max_position_embeddings = max_position_embeddings
|
103 |
+
self.hidden_size = hidden_size
|
104 |
+
self.intermediate_size = intermediate_size
|
105 |
+
self.num_hidden_layers = num_hidden_layers
|
106 |
+
|
107 |
+
# for backward compatibility
|
108 |
+
if num_key_value_heads is None:
|
109 |
+
num_key_value_heads = num_attention_heads
|
110 |
+
|
111 |
+
self.num_key_value_heads = num_key_value_heads
|
112 |
+
|
113 |
+
self.num_attention_heads = num_attention_heads
|
114 |
+
self.hidden_act = hidden_act
|
115 |
+
self.initializer_range = initializer_range
|
116 |
+
self.rms_norm_eps = rms_norm_eps
|
117 |
+
self.pretraining_tp = pretraining_tp
|
118 |
+
self.use_cache = use_cache
|
119 |
+
self.rope_theta = rope_theta
|
120 |
+
self.rope_scaling = rope_scaling
|
121 |
+
|
122 |
+
super().__init__(
|
123 |
+
pad_token_id=pad_token_id,
|
124 |
+
bos_token_id=bos_token_id,
|
125 |
+
eos_token_id=eos_token_id,
|
126 |
+
tie_word_embeddings=tie_word_embeddings,
|
127 |
+
**kwargs,
|
128 |
+
)
|
generation_config.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 100006,
|
4 |
+
"eos_token_id": 100007,
|
5 |
+
"pad_token_id": 0,
|
6 |
+
"transformers_version": "4.40.1"
|
7 |
+
}
|
merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bbec4035f4b0f0a9fb05b808bd6d654de2c1af3eb5e14a463f5cf3eda2667c7a
|
3 |
+
size 25703736
|
modeling_aquila.py
ADDED
@@ -0,0 +1,1146 @@
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2023 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
""" PyTorch Aquila model."""
|
21 |
+
import math
|
22 |
+
from typing import List, Optional, Tuple, Union
|
23 |
+
|
24 |
+
import torch
|
25 |
+
import torch.utils.checkpoint
|
26 |
+
from torch import nn
|
27 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
28 |
+
|
29 |
+
from transformers.activations import ACT2FN
|
30 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
|
31 |
+
from transformers.modeling_utils import PreTrainedModel
|
32 |
+
from transformers.utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
|
33 |
+
from .configuration_aquila import AquilaConfig
|
34 |
+
from transformers import (
|
35 |
+
LogitsProcessorList,
|
36 |
+
MinLengthLogitsProcessor,
|
37 |
+
TopKLogitsWarper,
|
38 |
+
TemperatureLogitsWarper,
|
39 |
+
TopPLogitsWarper,
|
40 |
+
StoppingCriteriaList,
|
41 |
+
MaxLengthCriteria,
|
42 |
+
BitsAndBytesConfig,
|
43 |
+
)
|
44 |
+
|
45 |
+
logger = logging.get_logger(__name__)
|
46 |
+
|
47 |
+
_CONFIG_FOR_DOC = "AquilaConfig"
|
48 |
+
|
49 |
+
|
50 |
+
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
|
51 |
+
def _make_causal_mask(
|
52 |
+
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
53 |
+
):
|
54 |
+
"""
|
55 |
+
Make causal mask used for bi-directional self-attention.
|
56 |
+
"""
|
57 |
+
bsz, tgt_len = input_ids_shape
|
58 |
+
mask = torch.full((tgt_len, tgt_len), torch.tensor(torch.finfo(dtype).min, device=device), device=device)
|
59 |
+
mask_cond = torch.arange(mask.size(-1), device=device)
|
60 |
+
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
|
61 |
+
mask = mask.to(dtype)
|
62 |
+
|
63 |
+
if past_key_values_length > 0:
|
64 |
+
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
|
65 |
+
return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
|
66 |
+
|
67 |
+
|
68 |
+
# Copied from transformers.models.bart.modeling_bart._expand_mask
|
69 |
+
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
|
70 |
+
"""
|
71 |
+
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
|
72 |
+
"""
|
73 |
+
bsz, src_len = mask.size()
|
74 |
+
tgt_len = tgt_len if tgt_len is not None else src_len
|
75 |
+
|
76 |
+
expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
|
77 |
+
|
78 |
+
inverted_mask = 1.0 - expanded_mask
|
79 |
+
|
80 |
+
return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
|
81 |
+
|
82 |
+
|
83 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->Aquila
|
84 |
+
class AquilaRMSNorm(nn.Module):
|
85 |
+
def __init__(self, hidden_size, eps=1e-6):
|
86 |
+
"""
|
87 |
+
AquilaRMSNorm is equivalent to T5LayerNorm
|
88 |
+
"""
|
89 |
+
super().__init__()
|
90 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
91 |
+
self.variance_epsilon = eps
|
92 |
+
|
93 |
+
def forward(self, hidden_states):
|
94 |
+
input_dtype = hidden_states.dtype
|
95 |
+
variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
|
96 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
97 |
+
|
98 |
+
return (self.weight * hidden_states).to(input_dtype)
|
99 |
+
|
100 |
+
|
101 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaRotaryEmbedding with Llama->Aquila
|
102 |
+
class AquilaRotaryEmbedding(torch.nn.Module):
|
103 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
104 |
+
super().__init__()
|
105 |
+
|
106 |
+
self.dim = dim
|
107 |
+
self.max_position_embeddings = max_position_embeddings
|
108 |
+
self.base = base
|
109 |
+
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
|
110 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
111 |
+
|
112 |
+
# Build here to make `torch.jit.trace` work.
|
113 |
+
self._set_cos_sin_cache(
|
114 |
+
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
|
115 |
+
)
|
116 |
+
|
117 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
118 |
+
self.max_seq_len_cached = seq_len
|
119 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
120 |
+
|
121 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
122 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
123 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
124 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
|
125 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
|
126 |
+
|
127 |
+
def forward(self, x, seq_len=None):
|
128 |
+
# x: [bs, num_attention_heads, seq_len, head_size]
|
129 |
+
if seq_len > self.max_seq_len_cached:
|
130 |
+
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
|
131 |
+
|
132 |
+
return (
|
133 |
+
self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
134 |
+
self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
135 |
+
)
|
136 |
+
|
137 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaLinearScalingRotaryEmbedding with Llama->Aquila
|
138 |
+
class AquilaLinearScalingRotaryEmbedding(AquilaRotaryEmbedding):
|
139 |
+
"""AquilaRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
|
140 |
+
|
141 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
|
142 |
+
self.scaling_factor = scaling_factor
|
143 |
+
super().__init__(dim, max_position_embeddings, base, device)
|
144 |
+
|
145 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
146 |
+
self.max_seq_len_cached = seq_len
|
147 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
148 |
+
t = t / self.scaling_factor
|
149 |
+
|
150 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
151 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
152 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
153 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
|
154 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
|
155 |
+
|
156 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaDynamicNTKScalingRotaryEmbedding with Llama->Aquila
|
157 |
+
class AquilaDynamicNTKScalingRotaryEmbedding(AquilaRotaryEmbedding):
|
158 |
+
"""AquilaRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
|
159 |
+
|
160 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
|
161 |
+
self.scaling_factor = scaling_factor
|
162 |
+
super().__init__(dim, max_position_embeddings, base, device)
|
163 |
+
|
164 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
165 |
+
self.max_seq_len_cached = seq_len
|
166 |
+
|
167 |
+
if seq_len > self.max_position_embeddings:
|
168 |
+
base = self.base * (
|
169 |
+
(self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
|
170 |
+
) ** (self.dim / (self.dim - 2))
|
171 |
+
inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
|
172 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
173 |
+
|
174 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
175 |
+
|
176 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
177 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
178 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
179 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
|
180 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
|
181 |
+
|
182 |
+
|
183 |
+
def rotate_half(x):
|
184 |
+
"""Rotates half the hidden dims of the input."""
|
185 |
+
x1 = x[..., : x.shape[-1] // 2]
|
186 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
187 |
+
return torch.cat((-x2, x1), dim=-1)
|
188 |
+
|
189 |
+
|
190 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
|
191 |
+
# The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
|
192 |
+
cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
|
193 |
+
sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
|
194 |
+
cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
195 |
+
sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
196 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
197 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
198 |
+
return q_embed, k_embed
|
199 |
+
|
200 |
+
|
201 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaMLP with Llama->Aquila
|
202 |
+
class AquilaMLP(nn.Module):
|
203 |
+
def __init__(self, config):
|
204 |
+
super().__init__()
|
205 |
+
self.config = config
|
206 |
+
self.hidden_size = config.hidden_size
|
207 |
+
self.intermediate_size = config.intermediate_size
|
208 |
+
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
209 |
+
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
210 |
+
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
|
211 |
+
self.act_fn = ACT2FN[config.hidden_act]
|
212 |
+
|
213 |
+
def forward(self, x):
|
214 |
+
if self.config.pretraining_tp > 1:
|
215 |
+
slice = self.intermediate_size // self.config.pretraining_tp
|
216 |
+
gate_proj_slices = self.gate_proj.weight.split(slice, dim=0)
|
217 |
+
up_proj_slices = self.up_proj.weight.split(slice, dim=0)
|
218 |
+
down_proj_slices = self.down_proj.weight.split(slice, dim=1)
|
219 |
+
|
220 |
+
gate_proj = torch.cat(
|
221 |
+
[F.linear(x, gate_proj_slices[i]) for i in range(self.config.pretraining_tp)], dim=-1
|
222 |
+
)
|
223 |
+
up_proj = torch.cat([F.linear(x, up_proj_slices[i]) for i in range(self.config.pretraining_tp)], dim=-1)
|
224 |
+
|
225 |
+
intermediate_states = (self.act_fn(gate_proj) * up_proj).split(slice, dim=2)
|
226 |
+
down_proj = [
|
227 |
+
F.linear(intermediate_states[i], down_proj_slices[i]) for i in range(self.config.pretraining_tp)
|
228 |
+
]
|
229 |
+
down_proj = sum(down_proj)
|
230 |
+
else:
|
231 |
+
down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
232 |
+
|
233 |
+
return down_proj
|
234 |
+
|
235 |
+
|
236 |
+
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
237 |
+
"""
|
238 |
+
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
239 |
+
num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
|
240 |
+
"""
|
241 |
+
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
|
242 |
+
if n_rep == 1:
|
243 |
+
return hidden_states
|
244 |
+
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
|
245 |
+
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
246 |
+
|
247 |
+
|
248 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaAttention with Llama->Aquila
|
249 |
+
class AquilaAttention(nn.Module):
|
250 |
+
"""Multi-headed attention from 'Attention Is All You Need' paper"""
|
251 |
+
def __init__(self, config: AquilaConfig):
|
252 |
+
super().__init__()
|
253 |
+
self.config = config
|
254 |
+
self.hidden_size = config.hidden_size
|
255 |
+
self.num_heads = config.num_attention_heads
|
256 |
+
self.head_dim = self.hidden_size // self.num_heads
|
257 |
+
self.num_key_value_heads = config.num_key_value_heads
|
258 |
+
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
259 |
+
self.max_position_embeddings = config.max_position_embeddings
|
260 |
+
self.rope_theta = config.rope_theta
|
261 |
+
|
262 |
+
if (self.head_dim * self.num_heads) != self.hidden_size:
|
263 |
+
raise ValueError(
|
264 |
+
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
265 |
+
f" and `num_heads`: {self.num_heads})."
|
266 |
+
)
|
267 |
+
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
268 |
+
self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
|
269 |
+
self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
|
270 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
271 |
+
self._init_rope()
|
272 |
+
|
273 |
+
def _init_rope(self):
|
274 |
+
if self.config.rope_scaling is None:
|
275 |
+
self.rotary_emb = AquilaRotaryEmbedding(
|
276 |
+
self.head_dim,
|
277 |
+
max_position_embeddings=self.max_position_embeddings,
|
278 |
+
base=self.rope_theta,
|
279 |
+
)
|
280 |
+
else:
|
281 |
+
scaling_type = self.config.rope_scaling["type"]
|
282 |
+
scaling_factor = self.config.rope_scaling["factor"]
|
283 |
+
if scaling_type == "linear":
|
284 |
+
self.rotary_emb = AquilaLinearScalingRotaryEmbedding(
|
285 |
+
self.head_dim,
|
286 |
+
max_position_embeddings=self.max_position_embeddings,
|
287 |
+
scaling_factor=scaling_factor,
|
288 |
+
base=self.rope_theta,
|
289 |
+
)
|
290 |
+
elif scaling_type == "dynamic":
|
291 |
+
self.rotary_emb = AquilaDynamicNTKScalingRotaryEmbedding(
|
292 |
+
self.head_dim,
|
293 |
+
max_position_embeddings=self.max_position_embeddings,
|
294 |
+
scaling_factor=scaling_factor,
|
295 |
+
base=self.rope_theta,
|
296 |
+
)
|
297 |
+
else:
|
298 |
+
raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
|
299 |
+
|
300 |
+
def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
|
301 |
+
return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
|
302 |
+
|
303 |
+
def forward(
|
304 |
+
self,
|
305 |
+
hidden_states: torch.Tensor,
|
306 |
+
attention_mask: Optional[torch.Tensor] = None,
|
307 |
+
position_ids: Optional[torch.LongTensor] = None,
|
308 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
309 |
+
output_attentions: bool = False,
|
310 |
+
use_cache: bool = False,
|
311 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
312 |
+
bsz, q_len, _ = hidden_states.size()
|
313 |
+
|
314 |
+
if self.config.pretraining_tp > 1:
|
315 |
+
key_value_slicing = (self.num_key_value_heads * self.head_dim) // self.config.pretraining_tp
|
316 |
+
query_slices = self.q_proj.weight.split(
|
317 |
+
(self.num_heads * self.head_dim) // self.config.pretraining_tp, dim=0
|
318 |
+
)
|
319 |
+
key_slices = self.k_proj.weight.split(key_value_slicing, dim=0)
|
320 |
+
value_slices = self.v_proj.weight.split(key_value_slicing, dim=0)
|
321 |
+
|
322 |
+
query_states = [F.linear(hidden_states, query_slices[i]) for i in range(self.config.pretraining_tp)]
|
323 |
+
query_states = torch.cat(query_states, dim=-1)
|
324 |
+
|
325 |
+
key_states = [F.linear(hidden_states, key_slices[i]) for i in range(self.config.pretraining_tp)]
|
326 |
+
key_states = torch.cat(key_states, dim=-1)
|
327 |
+
|
328 |
+
value_states = [F.linear(hidden_states, value_slices[i]) for i in range(self.config.pretraining_tp)]
|
329 |
+
value_states = torch.cat(value_states, dim=-1)
|
330 |
+
|
331 |
+
else:
|
332 |
+
query_states = self.q_proj(hidden_states)
|
333 |
+
key_states = self.k_proj(hidden_states)
|
334 |
+
value_states = self.v_proj(hidden_states)
|
335 |
+
|
336 |
+
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
337 |
+
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
338 |
+
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
339 |
+
|
340 |
+
kv_seq_len = key_states.shape[-2]
|
341 |
+
if past_key_value is not None:
|
342 |
+
kv_seq_len += past_key_value[0].shape[-2]
|
343 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
344 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
345 |
+
|
346 |
+
if past_key_value is not None:
|
347 |
+
# reuse k, v, self_attention
|
348 |
+
key_states = torch.cat([past_key_value[0], key_states], dim=2)
|
349 |
+
value_states = torch.cat([past_key_value[1], value_states], dim=2)
|
350 |
+
|
351 |
+
past_key_value = (key_states, value_states) if use_cache else None
|
352 |
+
|
353 |
+
# repeat k/v heads if n_kv_heads < n_heads
|
354 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
355 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
356 |
+
|
357 |
+
attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
|
358 |
+
attn_weights = torch.clamp(attn_weights, min=-1024., max=1024.)
|
359 |
+
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
360 |
+
raise ValueError(
|
361 |
+
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
|
362 |
+
f" {attn_weights.size()}"
|
363 |
+
)
|
364 |
+
|
365 |
+
if attention_mask is not None:
|
366 |
+
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
|
367 |
+
raise ValueError(
|
368 |
+
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
|
369 |
+
)
|
370 |
+
attn_weights = attn_weights + attention_mask
|
371 |
+
|
372 |
+
# upcast attention to fp32
|
373 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
|
374 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
375 |
+
|
376 |
+
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
|
377 |
+
raise ValueError(
|
378 |
+
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
|
379 |
+
f" {attn_output.size()}"
|
380 |
+
)
|
381 |
+
|
382 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
383 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
384 |
+
|
385 |
+
if self.config.pretraining_tp > 1:
|
386 |
+
attn_output = attn_output.split(self.hidden_size // self.config.pretraining_tp, dim=2)
|
387 |
+
o_proj_slices = self.o_proj.weight.split(self.hidden_size // self.config.pretraining_tp, dim=1)
|
388 |
+
attn_output = sum([F.linear(attn_output[i], o_proj_slices[i]) for i in range(self.config.pretraining_tp)])
|
389 |
+
else:
|
390 |
+
attn_output = self.o_proj(attn_output)
|
391 |
+
|
392 |
+
if not output_attentions:
|
393 |
+
attn_weights = None
|
394 |
+
|
395 |
+
return attn_output, attn_weights, past_key_value
|
396 |
+
|
397 |
+
|
398 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaDecoderLayer with Llama->Aquila
|
399 |
+
class AquilaDecoderLayer(nn.Module):
|
400 |
+
def __init__(self, config: AquilaConfig):
|
401 |
+
super().__init__()
|
402 |
+
self.hidden_size = config.hidden_size
|
403 |
+
self.self_attn = AquilaAttention(config=config)
|
404 |
+
self.mlp = AquilaMLP(config)
|
405 |
+
self.input_layernorm = AquilaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
406 |
+
self.post_attention_layernorm = AquilaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
407 |
+
|
408 |
+
def forward(
|
409 |
+
self,
|
410 |
+
hidden_states: torch.Tensor,
|
411 |
+
attention_mask: Optional[torch.Tensor] = None,
|
412 |
+
position_ids: Optional[torch.LongTensor] = None,
|
413 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
414 |
+
output_attentions: Optional[bool] = False,
|
415 |
+
use_cache: Optional[bool] = False,
|
416 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
417 |
+
"""
|
418 |
+
Args:
|
419 |
+
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
420 |
+
attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
|
421 |
+
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
|
422 |
+
output_attentions (`bool`, *optional*):
|
423 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
424 |
+
returned tensors for more detail.
|
425 |
+
use_cache (`bool`, *optional*):
|
426 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
427 |
+
(see `past_key_values`).
|
428 |
+
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
|
429 |
+
"""
|
430 |
+
|
431 |
+
residual = hidden_states
|
432 |
+
|
433 |
+
hidden_states = self.input_layernorm(hidden_states)
|
434 |
+
|
435 |
+
# Self Attention
|
436 |
+
hidden_states, self_attn_weights, present_key_value = self.self_attn(
|
437 |
+
hidden_states=hidden_states,
|
438 |
+
attention_mask=attention_mask,
|
439 |
+
position_ids=position_ids,
|
440 |
+
past_key_value=past_key_value,
|
441 |
+
output_attentions=output_attentions,
|
442 |
+
use_cache=use_cache,
|
443 |
+
)
|
444 |
+
hidden_states = residual + hidden_states
|
445 |
+
|
446 |
+
# Fully Connected
|
447 |
+
residual = hidden_states
|
448 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
449 |
+
hidden_states = self.mlp(hidden_states)
|
450 |
+
hidden_states = residual + hidden_states
|
451 |
+
|
452 |
+
outputs = (hidden_states,)
|
453 |
+
|
454 |
+
if output_attentions:
|
455 |
+
outputs += (self_attn_weights,)
|
456 |
+
|
457 |
+
if use_cache:
|
458 |
+
outputs += (present_key_value,)
|
459 |
+
|
460 |
+
return outputs
|
461 |
+
|
462 |
+
AQUILA_START_DOCSTRING = r"""
|
463 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
464 |
+
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
465 |
+
etc.)
|
466 |
+
|
467 |
+
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
468 |
+
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
469 |
+
and behavior.
|
470 |
+
|
471 |
+
Parameters:
|
472 |
+
config ([`AquilaConfig`]):
|
473 |
+
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
474 |
+
load the weights associated with the model, only the configuration. Check out the
|
475 |
+
[`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
476 |
+
"""
|
477 |
+
|
478 |
+
|
479 |
+
@add_start_docstrings(
|
480 |
+
"The bare Aquila Model outputting raw hidden-states without any specific head on top.",
|
481 |
+
AQUILA_START_DOCSTRING,
|
482 |
+
)
|
483 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaPreTrainedModel with Llama->Aquila
|
484 |
+
class AquilaPreTrainedModel(PreTrainedModel):
|
485 |
+
config_class = AquilaConfig
|
486 |
+
base_model_prefix = "model"
|
487 |
+
supports_gradient_checkpointing = True
|
488 |
+
_no_split_modules = ["AquilaDecoderLayer"]
|
489 |
+
_skip_keys_device_placement = "past_key_values"
|
490 |
+
|
491 |
+
def _init_weights(self, module):
|
492 |
+
std = self.config.initializer_range
|
493 |
+
if isinstance(module, nn.Linear):
|
494 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
495 |
+
if module.bias is not None:
|
496 |
+
module.bias.data.zero_()
|
497 |
+
elif isinstance(module, nn.Embedding):
|
498 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
499 |
+
if module.padding_idx is not None:
|
500 |
+
module.weight.data[module.padding_idx].zero_()
|
501 |
+
|
502 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
503 |
+
if isinstance(module, AquilaModel):
|
504 |
+
module.gradient_checkpointing = value
|
505 |
+
|
506 |
+
|
507 |
+
AQUILA_INPUTS_DOCSTRING = r"""
|
508 |
+
Args:
|
509 |
+
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
510 |
+
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
511 |
+
it.
|
512 |
+
|
513 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
514 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
515 |
+
|
516 |
+
[What are input IDs?](../glossary#input-ids)
|
517 |
+
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
518 |
+
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
519 |
+
|
520 |
+
- 1 for tokens that are **not masked**,
|
521 |
+
- 0 for tokens that are **masked**.
|
522 |
+
|
523 |
+
[What are attention masks?](../glossary#attention-mask)
|
524 |
+
|
525 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
526 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
527 |
+
|
528 |
+
If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
|
529 |
+
`past_key_values`).
|
530 |
+
|
531 |
+
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
532 |
+
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
533 |
+
information on the default strategy.
|
534 |
+
|
535 |
+
- 1 indicates the head is **not masked**,
|
536 |
+
- 0 indicates the head is **masked**.
|
537 |
+
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
538 |
+
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
539 |
+
config.n_positions - 1]`.
|
540 |
+
|
541 |
+
[What are position IDs?](../glossary#position-ids)
|
542 |
+
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
|
543 |
+
Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
|
544 |
+
`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
|
545 |
+
`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
|
546 |
+
|
547 |
+
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
548 |
+
blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
|
549 |
+
|
550 |
+
If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
|
551 |
+
don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
|
552 |
+
`decoder_input_ids` of shape `(batch_size, sequence_length)`.
|
553 |
+
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
554 |
+
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
|
555 |
+
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
|
556 |
+
model's internal embedding lookup matrix.
|
557 |
+
use_cache (`bool`, *optional*):
|
558 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
|
559 |
+
`past_key_values`).
|
560 |
+
output_attentions (`bool`, *optional*):
|
561 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
|
562 |
+
tensors for more detail.
|
563 |
+
output_hidden_states (`bool`, *optional*):
|
564 |
+
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
|
565 |
+
more detail.
|
566 |
+
return_dict (`bool`, *optional*):
|
567 |
+
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
|
568 |
+
"""
|
569 |
+
|
570 |
+
|
571 |
+
@add_start_docstrings(
|
572 |
+
"The bare Aquila Model outputting raw hidden-states without any specific head on top.",
|
573 |
+
AQUILA_START_DOCSTRING,
|
574 |
+
)
|
575 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaModel with LLAMA->AQUILA,Llama->Aquila
|
576 |
+
class AquilaModel(AquilaPreTrainedModel):
|
577 |
+
"""
|
578 |
+
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`AquilaDecoderLayer`]
|
579 |
+
|
580 |
+
Args:
|
581 |
+
config: AquilaConfig
|
582 |
+
"""
|
583 |
+
|
584 |
+
def __init__(self, config: AquilaConfig):
|
585 |
+
super().__init__(config)
|
586 |
+
self.padding_idx = config.pad_token_id
|
587 |
+
self.vocab_size = config.vocab_size
|
588 |
+
|
589 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
590 |
+
self.layers = nn.ModuleList([AquilaDecoderLayer(config) for _ in range(config.num_hidden_layers)])
|
591 |
+
self.norm = AquilaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
592 |
+
|
593 |
+
self.gradient_checkpointing = False
|
594 |
+
# Initialize weights and apply final processing
|
595 |
+
self.post_init()
|
596 |
+
|
597 |
+
def get_input_embeddings(self):
|
598 |
+
return self.embed_tokens
|
599 |
+
|
600 |
+
def set_input_embeddings(self, value):
|
601 |
+
self.embed_tokens = value
|
602 |
+
|
603 |
+
def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
|
604 |
+
# create causal mask
|
605 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
606 |
+
combined_attention_mask = None
|
607 |
+
if input_shape[-1] > 1:
|
608 |
+
combined_attention_mask = _make_causal_mask(
|
609 |
+
input_shape,
|
610 |
+
inputs_embeds.dtype,
|
611 |
+
device=inputs_embeds.device,
|
612 |
+
past_key_values_length=past_key_values_length,
|
613 |
+
)
|
614 |
+
|
615 |
+
if attention_mask is not None:
|
616 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
617 |
+
expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
|
618 |
+
inputs_embeds.device
|
619 |
+
)
|
620 |
+
combined_attention_mask = (
|
621 |
+
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
|
622 |
+
)
|
623 |
+
|
624 |
+
return combined_attention_mask
|
625 |
+
|
626 |
+
@add_start_docstrings_to_model_forward(AQUILA_INPUTS_DOCSTRING)
|
627 |
+
def forward(
|
628 |
+
self,
|
629 |
+
input_ids: torch.LongTensor = None,
|
630 |
+
attention_mask: Optional[torch.Tensor] = None,
|
631 |
+
position_ids: Optional[torch.LongTensor] = None,
|
632 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
633 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
634 |
+
use_cache: Optional[bool] = None,
|
635 |
+
output_attentions: Optional[bool] = None,
|
636 |
+
output_hidden_states: Optional[bool] = None,
|
637 |
+
return_dict: Optional[bool] = None,
|
638 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
639 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
640 |
+
output_hidden_states = (
|
641 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
642 |
+
)
|
643 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
644 |
+
|
645 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
646 |
+
|
647 |
+
# retrieve input_ids and inputs_embeds
|
648 |
+
if input_ids is not None and inputs_embeds is not None:
|
649 |
+
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
|
650 |
+
elif input_ids is not None:
|
651 |
+
batch_size, seq_length = input_ids.shape
|
652 |
+
elif inputs_embeds is not None:
|
653 |
+
batch_size, seq_length, _ = inputs_embeds.shape
|
654 |
+
else:
|
655 |
+
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
|
656 |
+
|
657 |
+
seq_length_with_past = seq_length
|
658 |
+
past_key_values_length = 0
|
659 |
+
|
660 |
+
if past_key_values is not None:
|
661 |
+
past_key_values_length = past_key_values[0][0].shape[2]
|
662 |
+
seq_length_with_past = seq_length_with_past + past_key_values_length
|
663 |
+
|
664 |
+
if position_ids is None:
|
665 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
666 |
+
position_ids = torch.arange(
|
667 |
+
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
|
668 |
+
)
|
669 |
+
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
|
670 |
+
else:
|
671 |
+
position_ids = position_ids.view(-1, seq_length).long()
|
672 |
+
|
673 |
+
if inputs_embeds is None:
|
674 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
675 |
+
# embed positions
|
676 |
+
if attention_mask is None:
|
677 |
+
attention_mask = torch.ones(
|
678 |
+
(batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
|
679 |
+
)
|
680 |
+
attention_mask = self._prepare_decoder_attention_mask(
|
681 |
+
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
|
682 |
+
)
|
683 |
+
|
684 |
+
hidden_states = inputs_embeds
|
685 |
+
|
686 |
+
if self.gradient_checkpointing and self.training:
|
687 |
+
if use_cache:
|
688 |
+
logger.warning_once(
|
689 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
690 |
+
)
|
691 |
+
use_cache = False
|
692 |
+
|
693 |
+
# decoder layers
|
694 |
+
all_hidden_states = () if output_hidden_states else None
|
695 |
+
all_self_attns = () if output_attentions else None
|
696 |
+
next_decoder_cache = () if use_cache else None
|
697 |
+
|
698 |
+
for idx, decoder_layer in enumerate(self.layers):
|
699 |
+
if output_hidden_states:
|
700 |
+
all_hidden_states += (hidden_states,)
|
701 |
+
|
702 |
+
past_key_value = past_key_values[idx] if past_key_values is not None else None
|
703 |
+
|
704 |
+
if self.gradient_checkpointing and self.training:
|
705 |
+
|
706 |
+
def create_custom_forward(module):
|
707 |
+
def custom_forward(*inputs):
|
708 |
+
# None for past_key_value
|
709 |
+
return module(*inputs, past_key_value, output_attentions)
|
710 |
+
|
711 |
+
return custom_forward
|
712 |
+
|
713 |
+
layer_outputs = torch.utils.checkpoint.checkpoint(
|
714 |
+
create_custom_forward(decoder_layer),
|
715 |
+
hidden_states,
|
716 |
+
attention_mask,
|
717 |
+
position_ids,
|
718 |
+
)
|
719 |
+
else:
|
720 |
+
layer_outputs = decoder_layer(
|
721 |
+
hidden_states,
|
722 |
+
attention_mask=attention_mask,
|
723 |
+
position_ids=position_ids,
|
724 |
+
past_key_value=past_key_value,
|
725 |
+
output_attentions=output_attentions,
|
726 |
+
use_cache=use_cache,
|
727 |
+
)
|
728 |
+
|
729 |
+
hidden_states = layer_outputs[0]
|
730 |
+
|
731 |
+
if use_cache:
|
732 |
+
next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
|
733 |
+
|
734 |
+
if output_attentions:
|
735 |
+
all_self_attns += (layer_outputs[1],)
|
736 |
+
|
737 |
+
hidden_states = self.norm(hidden_states)
|
738 |
+
|
739 |
+
# add hidden states from the last decoder layer
|
740 |
+
if output_hidden_states:
|
741 |
+
all_hidden_states += (hidden_states,)
|
742 |
+
|
743 |
+
next_cache = next_decoder_cache if use_cache else None
|
744 |
+
if not return_dict:
|
745 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
|
746 |
+
return BaseModelOutputWithPast(
|
747 |
+
last_hidden_state=hidden_states,
|
748 |
+
past_key_values=next_cache,
|
749 |
+
hidden_states=all_hidden_states,
|
750 |
+
attentions=all_self_attns,
|
751 |
+
)
|
752 |
+
|
753 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM with LLAMA->AQUILA,Llama->Aquila
|
754 |
+
class AquilaForCausalLM(AquilaPreTrainedModel):
|
755 |
+
_tied_weights_keys = ["lm_head.weight"]
|
756 |
+
|
757 |
+
def __init__(self, config):
|
758 |
+
super().__init__(config)
|
759 |
+
self.model = AquilaModel(config)
|
760 |
+
self.vocab_size = config.vocab_size
|
761 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
762 |
+
|
763 |
+
# Initialize weights and apply final processing
|
764 |
+
self.post_init()
|
765 |
+
|
766 |
+
def get_input_embeddings(self):
|
767 |
+
return self.model.embed_tokens
|
768 |
+
|
769 |
+
def set_input_embeddings(self, value):
|
770 |
+
self.model.embed_tokens = value
|
771 |
+
|
772 |
+
def get_output_embeddings(self):
|
773 |
+
return self.lm_head
|
774 |
+
|
775 |
+
def set_output_embeddings(self, new_embeddings):
|
776 |
+
self.lm_head = new_embeddings
|
777 |
+
|
778 |
+
def set_decoder(self, decoder):
|
779 |
+
self.model = decoder
|
780 |
+
|
781 |
+
def get_decoder(self):
|
782 |
+
return self.model
|
783 |
+
|
784 |
+
@add_start_docstrings_to_model_forward(AQUILA_INPUTS_DOCSTRING)
|
785 |
+
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
|
786 |
+
def forward(
|
787 |
+
self,
|
788 |
+
input_ids: torch.LongTensor = None,
|
789 |
+
attention_mask: Optional[torch.Tensor] = None,
|
790 |
+
position_ids: Optional[torch.LongTensor] = None,
|
791 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
792 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
793 |
+
labels: Optional[torch.LongTensor] = None,
|
794 |
+
use_cache: Optional[bool] = None,
|
795 |
+
output_attentions: Optional[bool] = None,
|
796 |
+
output_hidden_states: Optional[bool] = None,
|
797 |
+
return_dict: Optional[bool] = None,
|
798 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
799 |
+
r"""
|
800 |
+
Args:
|
801 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
802 |
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
803 |
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
804 |
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
805 |
+
|
806 |
+
Returns:
|
807 |
+
|
808 |
+
Example:
|
809 |
+
|
810 |
+
```python
|
811 |
+
>>> from transformers import AutoTokenizer, AquilaForCausalLM
|
812 |
+
|
813 |
+
>>> model = AquilaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
814 |
+
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
815 |
+
|
816 |
+
>>> prompt = "Hey, are you consciours? Can you talk to me?"
|
817 |
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
818 |
+
|
819 |
+
>>> # Generate
|
820 |
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
821 |
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
822 |
+
"Hey, are you consciours? Can you talk to me?\nI'm not consciours, but I can talk to you."
|
823 |
+
```"""
|
824 |
+
|
825 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
826 |
+
output_hidden_states = (
|
827 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
828 |
+
)
|
829 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
830 |
+
|
831 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
832 |
+
outputs = self.model(
|
833 |
+
input_ids=input_ids,
|
834 |
+
attention_mask=attention_mask,
|
835 |
+
position_ids=position_ids,
|
836 |
+
past_key_values=past_key_values,
|
837 |
+
inputs_embeds=inputs_embeds,
|
838 |
+
use_cache=use_cache,
|
839 |
+
output_attentions=output_attentions,
|
840 |
+
output_hidden_states=output_hidden_states,
|
841 |
+
return_dict=return_dict,
|
842 |
+
)
|
843 |
+
|
844 |
+
hidden_states = outputs[0]
|
845 |
+
if self.config.pretraining_tp > 1:
|
846 |
+
lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
|
847 |
+
logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
|
848 |
+
logits = torch.cat(logits, dim=-1)
|
849 |
+
else:
|
850 |
+
logits = self.lm_head(hidden_states)
|
851 |
+
logits = logits.float()
|
852 |
+
|
853 |
+
loss = None
|
854 |
+
if labels is not None:
|
855 |
+
# Shift so that tokens < n predict n
|
856 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
857 |
+
shift_labels = labels[..., 1:].contiguous()
|
858 |
+
# Flatten the tokens
|
859 |
+
loss_fct = CrossEntropyLoss()
|
860 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
861 |
+
shift_labels = shift_labels.view(-1)
|
862 |
+
# Enable model parallelism
|
863 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
864 |
+
loss = loss_fct(shift_logits, shift_labels)
|
865 |
+
|
866 |
+
if not return_dict:
|
867 |
+
output = (logits,) + outputs[1:]
|
868 |
+
return (loss,) + output if loss is not None else output
|
869 |
+
|
870 |
+
return CausalLMOutputWithPast(
|
871 |
+
loss=loss,
|
872 |
+
logits=logits,
|
873 |
+
past_key_values=outputs.past_key_values,
|
874 |
+
hidden_states=outputs.hidden_states,
|
875 |
+
attentions=outputs.attentions,
|
876 |
+
)
|
877 |
+
|
878 |
+
def prepare_inputs_for_generation(
|
879 |
+
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
|
880 |
+
):
|
881 |
+
if past_key_values:
|
882 |
+
input_ids = input_ids[:, -1:]
|
883 |
+
|
884 |
+
position_ids = kwargs.get("position_ids", None)
|
885 |
+
if attention_mask is not None and position_ids is None:
|
886 |
+
# create position_ids on the fly for batch generation
|
887 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
888 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
889 |
+
if past_key_values:
|
890 |
+
position_ids = position_ids[:, -1].unsqueeze(-1)
|
891 |
+
|
892 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
893 |
+
if inputs_embeds is not None and past_key_values is None:
|
894 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
895 |
+
else:
|
896 |
+
model_inputs = {"input_ids": input_ids}
|
897 |
+
|
898 |
+
model_inputs.update(
|
899 |
+
{
|
900 |
+
"position_ids": position_ids,
|
901 |
+
"past_key_values": past_key_values,
|
902 |
+
"use_cache": kwargs.get("use_cache"),
|
903 |
+
"attention_mask": attention_mask,
|
904 |
+
}
|
905 |
+
)
|
906 |
+
return model_inputs
|
907 |
+
|
908 |
+
@staticmethod
|
909 |
+
def _reorder_cache(past_key_values, beam_idx):
|
910 |
+
reordered_past = ()
|
911 |
+
for layer_past in past_key_values:
|
912 |
+
reordered_past += (
|
913 |
+
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
|
914 |
+
)
|
915 |
+
return reordered_past
|
916 |
+
|
917 |
+
def predict(self, text, tokenizer=None,
|
918 |
+
max_gen_len=200, top_p=0.95,
|
919 |
+
seed=1234, topk=100,
|
920 |
+
temperature=0.9,
|
921 |
+
sft=True, convo_template = "aquila-chat",
|
922 |
+
device = "cuda"):
|
923 |
+
|
924 |
+
vocab = tokenizer.get_vocab()
|
925 |
+
#device = device
|
926 |
+
id2word = {v:k for k, v in vocab.items()}
|
927 |
+
|
928 |
+
|
929 |
+
set_random_seed(seed)
|
930 |
+
if temperature == 0:
|
931 |
+
topk = 1
|
932 |
+
temperature = 1.0
|
933 |
+
if sft:
|
934 |
+
tokens = covert_prompt_to_input_ids_with_history(text, history=[], tokenizer=tokenizer, max_token=2048, convo_template=convo_template)
|
935 |
+
tokens = torch.tensor(tokens)[None,].to(device)
|
936 |
+
else :
|
937 |
+
tokens = tokenizer.encode_plus(text)["input_ids"]
|
938 |
+
print(tokenizer.decode(tokens))
|
939 |
+
tokens = torch.tensor(tokens)[None,].to(device)
|
940 |
+
input_length = len(tokens[0])
|
941 |
+
with torch.no_grad():
|
942 |
+
|
943 |
+
# instantiate logits processors
|
944 |
+
logits_processor = LogitsProcessorList(
|
945 |
+
[
|
946 |
+
MinLengthLogitsProcessor(1, eos_token_id=100007),
|
947 |
+
]
|
948 |
+
)
|
949 |
+
# instantiate logits processors
|
950 |
+
logits_warper = LogitsProcessorList(
|
951 |
+
[
|
952 |
+
TopPLogitsWarper(top_p),
|
953 |
+
TopKLogitsWarper(topk),
|
954 |
+
TemperatureLogitsWarper(temperature),
|
955 |
+
|
956 |
+
]
|
957 |
+
)
|
958 |
+
|
959 |
+
stopping_criteria = StoppingCriteriaList([MaxLengthCriteria(max_length=input_length + max_gen_len)])
|
960 |
+
out = self.sample(
|
961 |
+
tokens,
|
962 |
+
logits_processor=logits_processor,
|
963 |
+
logits_warper=logits_warper,
|
964 |
+
stopping_criteria=stopping_criteria,
|
965 |
+
return_dict_in_generate=True,
|
966 |
+
output_scores=True,
|
967 |
+
)
|
968 |
+
|
969 |
+
|
970 |
+
# print(out)
|
971 |
+
out_ids = out["sequences"][0][input_length:].cpu().numpy()
|
972 |
+
|
973 |
+
out_scores = out["scores"]
|
974 |
+
|
975 |
+
out_scores = torch.cat(out_scores, dim=0)
|
976 |
+
out_scores = torch.nn.functional.softmax(out_scores, dim=-1).cpu().numpy()
|
977 |
+
|
978 |
+
probs = []
|
979 |
+
for i in range(len(out_ids)):
|
980 |
+
probs.append(float(out_scores[i][out_ids[i]]))
|
981 |
+
|
982 |
+
# print(f"probs is {probs}")
|
983 |
+
|
984 |
+
convert_tokens = []
|
985 |
+
for t in out_ids:
|
986 |
+
if t == 100006:
|
987 |
+
convert_tokens.append("[CLS]")
|
988 |
+
else :
|
989 |
+
convert_tokens.append(id2word.get(t, "[unkonwn_token]"))
|
990 |
+
|
991 |
+
out_text = tokenizer.decode(out_ids.tolist())
|
992 |
+
|
993 |
+
|
994 |
+
out = out_text
|
995 |
+
|
996 |
+
if "###" in out:
|
997 |
+
special_index = out.index("###")
|
998 |
+
out = out[: special_index]
|
999 |
+
token_length = len(tokenizer.encode_plus(out)["input_ids"])
|
1000 |
+
convert_tokens = convert_tokens[:token_length]
|
1001 |
+
probs = probs[:token_length]
|
1002 |
+
|
1003 |
+
if "[UNK]" in out:
|
1004 |
+
special_index = out.index("[UNK]")
|
1005 |
+
out = out[:special_index]
|
1006 |
+
token_length = len(tokenizer.encode_plus(out)["input_ids"])
|
1007 |
+
convert_tokens = convert_tokens[:token_length]
|
1008 |
+
probs = probs[:token_length]
|
1009 |
+
|
1010 |
+
if "</s>" in out:
|
1011 |
+
special_index = out.index("</s>")
|
1012 |
+
out = out[: special_index]
|
1013 |
+
token_length = len(tokenizer.encode_plus(out)["input_ids"])
|
1014 |
+
convert_tokens = convert_tokens[:token_length]
|
1015 |
+
probs = probs[:token_length]
|
1016 |
+
|
1017 |
+
if len(out) > 0 and out[0] == " ":
|
1018 |
+
out = out[1:]
|
1019 |
+
|
1020 |
+
convert_tokens = convert_tokens[1:]
|
1021 |
+
probs = probs[1:]
|
1022 |
+
return out
|
1023 |
+
|
1024 |
+
@add_start_docstrings(
|
1025 |
+
"""
|
1026 |
+
The LLaMa Model transformer with a sequence classification head on top (linear layer).
|
1027 |
+
|
1028 |
+
[`AquilaForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
1029 |
+
(e.g. GPT-2) do.
|
1030 |
+
|
1031 |
+
Since it does classification on the last token, it requires to know the position of the last token. If a
|
1032 |
+
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
1033 |
+
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
1034 |
+
padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
|
1035 |
+
each row of the batch).
|
1036 |
+
""",
|
1037 |
+
AQUILA_START_DOCSTRING,
|
1038 |
+
)
|
1039 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with LLAMA->AQUILA,Llama->Aquila
|
1040 |
+
class AquilaForSequenceClassification(AquilaPreTrainedModel):
|
1041 |
+
_keys_to_ignore_on_load_missing = [r"lm_head.weight"]
|
1042 |
+
|
1043 |
+
def __init__(self, config):
|
1044 |
+
super().__init__(config)
|
1045 |
+
self.num_labels = config.num_labels
|
1046 |
+
self.model = AquilaModel(config)
|
1047 |
+
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
1048 |
+
|
1049 |
+
# Initialize weights and apply final processing
|
1050 |
+
self.post_init()
|
1051 |
+
|
1052 |
+
def get_input_embeddings(self):
|
1053 |
+
return self.model.embed_tokens
|
1054 |
+
|
1055 |
+
def set_input_embeddings(self, value):
|
1056 |
+
self.model.embed_tokens = value
|
1057 |
+
|
1058 |
+
@add_start_docstrings_to_model_forward(AQUILA_INPUTS_DOCSTRING)
|
1059 |
+
def forward(
|
1060 |
+
self,
|
1061 |
+
input_ids: torch.LongTensor = None,
|
1062 |
+
attention_mask: Optional[torch.Tensor] = None,
|
1063 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1064 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
1065 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1066 |
+
labels: Optional[torch.LongTensor] = None,
|
1067 |
+
use_cache: Optional[bool] = None,
|
1068 |
+
output_attentions: Optional[bool] = None,
|
1069 |
+
output_hidden_states: Optional[bool] = None,
|
1070 |
+
return_dict: Optional[bool] = None,
|
1071 |
+
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
1072 |
+
r"""
|
1073 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
1074 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
1075 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
1076 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
1077 |
+
"""
|
1078 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1079 |
+
|
1080 |
+
transformer_outputs = self.model(
|
1081 |
+
input_ids,
|
1082 |
+
attention_mask=attention_mask,
|
1083 |
+
position_ids=position_ids,
|
1084 |
+
past_key_values=past_key_values,
|
1085 |
+
inputs_embeds=inputs_embeds,
|
1086 |
+
use_cache=use_cache,
|
1087 |
+
output_attentions=output_attentions,
|
1088 |
+
output_hidden_states=output_hidden_states,
|
1089 |
+
return_dict=return_dict,
|
1090 |
+
)
|
1091 |
+
hidden_states = transformer_outputs[0]
|
1092 |
+
logits = self.score(hidden_states)
|
1093 |
+
|
1094 |
+
if input_ids is not None:
|
1095 |
+
batch_size = input_ids.shape[0]
|
1096 |
+
else:
|
1097 |
+
batch_size = inputs_embeds.shape[0]
|
1098 |
+
|
1099 |
+
if self.config.pad_token_id is None and batch_size != 1:
|
1100 |
+
raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
|
1101 |
+
if self.config.pad_token_id is None:
|
1102 |
+
sequence_lengths = -1
|
1103 |
+
else:
|
1104 |
+
if input_ids is not None:
|
1105 |
+
sequence_lengths = (torch.eq(input_ids, self.config.pad_token_id).long().argmax(-1) - 1).to(
|
1106 |
+
logits.device
|
1107 |
+
)
|
1108 |
+
else:
|
1109 |
+
sequence_lengths = -1
|
1110 |
+
|
1111 |
+
pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
|
1112 |
+
|
1113 |
+
loss = None
|
1114 |
+
if labels is not None:
|
1115 |
+
labels = labels.to(logits.device)
|
1116 |
+
if self.config.problem_type is None:
|
1117 |
+
if self.num_labels == 1:
|
1118 |
+
self.config.problem_type = "regression"
|
1119 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
1120 |
+
self.config.problem_type = "single_label_classification"
|
1121 |
+
else:
|
1122 |
+
self.config.problem_type = "multi_label_classification"
|
1123 |
+
|
1124 |
+
if self.config.problem_type == "regression":
|
1125 |
+
loss_fct = MSELoss()
|
1126 |
+
if self.num_labels == 1:
|
1127 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
1128 |
+
else:
|
1129 |
+
loss = loss_fct(pooled_logits, labels)
|
1130 |
+
elif self.config.problem_type == "single_label_classification":
|
1131 |
+
loss_fct = CrossEntropyLoss()
|
1132 |
+
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
1133 |
+
elif self.config.problem_type == "multi_label_classification":
|
1134 |
+
loss_fct = BCEWithLogitsLoss()
|
1135 |
+
loss = loss_fct(pooled_logits, labels)
|
1136 |
+
if not return_dict:
|
1137 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
1138 |
+
return ((loss,) + output) if loss is not None else output
|
1139 |
+
|
1140 |
+
return SequenceClassifierOutputWithPast(
|
1141 |
+
loss=loss,
|
1142 |
+
logits=pooled_logits,
|
1143 |
+
past_key_values=transformer_outputs.past_key_values,
|
1144 |
+
hidden_states=transformer_outputs.hidden_states,
|
1145 |
+
attentions=transformer_outputs.attentions,
|
1146 |
+
)
|
special_tokens_map.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "[CLS]",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"eos_token": {
|
10 |
+
"content": "</s>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"pad_token": {
|
17 |
+
"content": "<|endoftext|>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"unk_token": {
|
24 |
+
"content": "<|endoftext|>",
|
25 |
+
"lstrip": false,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
}
|
30 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_prefix_space": false,
|
3 |
+
"added_tokens_decoder": {
|
4 |
+
"0": {
|
5 |
+
"content": "<|endoftext|>",
|
6 |
+
"lstrip": false,
|
7 |
+
"normalized": false,
|
8 |
+
"rstrip": false,
|
9 |
+
"single_word": false,
|
10 |
+
"special": true
|
11 |
+
},
|
12 |
+
"100000": {
|
13 |
+
"content": "<|startofpiece|>",
|
14 |
+
"lstrip": false,
|
15 |
+
"normalized": true,
|
16 |
+
"rstrip": false,
|
17 |
+
"single_word": false,
|
18 |
+
"special": false
|
19 |
+
},
|
20 |
+
"100001": {
|
21 |
+
"content": "<|endofpiece|>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": true,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false,
|
26 |
+
"special": false
|
27 |
+
},
|
28 |
+
"100002": {
|
29 |
+
"content": "<|LDWANG|>",
|
30 |
+
"lstrip": false,
|
31 |
+
"normalized": true,
|
32 |
+
"rstrip": false,
|
33 |
+
"single_word": false,
|
34 |
+
"special": false
|
35 |
+
},
|
36 |
+
"100003": {
|
37 |
+
"content": "[MASK]",
|
38 |
+
"lstrip": false,
|
39 |
+
"normalized": true,
|
40 |
+
"rstrip": false,
|
41 |
+
"single_word": false,
|
42 |
+
"special": false
|
43 |
+
},
|
44 |
+
"100004": {
|
45 |
+
"content": "[gMASK]",
|
46 |
+
"lstrip": false,
|
47 |
+
"normalized": true,
|
48 |
+
"rstrip": false,
|
49 |
+
"single_word": false,
|
50 |
+
"special": false
|
51 |
+
},
|
52 |
+
"100005": {
|
53 |
+
"content": "[sMASK]",
|
54 |
+
"lstrip": false,
|
55 |
+
"normalized": true,
|
56 |
+
"rstrip": false,
|
57 |
+
"single_word": false,
|
58 |
+
"special": false
|
59 |
+
},
|
60 |
+
"100006": {
|
61 |
+
"content": "[CLS]",
|
62 |
+
"lstrip": false,
|
63 |
+
"normalized": false,
|
64 |
+
"rstrip": false,
|
65 |
+
"single_word": false,
|
66 |
+
"special": true
|
67 |
+
},
|
68 |
+
"100007": {
|
69 |
+
"content": "</s>",
|
70 |
+
"lstrip": false,
|
71 |
+
"normalized": false,
|
72 |
+
"rstrip": false,
|
73 |
+
"single_word": false,
|
74 |
+
"special": true
|
75 |
+
}
|
76 |
+
},
|
77 |
+
"bos_token": "[CLS]",
|
78 |
+
"clean_up_tokenization_spaces": true,
|
79 |
+
"eos_token": "</s>",
|
80 |
+
"model_max_length": 2048,
|
81 |
+
"pad_token": "<|endoftext|>",
|
82 |
+
"padding_side": "right",
|
83 |
+
"tokenizer_class": "GPT2Tokenizer",
|
84 |
+
"unk_token": "<|endoftext|>"
|
85 |
+
}
|
vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|