Upload folder using huggingface_hub
Browse files- config.json +30 -0
- configuration_zhinao.py +119 -0
- generation_config.json +15 -0
- model-00001-of-00004.safetensors +3 -0
- model-00002-of-00004.safetensors +3 -0
- model-00003-of-00004.safetensors +3 -0
- model-00004-of-00004.safetensors +3 -0
- model.safetensors.index.json +266 -0
- modeling_zhinao.py +1012 -0
- special_tokens_map.json +3 -0
- tokenization_zhinao.py +266 -0
- tokenizer_config.json +27 -0
- vocab/360.tiktoken +0 -0
config.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"ZhinaoForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_dropout": 0.0,
|
| 6 |
+
"auto_map": {
|
| 7 |
+
"AutoConfig": "configuration_zhinao.ZhinaoConfig",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_zhinao.ZhinaoForCausalLM"
|
| 9 |
+
},
|
| 10 |
+
"flah-attn_version": "2.5.5",
|
| 11 |
+
"hidden_act": "silu",
|
| 12 |
+
"hidden_size": 4096,
|
| 13 |
+
"initializer_range": 0.01,
|
| 14 |
+
"intermediate_size": 13056,
|
| 15 |
+
"max_position_embeddings": 32768,
|
| 16 |
+
"max_window_layers": 28,
|
| 17 |
+
"model_type": "zhinao",
|
| 18 |
+
"num_attention_heads": 32,
|
| 19 |
+
"num_hidden_layers": 32,
|
| 20 |
+
"num_key_value_heads": 8,
|
| 21 |
+
"rms_norm_eps": 1e-05,
|
| 22 |
+
"rope_theta": 1000000,
|
| 23 |
+
"sliding_window": null,
|
| 24 |
+
"tie_word_embeddings": false,
|
| 25 |
+
"torch_dtype": "bfloat16",
|
| 26 |
+
"transformers_version": "4.51.0",
|
| 27 |
+
"use_cache": true,
|
| 28 |
+
"use_sliding_window": false,
|
| 29 |
+
"vocab_size": 158464
|
| 30 |
+
}
|
configuration_zhinao.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 2 |
+
from transformers.utils import logging
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
logger = logging.get_logger(__name__)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class ZhinaoConfig(PretrainedConfig):
|
| 9 |
+
r"""
|
| 10 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 11 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
vocab_size (`int`, *optional*, defaults to 158464):
|
| 16 |
+
Vocabulary size of the Zhinao model. Defines the number of different tokens that can be represented by the
|
| 17 |
+
`inputs_ids` passed when calling [`ZhinaoModel`]
|
| 18 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
| 19 |
+
Dimension of the hidden representations.
|
| 20 |
+
intermediate_size (`int`, *optional*, defaults to 13056):
|
| 21 |
+
Dimension of the MLP representations.
|
| 22 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
| 23 |
+
Number of hidden layers in the Transformer encoder.
|
| 24 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 25 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 26 |
+
num_key_value_heads (`int`, *optional*, defaults to 32):
|
| 27 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
| 28 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
| 29 |
+
`num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
| 30 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
| 31 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
| 32 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `32`.
|
| 33 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 34 |
+
The non-linear activation function (function or string) in the decoder.
|
| 35 |
+
max_position_embeddings (`int`, *optional*, defaults to 4096):
|
| 36 |
+
The maximum sequence length that this model might ever be used with.
|
| 37 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 38 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 39 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 40 |
+
The epsilon used by the rms normalization layers.
|
| 41 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 42 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
| 43 |
+
relevant if `config.is_decoder=True`.
|
| 44 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
| 45 |
+
Whether the model's input and output word embeddings should be tied.
|
| 46 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 47 |
+
The base period of the RoPE embeddings.
|
| 48 |
+
use_sliding_window (`bool`, *optional*, defaults to `False`):
|
| 49 |
+
Whether to use sliding window attention.
|
| 50 |
+
sliding_window (`int`, *optional*, defaults to 4096):
|
| 51 |
+
Sliding window attention (SWA) window size. If not specified, will default to `4096`.
|
| 52 |
+
max_window_layers (`int`, *optional*, defaults to 28):
|
| 53 |
+
The number of layers that use SWA (Sliding Window Attention). The bottom layers use SWA while the top use full attention.
|
| 54 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 55 |
+
The dropout ratio for the attention probabilities.
|
| 56 |
+
|
| 57 |
+
```python
|
| 58 |
+
>>> from transformers import ZhinaoModel, ZhinaoConfig
|
| 59 |
+
|
| 60 |
+
>>> # Initializing a Zhinao style configuration
|
| 61 |
+
>>> configuration = ZhinaoConfig()
|
| 62 |
+
|
| 63 |
+
>>> # Initializing a model from the Zhinao-7B style configuration
|
| 64 |
+
>>> model = Zhinao2Model(configuration)
|
| 65 |
+
|
| 66 |
+
>>> # Accessing the model configuration
|
| 67 |
+
>>> configuration = model.config
|
| 68 |
+
```"""
|
| 69 |
+
|
| 70 |
+
model_type = "zhinao"
|
| 71 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 72 |
+
|
| 73 |
+
def __init__(
|
| 74 |
+
self,
|
| 75 |
+
vocab_size=158464,
|
| 76 |
+
hidden_size=4096,
|
| 77 |
+
intermediate_size=13056,
|
| 78 |
+
num_hidden_layers=32,
|
| 79 |
+
num_attention_heads=32,
|
| 80 |
+
num_key_value_heads=32,
|
| 81 |
+
hidden_act="silu",
|
| 82 |
+
max_position_embeddings=4096,
|
| 83 |
+
initializer_range=0.01,
|
| 84 |
+
rms_norm_eps=1e-5,
|
| 85 |
+
use_cache=True,
|
| 86 |
+
tie_word_embeddings=False,
|
| 87 |
+
rope_theta=10000.0,
|
| 88 |
+
use_sliding_window=False,
|
| 89 |
+
sliding_window=4096,
|
| 90 |
+
max_window_layers=28,
|
| 91 |
+
attention_dropout=0.0,
|
| 92 |
+
**kwargs,
|
| 93 |
+
):
|
| 94 |
+
self.vocab_size = vocab_size
|
| 95 |
+
self.max_position_embeddings = max_position_embeddings
|
| 96 |
+
self.hidden_size = hidden_size
|
| 97 |
+
self.intermediate_size = intermediate_size
|
| 98 |
+
self.num_hidden_layers = num_hidden_layers
|
| 99 |
+
self.num_attention_heads = num_attention_heads
|
| 100 |
+
self.use_sliding_window = use_sliding_window
|
| 101 |
+
self.sliding_window = sliding_window if use_sliding_window else None
|
| 102 |
+
self.max_window_layers = max_window_layers
|
| 103 |
+
|
| 104 |
+
# for backward compatibility
|
| 105 |
+
if num_key_value_heads is None:
|
| 106 |
+
num_key_value_heads = num_attention_heads
|
| 107 |
+
|
| 108 |
+
self.num_key_value_heads = num_key_value_heads
|
| 109 |
+
self.hidden_act = hidden_act
|
| 110 |
+
self.initializer_range = initializer_range
|
| 111 |
+
self.rms_norm_eps = rms_norm_eps
|
| 112 |
+
self.use_cache = use_cache
|
| 113 |
+
self.rope_theta = rope_theta
|
| 114 |
+
self.attention_dropout = attention_dropout
|
| 115 |
+
|
| 116 |
+
super().__init__(
|
| 117 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 118 |
+
**kwargs,
|
| 119 |
+
)
|
generation_config.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token_id": 158326,
|
| 3 |
+
"do_sample": true,
|
| 4 |
+
"eos_token_id": [
|
| 5 |
+
158326,
|
| 6 |
+
158332,
|
| 7 |
+
158333
|
| 8 |
+
],
|
| 9 |
+
"pad_token_id": 158326,
|
| 10 |
+
"repetition_penalty": 1.05,
|
| 11 |
+
"temperature": 0.7,
|
| 12 |
+
"top_k": 20,
|
| 13 |
+
"top_p": 0.8,
|
| 14 |
+
"transformers_version": "4.51.0"
|
| 15 |
+
}
|
model-00001-of-00004.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4346b1c2e07ce69df62a60f7f8e05e89bfba8e1ec5b102422b0a64fd2547859e
|
| 3 |
+
size 4991500768
|
model-00002-of-00004.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:53721c92a51c8282e6b600899012711ced0aa48ec47130126c6b6d9605b61cad
|
| 3 |
+
size 4997868656
|
model-00003-of-00004.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e7c4cc4a03889e94bc8d9eaea7e507ce7f19612c631d7ba5b2ea86338a91869
|
| 3 |
+
size 4261734000
|
model-00004-of-00004.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2d7f92c434e684d79750be0f8a525e1f3051aea3d064915a7a3f53f828991639
|
| 3 |
+
size 1298137216
|
model.safetensors.index.json
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"metadata": {
|
| 3 |
+
"total_size": 15549210624
|
| 4 |
+
},
|
| 5 |
+
"weight_map": {
|
| 6 |
+
"lm_head.weight": "model-00004-of-00004.safetensors",
|
| 7 |
+
"model.embed_tokens.weight": "model-00001-of-00004.safetensors",
|
| 8 |
+
"model.layers.0.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 9 |
+
"model.layers.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 10 |
+
"model.layers.0.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 11 |
+
"model.layers.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 12 |
+
"model.layers.0.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 13 |
+
"model.layers.0.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 14 |
+
"model.layers.0.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 15 |
+
"model.layers.0.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 16 |
+
"model.layers.1.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 17 |
+
"model.layers.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 18 |
+
"model.layers.1.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 19 |
+
"model.layers.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 20 |
+
"model.layers.1.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 21 |
+
"model.layers.1.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 22 |
+
"model.layers.1.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 23 |
+
"model.layers.1.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 24 |
+
"model.layers.10.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 25 |
+
"model.layers.10.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 26 |
+
"model.layers.10.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 27 |
+
"model.layers.10.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 28 |
+
"model.layers.10.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 29 |
+
"model.layers.10.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 30 |
+
"model.layers.10.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 31 |
+
"model.layers.10.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 32 |
+
"model.layers.11.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 33 |
+
"model.layers.11.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 34 |
+
"model.layers.11.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 35 |
+
"model.layers.11.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 36 |
+
"model.layers.11.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 37 |
+
"model.layers.11.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 38 |
+
"model.layers.11.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 39 |
+
"model.layers.11.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 40 |
+
"model.layers.12.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 41 |
+
"model.layers.12.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 42 |
+
"model.layers.12.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 43 |
+
"model.layers.12.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 44 |
+
"model.layers.12.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 45 |
+
"model.layers.12.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 46 |
+
"model.layers.12.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 47 |
+
"model.layers.12.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 48 |
+
"model.layers.13.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 49 |
+
"model.layers.13.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 50 |
+
"model.layers.13.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 51 |
+
"model.layers.13.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 52 |
+
"model.layers.13.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 53 |
+
"model.layers.13.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 54 |
+
"model.layers.13.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 55 |
+
"model.layers.13.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 56 |
+
"model.layers.14.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 57 |
+
"model.layers.14.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 58 |
+
"model.layers.14.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 59 |
+
"model.layers.14.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 60 |
+
"model.layers.14.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 61 |
+
"model.layers.14.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 62 |
+
"model.layers.14.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 63 |
+
"model.layers.14.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 64 |
+
"model.layers.15.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 65 |
+
"model.layers.15.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 66 |
+
"model.layers.15.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 67 |
+
"model.layers.15.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 68 |
+
"model.layers.15.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 69 |
+
"model.layers.15.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 70 |
+
"model.layers.15.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 71 |
+
"model.layers.15.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 72 |
+
"model.layers.16.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 73 |
+
"model.layers.16.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 74 |
+
"model.layers.16.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 75 |
+
"model.layers.16.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 76 |
+
"model.layers.16.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 77 |
+
"model.layers.16.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 78 |
+
"model.layers.16.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 79 |
+
"model.layers.16.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 80 |
+
"model.layers.17.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 81 |
+
"model.layers.17.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 82 |
+
"model.layers.17.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 83 |
+
"model.layers.17.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 84 |
+
"model.layers.17.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 85 |
+
"model.layers.17.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 86 |
+
"model.layers.17.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 87 |
+
"model.layers.17.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 88 |
+
"model.layers.18.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 89 |
+
"model.layers.18.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 90 |
+
"model.layers.18.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 91 |
+
"model.layers.18.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 92 |
+
"model.layers.18.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 93 |
+
"model.layers.18.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 94 |
+
"model.layers.18.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 95 |
+
"model.layers.18.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 96 |
+
"model.layers.19.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 97 |
+
"model.layers.19.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 98 |
+
"model.layers.19.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 99 |
+
"model.layers.19.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 100 |
+
"model.layers.19.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 101 |
+
"model.layers.19.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 102 |
+
"model.layers.19.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 103 |
+
"model.layers.19.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 104 |
+
"model.layers.2.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 105 |
+
"model.layers.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 106 |
+
"model.layers.2.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 107 |
+
"model.layers.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 108 |
+
"model.layers.2.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 109 |
+
"model.layers.2.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 110 |
+
"model.layers.2.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 111 |
+
"model.layers.2.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 112 |
+
"model.layers.20.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 113 |
+
"model.layers.20.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 114 |
+
"model.layers.20.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 115 |
+
"model.layers.20.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 116 |
+
"model.layers.20.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 117 |
+
"model.layers.20.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 118 |
+
"model.layers.20.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 119 |
+
"model.layers.20.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 120 |
+
"model.layers.21.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 121 |
+
"model.layers.21.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 122 |
+
"model.layers.21.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 123 |
+
"model.layers.21.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 124 |
+
"model.layers.21.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 125 |
+
"model.layers.21.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 126 |
+
"model.layers.21.self_attn.qkv_proj.bias": "model-00002-of-00004.safetensors",
|
| 127 |
+
"model.layers.21.self_attn.qkv_proj.weight": "model-00002-of-00004.safetensors",
|
| 128 |
+
"model.layers.22.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 129 |
+
"model.layers.22.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 130 |
+
"model.layers.22.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 131 |
+
"model.layers.22.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 132 |
+
"model.layers.22.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 133 |
+
"model.layers.22.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 134 |
+
"model.layers.22.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 135 |
+
"model.layers.22.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 136 |
+
"model.layers.23.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 137 |
+
"model.layers.23.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 138 |
+
"model.layers.23.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 139 |
+
"model.layers.23.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 140 |
+
"model.layers.23.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 141 |
+
"model.layers.23.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 142 |
+
"model.layers.23.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 143 |
+
"model.layers.23.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 144 |
+
"model.layers.24.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 145 |
+
"model.layers.24.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 146 |
+
"model.layers.24.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 147 |
+
"model.layers.24.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 148 |
+
"model.layers.24.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 149 |
+
"model.layers.24.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 150 |
+
"model.layers.24.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 151 |
+
"model.layers.24.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 152 |
+
"model.layers.25.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 153 |
+
"model.layers.25.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 154 |
+
"model.layers.25.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 155 |
+
"model.layers.25.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 156 |
+
"model.layers.25.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 157 |
+
"model.layers.25.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 158 |
+
"model.layers.25.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 159 |
+
"model.layers.25.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 160 |
+
"model.layers.26.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 161 |
+
"model.layers.26.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 162 |
+
"model.layers.26.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 163 |
+
"model.layers.26.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 164 |
+
"model.layers.26.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 165 |
+
"model.layers.26.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 166 |
+
"model.layers.26.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 167 |
+
"model.layers.26.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 168 |
+
"model.layers.27.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 169 |
+
"model.layers.27.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 170 |
+
"model.layers.27.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 171 |
+
"model.layers.27.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 172 |
+
"model.layers.27.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 173 |
+
"model.layers.27.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 174 |
+
"model.layers.27.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 175 |
+
"model.layers.27.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 176 |
+
"model.layers.28.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 177 |
+
"model.layers.28.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 178 |
+
"model.layers.28.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 179 |
+
"model.layers.28.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 180 |
+
"model.layers.28.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 181 |
+
"model.layers.28.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 182 |
+
"model.layers.28.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 183 |
+
"model.layers.28.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 184 |
+
"model.layers.29.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 185 |
+
"model.layers.29.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 186 |
+
"model.layers.29.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 187 |
+
"model.layers.29.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 188 |
+
"model.layers.29.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 189 |
+
"model.layers.29.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 190 |
+
"model.layers.29.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 191 |
+
"model.layers.29.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 192 |
+
"model.layers.3.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 193 |
+
"model.layers.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 194 |
+
"model.layers.3.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 195 |
+
"model.layers.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 196 |
+
"model.layers.3.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 197 |
+
"model.layers.3.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 198 |
+
"model.layers.3.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 199 |
+
"model.layers.3.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 200 |
+
"model.layers.30.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 201 |
+
"model.layers.30.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 202 |
+
"model.layers.30.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 203 |
+
"model.layers.30.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 204 |
+
"model.layers.30.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 205 |
+
"model.layers.30.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 206 |
+
"model.layers.30.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 207 |
+
"model.layers.30.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 208 |
+
"model.layers.31.input_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 209 |
+
"model.layers.31.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
|
| 210 |
+
"model.layers.31.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
|
| 211 |
+
"model.layers.31.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
|
| 212 |
+
"model.layers.31.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
|
| 213 |
+
"model.layers.31.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
|
| 214 |
+
"model.layers.31.self_attn.qkv_proj.bias": "model-00003-of-00004.safetensors",
|
| 215 |
+
"model.layers.31.self_attn.qkv_proj.weight": "model-00003-of-00004.safetensors",
|
| 216 |
+
"model.layers.4.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 217 |
+
"model.layers.4.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 218 |
+
"model.layers.4.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 219 |
+
"model.layers.4.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 220 |
+
"model.layers.4.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 221 |
+
"model.layers.4.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 222 |
+
"model.layers.4.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 223 |
+
"model.layers.4.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 224 |
+
"model.layers.5.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 225 |
+
"model.layers.5.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 226 |
+
"model.layers.5.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 227 |
+
"model.layers.5.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 228 |
+
"model.layers.5.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 229 |
+
"model.layers.5.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 230 |
+
"model.layers.5.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 231 |
+
"model.layers.5.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 232 |
+
"model.layers.6.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 233 |
+
"model.layers.6.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 234 |
+
"model.layers.6.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 235 |
+
"model.layers.6.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 236 |
+
"model.layers.6.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 237 |
+
"model.layers.6.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 238 |
+
"model.layers.6.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 239 |
+
"model.layers.6.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 240 |
+
"model.layers.7.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 241 |
+
"model.layers.7.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 242 |
+
"model.layers.7.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 243 |
+
"model.layers.7.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 244 |
+
"model.layers.7.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 245 |
+
"model.layers.7.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 246 |
+
"model.layers.7.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 247 |
+
"model.layers.7.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 248 |
+
"model.layers.8.input_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 249 |
+
"model.layers.8.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
|
| 250 |
+
"model.layers.8.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
|
| 251 |
+
"model.layers.8.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
|
| 252 |
+
"model.layers.8.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
|
| 253 |
+
"model.layers.8.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
|
| 254 |
+
"model.layers.8.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 255 |
+
"model.layers.8.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 256 |
+
"model.layers.9.input_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 257 |
+
"model.layers.9.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
|
| 258 |
+
"model.layers.9.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
|
| 259 |
+
"model.layers.9.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
|
| 260 |
+
"model.layers.9.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
|
| 261 |
+
"model.layers.9.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
|
| 262 |
+
"model.layers.9.self_attn.qkv_proj.bias": "model-00001-of-00004.safetensors",
|
| 263 |
+
"model.layers.9.self_attn.qkv_proj.weight": "model-00001-of-00004.safetensors",
|
| 264 |
+
"model.norm.weight": "model-00003-of-00004.safetensors"
|
| 265 |
+
}
|
| 266 |
+
}
|
modeling_zhinao.py
ADDED
|
@@ -0,0 +1,1012 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) 360zhinao and the HuggingFace Inc. team. All rights reserved.
|
| 2 |
+
# This code is built upon Huggingface's transformers repository.
|
| 3 |
+
|
| 4 |
+
import math
|
| 5 |
+
from typing import List, Optional, Tuple, Union
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import torch.utils.checkpoint
|
| 9 |
+
from torch import nn
|
| 10 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
| 11 |
+
|
| 12 |
+
from transformers.activations import ACT2FN
|
| 13 |
+
from transformers.cache_utils import Cache, DynamicCache, StaticCache
|
| 14 |
+
from .configuration_zhinao import ZhinaoConfig
|
| 15 |
+
from transformers.modeling_attn_mask_utils import AttentionMaskConverter
|
| 16 |
+
from transformers.modeling_outputs import (
|
| 17 |
+
BaseModelOutputWithPast,
|
| 18 |
+
CausalLMOutputWithPast,
|
| 19 |
+
SequenceClassifierOutputWithPast,
|
| 20 |
+
TokenClassifierOutput,
|
| 21 |
+
)
|
| 22 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 23 |
+
from transformers.utils import (
|
| 24 |
+
add_start_docstrings,
|
| 25 |
+
add_start_docstrings_to_model_forward,
|
| 26 |
+
is_flash_attn_2_available,
|
| 27 |
+
is_flash_attn_greater_or_equal_2_10,
|
| 28 |
+
logging,
|
| 29 |
+
replace_return_docstrings,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if is_flash_attn_2_available():
|
| 33 |
+
from transformers.modeling_flash_attention_utils import _flash_attention_forward
|
| 34 |
+
|
| 35 |
+
logger = logging.get_logger(__name__)
|
| 36 |
+
_CONFIG_FOR_DOC = "ZhinaoConfig"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->Zhinao
|
| 40 |
+
class ZhinaoRMSNorm(nn.Module):
|
| 41 |
+
def __init__(self, hidden_size, eps=1e-6):
|
| 42 |
+
"""
|
| 43 |
+
ZhinaoRMSNorm is equivalent to T5LayerNorm
|
| 44 |
+
"""
|
| 45 |
+
super().__init__()
|
| 46 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
| 47 |
+
self.variance_epsilon = eps
|
| 48 |
+
|
| 49 |
+
def forward(self, hidden_states):
|
| 50 |
+
input_dtype = hidden_states.dtype
|
| 51 |
+
hidden_states = hidden_states.to(torch.float32)
|
| 52 |
+
variance = hidden_states.pow(2).mean(-1, keepdim=True)
|
| 53 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
| 54 |
+
return self.weight * hidden_states.to(input_dtype)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# Copied from transformers.models.mixtral.modeling_mixtral.MixtralRotaryEmbedding with Mixtral->Zhinao
|
| 58 |
+
class ZhinaoRotaryEmbedding(nn.Module):
|
| 59 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
| 60 |
+
super().__init__()
|
| 61 |
+
|
| 62 |
+
self.dim = dim
|
| 63 |
+
self.max_position_embeddings = max_position_embeddings
|
| 64 |
+
self.base = base
|
| 65 |
+
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim))
|
| 66 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 67 |
+
|
| 68 |
+
# Build here to make `torch.jit.trace` work.
|
| 69 |
+
self._set_cos_sin_cache(
|
| 70 |
+
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
| 74 |
+
self.max_seq_len_cached = seq_len
|
| 75 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq)
|
| 76 |
+
|
| 77 |
+
freqs = torch.outer(t, self.inv_freq)
|
| 78 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
| 79 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
| 80 |
+
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
| 81 |
+
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
| 82 |
+
|
| 83 |
+
def forward(self, x, seq_len=None):
|
| 84 |
+
# x: [bs, num_attention_heads, seq_len, head_size]
|
| 85 |
+
if seq_len > self.max_seq_len_cached:
|
| 86 |
+
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
|
| 87 |
+
|
| 88 |
+
return (
|
| 89 |
+
self.cos_cached[:seq_len].to(dtype=x.dtype),
|
| 90 |
+
self.sin_cached[:seq_len].to(dtype=x.dtype),
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# Copied from transformers.models.llama.modeling_llama.rotate_half
|
| 95 |
+
def rotate_half(x):
|
| 96 |
+
"""Rotates half the hidden dims of the input."""
|
| 97 |
+
x1 = x[..., : x.shape[-1] // 2]
|
| 98 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
| 99 |
+
return torch.cat((-x2, x1), dim=-1)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# Copied from transformers.models.mixtral.modeling_mixtral.apply_rotary_pos_emb
|
| 103 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
|
| 104 |
+
"""Applies Rotary Position Embedding to the query and key tensors.
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
q (`torch.Tensor`): The query tensor.
|
| 108 |
+
k (`torch.Tensor`): The key tensor.
|
| 109 |
+
cos (`torch.Tensor`): The cosine part of the rotary embedding.
|
| 110 |
+
sin (`torch.Tensor`): The sine part of the rotary embedding.
|
| 111 |
+
position_ids (`torch.Tensor`):
|
| 112 |
+
The position indices of the tokens corresponding to the query and key tensors. For example, this can be
|
| 113 |
+
used to pass offsetted position ids when working with a KV-cache.
|
| 114 |
+
unsqueeze_dim (`int`, *optional*, defaults to 1):
|
| 115 |
+
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
|
| 116 |
+
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
|
| 117 |
+
that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
|
| 118 |
+
k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
|
| 119 |
+
cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
|
| 120 |
+
the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
|
| 121 |
+
Returns:
|
| 122 |
+
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
|
| 123 |
+
"""
|
| 124 |
+
cos = cos[position_ids].unsqueeze(unsqueeze_dim)
|
| 125 |
+
sin = sin[position_ids].unsqueeze(unsqueeze_dim)
|
| 126 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
| 127 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
| 128 |
+
return q_embed, k_embed
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# Copied from transformers.models.mistral.modeling_mistral.MistralMLP with Mistral->Zhinao
|
| 132 |
+
class ZhinaoMLP(nn.Module):
|
| 133 |
+
def __init__(self, config):
|
| 134 |
+
super().__init__()
|
| 135 |
+
self.hidden_size = config.hidden_size
|
| 136 |
+
self.intermediate_size = config.intermediate_size
|
| 137 |
+
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 138 |
+
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 139 |
+
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
|
| 140 |
+
self.act_fn = ACT2FN[config.hidden_act]
|
| 141 |
+
|
| 142 |
+
def forward(self, hidden_state):
|
| 143 |
+
return self.down_proj(self.act_fn(self.gate_proj(hidden_state)) * self.up_proj(hidden_state))
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
# Copied from transformers.models.llama.modeling_llama.repeat_kv
|
| 147 |
+
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
| 148 |
+
"""
|
| 149 |
+
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
| 150 |
+
num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
|
| 151 |
+
"""
|
| 152 |
+
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
|
| 153 |
+
if n_rep == 1:
|
| 154 |
+
return hidden_states
|
| 155 |
+
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
|
| 156 |
+
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
class ZhinaoAttention(nn.Module):
|
| 160 |
+
"""
|
| 161 |
+
Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer
|
| 162 |
+
and "Generating Long Sequences with Sparse Transformers".
|
| 163 |
+
"""
|
| 164 |
+
|
| 165 |
+
def __init__(self, config: ZhinaoConfig, layer_idx: Optional[int] = None):
|
| 166 |
+
super().__init__()
|
| 167 |
+
self.config = config
|
| 168 |
+
self.layer_idx = layer_idx
|
| 169 |
+
if layer_idx is None:
|
| 170 |
+
logger.warning_once(
|
| 171 |
+
f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
|
| 172 |
+
"to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
|
| 173 |
+
"when creating this class."
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
self.hidden_size = config.hidden_size
|
| 177 |
+
self.num_heads = config.num_attention_heads
|
| 178 |
+
self.head_dim = self.hidden_size // self.num_heads
|
| 179 |
+
self.num_key_value_heads = config.num_key_value_heads
|
| 180 |
+
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
| 181 |
+
self.max_position_embeddings = config.max_position_embeddings
|
| 182 |
+
self.rope_theta = config.rope_theta
|
| 183 |
+
self.is_causal = True
|
| 184 |
+
self.attention_dropout = config.attention_dropout
|
| 185 |
+
|
| 186 |
+
if (self.head_dim * self.num_heads) != self.hidden_size:
|
| 187 |
+
raise ValueError(
|
| 188 |
+
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
| 189 |
+
f" and `num_heads`: {self.num_heads})."
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
self.qkv_hidden_size = (self.num_heads + 2 * self.num_key_value_heads) * self.head_dim
|
| 193 |
+
self.qkv_proj = nn.Linear(self.hidden_size, self.qkv_hidden_size, bias=True)
|
| 194 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
| 195 |
+
|
| 196 |
+
self.rotary_emb = ZhinaoRotaryEmbedding(
|
| 197 |
+
self.head_dim,
|
| 198 |
+
max_position_embeddings=self.max_position_embeddings,
|
| 199 |
+
base=self.rope_theta,
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
def forward(
|
| 203 |
+
self,
|
| 204 |
+
hidden_states: torch.Tensor,
|
| 205 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 206 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 207 |
+
past_key_value: Optional[Cache] = None,
|
| 208 |
+
output_attentions: bool = False,
|
| 209 |
+
use_cache: bool = False,
|
| 210 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 211 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
| 212 |
+
bsz, q_len, _ = hidden_states.size()
|
| 213 |
+
|
| 214 |
+
mixed_x_layer = self.qkv_proj(hidden_states)
|
| 215 |
+
new_tensor_shape = mixed_x_layer.size()[:-1] + \
|
| 216 |
+
(self.num_key_value_heads, ((self.num_heads // self.num_key_value_heads + 2) * self.head_dim))
|
| 217 |
+
mixed_x_layer = mixed_x_layer.view(*new_tensor_shape)
|
| 218 |
+
query, key_states, value_states = torch.split(
|
| 219 |
+
mixed_x_layer,
|
| 220 |
+
[self.num_heads // self.num_key_value_heads * self.head_dim, self.head_dim, self.head_dim],
|
| 221 |
+
dim=3
|
| 222 |
+
)
|
| 223 |
+
# [sq, b, ng, np/ng * hn] -> [sq, b, np, hn]
|
| 224 |
+
query_states = query.contiguous().view(query.size(0), query.size(1), -1, self.head_dim)
|
| 225 |
+
|
| 226 |
+
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
| 227 |
+
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
| 228 |
+
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
| 229 |
+
|
| 230 |
+
kv_seq_len = key_states.shape[-2]
|
| 231 |
+
if past_key_value is not None:
|
| 232 |
+
if self.layer_idx is None:
|
| 233 |
+
raise ValueError(
|
| 234 |
+
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
| 235 |
+
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
| 236 |
+
"with a layer index."
|
| 237 |
+
)
|
| 238 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
| 239 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
| 240 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
| 241 |
+
|
| 242 |
+
if past_key_value is not None:
|
| 243 |
+
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models
|
| 244 |
+
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
| 245 |
+
|
| 246 |
+
# repeat k/v heads if n_kv_heads < n_heads
|
| 247 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
| 248 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
| 249 |
+
|
| 250 |
+
attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
|
| 251 |
+
|
| 252 |
+
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
| 253 |
+
raise ValueError(
|
| 254 |
+
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
|
| 255 |
+
f" {attn_weights.size()}"
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
if attention_mask is not None: # no matter the length, we just slice it
|
| 259 |
+
causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
|
| 260 |
+
attn_weights = attn_weights + causal_mask
|
| 261 |
+
|
| 262 |
+
# upcast attention to fp32
|
| 263 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
|
| 264 |
+
attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
|
| 265 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
| 266 |
+
|
| 267 |
+
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
|
| 268 |
+
raise ValueError(
|
| 269 |
+
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
|
| 270 |
+
f" {attn_output.size()}"
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
| 274 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
| 275 |
+
|
| 276 |
+
attn_output = self.o_proj(attn_output)
|
| 277 |
+
|
| 278 |
+
if not output_attentions:
|
| 279 |
+
attn_weights = None
|
| 280 |
+
|
| 281 |
+
return attn_output, attn_weights, past_key_value
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
class ZhinaoFlashAttention2(ZhinaoAttention):
|
| 285 |
+
"""
|
| 286 |
+
Zhinao flash attention module, following Zhinao attention module. This module inherits from `ZhinaoAttention`
|
| 287 |
+
as the weights of the module stays untouched. The only required change would be on the forward pass
|
| 288 |
+
where it needs to correctly call the public API of flash attention and deal with padding tokens
|
| 289 |
+
in case the input contains any of them. Additionally, for sliding window attention, we apply SWA only to the bottom
|
| 290 |
+
config.max_window_layers layers.
|
| 291 |
+
"""
|
| 292 |
+
|
| 293 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
|
| 294 |
+
def __init__(self, *args, **kwargs):
|
| 295 |
+
super().__init__(*args, **kwargs)
|
| 296 |
+
|
| 297 |
+
# TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
|
| 298 |
+
# 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.
|
| 299 |
+
# 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).
|
| 300 |
+
self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
|
| 301 |
+
|
| 302 |
+
def forward(
|
| 303 |
+
self,
|
| 304 |
+
hidden_states: torch.Tensor,
|
| 305 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 306 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 307 |
+
past_key_value: Optional[Cache] = None,
|
| 308 |
+
output_attentions: bool = False,
|
| 309 |
+
use_cache: bool = False,
|
| 310 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 311 |
+
):
|
| 312 |
+
bsz, q_len, _ = hidden_states.size()
|
| 313 |
+
|
| 314 |
+
mixed_x_layer = self.qkv_proj(hidden_states)
|
| 315 |
+
new_tensor_shape = mixed_x_layer.size()[:-1] + \
|
| 316 |
+
(self.num_key_value_heads, ((self.num_heads // self.num_key_value_heads + 2) * self.head_dim))
|
| 317 |
+
mixed_x_layer = mixed_x_layer.view(*new_tensor_shape)
|
| 318 |
+
query, key_states, value_states = torch.split(
|
| 319 |
+
mixed_x_layer,
|
| 320 |
+
[self.num_heads // self.num_key_value_heads * self.head_dim, self.head_dim, self.head_dim],
|
| 321 |
+
dim=3
|
| 322 |
+
)
|
| 323 |
+
# [sq, b, ng, np/ng * hn] -> [sq, b, np, hn]
|
| 324 |
+
query_states = query.contiguous().view(query.size(0), query.size(1), -1, self.head_dim)
|
| 325 |
+
|
| 326 |
+
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
| 327 |
+
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
| 328 |
+
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
| 329 |
+
|
| 330 |
+
kv_seq_len = key_states.shape[-2]
|
| 331 |
+
if past_key_value is not None:
|
| 332 |
+
if self.layer_idx is None:
|
| 333 |
+
raise ValueError(
|
| 334 |
+
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
| 335 |
+
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
| 336 |
+
"with a layer index."
|
| 337 |
+
)
|
| 338 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
| 339 |
+
|
| 340 |
+
# Because the input can be padded, the absolute sequence length depends on the max position id.
|
| 341 |
+
rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1
|
| 342 |
+
cos, sin = self.rotary_emb(value_states, seq_len=rotary_seq_len)
|
| 343 |
+
|
| 344 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
| 345 |
+
|
| 346 |
+
if past_key_value is not None:
|
| 347 |
+
# Activate slicing cache only if the config has a value `sliding_windows` attribute
|
| 348 |
+
cache_has_contents = past_key_value.get_seq_length(self.layer_idx) > 0
|
| 349 |
+
if (
|
| 350 |
+
getattr(self.config, "sliding_window", None) is not None
|
| 351 |
+
and kv_seq_len > self.config.sliding_window
|
| 352 |
+
and cache_has_contents
|
| 353 |
+
):
|
| 354 |
+
slicing_tokens = 1 - self.config.sliding_window
|
| 355 |
+
|
| 356 |
+
past_key = past_key_value[self.layer_idx][0]
|
| 357 |
+
past_value = past_key_value[self.layer_idx][1]
|
| 358 |
+
|
| 359 |
+
past_key = past_key[:, :, slicing_tokens:, :].contiguous()
|
| 360 |
+
past_value = past_value[:, :, slicing_tokens:, :].contiguous()
|
| 361 |
+
|
| 362 |
+
if past_key.shape[-2] != self.config.sliding_window - 1:
|
| 363 |
+
raise ValueError(
|
| 364 |
+
f"past key must have a shape of (`batch_size, num_heads, self.config.sliding_window-1, head_dim`), got"
|
| 365 |
+
f" {past_key.shape}"
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
if attention_mask is not None:
|
| 369 |
+
attention_mask = attention_mask[:, slicing_tokens:]
|
| 370 |
+
attention_mask = torch.cat([attention_mask, torch.ones_like(attention_mask[:, -1:])], dim=-1)
|
| 371 |
+
|
| 372 |
+
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models
|
| 373 |
+
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
| 374 |
+
|
| 375 |
+
# repeat k/v heads if n_kv_heads < n_heads
|
| 376 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
| 377 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
| 378 |
+
dropout_rate = 0.0 if not self.training else self.attention_dropout
|
| 379 |
+
|
| 380 |
+
# In PEFT, usually we cast the layer norms in float32 for training stability reasons
|
| 381 |
+
# therefore the input hidden states gets silently casted in float32. Hence, we need
|
| 382 |
+
# cast them back in float16 just to be sure everything works as expected.
|
| 383 |
+
input_dtype = query_states.dtype
|
| 384 |
+
if input_dtype == torch.float32:
|
| 385 |
+
if torch.is_autocast_enabled():
|
| 386 |
+
target_dtype = torch.get_autocast_gpu_dtype()
|
| 387 |
+
# Handle the case where the model is quantized
|
| 388 |
+
elif hasattr(self.config, "_pre_quantization_dtype"):
|
| 389 |
+
target_dtype = self.config._pre_quantization_dtype
|
| 390 |
+
else:
|
| 391 |
+
target_dtype = self.qkv_proj.weight.dtype
|
| 392 |
+
|
| 393 |
+
logger.warning_once(
|
| 394 |
+
f"The input hidden states seems to be silently casted in float32, this might be related to"
|
| 395 |
+
f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
|
| 396 |
+
f" {target_dtype}."
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
query_states = query_states.to(target_dtype)
|
| 400 |
+
key_states = key_states.to(target_dtype)
|
| 401 |
+
value_states = value_states.to(target_dtype)
|
| 402 |
+
|
| 403 |
+
# Reashape to the expected shape for Flash Attention
|
| 404 |
+
query_states = query_states.transpose(1, 2)
|
| 405 |
+
key_states = key_states.transpose(1, 2)
|
| 406 |
+
value_states = value_states.transpose(1, 2)
|
| 407 |
+
|
| 408 |
+
if (
|
| 409 |
+
self.config.use_sliding_window
|
| 410 |
+
and getattr(self.config, "sliding_window", None) is not None
|
| 411 |
+
and self.layer_idx >= self.config.max_window_layers
|
| 412 |
+
):
|
| 413 |
+
sliding_window = self.config.sliding_window
|
| 414 |
+
else:
|
| 415 |
+
sliding_window = None
|
| 416 |
+
|
| 417 |
+
attn_output = _flash_attention_forward(
|
| 418 |
+
query_states,
|
| 419 |
+
key_states,
|
| 420 |
+
value_states,
|
| 421 |
+
attention_mask,
|
| 422 |
+
q_len,
|
| 423 |
+
dropout=dropout_rate,
|
| 424 |
+
sliding_window=sliding_window,
|
| 425 |
+
is_causal=self.is_causal,
|
| 426 |
+
use_top_left_mask=self._flash_attn_uses_top_left_mask,
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
|
| 430 |
+
attn_output = self.o_proj(attn_output)
|
| 431 |
+
|
| 432 |
+
if not output_attentions:
|
| 433 |
+
attn_weights = None
|
| 434 |
+
|
| 435 |
+
return attn_output, attn_weights, past_key_value
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
# Copied from transformers.models.mixtral.modeling_mixtral.MixtralSdpaAttention with Mixtral->Zhinao
|
| 439 |
+
class ZhinaoSdpaAttention(ZhinaoAttention):
|
| 440 |
+
"""
|
| 441 |
+
Zhinao attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
|
| 442 |
+
`ZhinaoAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
|
| 443 |
+
SDPA API.
|
| 444 |
+
"""
|
| 445 |
+
|
| 446 |
+
# Adapted from ZhinaoAttention.forward
|
| 447 |
+
def forward(
|
| 448 |
+
self,
|
| 449 |
+
hidden_states: torch.Tensor,
|
| 450 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 451 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 452 |
+
past_key_value: Optional[Cache] = None,
|
| 453 |
+
output_attentions: bool = False,
|
| 454 |
+
use_cache: bool = False,
|
| 455 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 456 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
| 457 |
+
if output_attentions:
|
| 458 |
+
# TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
|
| 459 |
+
logger.warning_once(
|
| 460 |
+
"ZhinaoModel is using ZhinaoSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
|
| 461 |
+
'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.'
|
| 462 |
+
)
|
| 463 |
+
return super().forward(
|
| 464 |
+
hidden_states=hidden_states,
|
| 465 |
+
attention_mask=attention_mask,
|
| 466 |
+
position_ids=position_ids,
|
| 467 |
+
past_key_value=past_key_value,
|
| 468 |
+
output_attentions=output_attentions,
|
| 469 |
+
use_cache=use_cache,
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
bsz, q_len, _ = hidden_states.size()
|
| 473 |
+
|
| 474 |
+
mixed_x_layer = self.qkv_proj(hidden_states)
|
| 475 |
+
new_tensor_shape = mixed_x_layer.size()[:-1] + \
|
| 476 |
+
(self.num_key_value_heads, ((self.num_heads // self.num_key_value_heads + 2) * self.head_dim))
|
| 477 |
+
mixed_x_layer = mixed_x_layer.view(*new_tensor_shape)
|
| 478 |
+
query, key_states, value_states = torch.split(
|
| 479 |
+
mixed_x_layer,
|
| 480 |
+
[self.num_heads // self.num_key_value_heads * self.head_dim, self.head_dim, self.head_dim],
|
| 481 |
+
dim=3
|
| 482 |
+
)
|
| 483 |
+
# [sq, b, ng, np/ng * hn] -> [sq, b, np, hn]
|
| 484 |
+
query_states = query.contiguous().view(query.size(0), query.size(1), -1, self.head_dim)
|
| 485 |
+
|
| 486 |
+
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
| 487 |
+
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
| 488 |
+
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
| 489 |
+
|
| 490 |
+
kv_seq_len = key_states.shape[-2]
|
| 491 |
+
if past_key_value is not None:
|
| 492 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
| 493 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
| 494 |
+
|
| 495 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
| 496 |
+
|
| 497 |
+
if past_key_value is not None:
|
| 498 |
+
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models
|
| 499 |
+
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
| 500 |
+
|
| 501 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
| 502 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
| 503 |
+
|
| 504 |
+
causal_mask = attention_mask
|
| 505 |
+
if attention_mask is not None: # no matter the length, we just slice it
|
| 506 |
+
causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
|
| 507 |
+
|
| 508 |
+
# SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
|
| 509 |
+
# Reference: https://github.com/pytorch/pytorch/issues/112577.
|
| 510 |
+
if query_states.device.type == "cuda" and attention_mask is not None:
|
| 511 |
+
query_states = query_states.contiguous()
|
| 512 |
+
key_states = key_states.contiguous()
|
| 513 |
+
value_states = value_states.contiguous()
|
| 514 |
+
|
| 515 |
+
# We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of an inline conditional assignment
|
| 516 |
+
# in SDPA to support both torch.compile's dynamic shapes and full graph options. An inline conditional prevents dynamic shapes from compiling.
|
| 517 |
+
# 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.
|
| 518 |
+
is_causal = True if causal_mask is None and q_len > 1 else False
|
| 519 |
+
|
| 520 |
+
attn_output = torch.nn.functional.scaled_dot_product_attention(
|
| 521 |
+
query_states,
|
| 522 |
+
key_states,
|
| 523 |
+
value_states,
|
| 524 |
+
attn_mask=causal_mask,
|
| 525 |
+
dropout_p=self.attention_dropout if self.training else 0.0,
|
| 526 |
+
is_causal=is_causal,
|
| 527 |
+
)
|
| 528 |
+
|
| 529 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
| 530 |
+
attn_output = attn_output.view(bsz, q_len, self.hidden_size)
|
| 531 |
+
|
| 532 |
+
attn_output = self.o_proj(attn_output)
|
| 533 |
+
|
| 534 |
+
return attn_output, None, past_key_value
|
| 535 |
+
|
| 536 |
+
|
| 537 |
+
Zhinao_ATTENTION_CLASSES = {
|
| 538 |
+
"eager": ZhinaoAttention,
|
| 539 |
+
"flash_attention_2": ZhinaoFlashAttention2,
|
| 540 |
+
"sdpa": ZhinaoSdpaAttention,
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
|
| 544 |
+
class ZhinaoDecoderLayer(nn.Module):
|
| 545 |
+
def __init__(self, config: ZhinaoConfig, layer_idx: int):
|
| 546 |
+
super().__init__()
|
| 547 |
+
self.hidden_size = config.hidden_size
|
| 548 |
+
|
| 549 |
+
if config.sliding_window and config._attn_implementation != "flash_attention_2":
|
| 550 |
+
logger.warning_once(
|
| 551 |
+
f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; "
|
| 552 |
+
"unexpected results may be encountered."
|
| 553 |
+
)
|
| 554 |
+
if layer_idx == 0:
|
| 555 |
+
print("_attn_implementation:", config._attn_implementation)
|
| 556 |
+
self.self_attn = Zhinao_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx)
|
| 557 |
+
|
| 558 |
+
self.mlp = ZhinaoMLP(config)
|
| 559 |
+
self.input_layernorm = ZhinaoRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 560 |
+
self.post_attention_layernorm = ZhinaoRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 561 |
+
|
| 562 |
+
def forward(
|
| 563 |
+
self,
|
| 564 |
+
hidden_states: torch.Tensor,
|
| 565 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 566 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 567 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
| 568 |
+
output_attentions: Optional[bool] = False,
|
| 569 |
+
use_cache: Optional[bool] = False,
|
| 570 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 571 |
+
**kwargs,
|
| 572 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 573 |
+
"""
|
| 574 |
+
Args:
|
| 575 |
+
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
| 576 |
+
attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
|
| 577 |
+
`(batch, sequence_length)` where padding elements are indicated by 0.
|
| 578 |
+
output_attentions (`bool`, *optional*):
|
| 579 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
| 580 |
+
returned tensors for more detail.
|
| 581 |
+
use_cache (`bool`, *optional*):
|
| 582 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
| 583 |
+
(see `past_key_values`).
|
| 584 |
+
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
|
| 585 |
+
cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
|
| 586 |
+
Indices depicting the position of the input sequence tokens in the sequence.
|
| 587 |
+
kwargs (`dict`, *optional*):
|
| 588 |
+
Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code
|
| 589 |
+
into the model
|
| 590 |
+
"""
|
| 591 |
+
|
| 592 |
+
residual = hidden_states
|
| 593 |
+
|
| 594 |
+
hidden_states = self.input_layernorm(hidden_states)
|
| 595 |
+
|
| 596 |
+
# Self Attention
|
| 597 |
+
hidden_states, self_attn_weights, present_key_value = self.self_attn(
|
| 598 |
+
hidden_states=hidden_states,
|
| 599 |
+
attention_mask=attention_mask,
|
| 600 |
+
position_ids=position_ids,
|
| 601 |
+
past_key_value=past_key_value,
|
| 602 |
+
output_attentions=output_attentions,
|
| 603 |
+
use_cache=use_cache,
|
| 604 |
+
cache_position=cache_position,
|
| 605 |
+
)
|
| 606 |
+
hidden_states = residual + hidden_states
|
| 607 |
+
|
| 608 |
+
# Fully Connected
|
| 609 |
+
residual = hidden_states
|
| 610 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
| 611 |
+
hidden_states = self.mlp(hidden_states)
|
| 612 |
+
hidden_states = residual + hidden_states
|
| 613 |
+
|
| 614 |
+
outputs = (hidden_states,)
|
| 615 |
+
|
| 616 |
+
if output_attentions:
|
| 617 |
+
outputs += (self_attn_weights,)
|
| 618 |
+
|
| 619 |
+
if use_cache:
|
| 620 |
+
outputs += (present_key_value,)
|
| 621 |
+
|
| 622 |
+
return outputs
|
| 623 |
+
|
| 624 |
+
class ZhinaoPreTrainedModel(PreTrainedModel):
|
| 625 |
+
config_class = ZhinaoConfig
|
| 626 |
+
base_model_prefix = "model"
|
| 627 |
+
supports_gradient_checkpointing = True
|
| 628 |
+
_no_split_modules = ["ZhinaoDecoderLayer"]
|
| 629 |
+
_skip_keys_device_placement = "past_key_values"
|
| 630 |
+
_supports_flash_attn_2 = True
|
| 631 |
+
_supports_sdpa = True
|
| 632 |
+
_supports_cache_class = True
|
| 633 |
+
|
| 634 |
+
def _init_weights(self, module):
|
| 635 |
+
std = self.config.initializer_range
|
| 636 |
+
if isinstance(module, nn.Linear):
|
| 637 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
| 638 |
+
if module.bias is not None:
|
| 639 |
+
module.bias.data.zero_()
|
| 640 |
+
elif isinstance(module, nn.Embedding):
|
| 641 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
| 642 |
+
if module.padding_idx is not None:
|
| 643 |
+
module.weight.data[module.padding_idx].zero_()
|
| 644 |
+
|
| 645 |
+
|
| 646 |
+
class ZhinaoModel(ZhinaoPreTrainedModel):
|
| 647 |
+
"""
|
| 648 |
+
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`ZhinaoDecoderLayer`]
|
| 649 |
+
|
| 650 |
+
Args:
|
| 651 |
+
config: ZhinaoConfig
|
| 652 |
+
"""
|
| 653 |
+
|
| 654 |
+
def __init__(self, config: ZhinaoConfig):
|
| 655 |
+
super().__init__(config)
|
| 656 |
+
self.padding_idx = config.pad_token_id
|
| 657 |
+
self.vocab_size = config.vocab_size
|
| 658 |
+
|
| 659 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 660 |
+
self.layers = nn.ModuleList(
|
| 661 |
+
[ZhinaoDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
| 662 |
+
)
|
| 663 |
+
self._attn_implementation = config._attn_implementation
|
| 664 |
+
self.norm = ZhinaoRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 665 |
+
|
| 666 |
+
self.gradient_checkpointing = False
|
| 667 |
+
# Initialize weights and apply final processing
|
| 668 |
+
self.post_init()
|
| 669 |
+
|
| 670 |
+
def get_input_embeddings(self):
|
| 671 |
+
return self.embed_tokens
|
| 672 |
+
|
| 673 |
+
def set_input_embeddings(self, value):
|
| 674 |
+
self.embed_tokens = value
|
| 675 |
+
|
| 676 |
+
def forward(
|
| 677 |
+
self,
|
| 678 |
+
input_ids: torch.LongTensor = None,
|
| 679 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 680 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 681 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
| 682 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 683 |
+
use_cache: Optional[bool] = None,
|
| 684 |
+
output_attentions: Optional[bool] = None,
|
| 685 |
+
output_hidden_states: Optional[bool] = None,
|
| 686 |
+
return_dict: Optional[bool] = None,
|
| 687 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 688 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
| 689 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 690 |
+
output_hidden_states = (
|
| 691 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 692 |
+
)
|
| 693 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
| 694 |
+
|
| 695 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 696 |
+
|
| 697 |
+
if (input_ids is None) ^ (inputs_embeds is not None):
|
| 698 |
+
raise ValueError(
|
| 699 |
+
"You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one"
|
| 700 |
+
)
|
| 701 |
+
|
| 702 |
+
if self.gradient_checkpointing and self.training:
|
| 703 |
+
if use_cache:
|
| 704 |
+
logger.warning_once(
|
| 705 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
| 706 |
+
)
|
| 707 |
+
use_cache = False
|
| 708 |
+
|
| 709 |
+
use_legacy_cache = False
|
| 710 |
+
if use_cache and not isinstance(past_key_values, Cache) and not self.training:
|
| 711 |
+
use_legacy_cache = True
|
| 712 |
+
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
|
| 713 |
+
logger.warning_once(
|
| 714 |
+
"We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. "
|
| 715 |
+
"Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)"
|
| 716 |
+
)
|
| 717 |
+
|
| 718 |
+
if inputs_embeds is None:
|
| 719 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
| 720 |
+
|
| 721 |
+
if cache_position is None:
|
| 722 |
+
past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
|
| 723 |
+
cache_position = torch.arange(
|
| 724 |
+
past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
|
| 725 |
+
)
|
| 726 |
+
if position_ids is None:
|
| 727 |
+
position_ids = cache_position.unsqueeze(0)
|
| 728 |
+
|
| 729 |
+
causal_mask = self._update_causal_mask(
|
| 730 |
+
attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions
|
| 731 |
+
)
|
| 732 |
+
|
| 733 |
+
hidden_states = inputs_embeds
|
| 734 |
+
|
| 735 |
+
# decoder layers
|
| 736 |
+
all_hidden_states = () if output_hidden_states else None
|
| 737 |
+
all_self_attns = () if output_attentions else None
|
| 738 |
+
next_decoder_cache = None
|
| 739 |
+
|
| 740 |
+
for decoder_layer in self.layers:
|
| 741 |
+
if output_hidden_states:
|
| 742 |
+
all_hidden_states += (hidden_states,)
|
| 743 |
+
|
| 744 |
+
if self.gradient_checkpointing and self.training:
|
| 745 |
+
layer_outputs = self._gradient_checkpointing_func(
|
| 746 |
+
decoder_layer.__call__,
|
| 747 |
+
hidden_states,
|
| 748 |
+
causal_mask,
|
| 749 |
+
position_ids,
|
| 750 |
+
past_key_values,
|
| 751 |
+
output_attentions,
|
| 752 |
+
use_cache,
|
| 753 |
+
cache_position,
|
| 754 |
+
)
|
| 755 |
+
else:
|
| 756 |
+
layer_outputs = decoder_layer(
|
| 757 |
+
hidden_states,
|
| 758 |
+
attention_mask=causal_mask,
|
| 759 |
+
position_ids=position_ids,
|
| 760 |
+
past_key_value=past_key_values,
|
| 761 |
+
output_attentions=output_attentions,
|
| 762 |
+
use_cache=use_cache,
|
| 763 |
+
cache_position=cache_position,
|
| 764 |
+
)
|
| 765 |
+
|
| 766 |
+
hidden_states = layer_outputs[0]
|
| 767 |
+
|
| 768 |
+
if use_cache:
|
| 769 |
+
next_decoder_cache = layer_outputs[2 if output_attentions else 1]
|
| 770 |
+
|
| 771 |
+
if output_attentions:
|
| 772 |
+
all_self_attns += (layer_outputs[1],)
|
| 773 |
+
|
| 774 |
+
hidden_states = self.norm(hidden_states)
|
| 775 |
+
|
| 776 |
+
# add hidden states from the last decoder layer
|
| 777 |
+
if output_hidden_states:
|
| 778 |
+
all_hidden_states += (hidden_states,)
|
| 779 |
+
|
| 780 |
+
next_cache = None
|
| 781 |
+
if use_cache:
|
| 782 |
+
next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
|
| 783 |
+
|
| 784 |
+
if not return_dict:
|
| 785 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
|
| 786 |
+
return BaseModelOutputWithPast(
|
| 787 |
+
last_hidden_state=hidden_states,
|
| 788 |
+
past_key_values=next_cache,
|
| 789 |
+
hidden_states=all_hidden_states,
|
| 790 |
+
attentions=all_self_attns,
|
| 791 |
+
)
|
| 792 |
+
|
| 793 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask
|
| 794 |
+
def _update_causal_mask(
|
| 795 |
+
self,
|
| 796 |
+
attention_mask: torch.Tensor,
|
| 797 |
+
input_tensor: torch.Tensor,
|
| 798 |
+
cache_position: torch.Tensor,
|
| 799 |
+
past_key_values: Cache,
|
| 800 |
+
output_attentions: bool,
|
| 801 |
+
):
|
| 802 |
+
# TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static
|
| 803 |
+
# KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes.
|
| 804 |
+
# (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using
|
| 805 |
+
# `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114
|
| 806 |
+
|
| 807 |
+
if self.config._attn_implementation == "flash_attention_2":
|
| 808 |
+
if attention_mask is not None and 0.0 in attention_mask:
|
| 809 |
+
return attention_mask
|
| 810 |
+
return None
|
| 811 |
+
|
| 812 |
+
# For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in
|
| 813 |
+
# order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail
|
| 814 |
+
# to infer the attention mask.
|
| 815 |
+
past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
|
| 816 |
+
using_static_cache = isinstance(past_key_values, StaticCache)
|
| 817 |
+
|
| 818 |
+
# When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward
|
| 819 |
+
if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions:
|
| 820 |
+
if AttentionMaskConverter._ignore_causal_mask_sdpa(
|
| 821 |
+
attention_mask,
|
| 822 |
+
inputs_embeds=input_tensor,
|
| 823 |
+
past_key_values_length=past_seen_tokens,
|
| 824 |
+
is_training=self.training,
|
| 825 |
+
):
|
| 826 |
+
return None
|
| 827 |
+
|
| 828 |
+
dtype, device = input_tensor.dtype, input_tensor.device
|
| 829 |
+
min_dtype = torch.finfo(dtype).min
|
| 830 |
+
sequence_length = input_tensor.shape[1]
|
| 831 |
+
if using_static_cache:
|
| 832 |
+
target_length = past_key_values.get_max_length()
|
| 833 |
+
else:
|
| 834 |
+
target_length = (
|
| 835 |
+
attention_mask.shape[-1]
|
| 836 |
+
if isinstance(attention_mask, torch.Tensor)
|
| 837 |
+
else past_seen_tokens + sequence_length + 1
|
| 838 |
+
)
|
| 839 |
+
|
| 840 |
+
if attention_mask is not None and attention_mask.dim() == 4:
|
| 841 |
+
# in this case we assume that the mask comes already in inverted form and requires no inversion or slicing
|
| 842 |
+
if attention_mask.max() != 0:
|
| 843 |
+
raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`")
|
| 844 |
+
causal_mask = attention_mask
|
| 845 |
+
else:
|
| 846 |
+
causal_mask = torch.full(
|
| 847 |
+
(sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device
|
| 848 |
+
)
|
| 849 |
+
if sequence_length != 1:
|
| 850 |
+
causal_mask = torch.triu(causal_mask, diagonal=1)
|
| 851 |
+
causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1)
|
| 852 |
+
causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1)
|
| 853 |
+
if attention_mask is not None:
|
| 854 |
+
causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit
|
| 855 |
+
mask_length = attention_mask.shape[-1]
|
| 856 |
+
padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :]
|
| 857 |
+
padding_mask = padding_mask == 0
|
| 858 |
+
causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(
|
| 859 |
+
padding_mask, min_dtype
|
| 860 |
+
)
|
| 861 |
+
if (
|
| 862 |
+
self.config._attn_implementation == "sdpa"
|
| 863 |
+
and attention_mask is not None
|
| 864 |
+
and attention_mask.device.type == "cuda"
|
| 865 |
+
and not output_attentions
|
| 866 |
+
):
|
| 867 |
+
# Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when
|
| 868 |
+
# using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path.
|
| 869 |
+
# Details: https://github.com/pytorch/pytorch/issues/110213
|
| 870 |
+
causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype)
|
| 871 |
+
|
| 872 |
+
return causal_mask
|
| 873 |
+
|
| 874 |
+
|
| 875 |
+
class ZhinaoForCausalLM(ZhinaoPreTrainedModel):
|
| 876 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 877 |
+
|
| 878 |
+
def __init__(self, config):
|
| 879 |
+
super().__init__(config)
|
| 880 |
+
self.model = ZhinaoModel(config)
|
| 881 |
+
self.vocab_size = config.vocab_size
|
| 882 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 883 |
+
|
| 884 |
+
# Initialize weights and apply final processing
|
| 885 |
+
self.post_init()
|
| 886 |
+
|
| 887 |
+
def get_input_embeddings(self):
|
| 888 |
+
return self.model.embed_tokens
|
| 889 |
+
|
| 890 |
+
def set_input_embeddings(self, value):
|
| 891 |
+
self.model.embed_tokens = value
|
| 892 |
+
|
| 893 |
+
def get_output_embeddings(self):
|
| 894 |
+
return self.lm_head
|
| 895 |
+
|
| 896 |
+
def set_output_embeddings(self, new_embeddings):
|
| 897 |
+
self.lm_head = new_embeddings
|
| 898 |
+
|
| 899 |
+
def set_decoder(self, decoder):
|
| 900 |
+
self.model = decoder
|
| 901 |
+
|
| 902 |
+
def get_decoder(self):
|
| 903 |
+
return self.model
|
| 904 |
+
|
| 905 |
+
def forward(
|
| 906 |
+
self,
|
| 907 |
+
input_ids: torch.LongTensor = None,
|
| 908 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 909 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 910 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
| 911 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 912 |
+
labels: Optional[torch.LongTensor] = None,
|
| 913 |
+
use_cache: Optional[bool] = None,
|
| 914 |
+
output_attentions: Optional[bool] = None,
|
| 915 |
+
output_hidden_states: Optional[bool] = None,
|
| 916 |
+
return_dict: Optional[bool] = None,
|
| 917 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 918 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 919 |
+
|
| 920 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 921 |
+
output_hidden_states = (
|
| 922 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 923 |
+
)
|
| 924 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 925 |
+
|
| 926 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
| 927 |
+
outputs = self.model(
|
| 928 |
+
input_ids=input_ids,
|
| 929 |
+
attention_mask=attention_mask,
|
| 930 |
+
position_ids=position_ids,
|
| 931 |
+
past_key_values=past_key_values,
|
| 932 |
+
inputs_embeds=inputs_embeds,
|
| 933 |
+
use_cache=use_cache,
|
| 934 |
+
output_attentions=output_attentions,
|
| 935 |
+
output_hidden_states=output_hidden_states,
|
| 936 |
+
return_dict=return_dict,
|
| 937 |
+
cache_position=cache_position,
|
| 938 |
+
)
|
| 939 |
+
|
| 940 |
+
hidden_states = outputs[0]
|
| 941 |
+
logits = self.lm_head(hidden_states)
|
| 942 |
+
logits = logits.float()
|
| 943 |
+
|
| 944 |
+
loss = None
|
| 945 |
+
if labels is not None:
|
| 946 |
+
# Shift so that tokens < n predict n
|
| 947 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 948 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 949 |
+
# Flatten the tokens
|
| 950 |
+
loss_fct = CrossEntropyLoss()
|
| 951 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
| 952 |
+
shift_labels = shift_labels.view(-1)
|
| 953 |
+
# Enable model parallelism
|
| 954 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
| 955 |
+
loss = loss_fct(shift_logits, shift_labels)
|
| 956 |
+
|
| 957 |
+
if not return_dict:
|
| 958 |
+
output = (logits,) + outputs[1:]
|
| 959 |
+
return (loss,) + output if loss is not None else output
|
| 960 |
+
|
| 961 |
+
return CausalLMOutputWithPast(
|
| 962 |
+
loss=loss,
|
| 963 |
+
logits=logits,
|
| 964 |
+
past_key_values=outputs.past_key_values,
|
| 965 |
+
hidden_states=outputs.hidden_states,
|
| 966 |
+
attentions=outputs.attentions,
|
| 967 |
+
)
|
| 968 |
+
|
| 969 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.prepare_inputs_for_generation
|
| 970 |
+
def prepare_inputs_for_generation(
|
| 971 |
+
self,
|
| 972 |
+
input_ids,
|
| 973 |
+
past_key_values=None,
|
| 974 |
+
attention_mask=None,
|
| 975 |
+
inputs_embeds=None,
|
| 976 |
+
cache_position=None,
|
| 977 |
+
position_ids=None,
|
| 978 |
+
use_cache=True,
|
| 979 |
+
**kwargs,
|
| 980 |
+
):
|
| 981 |
+
# If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens
|
| 982 |
+
# Exception 1: when passing input_embeds, input_ids may be missing entries
|
| 983 |
+
# Exception 2: some generation methods do special slicing of input_ids, so we don't need to do it here
|
| 984 |
+
if past_key_values is not None:
|
| 985 |
+
if inputs_embeds is not None: # Exception 1
|
| 986 |
+
input_ids = input_ids[:, -cache_position.shape[0] :]
|
| 987 |
+
elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2)
|
| 988 |
+
input_ids = input_ids[:, cache_position]
|
| 989 |
+
|
| 990 |
+
if attention_mask is not None and position_ids is None:
|
| 991 |
+
# create position_ids on the fly for batch generation
|
| 992 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
| 993 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
| 994 |
+
if past_key_values:
|
| 995 |
+
position_ids = position_ids[:, -input_ids.shape[1] :]
|
| 996 |
+
|
| 997 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 998 |
+
if inputs_embeds is not None and cache_position[0] == 0:
|
| 999 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
| 1000 |
+
else:
|
| 1001 |
+
model_inputs = {"input_ids": input_ids.contiguous()} # `contiguous()` needed for compilation use cases
|
| 1002 |
+
|
| 1003 |
+
model_inputs.update(
|
| 1004 |
+
{
|
| 1005 |
+
"position_ids": position_ids,
|
| 1006 |
+
"cache_position": cache_position,
|
| 1007 |
+
"past_key_values": past_key_values,
|
| 1008 |
+
"use_cache": use_cache,
|
| 1009 |
+
"attention_mask": attention_mask,
|
| 1010 |
+
}
|
| 1011 |
+
)
|
| 1012 |
+
return model_inputs
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import base64
|
| 4 |
+
import tiktoken
|
| 5 |
+
from typing import Collection, Optional, Dict, List, Set, Tuple, Union
|
| 6 |
+
from transformers import PreTrainedTokenizer
|
| 7 |
+
from transformers.utils import PaddingStrategy
|
| 8 |
+
from transformers.tokenization_utils import PreTrainedTokenizer
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
PAT_STR = r"""(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"""
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class SPTokenizer:
|
| 15 |
+
def __init__(self, model_path):
|
| 16 |
+
self.vocab_file = model_path
|
| 17 |
+
self.pad_token = '<pad>'
|
| 18 |
+
self.unk_token = '<unk>'
|
| 19 |
+
self.mask_token = '<mask>'
|
| 20 |
+
self.eod_token = '<eod>'
|
| 21 |
+
self.eop_token = '<eop>'
|
| 22 |
+
self.im_start_token = '<|im_start|>'
|
| 23 |
+
self.im_end_token = '<|im_end|>'
|
| 24 |
+
self.think_eob = "<think>"
|
| 25 |
+
self.think_eod = "</think>"
|
| 26 |
+
|
| 27 |
+
## special_tokens
|
| 28 |
+
self.SPECIAL_TOKENS = (
|
| 29 |
+
self.pad_token,
|
| 30 |
+
self.unk_token,
|
| 31 |
+
self.mask_token,
|
| 32 |
+
self.eod_token,
|
| 33 |
+
self.eop_token,
|
| 34 |
+
'[space2]', '[space3]', '[space4]', '[space8]',
|
| 35 |
+
self.im_start_token, self.im_end_token,
|
| 36 |
+
self.think_eob, self.think_eod
|
| 37 |
+
)
|
| 38 |
+
self.bulid_tokenizer()
|
| 39 |
+
self.out = self.output_core_token()
|
| 40 |
+
|
| 41 |
+
self.token2strs = {
|
| 42 |
+
"[space2]": " ",
|
| 43 |
+
"[space3]": " ",
|
| 44 |
+
"[space4]": " ",
|
| 45 |
+
"[space8]": " ",
|
| 46 |
+
}
|
| 47 |
+
self.str2tokens = {v: k for k, v in self.token2strs.items()}
|
| 48 |
+
self.sorted_strs = sorted(list(self.str2tokens.keys()),
|
| 49 |
+
key=lambda x: len(x), reverse=True)
|
| 50 |
+
|
| 51 |
+
## skip_special_tokens
|
| 52 |
+
self.decode_skip_special_tokens = [
|
| 53 |
+
self.pad_token,
|
| 54 |
+
self.unk_token,
|
| 55 |
+
self.mask_token,
|
| 56 |
+
self.eod_token,
|
| 57 |
+
self.eop_token,
|
| 58 |
+
self.im_start_token,
|
| 59 |
+
self.im_end_token,
|
| 60 |
+
self.think_eob,
|
| 61 |
+
self.think_eod]
|
| 62 |
+
self.decode_skip_special_tokens_ids = [self.convert_token_to_id(token) for token in self.decode_skip_special_tokens]
|
| 63 |
+
|
| 64 |
+
def _load_tiktoken_bpe(self, tiktoken_bpe_file: str):
|
| 65 |
+
with open(tiktoken_bpe_file, "rb") as f:
|
| 66 |
+
contents = f.read()
|
| 67 |
+
return {
|
| 68 |
+
base64.b64decode(token): int(rank)
|
| 69 |
+
for token, rank in (line.split() for line in contents.splitlines() if line)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
def bulid_tokenizer(self):
|
| 73 |
+
mergeable_ranks = self._load_tiktoken_bpe(self.vocab_file)
|
| 74 |
+
special_tokens = {
|
| 75 |
+
token: index
|
| 76 |
+
for index, token in enumerate(
|
| 77 |
+
self.SPECIAL_TOKENS, start=len(mergeable_ranks)
|
| 78 |
+
)
|
| 79 |
+
}
|
| 80 |
+
encode = tiktoken.Encoding(
|
| 81 |
+
"zhinao",
|
| 82 |
+
pat_str=PAT_STR,
|
| 83 |
+
mergeable_ranks=mergeable_ranks,
|
| 84 |
+
special_tokens=special_tokens
|
| 85 |
+
)
|
| 86 |
+
decoder = {v: k for k, v in mergeable_ranks.items()}
|
| 87 |
+
decoder.update({v: k for k, v in special_tokens.items()})
|
| 88 |
+
decoder_token2id = {v: k for k, v in decoder.items()}
|
| 89 |
+
|
| 90 |
+
self.tokenizer = encode
|
| 91 |
+
self.decoder = decoder
|
| 92 |
+
self.decoder_token2id = decoder_token2id
|
| 93 |
+
self.num_tokens = len(mergeable_ranks) + len(self.SPECIAL_TOKENS)
|
| 94 |
+
|
| 95 |
+
def output_core_token(self):
|
| 96 |
+
"""output special tokens"""
|
| 97 |
+
out = {}
|
| 98 |
+
for t in self.SPECIAL_TOKENS:
|
| 99 |
+
out[t] = self.convert_token_to_id(t)
|
| 100 |
+
return out
|
| 101 |
+
|
| 102 |
+
def tokenize(
|
| 103 |
+
self,
|
| 104 |
+
text,
|
| 105 |
+
allowed_special: Union[Set, str] = "all",
|
| 106 |
+
disallowed_special: Union[Collection, str] = ()):
|
| 107 |
+
tokens = []
|
| 108 |
+
text = self.convert(text)
|
| 109 |
+
for idx in self.tokenizer.encode(text, allowed_special=allowed_special, disallowed_special=disallowed_special):
|
| 110 |
+
tokens.append(self.decoder[idx])
|
| 111 |
+
return tokens
|
| 112 |
+
|
| 113 |
+
def encode(self, text, allowed_special="all", disallowed_special=()):
|
| 114 |
+
"""text to id"""
|
| 115 |
+
text = self.convert(text)
|
| 116 |
+
return self.tokenizer.encode(text, allowed_special=allowed_special, disallowed_special=disallowed_special)
|
| 117 |
+
|
| 118 |
+
def decode(self, ids, errors="replace"):
|
| 119 |
+
"""id to text"""
|
| 120 |
+
text = self.tokenizer.decode(ids, errors=errors)
|
| 121 |
+
return self.deconvert(text)
|
| 122 |
+
|
| 123 |
+
def decode_tokens(self, tokens: List[str]) -> str:
|
| 124 |
+
"""
|
| 125 |
+
Converts a sequence of tokens in a single string.
|
| 126 |
+
"""
|
| 127 |
+
text = ""
|
| 128 |
+
temp = b""
|
| 129 |
+
for t in tokens:
|
| 130 |
+
if isinstance(t, str):
|
| 131 |
+
if temp:
|
| 132 |
+
text += temp.decode("utf-8", errors="ignore")
|
| 133 |
+
temp = b""
|
| 134 |
+
text += t
|
| 135 |
+
elif isinstance(t, bytes):
|
| 136 |
+
temp += t
|
| 137 |
+
else:
|
| 138 |
+
raise TypeError("token should only be of type bytes or str")
|
| 139 |
+
if temp:
|
| 140 |
+
text += temp.decode("utf-8", errors="ignore")
|
| 141 |
+
return self.deconvert(text)
|
| 142 |
+
|
| 143 |
+
def convert_id_to_token(self, idx):
|
| 144 |
+
return self.decoder[idx]
|
| 145 |
+
|
| 146 |
+
def convert_token_to_id(self, token):
|
| 147 |
+
return self.decoder_token2id[token]
|
| 148 |
+
|
| 149 |
+
def convert(self, text):
|
| 150 |
+
"""将文本的特殊字符转换成特殊token"""
|
| 151 |
+
for k in ["[br]", "<br>"]:
|
| 152 |
+
text = text.replace(k, "\n")
|
| 153 |
+
for k in self.sorted_strs:
|
| 154 |
+
if k in text:
|
| 155 |
+
text = text.replace(k, self.str2tokens[k])
|
| 156 |
+
return text
|
| 157 |
+
|
| 158 |
+
def deconvert(self, text):
|
| 159 |
+
"""将解码文本恢复原始字符"""
|
| 160 |
+
for t in self.token2strs:
|
| 161 |
+
if t in text:
|
| 162 |
+
text = text.replace(t, self.token2strs[t])
|
| 163 |
+
return text
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
class ZhinaoTokenizer(PreTrainedTokenizer):
|
| 167 |
+
vocab_files_names = {"vocab_file": "vocab/360.tiktoken"}
|
| 168 |
+
model_input_names = ["input_ids", "attention_mask"]
|
| 169 |
+
|
| 170 |
+
def __init__(self, vocab_file, padding_side="left", clean_up_tokenization_spaces=False, **kwargs):
|
| 171 |
+
self.name = "ZhinaoTokenizer"
|
| 172 |
+
self.vocab_file = vocab_file
|
| 173 |
+
self.tokenizer = SPTokenizer(model_path=vocab_file)
|
| 174 |
+
try:
|
| 175 |
+
kwargs.pop('eos_token')
|
| 176 |
+
kwargs.pop('pad_token')
|
| 177 |
+
kwargs.pop('unk_token')
|
| 178 |
+
except:
|
| 179 |
+
pass
|
| 180 |
+
super().__init__(padding_side=padding_side, clean_up_tokenization_spaces=clean_up_tokenization_spaces, **kwargs)
|
| 181 |
+
self.pad_token_id = self.tokenizer.convert_token_to_id(self.tokenizer.pad_token)
|
| 182 |
+
self.eod_id = self.tokenizer.convert_token_to_id(self.tokenizer.eod_token)
|
| 183 |
+
self.im_start_id = self.tokenizer.convert_token_to_id(self.tokenizer.im_start_token)
|
| 184 |
+
self.im_end_id = self.tokenizer.convert_token_to_id(self.tokenizer.im_end_token)
|
| 185 |
+
|
| 186 |
+
@property
|
| 187 |
+
def eop_token(self) -> str:
|
| 188 |
+
return self.tokenizer.eop_token
|
| 189 |
+
|
| 190 |
+
@property
|
| 191 |
+
def eop_token_id(self):
|
| 192 |
+
return self.tokenizer.convert_token_to_id(self.tokenizer.eop_token)
|
| 193 |
+
|
| 194 |
+
@property
|
| 195 |
+
def eos_token_id(self):
|
| 196 |
+
return self.tokenizer.convert_token_to_id(self.tokenizer.eod_token)
|
| 197 |
+
|
| 198 |
+
@property
|
| 199 |
+
def vocab_size(self):
|
| 200 |
+
return self.tokenizer.num_tokens
|
| 201 |
+
|
| 202 |
+
def get_vocab(self):
|
| 203 |
+
""" Returns vocab as a dict """
|
| 204 |
+
vocab = {self._convert_id_to_token(i): i for i in range(self.vocab_size)}
|
| 205 |
+
vocab.update(self.added_tokens_encoder)
|
| 206 |
+
return vocab
|
| 207 |
+
|
| 208 |
+
def tokenize(
|
| 209 |
+
self,
|
| 210 |
+
text: str,
|
| 211 |
+
allowed_special: Union[Set, str] = "all",
|
| 212 |
+
disallowed_special: Union[Collection, str] = (),
|
| 213 |
+
split_special_tokens=False,
|
| 214 |
+
) -> List[Union[bytes, str]]:
|
| 215 |
+
tokens = []
|
| 216 |
+
for t in self.tokenizer.encode(
|
| 217 |
+
text, allowed_special=allowed_special, disallowed_special=disallowed_special
|
| 218 |
+
):
|
| 219 |
+
tokens.append(self.tokenizer.decoder[t])
|
| 220 |
+
return tokens
|
| 221 |
+
|
| 222 |
+
def _decode(
|
| 223 |
+
self,
|
| 224 |
+
token_ids: Union[int, List[int]],
|
| 225 |
+
skip_special_tokens: bool = False,
|
| 226 |
+
errors: str = "ignore",
|
| 227 |
+
**kwargs,
|
| 228 |
+
) -> str:
|
| 229 |
+
if isinstance(token_ids, int):
|
| 230 |
+
token_ids = [token_ids]
|
| 231 |
+
if skip_special_tokens:
|
| 232 |
+
token_ids = [i for i in token_ids if i not in self.tokenizer.decode_skip_special_tokens_ids]
|
| 233 |
+
return self.tokenizer.decode(token_ids, errors=errors)
|
| 234 |
+
|
| 235 |
+
def _tokenize(self, text, **kwargs):
|
| 236 |
+
raise NotImplementedError
|
| 237 |
+
|
| 238 |
+
def _convert_token_to_id(self, token):
|
| 239 |
+
""" Converts a token (str) in an id using the vocab. """
|
| 240 |
+
return self.tokenizer.convert_token_to_id(token)
|
| 241 |
+
|
| 242 |
+
def _convert_id_to_token(self, index):
|
| 243 |
+
"""Converts an index (integer) in a token (str) using the vocab. """
|
| 244 |
+
return self.tokenizer.convert_id_to_token(index)
|
| 245 |
+
|
| 246 |
+
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
| 247 |
+
"""
|
| 248 |
+
Converts a sequence of tokens in a single string.
|
| 249 |
+
"""
|
| 250 |
+
return self.tokenizer.decode_tokens(tokens)
|
| 251 |
+
|
| 252 |
+
def save_vocabulary(self, save_directory, filename_prefix=None):
|
| 253 |
+
"""Save only the vocabulary of the tokenizer (vocabulary). """
|
| 254 |
+
if os.path.isdir(save_directory):
|
| 255 |
+
vocab_file = os.path.join(save_directory, self.vocab_files_names["vocab_file"])
|
| 256 |
+
else:
|
| 257 |
+
vocab_file = save_directory
|
| 258 |
+
|
| 259 |
+
with open(self.vocab_file, 'rb') as fin:
|
| 260 |
+
proto_str = fin.read()
|
| 261 |
+
|
| 262 |
+
os.makedirs(save_directory + "/vocab", exist_ok=True)
|
| 263 |
+
with open(vocab_file, "wb") as writer:
|
| 264 |
+
writer.write(proto_str)
|
| 265 |
+
|
| 266 |
+
return (vocab_file,)
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"158323": {
|
| 4 |
+
"content": "<pad>",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
}
|
| 11 |
+
},
|
| 12 |
+
"auto_map": {
|
| 13 |
+
"AutoTokenizer": [
|
| 14 |
+
"tokenization_zhinao.ZhinaoTokenizer",
|
| 15 |
+
null
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
"chat_template": "{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n' }}{% endif %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
|
| 19 |
+
"clean_up_tokenization_spaces": false,
|
| 20 |
+
"do_lower_case": false,
|
| 21 |
+
"extra_special_tokens": {},
|
| 22 |
+
"model_max_length": 4096,
|
| 23 |
+
"pad_token": "<pad>",
|
| 24 |
+
"padding_side": "left",
|
| 25 |
+
"remove_space": false,
|
| 26 |
+
"tokenizer_class": "ZhinaoTokenizer"
|
| 27 |
+
}
|
vocab/360.tiktoken
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|