Text Generation
Transformers
Safetensors
deepseek_v3
conversational
custom_code
text-generation-inference
Instructions to use kovwang/moonlight_16B_modified with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use kovwang/moonlight_16B_modified with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="kovwang/moonlight_16B_modified", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("kovwang/moonlight_16B_modified", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("kovwang/moonlight_16B_modified", trust_remote_code=True, device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use kovwang/moonlight_16B_modified with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "kovwang/moonlight_16B_modified" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "kovwang/moonlight_16B_modified", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/kovwang/moonlight_16B_modified
- SGLang
How to use kovwang/moonlight_16B_modified 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 "kovwang/moonlight_16B_modified" \ --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": "kovwang/moonlight_16B_modified", "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 "kovwang/moonlight_16B_modified" \ --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": "kovwang/moonlight_16B_modified", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use kovwang/moonlight_16B_modified with Docker Model Runner:
docker model run hf.co/kovwang/moonlight_16B_modified
Upload modeling_deepseek.py with huggingface_hub
Browse files- modeling_deepseek.py +16 -2
modeling_deepseek.py
CHANGED
|
@@ -521,14 +521,28 @@ class DeepseekV3MoE(nn.Module):
|
|
| 521 |
config=config, intermediate_size=intermediate_size
|
| 522 |
)
|
| 523 |
|
| 524 |
-
|
| 525 |
identity = hidden_states
|
| 526 |
orig_shape = hidden_states.shape
|
| 527 |
topk_idx, topk_weight = self.gate(hidden_states)
|
| 528 |
hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
|
| 529 |
flat_topk_idx = topk_idx.view(-1)
|
| 530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
y = self.moe_infer(hidden_states, topk_idx, topk_weight).view(*orig_shape)
|
|
|
|
| 532 |
if self.config.n_shared_experts is not None:
|
| 533 |
y = y + self.shared_experts(identity)
|
| 534 |
return y
|
|
|
|
| 521 |
config=config, intermediate_size=intermediate_size
|
| 522 |
)
|
| 523 |
|
| 524 |
+
def forward(self, hidden_states):
|
| 525 |
identity = hidden_states
|
| 526 |
orig_shape = hidden_states.shape
|
| 527 |
topk_idx, topk_weight = self.gate(hidden_states)
|
| 528 |
hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
|
| 529 |
flat_topk_idx = topk_idx.view(-1)
|
| 530 |
+
|
| 531 |
+
if self.training:
|
| 532 |
+
# 训练模式:处理MoE前向传播
|
| 533 |
+
hidden_states = hidden_states.repeat_interleave(self.num_experts_per_tok, dim=0)
|
| 534 |
+
y = torch.empty_like(hidden_states)
|
| 535 |
+
for i, expert in enumerate(self.experts):
|
| 536 |
+
if expert is not None: # 处理expert parallelism的情况
|
| 537 |
+
mask = (flat_topk_idx == i)
|
| 538 |
+
if mask.any():
|
| 539 |
+
y[mask] = expert(hidden_states[mask])
|
| 540 |
+
y = (y.view(*topk_weight.shape, -1) * topk_weight.unsqueeze(-1)).sum(dim=1)
|
| 541 |
+
y = y.view(*orig_shape)
|
| 542 |
+
else:
|
| 543 |
+
# 推理模式:使用moe_infer
|
| 544 |
y = self.moe_infer(hidden_states, topk_idx, topk_weight).view(*orig_shape)
|
| 545 |
+
|
| 546 |
if self.config.n_shared_experts is not None:
|
| 547 |
y = y + self.shared_experts(identity)
|
| 548 |
return y
|