Upload folder using huggingface_hub
Browse files- LICENSE +39 -0
- README.md +222 -0
- added_tokens.json +12 -0
- config.json +28 -0
- configuration_inflm.py +36 -0
- generation_config.json +9 -0
- modeling_inflm.py +69 -0
- special_tokens_map.json +78 -0
- tokenization_inflm.py +292 -0
- tokenizer.model +3 -0
- tokenizer_config.json +147 -0
LICENSE
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Version Release Date: July 16, 2024
|
| 2 |
+
|
| 3 |
+
By engaging in any of the following activities with the Model or any Derivative Model, or by accepting the terms of this Agreement, you consent to be bound by the terms.
|
| 4 |
+
|
| 5 |
+
1. Definitions.
|
| 6 |
+
The following definitions apply to this Agreement:
|
| 7 |
+
1.1. "Derivative Model" refers to any of the following related to the Model: a. Modifications made to the Model; b. Works created based on the Model. c. Any other works derived from the Model.
|
| 8 |
+
1.2. "Legal Entity" means the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of fifty percent (50%) or more of the outstanding shares, or (c) beneficial ownership of such entity.
|
| 9 |
+
1.3. "Model" encompasses the following components of the machine learning model shared under this Agreement: Software,Checkpoints,Algorithms,Model Weights,Configuration files,Documentation,Code.
|
| 10 |
+
1.4. "Distribution" means any transmission, reproduction, publication or other sharing of the Model or Derivatives of the Model to a third party, including providing the Model as a hosted service made available by electronic or other remote means - e.g. API-based or web access.
|
| 11 |
+
1.5. "You" or "Your" means an individual or Legal Entity exercising permissions granted by this Agreement.
|
| 12 |
+
1.6. "INF" (or "we") means INF Technology (Shanghai) Co., Ltd. and/or any of their affiliates.
|
| 13 |
+
1.7. "Third Parties" means individuals or legal entities that are not under common control with INF or You.
|
| 14 |
+
|
| 15 |
+
2. Grant of Rights.
|
| 16 |
+
Your use of the following rights is strictly dependent on your complete adherence to the terms of this Agreement. In accordance with the stipulations of this Agreement, INF grants you the following rights, which are perpetual, global, non-exclusive, free of charge, and royalty-free, and are subject to revocation:
|
| 17 |
+
2.1. The right to publicly perform and display the Model.
|
| 18 |
+
2.2. The right to reproduce and utilize the Model.
|
| 19 |
+
2.3. The right to create derivative works based on the Model.
|
| 20 |
+
2.4. The authority to manufacture, have manufactured, and sell the Model or its derivatives.
|
| 21 |
+
2.5. The ability to offer for sale, distribute, and import the Model or its derivatives through various distribution channels.
|
| 22 |
+
|
| 23 |
+
3. Redistribution.
|
| 24 |
+
You are permitted to reproduce and distribute the Model or any Derivative Models, either in their original form or with modifications, across various mediums, as long as you fulfill the following requirements:
|
| 25 |
+
3.1. If you choose to distribute the Model, it is mandatory to:
|
| 26 |
+
Provide each recipient with a copy of this Agreement.
|
| 27 |
+
3.2. You have the right to:
|
| 28 |
+
a. Append your own copyright statement to the modifications you make.
|
| 29 |
+
b. Offer alternative or supplementary licensing terms and conditions for the use, reproduction, or distribution of your modifications.
|
| 30 |
+
c. Establish terms and conditions for the Derivative Models as a whole.
|
| 31 |
+
d. However, this is only permissible if your use, reproduction, and distribution of the original Model align with the conditions laid out in this Agreement.
|
| 32 |
+
|
| 33 |
+
4. Other Provisions
|
| 34 |
+
4.1. Trademarks. No rights are given to use the trade names, trademarks, service marks, or product names of INF as part of this agreement, except as required for reasonable and customary use in describing the origin of the Model and fulfilling the notice requirements explicitly stated in this Agreement.
|
| 35 |
+
4.2. Disclaimer of Warranty. Unless explicitly stipulated in writing or mandated by law, INF offers the Model strictly in its existing condition, without any form of warranty, whether stated or implied. This includes, but is not limited to, any warranties or conditions regarding the title, non-infringement, merchantability, or suitability for a specific purpose. It is your sole responsibility to assess the suitability of using or redistributing the Model, its derivatives, and any outputs. You also assume all risks related to the exercise of the rights granted by this Agreement.
|
| 36 |
+
4.3. Governing Law and Jurisdiction. This agreement will be governed and construed under PRC laws without regard to choice of law principles, and the UN Convention on Contracts for the International Sale of Goods does not apply to this agreement. The People's Courts in Shanghai shall have exclusive jurisdiction over any dispute arising out of this Agreement.
|
| 37 |
+
4.5. If any provision of this License is held to be invalid, illegal or unenforceable, the remaining provisions shall be unaffected thereby and remain valid as if such provision had not been set forth herein.
|
| 38 |
+
4.6. Personal information, IP rights and related. This Model may contain personal information and works with IP rights. You commit to complying with applicable laws and regulations in the handling of personal information and the use of such works. Please note that INF's license granted to you to use the Model does not imply that you have obtained a legitimate basis for processing the related information or works. As an independent personal information processor and IP rights user, you need to ensure full compliance with relevant legal and regulatory requirements when handling personal information and works with IP rights that may be contained in the Model, and are willing to assume solely any risks and consequences that may arise from that.
|
| 39 |
+
4.7. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall INF be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Model and the Complementary Material (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if INF has been advised of the possibility of such damages.
|
README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: inf
|
| 4 |
+
license_link: LICENSE
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
<div align="center">
|
| 8 |
+
<img src="https://github.com/infly-ai/INF-LLM/blob/main/images/logo.png?raw=true" width="35%" alt="INF-34B" />
|
| 9 |
+
</div>
|
| 10 |
+
<hr>
|
| 11 |
+
<div align="center">
|
| 12 |
+
|
| 13 |
+
<a href="https://chat.infly.cn/" target="_blank">
|
| 14 |
+
<img alt="Chat" src="https://img.shields.io/badge/🤖%20Chat-INF%20LLM-536af5?color=536af5&logoColor=white" />
|
| 15 |
+
</a>
|
| 16 |
+
<a href="https://huggingface.co/infly" target="_blank">
|
| 17 |
+
<img alt="Hugging Face" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-INF%20AI-ffc107?color=ffc107&logoColor=white" />
|
| 18 |
+
</a>
|
| 19 |
+
</div>
|
| 20 |
+
|
| 21 |
+
<p align="center">
|
| 22 |
+
<a href="https://s.infly.cn/f/img/pdf/inf_34b_tech_report.pdf"><b>Paper Link</b></a>
|
| 23 |
+
</p>
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
## 1. Introduction
|
| 27 |
+
|
| 28 |
+
INF-34B has 34 billion parameters with a context window length of 32K, and is trained on about 3.5T well-processed tokens from English and Chinese bilingual corpus. Compared with open source models of the comparable size, INF-34B not only provides competitive performance in the OpenCompass evaluation, but also has impressive potential on both finance and healthcare domains. Besides, the quantized INF-34B runs on graphics cards of 24GB VRAM with negligible accuracy loss, which facilitates commercial applications, especially low-resource scenarios.
|
| 29 |
+
|
| 30 |
+
<div align="center">
|
| 31 |
+
<img src="https://github.com/infly-ai/INF-LLM/blob/main/images/teaser.png?raw=true" alt="result" width="100%">
|
| 32 |
+
</div>
|
| 33 |
+
|
| 34 |
+
- **Detailed for Training GPT Model:** We provide comprehensive details about our model pretraining and alignment, including high-quality data pipeline, instruction data preparation, and quantization results etc.
|
| 35 |
+
|
| 36 |
+
- **Superior Performance on Benchmarks:** We demonstrate superior performance of the INF-34B models by comparing against two competitors with comparable model size, Qwen1.5-32B and Yi1.5-34B, on the public OpenCompass benchmarks.
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
## 2. Models
|
| 40 |
+
|
| 41 |
+
We release the base and chat models with 34B parameters based on the LLaMA framework, while using LayerNorm with zero-centered gamma instead of RMSNorm for training stability. Please **note** that you could use our models for commercial applications under the terms outlined in [License section](#6-license).
|
| 42 |
+
|
| 43 |
+
### Huggingface
|
| 44 |
+
|
| 45 |
+
| Model | Sequence Length | Download |
|
| 46 |
+
|:---------------------:|:---------------:|:-----------------------------------------------------------------------:|
|
| 47 |
+
| INF-34B-Base | 32K | 🤗 [HuggingFace](https://huggingface.co/infly/INF-34B-Base) |
|
| 48 |
+
| Inf-34B-Chat | 32K | 🤗 [HuggingFace](https://huggingface.co/infly/INF-34B-Chat) |
|
| 49 |
+
| Inf-34B-Chat-GPTQ-4bits | 32K | 🤗 [HuggingFace](https://huggingface.co/infly/INF-34B-Chat-GPTQ-4bit) |
|
| 50 |
+
| Inf-34B-Chat-GPTQ-8bits | 32K | 🤗 [HuggingFace](https://huggingface.co/infly/INF-34B-Chat-GPTQ-8bit) |
|
| 51 |
+
| Inf-34B-Chat-AWQ | 32K | 🤗 [HuggingFace](https://huggingface.co/infly/INF-34B-Chat-AWQ) |
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
## 3. Benchmarks
|
| 55 |
+
|
| 56 |
+
**Note:** If you want to reproduce the evaluation results, please refer to the [details of evaluation](https://github.com/infly-ai/INF-LLM/blob/main/evaluation/Evaluation.md), including prompts, postprocess scripts and version of inference frameworks.
|
| 57 |
+
|
| 58 |
+
### Base Model
|
| 59 |
+
|
| 60 |
+
We evaluate our model on several academic benchmarks then compare with other similar-sized open access model. INF-34B has stronger performance in the fields that we chose to optimize while simultaneously preserves the general capabilities of LLM such as commonsense, world knowledge, math and coding.
|
| 61 |
+
|
| 62 |
+
| model | QWen1.5-32B | Yi1.5-34B | INF-34B |
|
| 63 |
+
|:---------------:|:-------------:|:------------:|:------:|
|
| 64 |
+
| MMLU(5-shot) | 73.60 | 77.86 | 76.11 |
|
| 65 |
+
| CMMLU(5-shot) | 81.87 | 81.85 | 80.08 |
|
| 66 |
+
| GSM8K(4-shot) | 72.86 | 80.06 | 83.02 |
|
| 67 |
+
| MATH(4-shot) | 36.80 | 33.88 | 38.34 |
|
| 68 |
+
| HumanEval(0-shot) | 44.51 | 47.56 | 65.24 |
|
| 69 |
+
| MBPP(3-shot) | 51.00 | 65.60 | 64.00 |
|
| 70 |
+
| BBH(3-shot) | 70.60 | 74.83 | 71.20 |
|
| 71 |
+
| HellaSwag(0-shot) | 82.03 | 81.57 | 83.32 |
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
**Note:** To facilitate reproduction, the results of common benchmarks are generated by [OpenCompass](https://github.com/open-compass/opencompass) except humaneval and mbpp as we experience code timeout and postprocess issues.
|
| 75 |
+
|
| 76 |
+
### Chat Model
|
| 77 |
+
|
| 78 |
+
We present the performance results of our chat model and other LLM on various standard benchmarks, as well as two domain-specific benchmarks.
|
| 79 |
+
|
| 80 |
+
| model | QWen1.5-32B-Chat | Yi1.5-34B-Chat | INF-34B-Chat |
|
| 81 |
+
|:---------------:|:-------------:|:------------:|:------:|
|
| 82 |
+
| MT-bench | 8.3 | 8.5 | 8.3 |
|
| 83 |
+
| AlignBench | 7.1 | 7.2 | 7.1 |
|
| 84 |
+
| IFEval | 49.54 | 58.04 | 59.70 |
|
| 85 |
+
| Arena-Hard | 24.2 | 42.6 | 43.1 |
|
| 86 |
+
| GSM8K | 81.42 | 79.45 | 84.04 |
|
| 87 |
+
| MATH | 42.28 | 54.06 | 51.48 |
|
| 88 |
+
|
| 89 |
+
### Long Context
|
| 90 |
+
|
| 91 |
+
We employed a long context SFT dataset of various length. Specifically, 37.7% shorter than 8k tokens, 40.5% falling within 8k to 16k tokens and 21.8% ranging from 16k to 32k tokens. And Our model has demonstrated superior performance on LongBench(via [OpenCompass](https://github.com/open-compass/opencompass)) tasks compared to Qwen1.5-32B.
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
| model | Single-Doc<br>QA | Multi-Doc<br>QA | Summari-<br>zation | Few-shot<br>Learning | Synthetic | Code |
|
| 95 |
+
|:---------------:|:-------------:|:------------:|:------:|:------:|:------:|:------:|
|
| 96 |
+
| QWen1.5-32B-Chat | 45.6 | 40.4 | 23.1 | 52.6 | 67.3 | 43.8 |
|
| 97 |
+
| INF-34B-Chat | 47.4 | 43.2 | 24.1 | 66.0 | 66.8 | 57.2 |
|
| 98 |
+
|
| 99 |
+
**Note:** All the reported results on the table are the average of sub-tasks for different categories.
|
| 100 |
+
|
| 101 |
+
INF-34B-32k also performs well across context window lengths up to 32k on Single-Needle RetrievalTask(S-RT) as visualized below.
|
| 102 |
+
<div align="center">
|
| 103 |
+
<img src="https://github.com/infly-ai/INF-LLM/blob/main/images/srt.png?raw=true" alt="SRT Results" width="100%">
|
| 104 |
+
</div>
|
| 105 |
+
|
| 106 |
+
## 4. Training Details
|
| 107 |
+
|
| 108 |
+
### Data Pipeline
|
| 109 |
+
|
| 110 |
+
We propose different data pipelines for general, domain and code data to ensure the richness, variety and quality of training samples. The general data pipeline involves general processing methods. For the domain data of interest, e.g., math, wiki, code, we propose a domain-specific data pipeline to extract the domain data from Common Crawl (CC). We also devise a code-specific pipeline to handle massive code data, since the code data has proven its effectiveness in improving the model’s reasoning and comprehension ability.
|
| 111 |
+
|
| 112 |
+
- **General Data Pipeline**: Our text cleaning pipeline mainly includes two stages: filtering and deduplication. The filtering involves language identification, URL filtering, and heuristic filtering rules. The deduplication includes both fuzzy deduplication and exact deduplication techniques.
|
| 113 |
+
|
| 114 |
+
- **Domain Data Pipeline**: We propose an iterative high-quality data retrieval method that recalls relevant data from the Common Crawl (CC) dataset for various target domains. It comprises three main component: FastText training, Performing recall and Human annotation.
|
| 115 |
+
|
| 116 |
+
- **Code Data Pipeline**: The code-specific data processing pipeline includes modules of preprocessing, heuristic filtering, deduplication, transformation and data mixture.
|
| 117 |
+
|
| 118 |
+
### Training Settings
|
| 119 |
+
|
| 120 |
+
The architecture choices of INF-34B follows LLaMA framework. Specifically, we opt Rotary Embedding for positional encoding, SwiGLU for activation function, Grouped Query Attention (GQA) and LayerNorm with zero-centered gamma instead of RMSNorm for training stability.
|
| 121 |
+
|
| 122 |
+
Motivated by the idea of first training on relatively large but less polished corpus to equip the model with language understanding and world knowledge and then improves model’s domain knowledge and reasoning ability, our training process is split into 3 stages:
|
| 123 |
+
- Stage 1: The dataset mainly includes web text, paper, Wikipedia and source code. In this early stage, we aim at larger data and higher diversity.
|
| 124 |
+
- Stage 2: For second stage we seek to gradually challenge the model with longer and more complex texts. We up-weight long texts in the same data distribution of stage 1. We tune the rope base and extend our context window to 32k for more sophisticated comprehension of human knowledge.
|
| 125 |
+
- Stage 3: The final stage is composed of domain data recalled from Web text and synthetic data.
|
| 126 |
+
|
| 127 |
+
<div align="center">
|
| 128 |
+
<img src="https://github.com/infly-ai/INF-LLM/blob/main/images/setting.png?raw=true" alt="result" width="70%">
|
| 129 |
+
</div>
|
| 130 |
+
|
| 131 |
+
## 5. Inference
|
| 132 |
+
|
| 133 |
+
### Installation
|
| 134 |
+
|
| 135 |
+
Please clone our [GitHub](https://github.com/infly-ai/INF-LLM), and install the dependencies for `Python >= 3.8` by running the following command:
|
| 136 |
+
|
| 137 |
+
```shell
|
| 138 |
+
git clone https://github.com/infly-ai/INF-LLM
|
| 139 |
+
cd INF-LLM
|
| 140 |
+
pip install -r requirements.txt
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
### Inference with Huggingface's Transformers
|
| 144 |
+
|
| 145 |
+
We provide the inference examples with [Huggingface's Transformers](https://github.com/huggingface/transformers).
|
| 146 |
+
|
| 147 |
+
**Text Generation with Base Model**
|
| 148 |
+
|
| 149 |
+
```python
|
| 150 |
+
import torch
|
| 151 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 152 |
+
|
| 153 |
+
model_name = "infly-ai/INF-34B-Base"
|
| 154 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 155 |
+
model = AutoModelForCausalLM.from_pretrained(model_name,
|
| 156 |
+
torch_dtype=torch.bfloat16,
|
| 157 |
+
device_map="auto",
|
| 158 |
+
trust_remote_code=True)
|
| 159 |
+
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
| 160 |
+
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
| 161 |
+
|
| 162 |
+
inputs = tokenizer("对酒当歌,", return_tensors="pt")
|
| 163 |
+
outputs = model.generate(**inputs.to(model.device), max_new_tokens=100)
|
| 164 |
+
|
| 165 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 166 |
+
print(result)
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
**Text Generation with Chat Model**
|
| 170 |
+
|
| 171 |
+
```python
|
| 172 |
+
import torch
|
| 173 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 174 |
+
|
| 175 |
+
model_name = "infly-ai/INF-34B-Chat"
|
| 176 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 177 |
+
model = AutoModelForCausalLM.from_pretrained(model_name,
|
| 178 |
+
torch_dtype=torch.bfloat16,
|
| 179 |
+
device_map="auto",
|
| 180 |
+
trust_remote_code=True)
|
| 181 |
+
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
| 182 |
+
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
| 183 |
+
|
| 184 |
+
messages = [
|
| 185 |
+
{"role": "user", "content": "Who are you?"}
|
| 186 |
+
]
|
| 187 |
+
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 188 |
+
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
|
| 189 |
+
|
| 190 |
+
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
| 191 |
+
print(result)
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
You can also interact with our model following the sample template without `apply_chat_template`. You should tokenize the completed templates as the base model does for more flexible usage.
|
| 195 |
+
|
| 196 |
+
```
|
| 197 |
+
<|start|>user
|
| 198 |
+
messages[0]['content']<|end|>
|
| 199 |
+
<|start|>assistant<|message|>{messages[1]['content']}<|end|>
|
| 200 |
+
<|start|>user
|
| 201 |
+
messages[2]['content']<|end|>
|
| 202 |
+
<|start|>assistant<|message|>
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
## 6. License
|
| 206 |
+
|
| 207 |
+
INF-34B series (including Base and Chat) support commercial applications under a permissive [License](https://github.com/infly-ai/INF-LLM/blob/main/LICENSE).
|
| 208 |
+
|
| 209 |
+
## 7. Citation
|
| 210 |
+
|
| 211 |
+
```
|
| 212 |
+
@article{inf-llm,
|
| 213 |
+
author = {INF-Team},
|
| 214 |
+
title = {INF’s Open-Source Large Language Models},
|
| 215 |
+
year = {2024},
|
| 216 |
+
url = {https://github.com/infly-ai/INF-LLM}
|
| 217 |
+
}
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
## 8. Contact
|
| 221 |
+
|
| 222 |
+
If you have any questions or seek for cooperation, please contact us at [bd@infteach.ai](mailto:bd@infteach.ai).
|
added_tokens.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"<|im_end|>": 96508,
|
| 3 |
+
"<|im_start|>": 96507,
|
| 4 |
+
"<|endoftext|>": 96506,
|
| 5 |
+
"<|end|>": 96500,
|
| 6 |
+
"<|message|>": 96501,
|
| 7 |
+
"<|pad|>": 96505,
|
| 8 |
+
"<|start|>": 96499,
|
| 9 |
+
"<|tool_end|>": 96504,
|
| 10 |
+
"<|tool_excute|>": 96503,
|
| 11 |
+
"<|tool_start|>": 96502
|
| 12 |
+
}
|
config.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"auto_map": {
|
| 3 |
+
"AutoConfig": "configuration_inflm.INFLMConfig",
|
| 4 |
+
"AutoModelForCausalLM": "modeling_inflm.INFLMForCausalLM"
|
| 5 |
+
},
|
| 6 |
+
"architectures": [
|
| 7 |
+
"INFLMForCausalLM"
|
| 8 |
+
],
|
| 9 |
+
"bos_token_id": 1,
|
| 10 |
+
"eos_token_id": 96508,
|
| 11 |
+
"hidden_act": "silu",
|
| 12 |
+
"hidden_size": 8192,
|
| 13 |
+
"initializer_range": 0.02,
|
| 14 |
+
"intermediate_size": 22016,
|
| 15 |
+
"max_position_embeddings": 4096,
|
| 16 |
+
"model_type": "inflm",
|
| 17 |
+
"num_attention_heads": 64,
|
| 18 |
+
"num_hidden_layers": 48,
|
| 19 |
+
"num_key_value_heads": 8,
|
| 20 |
+
"pretraining_tp": 1,
|
| 21 |
+
"layer_norm_eps": 1e-05,
|
| 22 |
+
"rope_theta": 500000,
|
| 23 |
+
"rope_scaling": null,
|
| 24 |
+
"tie_word_embeddings": false,
|
| 25 |
+
"torch_dtype": "bfloat16",
|
| 26 |
+
"use_cache": true,
|
| 27 |
+
"vocab_size": 96512
|
| 28 |
+
}
|
configuration_inflm.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
| 5 |
+
# and OPT implementations in this library. It has been modified from its
|
| 6 |
+
# original forms to accommodate minor architectural differences compared
|
| 7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
| 8 |
+
#
|
| 9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 10 |
+
# you may not use this file except in compliance with the License.
|
| 11 |
+
# You may obtain a copy of the License at
|
| 12 |
+
#
|
| 13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 14 |
+
#
|
| 15 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 18 |
+
# See the License for the specific language governing permissions and
|
| 19 |
+
# limitations under the License.
|
| 20 |
+
"""INFLM model configuration."""
|
| 21 |
+
|
| 22 |
+
from transformers.models.llama.configuration_llama import LlamaConfig
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class INFLMConfig(LlamaConfig):
|
| 26 |
+
model_type = "inflm"
|
| 27 |
+
|
| 28 |
+
def __init__(
|
| 29 |
+
self,
|
| 30 |
+
layer_norm_eps=1e-5,
|
| 31 |
+
**kwargs,
|
| 32 |
+
):
|
| 33 |
+
self.layer_norm_eps = layer_norm_eps
|
| 34 |
+
super().__init__(
|
| 35 |
+
**kwargs,
|
| 36 |
+
)
|
generation_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"eos_token_id": 2,
|
| 3 |
+
"pad_token_id": 3,
|
| 4 |
+
"max_new_tokens": 2048,
|
| 5 |
+
"do_sample": true,
|
| 6 |
+
"top_k": 0,
|
| 7 |
+
"top_p": 0.8,
|
| 8 |
+
"transformers_version": "4.39.0"
|
| 9 |
+
}
|
modeling_inflm.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
| 5 |
+
# and OPT implementations in this library. It has been modified from its
|
| 6 |
+
# original forms to accommodate minor architectural differences compared
|
| 7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
| 8 |
+
#
|
| 9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 10 |
+
# you may not use this file except in compliance with the License.
|
| 11 |
+
# You may obtain a copy of the License at
|
| 12 |
+
#
|
| 13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 14 |
+
#
|
| 15 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 18 |
+
# See the License for the specific language governing permissions and
|
| 19 |
+
# limitations under the License.
|
| 20 |
+
"""PyTorch INFLM model."""
|
| 21 |
+
|
| 22 |
+
import torch
|
| 23 |
+
from torch import nn
|
| 24 |
+
from transformers.models.llama.modeling_llama import (
|
| 25 |
+
LlamaDecoderLayer,
|
| 26 |
+
LlamaModel,
|
| 27 |
+
LlamaForCausalLM
|
| 28 |
+
)
|
| 29 |
+
from .configuration_inflm import INFLMConfig
|
| 30 |
+
|
| 31 |
+
_CONFIG_FOR_DOC = "INFLMConfig"
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class INFLMDecoderLayer(LlamaDecoderLayer):
|
| 35 |
+
def __init__(self, config: INFLMConfig, layer_idx: int):
|
| 36 |
+
super().__init__(config, layer_idx)
|
| 37 |
+
self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 38 |
+
self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class INFLMModel(LlamaModel):
|
| 42 |
+
config_class = INFLMConfig
|
| 43 |
+
_no_split_modules = ["INFLMDecoderLayer"]
|
| 44 |
+
|
| 45 |
+
def __init__(self, config: INFLMConfig):
|
| 46 |
+
super().__init__(config)
|
| 47 |
+
self.padding_idx = config.pad_token_id
|
| 48 |
+
self.vocab_size = config.vocab_size
|
| 49 |
+
|
| 50 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 51 |
+
self.layers = nn.ModuleList([INFLMDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])
|
| 52 |
+
self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 53 |
+
|
| 54 |
+
self.gradient_checkpointing = False
|
| 55 |
+
# Initialize weights and apply final processing
|
| 56 |
+
self.post_init()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class INFLMForCausalLM(LlamaForCausalLM):
|
| 60 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 61 |
+
|
| 62 |
+
def __init__(self, config: INFLMConfig):
|
| 63 |
+
super().__init__(config)
|
| 64 |
+
self.model = INFLMModel(config)
|
| 65 |
+
self.vocab_size = config.vocab_size
|
| 66 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 67 |
+
|
| 68 |
+
# Initialize weights and apply final processing
|
| 69 |
+
self.post_init()
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
{
|
| 4 |
+
"content": "<|start|>",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"content": "<|end|>",
|
| 12 |
+
"lstrip": false,
|
| 13 |
+
"normalized": false,
|
| 14 |
+
"rstrip": false,
|
| 15 |
+
"single_word": false
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"content": "<|message|>",
|
| 19 |
+
"lstrip": false,
|
| 20 |
+
"normalized": false,
|
| 21 |
+
"rstrip": false,
|
| 22 |
+
"single_word": false
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"content": "<|tool_start|>",
|
| 26 |
+
"lstrip": false,
|
| 27 |
+
"normalized": false,
|
| 28 |
+
"rstrip": false,
|
| 29 |
+
"single_word": false
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"content": "<|tool_excute|>",
|
| 33 |
+
"lstrip": false,
|
| 34 |
+
"normalized": false,
|
| 35 |
+
"rstrip": false,
|
| 36 |
+
"single_word": false
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"content": "<|tool_end|>",
|
| 40 |
+
"lstrip": false,
|
| 41 |
+
"normalized": false,
|
| 42 |
+
"rstrip": false,
|
| 43 |
+
"single_word": false
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"content": "<|pad|>",
|
| 47 |
+
"lstrip": false,
|
| 48 |
+
"normalized": false,
|
| 49 |
+
"rstrip": false,
|
| 50 |
+
"single_word": false
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"content": "<|endoftext|>",
|
| 54 |
+
"lstrip": false,
|
| 55 |
+
"normalized": false,
|
| 56 |
+
"rstrip": false,
|
| 57 |
+
"single_word": false
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"content": "<|im_start|>",
|
| 61 |
+
"lstrip": false,
|
| 62 |
+
"normalized": false,
|
| 63 |
+
"rstrip": false,
|
| 64 |
+
"single_word": false
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"content": "<|im_end|>",
|
| 68 |
+
"lstrip": false,
|
| 69 |
+
"normalized": false,
|
| 70 |
+
"rstrip": false,
|
| 71 |
+
"single_word": false
|
| 72 |
+
}
|
| 73 |
+
],
|
| 74 |
+
"bos_token": "<s>",
|
| 75 |
+
"eos_token": "<|im_end|>",
|
| 76 |
+
"pad_token": "<pad>",
|
| 77 |
+
"unk_token": "<unk>"
|
| 78 |
+
}
|
tokenization_inflm.py
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
| 5 |
+
# and OPT implementations in this library. It has been modified from its
|
| 6 |
+
# original forms to accommodate minor architectural differences compared
|
| 7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
| 8 |
+
#
|
| 9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 10 |
+
# you may not use this file except in compliance with the License.
|
| 11 |
+
# You may obtain a copy of the License at
|
| 12 |
+
#
|
| 13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 14 |
+
#
|
| 15 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 18 |
+
# See the License for the specific language governing permissions and
|
| 19 |
+
# limitations under the License.
|
| 20 |
+
|
| 21 |
+
"""Tokenization classes for INFLMTokenizer."""
|
| 22 |
+
import os
|
| 23 |
+
from shutil import copyfile
|
| 24 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 25 |
+
|
| 26 |
+
import sentencepiece as spm
|
| 27 |
+
|
| 28 |
+
from transformers.tokenization_utils import PreTrainedTokenizer
|
| 29 |
+
from transformers.utils import logging
|
| 30 |
+
|
| 31 |
+
from tokenizers import pre_tokenizers,Regex,decoders
|
| 32 |
+
from tokenizers.pre_tokenizers import Digits, Split, ByteLevel
|
| 33 |
+
import os
|
| 34 |
+
|
| 35 |
+
# same as gpt4 cl-base-100k
|
| 36 |
+
PATTERN = Regex("(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+\s+(\S)+")
|
| 37 |
+
|
| 38 |
+
logger = logging.get_logger(__name__)
|
| 39 |
+
|
| 40 |
+
VOCAB_FILES_NAMES = {"vocab_file": "./tokenizer.model"}
|
| 41 |
+
|
| 42 |
+
PRETRAINED_VOCAB_FILES_MAP = {}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class INFLMTokenizer(PreTrainedTokenizer):
|
| 46 |
+
"""
|
| 47 |
+
Construct a INFLMTokenizer tokenizer based on sentence-piece
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
vocab_file (`str`):
|
| 51 |
+
Path to the vocabulary file.
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
| 55 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
| 56 |
+
model_input_names = ["input_ids", "attention_mask"]
|
| 57 |
+
_auto_class = "AutoTokenizer"
|
| 58 |
+
|
| 59 |
+
def __init__(
|
| 60 |
+
self,
|
| 61 |
+
vocab_file,
|
| 62 |
+
unk_token="<unk>",
|
| 63 |
+
bos_token="<s>",
|
| 64 |
+
eos_token="</s>",
|
| 65 |
+
pad_token="<pad>",
|
| 66 |
+
sp_model_kwargs: Optional[Dict[str, Any]] = None,
|
| 67 |
+
add_bos_token=False,
|
| 68 |
+
add_eos_token=False,
|
| 69 |
+
decode_with_prefix_space=False,
|
| 70 |
+
clean_up_tokenization_spaces=False,
|
| 71 |
+
spaces_between_special_tokens=False,
|
| 72 |
+
**kwargs,
|
| 73 |
+
):
|
| 74 |
+
self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
|
| 75 |
+
self.vocab_file = vocab_file
|
| 76 |
+
self.add_bos_token = add_bos_token
|
| 77 |
+
self.add_eos_token = add_eos_token
|
| 78 |
+
self.decode_with_prefix_space = decode_with_prefix_space
|
| 79 |
+
self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
|
| 80 |
+
self.sp_model.Load(vocab_file)
|
| 81 |
+
self._no_prefix_space_tokens = None
|
| 82 |
+
self.pre_tokenizer = pre_tokenizers.Sequence([Split(pattern =PATTERN,behavior = "isolated", invert = False)])
|
| 83 |
+
super().__init__(
|
| 84 |
+
bos_token=bos_token,
|
| 85 |
+
eos_token=eos_token,
|
| 86 |
+
unk_token=unk_token,
|
| 87 |
+
pad_token=pad_token,
|
| 88 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
| 89 |
+
spaces_between_special_tokens=spaces_between_special_tokens,
|
| 90 |
+
**kwargs,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
""" Initialisation"""
|
| 94 |
+
|
| 95 |
+
@property
|
| 96 |
+
def no_prefix_space_tokens(self):
|
| 97 |
+
if self._no_prefix_space_tokens is None:
|
| 98 |
+
vocab = self.convert_ids_to_tokens(list(range(self.vocab_size)))
|
| 99 |
+
self._no_prefix_space_tokens = {i for i, tok in enumerate(vocab) if not tok.startswith("▁")}
|
| 100 |
+
return self._no_prefix_space_tokens
|
| 101 |
+
|
| 102 |
+
@property
|
| 103 |
+
def vocab_size(self):
|
| 104 |
+
"""Returns vocab size"""
|
| 105 |
+
return self.sp_model.get_piece_size()
|
| 106 |
+
|
| 107 |
+
@property
|
| 108 |
+
def bos_token_id(self) -> Optional[int]:
|
| 109 |
+
return self.sp_model.bos_id()
|
| 110 |
+
|
| 111 |
+
@property
|
| 112 |
+
def eos_token_id(self) -> Optional[int]:
|
| 113 |
+
return self.sp_model.eos_id()
|
| 114 |
+
|
| 115 |
+
def get_vocab(self):
|
| 116 |
+
"""Returns vocab as a dict"""
|
| 117 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
| 118 |
+
vocab.update(self.added_tokens_encoder)
|
| 119 |
+
return vocab
|
| 120 |
+
|
| 121 |
+
def _tokenize(self, text):
|
| 122 |
+
"""Returns a tokenized string."""
|
| 123 |
+
|
| 124 |
+
splits = self.pre_tokenizer.pre_tokenize_str(text)
|
| 125 |
+
texts=[]
|
| 126 |
+
|
| 127 |
+
for split in splits:
|
| 128 |
+
texts.extend(self.sp_model.encode(split[0], out_type=str))
|
| 129 |
+
return texts
|
| 130 |
+
|
| 131 |
+
def _convert_token_to_id(self, token):
|
| 132 |
+
"""Converts a token (str) in an id using the vocab."""
|
| 133 |
+
|
| 134 |
+
return self.sp_model.piece_to_id(token)
|
| 135 |
+
|
| 136 |
+
def _convert_id_to_token(self, index):
|
| 137 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
| 138 |
+
token = self.sp_model.IdToPiece(index)
|
| 139 |
+
return token
|
| 140 |
+
|
| 141 |
+
def _maybe_add_prefix_space(self, tokens, decoded):
|
| 142 |
+
if tokens and tokens[0] not in self.no_prefix_space_tokens:
|
| 143 |
+
return " " + decoded
|
| 144 |
+
else:
|
| 145 |
+
return decoded
|
| 146 |
+
|
| 147 |
+
def convert_tokens_to_string(self, tokens):
|
| 148 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
| 149 |
+
current_sub_tokens = []
|
| 150 |
+
out_string = ""
|
| 151 |
+
prev_is_special = False
|
| 152 |
+
for token in tokens:
|
| 153 |
+
# make sure that special tokens are not decoded using sentencepiece model
|
| 154 |
+
if token in self.all_special_tokens:
|
| 155 |
+
out_string += self.sp_model.decode(current_sub_tokens) + token
|
| 156 |
+
prev_is_special = True
|
| 157 |
+
current_sub_tokens = []
|
| 158 |
+
else:
|
| 159 |
+
current_sub_tokens.append(token)
|
| 160 |
+
prev_is_special = False
|
| 161 |
+
out_string += self.sp_model.decode(current_sub_tokens)
|
| 162 |
+
|
| 163 |
+
return out_string
|
| 164 |
+
|
| 165 |
+
def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
| 166 |
+
"""
|
| 167 |
+
Save the vocabulary and special tokens file to a directory.
|
| 168 |
+
|
| 169 |
+
Args:
|
| 170 |
+
save_directory (`str`):
|
| 171 |
+
The directory in which to save the vocabulary.
|
| 172 |
+
|
| 173 |
+
Returns:
|
| 174 |
+
`Tuple(str)`: Paths to the files saved.
|
| 175 |
+
"""
|
| 176 |
+
if not os.path.isdir(save_directory):
|
| 177 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 178 |
+
return
|
| 179 |
+
out_vocab_file = os.path.join(
|
| 180 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file):
|
| 184 |
+
copyfile(self.vocab_file, out_vocab_file)
|
| 185 |
+
elif not os.path.isfile(self.vocab_file):
|
| 186 |
+
with open(out_vocab_file, "wb") as fi:
|
| 187 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
| 188 |
+
fi.write(content_spiece_model)
|
| 189 |
+
|
| 190 |
+
return (out_vocab_file,)
|
| 191 |
+
|
| 192 |
+
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
| 193 |
+
if self.add_bos_token:
|
| 194 |
+
bos_token_ids = [self.bos_token_id]
|
| 195 |
+
else:
|
| 196 |
+
bos_token_ids = []
|
| 197 |
+
|
| 198 |
+
output = bos_token_ids + token_ids_0
|
| 199 |
+
|
| 200 |
+
if token_ids_1 is not None:
|
| 201 |
+
output = output + token_ids_1
|
| 202 |
+
|
| 203 |
+
if self.add_eos_token:
|
| 204 |
+
output = output + [self.eos_token_id]
|
| 205 |
+
|
| 206 |
+
return output
|
| 207 |
+
|
| 208 |
+
def get_special_tokens_mask(
|
| 209 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
| 210 |
+
) -> List[int]:
|
| 211 |
+
"""
|
| 212 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
| 213 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
| 214 |
+
|
| 215 |
+
Args:
|
| 216 |
+
token_ids_0 (`List[int]`):
|
| 217 |
+
List of IDs.
|
| 218 |
+
token_ids_1 (`List[int]`, *optional*):
|
| 219 |
+
Optional second list of IDs for sequence pairs.
|
| 220 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
| 221 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
| 222 |
+
|
| 223 |
+
Returns:
|
| 224 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
| 225 |
+
"""
|
| 226 |
+
if already_has_special_tokens:
|
| 227 |
+
return super().get_special_tokens_mask(
|
| 228 |
+
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
eos_token_id = [1] if self.add_eos_token else []
|
| 232 |
+
if token_ids_1 is None:
|
| 233 |
+
return ([0] * len(token_ids_0)) + eos_token_id
|
| 234 |
+
return ([0] * len(token_ids_0)) + eos_token_id + ([0] * len(token_ids_1)) + eos_token_id
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def create_token_type_ids_from_sequences(
|
| 238 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
| 239 |
+
) -> List[int]:
|
| 240 |
+
"""
|
| 241 |
+
Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT
|
| 242 |
+
sequence pair mask has the following format:
|
| 243 |
+
|
| 244 |
+
```
|
| 245 |
+
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
| 246 |
+
| first sequence | second sequence |
|
| 247 |
+
```
|
| 248 |
+
|
| 249 |
+
if token_ids_1 is None, only returns the first portion of the mask (0s).
|
| 250 |
+
|
| 251 |
+
Note this is only used for back compatiblity, thus list of zero is returned.
|
| 252 |
+
|
| 253 |
+
Args:
|
| 254 |
+
token_ids_0 (`List[int]`):
|
| 255 |
+
List of ids.
|
| 256 |
+
token_ids_1 (`List[int]`, *optional*):
|
| 257 |
+
Optional second list of IDs for sequence pairs.
|
| 258 |
+
|
| 259 |
+
Returns:
|
| 260 |
+
`List[int]`: List of zeros.
|
| 261 |
+
"""
|
| 262 |
+
eos = [self.eos_token_id]
|
| 263 |
+
|
| 264 |
+
if token_ids_1 is None:
|
| 265 |
+
return len(token_ids_0 + eos) * [0]
|
| 266 |
+
return len(token_ids_0 + eos + token_ids_1 + eos) * [0]
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
@property
|
| 270 |
+
def default_chat_template(self):
|
| 271 |
+
return None
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def decode(
|
| 275 |
+
self,
|
| 276 |
+
token_ids,
|
| 277 |
+
skip_special_tokens: bool = False,
|
| 278 |
+
clean_up_tokenization_spaces: Optional[bool] = False,
|
| 279 |
+
spaces_between_special_tokens: bool = False,
|
| 280 |
+
**kwargs,
|
| 281 |
+
) -> str:
|
| 282 |
+
# default spaces_between_special_tokens should be false.
|
| 283 |
+
if spaces_between_special_tokens:
|
| 284 |
+
logger.warning_once('spaces_between_special_tokens is set. \
|
| 285 |
+
It has no effect for bos,eos,pad,unk when transformers<=4.38.')
|
| 286 |
+
return super().decode(
|
| 287 |
+
token_ids,
|
| 288 |
+
skip_special_tokens=skip_special_tokens,
|
| 289 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
| 290 |
+
spaces_between_special_tokens=spaces_between_special_tokens,
|
| 291 |
+
**kwargs,
|
| 292 |
+
)
|
tokenizer.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:76d43d618fc0c5a7c79dc4e72579f9f29bb803b36e4a4d709d1233626fd8fe2a
|
| 3 |
+
size 1535725
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "<unk>",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "<s>",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "</s>",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"3": {
|
| 28 |
+
"content": "<pad>",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"96499": {
|
| 36 |
+
"content": "<|start|>",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
},
|
| 43 |
+
"96500": {
|
| 44 |
+
"content": "<|end|>",
|
| 45 |
+
"lstrip": false,
|
| 46 |
+
"normalized": false,
|
| 47 |
+
"rstrip": false,
|
| 48 |
+
"single_word": false,
|
| 49 |
+
"special": true
|
| 50 |
+
},
|
| 51 |
+
"96501": {
|
| 52 |
+
"content": "<|message|>",
|
| 53 |
+
"lstrip": false,
|
| 54 |
+
"normalized": false,
|
| 55 |
+
"rstrip": false,
|
| 56 |
+
"single_word": false,
|
| 57 |
+
"special": true
|
| 58 |
+
},
|
| 59 |
+
"96502": {
|
| 60 |
+
"content": "<|tool_start|>",
|
| 61 |
+
"lstrip": false,
|
| 62 |
+
"normalized": false,
|
| 63 |
+
"rstrip": false,
|
| 64 |
+
"single_word": false,
|
| 65 |
+
"special": true
|
| 66 |
+
},
|
| 67 |
+
"96503": {
|
| 68 |
+
"content": "<|tool_excute|>",
|
| 69 |
+
"lstrip": false,
|
| 70 |
+
"normalized": false,
|
| 71 |
+
"rstrip": false,
|
| 72 |
+
"single_word": false,
|
| 73 |
+
"special": true
|
| 74 |
+
},
|
| 75 |
+
"96504": {
|
| 76 |
+
"content": "<|tool_end|>",
|
| 77 |
+
"lstrip": false,
|
| 78 |
+
"normalized": false,
|
| 79 |
+
"rstrip": false,
|
| 80 |
+
"single_word": false,
|
| 81 |
+
"special": true
|
| 82 |
+
},
|
| 83 |
+
"96505": {
|
| 84 |
+
"content": "<|pad|>",
|
| 85 |
+
"lstrip": false,
|
| 86 |
+
"normalized": false,
|
| 87 |
+
"rstrip": false,
|
| 88 |
+
"single_word": false,
|
| 89 |
+
"special": true
|
| 90 |
+
},
|
| 91 |
+
"96506": {
|
| 92 |
+
"content": "<|endoftext|>",
|
| 93 |
+
"lstrip": false,
|
| 94 |
+
"normalized": false,
|
| 95 |
+
"rstrip": false,
|
| 96 |
+
"single_word": false,
|
| 97 |
+
"special": true
|
| 98 |
+
},
|
| 99 |
+
"96507": {
|
| 100 |
+
"content": "<|im_start|>",
|
| 101 |
+
"lstrip": false,
|
| 102 |
+
"normalized": false,
|
| 103 |
+
"rstrip": false,
|
| 104 |
+
"single_word": false,
|
| 105 |
+
"special": true
|
| 106 |
+
},
|
| 107 |
+
"96508": {
|
| 108 |
+
"content": "<|im_end|>",
|
| 109 |
+
"lstrip": false,
|
| 110 |
+
"normalized": false,
|
| 111 |
+
"rstrip": false,
|
| 112 |
+
"single_word": false,
|
| 113 |
+
"special": true
|
| 114 |
+
}
|
| 115 |
+
},
|
| 116 |
+
"additional_special_tokens": [
|
| 117 |
+
"<|start|>",
|
| 118 |
+
"<|end|>",
|
| 119 |
+
"<|message|>",
|
| 120 |
+
"<|tool_start|>",
|
| 121 |
+
"<|tool_excute|>",
|
| 122 |
+
"<|tool_end|>",
|
| 123 |
+
"<|pad|>",
|
| 124 |
+
"<|endoftext|>",
|
| 125 |
+
"<|im_start|>",
|
| 126 |
+
"<|im_end|>"
|
| 127 |
+
],
|
| 128 |
+
"auto_map": {
|
| 129 |
+
"AutoTokenizer": [
|
| 130 |
+
"tokenization_inflm.INFLMTokenizer",
|
| 131 |
+
null
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
"add_bos_token": false,
|
| 135 |
+
"add_eos_token": false,
|
| 136 |
+
"add_prefix_space": false,
|
| 137 |
+
"bos_token": "<s>",
|
| 138 |
+
"clean_up_tokenization_spaces": false,
|
| 139 |
+
"eos_token": "<|im_end|>",
|
| 140 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 141 |
+
"chat_template": "{% for message in messages %}{% if message['role'] == 'user' %}{% if not loop.first %}{{ '\\n' }}{% endif %}{{'<|start|>user\\n' + message['content'] + '<|end|>\\n' }}{% if (loop.last and add_generation_prompt) %}{{ '<|start|>assistant<|message|>' }}{% endif %}{% elif message['role'] == 'system' %}{{ '<|start|>system\\n' + message['content'] + '<|end|>' }}{% elif message['role'] == 'assistant' %}{{ '<|start|>assistant<|message|>' + message['content'] + '<|end|>' }}{% endif %}{% endfor %}",
|
| 142 |
+
"pad_token": "<pad>",
|
| 143 |
+
"return_tensors": true,
|
| 144 |
+
"spaces_between_special_tokens": false,
|
| 145 |
+
"tokenizer_class": "INFLMTokenizer",
|
| 146 |
+
"unk_token": "<unk>"
|
| 147 |
+
}
|