yanhui
commited on
Commit
•
4a38946
1
Parent(s):
760549e
initial commit
Browse files- added_tokens.json +10 -0
- config.json +41 -0
- configuration_minicpm.py +196 -0
- generation_config.json +7 -0
- modeling_minicpm.py +1572 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +81 -0
- tokenization_minicpm.py +430 -0
- tokenizer.json +0 -0
- tokenizer.model +3 -0
- tokenizer_config.json +119 -0
added_tokens.json
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"<|execute_end|>": 73444,
|
3 |
+
"<|execute_start|>": 73443,
|
4 |
+
"<|fim_middle|>": 73446,
|
5 |
+
"<|fim_prefix|>": 73445,
|
6 |
+
"<|fim_suffix|>": 73447,
|
7 |
+
"<|im_end|>": 73440,
|
8 |
+
"<|im_start|>": 73441,
|
9 |
+
"<|tool_call|>": 73442
|
10 |
+
}
|
config.json
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "openbmb/CPM-2B",
|
3 |
+
"architectures": [
|
4 |
+
"MiniCPM3ForCausalLM"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "configuration_minicpm.MiniCPMConfig",
|
8 |
+
"AutoModel": "modeling_minicpm.MiniCPMModel",
|
9 |
+
"AutoModelForCausalLM": "modeling_minicpm.MiniCPMForCausalLM",
|
10 |
+
"AutoModelForSeq2SeqLM": "modeling_minicpm.MiniCPMForCausalLM",
|
11 |
+
"AutoModelForSequenceClassification": "modeling_minicpm.MiniCPMForSequenceClassification"
|
12 |
+
},
|
13 |
+
"bos_token_id": 1,
|
14 |
+
"eos_token_id": 2,
|
15 |
+
"hidden_act": "silu",
|
16 |
+
"initializer_range": 0.1,
|
17 |
+
"hidden_size": 2560,
|
18 |
+
"num_hidden_layers": 62,
|
19 |
+
"intermediate_size": 6400,
|
20 |
+
"max_position_embeddings": 32768,
|
21 |
+
"num_attention_heads": 40,
|
22 |
+
"num_key_value_heads": 40,
|
23 |
+
"qk_nope_head_dim": 64,
|
24 |
+
"qk_rope_head_dim": 32,
|
25 |
+
"q_lora_rank": 768,
|
26 |
+
"kv_lora_rank": 256,
|
27 |
+
"rms_norm_eps": 1e-05,
|
28 |
+
"rope_scaling": {
|
29 |
+
"type": "longrope",
|
30 |
+
"long_factor": [1.0591234137867171, 1.1241891283591912, 1.2596935748670968, 1.5380380402321725, 2.093982484148734, 3.1446935121267696, 4.937952647693647, 7.524541999994549, 10.475458000005451, 13.062047352306353, 14.85530648787323, 15.906017515851266, 16.461961959767827, 16.740306425132907, 16.87581087164081, 16.940876586213285],
|
31 |
+
"short_factor": [1.0591234137867171, 1.1241891283591912, 1.2596935748670968, 1.5380380402321725, 2.093982484148734, 3.1446935121267696, 4.937952647693647, 7.524541999994549, 10.475458000005451, 13.062047352306353, 14.85530648787323, 15.906017515851266, 16.461961959767827, 16.740306425132907, 16.87581087164081, 16.940876586213285],
|
32 |
+
"original_max_position_embeddings": 32768
|
33 |
+
},
|
34 |
+
"torch_dtype": "bfloat16",
|
35 |
+
"transformers_version": "4.36.0",
|
36 |
+
"use_cache": true,
|
37 |
+
"vocab_size": 73448,
|
38 |
+
"scale_emb": 12,
|
39 |
+
"dim_model_base": 256,
|
40 |
+
"scale_depth": 1.4
|
41 |
+
}
|
configuration_minicpm.py
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 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 |
+
""" MiniCPM model configuration"""
|
21 |
+
|
22 |
+
from transformers.configuration_utils import PretrainedConfig
|
23 |
+
from transformers.utils import logging
|
24 |
+
|
25 |
+
|
26 |
+
logger = logging.get_logger(__name__)
|
27 |
+
|
28 |
+
MINICPM_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
|
29 |
+
|
30 |
+
|
31 |
+
class MiniCPMConfig(PretrainedConfig):
|
32 |
+
r"""
|
33 |
+
This is the configuration class to store the configuration of a [`MiniCPMModel`]. It is used to instantiate an MiniCPM
|
34 |
+
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
|
35 |
+
defaults will yield a similar configuration to that of the MiniCPM-7B.
|
36 |
+
|
37 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
38 |
+
documentation from [`PretrainedConfig`] for more information.
|
39 |
+
|
40 |
+
|
41 |
+
Args:
|
42 |
+
vocab_size (`int`, *optional*, defaults to 32000):
|
43 |
+
Vocabulary size of the MiniCPM model. Defines the number of different tokens that can be represented by the
|
44 |
+
`inputs_ids` passed when calling [`MiniCPMModel`]
|
45 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
46 |
+
Dimension of the hidden representations.
|
47 |
+
intermediate_size (`int`, *optional*, defaults to 11008):
|
48 |
+
Dimension of the MLP representations.
|
49 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
50 |
+
Number of hidden layers in the Transformer decoder.
|
51 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
52 |
+
Number of attention heads for each attention layer in the Transformer decoder.
|
53 |
+
num_key_value_heads (`int`, *optional*):
|
54 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
55 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
56 |
+
`num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
57 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
58 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
59 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
|
60 |
+
`num_attention_heads`.
|
61 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
62 |
+
The non-linear activation function (function or string) in the decoder.
|
63 |
+
max_position_embeddings (`int`, *optional*, defaults to 2048):
|
64 |
+
The maximum sequence length that this model might ever be used with. MiniCPM 1 supports up to 2048 tokens,
|
65 |
+
MiniCPM 2 up to 4096, CodeMiniCPM up to 16384.
|
66 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
67 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
68 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
69 |
+
The epsilon used by the rms normalization layers.
|
70 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
71 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
72 |
+
relevant if `config.is_decoder=True`.
|
73 |
+
pad_token_id (`int`, *optional*):
|
74 |
+
Padding token id.
|
75 |
+
bos_token_id (`int`, *optional*, defaults to 1):
|
76 |
+
Beginning of stream token id.
|
77 |
+
eos_token_id (`int`, *optional*, defaults to 2):
|
78 |
+
End of stream token id.
|
79 |
+
pretraining_tp (`int`, *optional*, defaults to 1):
|
80 |
+
Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
|
81 |
+
document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is
|
82 |
+
necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
|
83 |
+
issue](https://github.com/pytorch/pytorch/issues/76232).
|
84 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
85 |
+
Whether to tie weight embeddings
|
86 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
87 |
+
The base period of the RoPE embeddings.
|
88 |
+
rope_scaling (`Dict`, *optional*):
|
89 |
+
Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
|
90 |
+
strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
|
91 |
+
`{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
|
92 |
+
`max_position_embeddings` to the expected new maximum. See the following thread for more information on how
|
93 |
+
these scaling strategies behave:
|
94 |
+
https://www.reddit.com/r/LocalMiniCPM/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
|
95 |
+
experimental feature, subject to breaking API changes in future versions.
|
96 |
+
attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
|
97 |
+
Whether to use a bias in the query, key, value and output projection layers during self-attention.
|
98 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
99 |
+
The dropout ratio for the attention probabilities.
|
100 |
+
|
101 |
+
```python
|
102 |
+
>>> from transformers import MiniCPMModel, MiniCPMConfig
|
103 |
+
|
104 |
+
>>> # Initializing a MiniCPM minicpm-7b style configuration
|
105 |
+
>>> configuration = MiniCPMConfig()
|
106 |
+
|
107 |
+
>>> # Initializing a model from the minicpm-7b style configuration
|
108 |
+
>>> model = MiniCPMModel(configuration)
|
109 |
+
|
110 |
+
>>> # Accessing the model configuration
|
111 |
+
>>> configuration = model.config
|
112 |
+
```"""
|
113 |
+
|
114 |
+
model_type = "minicpm3"
|
115 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
116 |
+
|
117 |
+
def __init__(
|
118 |
+
self,
|
119 |
+
vocab_size=32000,
|
120 |
+
hidden_size=4096,
|
121 |
+
intermediate_size=11008,
|
122 |
+
num_hidden_layers=32,
|
123 |
+
num_attention_heads=32,
|
124 |
+
num_key_value_heads=None,
|
125 |
+
qk_nope_head_dim=64,
|
126 |
+
qk_rope_head_dim=32,
|
127 |
+
q_lora_rank=768,
|
128 |
+
kv_lora_rank=256,
|
129 |
+
v_head_dim=None,
|
130 |
+
head_dim=None,
|
131 |
+
hidden_act="silu",
|
132 |
+
max_position_embeddings=2048,
|
133 |
+
initializer_range=0.02,
|
134 |
+
rms_norm_eps=1e-6,
|
135 |
+
use_cache=True,
|
136 |
+
pad_token_id=None,
|
137 |
+
bos_token_id=1,
|
138 |
+
eos_token_id=2,
|
139 |
+
pretraining_tp=1,
|
140 |
+
tie_word_embeddings=True,
|
141 |
+
rope_theta=10000.0,
|
142 |
+
rope_scaling=None,
|
143 |
+
attention_bias=False,
|
144 |
+
attention_dropout=0.0,
|
145 |
+
scale_emb=1,
|
146 |
+
dim_model_base=1,
|
147 |
+
scale_depth=1,
|
148 |
+
**kwargs,
|
149 |
+
):
|
150 |
+
self.vocab_size = vocab_size
|
151 |
+
self.max_position_embeddings = max_position_embeddings
|
152 |
+
self.hidden_size = hidden_size
|
153 |
+
self.intermediate_size = intermediate_size
|
154 |
+
self.num_hidden_layers = num_hidden_layers
|
155 |
+
self.num_attention_heads = num_attention_heads
|
156 |
+
self.qk_nope_head_dim = qk_nope_head_dim
|
157 |
+
self.qk_rope_head_dim = qk_rope_head_dim
|
158 |
+
self.q_lora_rank = q_lora_rank
|
159 |
+
self.kv_lora_rank = kv_lora_rank
|
160 |
+
|
161 |
+
if v_head_dim is None:
|
162 |
+
v_head_dim = qk_nope_head_dim
|
163 |
+
self.v_head_dim = v_head_dim
|
164 |
+
|
165 |
+
# for backward compatibility
|
166 |
+
if num_key_value_heads is None:
|
167 |
+
num_key_value_heads = num_attention_heads
|
168 |
+
|
169 |
+
self.num_key_value_heads = num_key_value_heads
|
170 |
+
self.hidden_act = hidden_act
|
171 |
+
self.initializer_range = initializer_range
|
172 |
+
self.rms_norm_eps = rms_norm_eps
|
173 |
+
self.pretraining_tp = pretraining_tp
|
174 |
+
self.use_cache = use_cache
|
175 |
+
self.rope_theta = rope_theta
|
176 |
+
self.rope_scaling = rope_scaling
|
177 |
+
self._rope_scaling_validation()
|
178 |
+
self.attention_bias = attention_bias
|
179 |
+
self.attention_dropout = attention_dropout
|
180 |
+
self.scale_emb = scale_emb
|
181 |
+
self.dim_model_base = dim_model_base
|
182 |
+
self.scale_depth = scale_depth
|
183 |
+
self.head_dim = self.qk_nope_head_dim + self.qk_rope_head_dim
|
184 |
+
|
185 |
+
super().__init__(
|
186 |
+
pad_token_id=pad_token_id,
|
187 |
+
bos_token_id=bos_token_id,
|
188 |
+
eos_token_id=eos_token_id,
|
189 |
+
tie_word_embeddings=tie_word_embeddings,
|
190 |
+
**kwargs,
|
191 |
+
)
|
192 |
+
try:
|
193 |
+
import flash_attn
|
194 |
+
self._attn_implementation = "flash_attention_2"
|
195 |
+
except:
|
196 |
+
pass
|
generation_config.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"do_sample": true,
|
3 |
+
"top_p": 0.8,
|
4 |
+
"temperature": 0.8,
|
5 |
+
"bos_token_id": 1,
|
6 |
+
"eos_token_id": [2, 73440]
|
7 |
+
}
|
modeling_minicpm.py
ADDED
@@ -0,0 +1,1572 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 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 MiniCPM model."""
|
21 |
+
import math
|
22 |
+
import warnings
|
23 |
+
from typing import List, Optional, Tuple, Union, Dict
|
24 |
+
|
25 |
+
import torch
|
26 |
+
import torch.nn.functional as F
|
27 |
+
import torch.utils.checkpoint
|
28 |
+
from torch import nn
|
29 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
30 |
+
|
31 |
+
from transformers.activations import ACT2FN
|
32 |
+
from transformers.cache_utils import Cache, DynamicCache
|
33 |
+
from transformers.modeling_attn_mask_utils import (
|
34 |
+
AttentionMaskConverter,
|
35 |
+
_prepare_4d_attention_mask,
|
36 |
+
_prepare_4d_causal_attention_mask,
|
37 |
+
_prepare_4d_causal_attention_mask_for_sdpa,
|
38 |
+
)
|
39 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
|
40 |
+
from transformers.modeling_utils import PreTrainedModel
|
41 |
+
from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS, is_torch_greater_or_equal_than_1_13
|
42 |
+
from transformers.utils import (
|
43 |
+
add_start_docstrings,
|
44 |
+
add_start_docstrings_to_model_forward,
|
45 |
+
is_flash_attn_2_available,
|
46 |
+
is_flash_attn_greater_or_equal_2_10,
|
47 |
+
logging,
|
48 |
+
replace_return_docstrings,
|
49 |
+
)
|
50 |
+
from transformers.utils.import_utils import is_torch_fx_available
|
51 |
+
from .configuration_minicpm import MiniCPMConfig
|
52 |
+
import re
|
53 |
+
|
54 |
+
try:
|
55 |
+
from flash_attn import flash_attn_func, flash_attn_varlen_func
|
56 |
+
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
|
57 |
+
except:
|
58 |
+
pass
|
59 |
+
|
60 |
+
|
61 |
+
# This makes `_prepare_4d_causal_attention_mask` a leaf function in the FX graph.
|
62 |
+
# It means that the function will not be traced through and simply appear as a node in the graph.
|
63 |
+
if is_torch_fx_available():
|
64 |
+
if not is_torch_greater_or_equal_than_1_13:
|
65 |
+
import torch.fx
|
66 |
+
|
67 |
+
_prepare_4d_causal_attention_mask = torch.fx.wrap(_prepare_4d_causal_attention_mask)
|
68 |
+
|
69 |
+
|
70 |
+
logger = logging.get_logger(__name__)
|
71 |
+
|
72 |
+
_CONFIG_FOR_DOC = "MiniCPMConfig"
|
73 |
+
|
74 |
+
|
75 |
+
def _get_unpad_data(attention_mask):
|
76 |
+
seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
|
77 |
+
indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
|
78 |
+
max_seqlen_in_batch = seqlens_in_batch.max().item()
|
79 |
+
cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
|
80 |
+
return (
|
81 |
+
indices,
|
82 |
+
cu_seqlens,
|
83 |
+
max_seqlen_in_batch,
|
84 |
+
)
|
85 |
+
|
86 |
+
|
87 |
+
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
|
88 |
+
warnings.warn(
|
89 |
+
"Calling `transformers.models.minicpm.modeling_minicpm._prepare_4d_attention_mask` is deprecated and will be removed in v4.37. Use `transformers.modeling_attn_mask_utils._prepare_4d_attention_mask"
|
90 |
+
)
|
91 |
+
return _prepare_4d_attention_mask(mask=mask, dtype=dtype, tgt_len=tgt_len)
|
92 |
+
|
93 |
+
|
94 |
+
def _make_causal_mask(
|
95 |
+
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
96 |
+
):
|
97 |
+
warnings.warn(
|
98 |
+
"Calling `transformers.models.minicpm.modeling_minicpm._make_causal_mask` is deprecated and will be removed in v4.37. Use `transformers.models.minicpm.modeling_minicpm.AttentionMaskConverter._make_causal_mask"
|
99 |
+
)
|
100 |
+
return AttentionMaskConverter._make_causal_mask(
|
101 |
+
input_ids_shape=input_ids_shape, dtype=dtype, device=device, past_key_values_length=past_key_values_length
|
102 |
+
)
|
103 |
+
|
104 |
+
# @torch.jit.script # type: ignore
|
105 |
+
def rms_layernorm(hidden: torch.Tensor, weight: torch.Tensor, eps: float):
|
106 |
+
old_dtype = hidden.dtype
|
107 |
+
variance = hidden.to(torch.float32).pow(2).mean(dim=-1, keepdim=True)
|
108 |
+
hidden = (hidden * torch.rsqrt(variance + eps)).to(old_dtype)
|
109 |
+
return hidden * weight
|
110 |
+
|
111 |
+
|
112 |
+
class MiniCPMRMSNorm(nn.Module):
|
113 |
+
def __init__(self, hidden_size, eps=1e-6):
|
114 |
+
"""
|
115 |
+
MiniCPMRMSNorm is equivalent to T5LayerNorm
|
116 |
+
"""
|
117 |
+
super().__init__()
|
118 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
119 |
+
self.variance_epsilon = eps
|
120 |
+
|
121 |
+
def forward(self, hidden_states):
|
122 |
+
return rms_layernorm(hidden_states, self.weight, self.variance_epsilon)
|
123 |
+
|
124 |
+
|
125 |
+
ALL_LAYERNORM_LAYERS.append(MiniCPMRMSNorm)
|
126 |
+
|
127 |
+
|
128 |
+
class MiniCPMRotaryEmbedding(nn.Module):
|
129 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
130 |
+
super().__init__()
|
131 |
+
|
132 |
+
self.dim = dim
|
133 |
+
self.max_position_embeddings = max_position_embeddings
|
134 |
+
self.base = base
|
135 |
+
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
|
136 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
137 |
+
|
138 |
+
# Build here to make `torch.jit.trace` work.
|
139 |
+
self._set_cos_sin_cache(
|
140 |
+
# seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
|
141 |
+
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.float32
|
142 |
+
)
|
143 |
+
|
144 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
145 |
+
self.max_seq_len_cached = seq_len
|
146 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
147 |
+
freqs = torch.outer(t, self.inv_freq)
|
148 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
149 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
150 |
+
|
151 |
+
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
152 |
+
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
153 |
+
|
154 |
+
def forward(self, x, seq_len=None):
|
155 |
+
# x: [bs, num_attention_heads, seq_len, head_size]
|
156 |
+
if seq_len > self.max_seq_len_cached:
|
157 |
+
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
|
158 |
+
|
159 |
+
return (
|
160 |
+
self.cos_cached[:seq_len].to(dtype=x.dtype),
|
161 |
+
self.sin_cached[:seq_len].to(dtype=x.dtype),
|
162 |
+
)
|
163 |
+
|
164 |
+
|
165 |
+
class MiniCPMLinearScalingRotaryEmbedding(MiniCPMRotaryEmbedding):
|
166 |
+
"""MiniCPMRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
|
167 |
+
|
168 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
|
169 |
+
self.scaling_factor = scaling_factor
|
170 |
+
super().__init__(dim, max_position_embeddings, base, device)
|
171 |
+
|
172 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
173 |
+
self.max_seq_len_cached = seq_len
|
174 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
175 |
+
t = t / self.scaling_factor
|
176 |
+
|
177 |
+
freqs = torch.outer(t, self.inv_freq)
|
178 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
179 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
180 |
+
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
181 |
+
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
182 |
+
|
183 |
+
|
184 |
+
class MiniCPMDynamicNTKScalingRotaryEmbedding(MiniCPMRotaryEmbedding):
|
185 |
+
"""MiniCPMRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
|
186 |
+
|
187 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
|
188 |
+
self.scaling_factor = scaling_factor
|
189 |
+
super().__init__(dim, max_position_embeddings, base, device)
|
190 |
+
|
191 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
192 |
+
self.max_seq_len_cached = seq_len
|
193 |
+
|
194 |
+
if seq_len > self.max_position_embeddings:
|
195 |
+
base = self.base * (
|
196 |
+
(self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
|
197 |
+
) ** (self.dim / (self.dim - 2))
|
198 |
+
inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
|
199 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
200 |
+
|
201 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
202 |
+
|
203 |
+
freqs = torch.outer(t, self.inv_freq)
|
204 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
205 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
206 |
+
|
207 |
+
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
208 |
+
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
209 |
+
|
210 |
+
|
211 |
+
class MiniCPMLongRoPE(MiniCPMRotaryEmbedding):
|
212 |
+
"""MiniCPMRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
|
213 |
+
|
214 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, short_factor=None, long_factor=None, original_max_position_embeddings=None):
|
215 |
+
self.short_factor = short_factor
|
216 |
+
self.long_factor = long_factor
|
217 |
+
self.original_max_position_embeddings = original_max_position_embeddings
|
218 |
+
scale = (max_position_embeddings /
|
219 |
+
self.original_max_position_embeddings)
|
220 |
+
self.scaling_factor = math.sqrt(
|
221 |
+
1 + math.log(scale) /
|
222 |
+
math.log(self.original_max_position_embeddings))
|
223 |
+
super().__init__(dim, max_position_embeddings, base, device)
|
224 |
+
|
225 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
226 |
+
self.max_seq_len_cached = seq_len
|
227 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
228 |
+
if seq_len > self.original_max_position_embeddings:
|
229 |
+
ext_factors = torch.tensor(self.long_factor, dtype=torch.float32, device=device)
|
230 |
+
else:
|
231 |
+
ext_factors = torch.tensor(self.short_factor, dtype=torch.float32, device=device)
|
232 |
+
|
233 |
+
freqs = torch.mul(
|
234 |
+
torch.outer(t, 1.0 / ext_factors).to(device=device),
|
235 |
+
self.inv_freq.to(device=device).to(dtype)
|
236 |
+
)
|
237 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
238 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
239 |
+
self.register_buffer("cos_cached", emb.cos().to(dtype) * self.scaling_factor, persistent=False)
|
240 |
+
self.register_buffer("sin_cached", emb.sin().to(dtype) * self.scaling_factor, persistent=False)
|
241 |
+
|
242 |
+
|
243 |
+
def rotate_half(x):
|
244 |
+
"""Rotates half the hidden dims of the input."""
|
245 |
+
x1 = x[..., : x.shape[-1] // 2]
|
246 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
247 |
+
return torch.cat((-x2, x1), dim=-1)
|
248 |
+
|
249 |
+
|
250 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
|
251 |
+
"""Applies Rotary Position Embedding to the query and key tensors.
|
252 |
+
|
253 |
+
Args:
|
254 |
+
q (`torch.Tensor`): The query tensor.
|
255 |
+
k (`torch.Tensor`): The key tensor.
|
256 |
+
cos (`torch.Tensor`): The cosine part of the rotary embedding.
|
257 |
+
sin (`torch.Tensor`): The sine part of the rotary embedding.
|
258 |
+
position_ids (`torch.Tensor`):
|
259 |
+
The position indices of the tokens corresponding to the query and key tensors. For example, this can be
|
260 |
+
used to pass offsetted position ids when working with a KV-cache.
|
261 |
+
unsqueeze_dim (`int`, *optional*, defaults to 1):
|
262 |
+
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
|
263 |
+
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
|
264 |
+
that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
|
265 |
+
k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
|
266 |
+
cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
|
267 |
+
the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
|
268 |
+
Returns:
|
269 |
+
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
|
270 |
+
"""
|
271 |
+
# cos = cos[position_ids].unsqueeze(unsqueeze_dim)
|
272 |
+
# sin = sin[position_ids].unsqueeze(unsqueeze_dim)
|
273 |
+
# q_embed = (q * cos) + (rotate_half(q) * sin)
|
274 |
+
# k_embed = (k * cos) + (rotate_half(k) * sin)
|
275 |
+
orig_dtype = k.dtype
|
276 |
+
cos = cos[position_ids].unsqueeze(unsqueeze_dim) # [bs, 1, seq_len, dim]
|
277 |
+
sin = sin[position_ids].unsqueeze(unsqueeze_dim) # [bs, 1, seq_len, dim]
|
278 |
+
q_fp32 = q.to(dtype=torch.float32, device=q.device)
|
279 |
+
k_fp32 = k.to(dtype=torch.float32, device=k.device)
|
280 |
+
q_embed = (q_fp32 * cos) + (rotate_half(q_fp32) * sin)
|
281 |
+
k_embed = (k_fp32 * cos) + (rotate_half(k_fp32) * sin)
|
282 |
+
return q_embed.to(dtype=orig_dtype), k_embed.to(dtype=orig_dtype)
|
283 |
+
|
284 |
+
class MiniCPMMLP(nn.Module):
|
285 |
+
def __init__(self, config):
|
286 |
+
super().__init__()
|
287 |
+
self.config = config
|
288 |
+
self.hidden_size = config.hidden_size
|
289 |
+
self.intermediate_size = config.intermediate_size
|
290 |
+
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
291 |
+
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
292 |
+
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
|
293 |
+
self.act_fn = ACT2FN[config.hidden_act]
|
294 |
+
|
295 |
+
def forward(self, x):
|
296 |
+
if self.config.pretraining_tp > 1:
|
297 |
+
slice = self.intermediate_size // self.config.pretraining_tp
|
298 |
+
gate_proj_slices = self.gate_proj.weight.split(slice, dim=0)
|
299 |
+
up_proj_slices = self.up_proj.weight.split(slice, dim=0)
|
300 |
+
down_proj_slices = self.down_proj.weight.split(slice, dim=1)
|
301 |
+
|
302 |
+
gate_proj = torch.cat(
|
303 |
+
[F.linear(x, gate_proj_slices[i]) for i in range(self.config.pretraining_tp)], dim=-1
|
304 |
+
)
|
305 |
+
up_proj = torch.cat([F.linear(x, up_proj_slices[i]) for i in range(self.config.pretraining_tp)], dim=-1)
|
306 |
+
|
307 |
+
intermediate_states = (self.act_fn(gate_proj) * up_proj).split(slice, dim=2)
|
308 |
+
down_proj = [
|
309 |
+
F.linear(intermediate_states[i], down_proj_slices[i]) for i in range(self.config.pretraining_tp)
|
310 |
+
]
|
311 |
+
down_proj = sum(down_proj)
|
312 |
+
else:
|
313 |
+
down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
314 |
+
|
315 |
+
return down_proj
|
316 |
+
|
317 |
+
|
318 |
+
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
319 |
+
"""
|
320 |
+
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
321 |
+
num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
|
322 |
+
"""
|
323 |
+
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
|
324 |
+
if n_rep == 1:
|
325 |
+
return hidden_states
|
326 |
+
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
|
327 |
+
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
328 |
+
|
329 |
+
|
330 |
+
|
331 |
+
class MiniCPMAttention(nn.Module):
|
332 |
+
"""Multi-headed attention from 'Attention Is All You Need' paper"""
|
333 |
+
|
334 |
+
def __init__(self, config: MiniCPMConfig, layer_idx: Optional[int] = None):
|
335 |
+
super().__init__()
|
336 |
+
self.config = config
|
337 |
+
self.layer_idx = layer_idx
|
338 |
+
if layer_idx is None:
|
339 |
+
logger.warning_once(
|
340 |
+
f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
|
341 |
+
"to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
|
342 |
+
"when creating this class."
|
343 |
+
)
|
344 |
+
|
345 |
+
self.attention_dropout = config.attention_dropout
|
346 |
+
self.hidden_size = config.hidden_size
|
347 |
+
self.num_heads = config.num_attention_heads
|
348 |
+
|
349 |
+
self.max_position_embeddings = config.max_position_embeddings
|
350 |
+
self.rope_theta = config.rope_theta
|
351 |
+
self.q_lora_rank = config.q_lora_rank
|
352 |
+
self.qk_rope_head_dim = config.qk_rope_head_dim
|
353 |
+
self.kv_lora_rank = config.kv_lora_rank
|
354 |
+
self.v_head_dim = config.hidden_size // config.num_attention_heads
|
355 |
+
self.qk_nope_head_dim = config.qk_nope_head_dim
|
356 |
+
self.q_head_dim = config.qk_nope_head_dim + config.qk_rope_head_dim
|
357 |
+
|
358 |
+
self.is_causal = True
|
359 |
+
|
360 |
+
self.q_a_proj = nn.Linear(
|
361 |
+
self.hidden_size, config.q_lora_rank, bias=config.attention_bias
|
362 |
+
)
|
363 |
+
self.q_a_layernorm = MiniCPMRMSNorm(config.q_lora_rank)
|
364 |
+
self.q_b_proj = nn.Linear(
|
365 |
+
config.q_lora_rank, self.num_heads * self.q_head_dim, bias=False
|
366 |
+
)
|
367 |
+
self.kv_a_proj_with_mqa = nn.Linear(
|
368 |
+
self.hidden_size,
|
369 |
+
config.kv_lora_rank + config.qk_rope_head_dim,
|
370 |
+
bias=config.attention_bias,
|
371 |
+
)
|
372 |
+
self.kv_a_layernorm = MiniCPMRMSNorm(config.kv_lora_rank)
|
373 |
+
self.kv_b_proj = nn.Linear(
|
374 |
+
config.kv_lora_rank,
|
375 |
+
self.num_heads
|
376 |
+
* (self.q_head_dim - self.qk_rope_head_dim + self.v_head_dim),
|
377 |
+
bias=False,
|
378 |
+
)
|
379 |
+
|
380 |
+
self.o_proj = nn.Linear(
|
381 |
+
self.num_heads * self.v_head_dim,
|
382 |
+
self.hidden_size,
|
383 |
+
bias=config.attention_bias,
|
384 |
+
)
|
385 |
+
self._init_rope()
|
386 |
+
|
387 |
+
self.softmax_scale = self.q_head_dim ** (-0.5)
|
388 |
+
|
389 |
+
def _init_rope(self):
|
390 |
+
if self.config.rope_scaling is None:
|
391 |
+
self.rotary_emb = MiniCPMRotaryEmbedding(
|
392 |
+
self.qk_rope_head_dim,
|
393 |
+
max_position_embeddings=self.max_position_embeddings,
|
394 |
+
base=self.rope_theta,
|
395 |
+
)
|
396 |
+
else:
|
397 |
+
scaling_type = self.config.rope_scaling["type"]
|
398 |
+
if scaling_type == "linear":
|
399 |
+
self.rotary_emb = MiniCPMLinearScalingRotaryEmbedding(
|
400 |
+
self.qk_rope_head_dim,
|
401 |
+
max_position_embeddings=self.max_position_embeddings,
|
402 |
+
scaling_factor = self.config.rope_scaling["factor"],
|
403 |
+
base=self.rope_theta,
|
404 |
+
)
|
405 |
+
elif scaling_type == "dynamic":
|
406 |
+
self.rotary_emb = MiniCPMDynamicNTKScalingRotaryEmbedding(
|
407 |
+
self.qk_rope_head_dim,
|
408 |
+
max_position_embeddings=self.max_position_embeddings,
|
409 |
+
scaling_factor = self.config.rope_scaling["factor"],
|
410 |
+
base=self.rope_theta,
|
411 |
+
)
|
412 |
+
elif scaling_type == "longrope":
|
413 |
+
self.rotary_emb = MiniCPMLongRoPE(
|
414 |
+
self.qk_rope_head_dim,
|
415 |
+
max_position_embeddings=self.max_position_embeddings,
|
416 |
+
short_factor = self.config.rope_scaling["short_factor"],
|
417 |
+
long_factor = self.config.rope_scaling["long_factor"],
|
418 |
+
base=self.rope_theta,
|
419 |
+
original_max_position_embeddings=self.config.rope_scaling["original_max_position_embeddings"]
|
420 |
+
)
|
421 |
+
else:
|
422 |
+
raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
|
423 |
+
|
424 |
+
def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
|
425 |
+
return tensor.view(bsz, seq_len, self.num_heads, self.v_head_dim).transpose(1, 2).contiguous()
|
426 |
+
|
427 |
+
def forward(
|
428 |
+
self,
|
429 |
+
hidden_states: torch.Tensor,
|
430 |
+
attention_mask: Optional[torch.Tensor] = None,
|
431 |
+
position_ids: Optional[torch.LongTensor] = None,
|
432 |
+
past_key_value: Optional[Cache] = None,
|
433 |
+
output_attentions: bool = False,
|
434 |
+
use_cache: bool = False,
|
435 |
+
**kwargs,
|
436 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
437 |
+
if "padding_mask" in kwargs:
|
438 |
+
warnings.warn(
|
439 |
+
"Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
|
440 |
+
)
|
441 |
+
|
442 |
+
bsz, q_len, _ = hidden_states.size()
|
443 |
+
|
444 |
+
q = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(hidden_states)))
|
445 |
+
q = q.view(bsz, q_len, self.num_heads, self.q_head_dim).transpose(1, 2)
|
446 |
+
q_nope, q_pe = torch.split(
|
447 |
+
q, [self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1
|
448 |
+
)
|
449 |
+
|
450 |
+
compressed_kv = self.kv_a_proj_with_mqa(hidden_states)
|
451 |
+
compressed_kv, k_pe = torch.split(
|
452 |
+
compressed_kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1
|
453 |
+
)
|
454 |
+
k_pe = k_pe.view(bsz, q_len, 1, self.qk_rope_head_dim).transpose(1, 2)
|
455 |
+
kv = (
|
456 |
+
self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
|
457 |
+
.view(bsz, q_len, self.num_heads, self.qk_nope_head_dim + self.v_head_dim)
|
458 |
+
.transpose(1, 2)
|
459 |
+
)
|
460 |
+
|
461 |
+
k_nope, value_states = torch.split(
|
462 |
+
kv, [self.qk_nope_head_dim, self.v_head_dim], dim=-1
|
463 |
+
)
|
464 |
+
kv_seq_len = value_states.shape[-2]
|
465 |
+
if past_key_value is not None:
|
466 |
+
if self.layer_idx is None:
|
467 |
+
raise ValueError(
|
468 |
+
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
469 |
+
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
470 |
+
"with a layer index."
|
471 |
+
)
|
472 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
473 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
474 |
+
|
475 |
+
q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids)
|
476 |
+
|
477 |
+
query_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
|
478 |
+
query_states[:, :, :, : self.qk_nope_head_dim] = q_nope
|
479 |
+
query_states[:, :, :, self.qk_nope_head_dim :] = q_pe
|
480 |
+
|
481 |
+
key_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
|
482 |
+
key_states[:, :, :, : self.qk_nope_head_dim] = k_nope
|
483 |
+
key_states[:, :, :, self.qk_nope_head_dim :] = k_pe
|
484 |
+
if past_key_value is not None:
|
485 |
+
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
|
486 |
+
key_states, value_states = past_key_value.update(
|
487 |
+
key_states, value_states, self.layer_idx, cache_kwargs
|
488 |
+
)
|
489 |
+
|
490 |
+
attn_weights = (
|
491 |
+
torch.matmul(query_states, key_states.transpose(2, 3)) * self.softmax_scale
|
492 |
+
)
|
493 |
+
|
494 |
+
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
495 |
+
raise ValueError(
|
496 |
+
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
|
497 |
+
f" {attn_weights.size()}"
|
498 |
+
)
|
499 |
+
assert attention_mask is not None
|
500 |
+
if attention_mask is not None:
|
501 |
+
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
|
502 |
+
raise ValueError(
|
503 |
+
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
|
504 |
+
)
|
505 |
+
attn_weights = attn_weights + attention_mask
|
506 |
+
|
507 |
+
# upcast attention to fp32
|
508 |
+
attn_weights = nn.functional.softmax(
|
509 |
+
attn_weights, dim=-1, dtype=torch.float32
|
510 |
+
).to(query_states.dtype)
|
511 |
+
attn_weights = nn.functional.dropout(
|
512 |
+
attn_weights, p=self.attention_dropout, training=self.training
|
513 |
+
)
|
514 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
515 |
+
|
516 |
+
if attn_output.size() != (bsz, self.num_heads, q_len, self.v_head_dim):
|
517 |
+
raise ValueError(
|
518 |
+
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.v_head_dim)}, but is"
|
519 |
+
f" {attn_output.size()}"
|
520 |
+
)
|
521 |
+
|
522 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
523 |
+
|
524 |
+
attn_output = attn_output.reshape(bsz, q_len, self.num_heads * self.v_head_dim)
|
525 |
+
|
526 |
+
attn_output = self.o_proj(attn_output)
|
527 |
+
|
528 |
+
if not output_attentions:
|
529 |
+
attn_weights = None
|
530 |
+
|
531 |
+
return attn_output, attn_weights, past_key_value
|
532 |
+
|
533 |
+
|
534 |
+
class MiniCPMFlashAttention2(MiniCPMAttention):
|
535 |
+
"""
|
536 |
+
MiniCPM flash attention module. This module inherits from `MiniCPMAttention` as the weights of the module stays
|
537 |
+
untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
|
538 |
+
flash attention and deal with padding tokens in case the input contains any of them.
|
539 |
+
"""
|
540 |
+
|
541 |
+
def __init__(self, *args, **kwargs):
|
542 |
+
super().__init__(*args, **kwargs)
|
543 |
+
|
544 |
+
# TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
|
545 |
+
# flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
|
546 |
+
# Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
|
547 |
+
self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
|
548 |
+
|
549 |
+
def forward(
|
550 |
+
self,
|
551 |
+
hidden_states: torch.Tensor,
|
552 |
+
attention_mask: Optional[torch.LongTensor] = None,
|
553 |
+
position_ids: Optional[torch.LongTensor] = None,
|
554 |
+
past_key_value: Optional[Cache] = None,
|
555 |
+
output_attentions: bool = False,
|
556 |
+
use_cache: bool = False,
|
557 |
+
**kwargs,
|
558 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
559 |
+
# MiniCPMFlashAttention2 attention does not support output_attentions
|
560 |
+
if "padding_mask" in kwargs:
|
561 |
+
warnings.warn(
|
562 |
+
"Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
|
563 |
+
)
|
564 |
+
|
565 |
+
# overwrite attention_mask with padding_mask
|
566 |
+
attention_mask = kwargs.pop("padding_mask")
|
567 |
+
|
568 |
+
output_attentions = False
|
569 |
+
|
570 |
+
bsz, q_len, _ = hidden_states.size()
|
571 |
+
|
572 |
+
q = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(hidden_states)))
|
573 |
+
q = q.view(bsz, q_len, self.num_heads, self.q_head_dim).transpose(1, 2)
|
574 |
+
q_nope, q_pe = torch.split(
|
575 |
+
q, [self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1
|
576 |
+
)
|
577 |
+
|
578 |
+
# Flash attention requires the input to have the shape
|
579 |
+
# batch_size x seq_length x head_dim x hidden_dim
|
580 |
+
# therefore we just need to keep the original shape
|
581 |
+
compressed_kv = self.kv_a_proj_with_mqa(hidden_states)
|
582 |
+
compressed_kv, k_pe = torch.split(
|
583 |
+
compressed_kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1
|
584 |
+
)
|
585 |
+
k_pe = k_pe.view(bsz, q_len, 1, self.qk_rope_head_dim).transpose(1, 2)
|
586 |
+
kv = (
|
587 |
+
self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
|
588 |
+
.view(bsz, q_len, self.num_heads, self.qk_nope_head_dim + self.v_head_dim)
|
589 |
+
.transpose(1, 2)
|
590 |
+
)
|
591 |
+
|
592 |
+
k_nope, value_states = torch.split(
|
593 |
+
kv, [self.qk_nope_head_dim, self.v_head_dim], dim=-1
|
594 |
+
)
|
595 |
+
|
596 |
+
kv_seq_len = value_states.shape[-2]
|
597 |
+
if past_key_value is not None:
|
598 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
599 |
+
|
600 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
601 |
+
q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids)
|
602 |
+
|
603 |
+
query_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
|
604 |
+
query_states[:, :, :, : self.qk_nope_head_dim] = q_nope
|
605 |
+
query_states[:, :, :, self.qk_nope_head_dim :] = q_pe
|
606 |
+
|
607 |
+
key_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
|
608 |
+
key_states[:, :, :, : self.qk_nope_head_dim] = k_nope
|
609 |
+
key_states[:, :, :, self.qk_nope_head_dim :] = k_pe
|
610 |
+
|
611 |
+
if self.q_head_dim != self.v_head_dim:
|
612 |
+
value_states = F.pad(value_states, [0, self.q_head_dim - self.v_head_dim])
|
613 |
+
|
614 |
+
if past_key_value is not None:
|
615 |
+
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
|
616 |
+
key_states, value_states = past_key_value.update(
|
617 |
+
key_states, value_states, self.layer_idx, cache_kwargs
|
618 |
+
)
|
619 |
+
|
620 |
+
# TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache
|
621 |
+
# to be able to avoid many of these transpose/reshape/view.
|
622 |
+
query_states = query_states.transpose(1, 2)
|
623 |
+
key_states = key_states.transpose(1, 2)
|
624 |
+
value_states = value_states.transpose(1, 2)
|
625 |
+
|
626 |
+
dropout_rate = self.attention_dropout if self.training else 0.0
|
627 |
+
|
628 |
+
input_dtype = query_states.dtype
|
629 |
+
if input_dtype == torch.float32:
|
630 |
+
# Handle the case where the model is quantized
|
631 |
+
if hasattr(self.config, "_pre_quantization_dtype"):
|
632 |
+
target_dtype = self.config._pre_quantization_dtype
|
633 |
+
elif torch.is_autocast_enabled():
|
634 |
+
target_dtype = torch.get_autocast_gpu_dtype()
|
635 |
+
else:
|
636 |
+
target_dtype = self.q_a_proj.weight.dtype
|
637 |
+
|
638 |
+
logger.warning_once(
|
639 |
+
f"The input hidden states seems to be silently casted in float32, this might be related to"
|
640 |
+
f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
|
641 |
+
f" {target_dtype}."
|
642 |
+
)
|
643 |
+
|
644 |
+
query_states = query_states.to(target_dtype)
|
645 |
+
key_states = key_states.to(target_dtype)
|
646 |
+
value_states = value_states.to(target_dtype)
|
647 |
+
|
648 |
+
attn_output = self._flash_attention_forward(
|
649 |
+
query_states,
|
650 |
+
key_states,
|
651 |
+
value_states,
|
652 |
+
attention_mask,
|
653 |
+
q_len,
|
654 |
+
dropout=dropout_rate,
|
655 |
+
softmax_scale=self.softmax_scale,
|
656 |
+
)
|
657 |
+
if self.q_head_dim != self.v_head_dim:
|
658 |
+
attn_output = attn_output[:, :, :, : self.v_head_dim]
|
659 |
+
|
660 |
+
attn_output = attn_output.reshape(
|
661 |
+
bsz, q_len, self.num_heads * self.v_head_dim
|
662 |
+
).contiguous()
|
663 |
+
attn_output = self.o_proj(attn_output)
|
664 |
+
|
665 |
+
if not output_attentions:
|
666 |
+
attn_weights = None
|
667 |
+
|
668 |
+
return attn_output, attn_weights, past_key_value
|
669 |
+
|
670 |
+
|
671 |
+
def _flash_attention_forward(
|
672 |
+
self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
|
673 |
+
):
|
674 |
+
"""
|
675 |
+
Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
|
676 |
+
first unpad the input, then computes the attention scores and pad the final attention scores.
|
677 |
+
|
678 |
+
Args:
|
679 |
+
query_states (`torch.Tensor`):
|
680 |
+
Input query states to be passed to Flash Attention API
|
681 |
+
key_states (`torch.Tensor`):
|
682 |
+
Input key states to be passed to Flash Attention API
|
683 |
+
value_states (`torch.Tensor`):
|
684 |
+
Input value states to be passed to Flash Attention API
|
685 |
+
attention_mask (`torch.Tensor`):
|
686 |
+
The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
|
687 |
+
position of padding tokens and 1 for the position of non-padding tokens.
|
688 |
+
dropout (`int`, *optional*):
|
689 |
+
Attention dropout
|
690 |
+
softmax_scale (`float`, *optional*):
|
691 |
+
The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
|
692 |
+
"""
|
693 |
+
if not self._flash_attn_uses_top_left_mask:
|
694 |
+
causal = self.is_causal
|
695 |
+
else:
|
696 |
+
# TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in MiniCPMFlashAttention2 __init__.
|
697 |
+
causal = self.is_causal and query_length != 1
|
698 |
+
# Contains at least one padding token in the sequence
|
699 |
+
if attention_mask is not None:
|
700 |
+
batch_size = query_states.shape[0]
|
701 |
+
query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
|
702 |
+
query_states, key_states, value_states, attention_mask, query_length
|
703 |
+
)
|
704 |
+
|
705 |
+
cu_seqlens_q, cu_seqlens_k = cu_seq_lens
|
706 |
+
max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
|
707 |
+
attn_output_unpad = flash_attn_varlen_func(
|
708 |
+
query_states,
|
709 |
+
key_states,
|
710 |
+
value_states,
|
711 |
+
cu_seqlens_q=cu_seqlens_q,
|
712 |
+
cu_seqlens_k=cu_seqlens_k,
|
713 |
+
max_seqlen_q=max_seqlen_in_batch_q,
|
714 |
+
max_seqlen_k=max_seqlen_in_batch_k,
|
715 |
+
dropout_p=dropout,
|
716 |
+
softmax_scale=softmax_scale,
|
717 |
+
causal=causal,
|
718 |
+
)
|
719 |
+
|
720 |
+
attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
|
721 |
+
else:
|
722 |
+
attn_output = flash_attn_func(
|
723 |
+
query_states, key_states, value_states, dropout, softmax_scale=softmax_scale, causal=causal
|
724 |
+
)
|
725 |
+
|
726 |
+
return attn_output
|
727 |
+
|
728 |
+
def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
|
729 |
+
indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
|
730 |
+
batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
|
731 |
+
|
732 |
+
key_layer = index_first_axis(
|
733 |
+
key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
|
734 |
+
)
|
735 |
+
value_layer = index_first_axis(
|
736 |
+
value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
|
737 |
+
)
|
738 |
+
if query_length == kv_seq_len:
|
739 |
+
query_layer = index_first_axis(
|
740 |
+
query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim), indices_k
|
741 |
+
)
|
742 |
+
cu_seqlens_q = cu_seqlens_k
|
743 |
+
max_seqlen_in_batch_q = max_seqlen_in_batch_k
|
744 |
+
indices_q = indices_k
|
745 |
+
elif query_length == 1:
|
746 |
+
max_seqlen_in_batch_q = 1
|
747 |
+
cu_seqlens_q = torch.arange(
|
748 |
+
batch_size + 1, dtype=torch.int32, device=query_layer.device
|
749 |
+
) # There is a memcpy here, that is very bad.
|
750 |
+
indices_q = cu_seqlens_q[:-1]
|
751 |
+
query_layer = query_layer.squeeze(1)
|
752 |
+
else:
|
753 |
+
# The -q_len: slice assumes left padding.
|
754 |
+
attention_mask = attention_mask[:, -query_length:]
|
755 |
+
query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
|
756 |
+
|
757 |
+
return (
|
758 |
+
query_layer,
|
759 |
+
key_layer,
|
760 |
+
value_layer,
|
761 |
+
indices_q,
|
762 |
+
(cu_seqlens_q, cu_seqlens_k),
|
763 |
+
(max_seqlen_in_batch_q, max_seqlen_in_batch_k),
|
764 |
+
)
|
765 |
+
|
766 |
+
|
767 |
+
class MiniCPMSdpaAttention(MiniCPMAttention):
|
768 |
+
"""
|
769 |
+
MiniCPM attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
|
770 |
+
`MiniCPMAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
|
771 |
+
SDPA API.
|
772 |
+
"""
|
773 |
+
|
774 |
+
# Adapted from MiniCPMAttention.forward
|
775 |
+
def forward(
|
776 |
+
self,
|
777 |
+
hidden_states: torch.Tensor,
|
778 |
+
attention_mask: Optional[torch.Tensor] = None,
|
779 |
+
position_ids: Optional[torch.LongTensor] = None,
|
780 |
+
past_key_value: Optional[Cache] = None,
|
781 |
+
output_attentions: bool = False,
|
782 |
+
use_cache: bool = False,
|
783 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
784 |
+
if output_attentions:
|
785 |
+
# TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
|
786 |
+
logger.warning_once(
|
787 |
+
"MiniCPMModel is using MiniCPMSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
|
788 |
+
'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
|
789 |
+
)
|
790 |
+
return super().forward(
|
791 |
+
hidden_states=hidden_states,
|
792 |
+
attention_mask=attention_mask,
|
793 |
+
position_ids=position_ids,
|
794 |
+
past_key_value=past_key_value,
|
795 |
+
output_attentions=output_attentions,
|
796 |
+
use_cache=use_cache,
|
797 |
+
)
|
798 |
+
|
799 |
+
bsz, q_len, _ = hidden_states.size()
|
800 |
+
|
801 |
+
q = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(hidden_states)))
|
802 |
+
q = q.view(bsz, q_len, self.num_heads, self.q_head_dim).transpose(1, 2)
|
803 |
+
q_nope, q_pe = torch.split(
|
804 |
+
q, [self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1
|
805 |
+
)
|
806 |
+
|
807 |
+
compressed_kv = self.kv_a_proj_with_mqa(hidden_states)
|
808 |
+
compressed_kv, k_pe = torch.split(
|
809 |
+
compressed_kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1
|
810 |
+
)
|
811 |
+
k_pe = k_pe.view(bsz, q_len, 1, self.qk_rope_head_dim).transpose(1, 2)
|
812 |
+
kv = (
|
813 |
+
self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
|
814 |
+
.view(bsz, q_len, self.num_heads, self.qk_nope_head_dim + self.v_head_dim)
|
815 |
+
.transpose(1, 2)
|
816 |
+
)
|
817 |
+
|
818 |
+
k_nope, value_states = torch.split(
|
819 |
+
kv, [self.qk_nope_head_dim, self.v_head_dim], dim=-1
|
820 |
+
)
|
821 |
+
|
822 |
+
kv_seq_len = value_states.shape[-2]
|
823 |
+
if past_key_value is not None:
|
824 |
+
if self.layer_idx is None:
|
825 |
+
raise ValueError(
|
826 |
+
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
827 |
+
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
828 |
+
"with a layer index."
|
829 |
+
)
|
830 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
831 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
832 |
+
|
833 |
+
q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids)
|
834 |
+
|
835 |
+
query_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
|
836 |
+
query_states[:, :, :, : self.qk_nope_head_dim] = q_nope
|
837 |
+
query_states[:, :, :, self.qk_nope_head_dim :] = q_pe
|
838 |
+
|
839 |
+
key_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
|
840 |
+
key_states[:, :, :, : self.qk_nope_head_dim] = k_nope
|
841 |
+
key_states[:, :, :, self.qk_nope_head_dim :] = k_pe
|
842 |
+
if past_key_value is not None:
|
843 |
+
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
|
844 |
+
key_states, value_states = past_key_value.update(
|
845 |
+
key_states, value_states, self.layer_idx, cache_kwargs
|
846 |
+
)
|
847 |
+
|
848 |
+
if attention_mask is not None:
|
849 |
+
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
|
850 |
+
raise ValueError(
|
851 |
+
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
|
852 |
+
)
|
853 |
+
|
854 |
+
# SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
|
855 |
+
# Reference: https://github.com/pytorch/pytorch/issues/112577.
|
856 |
+
if query_states.device.type == "cuda" and attention_mask is not None:
|
857 |
+
query_states = query_states.contiguous()
|
858 |
+
key_states = key_states.contiguous()
|
859 |
+
value_states = value_states.contiguous()
|
860 |
+
|
861 |
+
attn_output = torch.nn.functional.scaled_dot_product_attention(
|
862 |
+
query_states,
|
863 |
+
key_states,
|
864 |
+
value_states,
|
865 |
+
attn_mask=attention_mask,
|
866 |
+
dropout_p=self.attention_dropout if self.training else 0.0,
|
867 |
+
# The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
|
868 |
+
is_causal=self.is_causal and attention_mask is None and q_len > 1,
|
869 |
+
)
|
870 |
+
|
871 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
872 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
873 |
+
|
874 |
+
attn_output = self.o_proj(attn_output)
|
875 |
+
|
876 |
+
return attn_output, None, past_key_value
|
877 |
+
|
878 |
+
|
879 |
+
MINICPM_ATTENTION_CLASSES = {
|
880 |
+
"eager": MiniCPMAttention,
|
881 |
+
"flash_attention_2": MiniCPMFlashAttention2,
|
882 |
+
"sdpa": MiniCPMSdpaAttention,
|
883 |
+
}
|
884 |
+
|
885 |
+
|
886 |
+
class MiniCPMDecoderLayer(nn.Module):
|
887 |
+
def __init__(self, config: MiniCPMConfig, layer_idx: int):
|
888 |
+
super().__init__()
|
889 |
+
self.hidden_size = config.hidden_size
|
890 |
+
self.self_attn = MINICPM_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx)
|
891 |
+
|
892 |
+
self.mlp = MiniCPMMLP(config)
|
893 |
+
self.input_layernorm = MiniCPMRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
894 |
+
self.post_attention_layernorm = MiniCPMRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
895 |
+
|
896 |
+
self.scale_depth = config.scale_depth
|
897 |
+
self.num_hidden_layers = config.num_hidden_layers
|
898 |
+
|
899 |
+
def forward(
|
900 |
+
self,
|
901 |
+
hidden_states: torch.Tensor,
|
902 |
+
attention_mask: Optional[torch.Tensor] = None,
|
903 |
+
position_ids: Optional[torch.LongTensor] = None,
|
904 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
905 |
+
output_attentions: Optional[bool] = False,
|
906 |
+
use_cache: Optional[bool] = False,
|
907 |
+
**kwargs,
|
908 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
909 |
+
"""
|
910 |
+
Args:
|
911 |
+
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
912 |
+
attention_mask (`torch.FloatTensor`, *optional*):
|
913 |
+
attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1,
|
914 |
+
query_sequence_length, key_sequence_length)` if default attention is used.
|
915 |
+
output_attentions (`bool`, *optional*):
|
916 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
917 |
+
returned tensors for more detail.
|
918 |
+
use_cache (`bool`, *optional*):
|
919 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
920 |
+
(see `past_key_values`).
|
921 |
+
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
|
922 |
+
"""
|
923 |
+
if "padding_mask" in kwargs:
|
924 |
+
warnings.warn(
|
925 |
+
"Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
|
926 |
+
)
|
927 |
+
|
928 |
+
residual = hidden_states
|
929 |
+
hidden_states = self.input_layernorm(hidden_states)
|
930 |
+
# Self Attention
|
931 |
+
hidden_states, self_attn_weights, present_key_value = self.self_attn(
|
932 |
+
hidden_states=hidden_states,
|
933 |
+
attention_mask=attention_mask,
|
934 |
+
position_ids=position_ids,
|
935 |
+
past_key_value=past_key_value,
|
936 |
+
output_attentions=output_attentions,
|
937 |
+
use_cache=use_cache,
|
938 |
+
**kwargs,
|
939 |
+
)
|
940 |
+
|
941 |
+
hidden_states = residual + hidden_states * (self.scale_depth / math.sqrt(self.num_hidden_layers))
|
942 |
+
|
943 |
+
# Fully Connected
|
944 |
+
residual = hidden_states
|
945 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
946 |
+
|
947 |
+
hidden_states = self.mlp(hidden_states)
|
948 |
+
hidden_states = residual + hidden_states * (self.scale_depth / math.sqrt(self.num_hidden_layers))
|
949 |
+
|
950 |
+
outputs = (hidden_states,)
|
951 |
+
|
952 |
+
if output_attentions:
|
953 |
+
outputs += (self_attn_weights,)
|
954 |
+
|
955 |
+
if use_cache:
|
956 |
+
outputs += (present_key_value,)
|
957 |
+
|
958 |
+
return outputs
|
959 |
+
|
960 |
+
|
961 |
+
MINICPM_START_DOCSTRING = r"""
|
962 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
963 |
+
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
964 |
+
etc.)
|
965 |
+
|
966 |
+
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
967 |
+
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
968 |
+
and behavior.
|
969 |
+
|
970 |
+
Parameters:
|
971 |
+
config ([`MiniCPMConfig`]):
|
972 |
+
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
973 |
+
load the weights associated with the model, only the configuration. Check out the
|
974 |
+
[`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
975 |
+
"""
|
976 |
+
|
977 |
+
|
978 |
+
@add_start_docstrings(
|
979 |
+
"The bare MiniCPM Model outputting raw hidden-states without any specific head on top.",
|
980 |
+
MINICPM_START_DOCSTRING,
|
981 |
+
)
|
982 |
+
class MiniCPMPreTrainedModel(PreTrainedModel):
|
983 |
+
config_class = MiniCPMConfig
|
984 |
+
base_model_prefix = "model"
|
985 |
+
supports_gradient_checkpointing = True
|
986 |
+
_no_split_modules = ["MiniCPMDecoderLayer"]
|
987 |
+
_skip_keys_device_placement = "past_key_values"
|
988 |
+
_supports_flash_attn_2 = True
|
989 |
+
_supports_sdpa = True
|
990 |
+
_supports_cache_class = True
|
991 |
+
|
992 |
+
def _init_weights(self, module):
|
993 |
+
std = self.config.initializer_range
|
994 |
+
if isinstance(module, nn.Linear):
|
995 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
996 |
+
if module.bias is not None:
|
997 |
+
module.bias.data.zero_()
|
998 |
+
elif isinstance(module, nn.Embedding):
|
999 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
1000 |
+
if module.padding_idx is not None:
|
1001 |
+
module.weight.data[module.padding_idx].zero_()
|
1002 |
+
|
1003 |
+
|
1004 |
+
MINICPM_INPUTS_DOCSTRING = r"""
|
1005 |
+
Args:
|
1006 |
+
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
1007 |
+
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
1008 |
+
it.
|
1009 |
+
|
1010 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
1011 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
1012 |
+
|
1013 |
+
[What are input IDs?](../glossary#input-ids)
|
1014 |
+
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
1015 |
+
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
1016 |
+
|
1017 |
+
- 1 for tokens that are **not masked**,
|
1018 |
+
- 0 for tokens that are **masked**.
|
1019 |
+
|
1020 |
+
[What are attention masks?](../glossary#attention-mask)
|
1021 |
+
|
1022 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
1023 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
1024 |
+
|
1025 |
+
If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
|
1026 |
+
`past_key_values`).
|
1027 |
+
|
1028 |
+
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
1029 |
+
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
1030 |
+
information on the default strategy.
|
1031 |
+
|
1032 |
+
- 1 indicates the head is **not masked**,
|
1033 |
+
- 0 indicates the head is **masked**.
|
1034 |
+
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
1035 |
+
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
1036 |
+
config.n_positions - 1]`.
|
1037 |
+
|
1038 |
+
[What are position IDs?](../glossary#position-ids)
|
1039 |
+
past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
|
1040 |
+
Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
1041 |
+
blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
|
1042 |
+
returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
|
1043 |
+
|
1044 |
+
Two formats are allowed:
|
1045 |
+
- a [`~cache_utils.Cache`] instance;
|
1046 |
+
- Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
|
1047 |
+
shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
|
1048 |
+
cache format.
|
1049 |
+
|
1050 |
+
The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
|
1051 |
+
legacy cache format will be returned.
|
1052 |
+
|
1053 |
+
If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
|
1054 |
+
have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
|
1055 |
+
of shape `(batch_size, sequence_length)`.
|
1056 |
+
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
1057 |
+
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
|
1058 |
+
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
|
1059 |
+
model's internal embedding lookup matrix.
|
1060 |
+
use_cache (`bool`, *optional*):
|
1061 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
|
1062 |
+
`past_key_values`).
|
1063 |
+
output_attentions (`bool`, *optional*):
|
1064 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
|
1065 |
+
tensors for more detail.
|
1066 |
+
output_hidden_states (`bool`, *optional*):
|
1067 |
+
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
|
1068 |
+
more detail.
|
1069 |
+
return_dict (`bool`, *optional*):
|
1070 |
+
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
|
1071 |
+
"""
|
1072 |
+
|
1073 |
+
|
1074 |
+
@add_start_docstrings(
|
1075 |
+
"The bare MiniCPM Model outputting raw hidden-states without any specific head on top.",
|
1076 |
+
MINICPM_START_DOCSTRING,
|
1077 |
+
)
|
1078 |
+
class MiniCPMModel(MiniCPMPreTrainedModel):
|
1079 |
+
"""
|
1080 |
+
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MiniCPMDecoderLayer`]
|
1081 |
+
|
1082 |
+
Args:
|
1083 |
+
config: MiniCPMConfig
|
1084 |
+
"""
|
1085 |
+
|
1086 |
+
def __init__(self, config: MiniCPMConfig):
|
1087 |
+
super().__init__(config)
|
1088 |
+
self.padding_idx = config.pad_token_id
|
1089 |
+
self.vocab_size = config.vocab_size
|
1090 |
+
|
1091 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
1092 |
+
self.layers = nn.ModuleList(
|
1093 |
+
[MiniCPMDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
1094 |
+
)
|
1095 |
+
self._use_sdpa = config._attn_implementation == "sdpa"
|
1096 |
+
self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
|
1097 |
+
|
1098 |
+
self.norm = MiniCPMRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
1099 |
+
|
1100 |
+
self.gradient_checkpointing = False
|
1101 |
+
# Initialize weights and apply final processing
|
1102 |
+
self.post_init()
|
1103 |
+
|
1104 |
+
def get_input_embeddings(self):
|
1105 |
+
return self.embed_tokens
|
1106 |
+
|
1107 |
+
def set_input_embeddings(self, value):
|
1108 |
+
self.embed_tokens = value
|
1109 |
+
|
1110 |
+
@add_start_docstrings_to_model_forward(MINICPM_INPUTS_DOCSTRING)
|
1111 |
+
def forward(
|
1112 |
+
self,
|
1113 |
+
input_ids: torch.LongTensor = None,
|
1114 |
+
attention_mask: Optional[torch.Tensor] = None,
|
1115 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1116 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
1117 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1118 |
+
use_cache: Optional[bool] = None,
|
1119 |
+
output_attentions: Optional[bool] = None,
|
1120 |
+
output_hidden_states: Optional[bool] = None,
|
1121 |
+
return_dict: Optional[bool] = None,
|
1122 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
1123 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
1124 |
+
output_hidden_states = (
|
1125 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
1126 |
+
)
|
1127 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
1128 |
+
|
1129 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1130 |
+
|
1131 |
+
# retrieve input_ids and inputs_embeds
|
1132 |
+
if input_ids is not None and inputs_embeds is not None:
|
1133 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
1134 |
+
elif input_ids is not None:
|
1135 |
+
batch_size, seq_length = input_ids.shape[:2]
|
1136 |
+
elif inputs_embeds is not None:
|
1137 |
+
batch_size, seq_length = inputs_embeds.shape[:2]
|
1138 |
+
else:
|
1139 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
1140 |
+
|
1141 |
+
if self.gradient_checkpointing and self.training:
|
1142 |
+
if use_cache:
|
1143 |
+
logger.warning_once(
|
1144 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
1145 |
+
)
|
1146 |
+
use_cache = False
|
1147 |
+
|
1148 |
+
past_key_values_length = 0
|
1149 |
+
if use_cache:
|
1150 |
+
use_legacy_cache = not isinstance(past_key_values, Cache)
|
1151 |
+
if use_legacy_cache:
|
1152 |
+
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
|
1153 |
+
past_key_values_length = past_key_values.get_usable_length(seq_length)
|
1154 |
+
|
1155 |
+
if position_ids is None:
|
1156 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
1157 |
+
position_ids = torch.arange(
|
1158 |
+
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
|
1159 |
+
)
|
1160 |
+
position_ids = position_ids.unsqueeze(0)
|
1161 |
+
|
1162 |
+
if inputs_embeds is None:
|
1163 |
+
inputs_embeds = self.embed_tokens(input_ids) * self.config.scale_emb
|
1164 |
+
|
1165 |
+
if self._use_flash_attention_2:
|
1166 |
+
# 2d mask is passed through the layers
|
1167 |
+
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
|
1168 |
+
elif self._use_sdpa and not output_attentions:
|
1169 |
+
# output_attentions=True can not be supported when using SDPA, and we fall back on
|
1170 |
+
# the manual implementation that requires a 4D causal mask in all cases.
|
1171 |
+
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
|
1172 |
+
attention_mask,
|
1173 |
+
(batch_size, seq_length),
|
1174 |
+
inputs_embeds,
|
1175 |
+
past_key_values_length,
|
1176 |
+
)
|
1177 |
+
else:
|
1178 |
+
# 4d mask is passed through the layers
|
1179 |
+
attention_mask = _prepare_4d_causal_attention_mask(
|
1180 |
+
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
|
1181 |
+
)
|
1182 |
+
|
1183 |
+
# embed positions
|
1184 |
+
hidden_states = inputs_embeds
|
1185 |
+
|
1186 |
+
# decoder layers
|
1187 |
+
all_hidden_states = () if output_hidden_states else None
|
1188 |
+
all_self_attns = () if output_attentions else None
|
1189 |
+
next_decoder_cache = None
|
1190 |
+
|
1191 |
+
for decoder_layer in self.layers:
|
1192 |
+
if output_hidden_states:
|
1193 |
+
all_hidden_states += (hidden_states,)
|
1194 |
+
|
1195 |
+
if self.gradient_checkpointing and self.training:
|
1196 |
+
layer_outputs = self._gradient_checkpointing_func(
|
1197 |
+
decoder_layer.__call__,
|
1198 |
+
hidden_states,
|
1199 |
+
attention_mask,
|
1200 |
+
position_ids,
|
1201 |
+
past_key_values,
|
1202 |
+
output_attentions,
|
1203 |
+
use_cache,
|
1204 |
+
)
|
1205 |
+
else:
|
1206 |
+
layer_outputs = decoder_layer(
|
1207 |
+
hidden_states,
|
1208 |
+
attention_mask=attention_mask,
|
1209 |
+
position_ids=position_ids,
|
1210 |
+
past_key_value=past_key_values,
|
1211 |
+
output_attentions=output_attentions,
|
1212 |
+
use_cache=use_cache,
|
1213 |
+
)
|
1214 |
+
|
1215 |
+
hidden_states = layer_outputs[0]
|
1216 |
+
|
1217 |
+
if use_cache:
|
1218 |
+
next_decoder_cache = layer_outputs[2 if output_attentions else 1]
|
1219 |
+
|
1220 |
+
if output_attentions:
|
1221 |
+
all_self_attns += (layer_outputs[1],)
|
1222 |
+
|
1223 |
+
hidden_states = self.norm(hidden_states)
|
1224 |
+
|
1225 |
+
# add hidden states from the last decoder layer
|
1226 |
+
if output_hidden_states:
|
1227 |
+
all_hidden_states += (hidden_states,)
|
1228 |
+
|
1229 |
+
next_cache = None
|
1230 |
+
if use_cache:
|
1231 |
+
next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
|
1232 |
+
if not return_dict:
|
1233 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
|
1234 |
+
return BaseModelOutputWithPast(
|
1235 |
+
last_hidden_state=hidden_states,
|
1236 |
+
past_key_values=next_cache,
|
1237 |
+
hidden_states=all_hidden_states,
|
1238 |
+
attentions=all_self_attns,
|
1239 |
+
)
|
1240 |
+
|
1241 |
+
|
1242 |
+
class MiniCPMForCausalLM(MiniCPMPreTrainedModel):
|
1243 |
+
_tied_weights_keys = ["lm_head.weight"]
|
1244 |
+
|
1245 |
+
def __init__(self, config):
|
1246 |
+
super().__init__(config)
|
1247 |
+
self.model = MiniCPMModel(config)
|
1248 |
+
self.vocab_size = config.vocab_size
|
1249 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
1250 |
+
|
1251 |
+
# Initialize weights and apply final processing
|
1252 |
+
self.post_init()
|
1253 |
+
|
1254 |
+
def get_input_embeddings(self):
|
1255 |
+
return self.model.embed_tokens
|
1256 |
+
|
1257 |
+
def set_input_embeddings(self, value):
|
1258 |
+
self.model.embed_tokens = value
|
1259 |
+
|
1260 |
+
def get_output_embeddings(self):
|
1261 |
+
return self.lm_head
|
1262 |
+
|
1263 |
+
def set_output_embeddings(self, new_embeddings):
|
1264 |
+
self.lm_head = new_embeddings
|
1265 |
+
|
1266 |
+
def set_decoder(self, decoder):
|
1267 |
+
self.model = decoder
|
1268 |
+
|
1269 |
+
def get_decoder(self):
|
1270 |
+
return self.model
|
1271 |
+
|
1272 |
+
@add_start_docstrings_to_model_forward(MINICPM_INPUTS_DOCSTRING)
|
1273 |
+
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
|
1274 |
+
def forward(
|
1275 |
+
self,
|
1276 |
+
input_ids: torch.LongTensor = None,
|
1277 |
+
attention_mask: Optional[torch.Tensor] = None,
|
1278 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1279 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
1280 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1281 |
+
labels: Optional[torch.LongTensor] = None,
|
1282 |
+
use_cache: Optional[bool] = None,
|
1283 |
+
output_attentions: Optional[bool] = None,
|
1284 |
+
output_hidden_states: Optional[bool] = None,
|
1285 |
+
return_dict: Optional[bool] = None,
|
1286 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
1287 |
+
r"""
|
1288 |
+
Args:
|
1289 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
1290 |
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
1291 |
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
1292 |
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
1293 |
+
|
1294 |
+
Returns:
|
1295 |
+
|
1296 |
+
Example:
|
1297 |
+
|
1298 |
+
```python
|
1299 |
+
>>> from transformers import AutoTokenizer, MiniCPMForCausalLM
|
1300 |
+
|
1301 |
+
>>> model = MiniCPMForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
1302 |
+
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
1303 |
+
|
1304 |
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
1305 |
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
1306 |
+
|
1307 |
+
>>> # Generate
|
1308 |
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
1309 |
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
1310 |
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
1311 |
+
```"""
|
1312 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
1313 |
+
output_hidden_states = (
|
1314 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
1315 |
+
)
|
1316 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1317 |
+
|
1318 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
1319 |
+
outputs = self.model(
|
1320 |
+
input_ids=input_ids,
|
1321 |
+
attention_mask=attention_mask,
|
1322 |
+
position_ids=position_ids,
|
1323 |
+
past_key_values=past_key_values,
|
1324 |
+
inputs_embeds=inputs_embeds,
|
1325 |
+
use_cache=use_cache,
|
1326 |
+
output_attentions=output_attentions,
|
1327 |
+
output_hidden_states=output_hidden_states,
|
1328 |
+
return_dict=return_dict,
|
1329 |
+
)
|
1330 |
+
|
1331 |
+
hidden_states = outputs[0]
|
1332 |
+
if self.config.pretraining_tp > 1:
|
1333 |
+
lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
|
1334 |
+
logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
|
1335 |
+
logits = torch.cat(logits, dim=-1)
|
1336 |
+
else:
|
1337 |
+
logits = self.lm_head(hidden_states / (self.config.hidden_size / self.config.dim_model_base))
|
1338 |
+
logits = logits.float()
|
1339 |
+
|
1340 |
+
loss = None
|
1341 |
+
if labels is not None:
|
1342 |
+
# Shift so that tokens < n predict n
|
1343 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
1344 |
+
shift_labels = labels[..., 1:].contiguous()
|
1345 |
+
# Flatten the tokens
|
1346 |
+
loss_fct = CrossEntropyLoss(reduction="none")
|
1347 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
1348 |
+
shift_labels = shift_labels.view(-1)
|
1349 |
+
# Enable model parallelism
|
1350 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
1351 |
+
loss = loss_fct(shift_logits, shift_labels)
|
1352 |
+
|
1353 |
+
if not return_dict:
|
1354 |
+
output = (logits,) + outputs[1:]
|
1355 |
+
return (loss,) + output if loss is not None else output
|
1356 |
+
|
1357 |
+
return CausalLMOutputWithPast(
|
1358 |
+
loss=loss,
|
1359 |
+
logits=logits,
|
1360 |
+
past_key_values=outputs.past_key_values,
|
1361 |
+
hidden_states=outputs.hidden_states,
|
1362 |
+
attentions=outputs.attentions,
|
1363 |
+
)
|
1364 |
+
|
1365 |
+
def prepare_inputs_for_generation(
|
1366 |
+
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
|
1367 |
+
):
|
1368 |
+
if past_key_values is not None:
|
1369 |
+
if isinstance(past_key_values, Cache):
|
1370 |
+
cache_length = past_key_values.get_seq_length()
|
1371 |
+
past_length = past_key_values.seen_tokens
|
1372 |
+
max_cache_length = past_key_values.get_max_length()
|
1373 |
+
else:
|
1374 |
+
cache_length = past_length = past_key_values[0][0].shape[2]
|
1375 |
+
max_cache_length = None
|
1376 |
+
|
1377 |
+
# Keep only the unprocessed tokens:
|
1378 |
+
# 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
|
1379 |
+
# some of the inputs are exclusivelly passed as part of the cache (e.g. when passing input_embeds as
|
1380 |
+
# input)
|
1381 |
+
if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
|
1382 |
+
input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
|
1383 |
+
# 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
|
1384 |
+
# input_ids based on the past_length.
|
1385 |
+
elif past_length < input_ids.shape[1]:
|
1386 |
+
input_ids = input_ids[:, past_length:]
|
1387 |
+
# 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
|
1388 |
+
|
1389 |
+
# If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
|
1390 |
+
if (
|
1391 |
+
max_cache_length is not None
|
1392 |
+
and attention_mask is not None
|
1393 |
+
and cache_length + input_ids.shape[1] > max_cache_length
|
1394 |
+
):
|
1395 |
+
attention_mask = attention_mask[:, -max_cache_length:]
|
1396 |
+
|
1397 |
+
position_ids = kwargs.get("position_ids", None)
|
1398 |
+
if attention_mask is not None and position_ids is None:
|
1399 |
+
# create position_ids on the fly for batch generation
|
1400 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
1401 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
1402 |
+
if past_key_values:
|
1403 |
+
position_ids = position_ids[:, -input_ids.shape[1] :]
|
1404 |
+
|
1405 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
1406 |
+
if inputs_embeds is not None and past_key_values is None:
|
1407 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
1408 |
+
else:
|
1409 |
+
model_inputs = {"input_ids": input_ids}
|
1410 |
+
|
1411 |
+
model_inputs.update(
|
1412 |
+
{
|
1413 |
+
"position_ids": position_ids,
|
1414 |
+
"past_key_values": past_key_values,
|
1415 |
+
"use_cache": kwargs.get("use_cache"),
|
1416 |
+
"attention_mask": attention_mask,
|
1417 |
+
}
|
1418 |
+
)
|
1419 |
+
return model_inputs
|
1420 |
+
|
1421 |
+
@staticmethod
|
1422 |
+
def _reorder_cache(past_key_values, beam_idx):
|
1423 |
+
reordered_past = ()
|
1424 |
+
for layer_past in past_key_values:
|
1425 |
+
reordered_past += (
|
1426 |
+
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
|
1427 |
+
)
|
1428 |
+
return reordered_past
|
1429 |
+
|
1430 |
+
@torch.inference_mode()
|
1431 |
+
def chat(self, tokenizer, query: str, history: List[Dict] = None, role: str = "user",
|
1432 |
+
max_length: int = 4096, num_beams=1, do_sample=True, top_p=0.8, temperature=0.3, logits_processor=None,
|
1433 |
+
**kwargs):
|
1434 |
+
if history is None:
|
1435 |
+
history = []
|
1436 |
+
if logits_processor:
|
1437 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams, "do_sample": do_sample, "top_p": top_p,
|
1438 |
+
"temperature": temperature, "logits_processor": logits_processor, **kwargs}
|
1439 |
+
else:
|
1440 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams, "do_sample": do_sample, "top_p": top_p,
|
1441 |
+
"temperature": temperature, "logits_processor": logits_processor, **kwargs}
|
1442 |
+
|
1443 |
+
history.append({"role": role, "content": query})
|
1444 |
+
history_str = tokenizer.apply_chat_template(history, tokenize=False, add_generation_prompt=True)
|
1445 |
+
inputs = tokenizer(history_str, return_tensors='pt').to(self.device)
|
1446 |
+
outputs = self.generate(**inputs, **gen_kwargs)
|
1447 |
+
outputs = outputs.tolist()[0][len(inputs["input_ids"][0]):-1]
|
1448 |
+
response = tokenizer.decode(outputs)
|
1449 |
+
history.append({"role": "assistant", "content": response})
|
1450 |
+
return response, history
|
1451 |
+
|
1452 |
+
|
1453 |
+
@add_start_docstrings(
|
1454 |
+
"""
|
1455 |
+
The MiniCPM Model transformer with a sequence classification head on top (linear layer).
|
1456 |
+
|
1457 |
+
[`MiniCPMForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
1458 |
+
(e.g. GPT-2) do.
|
1459 |
+
|
1460 |
+
Since it does classification on the last token, it requires to know the position of the last token. If a
|
1461 |
+
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
1462 |
+
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
1463 |
+
padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
|
1464 |
+
each row of the batch).
|
1465 |
+
""",
|
1466 |
+
MINICPM_START_DOCSTRING,
|
1467 |
+
)
|
1468 |
+
class MiniCPMForSequenceClassification(MiniCPMPreTrainedModel):
|
1469 |
+
def __init__(self, config):
|
1470 |
+
super().__init__(config)
|
1471 |
+
self.num_labels = config.num_labels
|
1472 |
+
self.model = MiniCPMModel(config)
|
1473 |
+
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
1474 |
+
|
1475 |
+
# Initialize weights and apply final processing
|
1476 |
+
self.post_init()
|
1477 |
+
|
1478 |
+
def get_input_embeddings(self):
|
1479 |
+
return self.model.embed_tokens
|
1480 |
+
|
1481 |
+
def set_input_embeddings(self, value):
|
1482 |
+
self.model.embed_tokens = value
|
1483 |
+
|
1484 |
+
@add_start_docstrings_to_model_forward(MINICPM_INPUTS_DOCSTRING)
|
1485 |
+
def forward(
|
1486 |
+
self,
|
1487 |
+
input_ids: torch.LongTensor = None,
|
1488 |
+
attention_mask: Optional[torch.Tensor] = None,
|
1489 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1490 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
1491 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1492 |
+
labels: Optional[torch.LongTensor] = None,
|
1493 |
+
use_cache: Optional[bool] = None,
|
1494 |
+
output_attentions: Optional[bool] = None,
|
1495 |
+
output_hidden_states: Optional[bool] = None,
|
1496 |
+
return_dict: Optional[bool] = None,
|
1497 |
+
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
1498 |
+
r"""
|
1499 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
1500 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
1501 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
1502 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
1503 |
+
"""
|
1504 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1505 |
+
|
1506 |
+
transformer_outputs = self.model(
|
1507 |
+
input_ids,
|
1508 |
+
attention_mask=attention_mask,
|
1509 |
+
position_ids=position_ids,
|
1510 |
+
past_key_values=past_key_values,
|
1511 |
+
inputs_embeds=inputs_embeds,
|
1512 |
+
use_cache=use_cache,
|
1513 |
+
output_attentions=output_attentions,
|
1514 |
+
output_hidden_states=output_hidden_states,
|
1515 |
+
return_dict=return_dict,
|
1516 |
+
)
|
1517 |
+
hidden_states = transformer_outputs[0]
|
1518 |
+
logits = self.score(hidden_states)
|
1519 |
+
|
1520 |
+
if input_ids is not None:
|
1521 |
+
batch_size = input_ids.shape[0]
|
1522 |
+
else:
|
1523 |
+
batch_size = inputs_embeds.shape[0]
|
1524 |
+
|
1525 |
+
if self.config.pad_token_id is None and batch_size != 1:
|
1526 |
+
raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
|
1527 |
+
if self.config.pad_token_id is None:
|
1528 |
+
sequence_lengths = -1
|
1529 |
+
else:
|
1530 |
+
if input_ids is not None:
|
1531 |
+
sequence_lengths = (torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1).to(
|
1532 |
+
logits.device
|
1533 |
+
)
|
1534 |
+
else:
|
1535 |
+
sequence_lengths = -1
|
1536 |
+
|
1537 |
+
pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
|
1538 |
+
|
1539 |
+
loss = None
|
1540 |
+
if labels is not None:
|
1541 |
+
labels = labels.to(logits.device)
|
1542 |
+
if self.config.problem_type is None:
|
1543 |
+
if self.num_labels == 1:
|
1544 |
+
self.config.problem_type = "regression"
|
1545 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
1546 |
+
self.config.problem_type = "single_label_classification"
|
1547 |
+
else:
|
1548 |
+
self.config.problem_type = "multi_label_classification"
|
1549 |
+
|
1550 |
+
if self.config.problem_type == "regression":
|
1551 |
+
loss_fct = MSELoss()
|
1552 |
+
if self.num_labels == 1:
|
1553 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
1554 |
+
else:
|
1555 |
+
loss = loss_fct(pooled_logits, labels)
|
1556 |
+
elif self.config.problem_type == "single_label_classification":
|
1557 |
+
loss_fct = CrossEntropyLoss()
|
1558 |
+
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
1559 |
+
elif self.config.problem_type == "multi_label_classification":
|
1560 |
+
loss_fct = BCEWithLogitsLoss()
|
1561 |
+
loss = loss_fct(pooled_logits, labels)
|
1562 |
+
if not return_dict:
|
1563 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
1564 |
+
return ((loss,) + output) if loss is not None else output
|
1565 |
+
|
1566 |
+
return SequenceClassifierOutputWithPast(
|
1567 |
+
loss=loss,
|
1568 |
+
logits=pooled_logits,
|
1569 |
+
past_key_values=transformer_outputs.past_key_values,
|
1570 |
+
hidden_states=transformer_outputs.hidden_states,
|
1571 |
+
attentions=transformer_outputs.attentions,
|
1572 |
+
)
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f4c69dfe493d3b644071e1b3820d68f49bcd6674cd40e71b9e451b8534586613
|
3 |
+
size 8148010378
|
special_tokens_map.json
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"additional_special_tokens": [
|
3 |
+
{
|
4 |
+
"content": "<|im_end|>",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false
|
9 |
+
},
|
10 |
+
{
|
11 |
+
"content": "<|im_start|>",
|
12 |
+
"lstrip": false,
|
13 |
+
"normalized": false,
|
14 |
+
"rstrip": false,
|
15 |
+
"single_word": false
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"content": "<|tool_call|>",
|
19 |
+
"lstrip": false,
|
20 |
+
"normalized": false,
|
21 |
+
"rstrip": false,
|
22 |
+
"single_word": false
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"content": "<|execute_start|>",
|
26 |
+
"lstrip": false,
|
27 |
+
"normalized": false,
|
28 |
+
"rstrip": false,
|
29 |
+
"single_word": false
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"content": "<|execute_end|>",
|
33 |
+
"lstrip": false,
|
34 |
+
"normalized": false,
|
35 |
+
"rstrip": false,
|
36 |
+
"single_word": false
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"content": "<|fim_prefix|>",
|
40 |
+
"lstrip": false,
|
41 |
+
"normalized": false,
|
42 |
+
"rstrip": false,
|
43 |
+
"single_word": false
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"content": "<|fim_middle|>",
|
47 |
+
"lstrip": false,
|
48 |
+
"normalized": false,
|
49 |
+
"rstrip": false,
|
50 |
+
"single_word": false
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"content": "<|fim_suffix|>",
|
54 |
+
"lstrip": false,
|
55 |
+
"normalized": false,
|
56 |
+
"rstrip": false,
|
57 |
+
"single_word": false
|
58 |
+
}
|
59 |
+
],
|
60 |
+
"bos_token": {
|
61 |
+
"content": "<s>",
|
62 |
+
"lstrip": false,
|
63 |
+
"normalized": false,
|
64 |
+
"rstrip": false,
|
65 |
+
"single_word": false
|
66 |
+
},
|
67 |
+
"eos_token": {
|
68 |
+
"content": "</s>",
|
69 |
+
"lstrip": false,
|
70 |
+
"normalized": false,
|
71 |
+
"rstrip": false,
|
72 |
+
"single_word": false
|
73 |
+
},
|
74 |
+
"unk_token": {
|
75 |
+
"content": "<unk>",
|
76 |
+
"lstrip": false,
|
77 |
+
"normalized": false,
|
78 |
+
"rstrip": false,
|
79 |
+
"single_word": false
|
80 |
+
}
|
81 |
+
}
|
tokenization_minicpm.py
ADDED
@@ -0,0 +1,430 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
import json
|
3 |
+
import keyword
|
4 |
+
import traceback
|
5 |
+
import uuid
|
6 |
+
from collections import deque
|
7 |
+
from logging import getLogger
|
8 |
+
from typing import Any, Dict, List, Optional, Union
|
9 |
+
|
10 |
+
from datamodel_code_generator import DataModelType
|
11 |
+
from datamodel_code_generator.format import PythonVersion
|
12 |
+
from datamodel_code_generator.model import get_data_model_types
|
13 |
+
from datamodel_code_generator.parser.jsonschema import JsonSchemaParser
|
14 |
+
from jsonschema import Draft202012Validator, exceptions, validate
|
15 |
+
|
16 |
+
from transformers import LlamaTokenizer
|
17 |
+
from transformers.tokenization_utils_base import BatchEncoding
|
18 |
+
from transformers.utils import TensorType
|
19 |
+
|
20 |
+
|
21 |
+
logger = getLogger(__name__)
|
22 |
+
|
23 |
+
|
24 |
+
class MiniCPMTokenizer(LlamaTokenizer):
|
25 |
+
def apply_chat_template(
|
26 |
+
self,
|
27 |
+
conversation: Union[List[Dict[str, str]], List[List[Dict[str, str]]]],
|
28 |
+
tools: Optional[List[Dict]] = None,
|
29 |
+
documents: Optional[List[Dict[str, str]]] = None,
|
30 |
+
chat_template: Optional[str] = None,
|
31 |
+
add_generation_prompt: bool = False,
|
32 |
+
tokenize: bool = True,
|
33 |
+
padding: bool = False,
|
34 |
+
truncation: bool = False,
|
35 |
+
max_length: Optional[int] = None,
|
36 |
+
return_tensors: Optional[Union[str, TensorType]] = None,
|
37 |
+
return_dict: bool = False,
|
38 |
+
return_assistant_tokens_mask: bool = False,
|
39 |
+
tokenizer_kwargs: Optional[Dict[str, Any]] = None,
|
40 |
+
**kwargs,
|
41 |
+
) -> Union[str, List[int], List[str], List[List[int]], BatchEncoding]:
|
42 |
+
if tools is None:
|
43 |
+
tools = []
|
44 |
+
check_messages(conversation, tools)
|
45 |
+
functions = [tool["function"] for tool in tools]
|
46 |
+
conversation = self.reorder_tool_response(conversation)
|
47 |
+
input_messages = input_format(conversation, functions, add_to_system=True)
|
48 |
+
return super().apply_chat_template(
|
49 |
+
input_messages,
|
50 |
+
tools=None,
|
51 |
+
documents=documents,
|
52 |
+
chat_template=chat_template,
|
53 |
+
add_generation_prompt=add_generation_prompt,
|
54 |
+
tokenize=tokenize,
|
55 |
+
padding=padding,
|
56 |
+
truncation=truncation,
|
57 |
+
max_length=max_length,
|
58 |
+
return_tensors=return_tensors,
|
59 |
+
return_dict=return_dict,
|
60 |
+
return_assistant_tokens_mask=return_assistant_tokens_mask,
|
61 |
+
tokenizer_kwargs=tokenizer_kwargs,
|
62 |
+
**kwargs,
|
63 |
+
)
|
64 |
+
|
65 |
+
def reorder_tool_response(self, conversation: List[Dict[str, str]]):
|
66 |
+
tool_call_ids = deque()
|
67 |
+
tool_responses = deque()
|
68 |
+
|
69 |
+
new_conversation = []
|
70 |
+
for message in conversation:
|
71 |
+
if (
|
72 |
+
message["role"] == "assistant"
|
73 |
+
and "tool_calls" in message
|
74 |
+
and message["tool_calls"] is not None
|
75 |
+
and len(message["tool_calls"]) > 0
|
76 |
+
):
|
77 |
+
for tool_call in message["tool_calls"]:
|
78 |
+
tool_call_ids.append(tool_call["id"])
|
79 |
+
new_conversation.append(message)
|
80 |
+
elif message["role"] == "tool":
|
81 |
+
tool_call_id = message.get("tool_call_id", None)
|
82 |
+
if tool_call_id == tool_call_ids[0]:
|
83 |
+
new_conversation.append(message)
|
84 |
+
tool_call_ids.popleft()
|
85 |
+
while (
|
86 |
+
len(tool_call_ids) > 0
|
87 |
+
and len(tool_responses) > 0
|
88 |
+
and tool_call_ids[0] == tool_responses[0]["tool_call_id"]
|
89 |
+
):
|
90 |
+
new_conversation.append(tool_responses.popleft())
|
91 |
+
tool_call_ids.popleft()
|
92 |
+
else:
|
93 |
+
tool_responses.append(message)
|
94 |
+
else:
|
95 |
+
new_conversation.append(message)
|
96 |
+
if len(tool_call_ids) != 0:
|
97 |
+
raise ValueError(f"Message error, not all tool calls have responses: {tool_call_ids}")
|
98 |
+
if len(tool_responses) != 0:
|
99 |
+
raise ValueError(f"Message error, too many tool responses: {tool_responses}")
|
100 |
+
return new_conversation
|
101 |
+
|
102 |
+
def decode_function_call(
|
103 |
+
self,
|
104 |
+
sequence: str,
|
105 |
+
tool_call_start="<|tool_call_start|>",
|
106 |
+
tool_call_end="<|tool_call_end|>",
|
107 |
+
thought_start="<|thought_start|>",
|
108 |
+
thought_end="<|thought_end|>",
|
109 |
+
):
|
110 |
+
if thought_end in sequence and thought_start in sequence:
|
111 |
+
thought_string, sequence = sequence.rsplit(thought_end, 1)
|
112 |
+
thought_string = thought_string.split(thought_start, 1)[1]
|
113 |
+
else:
|
114 |
+
thought_string = ""
|
115 |
+
if tool_call_start in sequence and tool_call_end in sequence:
|
116 |
+
tool_call_string, content = sequence.rsplit(tool_call_end, 1)
|
117 |
+
tool_call_string = tool_call_string.split(tool_call_start, 1)[1]
|
118 |
+
try:
|
119 |
+
tool_calls = []
|
120 |
+
tool_call_string = tool_call_string.strip()
|
121 |
+
if tool_call_string.startswith("```"):
|
122 |
+
tool_call_string = tool_call_string.lstrip("```").strip()
|
123 |
+
if tool_call_string.startswith("python"):
|
124 |
+
tool_call_string = tool_call_string.lstrip("python").strip()
|
125 |
+
if tool_call_string.endswith("```"):
|
126 |
+
tool_call_string = tool_call_string.rstrip("```").strip()
|
127 |
+
for kw in keyword.kwlist:
|
128 |
+
tool_call_string = tool_call_string.replace("," + kw + "=", "," + kw + "_=")
|
129 |
+
tool_call_string = tool_call_string.replace(" " + kw + "=", " " + kw + "_=")
|
130 |
+
tool_call_string = tool_call_string.replace("(" + kw + "=", "(" + kw + "_=")
|
131 |
+
|
132 |
+
parsed = ast.parse(tool_call_string)
|
133 |
+
|
134 |
+
for elem in parsed.body:
|
135 |
+
assert isinstance(elem.value, ast.Call)
|
136 |
+
calls = resolve_ast_call(elem.value)
|
137 |
+
|
138 |
+
for func_name, func_args in calls.items():
|
139 |
+
new_args = {}
|
140 |
+
for k, v in func_args.items():
|
141 |
+
for kw in keyword.kwlist:
|
142 |
+
if k == kw + "_":
|
143 |
+
k = kw
|
144 |
+
new_args[k] = v
|
145 |
+
|
146 |
+
this_one = {"name": func_name, "arguments": new_args}
|
147 |
+
tool_calls.append(this_one)
|
148 |
+
|
149 |
+
return {
|
150 |
+
"content": content.strip(),
|
151 |
+
"tool_calls": [
|
152 |
+
{"type": "function", "function": tool_call, "id": "call_" + uuid.uuid4().hex}
|
153 |
+
for tool_call in tool_calls
|
154 |
+
],
|
155 |
+
"role": "assistant",
|
156 |
+
}
|
157 |
+
except:
|
158 |
+
logger.error(traceback.format_exc())
|
159 |
+
return {
|
160 |
+
"content": content.strip(),
|
161 |
+
"role": "assistant",
|
162 |
+
"thought": thought_string,
|
163 |
+
}
|
164 |
+
else:
|
165 |
+
return {
|
166 |
+
"content": sequence.strip(),
|
167 |
+
"role": "assistant",
|
168 |
+
"thought": thought_string,
|
169 |
+
}
|
170 |
+
|
171 |
+
|
172 |
+
def check_messages(conversation: List[Dict[str, str]], tools: List[Dict]):
|
173 |
+
if tools is not None:
|
174 |
+
for tool in tools:
|
175 |
+
if "type" not in tool or tool["type"] != "function":
|
176 |
+
raise ValueError(f"Tool {tool} is not valid")
|
177 |
+
if "name" not in tool["function"]:
|
178 |
+
raise ValueError(f"Tool {tool} is not valid")
|
179 |
+
if "parameters" not in tool["function"] or not check_tool(tool["function"]["parameters"]["properties"]):
|
180 |
+
raise ValueError(f"Tool {tool} is not valid")
|
181 |
+
for message in conversation:
|
182 |
+
if message["role"] == "assistant" and "tool_calls" in message and len(message["tool_calls"]) > 0:
|
183 |
+
for tool_call in message["tool_calls"]:
|
184 |
+
if "id" not in tool_call:
|
185 |
+
raise ValueError(f"Tool call {tool_call} is not valid")
|
186 |
+
if tool_call["type"] != "function":
|
187 |
+
raise ValueError(f"Tool call {tool_call} is not valid")
|
188 |
+
if "function" not in tool_call:
|
189 |
+
raise ValueError(f"Tool call {tool_call} is not valid")
|
190 |
+
if not check_tool(tool_call["function"]):
|
191 |
+
raise ValueError(f"Tool call function {tool_call['function']} is not valid")
|
192 |
+
elif message["role"] == "tool":
|
193 |
+
if "tool_call_id" not in message:
|
194 |
+
raise ValueError(f"Tool message {message['content']} is not valid")
|
195 |
+
|
196 |
+
|
197 |
+
def check_tool(tool_schema):
|
198 |
+
try:
|
199 |
+
Draft202012Validator.check_schema(tool_schema)
|
200 |
+
return True
|
201 |
+
except exceptions.SchemaError as e:
|
202 |
+
print(f"SchemaError: {e}")
|
203 |
+
return False
|
204 |
+
|
205 |
+
|
206 |
+
def check_args(args, tool_schema):
|
207 |
+
try:
|
208 |
+
validate(instance=args, schema=tool_schema)
|
209 |
+
return True
|
210 |
+
except exceptions.ValidationError as e:
|
211 |
+
print(f"Data failed validation: {e}")
|
212 |
+
return False
|
213 |
+
|
214 |
+
|
215 |
+
def message_format(msg, system_suffix="", user_prefix=""):
|
216 |
+
if "thought" in msg and msg["thought"] is not None and len(msg["thought"]) > 0:
|
217 |
+
thought_prefix = f"<|thought_start|>\n{msg['thought']}\n<|thought_end|>\n"
|
218 |
+
else:
|
219 |
+
thought_prefix = ""
|
220 |
+
if msg["role"] == "assistant":
|
221 |
+
content = msg.get("content", "")
|
222 |
+
if content is None:
|
223 |
+
content = ""
|
224 |
+
if "tool_calls" in msg and msg["tool_calls"] is not None and len(msg["tool_calls"]) > 0:
|
225 |
+
|
226 |
+
def add_quotes(variable):
|
227 |
+
if isinstance(variable, str):
|
228 |
+
return repr(variable)
|
229 |
+
else:
|
230 |
+
return str(variable)
|
231 |
+
|
232 |
+
tool_calls = []
|
233 |
+
for _tool_call in msg["tool_calls"]:
|
234 |
+
if _tool_call is None:
|
235 |
+
continue
|
236 |
+
tool_call = _tool_call["function"]
|
237 |
+
tool_name = tool_call["name"]
|
238 |
+
if "arguments" not in tool_call or tool_call["arguments"] is None:
|
239 |
+
continue
|
240 |
+
if isinstance(tool_call["arguments"], str):
|
241 |
+
try:
|
242 |
+
tool_call["arguments"] = json.loads(tool_call["arguments"])
|
243 |
+
except:
|
244 |
+
continue
|
245 |
+
args = ",".join([k + "=" + add_quotes(v) for k, v in tool_call["arguments"].items()])
|
246 |
+
tool_calls.append(f"{tool_name}({args})")
|
247 |
+
|
248 |
+
content = (
|
249 |
+
thought_prefix
|
250 |
+
+ "<|tool_call_start|>\n```python\n"
|
251 |
+
+ "\n".join(tool_calls).strip()
|
252 |
+
+ "\n```\n<|tool_call_end|>\n"
|
253 |
+
+ content
|
254 |
+
)
|
255 |
+
# msg["tool_call_string"] = "\n".join(tool_calls).strip()
|
256 |
+
msg["content"] = content
|
257 |
+
else:
|
258 |
+
content = thought_prefix + content
|
259 |
+
msg["content"] = content
|
260 |
+
elif msg["role"] == "user":
|
261 |
+
msg["content"] = user_prefix + "\n" + msg["content"]
|
262 |
+
elif msg["role"] == "system":
|
263 |
+
msg["content"] = msg["content"] + "\n" + system_suffix
|
264 |
+
msg["content"] = msg["content"].strip()
|
265 |
+
return msg
|
266 |
+
|
267 |
+
|
268 |
+
def jsonschema_to_code(jsonschema: dict) -> str:
|
269 |
+
input_text = json.dumps(jsonschema)
|
270 |
+
data_model_types = get_data_model_types(
|
271 |
+
DataModelType.PydanticBaseModel,
|
272 |
+
PythonVersion.PY_310,
|
273 |
+
)
|
274 |
+
parser = JsonSchemaParser(
|
275 |
+
source=input_text,
|
276 |
+
data_model_type=data_model_types.data_model,
|
277 |
+
data_model_root_type=data_model_types.root_model,
|
278 |
+
data_model_field_type=data_model_types.field_model,
|
279 |
+
data_type_manager_type=data_model_types.data_type_manager,
|
280 |
+
target_python_version=PythonVersion.PY_310,
|
281 |
+
dump_resolve_reference_action=data_model_types.dump_resolve_reference_action,
|
282 |
+
field_constraints=True,
|
283 |
+
)
|
284 |
+
results = parser.parse()
|
285 |
+
return results
|
286 |
+
|
287 |
+
|
288 |
+
def transform_function(function: dict):
|
289 |
+
"""turn json format of function into signature"""
|
290 |
+
params, default_params = [], []
|
291 |
+
for prop_name, prop in function["parameters"]["properties"].items():
|
292 |
+
if "default" in prop:
|
293 |
+
default_params.append(f'{prop_name}={repr(prop["default"])}')
|
294 |
+
elif prop_name not in function["parameters"].get("required", []):
|
295 |
+
default_params.append(f"{prop_name}={repr(None)}")
|
296 |
+
else:
|
297 |
+
params.append(prop_name)
|
298 |
+
ps = ", ".join(params + default_params)
|
299 |
+
res = "def {f_name}({ps}):\n".format(f_name=function["name"], ps=ps)
|
300 |
+
f_des = function.get("description", "")
|
301 |
+
content = jsonschema_to_code(function["parameters"])
|
302 |
+
if "class" in content:
|
303 |
+
i = content.index("class")
|
304 |
+
# print(content[:i])
|
305 |
+
content = content[i:]
|
306 |
+
classes, args = content.split("class Model(BaseModel):", 1)
|
307 |
+
lint_msg = f' """\n {f_des}\n Args:\n{args}\n """\n'
|
308 |
+
res += lint_msg
|
309 |
+
if len(classes) > 0:
|
310 |
+
res = classes + res
|
311 |
+
return res
|
312 |
+
|
313 |
+
|
314 |
+
def input_format(messages: List[Dict], tools: List[Dict], add_to_system=True):
|
315 |
+
"""
|
316 |
+
Process the input messages, global_arguments, tools, tool_choice,
|
317 |
+
and convert it into a input string.
|
318 |
+
The global arguments and tools can not be both empty.
|
319 |
+
parameters:
|
320 |
+
messages: List[Dict]
|
321 |
+
the input messages
|
322 |
+
For example:
|
323 |
+
tools: List[Dict]
|
324 |
+
the tools list you can use
|
325 |
+
For example:
|
326 |
+
"""
|
327 |
+
if tools is not None and len(tools) > 0:
|
328 |
+
header = (
|
329 |
+
"from enum import Enum\nfrom typing import List, Dict, Optional\nfrom pydantic import BaseModel, Field\n\n"
|
330 |
+
)
|
331 |
+
tools_string = header
|
332 |
+
for tool in tools:
|
333 |
+
try:
|
334 |
+
tools_string += "\n\n" + transform_function(tool)
|
335 |
+
except:
|
336 |
+
pass
|
337 |
+
tools_template = """# Functions
|
338 |
+
Here is a list of functions that you can invoke:
|
339 |
+
```python
|
340 |
+
{tools}
|
341 |
+
```
|
342 |
+
|
343 |
+
# Function Call Rule and Output Format
|
344 |
+
- If the user's question can be answered without calling any function, please answer the user's question directly. In this situation, you should return your thought and answer the user's question directly.
|
345 |
+
- If the user cannot be answered without calling any function, and the user does not provide enough information to call functions, please ask the user for more information. In this situation, you should return your thought and ask the user for more information.
|
346 |
+
- If the user's question cannot be answered without calling any function, and the user has provided enough information to call functions to solve it, you should call the functions. In this situation, the assistant should return your thought and call the functions.
|
347 |
+
- Use default parameters unless the user has specified otherwise.
|
348 |
+
- You should answer in the following format:
|
349 |
+
|
350 |
+
<|thought_start|>
|
351 |
+
{{explain why the user's question can be answered without calling a function or why you should ask the user for more information or why you should call one or more functions and your plan to solve the user's question.}}
|
352 |
+
<|thought_end|>
|
353 |
+
<|tool_call_start|>
|
354 |
+
```python
|
355 |
+
func1(params_name=params_value, params_name2=params_value2...)
|
356 |
+
func2(params)
|
357 |
+
```
|
358 |
+
<|tool_call_end|>
|
359 |
+
{{answer the user's question directly or ask the user for more information}}
|
360 |
+
"""
|
361 |
+
tools_string = tools_template.format(tools=tools_string).strip()
|
362 |
+
else:
|
363 |
+
tools_string = ""
|
364 |
+
|
365 |
+
if add_to_system:
|
366 |
+
if len(messages) > 0 and messages[0]["role"] != "system":
|
367 |
+
messages.insert(0, {"role": "system", "content": ""})
|
368 |
+
return [message_format(msg, system_suffix=tools_string, user_prefix="") for msg in messages]
|
369 |
+
else:
|
370 |
+
return [message_format(msg, system_suffix="", user_prefix=tools_string) for msg in messages]
|
371 |
+
|
372 |
+
|
373 |
+
# This is a modified version of
|
374 |
+
# https://github.com/ShishirPatil/gorilla/blob/main/berkeley-function-call-leaderboard/bfcl/model_handler/utils.py
|
375 |
+
# Thanks to the gorilla team for the original implementation
|
376 |
+
def resolve_ast_call(elem):
|
377 |
+
# Handle nested attributes for deeply nested module paths
|
378 |
+
func_parts = []
|
379 |
+
func_part = elem.func
|
380 |
+
while isinstance(func_part, ast.Attribute):
|
381 |
+
func_parts.append(func_part.attr)
|
382 |
+
func_part = func_part.value
|
383 |
+
if isinstance(func_part, ast.Name):
|
384 |
+
func_parts.append(func_part.id)
|
385 |
+
func_name = ".".join(reversed(func_parts))
|
386 |
+
args_dict = {}
|
387 |
+
for arg in elem.keywords:
|
388 |
+
output = resolve_ast_by_type(arg.value)
|
389 |
+
args_dict[arg.arg] = output
|
390 |
+
return {func_name: args_dict}
|
391 |
+
|
392 |
+
|
393 |
+
def resolve_ast_by_type(value):
|
394 |
+
if isinstance(value, ast.Constant):
|
395 |
+
if value.value is Ellipsis:
|
396 |
+
output = "..."
|
397 |
+
else:
|
398 |
+
output = value.value
|
399 |
+
elif isinstance(value, ast.UnaryOp):
|
400 |
+
output = -value.operand.value
|
401 |
+
elif isinstance(value, ast.List):
|
402 |
+
output = [resolve_ast_by_type(v) for v in value.elts]
|
403 |
+
elif isinstance(value, ast.Dict):
|
404 |
+
output = {resolve_ast_by_type(k): resolve_ast_by_type(v) for k, v in zip(value.keys, value.values)}
|
405 |
+
elif isinstance(value, ast.NameConstant): # Added this condition to handle boolean values
|
406 |
+
output = value.value
|
407 |
+
elif isinstance(value, ast.BinOp): # Added this condition to handle function calls as arguments
|
408 |
+
output = eval(ast.unparse(value))
|
409 |
+
elif isinstance(value, ast.Name):
|
410 |
+
output = value.id
|
411 |
+
elif isinstance(value, ast.Call):
|
412 |
+
if len(value.keywords) == 0:
|
413 |
+
output = ast.unparse(value)
|
414 |
+
else:
|
415 |
+
output = resolve_ast_call(value)
|
416 |
+
elif isinstance(value, ast.Tuple):
|
417 |
+
output = tuple(resolve_ast_by_type(v) for v in value.elts)
|
418 |
+
elif isinstance(value, ast.Lambda):
|
419 |
+
output = eval(ast.unparse(value.body[0].value))
|
420 |
+
elif isinstance(value, ast.Ellipsis):
|
421 |
+
output = "..."
|
422 |
+
elif isinstance(value, ast.Subscript):
|
423 |
+
try:
|
424 |
+
output = ast.unparse(value.body[0].value)
|
425 |
+
except:
|
426 |
+
output = ast.unparse(value.value) + "[" + ast.unparse(value.slice) + "]"
|
427 |
+
else:
|
428 |
+
raise Exception(f"Unsupported AST type: {type(value)}")
|
429 |
+
return output
|
430 |
+
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer.model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bb74d51116831c3bf65db812c553f94ab0c88dcf97a5bbb37e3504f6d359c530
|
3 |
+
size 1181204
|
tokenizer_config.json
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"auto_map": {
|
3 |
+
"AutoTokenizer": ["tokenization_minicpm.MiniCPMTokenizer", null]
|
4 |
+
},
|
5 |
+
"add_bos_token": true,
|
6 |
+
"add_eos_token": false,
|
7 |
+
"added_tokens_decoder": {
|
8 |
+
"0": {
|
9 |
+
"content": "<unk>",
|
10 |
+
"lstrip": false,
|
11 |
+
"normalized": false,
|
12 |
+
"rstrip": false,
|
13 |
+
"single_word": false,
|
14 |
+
"special": true
|
15 |
+
},
|
16 |
+
"1": {
|
17 |
+
"content": "<s>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false,
|
22 |
+
"special": true
|
23 |
+
},
|
24 |
+
"2": {
|
25 |
+
"content": "</s>",
|
26 |
+
"lstrip": false,
|
27 |
+
"normalized": false,
|
28 |
+
"rstrip": false,
|
29 |
+
"single_word": false,
|
30 |
+
"special": true
|
31 |
+
},
|
32 |
+
"73440": {
|
33 |
+
"content": "<|im_end|>",
|
34 |
+
"lstrip": false,
|
35 |
+
"normalized": false,
|
36 |
+
"rstrip": false,
|
37 |
+
"single_word": false,
|
38 |
+
"special": true
|
39 |
+
},
|
40 |
+
"73441": {
|
41 |
+
"content": "<|im_start|>",
|
42 |
+
"lstrip": false,
|
43 |
+
"normalized": false,
|
44 |
+
"rstrip": false,
|
45 |
+
"single_word": false,
|
46 |
+
"special": true
|
47 |
+
},
|
48 |
+
"73442": {
|
49 |
+
"content": "<|tool_call|>",
|
50 |
+
"lstrip": false,
|
51 |
+
"normalized": false,
|
52 |
+
"rstrip": false,
|
53 |
+
"single_word": false,
|
54 |
+
"special": true
|
55 |
+
},
|
56 |
+
"73443": {
|
57 |
+
"content": "<|execute_start|>",
|
58 |
+
"lstrip": false,
|
59 |
+
"normalized": false,
|
60 |
+
"rstrip": false,
|
61 |
+
"single_word": false,
|
62 |
+
"special": true
|
63 |
+
},
|
64 |
+
"73444": {
|
65 |
+
"content": "<|execute_end|>",
|
66 |
+
"lstrip": false,
|
67 |
+
"normalized": false,
|
68 |
+
"rstrip": false,
|
69 |
+
"single_word": false,
|
70 |
+
"special": true
|
71 |
+
},
|
72 |
+
"73445": {
|
73 |
+
"content": "<|fim_prefix|>",
|
74 |
+
"lstrip": false,
|
75 |
+
"normalized": false,
|
76 |
+
"rstrip": false,
|
77 |
+
"single_word": false,
|
78 |
+
"special": true
|
79 |
+
},
|
80 |
+
"73446": {
|
81 |
+
"content": "<|fim_middle|>",
|
82 |
+
"lstrip": false,
|
83 |
+
"normalized": false,
|
84 |
+
"rstrip": false,
|
85 |
+
"single_word": false,
|
86 |
+
"special": true
|
87 |
+
},
|
88 |
+
"73447": {
|
89 |
+
"content": "<|fim_suffix|>",
|
90 |
+
"lstrip": false,
|
91 |
+
"normalized": false,
|
92 |
+
"rstrip": false,
|
93 |
+
"single_word": false,
|
94 |
+
"special": true
|
95 |
+
}
|
96 |
+
},
|
97 |
+
"additional_special_tokens": [
|
98 |
+
"<|im_end|>",
|
99 |
+
"<|im_start|>",
|
100 |
+
"<|tool_call|>",
|
101 |
+
"<|execute_start|>",
|
102 |
+
"<|execute_end|>",
|
103 |
+
"<|fim_prefix|>",
|
104 |
+
"<|fim_middle|>",
|
105 |
+
"<|fim_suffix|>"
|
106 |
+
],
|
107 |
+
"bos_token": "<s>",
|
108 |
+
"clean_up_tokenization_spaces": false,
|
109 |
+
"eos_token": "<|im_end|>",
|
110 |
+
"legacy": true,
|
111 |
+
"model_max_length": 1000000000000000019884624838656,
|
112 |
+
"pad_token": null,
|
113 |
+
"sp_model_kwargs": {},
|
114 |
+
"spaces_between_special_tokens": false,
|
115 |
+
"tokenizer_class": "MiniCPMTokenizer",
|
116 |
+
"unk_token": "<unk>",
|
117 |
+
"use_default_system_prompt": false,
|
118 |
+
"chat_template": "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
|
119 |
+
}
|