Run this model with HF Jobs
Run Mmmmmzzzzz/DeepSeek-Math-V2 on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/Mmmmmzzzzz/DeepSeek-Math-V2. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/Mmmmmzzzzz/DeepSeek-Math-V2 on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/Mmmmmzzzzz/DeepSeek-Math-V2. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Instructions to use Mmmmmzzzzz/DeepSeek-Math-V2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Mmmmmzzzzz/DeepSeek-Math-V2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Mmmmmzzzzz/DeepSeek-Math-V2") 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("Mmmmmzzzzz/DeepSeek-Math-V2") model = AutoModelForCausalLM.from_pretrained("Mmmmmzzzzz/DeepSeek-Math-V2", 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 Mmmmmzzzzz/DeepSeek-Math-V2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Mmmmmzzzzz/DeepSeek-Math-V2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Mmmmmzzzzz/DeepSeek-Math-V2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Mmmmmzzzzz/DeepSeek-Math-V2
- SGLang
How to use Mmmmmzzzzz/DeepSeek-Math-V2 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 "Mmmmmzzzzz/DeepSeek-Math-V2" \ --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": "Mmmmmzzzzz/DeepSeek-Math-V2", "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 "Mmmmmzzzzz/DeepSeek-Math-V2" \ --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": "Mmmmmzzzzz/DeepSeek-Math-V2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use Mmmmmzzzzz/DeepSeek-Math-V2 with Docker Model Runner:
docker model run hf.co/Mmmmmzzzzz/DeepSeek-Math-V2
Commit ·
86117ae
0
Parent(s):
Duplicate from deepseek-ai/DeepSeek-Math-V2
Browse filesCo-authored-by: Zhihong Shao <ZhihongShao@users.noreply.huggingface.co>
This view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributesAdded+35
- LICENSEAdded+202
- README.mdAdded+93
- config.jsonAdded+66
- generation_config.jsonAdded+9
- inference/README.mdAdded+21
- inference/config_671B_v3.2.jsonAdded+26
- inference/convert.pyAdded+101
- inference/dist_writer.pyAdded+224
- inference/generate.pyAdded+194
- inference/model_v32.pyAdded+941
- inference/requirements.txtAdded+6
- inference/tilelang_kernel.pyAdded+274
- model-00001-of-000163.safetensorsAdded+3
- model-00002-of-000163.safetensorsAdded+3
- model-00003-of-000163.safetensorsAdded+3
- model-00004-of-000163.safetensorsAdded+3
- model-00005-of-000163.safetensorsAdded+3
- model-00006-of-000163.safetensorsAdded+3
- model-00007-of-000163.safetensorsAdded+3
- model-00008-of-000163.safetensorsAdded+3
- model-00009-of-000163.safetensorsAdded+3
- model-00010-of-000163.safetensorsAdded+3
- model-00011-of-000163.safetensorsAdded+3
- model-00012-of-000163.safetensorsAdded+3
- model-00013-of-000163.safetensorsAdded+3
- model-00014-of-000163.safetensorsAdded+3
- model-00015-of-000163.safetensorsAdded+3
- model-00016-of-000163.safetensorsAdded+3
- model-00017-of-000163.safetensorsAdded+3
- model-00018-of-000163.safetensorsAdded+3
- model-00019-of-000163.safetensorsAdded+3
- model-00020-of-000163.safetensorsAdded+3
- model-00021-of-000163.safetensorsAdded+3
- model-00022-of-000163.safetensorsAdded+3
- model-00023-of-000163.safetensorsAdded+3
- model-00024-of-000163.safetensorsAdded+3
- model-00025-of-000163.safetensorsAdded+3
- model-00026-of-000163.safetensorsAdded+3
- model-00027-of-000163.safetensorsAdded+3
- model-00028-of-000163.safetensorsAdded+3
- model-00029-of-000163.safetensorsAdded+3
- model-00030-of-000163.safetensorsAdded+3
- model-00031-of-000163.safetensorsAdded+3
- model-00032-of-000163.safetensorsAdded+3
- model-00033-of-000163.safetensorsAdded+3
- model-00034-of-000163.safetensorsAdded+3
- model-00035-of-000163.safetensorsAdded+3
- model-00036-of-000163.safetensorsAdded+3
- model-00037-of-000163.safetensorsAdded+3
1234567891011121314151617181920212223242526272829303132333435*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright (c) 2023 DeepSeek
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293---license: apache-2.0library_name: transformersbase_model: - deepseek-ai/DeepSeek-Math-V2---
<!-- markdownlint-disable first-line-h1 --><!-- markdownlint-disable html --><!-- markdownlint-disable no-duplicate-header -->
<div align="center"> <img src="https://github.com/deepseek-ai/DeepSeek-V2/blob/main/figures/logo.svg?raw=true" width="60%" alt="DeepSeek-V3" /></div><hr><div align="center" style="line-height: 1;"> <a href="https://www.deepseek.com/"><img alt="Homepage" src="https://github.com/deepseek-ai/DeepSeek-V2/blob/main/figures/badge.svg?raw=true"/></a> <a href="https://chat.deepseek.com/"><img alt="Chat" src="https://img.shields.io/badge/🤖%20Chat-DeepSeek%20V3-536af5?color=536af5&logoColor=white"/></a> <a href="https://huggingface.co/deepseek-ai"><img alt="Hugging Face" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-DeepSeek%20AI-ffc107?color=ffc107&logoColor=white"/></a> <br> <a href="https://discord.gg/Tc7c45Zzu5"><img alt="Discord" src="https://img.shields.io/badge/Discord-DeepSeek%20AI-7289da?logo=discord&logoColor=white&color=7289da"/></a> <a href="https://github.com/deepseek-ai/DeepSeek-V2/blob/main/figures/qr.jpeg?raw=true"><img alt="Wechat" src="https://img.shields.io/badge/WeChat-DeepSeek%20AI-brightgreen?logo=wechat&logoColor=white"/></a> <a href="https://twitter.com/deepseek_ai"><img alt="Twitter Follow" src="https://img.shields.io/badge/Twitter-deepseek_ai-white?logo=x&logoColor=white"/></a> <br> <a href="LICENSE" style="margin: 2px;"> <img alt="License" src="https://img.shields.io/badge/License-Apache 2.0-f5de53?&color=f5de53" style="display: inline-block; vertical-align: middle;"/> </a> <br></div>
# DeepSeekMath-V2: Towards Self-Verifiable Mathematical Reasoning
## 1. Introduction
Large language models have made significant progress in mathematical reasoning, which serves as an important testbed for AI and could impact scientific research if further advanced.By scaling reasoning with reinforcement learning that rewards correct final answers, LLMs have improved from poor performance to saturating quantitative reasoning competitions like AIME and HMMT in one year.However, this approach faces fundamental limitations.Pursuing higher final answer accuracy doesn't address a key issue: correct answers don't guarantee correct reasoning.Moreover, many mathematical tasks like theorem proving require rigorous step-by-step derivation rather than numerical answers, making final answer rewards inapplicable.To push the limits of deep reasoning, we believe it is necessary to verify the comprehensiveness and rigor of mathematical reasoning.Self-verification is particularly important for scaling test-time compute, especially for open problems without known solutions.Towards self-verifiable mathematical reasoning, we investigate how to train an accurate and faithful LLM-based verifier for theorem proving.We then train a proof generator using the verifier as the reward model, and incentivize the generator to identify and resolve as many issues as possible in their own proofs before finalizing them.To maintain the generation-verification gap as the generator becomes stronger, we propose to scale verification compute to automatically label new hard-to-verify proofs, creating training data to further improve the verifier.Our resulting model, DeepSeekMath-V2, demonstrates strong theorem-proving capabilities, achieving gold-level scores on IMO 2025 and CMO 2024 and a near-perfect 118/120 on Putnam 2024 with scaled test-time compute.While much work remains, these results suggest that self-verifiable mathematical reasoning is a feasible research direction that may help develop more capable mathematical AI systems.
## 2. Evaluation Results
Below are evaluation results on [IMO-ProofBench](https://github.com/google-deepmind/superhuman/tree/main/imobench) (developed by the DeepMind team behind DeepThink IMO-Gold) and recent mathematics competitions including IMO 2025, CMO 2024, and Putnam 2024.
**IMO-ProofBench**
<p align="center"> <img width="100%" src="https://raw.githubusercontent.com/deepseek-ai/DeepSeek-Math-V2/refs/heads/main/figures/IMO-ProofBench.png"></p>
---
**Mathematics Competitions**
<p align="center"> <img width=41%" src="https://raw.githubusercontent.com/deepseek-ai/DeepSeek-Math-V2/refs/heads/main/figures/Competitions.png"></p>
## 4. Quick Start
DeepSeekMath-V2 is built on top of DeepSeek-V3.2-Exp-Base.For inference support, please refer to [the DeepSeek-V3.2-Exp github repository](https://github.com/deepseek-ai/DeepSeek-V3.2-Exp).
## 6. LicenseThis repository and the model weights are licensed under [the Apache License, Version 2.0 (Apache 2.0)](LICENSE).
## 7. Citation
```@misc{deepseek-math-v2, author = {Zhihong Shao, Yuxiang Luo, Chengda Lu, Z.Z. Ren, Jiewen Hu, Tian Ye, Zhibin Gou, Shirong Ma, Xiaokang Zhang}, title = {DeepSeekMath-V2: Towards Self-Verifiable Mathematical Reasoning}, year = {2025},}```
## 8. Contact
If you have any questions, please raise an issue or contact us at [service@deepseek.com](mailto:service@deepseek.com).123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566{ "architectures": [ "DeepseekV32ForCausalLM" ], "attention_bias": false, "attention_dropout": 0.0, "bos_token_id": 0, "eos_token_id": 1, "ep_size": 1, "first_k_dense_replace": 3, "hidden_act": "silu", "hidden_size": 7168, "index_head_dim": 128, "index_n_heads": 64, "index_topk": 2048, "initializer_range": 0.02, "intermediate_size": 18432, "kv_lora_rank": 512, "max_position_embeddings": 163840, "model_type": "deepseek_v32", "moe_intermediate_size": 2048, "moe_layer_freq": 1, "n_group": 8, "n_routed_experts": 256, "n_shared_experts": 1, "norm_topk_prob": true, "num_attention_heads": 128, "num_experts_per_tok": 8, "num_hidden_layers": 61, "num_key_value_heads": 128, "num_nextn_predict_layers": 1, "q_lora_rank": 1536, "qk_nope_head_dim": 128, "qk_rope_head_dim": 64, "quantization_config": { "activation_scheme": "dynamic", "fmt": "e4m3", "quant_method": "fp8", "scale_fmt": "ue8m0", "weight_block_size": [ 128, 128 ] }, "rms_norm_eps": 1e-06, "rope_scaling": { "beta_fast": 32, "beta_slow": 1, "factor": 40, "mscale": 1.0, "mscale_all_dim": 1.0, "original_max_position_embeddings": 4096, "type": "yarn" }, "rope_theta": 10000, "routed_scaling_factor": 2.5, "scoring_func": "sigmoid", "tie_word_embeddings": false, "topk_group": 4, "topk_method": "noaux_tc", "torch_dtype": "bfloat16", "transformers_version": "4.44.2", "use_cache": true, "v_head_dim": 128, "vocab_size": 129280}123456789{ "_from_model_config": true, "bos_token_id": 0, "eos_token_id": 1, "do_sample": true, "temperature": 0.6, "top_p": 0.95, "transformers_version": "4.46.3"}123456789101112131415161718192021# Inference code for DeepSeek models
First convert huggingface model weight files to the format of this project.```bashpython convert.py --hf-ckpt-path ${HF_CKPT_PATH} --save-path ${SAVE_PATH} --n-experts ${EXPERTS} --model-parallel ${MP}```
Then chat with DeepSeek model at will!```bashtorchrun --nproc-per-node ${MP} generate.py --ckpt-path ${SAVE_PATH} --config ${CONFIG} --interactive --temperature {T}```
Or batch inference from file.```bashtorchrun --nproc-per-node ${MP} generate.py --ckpt-path ${SAVE_PATH} --config ${CONFIG} --input-file ${FILE}```
Or multi nodes inference.```bashtorchrun --nnodes ${NODES} --nproc-per-node $((MP / NODES)) --node-rank $RANK --master-addr $ADDR generate.py --ckpt-path ${SAVE_PATH} --config ${CONFIG} --input-file ${FILE}```1234567891011121314151617181920212223242526{ "vocab_size": 129280, "dim": 7168, "inter_dim": 18432, "moe_inter_dim": 2048, "n_layers": 61, "n_dense_layers": 3, "n_heads": 128, "n_routed_experts": 256, "n_shared_experts": 1, "n_activated_experts": 8, "n_expert_groups": 8, "n_limited_groups": 4, "route_scale": 2.5, "score_func": "sigmoid", "q_lora_rank": 1536, "kv_lora_rank": 512, "qk_nope_head_dim": 128, "qk_rope_head_dim": 64, "v_head_dim": 128, "dtype": "fp8", "scale_fmt": "ue8m0", "index_n_heads": 64, "index_head_dim": 128, "index_topk": 2048}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import osimport shutilfrom argparse import ArgumentParserfrom glob import globfrom tqdm import tqdm, trange
import torchfrom safetensors.torch import safe_openfrom dist_writer import save_file
mapping = { "embed_tokens": ("embed", 0), "input_layernorm": ("attn_norm", None), "post_attention_layernorm": ("ffn_norm", None), "q_proj": ("wq", 0), "q_a_proj": ("wq_a", None), "q_a_layernorm": ("q_norm", None), "q_b_proj": ("wq_b", 0), "kv_a_proj_with_mqa": ("wkv_a", None), "kv_a_layernorm": ("kv_norm", None), "kv_b_proj": ("wkv_b", 0), "o_proj": ("wo", 1), "gate": ("gate", None), "gate_proj": ("w1", 0), "down_proj": ("w2", 1), "up_proj": ("w3", 0), "norm": ("norm", None), "lm_head": ("head", 0), "scale": ("scale", None), "wq_b": ("wq_b", None), "wk": ("wk", None), "k_norm": ("k_norm", None), "weights_proj": ("weights_proj", None),}
def main(hf_ckpt_path, save_path, n_experts, mp): """ Converts and saves model checkpoint files into a specified format.
Args: hf_ckpt_path (str): Path to the directory containing the input checkpoint files. save_path (str): Path to the directory where the converted checkpoint files will be saved. n_experts (int): Total number of experts in the model. mp (int): Model parallelism factor. Returns: None """ torch.set_num_threads(8) n_local_experts = n_experts // mp state_dicts = [{} for _ in range(mp)]
for file_path in tqdm(glob(os.path.join(hf_ckpt_path, "*.safetensors"))): with safe_open(file_path, framework="pt", device="cpu") as f: for name in f.keys(): if "model.layers.61" in name: continue param: torch.Tensor = f.get_tensor(name) if name.startswith("model."): name = name[len("model."):] name = name.replace("self_attn", "attn") name = name.replace("mlp", "ffn") name = name.replace("weight_scale_inv", "scale") name = name.replace("e_score_correction_bias", "bias") key = name.split(".")[-2] assert key in mapping, f"Key {key} not found in mapping" new_key, dim = mapping[key] name = name.replace(key, new_key) for i in range(mp): new_param = param if "experts" in name and "shared_experts" not in name: idx = int(name.split(".")[-3]) if idx < i * n_local_experts or idx >= (i + 1) * n_local_experts: continue elif dim is not None: assert param.size(dim) % mp == 0, f"Dimension {dim} must be divisible by {mp}" shard_size = param.size(dim) // mp new_param = param.narrow(dim, i * shard_size, shard_size).contiguous() state_dicts[i][name] = new_param
os.makedirs(save_path, exist_ok=True)
for i in trange(mp): save_file(state_dicts[i], os.path.join(save_path, f"model{i}-mp{mp}.safetensors"))
for file_path in glob(os.path.join(hf_ckpt_path, "*token*")): new_file_path = os.path.join(save_path, os.path.basename(file_path)) shutil.copyfile(file_path, new_file_path)
if __name__ == "__main__": parser = ArgumentParser() parser.add_argument("--hf-ckpt-path", type=str, required=True) parser.add_argument("--save-path", type=str, required=True) parser.add_argument("--n-experts", type=int, required=True) parser.add_argument("--model-parallel", type=int, required=True) args = parser.parse_args() assert args.n_experts % args.model_parallel == 0, "Number of experts must be divisible by model parallelism" main(args.hf_ckpt_path, args.save_path, args.n_experts, args.model_parallel)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224# copy from https://gitlab.deepseek.com/deepseek/hai-llm/-/blob/master/scripts/dist_safetensor_writer.py
import osimport mathimport torchfrom pathlib import Pathfrom datetime import timedeltafrom multiprocessing.shared_memory import SharedMemoryfrom uuid import uuid4import numpy as npimport timeimport jsontry: from hf3fs_fuse.io import make_iovec, make_ioring, ioring, register_fd, deregister_fd, h3fioexcept Exception: pass
INT_LEN = 8BYTE_ORDER = 'big'
def tensor_to_bytes(tensor: torch.Tensor) -> bytes: if tensor.numel() == 0: return b'' return tensor.view(torch.int8).numpy().data.cast('B')
except_fs = {'cpu'}clusters = ['jd', 'hg']hf3fs_paths = []hf3fs_mount_points = []for cluster in clusters: hf3fs_paths += os.listdir(f'/hf3fs-{cluster}') if os.path.exists(f'/hf3fs-{cluster}') else [] hf3fs_mount_points += [os.path.join(f'/hf3fs-{cluster}', f) for f in hf3fs_paths if f not in except_fs]
def get_hf3fs_mount_point(file_path: str) -> str: rp = os.path.realpath(Path(file_path).absolute()) return '/'.join(rp.split('/')[:3])
class DistWriter(): def __init__(self, max_ops=100<<10, write_buf_size=1<<29): self.max_ops = max_ops self.write_buf_size = write_buf_size self.shm = SharedMemory(name=f'hf3fs-iovs-{uuid4()}', create=True, size=self.write_buf_size) self._iov = {} self._buf = {} self._ior = {} for hf3fs_mount_point in hf3fs_mount_points: try: iov = make_iovec(self.shm, hf3fs_mount_point, block_size=0, numa=-1) buf = memoryview(iov.iov) ior = make_ioring(hf3fs_mount_point, 100 << 10, for_read=False, io_depth=-1, numa=-1) self._iov[hf3fs_mount_point] = iov self._buf[hf3fs_mount_point] = buf self._ior[hf3fs_mount_point] = ior except Exception: pass self.shm.unlink() self.fd_cache = {}
def _open(self, file_path): if self.fd_cache.get(file_path) is None: # os.makedirs(os.path.dirname(file_path), exist_ok=True) hf3fs_mount_point = get_hf3fs_mount_point(file_path) try: fd = os.open(file_path, os.O_WRONLY | os.O_CREAT | os.O_SYNC) except Exception: # 发现在 weka 上打开文件会 FileExistsError fd = os.open(file_path, os.O_WRONLY | os.O_SYNC) register_fd(fd) self.fd_cache[file_path] = (fd, hf3fs_mount_point) return self.fd_cache[file_path]
def _close_all(self, file_total_bytes): for fd, _ in self.fd_cache.values(): os.truncate(fd, file_total_bytes) deregister_fd(fd) os.close(fd) self.fd_cache = {}
def chunk_batch_pwrite(self, write_offsets): chunks = [] chunk = [] total = 0 def add_chunk(): nonlocal chunk, total if len(chunk) > 0: chunks.append(chunk) chunk = [] total = 0
for r in write_offsets: write_file_path, write_bytes, write_file_offset = r write_length = len(write_bytes) if write_length == 0: continue if write_length > self.write_buf_size: add_chunk() chunks.append([r]) elif total + write_length > self.write_buf_size: add_chunk() chunk.append(r) total += write_length else: chunk.append(r) total += write_length if len(chunk) == self.max_ops: add_chunk() add_chunk() return chunks
def convert_to_pwrite_list(self, filepath, tensors, metadata): head = {} if metadata is not None: head["__metadata__"] = metadata dtype_dict = { torch.float64 : 'F64', torch.float32: 'F32', torch.float16 : 'F16', torch.bfloat16: 'BF16', torch.float8_e4m3fn: 'F8_E4M3', torch.int64 : 'I64', torch.int32: 'I32', torch.int16 : 'I16', torch.int8: 'I8', torch.uint8 : 'U8', torch.bool : 'BOOL' } cur_off = 0 values = [] for k, v in tensors.items(): cur_len = v.numel() * v.element_size() item = dict( dtype = dtype_dict[v.dtype], shape = list(v.shape), data_offsets = [cur_off, cur_off + cur_len], ) cur_off += cur_len head[k] = item values.append(v) head_bytes = json.dumps(head, ensure_ascii=True).replace(" ","").encode("utf8") n = np.array([len(head_bytes)], dtype = np.uint64).tobytes() assert np.frombuffer(n, dtype=np.int64)[0] == len(head_bytes) head_bytes = n + head_bytes p_list = [] p_list.append((filepath, head_bytes, 0)) cur_off = len(head_bytes) for v in values: data_bytes = tensor_to_bytes(v) p_list.append((filepath, data_bytes, cur_off)) cur_off += len(data_bytes) return p_list
def save_tensors(self, filepath, tensors, metadata = None): pwrite_list = self.convert_to_pwrite_list(filepath, tensors, metadata) file_total_bytes = sum([len(item[1]) for item in pwrite_list]) for chunk in self.chunk_batch_pwrite(pwrite_list): if len(chunk) == 1: # 如果超过 self.write_buf_size 的数据,只允许单次 pwrite write_file_path, write_bytes, write_file_offset = chunk[0] fd, hf3fs_mount_point = self._open(write_file_path) iov = self._iov[hf3fs_mount_point] buf = self._buf[hf3fs_mount_point] ior = self._ior[hf3fs_mount_point] content_view = write_bytes _write = 0 total = len(write_bytes) while _write < total: to_write = min(self.write_buf_size, total-_write) buf[:to_write] = content_view[_write:_write+to_write] ior.prepare(iov[:to_write], False, fd, write_file_offset+_write) submit_result = ior.submit() total_waited = 0 results = [] while True: res = submit_result.wait(max_results=1000, min_results=0, timeout=timedelta(seconds=0)) total_waited += len(res) results += res if total_waited == 1: break time.sleep(0.01) write_len = results[0].result assert write_len == to_write, f'hf3fs 返回的 write_len({write_len}) 不匹配 file_path={write_file_path} offset={write_file_offset} to_write={to_write}' _write += write_len elif len(chunk) > 0: # 多次 pwrite,加起来的总和不能超过 self.write_buf_size,避免最后一个比较大,但是 buf 只剩很小,要提交很多次的问题 # 这里只允许 batch write 同一个 mount point 的数据,不然比较难管理 hf3fs_mount_point = self._open(chunk[0][0])[1] iov = self._iov[hf3fs_mount_point] buf = self._buf[hf3fs_mount_point] ior = self._ior[hf3fs_mount_point] ops = [] buf_offsets = [] buf_offset = 0 for write_file_path, write_bytes, write_file_offset in chunk: fd, h = self._open(write_file_path) assert h == hf3fs_mount_point, f'不能 load 不同 mount point 的数据 {h} {hf3fs_mount_point}' write_length = len(write_bytes) op = [write_file_path, write_length, write_file_offset] ops.append(op) assert buf_offset+write_length <= self.write_buf_size, f'batch write 超过了 buf 最大长度 {self.write_buf_size}' buf[buf_offset:buf_offset+write_length] = write_bytes ior.prepare(iov[buf_offset:buf_offset+write_length], False, fd, write_file_offset, userdata=op) buf_offsets.append((buf_offset, buf_offset+write_length)) buf_offset += write_length
submit_result = ior.submit() total_waited = 0 results = [] while True: res = submit_result.wait(max_results=1000, min_results=0, timeout=timedelta(seconds=0)) total_waited += len(res) results += res if total_waited == len(ops): break time.sleep(0.01) for result in results: write_file_path, write_length, write_file_offset = result.userdata assert result.result == write_length, f'hf3fs 返回的 write_len({result.result}) 不匹配 file_path={write_file_path} offset={write_file_offset} to_write={write_length}' self._close_all(file_total_bytes)
def save_file(tensors, filepath, metadata = None): DistWriter().save_tensors(filepath, tensors, metadata=metadata)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194import osimport jsonfrom argparse import ArgumentParserfrom typing import List
import torchimport torch.distributed as distfrom transformers import AutoTokenizerfrom safetensors.torch import load_model
from model_v32 import Transformer, ModelArgs, update_ws
def sample(logits, temperature: float = 1.0): """ Samples a token from the logits using temperature scaling.
Args: logits (torch.Tensor): The logits tensor for token predictions. temperature (float, optional): Temperature for scaling logits. Defaults to 1.0.
Returns: torch.Tensor: The sampled token. """ logits = logits / max(temperature, 1e-5) probs = torch.softmax(logits, dim=-1, dtype=torch.float32) return probs.div_(torch.empty_like(probs).exponential_(1)).argmax(dim=-1)
@torch.inference_mode()def generate( model: Transformer, prompt_tokens: List[List[int]], max_new_tokens: int, eos_id: int, temperature: float = 1.0) -> List[List[int]]: """ Generates new tokens based on the given prompt tokens using the specified model.
Args: model (Transformer): The transformer model used for token generation. prompt_tokens (List[List[int]]): A list of lists containing the prompt tokens for each sequence. max_new_tokens (int): The maximum number of new tokens to generate. eos_id (int): The end-of-sequence token ID. temperature (float, optional): The temperature value for sampling. Defaults to 1.0.
Returns: List[List[int]]: A list of lists containing the generated tokens for each sequence. """ prompt_lens = [len(t) for t in prompt_tokens] assert max(prompt_lens) <= model.max_seq_len, f"Prompt length exceeds model maximum sequence length (max_seq_len={model.max_seq_len})" total_len = min(model.max_seq_len, max_new_tokens + max(prompt_lens)) tokens = torch.full((len(prompt_tokens), total_len), -1, dtype=torch.long, device="cuda") for i, t in enumerate(prompt_tokens): tokens[i, :len(t)] = torch.tensor(t, dtype=torch.long, device="cuda") prev_pos = 0 finished = torch.tensor([False] * len(prompt_tokens), device="cuda") prompt_mask = tokens != -1 for cur_pos in range(min(prompt_lens), total_len): logits = model.forward(tokens[:, prev_pos:cur_pos], prev_pos) if temperature > 0: next_token = sample(logits, temperature) else: next_token = logits.argmax(dim=-1) next_token = torch.where(prompt_mask[:, cur_pos], tokens[:, cur_pos], next_token) tokens[:, cur_pos] = next_token finished |= torch.logical_and(~prompt_mask[:, cur_pos], next_token == eos_id) prev_pos = cur_pos if finished.all(): break completion_tokens = [] for i, toks in enumerate(tokens.tolist()): toks = toks[prompt_lens[i]:prompt_lens[i]+max_new_tokens] if eos_id in toks: toks = toks[:toks.index(eos_id)] completion_tokens.append(toks) return completion_tokens
def main( ckpt_path: str, config: str, input_file: str = "", interactive: bool = True, max_new_tokens: int = 100, temperature: float = 1.0,) -> None: """ Main function to load the model and perform interactive or batch text generation.
Args: ckpt_path (str): Path to the model checkpoint directory. config (str): Path to the model configuration file. input_file (str, optional): Path to a file containing input prompts. Defaults to "". interactive (bool, optional): Whether to run in interactive mode. Defaults to True. max_new_tokens (int, optional): Maximum number of new tokens to generate. Defaults to 100. temperature (float, optional): Temperature for sampling. Defaults to 1.0. """ world_size = int(os.getenv("WORLD_SIZE", "1")) rank = int(os.getenv("RANK", "0")) local_rank = int(os.getenv("LOCAL_RANK", "0")) if world_size > 1: dist.init_process_group("nccl") global print if rank != 0: print = lambda *_, **__: None torch.cuda.set_device(local_rank) torch.set_default_dtype(torch.bfloat16) torch.set_num_threads(8) torch.manual_seed(33377335) with open(config) as f: args = ModelArgs(**json.load(f)) if "baidu" in input_file: args.max_batch_size = 1 args.max_seq_len = 24 * 1024 print(args) with torch.device("cuda"): model = Transformer(args) tokenizer = AutoTokenizer.from_pretrained(ckpt_path) print("dummy run") tokenizer.decode(generate(model, [tokenizer.encode("DeepSeek")], 2, -1, 1.)[0]) print("load model") load_model(model, os.path.join(ckpt_path, f"model{rank}-mp{world_size}.safetensors")) update_ws(model) print("I'm DeepSeek 👋")
if interactive: messages = [] while True: if world_size == 1: prompt = input(">>> ") elif rank == 0: prompt = input(">>> ") objects = [prompt] dist.broadcast_object_list(objects, 0) else: objects = [None] dist.broadcast_object_list(objects, 0) prompt = objects[0] if prompt == "/exit": break elif prompt == "/clear": messages.clear() continue messages.append({"role": "user", "content": prompt}) prompt_tokens = tokenizer.apply_chat_template(messages, add_generation_prompt=True) completion_tokens = generate(model, [prompt_tokens], max_new_tokens, tokenizer.eos_token_id, temperature) completion = tokenizer.decode(completion_tokens[0], skip_special_tokens=True) print(completion) messages.append({"role": "assistant", "content": completion}) else: with open(input_file) as f: prompts = f.read().split("\n\n") prompt_tokens = [tokenizer.apply_chat_template([{"role": "user", "content": prompt}], add_generation_prompt=True) for prompt in prompts] if "baidu" in input_file: completion_tokens = [generate(model, [p], max_new_tokens, tokenizer.eos_token_id, temperature)[0] for p in prompt_tokens] else: completion_tokens = generate(model, prompt_tokens, max_new_tokens, tokenizer.eos_token_id, temperature) completions = tokenizer.batch_decode(completion_tokens, skip_special_tokens=True) for prompt, completion in zip(prompts, completions): print("Prompt:", prompt) print("Completion:", completion) print()
if world_size > 1: dist.destroy_process_group()
if __name__ == "__main__": """ Command-line interface for distributed text generation.
Arguments: --ckpt-path (str): Path to the model checkpoint directory. --config (str): Path to the model configuration file. --input-file (str, optional): File containing prompts for batch processing. --interactive (bool, optional): Enable interactive mode for generating text. --max-new-tokens (int, optional): Maximum number of new tokens to generate. Defaults to 200. --temperature (float, optional): Temperature for sampling. Defaults to 0.2.
Raises: AssertionError: If neither input-file nor interactive mode is specified. """ parser = ArgumentParser() parser.add_argument("--ckpt-path", type=str, required=True) parser.add_argument("--config", type=str, required=True) parser.add_argument("--input-file", type=str, default="") parser.add_argument("--interactive", action="store_true") parser.add_argument("--max-new-tokens", type=int, default=200) parser.add_argument("--temperature", type=float, default=0.2) args = parser.parse_args() assert args.input_file or args.interactive, "Either input-file or interactive mode must be specified" main(args.ckpt_path, args.config, args.input_file, args.interactive, args.max_new_tokens, args.temperature)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941import mathfrom dataclasses import dataclassfrom typing import Tuple, Optional, Literal
import torchfrom torch import nnimport torch.nn.functional as Fimport torch.distributed as dist
from tilelang_kernel import act_quant, fp8_gemm, fp8_index
world_size = 1rank = 0block_size = 128
@dataclassclass ModelArgs: """ Data class for defining model arguments and hyperparameters.
Attributes: max_batch_size (int): Maximum batch size. max_seq_len (int): Maximum sequence length. dtype (Literal["bf16", "fp8"]): Data type for computations. scale_fmt (Optional[str]): Format for quantization scale. vocab_size (int): Vocabulary size. dim (int): Model dimension. inter_dim (int): Intermediate dimension for MLP layers. moe_inter_dim (int): Intermediate dimension for MoE layers. n_layers (int): Number of transformer layers. n_dense_layers (int): Number of dense layers in the model. n_heads (int): Number of attention heads. n_routed_experts (int): Number of routed experts for MoE layers. n_shared_experts (int): Number of shared experts for MoE layers. n_activated_experts (int): Number of activated experts in MoE layers. n_expert_groups (int): Number of expert groups. n_limited_groups (int): Number of limited groups for MoE routing. score_func (Literal["softmax", "sigmoid"]): Scoring function for MoE routing. route_scale (float): Scaling factor for routing scores. q_lora_rank (int): LoRA rank for query projections. kv_lora_rank (int): LoRA rank for key-value projections. qk_nope_head_dim (int): Dimension for query-key projections without positional embeddings. qk_rope_head_dim (int): Dimension for query-key projections with rotary embeddings. v_head_dim (int): Dimension for value projections. original_seq_len (int): Original sequence length. rope_theta (float): Base for rotary positional encoding. rope_factor (float): Scaling factor for extended sequence lengths. beta_fast (int): Fast beta correction factor. beta_slow (int): Slow beta correction factor. mscale (float): Scaling factor for extended attention. index_head_dim (int): Dimension for index head. index_topk (int): Top-k for index head. """ max_batch_size: int = 8 max_seq_len: int = 4096 * 4 dtype: Literal["bf16", "fp8"] = "bf16" scale_fmt: Optional[str] = None vocab_size: int = 102400 dim: int = 2048 inter_dim: int = 10944 moe_inter_dim: int = 1408 n_layers: int = 27 n_dense_layers: int = 1 n_heads: int = 16 # moe n_routed_experts: int = 64 n_shared_experts: int = 2 n_activated_experts: int = 6 n_expert_groups: int = 1 n_limited_groups: int = 1 score_func: Literal["softmax", "sigmoid"] = "softmax" route_scale: float = 1. # mla q_lora_rank: int = 0 kv_lora_rank: int = 512 qk_nope_head_dim: int = 128 qk_rope_head_dim: int = 64 v_head_dim: int = 128 # yarn original_seq_len: int = 4096 rope_theta: float = 10000.0 rope_factor: float = 40 beta_fast: int = 32 beta_slow: int = 1 mscale: float = 1. # index index_n_heads: int = 64 index_head_dim: int = 128 index_topk: int = 2048
class ParallelEmbedding(nn.Module): """ Embedding layer with parallelism support across distributed processes.
Args: vocab_size (int): Vocabulary size. dim (int): Embedding dimension. """ def __init__(self, vocab_size: int, dim: int): super().__init__() self.vocab_size = vocab_size self.dim = dim assert vocab_size % world_size == 0, f"Vocabulary size must be divisible by world size (world_size={world_size})" self.part_vocab_size = (vocab_size // world_size) self.vocab_start_idx = rank * self.part_vocab_size self.vocab_end_idx = self.vocab_start_idx + self.part_vocab_size self.weight = nn.Parameter(torch.empty(self.part_vocab_size, self.dim))
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward pass for parallel embedding layer.
Args: x (torch.Tensor): Input tensor containing token indices.
Returns: torch.Tensor: Embedded representations.
Raises: ValueError: If `world_size` is not defined. """ if world_size > 1: mask = (x < self.vocab_start_idx) | (x >= self.vocab_end_idx) x = x - self.vocab_start_idx x[mask] = 0 y = F.embedding(x, self.weight) if world_size > 1: y[mask] = 0 dist.all_reduce(y) return y
def linear(x: torch.Tensor, weight: torch.Tensor, bias: Optional[torch.Tensor] = None, scale_fmt: Optional[str] = None) -> torch.Tensor: """ Applies a linear transformation to the incoming data: y = xA^T + b. This function supports specialized implementations based on quantization and tensor formats.
Args: x (torch.Tensor): The input tensor. weight (torch.Tensor): The weight tensor. bias (Optional[torch.Tensor]): The bias tensor to be added. Default is None. scale_fmt (Optional[str]): The format of scaling factors.
Returns: torch.Tensor: The result of the linear transformation, which may involve quantization-aware computations depending on the input parameters.
Notes: - If `weight` is not quantized, a normal version is used for computation. - For other cases, the function applies quantization to `x` and uses `fp8_gemm` for computation. """ assert bias is None
if weight.dtype != torch.float8_e4m3fn: return F.linear(x, weight) else: x, scale = act_quant(x, block_size, scale_fmt) return fp8_gemm(x, scale, weight, weight.scale)
class Linear(nn.Module): """ Custom linear layer with support for quantized weights and optional bias.
Args: in_features (int): Number of input features. out_features (int): Number of output features. bias (bool): Whether to include a bias term. Defaults to False. dtype (optional): Data type for the layer. Defaults to `torch.bfloat16`. """ dtype = torch.bfloat16 scale_fmt: Optional[str] = None
def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype = None): super().__init__() self.in_features = in_features self.out_features = out_features self.weight = nn.Parameter(torch.empty(out_features, in_features, dtype=dtype or Linear.dtype)) if self.weight.element_size() == 1: scale_out_features = (out_features + block_size - 1) // block_size scale_in_features = (in_features + block_size - 1) // block_size self.weight.scale = self.scale = nn.Parameter(torch.empty(scale_out_features, scale_in_features, dtype=torch.float32)) else: self.register_parameter("scale", None) if bias: self.bias = nn.Parameter(torch.empty(out_features)) else: self.register_parameter("bias", None)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward pass for the custom linear layer.
Args: x (torch.Tensor): Input tensor.
Returns: torch.Tensor: Transformed tensor after linear computation. """ return linear(x, self.weight, self.bias, self.scale_fmt)
class ColumnParallelLinear(Linear): """ Linear layer with column parallelism, splitting output features across distributed processes.
Args: in_features (int): Number of input features. out_features (int): Total number of output features. bias (bool): Whether to include a bias term. Defaults to False. dtype (optional): Data type for the layer. Defaults to `torch.bfloat16`. """ def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype = None): assert out_features % world_size == 0, f"Output features must be divisible by world size (world_size={world_size})" self.part_out_features = out_features // world_size super().__init__(in_features, self.part_out_features, bias, dtype)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward pass for column parallel linear layer.
Args: x (torch.Tensor): Input tensor.
Returns: torch.Tensor: Transformed tensor with column-parallel computation. """ y = linear(x, self.weight, self.bias, self.scale_fmt) return y
class RowParallelLinear(Linear): """ Linear layer with row parallelism, splitting input features across distributed processes.
Args: in_features (int): Total number of input features. out_features (int): Number of output features. bias (bool): Whether to include a bias term. Defaults to False. dtype (optional): Data type for the layer. Defaults to `torch.bfloat16`. """ def __init__(self, in_features: int, out_features: int, bias: bool = False, reduce_output = True, dtype = None): assert in_features % world_size == 0, f"Input features must be divisible by world size (world_size={world_size})" self.part_in_features = in_features // world_size self.reduce_output = reduce_output super().__init__(self.part_in_features, out_features, bias, dtype)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward pass for row parallel linear layer.
Args: x (torch.Tensor): Input tensor.
Returns: torch.Tensor: Transformed tensor with row-parallel computation. """ y = linear(x, self.weight, None, self.scale_fmt) if self.reduce_output and world_size > 1: y = y.float() dist.all_reduce(y) if self.bias is not None: y += self.bias return y.type_as(x)
class RMSNorm(nn.Module): """ Root Mean Square Layer Normalization (RMSNorm).
Args: dim (int): Dimension of the input tensor. eps (float): Epsilon value for numerical stability. Defaults to 1e-6. """ def __init__(self, dim: int, eps: float = 1e-6): super().__init__() self.dim = dim self.eps = eps # rmsnorm in the checkpoint is stored in bf16, while the parameter here is stored in fp32 for convenient. self.weight = nn.Parameter(torch.ones(dim, dtype=torch.float32))
def forward(self, x: torch.Tensor, residual: Optional[torch.Tensor] = None): """ Forward pass for RMSNorm.
Args: x (torch.Tensor): Input tensor.
Returns: torch.Tensor: Normalized tensor with the same shape as input. """ dtype = x.dtype if residual is None: x = x.float() var = x.pow(2).mean(-1, keepdim=True) x = x * torch.rsqrt(var + self.eps) return (self.weight * x).to(dtype) else: x = residual = x.float() + residual.float() var = x.pow(2).mean(-1, keepdim=True) x = x * torch.rsqrt(var + self.eps) return (self.weight * x).to(dtype), residual.to(dtype)
class LayerNorm(nn.Module): """ Layer Normalization. """ def __init__(self, dim: int, eps: float = 1e-6): super().__init__() self.dim = dim self.eps = eps # layernorm in the checkpoint is stored in bf16, while the parameters here are stored in fp32 for convenient. self.weight = nn.Parameter(torch.ones(dim, dtype=torch.float32)) self.bias = nn.Parameter(torch.zeros(dim, dtype=torch.float32))
def forward(self, x: torch.Tensor): return F.layer_norm(x.float(), (self.dim,), self.weight, self.bias, self.eps).type_as(x)
def precompute_freqs_cis(args: ModelArgs) -> torch.Tensor: """ Precomputes frequency-based complex exponential values for rotary positional embeddings.
Args: args (ModelArgs): Model arguments containing positional embedding parameters.
Returns: torch.Tensor: Precomputed complex exponential values for positional embeddings. """ dim = args.qk_rope_head_dim seqlen = args.max_seq_len beta_fast = args.beta_fast beta_slow = args.beta_slow base = args.rope_theta factor = args.rope_factor
def find_correction_dim(num_rotations, dim, base, max_seq_len): """ Computes the correction dimension for a given number of rotations in the rotary positional embedding.
Args: num_rotations (float): Number of rotations to compute the correction for. dim (int): Dimensionality of the embedding space. base (float): Base value for the exponential computation. max_seq_len (int): Maximum sequence length.
Returns: float: The correction dimension based on the input parameters. """ return dim * math.log(max_seq_len / (num_rotations * 2 * math.pi)) / (2 * math.log(base))
def find_correction_range(low_rot, high_rot, dim, base, max_seq_len): """ Computes the range of correction dimensions for rotary positional embeddings.
Args: low_rot (float): Lower bound for the number of rotations. high_rot (float): Upper bound for the number of rotations. dim (int): Dimensionality of the embedding space. base (float): Base value for the exponential computation. max_seq_len (int): Maximum sequence length.
Returns: Tuple[int, int]: The range of correction dimensions (low, high), clamped to valid indices. """ low = math.floor(find_correction_dim(low_rot, dim, base, max_seq_len)) high = math.ceil(find_correction_dim(high_rot, dim, base, max_seq_len)) return max(low, 0), min(high, dim-1)
def linear_ramp_factor(min, max, dim): """ Computes a linear ramp function used to smooth values between a minimum and maximum range.
Args: min (float): Minimum value for the ramp function. max (float): Maximum value for the ramp function. dim (int): Dimensionality of the ramp tensor.
Returns: torch.Tensor: A tensor of shape (dim,) with values linearly interpolated between 0 and 1, clamped to the range [0, 1]. """ if min == max: max += 0.001 linear_func = (torch.arange(dim, dtype=torch.float32) - min) / (max - min) ramp_func = torch.clamp(linear_func, 0, 1) return ramp_func
freqs = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim)) if seqlen > args.original_seq_len: low, high = find_correction_range(beta_fast, beta_slow, dim, base, args.original_seq_len) smooth = 1 - linear_ramp_factor(low, high, dim // 2) freqs = freqs / factor * (1 - smooth) + freqs * smooth
t = torch.arange(seqlen) freqs = torch.outer(t, freqs) freqs_cis = torch.polar(torch.ones_like(freqs), freqs) return freqs_cis
def apply_rotary_emb(x: torch.Tensor, freqs_cis: torch.Tensor, interleaved: bool = True) -> torch.Tensor: """ Applies rotary positional embeddings to the input tensor.
Args: x (torch.Tensor): Input tensor with positional embeddings to be applied. freqs_cis (torch.Tensor): Precomputed complex exponential values for positional embeddings.
Returns: torch.Tensor: Tensor with rotary embeddings applied. """ dtype = x.dtype shape = x.shape if not interleaved: x = x.view(*shape[:-1], 2, -1).transpose(-1, -2).contiguous() x = torch.view_as_complex(x.float().view(*shape[:-1], -1, 2)) freqs_cis = freqs_cis.view(1, x.size(1), 1, x.size(-1)) y = torch.view_as_real(x * freqs_cis).flatten(3) if not interleaved: y = torch.cat([y[..., 0::2], y[..., 1::2]], dim=-1) return y.to(dtype)
def rotate_activation(x: torch.Tensor) -> torch.Tensor: assert x.dtype == torch.bfloat16 from fast_hadamard_transform import hadamard_transform hidden_size = x.size(-1) # 确保hidden_size是2的幂 return hadamard_transform(x, scale=hidden_size ** -0.5)
class Indexer(torch.nn.Module): def __init__(self, args: ModelArgs): super().__init__() self.dim: int = args.dim self.n_heads: int = args.index_n_heads self.n_local_heads = args.index_n_heads // world_size self.head_dim: int = args.index_head_dim self.rope_head_dim: int = args.qk_rope_head_dim self.index_topk: int = args.index_topk self.q_lora_rank: int = args.q_lora_rank self.wq_b = Linear(self.q_lora_rank, self.n_heads * self.head_dim) self.wk = Linear(self.dim, self.head_dim) self.k_norm = LayerNorm(self.head_dim) # weights_proj in the checkpoint is stored in bf16, while the parameters here are stored in fp32 for convenient. self.weights_proj = Linear(self.dim, self.n_heads, dtype=torch.float32) self.softmax_scale = self.head_dim ** -0.5 self.scale_fmt = args.scale_fmt
self.register_buffer("k_cache", torch.zeros(args.max_batch_size, args.max_seq_len, self.head_dim, dtype=torch.float8_e4m3fn), persistent=False) self.register_buffer("k_scale_cache", torch.zeros(args.max_batch_size, args.max_seq_len, self.head_dim // block_size, dtype=torch.float32), persistent=False)
def forward(self, x: torch.Tensor, qr: torch.Tensor, start_pos: int, freqs_cis: torch.Tensor, mask: Optional[torch.Tensor]): bsz, seqlen, _ = x.size() end_pos = start_pos + seqlen q = self.wq_b(qr) q = q.view(bsz, seqlen, self.n_heads, self.head_dim) q_pe, q_nope = torch.split(q, [self.rope_head_dim, self.head_dim - self.rope_head_dim], dim=-1) # rope in indexer is not interleaved q_pe = apply_rotary_emb(q_pe, freqs_cis, False) q = torch.cat([q_pe, q_nope], dim=-1) k = self.wk(x) k = self.k_norm(k) k_pe, k_nope = torch.split(k, [self.rope_head_dim, self.head_dim - self.rope_head_dim], dim=-1) # rope in indexer is not interleaved k_pe = apply_rotary_emb(k_pe.unsqueeze(2), freqs_cis, False).squeeze(2) k = torch.cat([k_pe, k_nope], dim=-1) q = rotate_activation(q) k = rotate_activation(k) q_fp8, q_scale = act_quant(q, block_size, self.scale_fmt) k_fp8, k_scale = act_quant(k, block_size, self.scale_fmt) self.k_cache[:bsz, start_pos:end_pos] = k_fp8 self.k_scale_cache[:bsz, start_pos:end_pos] = k_scale weights = self.weights_proj(x.float()) * self.n_heads ** -0.5 weights = weights.unsqueeze(-1) * q_scale * self.softmax_scale index_score = fp8_index(q_fp8.contiguous(), weights, self.k_cache[:bsz, :end_pos].contiguous(), self.k_scale_cache[:bsz, :end_pos].contiguous()) if mask is not None: index_score += mask topk_indices = index_score.topk(min(self.index_topk, end_pos), dim=-1)[1] topk_indices_ = topk_indices.clone() dist.broadcast(topk_indices_, src=0) assert torch.all(topk_indices == topk_indices_), f"{topk_indices=} {topk_indices_=}" return topk_indices
def weight_dequant(weight, scale): shape = weight.shape assert weight.dim() == 2 weight = weight.view(shape[0] // block_size, block_size, shape[1] // block_size, block_size).transpose(1, 2).reshape(-1, block_size * block_size) weight = (weight.float() * scale.view(-1, 1).float()).to(torch.get_default_dtype()).view(shape[0] // block_size, shape[1] // block_size, block_size, block_size).transpose(1, 2).reshape(shape) return weight
def update_ws(model): for m in model.modules(): if isinstance(m, MLA): if m.wkv_b.scale is not None: m.dequant_wkv_b = weight_dequant(m.wkv_b.weight, m.wkv_b.scale)
class MLA(nn.Module): """ Multi-Head Latent Attention (MLA) Layer.
Attributes: dim (int): Dimensionality of the input features. n_heads (int): Number of attention heads. n_local_heads (int): Number of local attention heads for distributed systems. q_lora_rank (int): Rank for low-rank query projection. kv_lora_rank (int): Rank for low-rank key/value projection. qk_nope_head_dim (int): Dimensionality of non-positional query/key projections. qk_rope_head_dim (int): Dimensionality of rotary-positional query/key projections. qk_head_dim (int): Total dimensionality of query/key projections. v_head_dim (int): Dimensionality of value projections. softmax_scale (float): Scaling factor for softmax in attention computation. """ def __init__(self, args: ModelArgs): super().__init__() self.dim = args.dim self.n_heads = args.n_heads self.n_local_heads = args.n_heads // world_size self.q_lora_rank = args.q_lora_rank self.kv_lora_rank = args.kv_lora_rank self.qk_nope_head_dim = args.qk_nope_head_dim self.qk_rope_head_dim = args.qk_rope_head_dim self.qk_head_dim = args.qk_nope_head_dim + args.qk_rope_head_dim self.v_head_dim = args.v_head_dim
self.wq_a = Linear(self.dim, self.q_lora_rank) self.q_norm = RMSNorm(self.q_lora_rank) self.wq_b = ColumnParallelLinear(self.q_lora_rank, self.n_heads * self.qk_head_dim) self.wkv_a = Linear(self.dim, self.kv_lora_rank + self.qk_rope_head_dim) self.kv_norm = RMSNorm(self.kv_lora_rank) self.wkv_b = ColumnParallelLinear(self.kv_lora_rank, self.n_heads * (self.qk_nope_head_dim + self.v_head_dim)) self.wo = RowParallelLinear(self.n_heads * self.v_head_dim, self.dim) self.softmax_scale = self.qk_head_dim ** -0.5 self.scale_fmt = args.scale_fmt if args.max_seq_len > args.original_seq_len: mscale = 0.1 * args.mscale * math.log(args.rope_factor) + 1.0 self.softmax_scale = self.softmax_scale * mscale * mscale
self.indexer = Indexer(args)
self.register_buffer("kv_cache", torch.zeros(args.max_batch_size, args.max_seq_len, self.kv_lora_rank), persistent=False) self.register_buffer("pe_cache", torch.zeros(args.max_batch_size, args.max_seq_len, self.qk_rope_head_dim), persistent=False) self.dequant_wkv_b = None
def forward(self, x: torch.Tensor, start_pos: int, freqs_cis: torch.Tensor, mask: Optional[torch.Tensor]): """ Forward pass for the Multi-Head Latent Attention (MLA) Layer.
Args: x (torch.Tensor): Input tensor of shape (batch_size, seq_len, dim). start_pos (int): Starting position in the sequence for caching. freqs_cis (torch.Tensor): Precomputed complex exponential values for rotary embeddings. mask (Optional[torch.Tensor]): Mask tensor to exclude certain positions from attention.
Returns: torch.Tensor: Output tensor with the same shape as the input. """ bsz, seqlen, _ = x.size() end_pos = start_pos + seqlen qr = self.q_norm(self.wq_a(x)) q = self.wq_b(qr) q = q.view(bsz, seqlen, self.n_local_heads, self.qk_head_dim) q_nope, q_pe = torch.split(q, [self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1) q_pe = apply_rotary_emb(q_pe, freqs_cis) kv = self.wkv_a(x) kv, k_pe = torch.split(kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1) kv = self.kv_norm(kv) k_pe = apply_rotary_emb(k_pe.unsqueeze(2), freqs_cis) # we use fp8 kv cache in actual deployment, so here we simulate the precision by casting kv to fp8 and then back to bf16. kv_fp8, kv_scale = act_quant(kv, block_size, self.scale_fmt) kv = (kv_fp8.view(-1, block_size).float() * kv_scale.view(-1, 1)).to(kv.dtype).view_as(kv) self.kv_cache[:bsz, start_pos:end_pos] = kv self.pe_cache[:bsz, start_pos:end_pos] = k_pe.squeeze(2) if mask is not None: # MHA prefill q = torch.cat([q_nope, q_pe], dim=-1) kv = self.wkv_b(kv) kv = kv.view(bsz, seqlen, self.n_local_heads, self.qk_nope_head_dim + self.v_head_dim) k_nope, v = torch.split(kv, [self.qk_nope_head_dim, self.v_head_dim], dim=-1) k = torch.cat([k_nope, k_pe.expand(-1, -1, self.n_local_heads, -1)], dim=-1) del q_nope, q_pe, kv, k_nope, k_pe, kv_fp8, kv_scale scores = torch.einsum("bshd,bthd->bsht", q, k).mul_(self.softmax_scale) del q, k
# indexer topk_indices = self.indexer(x, qr, start_pos, freqs_cis, mask) index_mask = torch.full((bsz, seqlen, seqlen), float("-inf"), device=x.device).scatter_(-1, topk_indices, 0) index_mask += mask scores += index_mask.unsqueeze(2) del topk_indices, index_mask, qr
scores = scores.softmax(dim=-1) x = torch.einsum("bsht,bthd->bshd", scores, v) # use code below if oom # scores_ = torch.empty_like(scores) # for i in range(0, scores.size(1), 512): # scores_[:, i: i+512] = scores[:, i: i+512].softmax(dim=-1) # del scores # x = torch.einsum("bsht,bthd->bshd", scores_, v) else: # MQA decode if self.wkv_b.scale is not None and self.dequant_wkv_b is None: self.dequant_wkv_b = weight_dequant(self.wkv_b.weight, self.wkv_b.scale) wkv_b = self.wkv_b.weight if self.wkv_b.scale is None else self.dequant_wkv_b wkv_b = wkv_b.view(self.n_local_heads, -1, self.kv_lora_rank) q_nope = torch.einsum("bshd,hdc->bshc", q_nope, wkv_b[:, :self.qk_nope_head_dim]) scores = (torch.einsum("bshc,btc->bsht", q_nope, self.kv_cache[:bsz, :end_pos]) + torch.einsum("bshr,btr->bsht", q_pe, self.pe_cache[:bsz, :end_pos])) * self.softmax_scale
# indexer topk_indices = self.indexer(x, qr, start_pos, freqs_cis, mask) index_mask = torch.full((bsz, 1, end_pos), float("-inf"), device=x.device).scatter_(-1, topk_indices, 0) scores += index_mask.unsqueeze(2)
scores = scores.softmax(dim=-1) x = torch.einsum("bsht,btc->bshc", scores, self.kv_cache[:bsz, :end_pos]) x = torch.einsum("bshc,hdc->bshd", x, wkv_b[:, -self.v_head_dim:]) x = self.wo(x.flatten(2)) return x
class MLP(nn.Module): """ Multi-Layer Perceptron (MLP) used as a feed-forward layer.
Attributes: w1 (nn.Module): Linear layer for input-to-hidden transformation. w2 (nn.Module): Linear layer for hidden-to-output transformation. w3 (nn.Module): Additional linear layer for feature transformation. """ def __init__(self, dim: int, inter_dim: int, reduce_output: bool = True): """ Initializes the MLP layer.
Args: dim (int): Input and output dimensionality. inter_dim (int): Hidden layer dimensionality. """ super().__init__() self.w1 = ColumnParallelLinear(dim, inter_dim) self.w2 = RowParallelLinear(inter_dim, dim, reduce_output=reduce_output) self.w3 = ColumnParallelLinear(dim, inter_dim)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward pass for the MLP layer.
Args: x (torch.Tensor): Input tensor.
Returns: torch.Tensor: Output tensor after MLP computation. """ return self.w2((F.silu(self.w1(x).float()) * self.w3(x).float()).type_as(x))
class Gate(nn.Module): """ Gating mechanism for routing inputs in a mixture-of-experts (MoE) model.
Attributes: dim (int): Dimensionality of input features. topk (int): Number of top experts activated for each input. n_groups (int): Number of groups for routing. topk_groups (int): Number of groups to route inputs to. score_func (str): Scoring function ('softmax' or 'sigmoid'). route_scale (float): Scaling factor for routing weights. weight (torch.nn.Parameter): Learnable weights for the gate. bias (Optional[torch.nn.Parameter]): Optional bias term for the gate. """ def __init__(self, args: ModelArgs): """ Initializes the Gate module.
Args: args (ModelArgs): Model arguments containing gating parameters. """ super().__init__() self.dim = args.dim self.topk = args.n_activated_experts self.n_groups = args.n_expert_groups self.topk_groups = args.n_limited_groups self.score_func = args.score_func self.route_scale = args.route_scale self.weight = nn.Parameter(torch.empty(args.n_routed_experts, args.dim)) self.bias = nn.Parameter(torch.empty(args.n_routed_experts, dtype=torch.float32)) if self.dim == 7168 else None
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ Forward pass for the gating mechanism.
Args: x (torch.Tensor): Input tensor.
Returns: Tuple[torch.Tensor, torch.Tensor]: Routing weights and selected expert indices. """ scores = linear(x.float(), self.weight.float()) if self.score_func == "softmax": scores = scores.softmax(dim=-1) else: scores = scores.sigmoid() original_scores = scores if self.bias is not None: scores = scores + self.bias if self.n_groups > 1: scores = scores.view(x.size(0), self.n_groups, -1) if self.bias is None: group_scores = scores.amax(dim=-1) else: group_scores = scores.topk(2, dim=-1)[0].sum(dim=-1) indices = group_scores.topk(self.topk_groups, dim=-1)[1] mask = scores.new_ones(x.size(0), self.n_groups, dtype=bool).scatter_(1, indices, False) scores = scores.masked_fill_(mask.unsqueeze(-1), float("-inf")).flatten(1) indices = scores.topk(self.topk, dim=-1)[1] weights = original_scores.gather(1, indices) if self.score_func == "sigmoid": weights /= weights.sum(dim=-1, keepdim=True) weights *= self.route_scale return weights, indices
class Expert(nn.Module): """ Expert layer for Mixture-of-Experts (MoE) models.
Attributes: w1 (nn.Module): Linear layer for input-to-hidden transformation. w2 (nn.Module): Linear layer for hidden-to-output transformation. w3 (nn.Module): Additional linear layer for feature transformation. """ def __init__(self, dim: int, inter_dim: int): """ Initializes the Expert layer.
Args: dim (int): Input and output dimensionality. inter_dim (int): Hidden layer dimensionality. """ super().__init__() self.w1 = Linear(dim, inter_dim) self.w2 = Linear(inter_dim, dim) self.w3 = Linear(dim, inter_dim)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward pass for the Expert layer.
Args: x (torch.Tensor): Input tensor.
Returns: torch.Tensor: Output tensor after expert computation. """ return self.w2((F.silu(self.w1(x).float()) * self.w3(x).float()).type_as(x))
class MoE(nn.Module): """ Mixture-of-Experts (MoE) module.
Attributes: dim (int): Dimensionality of input features. n_routed_experts (int): Total number of experts in the model. n_local_experts (int): Number of experts handled locally in distributed systems. n_activated_experts (int): Number of experts activated for each input. gate (nn.Module): Gating mechanism to route inputs to experts. experts (nn.ModuleList): List of expert modules. shared_experts (nn.Module): Shared experts applied to all inputs. """ def __init__(self, args: ModelArgs): """ Initializes the MoE module.
Args: args (ModelArgs): Model arguments containing MoE parameters. """ super().__init__() self.dim = args.dim assert args.n_routed_experts % world_size == 0, f"Number of experts must be divisible by world size (world_size={world_size})" self.n_routed_experts = args.n_routed_experts self.n_local_experts = args.n_routed_experts // world_size self.n_activated_experts = args.n_activated_experts self.experts_start_idx = rank * self.n_local_experts self.experts_end_idx = self.experts_start_idx + self.n_local_experts self.gate = Gate(args) self.experts = nn.ModuleList([Expert(args.dim, args.moe_inter_dim) if self.experts_start_idx <= i < self.experts_end_idx else None for i in range(self.n_routed_experts)]) self.shared_experts = MLP(args.dim, args.n_shared_experts * args.moe_inter_dim, reduce_output=False) def run_gate(self, x): return self.gate(x)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward pass for the MoE module.
Args: x (torch.Tensor): Input tensor.
Returns: torch.Tensor: Output tensor after expert routing and computation. """ shape = x.size() x = x.view(-1, self.dim) weights, indices = self.run_gate(x) y = torch.zeros_like(x, dtype=torch.float32) counts = torch.bincount(indices.flatten(), minlength=self.n_routed_experts).tolist() for i in range(self.experts_start_idx, self.experts_end_idx): if counts[i] == 0: continue expert = self.experts[i] idx, top = torch.where(indices == i) y[idx] += expert(x[idx]) * weights[idx, top, None] y += self.shared_experts(x) if world_size > 1: dist.all_reduce(y) return y.type_as(x).view(shape)
class Block(nn.Module): """ Transformer block combining attention and feed-forward layers.
Attributes: attn (nn.Module): Attention layer (MLA). ffn (nn.Module): Feed-forward network (MLP or MoE). attn_norm (nn.Module): Layer normalization for attention. ffn_norm (nn.Module): Layer normalization for feed-forward network. """ def __init__(self, layer_id: int, args: ModelArgs): """ Initializes the Transformer block.
Args: layer_id (int): Layer index in the transformer. args (ModelArgs): Model arguments containing block parameters. """ super().__init__() self.attn = MLA(args) self.ffn = MLP(args.dim, args.inter_dim) if layer_id < args.n_dense_layers else MoE(args) self.attn_norm = RMSNorm(args.dim) self.ffn_norm = RMSNorm(args.dim)
def forward(self, x: torch.Tensor, residual: torch.Tensor, start_pos: int, freqs_cis: torch.Tensor, mask: Optional[torch.Tensor]) -> torch.Tensor: """ Forward pass for the Transformer block.
Args: x (torch.Tensor): Input tensor. start_pos (int): Starting position in the sequence. freqs_cis (torch.Tensor): Precomputed complex exponential values for rotary embeddings. mask (Optional[torch.Tensor]): Mask tensor to exclude certain positions from attention.
Returns: torch.Tensor: Output tensor after block computation. """ if residual is None: x, residual = self.attn_norm(x), x else: x, residual = self.attn_norm(x, residual) x = self.attn(x, start_pos, freqs_cis, mask) x, residual = self.ffn_norm(x, residual) x = self.ffn(x) return x, residual
class Transformer(nn.Module): """ Transformer model with positional embeddings, multiple layers, and output projection.
Attributes: max_seq_len (int): Maximum sequence length for the transformer. embed (nn.Module): Embedding layer for input tokens. layers (torch.nn.ModuleList): List of transformer blocks. norm (nn.Module): Layer normalization applied after all blocks. head (nn.Module): Output projection layer mapping to vocabulary size. freqs_cis (torch.Tensor): Precomputed complex exponential values for rotary embeddings. """ def __init__(self, args: ModelArgs): """ Initializes the Transformer model.
Args: args (ModelArgs): Model arguments containing transformer parameters. """ global world_size, rank world_size = dist.get_world_size() if dist.is_initialized() else 1 rank = dist.get_rank() if dist.is_initialized() else 0 Linear.dtype = torch.float8_e4m3fn if args.dtype == "fp8" else torch.bfloat16 Linear.scale_fmt = args.scale_fmt super().__init__() self.max_seq_len = args.max_seq_len self.embed = ParallelEmbedding(args.vocab_size, args.dim) self.layers = torch.nn.ModuleList() for layer_id in range(args.n_layers): self.layers.append(Block(layer_id, args)) self.norm = RMSNorm(args.dim) # lm_head in the checkpoint is stored in bf16, while the parameter here is stored in fp32 for easier computation of logits later. self.head = ColumnParallelLinear(args.dim, args.vocab_size, dtype=torch.float32) self.register_buffer("freqs_cis", precompute_freqs_cis(args), persistent=False)
@torch.inference_mode() def forward(self, tokens: torch.Tensor, start_pos: int = 0): """ Forward pass for the Transformer model.
Args: tokens (torch.Tensor): Input tensor of token IDs with shape (batch_size, seq_len). start_pos (int, optional): Starting position in the sequence for rotary embeddings. Defaults to 0.
Returns: torch.Tensor: Logits tensor of shape (batch_size, vocab_size). """ seqlen = tokens.size(1) freqs_cis = self.freqs_cis[start_pos:start_pos+seqlen] mask = torch.full((seqlen, seqlen), float("-inf"), device=tokens.device).triu_(1) if seqlen > 1 else None h, residual = self.embed(tokens), None for layer in self.layers: h, residual = layer(h, residual, start_pos, freqs_cis, mask) h, _ = self.norm(h, residual) logits = self.head(h[:, -1].float()) if world_size > 1: all_logits = [torch.empty_like(logits) for _ in range(world_size)] dist.all_gather(all_logits, logits) logits = torch.cat(all_logits, dim=-1) return logits
if __name__ == "__main__": torch.set_default_dtype(torch.bfloat16) torch.set_default_device("cuda") torch.manual_seed(0) args = ModelArgs() x = torch.randint(0, args.vocab_size, (2, 128)) model = Transformer(args) print(model(x).size())123456torchtritontransformerssafetensorsfast_hadamard_transformtilelang==0.1.6.post1123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274import torchimport tilelangimport tilelang.language as Tfrom typing import Tuple, Optional
tilelang.set_log_level("WARNING")
pass_configs = { tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True, tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True, tilelang.PassConfigKey.TL_DISABLE_FAST_MATH: True,}
FP8 = "float8_e4m3"BF16 = "bfloat16"FP32 = "float32"
def fast_log2_ceil(x): bits_x = T.reinterpret("uint32", x) exp_x = (bits_x >> 23) & 0xFF man_bits = bits_x & ((1 << 23) - 1) return T.Cast("int32", exp_x - 127 + T.if_then_else(man_bits != 0, 1, 0))
def fast_pow2(x): bits_x = (x + 127) << 23 return T.reinterpret("float32", bits_x)
def fast_round_scale(amax, fp8_max_inv): return fast_pow2(fast_log2_ceil(amax * fp8_max_inv))
@tilelang.jit(pass_configs=pass_configs)def act_quant_kernel( N, in_dtype=BF16, out_dtype=FP8, scale_dtype=FP32, round_scale=False): M = T.symbolic("M") fp8_min = -448.0 fp8_max = 448.0 fp8_max_inv = 1 / fp8_max num_stages = 0 if round_scale else 2 blk_m = 32 group_size = 128
@T.prim_func def act_quant_kernel_( X: T.Tensor[(M, N), in_dtype], Y: T.Tensor[(M, N), out_dtype], S: T.Tensor[(M, T.ceildiv(N, group_size)), scale_dtype], ): with T.Kernel(T.ceildiv(M, blk_m), T.ceildiv(N, group_size), threads=128) as ( pid_m, pid_n, ): x_shared = T.alloc_shared((blk_m, group_size), in_dtype) x_local = T.alloc_fragment((blk_m, group_size), in_dtype) amax_local = T.alloc_fragment((blk_m,), scale_dtype) s_local = T.alloc_fragment((blk_m,), scale_dtype) y_local = T.alloc_fragment((blk_m, group_size), out_dtype) y_shared = T.alloc_shared((blk_m, group_size), out_dtype)
for _ in T.Pipelined(1, num_stages=num_stages): T.copy(X[pid_m * blk_m, pid_n * group_size], x_shared) T.copy(x_shared, x_local) T.reduce_absmax(x_local, amax_local, dim=1) for i in T.Parallel(blk_m): amax_local[i] = T.max(amax_local[i], 1e-4) if round_scale: s_local[i] = fast_round_scale(amax_local[i], fp8_max_inv) else: s_local[i] = amax_local[i] * fp8_max_inv for i, j in T.Parallel(blk_m, group_size): y_local[i, j] = T.clamp( x_local[i, j] / s_local[i], fp8_min, fp8_max ) for i in T.Parallel(blk_m): S[pid_m * blk_m + i, pid_n] = s_local[i] T.copy(y_local, y_shared) T.copy(y_shared, Y[pid_m * blk_m, pid_n * group_size])
return act_quant_kernel_
def act_quant( x: torch.Tensor, block_size: int = 128, scale_fmt: Optional[str] = None) -> Tuple[torch.Tensor, torch.Tensor]: """ Quantizes the input tensor `x` using block-wise quantization.
Args: x (torch.Tensor): The input tensor to be quantized. Must be contiguous and its last dimension size must be divisible by `block_size`. block_size (int, optional): The size of the blocks to be used for quantization. Default is 128. scale_fmt (Optional[str], optional): The format of the scale. Default is None. Returns: Tuple[torch.Tensor, torch.Tensor]: A tuple containing: - The quantized tensor with dtype `torch.float8_e4m3fn`. - A tensor of scaling factors with dtype `torch.float32`. """ assert x.is_contiguous(), "Input tensor must be contiguous" assert x.size(-1) % block_size == 0, ( f"Last dimension size must be divisible by block_size (block_size={block_size})" ) N = x.size(-1) y = torch.empty_like(x, dtype=torch.float8_e4m3fn) s = x.new_empty(*x.size()[:-1], N // block_size, dtype=torch.float32) kernel = act_quant_kernel(N, round_scale=scale_fmt is not None) kernel(x.view(-1, N), y.view(-1, N), s.view(-1, N // block_size)) return y, s
@tilelang.jit(pass_configs=pass_configs)def fp8_gemm_kernel(N, K, out_dtype=BF16, accum_dtype="float32"): assert out_dtype in [BF16, "float32"]
M = T.symbolic("M") group_size = 128 block_M = 32 block_N = 128 block_K = 128
@T.prim_func def fp8_gemm_kernel_( A: T.Tensor[(M, K), FP8], B: T.Tensor[(N, K), FP8], C: T.Tensor[(M, N), out_dtype], scales_a: T.Tensor[(M, T.ceildiv(K, group_size)), FP32], scales_b: T.Tensor[(T.ceildiv(N, group_size), T.ceildiv(K, group_size)), FP32], ): with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=128) as ( bx, by, ): A_shared = T.alloc_shared((block_M, block_K), FP8) B_shared = T.alloc_shared((block_N, block_K), FP8) C_shared = T.alloc_shared((block_M, block_N), out_dtype) Scale_C_shared = T.alloc_shared((block_M), FP32) C_local = T.alloc_fragment((block_M, block_N), accum_dtype) C_local_accum = T.alloc_fragment((block_M, block_N), accum_dtype)
# Improve L2 Cache T.use_swizzle(panel_size=10)
T.clear(C_local) T.clear(C_local_accum) K_iters = T.ceildiv(K, block_K) for k in T.Pipelined(K_iters, num_stages=4): # Load A into shared memory T.copy(A[by * block_M, k * block_K], A_shared) # Load B into shared memory T.copy(B[bx * block_N, k * block_K], B_shared) # Load scale into shared memory Scale_B = scales_b[bx * block_N // group_size, k] for i in T.Parallel(block_M): Scale_C_shared[i] = scales_a[by * block_M + i, k] * Scale_B
T.gemm(A_shared, B_shared, C_local, transpose_B=True) # Promote to enable 2xAcc for i, j in T.Parallel(block_M, block_N): C_local_accum[i, j] += C_local[i, j] * Scale_C_shared[i] T.clear(C_local) # TMA store T.copy(C_local_accum, C_shared) T.copy(C_shared, C[by * block_M, bx * block_N])
return fp8_gemm_kernel_
def fp8_gemm( a: torch.Tensor, a_s: torch.Tensor, b: torch.Tensor, b_s: torch.Tensor) -> torch.Tensor: """ Perform a matrix multiplication using FP8 precision.
Args: a (torch.Tensor): The first input matrix, must be contiguous. a_s (torch.Tensor): The scaling factor for the first input matrix, must be contiguous. b (torch.Tensor): The second input matrix, must be contiguous. b_s (torch.Tensor): The scaling factor for the second input matrix, must be contiguous.
Returns: torch.Tensor: The result of the matrix multiplication. """ assert a.is_contiguous() and b.is_contiguous(), "Input tensors must be contiguous" assert a_s.is_contiguous() and b_s.is_contiguous(), ( "Scaling factor tensors must be contiguous" ) K = a.size(-1) M = a.numel() // K N = b.size(0) c = a.new_empty(*a.size()[:-1], N, dtype=torch.get_default_dtype()) kernel = fp8_gemm_kernel(N, K) kernel(a.view(M, K), b, c.view(M, N), a_s.view(M, -1), b_s) return c
@tilelang.jit(out_idx=[4], pass_configs=pass_configs)def fp8_index_kernel(h: int, d: int): b = T.symbolic("b") m = T.symbolic("m") n = T.symbolic("n")
blk_n1 = 512 blk_n2 = 128
@T.prim_func def fp8_index_kernel_( q: T.Tensor[(b, m, h, d), FP8], q_s: T.Tensor[(b, m, h), FP32], k: T.Tensor[(b, n, d), FP8], k_s: T.Tensor[(b, n), FP32], o: T.Tensor[(b, m, n), FP32], ) -> None: with T.Kernel(b, m, T.ceildiv(n, blk_n1)) as (i_b, i_m, i1_n): q_smem = T.alloc_shared((h, d), FP8) T.copy(q[i_b, i_m, 0, 0], q_smem)
q_s_frag = T.alloc_fragment(h, FP32) T.copy(q_s[i_b, i_m, 0], q_s_frag)
for i2_n in T.Pipelined(blk_n1 // blk_n2, num_stages=2): k_smem = T.alloc_shared((blk_n2, d), FP8) T.copy(k[i_b, i1_n * blk_n1 + i2_n * blk_n2, 0], k_smem)
k_s_frag = T.alloc_fragment(blk_n2, FP32) T.copy(k_s[i_b, i1_n * blk_n1 + i2_n * blk_n2], k_s_frag)
logits = T.alloc_fragment((blk_n2, h), FP32) T.gemm( k_smem, q_smem, logits, transpose_A=False, transpose_B=True, clear_accum=True, )
for i_h, i3_n in T.Parallel(h, blk_n2): logits[i3_n, i_h] = T.max(logits[i3_n, i_h], 0) * q_s_frag[i_h]
logits_sum = T.alloc_fragment(blk_n2, FP32) T.reduce_sum(logits, logits_sum, dim=1)
for i3_n in T.Parallel(blk_n2): logits_sum[i3_n] *= k_s_frag[i3_n]
T.copy(logits_sum, o[i_b, i_m, i1_n * blk_n1 + i2_n * blk_n2])
return fp8_index_kernel_
def fp8_index( q: torch.Tensor, q_s: torch.Tensor, k: torch.Tensor, k_s: torch.Tensor,) -> torch.Tensor: """ Perform index score using FP8 precision.
Args: q (torch.Tensor): The Q tensor, must be contiguous. q_s (torch.Tensor): The scaling factor for Q (float), must be contiguous. k (torch.Tensor): The K tensor, must be contiguous. k_s (torch.Tensor): The scaling factor for K (e8m0 here), must be contiguous.
fp8 q @ fp8 k -> fp32 logits relu(fp32 logits) * q_s (weights) -> fp32 logits fp32 logits -> fp32 logits_sum fp32 logits_sum * k_s (e8m0) -> fp32 index_score """ return fp8_index_kernel(q.shape[2], q.shape[3])(q, q_s, k, k_s)123version https://git-lfs.github.com/spec/v1oid sha256:3bf13579c58e732f9576e35fb5371061d83ebb224e90e6bcd0a22d72b586248bsize 5233198531123version https://git-lfs.github.com/spec/v1oid sha256:a2952a7b604ac39de300afe9d84e8bb71fb799e9c3e4602dc2d0138bd33c51b9size 4302383956123version https://git-lfs.github.com/spec/v1oid sha256:86d604c3cf718578b809939d6f0600b0a340b2c55c761ba38dd9bb2a6a3244d0size 4302384377123version https://git-lfs.github.com/spec/v1oid sha256:7f25ac892c91d6b156ed1d5ab018b61c5d30abdfaffebb08dbdc8549288ac84asize 4302121967123version https://git-lfs.github.com/spec/v1oid sha256:71ecdd53a830e99bff56aa61b58cdd51f96f871fc3c25754932b45b0c2d4f7fcsize 4302384146123version https://git-lfs.github.com/spec/v1oid sha256:140a23dd2c091377ae6efba83ba8db30dbbabbaffb39d0106a739f29f2bae834size 4307162046123version https://git-lfs.github.com/spec/v1oid sha256:00f8f685db31801d1805b2f2f4ecdc47fa4498edc7c22076dddcc61c2a53ae8asize 4312028034123version https://git-lfs.github.com/spec/v1oid sha256:f2aa12c9bb5044721bebfe4440ebe0a205058445feb462679ecf0aefd3469e8csize 4302384334123version https://git-lfs.github.com/spec/v1oid sha256:f913df371365ef56a7c7fc88ac5904c651dda717b115c4b443d39f14b6794e89size 4302122175123version https://git-lfs.github.com/spec/v1oid sha256:497a9a01a56fb56dd8a68edafc0dff983b1eddfe551dea67d42d53a3c4d28c6esize 4302383938123version https://git-lfs.github.com/spec/v1oid sha256:6465af291be53ccf00312b71fdbbc07916876903b84bc11923cef269593f5927size 4302384377123version https://git-lfs.github.com/spec/v1oid sha256:d32fbd261802b9d20503193fe2516349eb53a52cb8861d8ac959ce6eb39214d1size 1483135583123version https://git-lfs.github.com/spec/v1oid sha256:05ca1cea74eb5434a7f628fa5bf43496a9b899d47077cc1f93178195e5f768ccsize 4302060527123version https://git-lfs.github.com/spec/v1oid sha256:4b51404eb2d75a1f0682f86da6505408ff4be10134f25cd55c1e6aed548ad04dsize 4302384328123version https://git-lfs.github.com/spec/v1oid sha256:5e4be243c5fea52cad7db0e6b319e126f2d0b52503cdd460f1788280fa5ad0c6size 4302122183123version https://git-lfs.github.com/spec/v1oid sha256:dd58081e5e8edea0ce7ef459c951d9d13bfec879523c7cf12364e288e990e2d2size 4302383930123version https://git-lfs.github.com/spec/v1oid sha256:649c56eb19760ca63060b2a0ba949c918c536c9e15cec5b905c61915620f124esize 4302384375123version https://git-lfs.github.com/spec/v1oid sha256:aaae76f041a75c8eefc3f1a21320a414908e062311f4df6bdb1f7807908ac145size 4302121995123version https://git-lfs.github.com/spec/v1oid sha256:f0df3aa19662d47640bd506dba8ce7c01e69704404673a06e6dde72b48fb4132size 4302384118123version https://git-lfs.github.com/spec/v1oid sha256:b895fcd6a76f7a0b1be83028942422416652431d88a27a2c228647e281b61fe4size 4302384377123version https://git-lfs.github.com/spec/v1oid sha256:b03060cddce6151e54b2148508f15d0428aab550294e83b84943ba77c48984e9size 4302122373123version https://git-lfs.github.com/spec/v1oid sha256:cb7de77ba45b1268a92fb204cebabe275b4acae3506e1e2c0e9bbb2f89594167size 4302384890123version https://git-lfs.github.com/spec/v1oid sha256:aea6fbef20522d150b30e6e8f724a35175dd1709245629f6a6539c8344c761f6size 4302122786123version https://git-lfs.github.com/spec/v1oid sha256:4b190f1abbe4c3f3a2b3530319cad97539bc691cd470f09d9a3bc7dc54d04b48size 4302384494123version https://git-lfs.github.com/spec/v1oid sha256:c09e754f5e89564cb91c57a485bb8549ccbe3e00dcf3c232ca8294d3e268825esize 4302384963123version https://git-lfs.github.com/spec/v1oid sha256:2f4cf545ddc9b6cb26b1880ee6e214cce1aa73174d827dc3cbf3a3fce26687eesize 4302122598123version https://git-lfs.github.com/spec/v1oid sha256:97be9a81a09d64ae2fdda52a324fb4c9e61a4e0c14626ec4bc531e4ea6907087size 4302384680123version https://git-lfs.github.com/spec/v1oid sha256:48a40296250db5c15a84f891d5fc522231cece62beec0189d7255091269a1ac1size 4302384963123version https://git-lfs.github.com/spec/v1oid sha256:6dc18c1a1a7f1e15da50eab29a658cbc13541fbb33cd23e2caec93fa52869434size 4302122420123version https://git-lfs.github.com/spec/v1oid sha256:a2a8bafeea8d6e6619fbaef8b646708af2c9fcfb65859fa790f2d8d9b197e842size 4302384870123version https://git-lfs.github.com/spec/v1oid sha256:1279f4092977aecc67864782f77db33ff78127bd8f8e194e2b633f303ace0eb8size 4302122808123version https://git-lfs.github.com/spec/v1oid sha256:109b153570b3a5923d6317c3cfcb180edbd6aa7ace9c60fa4023489a623f9f70size 4302384470123version https://git-lfs.github.com/spec/v1oid sha256:8c6ae8dc8e81a37933d0c113763cd796b912b33471df83a8b5d8ea5146a847fasize 4302384963123version https://git-lfs.github.com/spec/v1oid sha256:f12d45219361dface9621c2755ecd420d229fd75ac786b7f3145df0cb60bbc95size 1864917414123version https://git-lfs.github.com/spec/v1oid sha256:20913598d2c10c55a66c678fd06d5e09c5d7659872f9c6e7d927f39e5df3673bsize 4302061105123version https://git-lfs.github.com/spec/v1oid sha256:e7d93918dcc274ac679e57a184c04417aede4a6ccdcbdc9a4dcdc6117dc89233size 4302384914123version https://git-lfs.github.com/spec/v1oid sha256:58da6cefe61eeba2bcef149d5e6186c39b74f745e73a88fcb7f4f80e9ec45995size 4302122764.gitattributesAdded+35
LICENSEAdded+202
README.mdAdded+93
config.jsonAdded+66
generation_config.jsonAdded+9
inference/README.mdAdded+21
inference/config_671B_v3.2.jsonAdded+26
inference/convert.pyAdded+101
inference/dist_writer.pyAdded+224
inference/generate.pyAdded+194
inference/model_v32.pyAdded+941
inference/requirements.txtAdded+6
inference/tilelang_kernel.pyAdded+274
model-00001-of-000163.safetensorsAdded+3
model-00002-of-000163.safetensorsAdded+3
model-00003-of-000163.safetensorsAdded+3
model-00004-of-000163.safetensorsAdded+3
model-00005-of-000163.safetensorsAdded+3
model-00006-of-000163.safetensorsAdded+3
model-00007-of-000163.safetensorsAdded+3
model-00008-of-000163.safetensorsAdded+3
model-00009-of-000163.safetensorsAdded+3
model-00010-of-000163.safetensorsAdded+3
model-00011-of-000163.safetensorsAdded+3
model-00012-of-000163.safetensorsAdded+3
model-00013-of-000163.safetensorsAdded+3
model-00014-of-000163.safetensorsAdded+3
model-00015-of-000163.safetensorsAdded+3
model-00016-of-000163.safetensorsAdded+3
model-00017-of-000163.safetensorsAdded+3
model-00018-of-000163.safetensorsAdded+3
model-00019-of-000163.safetensorsAdded+3
model-00020-of-000163.safetensorsAdded+3
model-00021-of-000163.safetensorsAdded+3
model-00022-of-000163.safetensorsAdded+3
model-00023-of-000163.safetensorsAdded+3
model-00024-of-000163.safetensorsAdded+3
model-00025-of-000163.safetensorsAdded+3
model-00026-of-000163.safetensorsAdded+3
model-00027-of-000163.safetensorsAdded+3
model-00028-of-000163.safetensorsAdded+3
model-00029-of-000163.safetensorsAdded+3
model-00030-of-000163.safetensorsAdded+3
model-00031-of-000163.safetensorsAdded+3
model-00032-of-000163.safetensorsAdded+3
model-00033-of-000163.safetensorsAdded+3
model-00034-of-000163.safetensorsAdded+3
model-00035-of-000163.safetensorsAdded+3
model-00036-of-000163.safetensorsAdded+3
model-00037-of-000163.safetensorsAdded+3