Mxode commited on
Commit
f5ddea6
1 Parent(s): 52c5b78

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +131 -3
README.md CHANGED
@@ -1,3 +1,131 @@
1
- ---
2
- license: gpl-3.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: gpl-3.0
3
+ datasets:
4
+ - Mxode/BiST
5
+ language:
6
+ - en
7
+ - zh
8
+ pipeline_tag: translation
9
+ library_name: transformers
10
+ ---
11
+ # **NanoTranslator-XL**
12
+
13
+ English | [简体中文](README_zh-CN.md)
14
+
15
+ ## Introduction
16
+
17
+ This is the **xx-large** model of the NanoTranslator, currently supported only in **English to Chinese**.
18
+
19
+ The ONNX version of the model is also available in the repository.
20
+
21
+ All models are collected in the [NanoTranslator Collection](https://huggingface.co/collections/Mxode/nanotranslator-66e1de2ba352e926ae865bd2).
22
+
23
+ | | P. | Arch. | Act. | V. | H. | I. | L. | A.H. | K.H. | Tie |
24
+ | :--: | :-----: | :--: | :--: | :--: | :-----: | :---: | :------: | :--: | :--: | :--: |
25
+ | [XXL](https://huggingface.co/Mxode/NanoTranslator-XXL) | 100 | LLaMA | SwiGLU | 16000 | 768 | 4096 | 8 | 24 | 8 | True |
26
+ | [XL](https://huggingface.co/Mxode/NanoTranslator-XL) | 78 | LLaMA | GeGLU | 16000 | 768 | 4096 | 6 | 24 | 8 | True |
27
+ | [L](https://huggingface.co/Mxode/NanoTranslator-L) | 49 | LLaMA | GeGLU | 16000 | 512 | 2816 | 8 | 16 | 8 | True |
28
+ | [M2](https://huggingface.co/Mxode/NanoTranslator-M2) | 22 | Qwen2 | GeGLU | 4000 | 432 | 2304 | 6 | 24 | 8 | True |
29
+ | [M](https://huggingface.co/Mxode/NanoTranslator-M) | 22 | LLaMA | SwiGLU | 8000 | 256 | 1408 | 16 | 16 | 4 | True |
30
+ | [S](https://huggingface.co/Mxode/NanoTranslator-S) | 9 | LLaMA | SwiGLU | 4000 | 168 | 896 | 16 | 12 | 4 | True |
31
+ | [XS](https://huggingface.co/Mxode/NanoTranslator-XS) | 2 | LLaMA | SwiGLU | 2000 | 96 | 512 | 12 | 12 | 4 | True |
32
+
33
+ - **P.** - Parameters (in million)
34
+ - **V.** - vocab size
35
+ - **H.** - hidden size
36
+ - **I.** - intermediate size
37
+ - **L.** - num layers
38
+ - **A.H.** - num attention heads
39
+ - **K.H.** - num kv heads
40
+ - **Tie** - tie word embeddings
41
+
42
+
43
+
44
+ ## How to use
45
+
46
+ Prompt format as follows:
47
+
48
+ ```
49
+ <|im_start|> {English Text} <|endoftext|>
50
+ ```
51
+
52
+ ### Directly using transformers
53
+
54
+ ```python
55
+ import torch
56
+ from transformers import AutoTokenizer, AutoModelForCausalLM
57
+
58
+ model_path = 'Mxode/NanoTranslator-XL'
59
+
60
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
61
+ model = AutoModelForCausalLM.from_pretrained(model_path)
62
+
63
+ def translate(text: str, model, **kwargs):
64
+ generation_args = dict(
65
+ max_new_tokens = kwargs.pop("max_new_tokens", 512),
66
+ do_sample = kwargs.pop("do_sample", True),
67
+ temperature = kwargs.pop("temperature", 0.55),
68
+ top_p = kwargs.pop("top_p", 0.8),
69
+ top_k = kwargs.pop("top_k", 40),
70
+ **kwargs
71
+ )
72
+
73
+ prompt = "<|im_start|>" + text + "<|endoftext|>"
74
+ model_inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
75
+
76
+ generated_ids = model.generate(model_inputs.input_ids, **generation_args)
77
+ generated_ids = [
78
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
79
+ ]
80
+
81
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
82
+ return response
83
+
84
+ text = "I love to watch my favorite TV series."
85
+
86
+ response = translate(text, model, max_new_tokens=64, do_sample=False)
87
+ print(response)
88
+ ```
89
+
90
+
91
+ ### ONNX
92
+
93
+ It has been measured that reasoning with ONNX models will be **2-10 times faster** than reasoning directly with transformers models.
94
+
95
+ You should switch to [onnx branch](https://huggingface.co/Mxode/NanoTranslator-XL/tree/onnx) manually and download to local.
96
+
97
+ reference docs:
98
+
99
+ - [Export to ONNX](https://huggingface.co/docs/transformers/serialization)
100
+ - [Inference pipelines with the ONNX Runtime accelerator](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/pipelines)
101
+
102
+ **Using ORTModelForCausalLM**
103
+
104
+ ```python
105
+ from optimum.onnxruntime import ORTModelForCausalLM
106
+ from transformers import AutoTokenizer
107
+
108
+ model_path = "your/folder/to/onnx_model"
109
+
110
+ ort_model = ORTModelForCausalLM.from_pretrained(model_path)
111
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
112
+
113
+ text = "I love to watch my favorite TV series."
114
+
115
+ response = translate(text, ort_model, max_new_tokens=64, do_sample=False)
116
+ print(response)
117
+ ```
118
+
119
+ **Using pipeline**
120
+
121
+ ```python
122
+ from optimum.pipelines import pipeline
123
+
124
+ model_path = "your/folder/to/onnx_model"
125
+ pipe = pipeline("text-generation", model=model_path, accelerator="ort")
126
+
127
+ text = "I love to watch my favorite TV series."
128
+
129
+ response = pipe(text, max_new_tokens=64, do_sample=False)
130
+ response
131
+ ```