katuni4ka commited on
Commit
d0a71ce
1 Parent(s): 0b8c35a

Upload 10 files

Browse files
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,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/home/ea/work/my_optimum_intel/optimum-intel/minicpm3",
3
+ "architectures": [
4
+ "MiniCPM3ForCausalLM"
5
+ ],
6
+ "attention_bias": false,
7
+ "attention_dropout": 0.0,
8
+ "auto_map": {
9
+ "AutoConfig": "configuration_minicpm.MiniCPM3Config",
10
+ "AutoModel": "modeling_minicpm.MiniCPM3Model",
11
+ "AutoModelForCausalLM": "modeling_minicpm.MiniCPM3ForCausalLM",
12
+ "AutoModelForSeq2SeqLM": "modeling_minicpm.MiniCPM3ForCausalLM",
13
+ "AutoModelForSequenceClassification": "modeling_minicpm.MiniCPM3ForSequenceClassification"
14
+ },
15
+ "bos_token_id": 1,
16
+ "dim_model_base": 32,
17
+ "eos_token_id": [
18
+ 2,
19
+ 73440
20
+ ],
21
+ "head_dim": 96,
22
+ "hidden_act": "silu",
23
+ "hidden_size": 256,
24
+ "initializer_range": 0.1,
25
+ "intermediate_size": 64,
26
+ "kv_lora_rank": 4,
27
+ "max_position_embeddings": 32768,
28
+ "model_type": "minicpm3",
29
+ "num_attention_heads": 4,
30
+ "num_hidden_layers": 6,
31
+ "num_key_value_heads": 4,
32
+ "pretraining_tp": 1,
33
+ "q_lora_rank": 64,
34
+ "qk_nope_head_dim": 64,
35
+ "qk_rope_head_dim": 32,
36
+ "rms_norm_eps": 1e-05,
37
+ "rope_scaling": {
38
+ "long_factor": [
39
+ 1.0591234137867171,
40
+ 1.1241891283591912,
41
+ 1.2596935748670968,
42
+ 1.5380380402321725,
43
+ 2.093982484148734,
44
+ 3.1446935121267696,
45
+ 4.937952647693647,
46
+ 7.524541999994549,
47
+ 10.475458000005451,
48
+ 13.062047352306353,
49
+ 14.85530648787323,
50
+ 15.906017515851266,
51
+ 16.461961959767827,
52
+ 16.740306425132907,
53
+ 16.87581087164081,
54
+ 16.940876586213285
55
+ ],
56
+ "original_max_position_embeddings": 32768,
57
+ "short_factor": [
58
+ 1.0591234137867171,
59
+ 1.1241891283591912,
60
+ 1.2596935748670968,
61
+ 1.5380380402321725,
62
+ 2.093982484148734,
63
+ 3.1446935121267696,
64
+ 4.937952647693647,
65
+ 7.524541999994549,
66
+ 10.475458000005451,
67
+ 13.062047352306353,
68
+ 14.85530648787323,
69
+ 15.906017515851266,
70
+ 16.461961959767827,
71
+ 16.740306425132907,
72
+ 16.87581087164081,
73
+ 16.940876586213285
74
+ ],
75
+ "type": "longrope"
76
+ },
77
+ "rope_theta": 10000.0,
78
+ "scale_depth": 1.4,
79
+ "scale_emb": 12,
80
+ "torch_dtype": "float32",
81
+ "transformers_version": "4.46.2",
82
+ "use_cache": true,
83
+ "v_head_dim": 8,
84
+ "vocab_size": 73448
85
+ }
configuration_minicpm.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 MiniCPM3Config(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.attention_bias = attention_bias
178
+ self.attention_dropout = attention_dropout
179
+ self.scale_emb = scale_emb
180
+ self.dim_model_base = dim_model_base
181
+ self.scale_depth = scale_depth
182
+ self.head_dim = self.qk_nope_head_dim + self.qk_rope_head_dim
183
+
184
+ super().__init__(
185
+ pad_token_id=pad_token_id,
186
+ bos_token_id=bos_token_id,
187
+ eos_token_id=eos_token_id,
188
+ tie_word_embeddings=tie_word_embeddings,
189
+ **kwargs,
190
+ )
191
+ try:
192
+ import flash_attn
193
+ self._attn_implementation = "flash_attention_2"
194
+ except:
195
+ pass
generation_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": [
5
+ 2,
6
+ 73440
7
+ ],
8
+ "transformers_version": "4.46.2"
9
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c978da8f2b3518321ea2e3e679accce1861ead2dcdae66e91fdd442cbaa1782f
3
+ size 79239744
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 MiniCPM3Config
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 = "MiniCPM3Config"
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: MiniCPM3Config, 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
+ "MiniCPM3Model 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: MiniCPM3Config, 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 ([`MiniCPM3Config`]):
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 MiniCPM3PreTrainedModel(PreTrainedModel):
983
+ config_class = MiniCPM3Config
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 MiniCPM3Model(MiniCPM3PreTrainedModel):
1079
+ """
1080
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MiniCPMDecoderLayer`]
1081
+
1082
+ Args:
1083
+ config: MiniCPM3Config
1084
+ """
1085
+
1086
+ def __init__(self, config: MiniCPM3Config):
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 MiniCPM3ForCausalLM(MiniCPM3PreTrainedModel):
1243
+ _tied_weights_keys = ["lm_head.weight"]
1244
+
1245
+ def __init__(self, config):
1246
+ super().__init__(config)
1247
+ self.model = MiniCPM3Model(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()
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 MiniCPM3ForSequenceClassification(MiniCPM3PreTrainedModel):
1469
+ def __init__(self, config):
1470
+ super().__init__(config)
1471
+ self.num_labels = config.num_labels
1472
+ self.model = MiniCPM3Model(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
+ )
special_tokens_map.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_end|>",
4
+ "<|im_start|>",
5
+ "<|tool_call|>",
6
+ "<|execute_start|>",
7
+ "<|execute_end|>",
8
+ "<|fim_prefix|>",
9
+ "<|fim_middle|>",
10
+ "<|fim_suffix|>"
11
+ ],
12
+ "bos_token": {
13
+ "content": "<s>",
14
+ "lstrip": false,
15
+ "normalized": false,
16
+ "rstrip": false,
17
+ "single_word": false
18
+ },
19
+ "eos_token": {
20
+ "content": "<|im_end|>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false
25
+ },
26
+ "unk_token": {
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": false,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }
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,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": null,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<unk>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<s>",
16
+ "lstrip": false,
17
+ "normalized": false,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "2": {
23
+ "content": "</s>",
24
+ "lstrip": false,
25
+ "normalized": false,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": true
29
+ },
30
+ "73440": {
31
+ "content": "<|im_end|>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false,
36
+ "special": true
37
+ },
38
+ "73441": {
39
+ "content": "<|im_start|>",
40
+ "lstrip": false,
41
+ "normalized": false,
42
+ "rstrip": false,
43
+ "single_word": false,
44
+ "special": true
45
+ },
46
+ "73442": {
47
+ "content": "<|tool_call|>",
48
+ "lstrip": false,
49
+ "normalized": false,
50
+ "rstrip": false,
51
+ "single_word": false,
52
+ "special": true
53
+ },
54
+ "73443": {
55
+ "content": "<|execute_start|>",
56
+ "lstrip": false,
57
+ "normalized": false,
58
+ "rstrip": false,
59
+ "single_word": false,
60
+ "special": true
61
+ },
62
+ "73444": {
63
+ "content": "<|execute_end|>",
64
+ "lstrip": false,
65
+ "normalized": false,
66
+ "rstrip": false,
67
+ "single_word": false,
68
+ "special": true
69
+ },
70
+ "73445": {
71
+ "content": "<|fim_prefix|>",
72
+ "lstrip": false,
73
+ "normalized": false,
74
+ "rstrip": false,
75
+ "single_word": false,
76
+ "special": true
77
+ },
78
+ "73446": {
79
+ "content": "<|fim_middle|>",
80
+ "lstrip": false,
81
+ "normalized": false,
82
+ "rstrip": false,
83
+ "single_word": false,
84
+ "special": true
85
+ },
86
+ "73447": {
87
+ "content": "<|fim_suffix|>",
88
+ "lstrip": false,
89
+ "normalized": false,
90
+ "rstrip": false,
91
+ "single_word": false,
92
+ "special": true
93
+ }
94
+ },
95
+ "additional_special_tokens": [
96
+ "<|im_end|>",
97
+ "<|im_start|>",
98
+ "<|tool_call|>",
99
+ "<|execute_start|>",
100
+ "<|execute_end|>",
101
+ "<|fim_prefix|>",
102
+ "<|fim_middle|>",
103
+ "<|fim_suffix|>"
104
+ ],
105
+ "bos_token": "<s>",
106
+ "chat_template": "{%- macro json_to_python_type(param_name, json_spec) %}\n{%- set basic_type_map = {\n 'string': 'str',\n 'number': 'float',\n 'integer': 'int',\n 'boolean': 'bool',\n 'null': 'None'\n} %}\n\n{%- if json_spec.enum %}\n {{- param_name|title }}\n{%- elif basic_type_map[json_spec.type] is defined %}\n {{- basic_type_map[json_spec.type] }}\n{%- elif json_spec.type == 'array' %}\n {{- 'List[' + json_to_python_type(param_name, json_spec['items']) + ']' }}\n{%- elif json_spec.type == 'object' %}\n {{- 'Dict[str, ' + json_to_python_type(param_name, json_spec.additionalProperties if json_spec.additionalProperties else 'Any') + ']' if not json_spec.properties else param_name|title }}\n{%- elif json_spec.type is iterable %}\n {{- 'Union[' }}\n {%- for t in json_spec.type %}\n {{- json_to_python_type(param_name, {'type': t}) }}\n {{- ', ' if not loop.last }}\n {%- endfor %}\n {{- ']' }}\n{%- else %}\n {{- 'Any' }}\n{%- endif %}\n{%- endmacro %}\n\n{%- macro object_to_fields(json_spec, field_indent) %}\n {%- set o_ns = namespace(f = caller()) %}\n {%- for param_name, param_fields in json_spec.properties|items %}\n {%- if param_fields.enum %}\n {{- '\\n\\nclass ' + param_name|title + '(Enum):\\n' }}\n {%- for enum_option in param_fields.enum %}\n {{- ' enum_' + loop.index0|string + ' = ' + enum_option|tojson + '\\n' }}\n {%- endfor %}\n {%- elif param_fields.type == 'object' and param_fields.properties %}\n {%- call object_to_fields(param_fields, ' ') %}\n {{- '\\n\\nclass ' + param_name|title + '(BaseModel):\\n' }}\n {%- endcall %}\n {%- elif param_fields.type == 'array' and param_fields['items'] and param_fields['items'].type == 'object' and param_fields['items'].properties %}\n {%- call object_to_fields(param_fields['items'], ' ') %}\n {{- '\\n\\nclass ' + param_name|title + '(BaseModel):\\n' }}\n {%- endcall %}\n {%- endif %}\n {%- set param_default = param_fields.default|tojson if param_fields.default is string else param_fields.default|string if param_fields.default is defined else 'None' %}\n {%- set o_ns.f = o_ns.f + field_indent + param_name + ': ' %}\n {%- set o_ns.f = o_ns.f + ('Optional[' + json_to_python_type(param_name, param_fields) + ']' if param_name not in json_spec.required else json_to_python_type(param_name, param_fields)) %}\n {%- if not param_fields.title and not param_fields.description and not param_fields.pattern %}\n {%- set o_ns.f = o_ns.f + (' = ' + param_default if param_name not in json_spec.required else '') %}\n {%- else %}\n {%- set o_ns.f = o_ns.f + (' = Field(...' if param_name in json_spec.required else ' = Field(' + param_default) %}\n {%- set o_ns.f = o_ns.f + (', description=' + param_fields.description|tojson if param_fields.description else '') %}\n {%- set o_ns.f = o_ns.f + (', regex=' + param_fields.pattern|tojson if param_fields.pattern else '') %}\n {%- set o_ns.f = o_ns.f + (', title=' + param_fields.title|tojson if param_fields.title else '') %}\n {%- set o_ns.f = o_ns.f + ')' %}\n {%- endif %}\n {%- set o_ns.f = o_ns.f + '\\n' %}\n {%- endfor %}\n {{- o_ns.f }}\n{%- endmacro %}\n\n{%- macro tool_parser(tools) %}\n{%- for tool in tools %}\n {%- if tool.type is not defined or tool.type == 'function' %}\n {%- if tool.function is defined %}\n {%- set tool = tool.function %}\n {%- endif %}\n {%- set tool_params = tool.parameters if tool.parameters is defined else none %}\n {%- call object_to_fields(tool_params, ' ') %}\n {{- '\\n\\ndef ' + tool.name + '(' }}\n {%- if tool_params %}\n {%- for param_name, param_fields in tool_params.properties|items %}\n {%- set param_default = param_fields.default|tojson if param_fields.default is string else param_fields.default|string if param_fields.default is defined else 'None' %}\n {{- ', ' if loop.index0 != 0 }}\n {{- param_name }}\n {{- '=' + param_default if param_name not in tool_params.required }}\n {%- endfor %}\n {%- endif %}\n {{- '):\\n \"\"\"' }}\n {{- tool.description }}\n {{- '\\n\\n Args:\\n' if tool_params else '\\n' }}\n {%- endcall %}\n {{- ' \"\"\"\\n' }}\n {%- endif %}\n{%- endfor %}\n{%- endmacro %}\n\n{%- if messages[0]['role'] == 'system' %}\n {%- set loop_messages = messages[1:] %}\n {%- set system_message = messages[0]['content'] %}\n{%- else %}\n {%- set loop_messages = messages %}\n {%- set system_message = '' %}\n{%- endif %}\n{{- '<|im_start|>system\\n' + system_message if system_message or tools }}\n{%- if tools %}\n {{- '\\n# Functions\\nHere is a list of functions that you can invoke:\\n```python\\nfrom enum import Enum\\nfrom typing import List, Dict, Optional\\nfrom pydantic import BaseModel, Field\\n\\n' }}\n {{- tool_parser(tools) }}\n {{- \"\\n```\\n\\n# Function Call Rule and Output Format\\n- 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.\\n- 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.\\n- 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.\\n- Use default parameters unless the user has specified otherwise.\\n- You should answer in the following format:\\n\\n<|thought_start|>\\n{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.}\\n<|thought_end|>\\n<|tool_call_start|>\\n```python\\nfunc1(params_name=params_value, params_name2=params_value2...)\\nfunc2(params)\\n```\\n<|tool_call_end|>\\n{answer the user's question directly or ask the user for more information}\" }}\n{%- endif %}\n{{- '<|im_end|>\\n' if system_message or tools }}\n{%- for message in loop_messages %}\n {%- set content = message.content %}\n {%- if message.role == 'assistant' and message.tool_calls %}\n {{- '<|im_start|>' + message.role + '\\n' }}\n {{- '<|thought_start|>\\n' + message.thought + '\\n<|thought_end|>\\n' if message.thought }}\n {{- '<|tool_call_start|>\\n```python\\n' }}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- tool_call.name + '(' }}\n {%- if tool_call.arguments is defined and tool_call.arguments|length > 0 %}\n {%- for param_name, param_value in tool_call.arguments|items %}\n {{- param_name + '=' + param_value|tojson }}\n {{- ',' if not loop.last }}\n {%- endfor %}\n {%- endif %}\n {{- ')\\n' }}\n {%- endfor %}\n {{- '```\\n<|tool_call_end|>\\n' }}\n {{- content if content and not content.startswith('<|tool_call_start|>') }}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == 'assistant' and message.thought %}\n {{- '<|im_start|>' + message.role + '\\n' + '<|thought_start|>\\n' + message.thought + '\\n<|thought_end|>\\n' + content + '<|im_end|>\\n' }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endfor %}\n\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n{%- endif %}",
107
+ "clean_up_tokenization_spaces": false,
108
+ "eos_token": "<|im_end|>",
109
+ "legacy": true,
110
+ "model_max_length": 1000000000000000019884624838656,
111
+ "pad_token": null,
112
+ "sp_model_kwargs": {},
113
+ "spaces_between_special_tokens": false,
114
+ "tokenizer_class": "LlamaTokenizer",
115
+ "unk_token": "<unk>",
116
+ "use_default_system_prompt": false
117
+ }