Upload model checkpoints
Browse files- config.json +35 -0
- configuration_internlm2.py +180 -0
- generation_config.json +11 -0
- model.safetensors +3 -0
- modeling_internlm2.py +0 -0
- special_tokens_map.json +38 -0
- tokenization_internlm2.py +236 -0
- tokenization_internlm2_fast.py +214 -0
- tokenizer.model +3 -0
- tokenizer_config.json +103 -0
config.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "/cpfs02/shared/llmit6/liudawei/xpuyu_work_dirs/internlm2-1_8b-fulldata/hf-150000",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"InternLM2ForCausalLM"
|
| 5 |
+
],
|
| 6 |
+
"attn_implementation": "flash_attention_2",
|
| 7 |
+
"auto_map": {
|
| 8 |
+
"AutoConfig": "configuration_internlm2.InternLM2Config",
|
| 9 |
+
"AutoModel": "modeling_internlm2.InternLM2ForCausalLM",
|
| 10 |
+
"AutoModelForCausalLM": "modeling_internlm2.InternLM2ForCausalLM"
|
| 11 |
+
},
|
| 12 |
+
"bias": false,
|
| 13 |
+
"bos_token_id": 1,
|
| 14 |
+
"eos_token_id": 2,
|
| 15 |
+
"fuse_cross_entropy": true,
|
| 16 |
+
"hidden_act": "silu",
|
| 17 |
+
"hidden_size": 2048,
|
| 18 |
+
"initializer_range": 0.02,
|
| 19 |
+
"intermediate_size": 8192,
|
| 20 |
+
"max_position_embeddings": 32768,
|
| 21 |
+
"model_type": "internlm2",
|
| 22 |
+
"num_attention_heads": 16,
|
| 23 |
+
"num_hidden_layers": 24,
|
| 24 |
+
"num_key_value_heads": 8,
|
| 25 |
+
"pad_token_id": 2,
|
| 26 |
+
"pretraining_tp": 1,
|
| 27 |
+
"rms_norm_eps": 1e-05,
|
| 28 |
+
"rope_scaling": null,
|
| 29 |
+
"rope_theta": 10000.0,
|
| 30 |
+
"tie_word_embeddings": false,
|
| 31 |
+
"torch_dtype": "bfloat16",
|
| 32 |
+
"transformers_version": "4.46.2",
|
| 33 |
+
"use_cache": false,
|
| 34 |
+
"vocab_size": 92544
|
| 35 |
+
}
|
configuration_internlm2.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright (c) The InternLM team and The HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This code is based on transformers/src/transformers/models/llama/configuration_llama.py
|
| 5 |
+
#
|
| 6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 7 |
+
# you may not use this file except in compliance with the License.
|
| 8 |
+
# You may obtain a copy of the License at
|
| 9 |
+
#
|
| 10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 11 |
+
#
|
| 12 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 15 |
+
# See the License for the specific language governing permissions and
|
| 16 |
+
# limitations under the License.
|
| 17 |
+
""" InternLM2 model configuration"""
|
| 18 |
+
|
| 19 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 20 |
+
from transformers.utils import logging
|
| 21 |
+
|
| 22 |
+
logger = logging.get_logger(__name__)
|
| 23 |
+
|
| 24 |
+
INTERNLM2_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Modified from transformers.model.llama.configuration_llama.LlamaConfig
|
| 28 |
+
class InternLM2Config(PretrainedConfig):
|
| 29 |
+
r"""
|
| 30 |
+
This is the configuration class to store the configuration of a [`InternLM2Model`]. It is used to instantiate
|
| 31 |
+
an InternLM2 model according to the specified arguments, defining the model architecture. Instantiating a
|
| 32 |
+
configuration with the defaults will yield a similar configuration to that of the InternLM2-7B.
|
| 33 |
+
|
| 34 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 35 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
vocab_size (`int`, *optional*, defaults to 32000):
|
| 40 |
+
Vocabulary size of the InternLM2 model. Defines the number of different tokens that can be represented by the
|
| 41 |
+
`inputs_ids` passed when calling [`InternLM2Model`]
|
| 42 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
| 43 |
+
Dimension of the hidden representations.
|
| 44 |
+
intermediate_size (`int`, *optional*, defaults to 11008):
|
| 45 |
+
Dimension of the MLP representations.
|
| 46 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
| 47 |
+
Number of hidden layers in the Transformer decoder.
|
| 48 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 49 |
+
Number of attention heads for each attention layer in the Transformer decoder.
|
| 50 |
+
num_key_value_heads (`int`, *optional*):
|
| 51 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
| 52 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
| 53 |
+
`num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
| 54 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
| 55 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
| 56 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
|
| 57 |
+
`num_attention_heads`.
|
| 58 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 59 |
+
The non-linear activation function (function or string) in the decoder.
|
| 60 |
+
max_position_embeddings (`int`, *optional*, defaults to 2048):
|
| 61 |
+
The maximum sequence length that this model might ever be used with. InternLM2 supports up to 32768 tokens.
|
| 62 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 63 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 64 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 65 |
+
The epsilon used by the rms normalization layers.
|
| 66 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 67 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
| 68 |
+
relevant if `config.is_decoder=True`.
|
| 69 |
+
pad_token_id (`int`, *optional*):
|
| 70 |
+
Padding token id.
|
| 71 |
+
bos_token_id (`int`, *optional*, defaults to 1):
|
| 72 |
+
Beginning of stream token id.
|
| 73 |
+
eos_token_id (`int`, *optional*, defaults to 2):
|
| 74 |
+
End of stream token id.
|
| 75 |
+
pretraining_tp (`int`, *optional*, defaults to 1):
|
| 76 |
+
Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
|
| 77 |
+
document](https://huggingface.co/docs/transformers/main/perf_train_gpu_many#tensor-parallelism)
|
| 78 |
+
to understand more about it. This value is necessary to ensure exact reproducibility
|
| 79 |
+
of the pretraining results. Please refer to [this
|
| 80 |
+
issue](https://github.com/pytorch/pytorch/issues/76232).
|
| 81 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
| 82 |
+
Whether to tie weight embeddings
|
| 83 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 84 |
+
The base period of the RoPE embeddings.
|
| 85 |
+
rope_scaling (`Dict`, *optional*):
|
| 86 |
+
Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
|
| 87 |
+
strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
|
| 88 |
+
`{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
|
| 89 |
+
`max_position_embeddings` to the expected new maximum. See the following thread for more information on how
|
| 90 |
+
these scaling strategies behave:
|
| 91 |
+
https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
|
| 92 |
+
experimental feature, subject to breaking API changes in future versions.
|
| 93 |
+
"""
|
| 94 |
+
_auto_class = "AutoConfig"
|
| 95 |
+
model_type = "internlm2"
|
| 96 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 97 |
+
|
| 98 |
+
def __init__( # pylint: disable=W0102
|
| 99 |
+
self,
|
| 100 |
+
vocab_size=103168,
|
| 101 |
+
hidden_size=4096,
|
| 102 |
+
intermediate_size=11008,
|
| 103 |
+
num_hidden_layers=32,
|
| 104 |
+
num_attention_heads=32,
|
| 105 |
+
num_key_value_heads=None,
|
| 106 |
+
hidden_act="silu",
|
| 107 |
+
max_position_embeddings=2048,
|
| 108 |
+
initializer_range=0.02,
|
| 109 |
+
rms_norm_eps=1e-6,
|
| 110 |
+
use_cache=True,
|
| 111 |
+
pad_token_id=0,
|
| 112 |
+
bos_token_id=1,
|
| 113 |
+
eos_token_id=2,
|
| 114 |
+
pretraining_tp=1,
|
| 115 |
+
tie_word_embeddings=False,
|
| 116 |
+
bias=True,
|
| 117 |
+
rope_theta=10000,
|
| 118 |
+
rope_scaling=None,
|
| 119 |
+
attn_implementation=None,
|
| 120 |
+
**kwargs,
|
| 121 |
+
):
|
| 122 |
+
self.vocab_size = vocab_size
|
| 123 |
+
self.max_position_embeddings = max_position_embeddings
|
| 124 |
+
self.hidden_size = hidden_size
|
| 125 |
+
self.intermediate_size = intermediate_size
|
| 126 |
+
self.num_hidden_layers = num_hidden_layers
|
| 127 |
+
self.num_attention_heads = num_attention_heads
|
| 128 |
+
self.bias = bias
|
| 129 |
+
|
| 130 |
+
if num_key_value_heads is None:
|
| 131 |
+
num_key_value_heads = num_attention_heads
|
| 132 |
+
self.num_key_value_heads = num_key_value_heads
|
| 133 |
+
|
| 134 |
+
self.hidden_act = hidden_act
|
| 135 |
+
self.initializer_range = initializer_range
|
| 136 |
+
self.rms_norm_eps = rms_norm_eps
|
| 137 |
+
self.pretraining_tp = pretraining_tp
|
| 138 |
+
self.use_cache = use_cache
|
| 139 |
+
self.rope_theta = rope_theta
|
| 140 |
+
self.rope_scaling = rope_scaling
|
| 141 |
+
self._rope_scaling_validation()
|
| 142 |
+
self.attn_implementation = attn_implementation
|
| 143 |
+
if self.attn_implementation is None:
|
| 144 |
+
self.attn_implementation = "eager"
|
| 145 |
+
|
| 146 |
+
super().__init__(
|
| 147 |
+
pad_token_id=pad_token_id,
|
| 148 |
+
bos_token_id=bos_token_id,
|
| 149 |
+
eos_token_id=eos_token_id,
|
| 150 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 151 |
+
**kwargs,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
def _rope_scaling_validation(self):
|
| 155 |
+
"""
|
| 156 |
+
Validate the `rope_scaling` configuration.
|
| 157 |
+
"""
|
| 158 |
+
if self.rope_scaling is None:
|
| 159 |
+
return
|
| 160 |
+
|
| 161 |
+
if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
|
| 162 |
+
raise ValueError(
|
| 163 |
+
"`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, "
|
| 164 |
+
f"got {self.rope_scaling}"
|
| 165 |
+
)
|
| 166 |
+
rope_scaling_type = self.rope_scaling.get("type", None)
|
| 167 |
+
rope_scaling_factor = self.rope_scaling.get("factor", None)
|
| 168 |
+
if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
|
| 169 |
+
raise ValueError(
|
| 170 |
+
f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
|
| 171 |
+
)
|
| 172 |
+
if (
|
| 173 |
+
rope_scaling_factor is None
|
| 174 |
+
or not isinstance(rope_scaling_factor, (float, int))
|
| 175 |
+
or rope_scaling_factor < 1.0
|
| 176 |
+
):
|
| 177 |
+
raise ValueError(
|
| 178 |
+
f"`rope_scaling`'s factor field must be a number >= 1, got {rope_scaling_factor} "
|
| 179 |
+
f"of type {type(rope_scaling_factor)}"
|
| 180 |
+
)
|
generation_config.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": [
|
| 5 |
+
2,
|
| 6 |
+
92542
|
| 7 |
+
],
|
| 8 |
+
"pad_token_id": 2,
|
| 9 |
+
"transformers_version": "4.46.2",
|
| 10 |
+
"use_cache": false
|
| 11 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f437a3ae3126fb8cd81e0d1f9672071c4c98a1344009f2cfcfddbe38f5cd2181
|
| 3 |
+
size 3778239296
|
modeling_internlm2.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
"<|im_start|>",
|
| 4 |
+
"<|im_end|>",
|
| 5 |
+
"<|action_start|>",
|
| 6 |
+
"<|action_end|>",
|
| 7 |
+
"<|interpreter|>",
|
| 8 |
+
"<|plugin|>"
|
| 9 |
+
],
|
| 10 |
+
"bos_token": {
|
| 11 |
+
"content": "<s>",
|
| 12 |
+
"lstrip": false,
|
| 13 |
+
"normalized": false,
|
| 14 |
+
"rstrip": false,
|
| 15 |
+
"single_word": false
|
| 16 |
+
},
|
| 17 |
+
"eos_token": {
|
| 18 |
+
"content": "</s>",
|
| 19 |
+
"lstrip": false,
|
| 20 |
+
"normalized": false,
|
| 21 |
+
"rstrip": false,
|
| 22 |
+
"single_word": false
|
| 23 |
+
},
|
| 24 |
+
"pad_token": {
|
| 25 |
+
"content": "</s>",
|
| 26 |
+
"lstrip": false,
|
| 27 |
+
"normalized": false,
|
| 28 |
+
"rstrip": false,
|
| 29 |
+
"single_word": false
|
| 30 |
+
},
|
| 31 |
+
"unk_token": {
|
| 32 |
+
"content": "<unk>",
|
| 33 |
+
"lstrip": false,
|
| 34 |
+
"normalized": false,
|
| 35 |
+
"rstrip": false,
|
| 36 |
+
"single_word": false
|
| 37 |
+
}
|
| 38 |
+
}
|
tokenization_internlm2.py
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright (c) The InternLM team and The HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This code is based on transformers/src/transformers/models/llama/tokenization_llama.py
|
| 5 |
+
#
|
| 6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 7 |
+
# you may not use this file except in compliance with the License.
|
| 8 |
+
# You may obtain a copy of the License at
|
| 9 |
+
#
|
| 10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 11 |
+
#
|
| 12 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 15 |
+
# See the License for the specific language governing permissions and
|
| 16 |
+
# limitations under the License.
|
| 17 |
+
|
| 18 |
+
"""Tokenization classes for InternLM."""
|
| 19 |
+
import os
|
| 20 |
+
from shutil import copyfile
|
| 21 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 22 |
+
|
| 23 |
+
import sentencepiece as spm
|
| 24 |
+
from transformers.tokenization_utils import PreTrainedTokenizer
|
| 25 |
+
from transformers.utils import logging
|
| 26 |
+
|
| 27 |
+
logger = logging.get_logger(__name__)
|
| 28 |
+
|
| 29 |
+
VOCAB_FILES_NAMES = {"vocab_file": "./tokenizer.model"}
|
| 30 |
+
|
| 31 |
+
PRETRAINED_VOCAB_FILES_MAP = {}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Modified from transformers.model.llama.tokenization_llama.LlamaTokenizer
|
| 35 |
+
class InternLM2Tokenizer(PreTrainedTokenizer):
|
| 36 |
+
"""
|
| 37 |
+
Construct a InternLM2 tokenizer. Based on byte-level Byte-Pair-Encoding.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
vocab_file (`str`):
|
| 41 |
+
Path to the vocabulary file.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
| 45 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
| 46 |
+
model_input_names = ["input_ids", "attention_mask"]
|
| 47 |
+
_auto_class = "AutoTokenizer"
|
| 48 |
+
|
| 49 |
+
def __init__(
|
| 50 |
+
self,
|
| 51 |
+
vocab_file,
|
| 52 |
+
unk_token="<unk>",
|
| 53 |
+
bos_token="<s>",
|
| 54 |
+
eos_token="</s>",
|
| 55 |
+
pad_token="</s>",
|
| 56 |
+
sp_model_kwargs: Optional[Dict[str, Any]] = None,
|
| 57 |
+
add_bos_token=True,
|
| 58 |
+
add_eos_token=False,
|
| 59 |
+
decode_with_prefix_space=False,
|
| 60 |
+
clean_up_tokenization_spaces=False,
|
| 61 |
+
**kwargs,
|
| 62 |
+
):
|
| 63 |
+
self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
|
| 64 |
+
self.vocab_file = vocab_file
|
| 65 |
+
self.add_bos_token = add_bos_token
|
| 66 |
+
self.add_eos_token = add_eos_token
|
| 67 |
+
self.decode_with_prefix_space = decode_with_prefix_space
|
| 68 |
+
self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
|
| 69 |
+
self.sp_model.Load(vocab_file)
|
| 70 |
+
self._no_prefix_space_tokens = None
|
| 71 |
+
super().__init__(
|
| 72 |
+
bos_token=bos_token,
|
| 73 |
+
eos_token=eos_token,
|
| 74 |
+
unk_token=unk_token,
|
| 75 |
+
pad_token=pad_token,
|
| 76 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
| 77 |
+
**kwargs,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
@property
|
| 81 |
+
def no_prefix_space_tokens(self):
|
| 82 |
+
if self._no_prefix_space_tokens is None:
|
| 83 |
+
vocab = self.convert_ids_to_tokens(list(range(self.vocab_size)))
|
| 84 |
+
self._no_prefix_space_tokens = {i for i, tok in enumerate(vocab) if not tok.startswith("▁")}
|
| 85 |
+
return self._no_prefix_space_tokens
|
| 86 |
+
|
| 87 |
+
@property
|
| 88 |
+
def vocab_size(self):
|
| 89 |
+
"""Returns vocab size"""
|
| 90 |
+
return self.sp_model.get_piece_size()
|
| 91 |
+
|
| 92 |
+
@property
|
| 93 |
+
def bos_token_id(self) -> Optional[int]:
|
| 94 |
+
return self.sp_model.bos_id()
|
| 95 |
+
|
| 96 |
+
@property
|
| 97 |
+
def eos_token_id(self) -> Optional[int]:
|
| 98 |
+
return self.sp_model.eos_id()
|
| 99 |
+
|
| 100 |
+
def get_vocab(self):
|
| 101 |
+
"""Returns vocab as a dict"""
|
| 102 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
| 103 |
+
vocab.update(self.added_tokens_encoder)
|
| 104 |
+
return vocab
|
| 105 |
+
|
| 106 |
+
def _tokenize(self, text):
|
| 107 |
+
"""Returns a tokenized string."""
|
| 108 |
+
return self.sp_model.encode(text, out_type=str)
|
| 109 |
+
|
| 110 |
+
def _convert_token_to_id(self, token):
|
| 111 |
+
"""Converts a token (str) in an id using the vocab."""
|
| 112 |
+
return self.sp_model.piece_to_id(token)
|
| 113 |
+
|
| 114 |
+
def _convert_id_to_token(self, index):
|
| 115 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
| 116 |
+
token = self.sp_model.IdToPiece(index)
|
| 117 |
+
return token
|
| 118 |
+
|
| 119 |
+
def _maybe_add_prefix_space(self, tokens, decoded):
|
| 120 |
+
if tokens and tokens[0] not in self.no_prefix_space_tokens:
|
| 121 |
+
return " " + decoded
|
| 122 |
+
else:
|
| 123 |
+
return decoded
|
| 124 |
+
|
| 125 |
+
def convert_tokens_to_string(self, tokens):
|
| 126 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
| 127 |
+
current_sub_tokens = []
|
| 128 |
+
out_string = ""
|
| 129 |
+
prev_is_special = False
|
| 130 |
+
for token in tokens:
|
| 131 |
+
# make sure that special tokens are not decoded using sentencepiece model
|
| 132 |
+
if token in self.all_special_tokens:
|
| 133 |
+
if not prev_is_special:
|
| 134 |
+
out_string += " "
|
| 135 |
+
out_string += self.sp_model.decode(current_sub_tokens) + token
|
| 136 |
+
prev_is_special = True
|
| 137 |
+
current_sub_tokens = []
|
| 138 |
+
else:
|
| 139 |
+
current_sub_tokens.append(token)
|
| 140 |
+
prev_is_special = False
|
| 141 |
+
out_string += self.sp_model.decode(current_sub_tokens)
|
| 142 |
+
out_string = self.clean_up_tokenization(out_string)
|
| 143 |
+
out_string = self._maybe_add_prefix_space(tokens=tokens, decoded=out_string)
|
| 144 |
+
return out_string[1:]
|
| 145 |
+
|
| 146 |
+
def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
| 147 |
+
"""
|
| 148 |
+
Save the vocabulary and special tokens file to a directory.
|
| 149 |
+
|
| 150 |
+
Args:
|
| 151 |
+
save_directory (`str`):
|
| 152 |
+
The directory in which to save the vocabulary.
|
| 153 |
+
|
| 154 |
+
Returns:
|
| 155 |
+
`Tuple(str)`: Paths to the files saved.
|
| 156 |
+
"""
|
| 157 |
+
if not os.path.isdir(save_directory):
|
| 158 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 159 |
+
return
|
| 160 |
+
out_vocab_file = os.path.join(
|
| 161 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file):
|
| 165 |
+
copyfile(self.vocab_file, out_vocab_file)
|
| 166 |
+
elif not os.path.isfile(self.vocab_file):
|
| 167 |
+
with open(out_vocab_file, "wb") as fi:
|
| 168 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
| 169 |
+
fi.write(content_spiece_model)
|
| 170 |
+
|
| 171 |
+
return (out_vocab_file,)
|
| 172 |
+
|
| 173 |
+
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
| 174 |
+
if self.add_bos_token:
|
| 175 |
+
bos_token_ids = [self.bos_token_id]
|
| 176 |
+
else:
|
| 177 |
+
bos_token_ids = []
|
| 178 |
+
|
| 179 |
+
output = bos_token_ids + token_ids_0
|
| 180 |
+
|
| 181 |
+
if token_ids_1 is not None:
|
| 182 |
+
output = output + token_ids_1
|
| 183 |
+
|
| 184 |
+
if self.add_eos_token:
|
| 185 |
+
output = output + [self.eos_token_id]
|
| 186 |
+
|
| 187 |
+
return output
|
| 188 |
+
|
| 189 |
+
def get_special_tokens_mask(
|
| 190 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
| 191 |
+
) -> List[int]:
|
| 192 |
+
"""
|
| 193 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
| 194 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
| 195 |
+
|
| 196 |
+
Args:
|
| 197 |
+
token_ids_0 (`List[int]`):
|
| 198 |
+
List of IDs.
|
| 199 |
+
token_ids_1 (`List[int]`, *optional*):
|
| 200 |
+
Optional second list of IDs for sequence pairs.
|
| 201 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
| 202 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
| 203 |
+
|
| 204 |
+
Returns:
|
| 205 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
| 206 |
+
"""
|
| 207 |
+
if already_has_special_tokens:
|
| 208 |
+
return super().get_special_tokens_mask(
|
| 209 |
+
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
if token_ids_1 is None:
|
| 213 |
+
return [1] + ([0] * len(token_ids_0)) + [1]
|
| 214 |
+
return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1]
|
| 215 |
+
|
| 216 |
+
def create_token_type_ids_from_sequences(
|
| 217 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
| 218 |
+
) -> List[int]:
|
| 219 |
+
"""
|
| 220 |
+
Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make
|
| 221 |
+
use of token type ids, therefore a list of zeros is returned.
|
| 222 |
+
|
| 223 |
+
Args:
|
| 224 |
+
token_ids_0 (`List[int]`):
|
| 225 |
+
List of IDs.
|
| 226 |
+
token_ids_1 (`List[int]`, *optional*):
|
| 227 |
+
Optional second list of IDs for sequence pairs.
|
| 228 |
+
|
| 229 |
+
Returns:
|
| 230 |
+
`List[int]`: List of zeros.
|
| 231 |
+
"""
|
| 232 |
+
eos = [self.eos_token_id]
|
| 233 |
+
|
| 234 |
+
if token_ids_1 is None:
|
| 235 |
+
return len(token_ids_0 + eos) * [0]
|
| 236 |
+
return len(token_ids_0 + eos + token_ids_1 + eos) * [0]
|
tokenization_internlm2_fast.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright (c) The InternLM team and The HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This code is based on transformers/src/transformers/models/llama/tokenization_llama_fast.py
|
| 5 |
+
#
|
| 6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 7 |
+
# you may not use this file except in compliance with the License.
|
| 8 |
+
# You may obtain a copy of the License at
|
| 9 |
+
#
|
| 10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 11 |
+
#
|
| 12 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 15 |
+
# See the License for the specific language governing permissions and
|
| 16 |
+
# limitations under the License.
|
| 17 |
+
|
| 18 |
+
"""Tokenization Fast class for InternLM."""
|
| 19 |
+
import os
|
| 20 |
+
from shutil import copyfile
|
| 21 |
+
from typing import Any, Dict, Optional, Tuple
|
| 22 |
+
|
| 23 |
+
from tokenizers import processors, decoders, Tokenizer, normalizers
|
| 24 |
+
from tokenizers.models import BPE
|
| 25 |
+
|
| 26 |
+
from transformers.tokenization_utils_fast import PreTrainedTokenizerFast
|
| 27 |
+
from transformers.utils import logging
|
| 28 |
+
|
| 29 |
+
from transformers.convert_slow_tokenizer import (
|
| 30 |
+
SLOW_TO_FAST_CONVERTERS,
|
| 31 |
+
SpmConverter,
|
| 32 |
+
SentencePieceExtractor,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
from .tokenization_internlm2 import InternLM2Tokenizer
|
| 36 |
+
|
| 37 |
+
logger = logging.get_logger(__name__)
|
| 38 |
+
|
| 39 |
+
VOCAB_FILES_NAMES = {"vocab_file": "./tokenizer.model"}
|
| 40 |
+
|
| 41 |
+
# Modified from transformers.convert_slow_tokenizer.LlamaConverter
|
| 42 |
+
class InternLM2Converter(SpmConverter):
|
| 43 |
+
handle_byte_fallback = True
|
| 44 |
+
|
| 45 |
+
def vocab(self, proto):
|
| 46 |
+
vocab = [
|
| 47 |
+
("<unk>", 0.0),
|
| 48 |
+
("<s>", 0.0),
|
| 49 |
+
("</s>", 0.0),
|
| 50 |
+
]
|
| 51 |
+
vocab += [(piece.piece, piece.score) for piece in proto.pieces[3:]]
|
| 52 |
+
return vocab
|
| 53 |
+
|
| 54 |
+
def unk_id(self, proto):
|
| 55 |
+
unk_id = 0
|
| 56 |
+
return unk_id
|
| 57 |
+
|
| 58 |
+
def decoder(self, replacement, add_prefix_space):
|
| 59 |
+
decoders_sequence = [
|
| 60 |
+
decoders.Replace("▁", " "),
|
| 61 |
+
decoders.ByteFallback(),
|
| 62 |
+
decoders.Fuse(),
|
| 63 |
+
]
|
| 64 |
+
if self.proto.normalizer_spec.add_dummy_prefix:
|
| 65 |
+
decoders_sequence.append(decoders.Strip(content=" ", left=1))
|
| 66 |
+
return decoders.Sequence(decoders_sequence)
|
| 67 |
+
|
| 68 |
+
def tokenizer(self, proto):
|
| 69 |
+
model_type = proto.trainer_spec.model_type
|
| 70 |
+
vocab_scores = self.vocab(proto)
|
| 71 |
+
# special tokens
|
| 72 |
+
added_tokens = self.original_tokenizer.added_tokens_decoder
|
| 73 |
+
for i in range(len(vocab_scores)):
|
| 74 |
+
piece, score = vocab_scores[i]
|
| 75 |
+
if i in added_tokens:
|
| 76 |
+
vocab_scores[i] = (added_tokens[i].content, score)
|
| 77 |
+
if model_type == 1:
|
| 78 |
+
raise RuntimeError("InternLM2 is supposed to be a BPE model!")
|
| 79 |
+
|
| 80 |
+
elif model_type == 2:
|
| 81 |
+
_, merges = SentencePieceExtractor(self.original_tokenizer.vocab_file).extract(vocab_scores)
|
| 82 |
+
bpe_vocab = {word: i for i, (word, _score) in enumerate(vocab_scores)}
|
| 83 |
+
tokenizer = Tokenizer(
|
| 84 |
+
BPE(bpe_vocab, merges, unk_token=proto.trainer_spec.unk_piece, fuse_unk=True, byte_fallback=True)
|
| 85 |
+
)
|
| 86 |
+
tokenizer.add_special_tokens(
|
| 87 |
+
[ added_token for index, added_token in added_tokens.items()]
|
| 88 |
+
)
|
| 89 |
+
else:
|
| 90 |
+
raise Exception(
|
| 91 |
+
"You're trying to run a `Unigram` model but you're file was trained with a different algorithm"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
return tokenizer
|
| 95 |
+
|
| 96 |
+
def normalizer(self, proto):
|
| 97 |
+
normalizers_list = []
|
| 98 |
+
if proto.normalizer_spec.add_dummy_prefix:
|
| 99 |
+
normalizers_list.append(normalizers.Prepend(prepend="▁"))
|
| 100 |
+
normalizers_list.append(normalizers.Replace(pattern=" ", content="▁"))
|
| 101 |
+
return normalizers.Sequence(normalizers_list)
|
| 102 |
+
|
| 103 |
+
def pre_tokenizer(self, replacement, add_prefix_space):
|
| 104 |
+
return None
|
| 105 |
+
|
| 106 |
+
SLOW_TO_FAST_CONVERTERS["InternLM2Tokenizer"] = InternLM2Converter
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# Modified from transformers.model.llama.tokenization_llama_fast.LlamaTokenizerFast -> InternLM2TokenizerFast
|
| 110 |
+
class InternLM2TokenizerFast(PreTrainedTokenizerFast):
|
| 111 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
| 112 |
+
slow_tokenizer_class = InternLM2Tokenizer
|
| 113 |
+
padding_side = "left"
|
| 114 |
+
model_input_names = ["input_ids", "attention_mask"]
|
| 115 |
+
_auto_class = "AutoTokenizer"
|
| 116 |
+
|
| 117 |
+
def __init__(
|
| 118 |
+
self,
|
| 119 |
+
vocab_file,
|
| 120 |
+
unk_token="<unk>",
|
| 121 |
+
bos_token="<s>",
|
| 122 |
+
eos_token="</s>",
|
| 123 |
+
pad_token="</s>",
|
| 124 |
+
sp_model_kwargs: Optional[Dict[str, Any]] = None,
|
| 125 |
+
add_bos_token=True,
|
| 126 |
+
add_eos_token=False,
|
| 127 |
+
decode_with_prefix_space=False,
|
| 128 |
+
clean_up_tokenization_spaces=False,
|
| 129 |
+
**kwargs,
|
| 130 |
+
):
|
| 131 |
+
super().__init__(
|
| 132 |
+
vocab_file=vocab_file,
|
| 133 |
+
unk_token=unk_token,
|
| 134 |
+
bos_token=bos_token,
|
| 135 |
+
eos_token=eos_token,
|
| 136 |
+
pad_token=pad_token,
|
| 137 |
+
sp_model_kwargs=sp_model_kwargs,
|
| 138 |
+
add_bos_token=add_bos_token,
|
| 139 |
+
add_eos_token=add_eos_token,
|
| 140 |
+
decode_with_prefix_space=decode_with_prefix_space,
|
| 141 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
| 142 |
+
**kwargs,
|
| 143 |
+
)
|
| 144 |
+
self._add_bos_token = add_bos_token
|
| 145 |
+
self._add_eos_token = add_eos_token
|
| 146 |
+
self.update_post_processor()
|
| 147 |
+
self.vocab_file = vocab_file
|
| 148 |
+
|
| 149 |
+
@property
|
| 150 |
+
def can_save_slow_tokenizer(self) -> bool:
|
| 151 |
+
return os.path.isfile(self.vocab_file) if self.vocab_file else False
|
| 152 |
+
|
| 153 |
+
def update_post_processor(self):
|
| 154 |
+
"""
|
| 155 |
+
Updates the underlying post processor with the current `bos_token` and `eos_token`.
|
| 156 |
+
"""
|
| 157 |
+
bos = self.bos_token
|
| 158 |
+
bos_token_id = self.bos_token_id
|
| 159 |
+
if bos is None and self.add_bos_token:
|
| 160 |
+
raise ValueError("add_bos_token = True but bos_token = None")
|
| 161 |
+
|
| 162 |
+
eos = self.eos_token
|
| 163 |
+
eos_token_id = self.eos_token_id
|
| 164 |
+
if eos is None and self.add_eos_token:
|
| 165 |
+
raise ValueError("add_eos_token = True but eos_token = None")
|
| 166 |
+
|
| 167 |
+
single = f"{(bos+':0 ') if self.add_bos_token else ''}$A:0{(' '+eos+':0') if self.add_eos_token else ''}"
|
| 168 |
+
pair = f"{single}{(' '+bos+':1') if self.add_bos_token else ''} $B:1{(' '+eos+':1') if self.add_eos_token else ''}"
|
| 169 |
+
|
| 170 |
+
special_tokens = []
|
| 171 |
+
if self.add_bos_token:
|
| 172 |
+
special_tokens.append((bos, bos_token_id))
|
| 173 |
+
if self.add_eos_token:
|
| 174 |
+
special_tokens.append((eos, eos_token_id))
|
| 175 |
+
self._tokenizer.post_processor = processors.TemplateProcessing(
|
| 176 |
+
single=single, pair=pair, special_tokens=special_tokens
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
@property
|
| 180 |
+
def add_eos_token(self):
|
| 181 |
+
return self._add_eos_token
|
| 182 |
+
|
| 183 |
+
@property
|
| 184 |
+
def add_bos_token(self):
|
| 185 |
+
return self._add_bos_token
|
| 186 |
+
|
| 187 |
+
@add_eos_token.setter
|
| 188 |
+
def add_eos_token(self, value):
|
| 189 |
+
self._add_eos_token = value
|
| 190 |
+
self.update_post_processor()
|
| 191 |
+
|
| 192 |
+
@add_bos_token.setter
|
| 193 |
+
def add_bos_token(self, value):
|
| 194 |
+
self._add_bos_token = value
|
| 195 |
+
self.update_post_processor()
|
| 196 |
+
|
| 197 |
+
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
| 198 |
+
if not self.can_save_slow_tokenizer:
|
| 199 |
+
raise ValueError(
|
| 200 |
+
"Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
|
| 201 |
+
"tokenizer."
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
if not os.path.isdir(save_directory):
|
| 205 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 206 |
+
return
|
| 207 |
+
out_vocab_file = os.path.join(
|
| 208 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file):
|
| 212 |
+
copyfile(self.vocab_file, out_vocab_file)
|
| 213 |
+
|
| 214 |
+
return (out_vocab_file,)
|
tokenizer.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f868398fc4e05ee1e8aeba95ddf18ddcc45b8bce55d5093bead5bbf80429b48b
|
| 3 |
+
size 1477754
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_bos_token": true,
|
| 3 |
+
"add_eos_token": false,
|
| 4 |
+
"added_tokens_decoder": {
|
| 5 |
+
"0": {
|
| 6 |
+
"content": "<unk>",
|
| 7 |
+
"lstrip": false,
|
| 8 |
+
"normalized": false,
|
| 9 |
+
"rstrip": false,
|
| 10 |
+
"single_word": false,
|
| 11 |
+
"special": true
|
| 12 |
+
},
|
| 13 |
+
"1": {
|
| 14 |
+
"content": "<s>",
|
| 15 |
+
"lstrip": false,
|
| 16 |
+
"normalized": false,
|
| 17 |
+
"rstrip": false,
|
| 18 |
+
"single_word": false,
|
| 19 |
+
"special": true
|
| 20 |
+
},
|
| 21 |
+
"2": {
|
| 22 |
+
"content": "</s>",
|
| 23 |
+
"lstrip": false,
|
| 24 |
+
"normalized": false,
|
| 25 |
+
"rstrip": false,
|
| 26 |
+
"single_word": false,
|
| 27 |
+
"special": true
|
| 28 |
+
},
|
| 29 |
+
"92538": {
|
| 30 |
+
"content": "<|plugin|>",
|
| 31 |
+
"lstrip": false,
|
| 32 |
+
"normalized": false,
|
| 33 |
+
"rstrip": false,
|
| 34 |
+
"single_word": false,
|
| 35 |
+
"special": true
|
| 36 |
+
},
|
| 37 |
+
"92539": {
|
| 38 |
+
"content": "<|interpreter|>",
|
| 39 |
+
"lstrip": false,
|
| 40 |
+
"normalized": false,
|
| 41 |
+
"rstrip": false,
|
| 42 |
+
"single_word": false,
|
| 43 |
+
"special": true
|
| 44 |
+
},
|
| 45 |
+
"92540": {
|
| 46 |
+
"content": "<|action_end|>",
|
| 47 |
+
"lstrip": false,
|
| 48 |
+
"normalized": false,
|
| 49 |
+
"rstrip": false,
|
| 50 |
+
"single_word": false,
|
| 51 |
+
"special": true
|
| 52 |
+
},
|
| 53 |
+
"92541": {
|
| 54 |
+
"content": "<|action_start|>",
|
| 55 |
+
"lstrip": false,
|
| 56 |
+
"normalized": false,
|
| 57 |
+
"rstrip": false,
|
| 58 |
+
"single_word": false,
|
| 59 |
+
"special": true
|
| 60 |
+
},
|
| 61 |
+
"92542": {
|
| 62 |
+
"content": "<|im_end|>",
|
| 63 |
+
"lstrip": false,
|
| 64 |
+
"normalized": false,
|
| 65 |
+
"rstrip": false,
|
| 66 |
+
"single_word": false,
|
| 67 |
+
"special": true
|
| 68 |
+
},
|
| 69 |
+
"92543": {
|
| 70 |
+
"content": "<|im_start|>",
|
| 71 |
+
"lstrip": false,
|
| 72 |
+
"normalized": false,
|
| 73 |
+
"rstrip": false,
|
| 74 |
+
"single_word": false,
|
| 75 |
+
"special": true
|
| 76 |
+
}
|
| 77 |
+
},
|
| 78 |
+
"additional_special_tokens": [
|
| 79 |
+
"<|im_start|>",
|
| 80 |
+
"<|im_end|>",
|
| 81 |
+
"<|action_start|>",
|
| 82 |
+
"<|action_end|>",
|
| 83 |
+
"<|interpreter|>",
|
| 84 |
+
"<|plugin|>"
|
| 85 |
+
],
|
| 86 |
+
"auto_map": {
|
| 87 |
+
"AutoTokenizer": [
|
| 88 |
+
"tokenization_internlm2.InternLM2Tokenizer",
|
| 89 |
+
"tokenization_internlm2_fast.InternLM2TokenizerFast"
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
"bos_token": "<s>",
|
| 93 |
+
"chat_template": "{{ bos_token }}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
|
| 94 |
+
"clean_up_tokenization_spaces": false,
|
| 95 |
+
"decode_with_prefix_space": false,
|
| 96 |
+
"eos_token": "</s>",
|
| 97 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 98 |
+
"pad_token": "</s>",
|
| 99 |
+
"padding_side": "right",
|
| 100 |
+
"sp_model_kwargs": null,
|
| 101 |
+
"tokenizer_class": "InternLM2Tokenizer",
|
| 102 |
+
"unk_token": "<unk>"
|
| 103 |
+
}
|