Text Generation
Transformers
Safetensors
Portuguese
nanothink
thinking
reasoning
reason
think
lowparams
5m_params
thinkset-ptbr
gpt2
Instructions to use AxionLab-Co/NanoThink-5M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AxionLab-Co/NanoThink-5M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="AxionLab-Co/NanoThink-5M")# Load model directly from transformers import NanoThink model = NanoThink.from_pretrained("AxionLab-Co/NanoThink-5M", dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use AxionLab-Co/NanoThink-5M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "AxionLab-Co/NanoThink-5M" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AxionLab-Co/NanoThink-5M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/AxionLab-Co/NanoThink-5M
- SGLang
How to use AxionLab-Co/NanoThink-5M with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "AxionLab-Co/NanoThink-5M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AxionLab-Co/NanoThink-5M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "AxionLab-Co/NanoThink-5M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AxionLab-Co/NanoThink-5M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use AxionLab-Co/NanoThink-5M with Docker Model Runner:
docker model run hf.co/AxionLab-Co/NanoThink-5M
Create model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class NanoThinkConfig(PretrainedConfig):
|
| 7 |
+
model_type = "nanothink"
|
| 8 |
+
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
vocab_size=1229,
|
| 12 |
+
dim=128,
|
| 13 |
+
n_layers=4,
|
| 14 |
+
n_heads=4,
|
| 15 |
+
max_len=256,
|
| 16 |
+
**kwargs
|
| 17 |
+
):
|
| 18 |
+
super().__init__(**kwargs)
|
| 19 |
+
self.vocab_size = vocab_size
|
| 20 |
+
self.dim = dim
|
| 21 |
+
self.n_layers = n_layers
|
| 22 |
+
self.n_heads = n_heads
|
| 23 |
+
self.max_len = max_len
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class NanoThinkModel(PreTrainedModel):
|
| 27 |
+
config_class = NanoThinkConfig
|
| 28 |
+
|
| 29 |
+
def __init__(self, config):
|
| 30 |
+
super().__init__(config)
|
| 31 |
+
|
| 32 |
+
self.token_emb = nn.Embedding(config.vocab_size, config.dim)
|
| 33 |
+
self.pos_emb = nn.Embedding(config.max_len, config.dim)
|
| 34 |
+
|
| 35 |
+
encoder_layer = nn.TransformerEncoderLayer(
|
| 36 |
+
d_model=config.dim,
|
| 37 |
+
nhead=config.n_heads,
|
| 38 |
+
batch_first=True
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
self.transformer = nn.TransformerEncoder(
|
| 42 |
+
encoder_layer,
|
| 43 |
+
num_layers=config.n_layers
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
self.ln = nn.LayerNorm(config.dim)
|
| 47 |
+
self.head = nn.Linear(config.dim, config.vocab_size)
|
| 48 |
+
|
| 49 |
+
self.post_init()
|
| 50 |
+
|
| 51 |
+
def forward(self, input_ids):
|
| 52 |
+
B, T = input_ids.shape
|
| 53 |
+
pos = torch.arange(T, device=input_ids.device).unsqueeze(0)
|
| 54 |
+
|
| 55 |
+
x = self.token_emb(input_ids) + self.pos_emb(pos)
|
| 56 |
+
|
| 57 |
+
mask = torch.triu(
|
| 58 |
+
torch.ones(T, T, device=input_ids.device),
|
| 59 |
+
diagonal=1
|
| 60 |
+
).bool()
|
| 61 |
+
|
| 62 |
+
x = self.transformer(x, mask=mask)
|
| 63 |
+
x = self.ln(x)
|
| 64 |
+
|
| 65 |
+
logits = self.head(x)
|
| 66 |
+
|
| 67 |
+
return logits
|