Mxode commited on
Commit
a8b2b6d
1 Parent(s): a0ebb6a

Create README_zh-CN.md

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