Text Generation
Transformers
Safetensors
English
Chinese
nanbeige
llm
conversational
custom_code
Eval Results
Instructions to use Nanbeige/Nanbeige4.2-3B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Nanbeige/Nanbeige4.2-3B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Nanbeige/Nanbeige4.2-3B", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Nanbeige/Nanbeige4.2-3B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Nanbeige/Nanbeige4.2-3B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Nanbeige/Nanbeige4.2-3B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Nanbeige/Nanbeige4.2-3B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Nanbeige/Nanbeige4.2-3B
- SGLang
How to use Nanbeige/Nanbeige4.2-3B 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 "Nanbeige/Nanbeige4.2-3B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Nanbeige/Nanbeige4.2-3B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "Nanbeige/Nanbeige4.2-3B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Nanbeige/Nanbeige4.2-3B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use Nanbeige/Nanbeige4.2-3B with Docker Model Runner:
docker model run hf.co/Nanbeige/Nanbeige4.2-3B
Update modeling_nanbeige.py
Browse files- modeling_nanbeige.py +6 -53
modeling_nanbeige.py
CHANGED
|
@@ -1111,26 +1111,7 @@ class NanbeigeMLP(nn.Module):
|
|
| 1111 |
self.act_fn = ACT2FN[config.hidden_act]
|
| 1112 |
|
| 1113 |
def forward(self, x):
|
| 1114 |
-
|
| 1115 |
-
slice = self.intermediate_size // self.config.pretraining_tp
|
| 1116 |
-
gate_proj_slices = self.gate_proj.weight.split(slice, dim=0)
|
| 1117 |
-
up_proj_slices = self.up_proj.weight.split(slice, dim=0)
|
| 1118 |
-
down_proj_slices = self.down_proj.weight.split(slice, dim=1)
|
| 1119 |
-
|
| 1120 |
-
gate_proj = torch.cat(
|
| 1121 |
-
[F.linear(x, gate_proj_slices[i]) for i in range(self.config.pretraining_tp)], dim=-1
|
| 1122 |
-
)
|
| 1123 |
-
up_proj = torch.cat([F.linear(x, up_proj_slices[i]) for i in range(self.config.pretraining_tp)], dim=-1)
|
| 1124 |
-
|
| 1125 |
-
intermediate_states = (self.act_fn(gate_proj) * up_proj).split(slice, dim=2)
|
| 1126 |
-
down_proj = [
|
| 1127 |
-
F.linear(intermediate_states[i], down_proj_slices[i]) for i in range(self.config.pretraining_tp)
|
| 1128 |
-
]
|
| 1129 |
-
down_proj = sum(down_proj)
|
| 1130 |
-
else:
|
| 1131 |
-
down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
| 1132 |
-
|
| 1133 |
-
return down_proj
|
| 1134 |
|
| 1135 |
|
| 1136 |
class NanbeigeAttention(nn.Module):
|
|
@@ -1239,27 +1220,9 @@ class NanbeigeAttention(nn.Module):
|
|
| 1239 |
depth_attention_kv_cache = kwargs.pop("depth_attention_kv_cache", None)
|
| 1240 |
bsz, q_len, _ = hidden_states.size()
|
| 1241 |
|
| 1242 |
-
|
| 1243 |
-
|
| 1244 |
-
|
| 1245 |
-
(self.num_heads * self.head_dim) // self.config.pretraining_tp, dim=0
|
| 1246 |
-
)
|
| 1247 |
-
key_slices = self.k_proj.weight.split(key_value_slicing, dim=0)
|
| 1248 |
-
value_slices = self.v_proj.weight.split(key_value_slicing, dim=0)
|
| 1249 |
-
|
| 1250 |
-
query_states = [F.linear(hidden_states, query_slices[i]) for i in range(self.config.pretraining_tp)]
|
| 1251 |
-
query_states = torch.cat(query_states, dim=-1)
|
| 1252 |
-
|
| 1253 |
-
key_states = [F.linear(hidden_states, key_slices[i]) for i in range(self.config.pretraining_tp)]
|
| 1254 |
-
key_states = torch.cat(key_states, dim=-1)
|
| 1255 |
-
|
| 1256 |
-
value_states = [F.linear(hidden_states, value_slices[i]) for i in range(self.config.pretraining_tp)]
|
| 1257 |
-
value_states = torch.cat(value_states, dim=-1)
|
| 1258 |
-
|
| 1259 |
-
else:
|
| 1260 |
-
query_states = self.q_proj(hidden_states)
|
| 1261 |
-
key_states = self.k_proj(hidden_states)
|
| 1262 |
-
value_states = self.v_proj(hidden_states)
|
| 1263 |
|
| 1264 |
query_states = query_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2)
|
| 1265 |
key_states = key_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2)
|
|
@@ -1354,12 +1317,7 @@ class NanbeigeAttention(nn.Module):
|
|
| 1354 |
|
| 1355 |
attn_output = attn_output.reshape(bsz, q_len, self.num_heads * self.head_dim)
|
| 1356 |
|
| 1357 |
-
|
| 1358 |
-
attn_output = attn_output.split((self.num_heads * self.head_dim) // self.config.pretraining_tp, dim=2)
|
| 1359 |
-
o_proj_slices = self.o_proj.weight.split((self.num_heads * self.head_dim) // self.config.pretraining_tp, dim=1)
|
| 1360 |
-
attn_output = sum([F.linear(attn_output[i], o_proj_slices[i]) for i in range(self.config.pretraining_tp)])
|
| 1361 |
-
else:
|
| 1362 |
-
attn_output = self.o_proj(attn_output)
|
| 1363 |
|
| 1364 |
if not output_attentions:
|
| 1365 |
attn_weights = None
|
|
@@ -2637,12 +2595,7 @@ class NanbeigeForCausalLM(NanbeigePreTrainedModel):
|
|
| 2637 |
)
|
| 2638 |
|
| 2639 |
hidden_states = outputs[0]
|
| 2640 |
-
|
| 2641 |
-
lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
|
| 2642 |
-
logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
|
| 2643 |
-
logits = torch.cat(logits, dim=-1)
|
| 2644 |
-
else:
|
| 2645 |
-
logits = self.lm_head(hidden_states)
|
| 2646 |
logits = logits.float()
|
| 2647 |
|
| 2648 |
loss = None
|
|
|
|
| 1111 |
self.act_fn = ACT2FN[config.hidden_act]
|
| 1112 |
|
| 1113 |
def forward(self, x):
|
| 1114 |
+
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1115 |
|
| 1116 |
|
| 1117 |
class NanbeigeAttention(nn.Module):
|
|
|
|
| 1220 |
depth_attention_kv_cache = kwargs.pop("depth_attention_kv_cache", None)
|
| 1221 |
bsz, q_len, _ = hidden_states.size()
|
| 1222 |
|
| 1223 |
+
query_states = self.q_proj(hidden_states)
|
| 1224 |
+
key_states = self.k_proj(hidden_states)
|
| 1225 |
+
value_states = self.v_proj(hidden_states)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1226 |
|
| 1227 |
query_states = query_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2)
|
| 1228 |
key_states = key_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2)
|
|
|
|
| 1317 |
|
| 1318 |
attn_output = attn_output.reshape(bsz, q_len, self.num_heads * self.head_dim)
|
| 1319 |
|
| 1320 |
+
attn_output = self.o_proj(attn_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1321 |
|
| 1322 |
if not output_attentions:
|
| 1323 |
attn_weights = None
|
|
|
|
| 2595 |
)
|
| 2596 |
|
| 2597 |
hidden_states = outputs[0]
|
| 2598 |
+
logits = self.lm_head(hidden_states)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2599 |
logits = logits.float()
|
| 2600 |
|
| 2601 |
loss = None
|