Instructions to use melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0", 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("melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0", 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=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0
- SGLang
How to use melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0 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 "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0" \ --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": "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0", "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 "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0" \ --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": "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0 with Docker Model Runner:
docker model run hf.co/melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0
Fork: transformers==5.13.0 compatibility
This is a fork of
moonshotai/Moonlight-16B-A3B-Instructwhose only differences from upstream are edits to the custommodeling_deepseek.pyfile so that the model loads and generates correctly ontransformers==5.13.0. The safetensors weights are identical to upstream. Verified working withtorch==2.6.0+cu124andtransformers==5.13.0.The upstream file targets
transformers==4.48.2. Between 4.48 and 5.13 several internal APIs changed or were removed, and the transformers-5.x meta-device-first load path caused an uninitialized RoPE buffer that silently broke the model. Concretely, the following was changed inmodeling_deepseek.py:
- RoPE
inv_freqbuffer (the critical fix). In transformers ≥ 5,from_pretrainedinitializes modules on themetadevice before loading the checkpoint. Buffers registered withpersistent=False(likeDeepseekV3RotaryEmbedding.inv_freq) are not populated fromstate_dict, soinv_freqremained as meta-device zeros — every position producedcos=1, sin=0and RoPE was silently disabled.DeepseekV3RotaryEmbeddingnow lazily recomputesinv_freqon the real device inside_set_cos_sin_cacheif it is onmetaor all-zero. Without this fix the model produces fluent but position-blind gibberish.rope_scalingdefault schema. transformers ≥ 4.45 auto-populatesconfig.rope_scalingwith{'rope_type': 'default', ...}even when the config file has norope_scalingkey._init_rope(and the softmax-scale branch) now call a new_rope_scaling_active()helper that treatsrope_type == 'default'(or a missingfactor) as "no scaling," matching the pre-5.x behavior for Moonlight.Cache.seen_tokensremoved.prepare_inputs_for_generationnow usesgetattr(past_key_values, "seen_tokens", cache_length)wherecache_lengthcomes fromget_seq_length().Cache.get_usable_length(...)removed. All call sites were replaced with the equivalentget_seq_length(...)(per-layer where applicable).DynamicCachehas no cache size limit, so the replacement is exact.DynamicCache.get_max_cache_shape()changed return value. It now returns-1(previouslyNone) for unbounded caches.prepare_inputs_for_generationtreats any non-positive value as "unbounded," which prevents an erroneousattention_mask[:, -(-1):]truncation that dropped the first token.- Full-sequence
position_idsin the decode loop. The new_sampleloop passes position IDs covering the whole sequence even when only one new token is being processed.prepare_inputs_for_generationnow trimsposition_idsdown toinput_ids' length so RoPE indexing stays consistent.The README's two "Inference with Hugging Face Transformers" snippets below have been updated for the new API (the second snippet uses
return_dict=Truewithapply_chat_template, which is required in transformers ≥ 5) and both were re-run end-to-end against this fork — the actual outputs are shown after each snippet.
Abstract
Recently, the Muon optimizer has demonstrated strong results in training small-scale language models, but the scalability to larger models has not been proven. We identify two crucial techniques for scaling up Muon:
- Weight Decay: Critical for scaling to larger models
- Consistent RMS Updates: Enforcing a consistent root mean square on model updates
These techniques allow Muon to work out-of-the-box on large-scale training without the need of hyper-parameter tuning. Scaling law experiments indicate that Muon is $\sim2\times$ more sample efficient than Adam with compute optimal training.
Based on these improvements, we introduce Moonlight, a 3B/16B-parameter Mixture-of-Expert (MoE) model trained with 5.7T tokens using Muon. Our model improves the current Pareto frontier, achieving better performance with much fewer training FLOPs compared to prior models.
We open-source our Muon implementation that is memory optimal and communication efficient. We also release the pretrained, instruction-tuned, and intermediate checkpoints to support future research.
Our code is available at MoonshotAI/Moonlight.
Key Ingredients
Our work builds upon Muon while systematically identifying and resolving its limitations in large-scale training scenarios. Our technical contributions include:
Analysis for Effective Scaling of Muon: Through extensive analysis, we identify that weight decay plays a crucial roles in Muon's scalability. Besides, we proposed to keep a consistent update root mean square (RMS) across different matrix and non-matrix parameters through parameter-wise update scale adjustments. Such adjustments significantly enhanced training stability.
Efficient Distributed Implementation: We develop a distributed version of Muon with ZeRO-1 style optimization, achieving optimal memory efficiency and reduced communication overhead while preserving the mathematical properties of the algorithm.
Scaling Law Validation: We performed scaling law research that compares Muon with strong AdamW baselines, and showed the superior performance of Muon (see Figure 1). Based on the scaling law results, Muon achieves comparable performance to AdamW trained counterparts while requiring only approximately 52% of the training FLOPs.
Scaling up with Muon. (a) Scaling law experiments comparing Muon and Adam. Muon is 2 times more sample efficient than Adam. (b) The MMLU performance of our Moonlight model optimized with Muon and other comparable models. Moonlight advances the Pareto frontier of performance vs training FLOPs.
Performance
We compared Moonlight with SOTA public models at similar scale:
- LLAMA3-3B is a 3B-parameter dense model trained with 9T tokens
- Qwen2.5-3B is a 3B-parameter dense model trained with 18T tokens
- Deepseek-v2-Lite is a 2.4B/16B-parameter MOE model trained with 5.7T tokens
| Benchmark (Metric) | Llama3.2-3B | Qwen2.5-3B | DSV2-Lite | Moonlight | |
|---|---|---|---|---|---|
| Activated Param† | 2.81B | 2.77B | 2.24B | 2.24B | |
| Total Params† | 2.81B | 2.77B | 15.29B | 15.29B | |
| Training Tokens | 9T | 18T | 5.7T | 5.7T | |
| Optimizer | AdamW | * | AdamW | Muon | |
| English | MMLU | 54.75 | 65.6 | 58.3 | 70.0 |
| MMLU-pro | 25.0 | 34.6 | 25.5 | 42.4 | |
| BBH | 46.8 | 56.3 | 44.1 | 65.2 | |
| TriviaQA‡ | 59.6 | 51.1 | 65.1 | 66.3 | |
| Code | HumanEval | 28.0 | 42.1 | 29.9 | 48.1 |
| MBPP | 48.7 | 57.1 | 43.2 | 63.8 | |
| Math | GSM8K | 34.0 | 79.1 | 41.1 | 77.4 |
| MATH | 8.5 | 42.6 | 17.1 | 45.3 | |
| CMath | - | 80.0 | 58.4 | 81.1 | |
| Chinese | C-Eval | - | 75.0 | 60.3 | 77.2 |
| CMMLU | - | 75.0 | 64.3 | 78.2 |
Qwen 2 & 2.5 reports didn't disclose their optimizer information. †The reported parameter counts exclude the embedding parameters. ‡We test all listed models with the full set of TriviaQA.
Example usage
Model Download
| Model | #Total Params | #Activated Params | Context Length | Download Link |
|---|---|---|---|---|
| Moonlight-16B-A3B | 16B | 3B | 8K | 🤗 Hugging Face |
| Moonlight-16B-A3B-Instruct | 16B | 3B | 8K | 🤗 Hugging Face |
Inference with Hugging Face Transformers
This fork targets transformers==5.13.0. Recommended environment: python=3.10+,
torch>=2.1 (verified on torch==2.6.0+cu124), transformers==5.13.0. Both snippets below
were re-run end-to-end against this fork and the verified outputs are shown after each.
For the base pretrained model (Moonlight-16B-A3B) — apply the same modeling_deepseek.py
patches described above if you want to run the base model on transformers==5.13.0:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0" # this fork; the base variant follows the same pattern
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
prompt = "1+1=2, 1+2="
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(model.device)
generated_ids = model.generate(**inputs, max_new_tokens=100)
response = tokenizer.batch_decode(generated_ids)[0]
print(response)
Verified output (transformers==5.13.0, torch==2.6.0+cu124, single H100):
1+1=2, 1+2=3, 2+1=3, 2+2=4, 3+1=4, 3+2=5, 3+3=6, 4+1=5, 4+2=6, 4+3=7, 4+4=8, 5+1=6, 5+2=7, 5+3=8, 5+4=9, 5+5=10,
For the instruct model (this fork). Note the change from the upstream snippet:
apply_chat_template(..., return_tensors="pt") returns a BatchEncoding in
transformers>=5, not a bare tensor, so we pass return_dict=True and unpack it into
generate(**inputs, ...):
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "melhoushi/Moonlight-16B-A3B-Instruct-transformers-5.13.0"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
messages = [
{"role": "system", "content": "You are a helpful assistant provided by Moonshot-AI."},
{"role": "user", "content": "Is 123 a prime?"},
]
inputs = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
).to(model.device)
generated_ids = model.generate(**inputs, max_new_tokens=500)
response = tokenizer.batch_decode(generated_ids)[0]
print(response)
Verified output (transformers==5.13.0, torch==2.6.0+cu124, single H100):
<|im_system|>system<|im_middle|>You are a helpful assistant provided by Moonshot-AI.<|im_end|><|im_user|>user<|im_middle|>Is 123 a prime?<|im_end|><|im_assistant|>assistant<|im_middle|>To determine if 123 is a prime number, we need to check if it has any divisors other than 1 and itself. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.
Let's check for divisibility by smaller prime numbers:
1. **Divisibility by 2**: 123 is an odd number, so it is not divisible by 2.
2. **Divisibility by 3**: Sum the digits of 123: \(1 + 2 + 3 = 6\). Since 6 is divisible by 3, 123 is also divisible by 3.
Since 123 is divisible by 3, it is not a prime number.<|im_end|>
Moonlight has the same architecture as DeepSeek-V3, which is supported by many popular inference engines, such as VLLM and SGLang. As a result, our model can also be easily deployed using these tools.
Citation
If you find Moonlight is useful or want to use in your projects, please kindly cite our paper:
@misc{liu2025muonscalablellmtraining,
title={Muon is Scalable for LLM Training},
author={Jingyuan Liu and Jianlin Su and Xingcheng Yao and Zhejun Jiang and Guokun Lai and Yulun Du and Yidao Qin and Weixin Xu and Enzhe Lu and Junjie Yan and Yanru Chen and Huabin Zheng and Yibo Liu and Shaowei Liu and Bohong Yin and Weiran He and Han Zhu and Yuzhi Wang and Jianzhou Wang and Mengnan Dong and Zheng Zhang and Yongsheng Kang and Hao Zhang and Xinran Xu and Yutao Zhang and Yuxin Wu and Xinyu Zhou and Zhilin Yang},
year={2025},
eprint={2502.16982},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2502.16982},
}
- Downloads last month
- 219
