|
# NanoLM-1B-Instruct-v2 |
|
|
|
[English](README.md) | 简体中文 |
|
|
|
|
|
## Introduction |
|
|
|
为了探究小模型的潜能,我尝试构建一系列小模型,并存放于 [NanoLM Collections](https://huggingface.co/collections/Mxode/nanolm-66d6d75b4a69536bca2705b2)。 |
|
|
|
这是 NanoLM-1B-Instruct-v2。该模型目前仅支持**英文**。 |
|
|
|
|
|
## 模型详情 |
|
|
|
| Nano LMs | Non-emb Params | Arch | Layers | Dim | Heads | Seq Len | |
|
| :----------: | :------------------: | :---: | :----: | :-------: | :---: | :---: | |
|
| 25M | 15M | MistralForCausalLM | 12 | 312 | 12 | 2K | |
|
| 70M | 42M | LlamaForCausalLM | 12 | 576 | 9 |2K| |
|
| 0.3B | 180M | Qwen2ForCausalLM | 12 | 896 | 14 |4K| |
|
| **1B** | **840M** | **Qwen2ForCausalLM** | **18** | **1536** | **12** | **4K** | |
|
|
|
|
|
## 如何使用 |
|
|
|
```python |
|
import torch |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
model_path = 'Mxode/NanoLM-1B-Instruct-v2' |
|
|
|
model = AutoModelForCausalLM.from_pretrained(model_path).to('cuda:0', torch.bfloat16) |
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
|
|
|
|
def get_response(prompt: str, **kwargs): |
|
generation_args = dict( |
|
max_new_tokens = kwargs.pop("max_new_tokens", 512), |
|
do_sample = kwargs.pop("do_sample", True), |
|
temperature = kwargs.pop("temperature", 0.7), |
|
top_p = kwargs.pop("top_p", 0.8), |
|
top_k = kwargs.pop("top_k", 40), |
|
**kwargs |
|
) |
|
|
|
messages = [ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": prompt} |
|
] |
|
text = tokenizer.apply_chat_template( |
|
messages, |
|
tokenize=False, |
|
add_generation_prompt=True |
|
) |
|
model_inputs = tokenizer([text], return_tensors="pt").to(model.device) |
|
|
|
generated_ids = model.generate(model_inputs.input_ids, **generation_args) |
|
generated_ids = [ |
|
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) |
|
] |
|
|
|
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] |
|
return response |
|
|
|
|
|
prompt = "Calculate (99 - 1) * (3 + 4)" |
|
print(get_response(prompt, do_sample=False)) |
|
|
|
""" |
|
To calculate \((99 - 1) * (3 + 4)\), follow the order of operations, also known as PEMDAS (Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction). |
|
|
|
First, solve the expressions inside the parentheses: |
|
|
|
1. \(99 - 1 = 98\) |
|
2. \(3 + 4 = 7\) |
|
|
|
Now, multiply the results: |
|
|
|
\(98 * 7 = 686\) |
|
|
|
So, \((99 - 1) * (3 + 4) = 686\). |
|
""" |
|
``` |
|
|