Instructions to use moonshotai/Moonlight-16B-A3B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use moonshotai/Moonlight-16B-A3B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="moonshotai/Moonlight-16B-A3B", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("moonshotai/Moonlight-16B-A3B", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("moonshotai/Moonlight-16B-A3B", trust_remote_code=True) 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=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use moonshotai/Moonlight-16B-A3B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "moonshotai/Moonlight-16B-A3B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moonshotai/Moonlight-16B-A3B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/moonshotai/Moonlight-16B-A3B
- SGLang
How to use moonshotai/Moonlight-16B-A3B 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 "moonshotai/Moonlight-16B-A3B" \ --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": "moonshotai/Moonlight-16B-A3B", "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 "moonshotai/Moonlight-16B-A3B" \ --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": "moonshotai/Moonlight-16B-A3B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use moonshotai/Moonlight-16B-A3B with Docker Model Runner:
docker model run hf.co/moonshotai/Moonlight-16B-A3B
Moonlight-16B-A3B training fails in DeepseekV3MoE.forward with UnboundLocalError when shared experts are enabled
Root Cause
In DeepseekV3MoE.forward, the variable y was assigned only in the inference branch:
if not self.training:
y = self.moe_infer(hidden_states, topk_idx, topk_weight).view(*orig_shape)
if self.config.n_shared_experts is not None:
y = y + self.shared_experts(identity)
return y
During training, execution skipped the if not self.training branch, then attempted to use y in the shared-expert accumulation step.
That caused:
UnboundLocalError: cannot access local variable 'y' where it is not associated with a value
Impact
Inference can proceed.
Fine-tuning/training fails on the first forward pass.
This is an upstream model-code bug, not a dataset or trainer configuration issue.
Local Fix Applied
Patched file:
.cache/huggingface/modules/transformers_modules/moonshotai/Moonlight_hyphen_16B_hyphen_A3B/476b36a473d4467f94469414bef6cee75c9c8172/modeling_deepseek.py
Fix applied:
Added a training-mode routed-expert computation path in DeepseekV3MoE.forward.
Ensured y is always initialized before shared experts are added.
Kept the inference path using moe_infer(...) unchanged.