IEIT-Yuan commited on
Commit
f5dbe81
1 Parent(s): 9dbf7b1
README.md CHANGED
@@ -1,3 +1,97 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: license-yuan
4
+ license_link: https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/LICENSE-Yuan
5
+ ---
6
+
7
+ <div align="center">
8
+ <h1>
9
+ Yuan 2
10
+ </h1>
11
+ </div>
12
+
13
+ <div align="center">
14
+ <a href="https://github.com/IEIT-Yuan/Yuan-2.0" target="_blank"> 💻GitHub Repo</a> | <a href="http://arxiv.org/pdf/2311.15786.pdf" target="_blank">📃Yuan2.0-paper</a>
15
+ </div>
16
+
17
+ # 目录/Table of Contents
18
+
19
+ - [模型介绍/Introduction](#Introduction)
20
+ - [代码调用/Code Usage](#Usage)
21
+ - [Benchmark评估/Benchmark Evaluation](#Benchmark)
22
+ - [声明与协议/Terms and Conditions](#Terms)
23
+ - [引用/Cite](#Cite)
24
+
25
+
26
+ # <span id="Introduction">模型介绍/Introduction</span>
27
+ 源2.0 是浪潮信息发布的新一代基础语言大模型。我们开源了全部的3个模型源2.0-102B,源2.0-51B和源2.0-2B。并且我们提供了预训练,微调,推理服务的相关脚本,以供研发人员做进一步的开发。源2.0是在源1.0的基础上,利用更多样的高质量预训练数据和指令微调数据集,令模型在语义、数学、推理、代码、知识等不同方面具备更强的理解能力。
28
+
29
+ Yuan2.0 is a new generation Fundamental Large Language Model developed by IEIT System. We have published all three models, Yuan 2.0-102B, Yuan 2.0-51B, and Yuan 2.0-2B. And we provide relevant scripts for pretraining, fine-tuning, and inference services for other developers. Yuan2.0 is based on Yuan1.0, utilizing a wider range of high-quality pre training data and instruction fine-tuning datasets to enhance the model's understanding of semantics, mathematics, reasoning, code, knowledge, and other aspects.
30
+
31
+
32
+ # <span id="Usage">代码调用/Code Usage</span>
33
+ 可以通过如下代码调用 Yuan2-2B-MoE 模型来生成文本:
34
+
35
+ You can generate text by invoking the Yuan2-2B-MoE model with the following code:
36
+
37
+ ```python
38
+ import torch, transformers
39
+ import sys, os
40
+ sys.path.append(
41
+ os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
42
+ from transformers import AutoModelForCausalLM,AutoTokenizer,LlamaTokenizer
43
+
44
+ print("Creat tokenizer...")
45
+ tokenizer = LlamaTokenizer.from_pretrained('IEITYuan/Yuan2-2B-hf-moe', add_eos_token=False, add_bos_token=False, eos_token='<eod>')
46
+ tokenizer.add_tokens(['<sep>', '<pad>', '<mask>', '<predict>', '<FIM_SUFFIX>', '<FIM_PREFIX>', '<FIM_MIDDLE>','<commit_before>','<commit_msg>','<commit_after>','<jupyter_start>','<jupyter_text>','<jupyter_code>','<jupyter_output>','<empty_output>'], special_tokens=True)
47
+
48
+ print("Creat model...")
49
+ model = AutoModelForCausalLM.from_pretrained('IEITYuan/Yuan2-2B-hf-moe', device_map='auto', torch_dtype=torch.bfloat16, trust_remote_code=True)
50
+
51
+ inputs = tokenizer("请问目前最先进的机器学习算法有哪些?", return_tensors="pt")["input_ids"].to("cuda:0")
52
+ outputs = model.generate(inputs,do_sample=False,max_length=100)
53
+ print(tokenizer.decode(outputs[0]))
54
+
55
+ ```
56
+
57
+ # <span id="Benchmark">Benchmark评估/Benchmark Evaluation</span>
58
+ 我们提供了[HumanEval](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_humaneval.md),[AGIEval-GK-Math](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_agieval_math.md),[GSM8K](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_gsm8k.md)和[TruthfulQA](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_TruthfulQA.md)的评估脚本。在4个典型任务上,我们用源2.0不同版本模型上进行了性能测试。
59
+
60
+ We have provided evaluation scripts for [HumanEval](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_humaneval.md),[AGIEval-GK-Math](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_agieval_math.md),[GSM8K](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_gsm8k.md) and [TruthfulQA](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/docs/eval_TruthfulQA.md). Performance tests were conducted on different versions of the Yuan2.0 model for four typical tasks.
61
+
62
+
63
+ | Model | GSM8K | AGIEval-GK-Math-QA | AGIEval-GK-Math-Cloze | HumanEval | TurthfulQA |
64
+ | ----------------- | :----: | :------------: | :---------------: | :-------: | ---------- |
65
+ | GPT-4 | 92% | 47.0% | 16.1% | 86.6% | 59% |
66
+ | ChatGPT | 68.6%\* | 36.5% | 7.3% | 66.5%\* | 34%\* |
67
+ | Llama2 | 56.8% | - | - | 29.9% | - |
68
+ | 源2.0-102B | 76.6% | 38.7% | 13.5% | 67.1% | 58% |
69
+ | 源2.0-102B-SC | 86.2% | 45.5% | 15.2% | 77.4% | - |
70
+
71
+ \* 使用与源2.0完全相同的输入数据对ChatGPT进行测试,时间2023年11月
72
+
73
+ \* Testing ChatGPT using the same input data as Yuan2.0, as of November 2023.
74
+
75
+ # <span id="Terms">声明与协议/Terms and Conditions</span>
76
+ 对该模型的原代码仓库使用遵循开源许可协议 Apache 2.0。
77
+
78
+ 源2.0模型支持商用,不需要申请授权,请您了解并遵循[《源2.0模型许可协议》](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/LICENSE-Yuan),勿将开源模型和代码及基于开源项目产生的衍生物用于任何可能给国家和社会带来危害的用途以及用于任何未经过安全评估和备案的服务。
79
+
80
+ 尽管模型在训练时我们已采取措施尽力确保数据的合规性和准确性,但模型参数量巨大且受概率随机性因素影响,我们无法保证输出内容的准确性,且模型易被输入指令所误导,本项目不承担开源模型和代码导致的数据安全、舆情风险或发生任何模型被误导、滥用、传播、不当利用而产生的风险和责任。**您将对通过使用、复制、分发和修改模型等方式利用该开源项目所产生的风险与后果,独自承担全部责任。**
81
+
82
+ The use of the original code repository for this model requires compliance with the open source license agreement Apache 2.0. The Yuan2.0 model supports commercial use and does not require authorization. Please understand and comply with the [《Yuan 2.0 Model License Agreement》](https://github.com/IEIT-Yuan/Yuan-2.0/blob/main/LICENSE-Yuan). Do not use the open source model and code, as well as derivatives generated from open source projects, for any purposes that may cause harm to the country and society, or for any services that have not undergone security assessment and filing. Although we have taken measures to ensure the compliance and accuracy of the data during training, the model has a huge number of parameters and is affected by probability and randomness factors. We cannot guarantee the accuracy of the output content, and the model is easily misled by input instructions. This project does not assume any data security, public opinion risks, or any model misleading, abusing, spreading caused by open-source models and code Risks and responsibilities arising from improper utilization **You will be solely responsible for the risks and consequences arising from the use, copying, distribution, and modification of the model in this open source project.**
83
+
84
+ # <span id="Cite">引用/Cite</span>
85
+ 欢迎阅读我们的技术报告 [YUAN 2.0: A Large Language Model with Localized Filtering-based Attention](http://arxiv.org/pdf/2311.15786.pdf)!
86
+
87
+ Welcome to read our technical report [YUAN 2.0: A Large Language Model with Localized Filtering-based Attention](http://arxiv.org/pdf/2311.15786.pdf)!
88
+
89
+ ```latex
90
+ @article{Wu2023,
91
+ title = {{YUAN 2.0: A Large Language Model with Localized Filtering-based Attention}},
92
+ author = {Wu, Shaohua and Zhao, Xudong and Wang, Shenling and Luo, Jiangang and Li, Lingjun and Chen, Xi and Zhao, Bing and Wang, Wei and Yu, Tong and Zhang, Rongguo and Zhang, Jiahua and Wang, Chao},
93
+ url = {http://arxiv.org/abs/2311.15786},
94
+ year = {2023}
95
+ }
96
+
97
+ ```
config.json ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "_name_or_path": "/mnt/beegfs2/sunzeyu/bin_522-7/",
4
+ "architectures": [
5
+ "YuanForCausalLM"
6
+ ],
7
+ "attention_projection_size": 4096,
8
+ "auto_map": {
9
+ "AutoConfig": "configuration_yuan.YuanConfig",
10
+ "AutoModelForCausalLM": "yuan_hf_model.YuanForCausalLM"
11
+ },
12
+ "bos_token_id": 77185,
13
+ "causal_mask": true,
14
+ "dropout": 0,
15
+ "eod_token": 77185,
16
+ "eod_token_id": 77185,
17
+ "eos_token_id": 77185,
18
+ "hidden_act": "silu",
19
+ "hidden_size": 2048,
20
+ "initializer_range": 0.02,
21
+ "intermediate_size": 8192,
22
+ "mask_token_id": 77185,
23
+ "max_position_embeddings": 4096,
24
+ "model_max_length": 8192,
25
+ "model_type": "yuan",
26
+ "moe_config": {
27
+ "ffn_hidden_size": 8192,
28
+ "gated_linear_unit": true,
29
+ "moe_num_experts": 32,
30
+ "moe_top_k": 2,
31
+ "norm_topk_prob": true
32
+ },
33
+ "num_attention_heads": 16,
34
+ "num_hidden_layers": 24,
35
+ "output_router_logits": true,
36
+ "pad_token_id": 77185,
37
+ "reset_attention_mask": false,
38
+ "reset_position_ids": true,
39
+ "rms_norm_eps": 1e-06,
40
+ "sep_token": 77187,
41
+ "sep_token_id": 77185,
42
+ "tokenizer_class": "YuanTokenizer",
43
+ "torch_dtype": "bfloat16",
44
+ "transformers_version": "4.30.2",
45
+ "use_cache": true,
46
+ "use_flash_attention": true,
47
+ "use_loss_mask": false,
48
+ "use_moe": true,
49
+ "vocab_size": 135040
50
+ }
configuration.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"framework":"Pytorch","task":"chatbot"}
configuration_yuan.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers.configuration_utils import PretrainedConfig
3
+
4
+
5
+ class YuanConfig(PretrainedConfig):
6
+ model_type = "yuan"
7
+ keys_to_ignore_at_inference = ["past_key_values"]
8
+
9
+ def __init__(
10
+ self,
11
+ vocab_size=135040,
12
+ hidden_size=2048,
13
+ intermediate_size=8192,
14
+ num_hidden_layers=24,
15
+ num_attention_heads=32,
16
+ hidden_act="silu",
17
+ model_max_length=8192,
18
+ initializer_range=0.02,
19
+ rms_norm_eps=1e-6,
20
+ use_cache=True,
21
+ pad_token_id=77185,
22
+ bos_token_id=77185,
23
+ eos_token_id=77185,
24
+ tie_word_embeddings=True,
25
+ **kwargs,
26
+ ):
27
+ self.vocab_size = vocab_size
28
+ self.model_max_length = model_max_length
29
+ self.hidden_size = hidden_size
30
+ self.intermediate_size = intermediate_size
31
+ self.num_hidden_layers = num_hidden_layers
32
+ self.num_attention_heads = num_attention_heads
33
+ self.hidden_act = hidden_act
34
+ self.initializer_range = initializer_range
35
+ self.rms_norm_eps = rms_norm_eps
36
+ self.use_cache = use_cache
37
+ super().__init__(
38
+ pad_token_id=pad_token_id,
39
+ bos_token_id=bos_token_id,
40
+ eos_token_id=eos_token_id,
41
+ tie_word_embeddings=tie_word_embeddings,
42
+ **kwargs,
43
+ )
44
+
generation_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 77185,
4
+ "eos_token_id": 77185,
5
+ "pad_token_id": 77185,
6
+ "transformers_version": "4.30.2"
7
+ }
pytorch_model-00001-of-00009.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a3ae777259524efea4b7da15a5dee99d1503a04db6a7604128bafbbd8b0f0d2b
3
+ size 9966449668
pytorch_model.bin.index.json ADDED
The diff for this file is too large to render. See raw diff
 
special_tokens_map.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": true,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "unk_token": {
17
+ "content": "<unk>",
18
+ "lstrip": false,
19
+ "normalized": true,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ }
23
+ }
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:36f79e0c70f73cdd2a8dd0fbe7bfe290da158eea746778d289e4ad76c8b383d9
3
+ size 2155861
tokenizer_config.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_eos_token": false,
4
+ "bos_token": {
5
+ "__type": "AddedToken",
6
+ "content": "<s>",
7
+ "lstrip": false,
8
+ "normalized": true,
9
+ "rstrip": false,
10
+ "single_word": false
11
+ },
12
+ "clean_up_tokenization_spaces": false,
13
+ "eos_token": {
14
+ "__type": "AddedToken",
15
+ "content": "</s>",
16
+ "lstrip": false,
17
+ "normalized": true,
18
+ "rstrip": false,
19
+ "single_word": false
20
+ },
21
+ "model_max_length": 1000000000000000019884624838656,
22
+ "pad_token": null,
23
+ "sp_model_kwargs": {},
24
+ "tokenizer_class": "LlamaTokenizer",
25
+ "unk_token": {
26
+ "__type": "AddedToken",
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": true,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }
yuan_moe_hf_model.py ADDED
@@ -0,0 +1,1454 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Yuan model."""
21
+ import math
22
+ from typing import List, Optional, Tuple, Union
23
+ import torch.nn.functional as F
24
+ import torch
25
+ import torch.utils.checkpoint
26
+ from torch import nn
27
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
28
+ from transformers.activations import ACT2FN
29
+ from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
30
+ from transformers.modeling_utils import PreTrainedModel
31
+ from transformers.utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
32
+ from configuration_yuan import YuanConfig
33
+ from einops import rearrange
34
+ #from flash_attn import flash_attn_varlen_func as flash_attn_unpadded_func
35
+ #from flash_attn import flash_attn_func
36
+
37
+ import copy
38
+
39
+ try:
40
+ from flash_attn import flash_attn_varlen_func as flash_attn_unpadded_func
41
+ from flash_attn import flash_attn_func
42
+ except ImportError:
43
+ flash_attn_unpadded_func = None
44
+
45
+
46
+ logger = logging.get_logger(__name__)
47
+
48
+ _CONFIG_FOR_DOC = "YuanConfig"
49
+
50
+
51
+ class LocalizedFiltering(torch.nn.Module):
52
+ """
53
+ Mega's Exponential Moving Average layer, largely left unmodified from the original repo with the exception of
54
+ variable names and moving away from the stateful representation of incremental decoding state. See
55
+ "https://arxiv.org/abs/2209.10655" for more details.
56
+ """
57
+
58
+ def __init__(self, hidden_size):
59
+ super().__init__()
60
+
61
+ self.embed_dim = hidden_size
62
+ self.lf_conv2d_group = 1
63
+ self.lf_conv2d_num_pad = 1
64
+
65
+ self.conv1 = torch.nn.Conv2d(self.embed_dim, self.embed_dim // 2, (2, 1), stride=(1, 1), padding=(self.lf_conv2d_num_pad, 0), groups=self.lf_conv2d_group)
66
+ self.conv2 = torch.nn.Conv2d(self.embed_dim // 2, self.embed_dim, (2, 1), stride=(1, 1), padding=(self.lf_conv2d_num_pad, 0), groups=self.lf_conv2d_group)
67
+ self.output_layernorm = YuanRMSNorm(self.embed_dim)
68
+
69
+ def _train_forward(self, inputs):
70
+ inputs = inputs.transpose(0,1)
71
+ seq_len, bsz, embed_dim = inputs.size()
72
+ if embed_dim != self.embed_dim:
73
+ raise ValueError(
74
+ f"Unexpected embedding dimension received: input is {embed_dim}, model expects {self.embed_dim}"
75
+ )
76
+ residual = inputs
77
+
78
+ inputs = inputs.view(seq_len, 1, bsz, embed_dim).permute(2, 3, 0, 1)
79
+ output1 = self.conv1(inputs)
80
+ output1 = output1[:, :, :seq_len, :]
81
+
82
+ output2 = self.conv2(output1)
83
+ output2 = output2[:, :, :seq_len, :].permute(2, 3, 0, 1).contiguous()
84
+ output2 = output2.view(seq_len, bsz, embed_dim)
85
+ assert output2.shape == residual.shape
86
+
87
+ lf_output = self.output_layernorm(output2 + residual)
88
+ lf_output = lf_output.transpose(0,1)
89
+ return lf_output
90
+
91
+ def _inference_forward(self, inputs, before_hidden_states):
92
+
93
+ if before_hidden_states is None:
94
+ inputs = inputs.transpose(0,1)
95
+ seq_len, bsz, embed_dim = inputs.size()
96
+ if embed_dim != self.embed_dim:
97
+ raise ValueError(
98
+ f"Unexpected embedding dimension received: input is {embed_dim}, model expects {self.embed_dim}"
99
+ )
100
+ residual = inputs
101
+
102
+ inputs = inputs.view(seq_len, 1, bsz, embed_dim).permute(2, 3, 0, 1)
103
+ output1 = self.conv1(inputs)
104
+ output1 = output1[:, :, :seq_len, :]
105
+
106
+ output2 = self.conv2(output1)
107
+ output2 = output2[:, :, :seq_len, :].permute(2, 3, 0, 1).contiguous()
108
+ output2 = output2.view(seq_len, bsz, embed_dim)
109
+ assert output2.shape == residual.shape
110
+
111
+ lf_output = self.output_layernorm(output2 + residual)
112
+ lf_output = lf_output.transpose(0,1)
113
+ return lf_output
114
+ else:
115
+ inputs = inputs.transpose(0,1)
116
+ before_hidden_states = before_hidden_states.transpose(0,1)
117
+ residual = inputs
118
+
119
+ seq_len, bsz, embed_dim = inputs.size()
120
+ seq_len_before, _, _ = before_hidden_states.size()
121
+
122
+ assert seq_len == 1 and seq_len_before == 2
123
+
124
+ inputs = torch.cat((before_hidden_states, inputs), dim=0)
125
+ inputs = inputs.view(3, 1, bsz, embed_dim).permute(2, 3, 0, 1)
126
+
127
+ output1 = self.conv1(inputs)
128
+ output2 = self.conv2(output1[:,:,1:-1,:])
129
+ output2 = output2[:,:,1:-1,:]
130
+ output2 = output2.view(1, bsz, embed_dim)
131
+ assert output2.shape == residual.shape
132
+
133
+ lf_output = self.output_layernorm(output2 + residual)
134
+ lf_output = lf_output.transpose(0,1)
135
+
136
+ return lf_output
137
+
138
+
139
+
140
+ def forward(
141
+ self,
142
+ inputs,
143
+ before_hidden_states
144
+ ) -> torch.Tensor:
145
+ assert self.lf_conv2d_num_pad == 1
146
+ if self.training:
147
+ lf_output = self._train_forward(inputs)
148
+ else:
149
+ lf_output = self._inference_forward(inputs, before_hidden_states)
150
+
151
+ return lf_output
152
+
153
+
154
+ # Copied from transformers.models.bart.modeling_bart._make_causal_mask
155
+ def _make_causal_mask(
156
+ input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
157
+ ):
158
+ """
159
+ Make causal mask used for bi-directional self-attention.
160
+ """
161
+ bsz, tgt_len = input_ids_shape
162
+ mask = torch.full((tgt_len, tgt_len), torch.tensor(torch.finfo(dtype).min, device=device), device=device)
163
+ mask_cond = torch.arange(mask.size(-1), device=device)
164
+ mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
165
+ mask = mask.to(dtype)
166
+
167
+ if past_key_values_length > 0:
168
+ mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
169
+ return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
170
+
171
+
172
+ # Copied from transformers.models.bart.modeling_bart._expand_mask
173
+ def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
174
+ """
175
+ Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
176
+ """
177
+ bsz, src_len = mask.size()
178
+ tgt_len = tgt_len if tgt_len is not None else src_len
179
+
180
+ expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
181
+
182
+ inverted_mask = 1.0 - expanded_mask
183
+
184
+ return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
185
+
186
+
187
+ def rotate_half(x):
188
+ """Rotates half the hidden dims of the input."""
189
+ x1 = x[..., : x.shape[-1] // 2]
190
+ x2 = x[..., x.shape[-1] // 2 :]
191
+ return torch.cat((-x2, x1), dim=-1)
192
+
193
+ def apply_rotary_pos_emb_0(q, k, cos, sin, position_ids):
194
+ # The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
195
+ rot_dim = sin.shape[-1]
196
+
197
+ cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
198
+ sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
199
+ cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
200
+ sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
201
+
202
+ q, q_pass = q[..., :rot_dim], q[..., rot_dim:]
203
+ k, k_pass = k[..., :rot_dim], k[..., rot_dim:]
204
+
205
+ q_embed = (q * cos) + (rotate_half(q) * sin)
206
+ k_embed = (k * cos) + (rotate_half(k) * sin)
207
+
208
+ return torch.cat((q_embed, q_pass), dim=-1), torch.cat((k_embed, k_pass), dim=-1)
209
+
210
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
211
+ # The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
212
+ #import pdb;pdb.set_trace()
213
+ cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
214
+ sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
215
+ cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
216
+ sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
217
+ q_embed = (q * cos) + (rotate_half(q) * sin)
218
+ k_embed = (k * cos) + (rotate_half(k) * sin)
219
+ return q_embed, k_embed
220
+
221
+ class YuanRMSNorm(nn.Module):
222
+ def __init__(self, hidden_size, eps=1e-6):
223
+ """
224
+ YuanRMSNorm is equivalent to LlamaRMSNorm
225
+ """
226
+ super().__init__()
227
+ self.weight = nn.Parameter(torch.ones(hidden_size))
228
+ self.variance_epsilon = eps
229
+
230
+ def forward(self, hidden_states):
231
+ input_dtype = hidden_states.dtype
232
+ hidden_states = hidden_states.to(torch.float32)
233
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
234
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
235
+ return self.weight * hidden_states.to(input_dtype)
236
+
237
+ class YuanRotaryEmbedding(torch.nn.Module):
238
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
239
+
240
+ """
241
+ YuanRotaryEmbedding is equivalent to LlamaRotaryEmbedding in transformers v4.36
242
+ """
243
+
244
+ super().__init__()
245
+
246
+ self.dim = dim
247
+ self.max_position_embeddings = max_position_embeddings
248
+ self.base = base
249
+
250
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
251
+ inv_freq = inv_freq.to(torch.bfloat16)
252
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
253
+
254
+ # Build here to make `torch.jit.trace` work.
255
+ self._set_cos_sin_cache(
256
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
257
+ )
258
+
259
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
260
+ self.max_seq_len_cached = seq_len
261
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
262
+
263
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
264
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
265
+ emb = torch.cat((freqs, freqs), dim=-1)
266
+ self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
267
+ self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
268
+
269
+ def forward(self, x, seq_len=None):
270
+ # x: [bs, num_attention_heads, seq_len, head_size]
271
+ if seq_len > self.max_seq_len_cached:
272
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
273
+
274
+ return (
275
+ self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
276
+ self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
277
+ )
278
+
279
+ # flash attn
280
+ class FlashSelfAttention(torch.nn.Module):
281
+ """Implement the scaled dot product attention with softmax.
282
+ Arguments
283
+ ---------
284
+ softmax_scale: The temperature to use for the softmax attention.
285
+ (default: 1/sqrt(d_keys) where d_keys is computed at
286
+ runtime)
287
+ attention_dropout: The dropout rate to apply to the attention
288
+ (default: 0.0)
289
+ """
290
+ def __init__(self, causal=False, softmax_scale=None, attention_dropout=0.0,
291
+ device=None, dtype=None):
292
+ super().__init__()
293
+ assert flash_attn_unpadded_func is not None, ('Please install FlashAttention first, '
294
+ 'e.g., with pip install flash-attn')
295
+ assert rearrange is not None, 'Please install einops first, e.g., with pip install einops'
296
+ self.causal = causal
297
+ self.softmax_scale = softmax_scale
298
+ self.dropout_p = attention_dropout
299
+
300
+ def forward(self, q, k, v):
301
+ """Implements the multihead softmax attention.
302
+ Arguments
303
+ ---------
304
+ q, k, v: The tensor containing the query, key, and value. (B, S, H, D)
305
+ """
306
+
307
+ assert all((i.dtype in [torch.float16, torch.bfloat16] for i in (q,k,v)))
308
+ assert all((i.is_cuda for i in (q,k,v)))
309
+
310
+ batch_size, seqlen_q = q.shape[0], q.shape[1]
311
+ seqlen_k = k.shape[1]
312
+
313
+ q, k, v = [rearrange(x, 'b s ... -> (b s) ...') for x in [q, k, v]]
314
+ cu_seqlens_q = torch.arange(0, (batch_size + 1) * seqlen_q, step=seqlen_q, dtype=torch.int32,
315
+ device=q.device)
316
+
317
+ if self.training:
318
+ # during training q,k,v always have same seqlen
319
+ assert seqlen_k == seqlen_q
320
+
321
+ is_causal = self.causal
322
+ cu_seqlens_k = cu_seqlens_q
323
+ dropout_p = self.dropout_p
324
+ else:
325
+ # turn off FA causal mask after first inference autoregressive iteration
326
+ # only on first autoregressive step q,k,v have same seqlen
327
+ is_causal = seqlen_q == seqlen_k
328
+ cu_seqlens_k = torch.arange(0, (batch_size + 1) * seqlen_k, step=seqlen_k, dtype=torch.int32,
329
+ device=q.device)
330
+ dropout_p = 0
331
+
332
+ output = flash_attn_unpadded_func(
333
+ q, k, v, cu_seqlens_q, cu_seqlens_k, seqlen_q, seqlen_k,
334
+ dropout_p,
335
+ softmax_scale=self.softmax_scale, causal=is_causal
336
+ )
337
+
338
+ output = rearrange(output, '(b s) ... -> b s ...', b=batch_size)
339
+ return output
340
+
341
+
342
+ class ParallelAttention_router(nn.Module):
343
+ def __init__(self, config):
344
+ super(ParallelAttention_router, self).__init__()
345
+ layer_number=0
346
+ self.layer_number = max(1, layer_number)
347
+
348
+
349
+ self.flash_attn_drop = 0.01
350
+ self.hidden_size = config.hidden_size
351
+ self.projection_size = config.moe_config['moe_num_experts']
352
+
353
+ self.query = nn.Linear(self.hidden_size, self.projection_size, bias=False)
354
+ self.key = nn.Linear(self.hidden_size, self.projection_size, bias=False)
355
+ self.value = nn.Linear(self.hidden_size, self.projection_size, bias=False)
356
+
357
+
358
+ def forward(self, hidden_states, attention_mask=None, enc_position_ids=None,
359
+ encoder_output=None, inference_params=None,
360
+ rotary_pos_emb=None):
361
+ is_first_step = False
362
+ before_hidden_states = None
363
+
364
+ query_layer = self.query(hidden_states)
365
+ key_layer = self.key(hidden_states)
366
+ value_layer = self.value(hidden_states)
367
+
368
+ b = query_layer.size(0)
369
+ s = query_layer.size(1) # seq*batch = token_num
370
+ z = query_layer.size(2) # expert_num
371
+
372
+ # use fp32 router
373
+ query_layer = query_layer.float().view(b,s,z,1)
374
+ key_layer = key_layer.float().view(b,s,z,1)
375
+ value_layer = value_layer.float().view(b,s,z,1)
376
+
377
+
378
+ attn_weights = torch.matmul(query_layer, key_layer.transpose(2, 3))
379
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
380
+
381
+ attn_output = torch.matmul(attn_weights, value_layer)
382
+
383
+ router_output = attn_output.view(b*s, z)
384
+
385
+ return router_output
386
+
387
+ class YuanExpertMLP(nn.Module):
388
+ def __init__(self, config):
389
+ super(YuanExpertMLP, self).__init__()
390
+
391
+ self.gated_linear_unit = config.moe_config['gated_linear_unit']
392
+ self.ffn_hidden_size = config.moe_config['ffn_hidden_size']
393
+
394
+
395
+ if self.gated_linear_unit:
396
+ self.w1 = nn.Linear(config.hidden_size, self.ffn_hidden_size*2, bias=False)
397
+
398
+
399
+ else:
400
+ self.w1 = nn.Linear(config.hidden_size, self.ffn_hidden_size, bias=False)
401
+
402
+ self.act_fn = ACT2FN[config.hidden_act]
403
+ self.w2 = nn.Linear(self.ffn_hidden_size, config.hidden_size, bias=False)
404
+
405
+
406
+ def forward(self, x):
407
+ x = self.w1(x)
408
+ if self.gated_linear_unit:
409
+ x = torch.chunk(x, 2, dim=-1)
410
+ x = self.act_fn(x[0]) * x[1]
411
+ else:
412
+ x = self.act_fn(x)
413
+ x = self.w2(x)
414
+ return x
415
+
416
+
417
+
418
+ class YuanMLP(nn.Module):
419
+ def __init__(
420
+ self,
421
+ hidden_size: int,
422
+ intermediate_size: int,
423
+ hidden_act: str
424
+ ):
425
+ super().__init__()
426
+ self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
427
+ self.gate_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
428
+ self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False)
429
+ self.act_fn = ACT2FN[hidden_act]
430
+
431
+ def forward(self, x):
432
+ return self.down_proj(self.gate_proj(x) * self.act_fn(self.up_proj(x)))
433
+
434
+
435
+ class YuanAttention(nn.Module):
436
+ """Localized Filtering-based Attention 'YUAN 2.0: A Large Language Model with Localized Filtering-based Attention' paper"""
437
+
438
+ def __init__(self, config: YuanConfig):
439
+ super().__init__()
440
+ self.config = config
441
+ self.hidden_size = config.hidden_size
442
+ self.num_heads = config.num_attention_heads
443
+
444
+ try:
445
+ self.attention_projection_size = config.attention_projection_size
446
+ except:
447
+ self.attention_projection_size = None
448
+
449
+ if self.attention_projection_size is None:
450
+ self.head_dim = self.hidden_size // self.num_heads
451
+ else:
452
+ self.head_dim = self.attention_projection_size // self.num_heads
453
+
454
+ self.max_position_embeddings = config.max_position_embeddings
455
+ self.causal_mask = config.causal_mask
456
+ self.softmax_scale = 1.0 / math.sqrt(self.head_dim)
457
+ self.use_flash_attention = config.use_flash_attention
458
+ try:
459
+ self.use_shareqk = config.use_shareqk
460
+ except Exception as e:
461
+ self.use_shareqk=False
462
+ self.dropout = 0.0
463
+
464
+ self.v_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
465
+ self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
466
+
467
+ if self.head_dim == self.hidden_size // self.num_heads:
468
+ self.rotary_emb = YuanRotaryEmbedding(self.head_dim, max_position_embeddings=self.max_position_embeddings)
469
+
470
+ else:
471
+ self.rotary_emb = YuanRotaryEmbedding(self.hidden_size // self.num_heads, max_position_embeddings=self.max_position_embeddings)
472
+
473
+ if self.use_shareqk:
474
+ self.qk_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
475
+ self.qk_weight = nn.Parameter(torch.Tensor(2, self.hidden_size))
476
+ self.qk_bias = nn.Parameter(torch.Tensor(2, self.hidden_size))
477
+ else:
478
+ self.lf_gate = LocalizedFiltering(self.hidden_size)
479
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
480
+ self.k_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
481
+
482
+
483
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
484
+ return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
485
+
486
+ def forward(
487
+ self,
488
+ hidden_states: torch.Tensor,
489
+ attention_mask: Optional[torch.Tensor] = None,
490
+ position_ids: Optional[torch.LongTensor] = None,
491
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
492
+ output_attentions: bool = False,
493
+ use_cache: bool = False,
494
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
495
+
496
+ bsz, q_len, _ = hidden_states.size()
497
+ before_hidden_states = None
498
+ is_first_step = False
499
+ if use_cache:
500
+ if past_key_value is None:
501
+ inference_hidden_states_memory = torch.empty(bsz, 2, hidden_states.shape[2], dtype=hidden_states.dtype)
502
+ is_first_step = True
503
+ else:
504
+ before_hidden_states = past_key_value[2]
505
+
506
+ if use_cache:
507
+ if is_first_step:
508
+ if q_len >= 2:
509
+ inference_hidden_states_memory = hidden_states[ :, -2:, :]
510
+ else:
511
+ inference_hidden_states_memory[:, :, :] = 0
512
+ inference_hidden_states_memory[:, -1:, :] = hidden_states[:, -1:, :]
513
+ else:
514
+ hidden_states_tmp = before_hidden_states[:, -1:, :]
515
+ inference_hidden_states_memory = copy.deepcopy(torch.cat((hidden_states_tmp, hidden_states), dim=1))
516
+
517
+ value_states = self.v_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
518
+ if self.use_shareqk:
519
+ qk_states = self.qk_proj(hidden_states).view(bsz, q_len, self.num_heads*self.head_dim)
520
+ query_key = qk_states.unsqueeze(2) * self.qk_weight + self.qk_bias
521
+ query_states, key_states = torch.unbind(query_key, dim=2)
522
+
523
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
524
+ key_states = key_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
525
+ else:
526
+ hidden_states = self.lf_gate(hidden_states,before_hidden_states)
527
+ query_states = self.q_proj(hidden_states)
528
+ key_states = self.k_proj(hidden_states)
529
+ qk_states = torch.cat([query_states, key_states], dim=-1)
530
+ qk_states = qk_states.view(bsz,q_len,self.num_heads,int(qk_states.shape[-1]//self.num_heads))
531
+ (query_states,key_states) = torch.chunk(qk_states, 2, dim=-1)
532
+ query_states = query_states.transpose(1, 2)
533
+ key_states = key_states.transpose(1, 2)
534
+
535
+ kv_seq_len = key_states.shape[-2]
536
+ if past_key_value is not None:
537
+ kv_seq_len += past_key_value[0].shape[-2]
538
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
539
+
540
+ query_states, key_states = apply_rotary_pos_emb_0(query_states, key_states, cos, sin, position_ids)
541
+
542
+ if past_key_value is not None:
543
+ # reuse k, v, self_attention
544
+ key_states = torch.cat([past_key_value[0], key_states], dim=2)
545
+ value_states = torch.cat([past_key_value[1], value_states], dim=2)
546
+
547
+ past_key_value = (key_states, value_states,inference_hidden_states_memory) if use_cache else None
548
+ if self.use_flash_attention:
549
+ attn_weights = None
550
+ query_states = query_states.transpose(1, 2)
551
+ key_states = key_states.transpose(1, 2)
552
+ value_states = value_states.transpose(1, 2)
553
+
554
+ batch_size, seqlen_q = query_states.shape[0], query_states.shape[1]
555
+ seqlen_k = key_states.shape[1]
556
+
557
+ q, k, v = [rearrange(x, 'b s ... -> (b s) ...') for x in [query_states, key_states, value_states]]
558
+
559
+ cu_seqlens_q = torch.arange(0, (batch_size + 1) * seqlen_q, step=seqlen_q, dtype=torch.int,
560
+ device=q.device)
561
+
562
+ if self.training:
563
+ assert seqlen_k == seqlen_q
564
+ cu_seqlens_k = cu_seqlens_q
565
+ is_causal = self.causal_mask
566
+ else:
567
+ is_causal = seqlen_q == seqlen_k
568
+ cu_seqlens_k = torch.arange(0, (batch_size + 1) * seqlen_k, step=seqlen_k, dtype=torch.int,
569
+ device=q.device)
570
+ self.dropout=0
571
+
572
+ output = flash_attn_unpadded_func(
573
+ q, k, v, cu_seqlens_q, cu_seqlens_k, seqlen_q, seqlen_k, self.dropout, causal=is_causal
574
+ )
575
+
576
+ attn_output = rearrange(output, '(b s) ... -> b s ...', b=batch_size)
577
+ else:
578
+ attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
579
+
580
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
581
+ raise ValueError(
582
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
583
+ f" {attn_weights.size()}"
584
+ )
585
+ if attention_mask is not None:
586
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
587
+ raise ValueError(
588
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
589
+ )
590
+ attn_weights = attn_weights + attention_mask
591
+ attn_weights = torch.max(attn_weights, torch.tensor(torch.finfo(attn_weights.dtype).min))
592
+
593
+ # upcast attention to fp32
594
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
595
+ attn_output = torch.matmul(attn_weights, value_states)
596
+
597
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
598
+ raise ValueError(
599
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
600
+ f" {attn_output.size()}"
601
+ )
602
+
603
+ attn_output = attn_output.transpose(1, 2)
604
+
605
+ if self.attention_projection_size is None:
606
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
607
+ else:
608
+ attn_output = attn_output.reshape(bsz, q_len, self.attention_projection_size)
609
+
610
+ attn_output = self.o_proj(attn_output)
611
+
612
+ if not output_attentions:
613
+ attn_weights = None
614
+ return attn_output, attn_weights, past_key_value
615
+
616
+
617
+
618
+ class YuanMoeLayer(nn.Module):
619
+ def __init__(self, config):
620
+ super().__init__()
621
+ self.num_experts = config.moe_config['moe_num_experts']
622
+ self.top_k = config.moe_config['moe_top_k']
623
+ self.norm_topk_prob = config.moe_config['norm_topk_prob']
624
+ self.hidden_size = config.hidden_size
625
+
626
+
627
+ self.gate = ParallelAttention_router(config)
628
+ self.experts = nn.ModuleList(
629
+ [YuanExpertMLP(config) for _ in range(self.num_experts)]
630
+ )
631
+
632
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
633
+ batch_size, sequence_length, hidden_dim = hidden_states.shape
634
+
635
+ # router_logits: (batch * sequence_length, n_experts)
636
+ router_logits = self.gate(hidden_states)
637
+ hidden_states = hidden_states.view(-1, hidden_dim)
638
+
639
+ routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float)
640
+ routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
641
+ if self.norm_topk_prob:
642
+ routing_weights /= routing_weights.sum(dim=-1, keepdim=True)
643
+ # we cast back to the input dtype
644
+ routing_weights = routing_weights.to(hidden_states.dtype)
645
+
646
+ final_hidden_states = torch.zeros(
647
+ (batch_size * sequence_length, hidden_dim), dtype=hidden_states.dtype, device=hidden_states.device
648
+ )
649
+
650
+ # One hot encode the selected experts to create an expert mask
651
+ # this will be used to easily index which expert is going to be sollicitated
652
+ expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0)
653
+
654
+ # Loop over all available experts in the model and perform the computation on each expert
655
+ for expert_idx in range(self.num_experts):
656
+ expert_layer = self.experts[expert_idx]
657
+ idx, top_x = torch.where(expert_mask[expert_idx])
658
+
659
+ if top_x.shape[0] == 0:
660
+ continue
661
+
662
+ # in torch it is faster to index using lists than torch tensors
663
+ top_x_list = top_x.tolist()
664
+ idx_list = idx.tolist()
665
+
666
+ # Index the correct hidden states and compute the expert hidden state for
667
+ # the current expert. We need to make sure to multiply the output hidden
668
+ # states by `routing_weights` on the corresponding tokens (top-1 and top-2)
669
+ current_state = hidden_states[None, top_x_list].reshape(-1, hidden_dim)
670
+ current_hidden_states = expert_layer(current_state) * routing_weights[top_x_list, idx_list, None]
671
+
672
+ # However `index_add_` only support torch tensors for indexing so we'll use
673
+ # the `top_x` tensor here.
674
+ final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))
675
+
676
+ final_hidden_states = final_hidden_states.reshape(batch_size, sequence_length, hidden_dim)
677
+ return final_hidden_states, router_logits
678
+
679
+
680
+ class YuanDecoderLayer(nn.Module):
681
+ def __init__(self, config: YuanConfig):
682
+ super().__init__()
683
+ self.hidden_size = config.hidden_size
684
+ self.self_attn = YuanAttention(config=config)
685
+
686
+ if config.moe_config['moe_num_experts'] > 0:
687
+ self.mlp = YuanMoeLayer(config)
688
+ else:
689
+ self.mlp = YuanMLP(
690
+ hidden_size=self.hidden_size,
691
+ intermediate_size=config.intermediate_size,
692
+ hidden_act=config.hidden_act,
693
+ )
694
+
695
+
696
+ self.input_layernorm = YuanRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
697
+ self.post_attention_layernorm = YuanRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
698
+
699
+ def forward(
700
+ self,
701
+ hidden_states: torch.Tensor,
702
+ attention_mask: Optional[torch.Tensor] = None,
703
+ position_ids: Optional[torch.LongTensor] = None,
704
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
705
+ output_attentions: Optional[bool] = False,
706
+ use_cache: Optional[bool] = False,
707
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
708
+ """
709
+ Args:
710
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
711
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
712
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
713
+ output_attentions (`bool`, *optional*):
714
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
715
+ returned tensors for more detail.
716
+ use_cache (`bool`, *optional*):
717
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
718
+ (see `past_key_values`).
719
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
720
+ """
721
+ residual = hidden_states
722
+ hidden_states = self.input_layernorm(hidden_states)
723
+
724
+ # Self Attention
725
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
726
+ hidden_states=hidden_states,
727
+ attention_mask=attention_mask,
728
+ position_ids=position_ids,
729
+ past_key_value=past_key_value,
730
+ output_attentions=output_attentions,
731
+ use_cache=use_cache,
732
+ )
733
+
734
+ hidden_states = residual + hidden_states
735
+
736
+ # Fully Connected
737
+ residual = hidden_states
738
+
739
+ hidden_states = self.post_attention_layernorm(hidden_states)
740
+
741
+ hidden_states, router_logits = self.mlp(hidden_states)
742
+
743
+ hidden_states = residual + hidden_states
744
+
745
+ outputs = (hidden_states,)
746
+
747
+ if output_attentions:
748
+ outputs += (self_attn_weights,)
749
+
750
+ if use_cache:
751
+ outputs += (present_key_value,)
752
+
753
+ return outputs
754
+
755
+
756
+ YUAN_START_DOCSTRING = r"""
757
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
758
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
759
+ etc.)
760
+
761
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
762
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
763
+ and behavior.
764
+
765
+ Parameters:
766
+ config ([`YuanConfig`]):
767
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
768
+ load the weights associated with the model, only the configuration. Check out the
769
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
770
+ """
771
+
772
+
773
+ @add_start_docstrings(
774
+ "The bare Yuan Model outputting raw hidden-states without any specific head on top.",
775
+ YUAN_START_DOCSTRING,
776
+ )
777
+ class YuanPreTrainedModel(PreTrainedModel):
778
+ config_class = YuanConfig
779
+ base_model_prefix = "model"
780
+ supports_gradient_checkpointing = True
781
+ _no_split_modules = ["YuanDecoderLayer"]
782
+ _skip_keys_device_placement = "past_key_values"
783
+ _keys_to_ignore_on_load_unexpected = [r"decoder\.version"]
784
+
785
+ def _init_weights(self, module):
786
+ std = self.config.initializer_range
787
+ if isinstance(module, nn.Linear):
788
+ module.weight.data.normal_(mean=0.0, std=std)
789
+ if module.bias is not None:
790
+ module.bias.data.zero_()
791
+ elif isinstance(module, nn.Embedding):
792
+ module.weight.data.normal_(mean=0.0, std=std)
793
+ if module.padding_idx is not None:
794
+ module.weight.data[module.padding_idx].zero_()
795
+
796
+ def _set_gradient_checkpointing(self, module, value=False):
797
+ if isinstance(module, YuanModel):
798
+ module.gradient_checkpointing = value
799
+
800
+
801
+ YUAN_INPUTS_DOCSTRING = r"""
802
+ Args:
803
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
804
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
805
+ it.
806
+
807
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
808
+ [`PreTrainedTokenizer.__call__`] for details.
809
+
810
+ [What are input IDs?](../glossary#input-ids)
811
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
812
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
813
+
814
+ - 1 for tokens that are **not masked**,
815
+ - 0 for tokens that are **masked**.
816
+
817
+ [What are attention masks?](../glossary#attention-mask)
818
+
819
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
820
+ [`PreTrainedTokenizer.__call__`] for details.
821
+
822
+ If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
823
+ `past_key_values`).
824
+
825
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
826
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
827
+ information on the default strategy.
828
+
829
+ - 1 indicates the head is **not masked**,
830
+ - 0 indicates the head is **masked**.
831
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
832
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
833
+ config.n_positions - 1]`.
834
+
835
+ [What are position IDs?](../glossary#position-ids)
836
+ past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
837
+ Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
838
+ `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
839
+ `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
840
+
841
+ Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
842
+ blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
843
+
844
+ If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
845
+ don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
846
+ `decoder_input_ids` of shape `(batch_size, sequence_length)`.
847
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
848
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
849
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
850
+ model's internal embedding lookup matrix.
851
+ use_cache (`bool`, *optional*):
852
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
853
+ `past_key_values`).
854
+ output_attentions (`bool`, *optional*):
855
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
856
+ tensors for more detail.
857
+ output_hidden_states (`bool`, *optional*):
858
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
859
+ more detail.
860
+ return_dict (`bool`, *optional*):
861
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
862
+ """
863
+
864
+
865
+ @add_start_docstrings(
866
+ "The bare Yuan Model outputting raw hidden-states without any specific head on top.",
867
+ YUAN_START_DOCSTRING,
868
+ )
869
+ class YuanModel(YuanPreTrainedModel):
870
+ """
871
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`YuanDecoderLayer`]
872
+
873
+ Args:
874
+ config: YuanConfig
875
+ """
876
+
877
+ def __init__(self, config: YuanConfig):
878
+ super().__init__(config)
879
+ self.padding_idx = config.pad_token_id
880
+ self.vocab_size = config.vocab_size
881
+
882
+ #TODO: control it by config
883
+ self.eod_token = config.eod_token
884
+ self.reset_attention_mask = config.reset_attention_mask
885
+ self.reset_position_ids = config.reset_position_ids
886
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
887
+ self.layers = nn.ModuleList([YuanDecoderLayer(config) for _ in range(config.num_hidden_layers)])
888
+ self.norm = YuanRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
889
+ self.gradient_checkpointing = False
890
+ # Initialize weights and apply final processing
891
+ self.post_init()
892
+
893
+ def get_input_embeddings(self):
894
+ return self.embed_tokens
895
+
896
+ def set_input_embeddings(self, value):
897
+ self.embed_tokens = value
898
+
899
+ # Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
900
+ def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
901
+ # create causal mask
902
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
903
+ combined_attention_mask = None
904
+ if input_shape[-1] > 1:
905
+ combined_attention_mask = _make_causal_mask(
906
+ input_shape,
907
+ inputs_embeds.dtype,
908
+ device=inputs_embeds.device,
909
+ past_key_values_length=past_key_values_length,
910
+ )
911
+
912
+ if attention_mask is not None:
913
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
914
+ expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
915
+ inputs_embeds.device
916
+ )
917
+ combined_attention_mask = (
918
+ expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
919
+ )
920
+
921
+ return combined_attention_mask
922
+
923
+ def _prepare_decoder_attention_mask_training(self, input_id, inputs_embeds, eod_token, reset_mask_flag ,reset_attention_mask=True, reset_position_ids=True):
924
+
925
+ micro_batch_size, seq_length = input_id.size()
926
+
927
+ attention_mask = torch.tril(torch.ones(
928
+ (micro_batch_size, seq_length, seq_length), device=inputs_embeds.device)).view(
929
+ micro_batch_size, 1, seq_length, seq_length)
930
+
931
+ position_ids = torch.arange(seq_length, dtype=torch.long,
932
+ device=inputs_embeds.device)
933
+ position_ids = position_ids.unsqueeze(0).expand_as(input_id)
934
+
935
+ if reset_position_ids:
936
+ position_ids = position_ids.clone()
937
+
938
+ if reset_position_ids or reset_attention_mask:
939
+ # Loop through the batches:
940
+ for b in range(micro_batch_size):
941
+
942
+ # Find indecies where EOD token is.
943
+ eod_index = position_ids[b, input_id[b] == eod_token]
944
+
945
+ # Detach indecies from positions if going to modify positions.
946
+ if reset_position_ids:
947
+ eod_index = eod_index.clone()
948
+ # Loop through EOD indecies:
949
+ prev_index = 0
950
+ for j in range(eod_index.size()[0]):
951
+ i = eod_index[j]
952
+ # Mask attention loss.
953
+ if reset_attention_mask:
954
+ attention_mask[b, 0, (i + 1):, :(i + 1)] = 0
955
+ # Reset positions.
956
+ if reset_position_ids:
957
+ position_ids[b, (i + 1):] -= (i + 1 - prev_index)
958
+ prev_index = i + 1
959
+
960
+ inverted_mask = 1 - attention_mask
961
+ output_attn_mask = inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(inputs_embeds.dtype).min)
962
+ if reset_mask_flag:
963
+ output_attn_mask = output_attn_mask[:,:,-1:,:]
964
+ return output_attn_mask, position_ids
965
+
966
+ @add_start_docstrings_to_model_forward(YUAN_INPUTS_DOCSTRING)
967
+ def forward(
968
+ self,
969
+ input_ids: torch.LongTensor = None,
970
+ attention_mask: Optional[torch.Tensor] = None,
971
+ position_ids: Optional[torch.LongTensor] = None,
972
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
973
+ inputs_embeds: Optional[torch.FloatTensor] = None,
974
+ use_cache: Optional[bool] = None,
975
+ output_attentions: Optional[bool] = None,
976
+ output_hidden_states: Optional[bool] = None,
977
+ output_router_logits: Optional[bool] = None,
978
+ return_dict: Optional[bool] = None,
979
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
980
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
981
+ output_router_logits = (
982
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
983
+ )
984
+ output_hidden_states = (
985
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
986
+ )
987
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
988
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
989
+ input_ids1 = copy.deepcopy(input_ids)
990
+ reset_mask_flag = False
991
+ if past_key_values:
992
+ input_ids = input_ids[:, -1:]
993
+ if use_cache:
994
+ reset_mask_flag = True
995
+ # retrieve input_ids and inputs_embeds
996
+ if input_ids is not None and inputs_embeds is not None:
997
+ raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
998
+ elif input_ids is not None:
999
+
1000
+ batch_size, seq_length = input_ids.shape
1001
+ elif inputs_embeds is not None:
1002
+ batch_size, seq_length, _ = inputs_embeds.shape
1003
+ else:
1004
+ raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
1005
+
1006
+ seq_length_with_past = seq_length
1007
+ past_key_values_length = 0
1008
+
1009
+ if past_key_values is not None:
1010
+ past_key_values_length = past_key_values[0][0].shape[2]
1011
+ seq_length_with_past = seq_length_with_past + past_key_values_length
1012
+
1013
+ if position_ids is None:
1014
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
1015
+ position_ids = torch.arange(
1016
+ past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
1017
+ )
1018
+ position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
1019
+ else:
1020
+ position_ids = position_ids.view(-1, seq_length).long()
1021
+
1022
+ if inputs_embeds is None:
1023
+ inputs_embeds = self.embed_tokens(input_ids)
1024
+ if self.training or self.reset_position_ids:
1025
+ attention_mask, _ = self._prepare_decoder_attention_mask_training(input_ids1, inputs_embeds, self.eod_token, reset_mask_flag, self.reset_attention_mask, self.reset_position_ids)
1026
+
1027
+ else:
1028
+ if attention_mask is None:
1029
+ attention_mask = torch.ones(
1030
+ (batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
1031
+ )
1032
+ attention_mask = self._prepare_decoder_attention_mask(
1033
+ attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
1034
+ )
1035
+
1036
+ hidden_states = inputs_embeds
1037
+
1038
+ if self.gradient_checkpointing and self.training:
1039
+ if use_cache:
1040
+ logger.warning_once(
1041
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
1042
+ )
1043
+ use_cache = False
1044
+
1045
+ # decoder layers
1046
+ all_hidden_states = () if output_hidden_states else None
1047
+ all_self_attns = () if output_attentions else None
1048
+ next_decoder_cache = () if use_cache else None
1049
+
1050
+ for idx, decoder_layer in enumerate(self.layers):
1051
+ if output_hidden_states:
1052
+ all_hidden_states += (hidden_states,)
1053
+
1054
+ past_key_value = past_key_values[idx] if past_key_values is not None else None
1055
+
1056
+ if self.gradient_checkpointing and self.training:
1057
+
1058
+ def create_custom_forward(module):
1059
+ def custom_forward(*inputs):
1060
+ # None for past_key_value
1061
+ return module(*inputs, output_attentions, None)
1062
+
1063
+ return custom_forward
1064
+
1065
+ layer_outputs = torch.utils.checkpoint.checkpoint(
1066
+ create_custom_forward(decoder_layer),
1067
+ hidden_states,
1068
+ attention_mask,
1069
+ position_ids,
1070
+ None,
1071
+ )
1072
+ else:
1073
+ layer_outputs = decoder_layer(
1074
+ hidden_states,
1075
+ attention_mask=attention_mask,
1076
+ position_ids=position_ids,
1077
+ past_key_value=past_key_value,
1078
+ output_attentions=output_attentions,
1079
+ use_cache=use_cache,
1080
+ )
1081
+
1082
+ hidden_states = layer_outputs[0]
1083
+
1084
+ if use_cache:
1085
+ next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
1086
+
1087
+ if output_attentions:
1088
+ all_self_attns += (layer_outputs[1],)
1089
+ hidden_states = self.norm(hidden_states)
1090
+
1091
+ # add hidden states from the last decoder layer
1092
+ if output_hidden_states:
1093
+ all_hidden_states += (hidden_states,)
1094
+ next_cache = next_decoder_cache if use_cache else None
1095
+ if not return_dict:
1096
+ return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
1097
+ return BaseModelOutputWithPast(
1098
+ last_hidden_state=hidden_states,
1099
+ past_key_values=next_cache,
1100
+ hidden_states=all_hidden_states,
1101
+ attentions=all_self_attns,
1102
+ )
1103
+
1104
+
1105
+ class YuanForCausalLM(YuanPreTrainedModel):
1106
+ def __init__(self, config):
1107
+ super().__init__(config)
1108
+ self.eod_token = config.eod_token
1109
+ self.sep_token = config.sep_token
1110
+ self.use_loss_mask = config.use_loss_mask
1111
+ self.model = YuanModel(config)
1112
+
1113
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1114
+
1115
+ # Initialize weights and apply final processing
1116
+ self.post_init()
1117
+
1118
+ def get_input_embeddings(self):
1119
+ return self.model.embed_tokens
1120
+
1121
+ def set_input_embeddings(self, value):
1122
+ self.model.embed_tokens = value
1123
+
1124
+ def get_output_embeddings(self):
1125
+ return self.lm_head
1126
+
1127
+ def set_output_embeddings(self, new_embeddings):
1128
+ self.lm_head = new_embeddings
1129
+
1130
+ def set_decoder(self, decoder):
1131
+ self.model = decoder
1132
+
1133
+ def get_decoder(self):
1134
+ return self.model
1135
+
1136
+ def get_loss_mask(self, input_ids, labels, eod_token, sep_token):
1137
+ micro_batch_size, seq_length = input_ids.size()
1138
+ loss_mask = torch.ones(input_ids.size(), dtype=torch.float, device=input_ids.device)
1139
+
1140
+ position_ids = torch.arange(seq_length, dtype=torch.long,
1141
+ device=input_ids.device)
1142
+ position_ids = position_ids.unsqueeze(0).expand_as(input_ids)
1143
+
1144
+
1145
+ """modify loss_mask to only calculate the loss of the answer (separated with [SEP])"""
1146
+
1147
+ for b in range(micro_batch_size):
1148
+ eod_indexs = position_ids[b, input_ids[b] == eod_token]
1149
+ sep_indexs = position_ids[b, input_ids[b] == sep_token]
1150
+
1151
+ if len(eod_indexs) == 0 or len(sep_indexs) == 0:
1152
+ loss_mask[b] = 1.0
1153
+ else:
1154
+ if eod_indexs[0] > sep_indexs[0]:
1155
+ loss_mask[b, 0:sep_indexs[0]] = 0
1156
+
1157
+ if len(eod_indexs) == len(sep_indexs):
1158
+ for ii, eod_index in enumerate(eod_indexs):
1159
+ start_index = eod_index
1160
+ if ii == (len(sep_indexs) - 1):
1161
+ stop_index = seq_length
1162
+ else:
1163
+ stop_index = sep_indexs[ii + 1]
1164
+ loss_mask[b, start_index:stop_index] = 0.0
1165
+ else:
1166
+ if len(eod_indexs) > len(sep_indexs):
1167
+ loss_mask[b,:] = 1.0
1168
+ else:
1169
+ for ii, eod_index in enumerate(eod_indexs):
1170
+ start_index = eod_index
1171
+ stop_index = sep_indexs[ii + 1]
1172
+
1173
+ loss_mask[b, start_index:stop_index] = 0.0
1174
+
1175
+ elif eod_indexs[0] < sep_indexs[0]:
1176
+
1177
+ if len(eod_indexs) == len(sep_indexs):
1178
+ for ii, eod_index in enumerate(eod_indexs):
1179
+ start_index = eod_index
1180
+ stop_index = sep_indexs[ii]
1181
+ loss_mask[b, start_index:stop_index] = 0.0
1182
+
1183
+ else:
1184
+ if len(eod_indexs) < len(sep_indexs):
1185
+ loss_mask[b,:] = 1.0
1186
+ else:
1187
+ for ii, eod_index in enumerate(eod_indexs):
1188
+ start_index = eod_index
1189
+ if ii >= len(sep_indexs):
1190
+ stop_index = seq_length
1191
+ else:
1192
+ stop_index = sep_indexs[ii]
1193
+ loss_mask[b, start_index:stop_index] = 0.0
1194
+
1195
+ loss_mask[input_ids == eod_token] = 1.0
1196
+ return loss_mask
1197
+ @add_start_docstrings_to_model_forward(YUAN_INPUTS_DOCSTRING)
1198
+ @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
1199
+ def forward(
1200
+ self,
1201
+ input_ids: torch.LongTensor = None,
1202
+ attention_mask: Optional[torch.Tensor] = None,
1203
+ position_ids: Optional[torch.LongTensor] = None,
1204
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1205
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1206
+ labels: Optional[torch.LongTensor] = None,
1207
+ use_cache: Optional[bool] = None,
1208
+ output_attentions: Optional[bool] = None,
1209
+ output_hidden_states: Optional[bool] = None,
1210
+ output_router_logits: Optional[bool] = None,
1211
+ return_dict: Optional[bool] = None,
1212
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1213
+ r"""
1214
+ Args:
1215
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1216
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1217
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1218
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1219
+
1220
+ Returns:
1221
+
1222
+ Example:
1223
+
1224
+ ```python
1225
+ >>> from transformers import AutoTokenizer, YuanForCausalLM
1226
+
1227
+ >>> model = YuanForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
1228
+ >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
1229
+
1230
+ >>> prompt = "Hey, are you consciours? Can you talk to me?"
1231
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1232
+
1233
+ >>> # Generate
1234
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1235
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1236
+ "Hey, are you consciours? Can you talk to me?\nI'm not consciours, but I can talk to you."
1237
+ ```"""
1238
+
1239
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1240
+
1241
+ output_hidden_states = (
1242
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1243
+ )
1244
+
1245
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1246
+
1247
+ outputs = self.model(
1248
+ input_ids=input_ids,
1249
+ attention_mask=attention_mask,
1250
+ position_ids=position_ids,
1251
+ past_key_values=past_key_values,
1252
+ inputs_embeds=inputs_embeds,
1253
+ use_cache=use_cache,
1254
+ output_attentions=output_attentions,
1255
+ output_hidden_states=output_hidden_states,
1256
+ return_dict=return_dict,
1257
+ )
1258
+
1259
+ hidden_states = outputs[0]
1260
+
1261
+ logits = self.lm_head(hidden_states)
1262
+ loss = None
1263
+ if labels is not None:
1264
+ if self.use_loss_mask:
1265
+ loss_mask = self.get_loss_mask(input_ids, labels, self.eod_token, self.sep_token)
1266
+ # Shift so that tokens < n predict n
1267
+ shift_logits = logits[..., :-1, :].contiguous()
1268
+ shift_labels = labels[..., 1:].contiguous()
1269
+ # Flatten the tokens
1270
+ if self.use_loss_mask:
1271
+ loss_fct = CrossEntropyLoss(reduction='none')
1272
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1273
+ shift_labels = shift_labels.view(-1)
1274
+ # Enable model parallelism
1275
+ shift_labels = shift_labels.to(shift_logits.device)
1276
+ loss = loss_fct(shift_logits, shift_labels)
1277
+ loss = torch.sum(loss * loss_mask) / loss_mask.sum()
1278
+ else:
1279
+ loss_fct = CrossEntropyLoss()
1280
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1281
+ shift_labels = shift_labels.view(-1)
1282
+ # Enable model parallelism
1283
+ shift_labels = shift_labels.to(shift_logits.device)
1284
+ loss = loss_fct(shift_logits, shift_labels)
1285
+ if not return_dict:
1286
+ output = (logits,) + outputs[1:]
1287
+ return (loss,) + output if loss is not None else output
1288
+
1289
+ return CausalLMOutputWithPast(
1290
+ loss=loss,
1291
+ logits=logits,
1292
+ past_key_values=outputs.past_key_values,
1293
+ hidden_states=hidden_states,
1294
+ attentions=outputs.attentions,
1295
+ )
1296
+
1297
+ def prepare_inputs_for_generation(
1298
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
1299
+ ):
1300
+
1301
+ position_ids = kwargs.get("position_ids", None)
1302
+ if attention_mask is not None and position_ids is None:
1303
+ # create position_ids on the fly for batch generation
1304
+ position_ids = attention_mask.long().cumsum(-1) - 1
1305
+ position_ids.masked_fill_(attention_mask == 0, 1)
1306
+ if past_key_values:
1307
+ position_ids = position_ids[:, -1].unsqueeze(-1)
1308
+
1309
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1310
+ if inputs_embeds is not None and past_key_values is None:
1311
+ model_inputs = {"inputs_embeds": inputs_embeds}
1312
+ else:
1313
+ model_inputs = {"input_ids": input_ids}
1314
+
1315
+ model_inputs.update(
1316
+ {
1317
+ "position_ids": position_ids,
1318
+ "past_key_values": past_key_values,
1319
+ "use_cache": kwargs.get("use_cache"),
1320
+ "attention_mask": attention_mask,
1321
+ }
1322
+ )
1323
+ return model_inputs
1324
+
1325
+ @staticmethod
1326
+ def _reorder_cache(past_key_values, beam_idx):
1327
+ reordered_past = ()
1328
+ for layer_past in past_key_values:
1329
+ reordered_past += (tuple(past_state.index_select(0, beam_idx) for past_state in layer_past),)
1330
+ return reordered_past
1331
+
1332
+
1333
+ @add_start_docstrings(
1334
+ """
1335
+ The Yuan Model transformer with a sequence classification head on top (linear layer).
1336
+
1337
+ [`YuanForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1338
+ (e.g. GPT-2) do.
1339
+
1340
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1341
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1342
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1343
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1344
+ each row of the batch).
1345
+ """,
1346
+ YUAN_START_DOCSTRING,
1347
+ )
1348
+ class YuanForSequenceClassification(YuanPreTrainedModel):
1349
+ #_keys_to_ignore_on_load_missing = [r"lm_head.weight"]
1350
+
1351
+ def __init__(self, config):
1352
+ super().__init__(config)
1353
+ self.num_labels = config.num_labels
1354
+ self.model = YuanModel(config)
1355
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1356
+
1357
+ # Initialize weights and apply final processing
1358
+ self.post_init()
1359
+
1360
+ def get_input_embeddings(self):
1361
+ return self.model.embed_tokens
1362
+
1363
+ def set_input_embeddings(self, value):
1364
+ self.model.embed_tokens = value
1365
+
1366
+ @add_start_docstrings_to_model_forward(YUAN_INPUTS_DOCSTRING)
1367
+ def forward(
1368
+ self,
1369
+ input_ids: torch.LongTensor = None,
1370
+ attention_mask: Optional[torch.Tensor] = None,
1371
+ position_ids: Optional[torch.LongTensor] = None,
1372
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1373
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1374
+ labels: Optional[torch.LongTensor] = None,
1375
+ use_cache: Optional[bool] = None,
1376
+ output_attentions: Optional[bool] = None,
1377
+ output_hidden_states: Optional[bool] = None,
1378
+ return_dict: Optional[bool] = None,
1379
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1380
+ r"""
1381
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1382
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1383
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1384
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1385
+ """
1386
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1387
+ transformer_outputs = self.model(
1388
+ input_ids,
1389
+ attention_mask=attention_mask,
1390
+ position_ids=position_ids,
1391
+ past_key_values=past_key_values,
1392
+ inputs_embeds=inputs_embeds,
1393
+ use_cache=use_cache,
1394
+ output_attentions=output_attentions,
1395
+ output_hidden_states=output_hidden_states,
1396
+ return_dict=return_dict,
1397
+ )
1398
+ hidden_states = transformer_outputs[0]
1399
+ logits = self.score(hidden_states)
1400
+
1401
+ if input_ids is not None:
1402
+ batch_size = input_ids.shape[0]
1403
+ else:
1404
+ batch_size = inputs_embeds.shape[0]
1405
+
1406
+ if self.config.pad_token_id is None and batch_size != 1:
1407
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
1408
+ if self.config.pad_token_id is None:
1409
+ sequence_lengths = -1
1410
+ else:
1411
+ if input_ids is not None:
1412
+ sequence_lengths = (torch.ne(input_ids, self.config.pad_token_id).sum(-1) - 1).to(logits.device)
1413
+ else:
1414
+ sequence_lengths = -1
1415
+
1416
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
1417
+
1418
+ loss = None
1419
+ if labels is not None:
1420
+ labels = labels.to(logits.device)
1421
+ if self.config.problem_type is None:
1422
+ if self.num_labels == 1:
1423
+ self.config.problem_type = "regression"
1424
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
1425
+ self.config.problem_type = "single_label_classification"
1426
+ else:
1427
+ self.config.problem_type = "multi_label_classification"
1428
+
1429
+ if self.config.problem_type == "regression":
1430
+ loss_fct = MSELoss()
1431
+ if self.num_labels == 1:
1432
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1433
+ else:
1434
+ loss = loss_fct(pooled_logits, labels)
1435
+ elif self.config.problem_type == "single_label_classification":
1436
+ loss_fct = CrossEntropyLoss()
1437
+ loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
1438
+ elif self.config.problem_type == "multi_label_classification":
1439
+ loss_fct = BCEWithLogitsLoss()
1440
+ loss = loss_fct(pooled_logits, labels)
1441
+ if not return_dict:
1442
+ output = (pooled_logits,) + transformer_outputs[1:]
1443
+ return ((loss,) + output) if loss is not None else output
1444
+
1445
+ return SequenceClassifierOutputWithPast(
1446
+ loss=loss,
1447
+ logits=pooled_logits,
1448
+ past_key_values=transformer_outputs.past_key_values,
1449
+ hidden_states=transformer_outputs.hidden_states,
1450
+ attentions=transformer_outputs.attentions,
1451
+ )
1452
+
1453
+
1454
+