File size: 11,809 Bytes
121ad4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dad928b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
---
license: apache-2.0
language:
- en
- zh
base_model: meta-llama/Meta-Llama-3-8B-Instruct
tags: 
- text-generation
- transformers
- lora
- llama.cpp
- autoawq
- auto-gptq
datasets:
- llamafactory/alpaca_zh
- llamafactory/alpaca_gpt4_zh
---

# Meta-Llama-3-8B-Instruct-zh-10k: A Llama🦙 which speaks Chinese / 一只说中文的羊驼🦙

## Model Details / 模型细节
This model, <u>`Meta-Llama-3-8B-Instruct-zh-10k`</u>, was fine-tuned from the original [Meta-Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct) due to its underperformance in Chinese. Utilizing the LoRa technology within the [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory) utilities, this model was adapted to better handle Chinese through three epochs on three corpora: `alpaca_zh`, `alpaca_gpt4_zh`, and `oaast_sft_zh`, amounting to approximately 10,000 examples. This is reflected in the `10k` in its name.

由于原模型[Meta-Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct)在中文上表现欠佳,于是该模型 <u>`Meta-Llama-3-8B-Instruct-zh-10k`</u> 微调自此。在[LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory)工具下,利用LoRa 技术,通过`alpaca_zh``alpaca_gpt4_zh``oaast_sft_zh`三个语料库上、经过三个训练轮次,我们将该模型调整得更好地掌握了中文。三个语料库共计约10,000个样本,这也是其名字中的 `10k` 的由来。

For efficient inference, the model was converted to the gguf format using [llama.cpp](https://github.com/ggerganov/llama.cpp) and underwent quantization, resulting in a compact model size of about 3.18 GB, suitable for distribution across various devices.

为了高效的推理,使用 [llama.cpp](https://github.com/ggerganov/llama.cpp),我们将该模型转化为了gguf格式并量化,从而得到了一个压缩到约 3.18 GB 大小的模型,适合分发在各类设备上。

### LoRa Hardware / LoRa 硬件
- RTX 4090D x 1

> [!NOTE]
> The complete fine-tuning process took approximately 12 hours. / 完整微调过程花费约12小时。

Additional fine-tuning configurations are avaiable at [Hands-On LoRa](https://github.com/XavierSpycy/hands-on-lora) or [Llama3Ops](https://github.com/XavierSpycy/llama-ops).

更多微调配置可以在我的个人仓库 [Hands-On LoRa](https://github.com/XavierSpycy/hands-on-lora) 或 [Llama3Ops](https://github.com/XavierSpycy/llama-ops) 获得。

### Other Models / 其他模型
- <u>LLaMA-Factory</u>
    - [Meta-Llama-3-8B-Instruct-zh-10k](https://huggingface.co/XavierSpycy/Meta-Llama-3-8B-Instruct-zh-10k)

- <u>llama.cpp</u>
    - [Meta-Llama-3-8B-Instruct-zh-10k-GGUF](https://huggingface.co/XavierSpycy/Meta-Llama-3-8B-Instruct-zh-10k-GGUF)

- <u>AutoGPTQ</u>
    - [Meta-Llama-3-8B-Instruct-zh-10k-GPTQ](https://huggingface.co/XavierSpycy/Meta-Llama-3-8B-Instruct-zh-10k-GPTQ)

### Model Developer / 模型开发者
- **Pretraining**: Meta
- **Fine-tuning**: [XavierSpycy @ GitHub ](https://github.com/XavierSpycy) | [XavierSpycy @ 🤗](https://huggingface.co/XavierSpycy)

- **预训练**: Meta
- **微调**: [XavierSpycy @ GitHub](https://github.com/XavierSpycy) | [XavierSpycy @ 🤗 ](https://huggingface.co/XavierSpycy)


### Usage / 用法
This model can be utilized like the original <u>Meta-Llama3</u> but offers enhanced performance in Chinese.

我们能够像原版的<u>Meta-Llama3</u>一样使用该模型,而它提供了提升后的中文能力。

#### 1. How to use in transformers
```python
# !pip install accelerate

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "XavierSpycy/Meta-Llama-3-8B-Instruct-zh-10k"

model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)

prompt = "你好,你是谁?"

messages = [
    {"role": "system", "content": "你是一个乐于助人的助手。"},
    {"role": "user", "content": prompt}]

input_ids = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt").to(model.device)

terminators = [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>")]

outputs = model.generate(
    input_ids,
    max_new_tokens=256,
    eos_token_id=terminators,
    do_sample=True,
    temperature=0.6,
    top_p=0.9)

response = outputs[0][input_ids.shape[-1]:]

print(tokenizer.decode(response, skip_special_tokens=True))
# 我是一个人工智能助手,旨在帮助用户解决问题和完成任务。
# 我是一个虚拟的人工智能助手,能够通过自然语言处理技术理解用户的需求并为用户提供帮助。
```

#### 2. How to use in llama.cpp / 如何在llama.cpp中使用


```python
# CMAKE_ARGS="-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS # -DLLAMA_CUDA=on" \
#  pip install llama-cpp-python \
#  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu121

# Please download the model weights first. / 请先下载模型权重。

from llama_cpp import Llama

llm = Llama(
      model_path="/path/to/your/model/Meta-Llama-3-8B-Instruct-zh-10k-GGUF/meta-llama-3-8b-instruct-zh-10k.Q8_0.gguf",
      n_gpu_layers=-1)

# Alternatively / 或者
# llm = Llama.from_pretrained(
#     repo_id="XavierSpycy/Meta-Llama-3-8B-Instruct-zh-10k-GGUF",
#     filename="*Q8_0.gguf",
#     verbose=False
# )

output = llm(
      "Q: 你好,你是谁?A:", # Prompt
      max_tokens=256, # Generate up to 32 tokens, set to None to generate up to the end of the context window
      stop=["Q:", "\n"], # Stop generating just before the model would generate a new question
      echo=True # Echo the prompt back in the output
) # Generate a completion, can also call create_completion

print(output['choices'][0]['text'].split("A:")[1].strip())

# 我是一个人工智能聊天机器人,我的名字叫做“智慧助手”,我由一群程序员设计和开发的。我的主要任务就是通过与您交流来帮助您解决问题,为您提供相关的建议和支持。
```

#### 3. How to use with AutoAWQ / 如何与AutoAWQ一起使用
```python
# !pip install autoawq

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "XavierSpycy/Meta-Llama-3-8B-Instruct-zh-10k-AWQ"

model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)

prompt = "你好,你是谁?"

messages = [
    {"role": "system", "content": "你是一个乐于助人的助手。"},
    {"role": "user", "content": prompt}]

input_ids = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt").to(model.device)

terminators = [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>")]

outputs = model.generate(
    input_ids,
    max_new_tokens=256,
    eos_token_id=terminators,
    do_sample=True,
    temperature=0.6,
    top_p=0.9)

response = outputs[0][input_ids.shape[-1]:]

print(tokenizer.decode(response, skip_special_tokens=True))
# 你好!我是一个人工智能助手,我的目的是帮助人们解决问题,回答问题,提供信息和建议。
```

#### 4. How to use with AutoGPTQ / 如何与AutoGPTQ一起使用
```python
# !pip install auto-gptq --no-build-isolation

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "XavierSpycy/Meta-Llama-3-8B-Instruct-zh-10k-GPTQ"

model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)

prompt = "什么是机器学习?"

messages = [
    {"role": "system", "content": "你是一个乐于助人的助手。"},
    {"role": "user", "content": prompt}]

input_ids = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt").to(model.device)

terminators = [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>")]

outputs = model.generate(
    input_ids,
    max_new_tokens=256,
    eos_token_id=terminators,
    do_sample=True,
    temperature=0.6,
    top_p=0.9)

response = outputs[0][input_ids.shape[-1]:]

print(tokenizer.decode(response, skip_special_tokens=True))
# 机器学习是人工智能(AI)的一个分支,它允许计算机从数据中学习并改善其性能。它是一种基于算法的方法,用于从数据中识别模式并进行预测。机器学习算法可以从数据中学习,例如文本、图像和音频,并从中获得知识和见解。
```

Further details about the deployment are available in the GitHub repository [Llama3Ops: From LoRa to Deployment with Llama3](https://github.com/XavierSpycy/llama-ops).

更多关于部署的细节可以在我的个人仓库 [Llama3Ops: From LoRa to Deployment with Llama3](https://github.com/XavierSpycy/llama-ops) 获得。

## Ethical Considerations, Safety & Risks / 伦理考量、安全性和风险
Please refer to [Meta Llama 3's Ethical Considerations](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct#ethical-considerations-and-limitations) for more information. Key points include bias monitoring, responsible usage guidelines, and transparency in model limitations.

请参考 [Meta Llama 3's Ethical Considerations](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct#ethical-considerations-and-limitations),以获取更多细节。关键点包括偏见监控、负责任的使用指南和模型限制的透明度。

## Limitations / 局限性
- The comprehensive abilities of the model have not been fully tested. 

- While it performs smoothly in Chinese conversations, further benchmarks are required to evaluate its full capabilities. The quality and quantity of the Chinese corpora used may also limit model outputs. 

- Based on current observations, it fundamentally meets the standards in common sense, logic, sentiment analysis, safety, writing, code, and function calls. However, there is room for improvement in role-playing, mathematics, and handling complex tasks with the same text but different meanings.

- Additionally, catastrophic forgetting in the fine-tuned model has not been evaluated.

- 该模型的全面的能力尚未全部测试。

- 尽管它在中文对话中表现流畅,但需要更多的测评以评估其完整的能力。中文语料库的质量和数量可能都会对模型输出有所制约。

- 根据目前的观察,它在常识、逻辑、情绪分析、安全性、写作、代码和函数调用上基本达标,然而,在角色扮演、数学、复杂的同文异义等任务上有待提高。

- 另外,微调模型中的灾难性遗忘尚未评估。

## Acknowledgements / 致谢
We thank Meta for their open-source contributions, which have greatly benefited the developer community, and acknowledge the collaborative efforts of developers in enhancing this community. 

我们感谢 Meta 的开源贡献,这极大地帮助了开发者社区,同时,也感谢致力于提升社区的开发者们的努力。

## References / 参考资料

```
@article{llama3modelcard,
title={Llama 3 Model Card},
author={AI@Meta},
year={2024},
url = {https://github.com/meta-llama/llama3/blob/main/MODEL_CARD.md}}

@inproceedings{zheng2024llamafactory,
  title={LlamaFactory: Unified Efficient Fine-Tuning of 100+ Language Models},
  author={Yaowei Zheng and Richong Zhang and Junhao Zhang and Yanhan Ye and Zheyan Luo and Zhangchi Feng and Yongqiang Ma},
  booktitle={Proceedings of the 62nd Annual Meeting of the Association for Computational Linguistics (Volume 3: System Demonstrations)},
  address={Bangkok, Thailand},
  publisher={Association for Computational Linguistics},
  year={2024},
  url={http://arxiv.org/abs/2403.13372}}
```