Mxode commited on
Commit
ab6e70f
1 Parent(s): f5ddea6

Create README_zh-CN.md

Browse files
Files changed (1) hide show
  1. README_zh-CN.md +119 -0
README_zh-CN.md ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # **NanoTranslator-XL**
2
+
3
+ [English](README.md) | 简体中文
4
+
5
+ ## Introduction
6
+
7
+ 这是 NanoTranslator 的 **XX-Large** 型号,目前仅支持**英译中**。仓库中同时提供了 ONNX 版本的模型。
8
+
9
+ 所有模型均收录于 [NanoTranslator Collection](https://huggingface.co/collections/Mxode/nanotranslator-66e1de2ba352e926ae865bd2) 中。
10
+
11
+ | | P. | Arch. | Act. | V. | H. | I. | L. | A.H. | K.H. | Tie |
12
+ | :--: | :-----: | :--: | :--: | :--: | :-----: | :---: | :------: | :--: | :--: | :--: |
13
+ | [XXL](https://huggingface.co/Mxode/NanoTranslator-XXL) | 100 | LLaMA | SwiGLU | 16000 | 768 | 4096 | 8 | 24 | 8 | True |
14
+ | [XL](https://huggingface.co/Mxode/NanoTranslator-XL) | 78 | LLaMA | GeGLU | 16000 | 768 | 4096 | 6 | 24 | 8 | True |
15
+ | [L](https://huggingface.co/Mxode/NanoTranslator-L) | 49 | LLaMA | GeGLU | 16000 | 512 | 2816 | 8 | 16 | 8 | True |
16
+ | [M2](https://huggingface.co/Mxode/NanoTranslator-M2) | 22 | Qwen2 | GeGLU | 4000 | 432 | 2304 | 6 | 24 | 8 | True |
17
+ | [M](https://huggingface.co/Mxode/NanoTranslator-M) | 22 | LLaMA | SwiGLU | 8000 | 256 | 1408 | 16 | 16 | 4 | True |
18
+ | [S](https://huggingface.co/Mxode/NanoTranslator-S) | 9 | LLaMA | SwiGLU | 4000 | 168 | 896 | 16 | 12 | 4 | True |
19
+ | [XS](https://huggingface.co/Mxode/NanoTranslator-XS) | 2 | LLaMA | SwiGLU | 2000 | 96 | 512 | 12 | 12 | 4 | True |
20
+
21
+ - **P.** - Parameters (in million)
22
+ - **V.** - vocab size
23
+ - **H.** - hidden size
24
+ - **I.** - intermediate size
25
+ - **L.** - num layers
26
+ - **A.H.** - num attention heads
27
+ - **K.H.** - num kv heads
28
+ - **Tie** - tie word embeddings
29
+
30
+
31
+
32
+ ## How to use
33
+
34
+ Prompt 格式如下:
35
+
36
+ ```
37
+ <|im_start|> {English Text} <|endoftext|>
38
+ ```
39
+
40
+ ### Directly using transformers
41
+
42
+ ```python
43
+ import torch
44
+ from transformers import AutoTokenizer, AutoModelForCausalLM
45
+
46
+ model_path = 'Mxode/NanoTranslator-XL'
47
+
48
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
49
+ model = AutoModelForCausalLM.from_pretrained(model_path)
50
+
51
+ def translate(text: str, model, **kwargs):
52
+ generation_args = dict(
53
+ max_new_tokens = kwargs.pop("max_new_tokens", 512),
54
+ do_sample = kwargs.pop("do_sample", True),
55
+ temperature = kwargs.pop("temperature", 0.55),
56
+ top_p = kwargs.pop("top_p", 0.8),
57
+ top_k = kwargs.pop("top_k", 40),
58
+ **kwargs
59
+ )
60
+
61
+ prompt = "<|im_start|>" + text + "<|endoftext|>"
62
+ model_inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
63
+
64
+ generated_ids = model.generate(model_inputs.input_ids, **generation_args)
65
+ generated_ids = [
66
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
67
+ ]
68
+
69
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
70
+ return response
71
+
72
+ text = "I love to watch my favorite TV series."
73
+
74
+ response = translate(text, model, max_new_tokens=64, do_sample=False)
75
+ print(response)
76
+ ```
77
+
78
+
79
+ ### ONNX
80
+
81
+ 根据实际测试,使用 ONNX 模型推理会比直接使用 transformers 推理要**快 2~10 倍**。
82
+
83
+ 如果希望使用 ONNX 模型,那么你需要手动切换到 [onnx 分支](https://huggingface.co/Mxode/NanoTranslator-XL/tree/onnx)并从本地加载。
84
+
85
+ 参考文档:
86
+
87
+ - [Export to ONNX](https://huggingface.co/docs/transformers/serialization)
88
+ - [Inference pipelines with the ONNX Runtime accelerator](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/pipelines)
89
+
90
+ **Using ORTModelForCausalLM**
91
+
92
+ ```python
93
+ from optimum.onnxruntime import ORTModelForCausalLM
94
+ from transformers import AutoTokenizer
95
+
96
+ model_path = "your/folder/to/onnx_model"
97
+
98
+ ort_model = ORTModelForCausalLM.from_pretrained(model_path)
99
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
100
+
101
+ text = "I love to watch my favorite TV series."
102
+
103
+ response = translate(text, ort_model, max_new_tokens=64, do_sample=False)
104
+ print(response)
105
+ ```
106
+
107
+ **Using pipeline**
108
+
109
+ ```python
110
+ from optimum.pipelines import pipeline
111
+
112
+ model_path = "your/folder/to/onnx_model"
113
+ pipe = pipeline("text-generation", model=model_path, accelerator="ort")
114
+
115
+ text = "I love to watch my favorite TV series."
116
+
117
+ response = pipe(text, max_new_tokens=64, do_sample=False)
118
+ response
119
+ ```