Text Generation
Transformers
Safetensors
afmoe
Mixture of Experts
nvfp4
modelopt
blackwell
vllm
conversational
custom_code
8-bit precision
Instructions to use CollectionStudio/Trinity-Large-Preview-NVFP4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use CollectionStudio/Trinity-Large-Preview-NVFP4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="CollectionStudio/Trinity-Large-Preview-NVFP4", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("CollectionStudio/Trinity-Large-Preview-NVFP4", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("CollectionStudio/Trinity-Large-Preview-NVFP4", trust_remote_code=True, device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use CollectionStudio/Trinity-Large-Preview-NVFP4 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "CollectionStudio/Trinity-Large-Preview-NVFP4" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "CollectionStudio/Trinity-Large-Preview-NVFP4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/CollectionStudio/Trinity-Large-Preview-NVFP4
- SGLang
How to use CollectionStudio/Trinity-Large-Preview-NVFP4 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 "CollectionStudio/Trinity-Large-Preview-NVFP4" \ --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": "CollectionStudio/Trinity-Large-Preview-NVFP4", "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 "CollectionStudio/Trinity-Large-Preview-NVFP4" \ --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": "CollectionStudio/Trinity-Large-Preview-NVFP4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use CollectionStudio/Trinity-Large-Preview-NVFP4 with Docker Model Runner:
docker model run hf.co/CollectionStudio/Trinity-Large-Preview-NVFP4
Commit ·
0c60a39
0
Parent(s):
Duplicate from arcee-ai/Trinity-Large-Preview-NVFP4
Browse filesCo-authored-by: Anneketh Vij <annekethvij@users.noreply.huggingface.co>
- .gitattributes +38 -0
- .quant_summary.txt +3 -0
- LICENSE +49 -0
- README.md +181 -0
- chat_template.jinja +65 -0
- config.json +252 -0
- configuration_afmoe.py +133 -0
- generation_config.json +9 -0
- hf_quant_config.json +128 -0
- model-00001-of-00024.safetensors +3 -0
- model-00002-of-00024.safetensors +3 -0
- model-00003-of-00024.safetensors +3 -0
- model-00004-of-00024.safetensors +3 -0
- model-00005-of-00024.safetensors +3 -0
- model-00006-of-00024.safetensors +3 -0
- model-00007-of-00024.safetensors +3 -0
- model-00008-of-00024.safetensors +3 -0
- model-00009-of-00024.safetensors +3 -0
- model-00010-of-00024.safetensors +3 -0
- model-00011-of-00024.safetensors +3 -0
- model-00012-of-00024.safetensors +3 -0
- model-00013-of-00024.safetensors +3 -0
- model-00014-of-00024.safetensors +3 -0
- model-00015-of-00024.safetensors +3 -0
- model-00016-of-00024.safetensors +3 -0
- model-00017-of-00024.safetensors +3 -0
- model-00018-of-00024.safetensors +3 -0
- model-00019-of-00024.safetensors +3 -0
- model-00020-of-00024.safetensors +3 -0
- model-00021-of-00024.safetensors +3 -0
- model-00022-of-00024.safetensors +3 -0
- model-00023-of-00024.safetensors +3 -0
- model-00024-of-00024.safetensors +3 -0
- model.safetensors.index.json +3 -0
- modeling_afmoe.py +680 -0
- special_tokens_map.json +23 -0
- tokenizer.json +3 -0
- tokenizer_config.json +272 -0
.gitattributes
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
model.safetensors.index.json filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
.quant_summary.txt filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
.quant_summary.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e1363a006c8d0775b6fe2b5527cfe8aacd30c58c75765b259cbd81acbad30dad
|
| 3 |
+
size 22952354
|
LICENSE
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
OpenMDW License Agreement, version 1.1 (OpenMDW-1.1)
|
| 2 |
+
|
| 3 |
+
By exercising rights granted to you under this agreement, you accept and agree
|
| 4 |
+
to its terms.
|
| 5 |
+
|
| 6 |
+
As used in this agreement, "Model Materials" means the materials provided to
|
| 7 |
+
you under this agreement, consisting of: (1) one or more machine learning
|
| 8 |
+
models (including architecture and parameters); and (2) all related artifacts
|
| 9 |
+
(including associated data, documentation and software) that are provided to
|
| 10 |
+
you hereunder.
|
| 11 |
+
|
| 12 |
+
Subject to your compliance with this agreement, permission is hereby granted,
|
| 13 |
+
free of charge, to deal in the Model Materials without restriction, including
|
| 14 |
+
under all copyright, patent, database, and trade secret rights included or
|
| 15 |
+
embodied therein.
|
| 16 |
+
|
| 17 |
+
If you distribute any portion of the Model Materials, you shall retain in your
|
| 18 |
+
distribution (1) a copy of this agreement, and (2) all copyright notices and
|
| 19 |
+
other notices of origin included in the Model Materials that are applicable to
|
| 20 |
+
your distribution.
|
| 21 |
+
|
| 22 |
+
If you file, maintain, or voluntarily participate in a lawsuit against any
|
| 23 |
+
person or entity asserting that the Model Materials directly or indirectly
|
| 24 |
+
infringe any patent or copyright, then all rights and grants made to you
|
| 25 |
+
hereunder are terminated, unless that lawsuit was in response to a
|
| 26 |
+
corresponding lawsuit first brought against you.
|
| 27 |
+
|
| 28 |
+
This agreement does not impose any restrictions or obligations with respect to
|
| 29 |
+
any use, modification, or sharing of any outputs generated by using the Model
|
| 30 |
+
Materials.
|
| 31 |
+
|
| 32 |
+
THE MODEL MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
| 33 |
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 34 |
+
FITNESS FOR A PARTICULAR PURPOSE, TITLE, NONINFRINGEMENT, ACCURACY, OR THE
|
| 35 |
+
ABSENCE OF LATENT OR OTHER DEFECTS OR ERRORS, WHETHER OR NOT DISCOVERABLE, ALL
|
| 36 |
+
TO THE GREATEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW.
|
| 37 |
+
|
| 38 |
+
YOU ARE SOLELY RESPONSIBLE FOR (1) CLEARING RIGHTS OF OTHER PERSONS THAT MAY
|
| 39 |
+
APPLY TO THE MODEL MATERIALS OR ANY USE THEREOF, INCLUDING WITHOUT LIMITATION
|
| 40 |
+
ANY PERSON'S COPYRIGHTS OR OTHER RIGHTS INCLUDED OR EMBODIED IN THE MODEL
|
| 41 |
+
MATERIALS; (2) OBTAINING ANY NECESSARY CONSENTS, PERMISSIONS OR OTHER RIGHTS
|
| 42 |
+
REQUIRED FOR ANY USE OF THE MODEL MATERIALS; OR (3) PERFORMING ANY DUE
|
| 43 |
+
DILIGENCE OR UNDERTAKING ANY OTHER INVESTIGATIONS INTO THE MODEL MATERIALS OR
|
| 44 |
+
ANYTHING INCORPORATED OR EMBODIED THEREIN.
|
| 45 |
+
|
| 46 |
+
IN NO EVENT SHALL THE PROVIDERS OF THE MODEL MATERIALS BE LIABLE FOR ANY CLAIM,
|
| 47 |
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
| 48 |
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MODEL MATERIALS, THE
|
| 49 |
+
USE THEREOF OR OTHER DEALINGS THEREIN.
|
README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
- es
|
| 6 |
+
- fr
|
| 7 |
+
- de
|
| 8 |
+
- it
|
| 9 |
+
- pt
|
| 10 |
+
- ru
|
| 11 |
+
- ar
|
| 12 |
+
- hi
|
| 13 |
+
- ko
|
| 14 |
+
- zh
|
| 15 |
+
library_name: transformers
|
| 16 |
+
base_model:
|
| 17 |
+
- arcee-ai/Trinity-Large-Preview
|
| 18 |
+
base_model_relation: quantized
|
| 19 |
+
tags:
|
| 20 |
+
- moe
|
| 21 |
+
- nvfp4
|
| 22 |
+
- modelopt
|
| 23 |
+
- blackwell
|
| 24 |
+
- vllm
|
| 25 |
+
license_link: LICENSE
|
| 26 |
+
license_name: openmdw-1.1
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
<div align="center">
|
| 30 |
+
<picture>
|
| 31 |
+
<img
|
| 32 |
+
src="https://cdn-uploads.huggingface.co/production/uploads/6435718aaaef013d1aec3b8b/i-v1KyAMOW_mgVGeic9WJ.png"
|
| 33 |
+
alt="Arcee Trinity Large"
|
| 34 |
+
style="max-width: 100%; height: auto;"
|
| 35 |
+
>
|
| 36 |
+
</picture>
|
| 37 |
+
</div>
|
| 38 |
+
<hr>
|
| 39 |
+
|
| 40 |
+
# Trinity-Large-Preview-NVFP4
|
| 41 |
+
|
| 42 |
+
**This repository contains the NVFP4 quantized weights of Trinity-Large-Preview for deployment on NVIDIA Blackwell GPUs.**
|
| 43 |
+
|
| 44 |
+
Trinity-Large-Preview is a 398B-parameter sparse Mixture-of-Experts (MoE) model with approximately 13B active parameters per token. It is the largest model in Arcee AI's Trinity family, trained on more than 17 trillion tokens and delivering frontier-level performance with strong long-context comprehension.
|
| 45 |
+
Trinity-Large-Preview is a lightly post-trained model based on Trinity-Large-Base.
|
| 46 |
+
|
| 47 |
+
Try it at [chat.arcee.ai](http://chat.arcee.ai/)
|
| 48 |
+
|
| 49 |
+
More details on the training of Trinity Large are available in the [technical report](https://arxiv.org/abs/2602.17004).
|
| 50 |
+
|
| 51 |
+
***
|
| 52 |
+
|
| 53 |
+
## Quantization Details
|
| 54 |
+
|
| 55 |
+
- **Scheme:** NVFP4 (`nvfp4_mlp_only` — MLP/expert weights only, attention remains BF16)
|
| 56 |
+
- **Tool:** [NVIDIA ModelOpt](https://github.com/NVIDIA/Model-Optimizer)
|
| 57 |
+
- **Calibration:** 512 samples, seq_length=2048, all-expert calibration enabled
|
| 58 |
+
- **KV cache:** Not quantized
|
| 59 |
+
|
| 60 |
+
***
|
| 61 |
+
|
| 62 |
+
## Model Variants
|
| 63 |
+
|
| 64 |
+
The Trinity Large family consists of three checkpoints from the same training run:
|
| 65 |
+
|
| 66 |
+
- **[Trinity-Large-Preview](https://huggingface.co/arcee-ai/Trinity-Large-Preview)**: Lightly post-trained, chat-ready model undergoing active RL
|
| 67 |
+
- **[Trinity-Large-TrueBase](https://huggingface.co/arcee-ai/Trinity-Large-TrueBase)**: 10T-token pre-anneal pretraining checkpoint
|
| 68 |
+
- **[Trinity-Large-Base](https://huggingface.co/arcee-ai/Trinity-Large-Base)**: Full 17T-token pretrained foundation model with mid-training anneals
|
| 69 |
+
|
| 70 |
+
## Architecture
|
| 71 |
+
|
| 72 |
+
| Hyperparameter | Value |
|
| 73 |
+
|:---|:---:|
|
| 74 |
+
| Total parameters | ~398B |
|
| 75 |
+
| Active parameters per token | ~13B |
|
| 76 |
+
| Experts | 256 (1 shared) |
|
| 77 |
+
| Active experts | 4 |
|
| 78 |
+
| Routing strategy | 4-of-256 (1.56% sparsity) |
|
| 79 |
+
| Dense layers | 6 |
|
| 80 |
+
| Pretraining context length | 8,192 |
|
| 81 |
+
| Context length after extension | 512k |
|
| 82 |
+
| Architecture | Sparse MoE (AfmoeForCausalLM) |
|
| 83 |
+
|
| 84 |
+
## Benchmarks
|
| 85 |
+
|
| 86 |
+
| Benchmark | Llama 4 Maverick | Trinity-Large Preview |
|
| 87 |
+
|-----------|------------------|----------------------|
|
| 88 |
+
| MMLU | 85.5 | 87.2 |
|
| 89 |
+
| MMLU-Pro | 80.5 | 75.2 |
|
| 90 |
+
| GPQA-Diamond | 69.8 | 63.3 |
|
| 91 |
+
| AIME 2025 | 19.3 | 24.0 |
|
| 92 |
+
|
| 93 |
+
***
|
| 94 |
+
|
| 95 |
+
## Running with vLLM
|
| 96 |
+
|
| 97 |
+
Requires [vLLM](https://github.com/vllm-project/vllm) >= 0.18.0. Native FP4 compute requires Blackwell GPUs; older GPUs fall back to Marlin weight decompression automatically.
|
| 98 |
+
|
| 99 |
+
### Blackwell GPUs (B200/B300/GB300) — Docker (recommended)
|
| 100 |
+
|
| 101 |
+
```bash
|
| 102 |
+
docker run --runtime nvidia --gpus all -p 8000:8000 \
|
| 103 |
+
-v ~/.cache/huggingface:/root/.cache/huggingface \
|
| 104 |
+
vllm/vllm-openai:v0.18.0-cu130 \
|
| 105 |
+
arcee-ai/Trinity-Large-Preview-NVFP4 \
|
| 106 |
+
--trust-remote-code \
|
| 107 |
+
--tensor-parallel-size 8 \
|
| 108 |
+
--gpu-memory-utilization 0.90 \
|
| 109 |
+
--max-model-len 8192
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
### Hopper GPUs (H100/H200) and others
|
| 113 |
+
|
| 114 |
+
```bash
|
| 115 |
+
vllm serve arcee-ai/Trinity-Large-Preview-NVFP4 \
|
| 116 |
+
--trust-remote-code \
|
| 117 |
+
--tensor-parallel-size 8 \
|
| 118 |
+
--gpu-memory-utilization 0.90 \
|
| 119 |
+
--max-model-len 8192 \
|
| 120 |
+
--host 0.0.0.0 \
|
| 121 |
+
--port 8000
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
**Note (Blackwell pip installs):** If installing vLLM via pip on Blackwell rather than using Docker, native FP4 kernels may produce incorrect output due to package version mismatches. As a workaround, force the Marlin backend:
|
| 125 |
+
|
| 126 |
+
```bash
|
| 127 |
+
export VLLM_NVFP4_GEMM_BACKEND=marlin
|
| 128 |
+
|
| 129 |
+
vllm serve arcee-ai/Trinity-Large-Preview-NVFP4 \
|
| 130 |
+
--trust-remote-code \
|
| 131 |
+
--tensor-parallel-size 8 \
|
| 132 |
+
--moe-backend marlin \
|
| 133 |
+
--gpu-memory-utilization 0.90 \
|
| 134 |
+
--max-model-len 8192 \
|
| 135 |
+
--host 0.0.0.0 \
|
| 136 |
+
--port 8000
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
Marlin decompresses FP4 weights to BF16 for compute, providing the full memory compression benefit but not native FP4 compute speedup. On Hopper GPUs (H100/H200), Marlin is selected automatically and no extra flags are needed.
|
| 140 |
+
|
| 141 |
+
### API
|
| 142 |
+
|
| 143 |
+
Available on OpenRouter:
|
| 144 |
+
|
| 145 |
+
```bash
|
| 146 |
+
curl -X POST "https://openrouter.ai/v1/chat/completions" \
|
| 147 |
+
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
|
| 148 |
+
-H "Content-Type: application/json" \
|
| 149 |
+
-d '{
|
| 150 |
+
"model": "arcee-ai/trinity-large-preview",
|
| 151 |
+
"messages": [
|
| 152 |
+
{
|
| 153 |
+
"role": "user",
|
| 154 |
+
"content": "What are some fun things to do in New York?"
|
| 155 |
+
}
|
| 156 |
+
]
|
| 157 |
+
}'
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
***
|
| 161 |
+
|
| 162 |
+
## License
|
| 163 |
+
|
| 164 |
+
Trinity-Large-Preview-NVFP4 is released under the OpenMDW License, version 1.1 (OpenMDW-1.1).
|
| 165 |
+
|
| 166 |
+
## Citation
|
| 167 |
+
|
| 168 |
+
If you use this model, please cite:
|
| 169 |
+
|
| 170 |
+
```bibtex
|
| 171 |
+
@misc{singh2026arceetrinity,
|
| 172 |
+
title = {Arcee Trinity Large Technical Report},
|
| 173 |
+
author = {Varun Singh and Lucas Krauss and Sami Jaghouar and Matej Sirovatka and Charles Goddard and Fares Obied and Jack Min Ong and Jannik Straube and Fern and Aria Harley and Conner Stewart and Colin Kealty and Maziyar Panahi and Simon Kirsten and Anushka Deshpande and Anneketh Vij and Arthur Bresnu and Pranav Veldurthi and Raghav Ravishankar and Hardik Bishnoi and DatologyAI Team and Arcee AI Team and Prime Intellect Team and Mark McQuade and Johannes Hagemann and Lucas Atkins},
|
| 174 |
+
year = {2026},
|
| 175 |
+
eprint = {2602.17004},
|
| 176 |
+
archivePrefix= {arXiv},
|
| 177 |
+
primaryClass = {cs.LG},
|
| 178 |
+
doi = {10.48550/arXiv.2602.17004},
|
| 179 |
+
url = {https://arxiv.org/abs/2602.17004}
|
| 180 |
+
}
|
| 181 |
+
```
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{{ bos_token }}{%- if tools %}
|
| 2 |
+
{{- '<|im_start|>system\n' }}
|
| 3 |
+
{%- if messages[0].role == 'system' %}
|
| 4 |
+
{{- messages[0].content + '\n\n' }}
|
| 5 |
+
{%- endif %}
|
| 6 |
+
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
|
| 7 |
+
{%- for tool in tools %}
|
| 8 |
+
{{- "\n" }}
|
| 9 |
+
{{- tool | tojson }}
|
| 10 |
+
{%- endfor %}
|
| 11 |
+
{{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
|
| 12 |
+
{%- else %}
|
| 13 |
+
{%- if messages[0].role == 'system' %}
|
| 14 |
+
{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
|
| 15 |
+
{%- endif %}
|
| 16 |
+
{%- endif %}
|
| 17 |
+
{%- for message in messages %}
|
| 18 |
+
{%- if message.content is string %}
|
| 19 |
+
{%- set content = message.content %}
|
| 20 |
+
{%- else %}
|
| 21 |
+
{%- set content = '' %}
|
| 22 |
+
{%- endif %}
|
| 23 |
+
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
|
| 24 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 25 |
+
{%- elif message.role == "assistant" %}
|
| 26 |
+
{{- '<|im_start|>' + message.role + '\n' }}
|
| 27 |
+
{% generation %}
|
| 28 |
+
{{- content}}
|
| 29 |
+
{%- if message.tool_calls %}
|
| 30 |
+
{%- for tool_call in message.tool_calls %}
|
| 31 |
+
{%- if (loop.first and content) or (not loop.first) %}
|
| 32 |
+
{{- '\n' }}
|
| 33 |
+
{%- endif %}
|
| 34 |
+
{%- if tool_call.function %}
|
| 35 |
+
{%- set tool_call = tool_call.function %}
|
| 36 |
+
{%- endif %}
|
| 37 |
+
{{- '<tool_call>\n{"name": "' }}
|
| 38 |
+
{{- tool_call.name }}
|
| 39 |
+
{{- '", "arguments": ' }}
|
| 40 |
+
{%- if tool_call.arguments is string %}
|
| 41 |
+
{{- tool_call.arguments }}
|
| 42 |
+
{%- else %}
|
| 43 |
+
{{- tool_call.arguments | tojson }}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{{- '}\n</tool_call>' }}
|
| 46 |
+
{%- endfor %}
|
| 47 |
+
{%- endif %}
|
| 48 |
+
{{- '<|im_end|>' }}
|
| 49 |
+
{% endgeneration%}
|
| 50 |
+
{{- '\n' }}
|
| 51 |
+
{%- elif message.role == "tool" %}
|
| 52 |
+
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
|
| 53 |
+
{{- '<|im_start|>user' }}
|
| 54 |
+
{%- endif %}
|
| 55 |
+
{{- '\n<tool_response>\n' }}
|
| 56 |
+
{{- content }}
|
| 57 |
+
{{- '\n</tool_response>' }}
|
| 58 |
+
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
|
| 59 |
+
{{- '<|im_end|>\n' }}
|
| 60 |
+
{%- endif %}
|
| 61 |
+
{%- endif %}
|
| 62 |
+
{%- endfor %}
|
| 63 |
+
{%- if add_generation_prompt %}
|
| 64 |
+
{{- '<|im_start|>assistant\n' }}
|
| 65 |
+
{%- endif %}
|
config.json
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"AfmoeForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_dropout": 0.0,
|
| 6 |
+
"auto_map": {
|
| 7 |
+
"AutoConfig": "configuration_afmoe.AfmoeConfig",
|
| 8 |
+
"AutoModel": "modeling_afmoe.AfmoeModel",
|
| 9 |
+
"AutoModelForCausalLM": "modeling_afmoe.AfmoeForCausalLM"
|
| 10 |
+
},
|
| 11 |
+
"dtype": "bfloat16",
|
| 12 |
+
"global_attn_every_n_layers": 4,
|
| 13 |
+
"head_dim": 128,
|
| 14 |
+
"hidden_act": "silu",
|
| 15 |
+
"hidden_size": 3072,
|
| 16 |
+
"initializer_range": 0.02,
|
| 17 |
+
"intermediate_size": 12288,
|
| 18 |
+
"layer_types": [
|
| 19 |
+
"sliding_attention",
|
| 20 |
+
"sliding_attention",
|
| 21 |
+
"sliding_attention",
|
| 22 |
+
"full_attention",
|
| 23 |
+
"sliding_attention",
|
| 24 |
+
"sliding_attention",
|
| 25 |
+
"sliding_attention",
|
| 26 |
+
"full_attention",
|
| 27 |
+
"sliding_attention",
|
| 28 |
+
"sliding_attention",
|
| 29 |
+
"sliding_attention",
|
| 30 |
+
"full_attention",
|
| 31 |
+
"sliding_attention",
|
| 32 |
+
"sliding_attention",
|
| 33 |
+
"sliding_attention",
|
| 34 |
+
"full_attention",
|
| 35 |
+
"sliding_attention",
|
| 36 |
+
"sliding_attention",
|
| 37 |
+
"sliding_attention",
|
| 38 |
+
"full_attention",
|
| 39 |
+
"sliding_attention",
|
| 40 |
+
"sliding_attention",
|
| 41 |
+
"sliding_attention",
|
| 42 |
+
"full_attention",
|
| 43 |
+
"sliding_attention",
|
| 44 |
+
"sliding_attention",
|
| 45 |
+
"sliding_attention",
|
| 46 |
+
"full_attention",
|
| 47 |
+
"sliding_attention",
|
| 48 |
+
"sliding_attention",
|
| 49 |
+
"sliding_attention",
|
| 50 |
+
"full_attention",
|
| 51 |
+
"sliding_attention",
|
| 52 |
+
"sliding_attention",
|
| 53 |
+
"sliding_attention",
|
| 54 |
+
"full_attention",
|
| 55 |
+
"sliding_attention",
|
| 56 |
+
"sliding_attention",
|
| 57 |
+
"sliding_attention",
|
| 58 |
+
"full_attention",
|
| 59 |
+
"sliding_attention",
|
| 60 |
+
"sliding_attention",
|
| 61 |
+
"sliding_attention",
|
| 62 |
+
"full_attention",
|
| 63 |
+
"sliding_attention",
|
| 64 |
+
"sliding_attention",
|
| 65 |
+
"sliding_attention",
|
| 66 |
+
"full_attention",
|
| 67 |
+
"sliding_attention",
|
| 68 |
+
"sliding_attention",
|
| 69 |
+
"sliding_attention",
|
| 70 |
+
"full_attention",
|
| 71 |
+
"sliding_attention",
|
| 72 |
+
"sliding_attention",
|
| 73 |
+
"sliding_attention",
|
| 74 |
+
"full_attention",
|
| 75 |
+
"sliding_attention",
|
| 76 |
+
"sliding_attention",
|
| 77 |
+
"sliding_attention",
|
| 78 |
+
"full_attention"
|
| 79 |
+
],
|
| 80 |
+
"load_balance_coeff": 5e-05,
|
| 81 |
+
"max_position_embeddings": 262144,
|
| 82 |
+
"model_type": "afmoe",
|
| 83 |
+
"moe_intermediate_size": 3072,
|
| 84 |
+
"mup_enabled": true,
|
| 85 |
+
"n_group": 1,
|
| 86 |
+
"num_attention_heads": 48,
|
| 87 |
+
"num_dense_layers": 6,
|
| 88 |
+
"num_expert_groups": 1,
|
| 89 |
+
"num_experts": 256,
|
| 90 |
+
"num_experts_per_tok": 4,
|
| 91 |
+
"num_hidden_layers": 60,
|
| 92 |
+
"num_key_value_heads": 8,
|
| 93 |
+
"num_limited_groups": 1,
|
| 94 |
+
"num_shared_experts": 1,
|
| 95 |
+
"rms_norm_eps": 1e-05,
|
| 96 |
+
"rope_scaling": null,
|
| 97 |
+
"rope_theta": 10000,
|
| 98 |
+
"route_norm": true,
|
| 99 |
+
"route_scale": 2.448,
|
| 100 |
+
"score_func": "sigmoid",
|
| 101 |
+
"sliding_window": 4096,
|
| 102 |
+
"tie_word_embeddings": false,
|
| 103 |
+
"topk_group": 1,
|
| 104 |
+
"transformers_version": "4.57.6",
|
| 105 |
+
"use_cache": true,
|
| 106 |
+
"use_grouped_mm": true,
|
| 107 |
+
"vocab_size": 200192,
|
| 108 |
+
"quantization_config": {
|
| 109 |
+
"config_groups": {
|
| 110 |
+
"group_0": {
|
| 111 |
+
"input_activations": {
|
| 112 |
+
"dynamic": false,
|
| 113 |
+
"num_bits": 4,
|
| 114 |
+
"type": "float",
|
| 115 |
+
"group_size": 16
|
| 116 |
+
},
|
| 117 |
+
"weights": {
|
| 118 |
+
"dynamic": false,
|
| 119 |
+
"num_bits": 4,
|
| 120 |
+
"type": "float",
|
| 121 |
+
"group_size": 16
|
| 122 |
+
},
|
| 123 |
+
"targets": [
|
| 124 |
+
"Linear"
|
| 125 |
+
]
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
"ignore": [
|
| 129 |
+
"lm_head",
|
| 130 |
+
"model.layers.0.self_attn*",
|
| 131 |
+
"model.layers.1.self_attn*",
|
| 132 |
+
"model.layers.10.mlp.router*",
|
| 133 |
+
"model.layers.10.self_attn*",
|
| 134 |
+
"model.layers.11.mlp.router*",
|
| 135 |
+
"model.layers.11.self_attn*",
|
| 136 |
+
"model.layers.12.mlp.router*",
|
| 137 |
+
"model.layers.12.self_attn*",
|
| 138 |
+
"model.layers.13.mlp.router*",
|
| 139 |
+
"model.layers.13.self_attn*",
|
| 140 |
+
"model.layers.14.mlp.router*",
|
| 141 |
+
"model.layers.14.self_attn*",
|
| 142 |
+
"model.layers.15.mlp.router*",
|
| 143 |
+
"model.layers.15.self_attn*",
|
| 144 |
+
"model.layers.16.mlp.router*",
|
| 145 |
+
"model.layers.16.self_attn*",
|
| 146 |
+
"model.layers.17.mlp.router*",
|
| 147 |
+
"model.layers.17.self_attn*",
|
| 148 |
+
"model.layers.18.mlp.router*",
|
| 149 |
+
"model.layers.18.self_attn*",
|
| 150 |
+
"model.layers.19.mlp.router*",
|
| 151 |
+
"model.layers.19.self_attn*",
|
| 152 |
+
"model.layers.2.self_attn*",
|
| 153 |
+
"model.layers.20.mlp.router*",
|
| 154 |
+
"model.layers.20.self_attn*",
|
| 155 |
+
"model.layers.21.mlp.router*",
|
| 156 |
+
"model.layers.21.self_attn*",
|
| 157 |
+
"model.layers.22.mlp.router*",
|
| 158 |
+
"model.layers.22.self_attn*",
|
| 159 |
+
"model.layers.23.mlp.router*",
|
| 160 |
+
"model.layers.23.self_attn*",
|
| 161 |
+
"model.layers.24.mlp.router*",
|
| 162 |
+
"model.layers.24.self_attn*",
|
| 163 |
+
"model.layers.25.mlp.router*",
|
| 164 |
+
"model.layers.25.self_attn*",
|
| 165 |
+
"model.layers.26.mlp.router*",
|
| 166 |
+
"model.layers.26.self_attn*",
|
| 167 |
+
"model.layers.27.mlp.router*",
|
| 168 |
+
"model.layers.27.self_attn*",
|
| 169 |
+
"model.layers.28.mlp.router*",
|
| 170 |
+
"model.layers.28.self_attn*",
|
| 171 |
+
"model.layers.29.mlp.router*",
|
| 172 |
+
"model.layers.29.self_attn*",
|
| 173 |
+
"model.layers.3.self_attn*",
|
| 174 |
+
"model.layers.30.mlp.router*",
|
| 175 |
+
"model.layers.30.self_attn*",
|
| 176 |
+
"model.layers.31.mlp.router*",
|
| 177 |
+
"model.layers.31.self_attn*",
|
| 178 |
+
"model.layers.32.mlp.router*",
|
| 179 |
+
"model.layers.32.self_attn*",
|
| 180 |
+
"model.layers.33.mlp.router*",
|
| 181 |
+
"model.layers.33.self_attn*",
|
| 182 |
+
"model.layers.34.mlp.router*",
|
| 183 |
+
"model.layers.34.self_attn*",
|
| 184 |
+
"model.layers.35.mlp.router*",
|
| 185 |
+
"model.layers.35.self_attn*",
|
| 186 |
+
"model.layers.36.mlp.router*",
|
| 187 |
+
"model.layers.36.self_attn*",
|
| 188 |
+
"model.layers.37.mlp.router*",
|
| 189 |
+
"model.layers.37.self_attn*",
|
| 190 |
+
"model.layers.38.mlp.router*",
|
| 191 |
+
"model.layers.38.self_attn*",
|
| 192 |
+
"model.layers.39.mlp.router*",
|
| 193 |
+
"model.layers.39.self_attn*",
|
| 194 |
+
"model.layers.4.self_attn*",
|
| 195 |
+
"model.layers.40.mlp.router*",
|
| 196 |
+
"model.layers.40.self_attn*",
|
| 197 |
+
"model.layers.41.mlp.router*",
|
| 198 |
+
"model.layers.41.self_attn*",
|
| 199 |
+
"model.layers.42.mlp.router*",
|
| 200 |
+
"model.layers.42.self_attn*",
|
| 201 |
+
"model.layers.43.mlp.router*",
|
| 202 |
+
"model.layers.43.self_attn*",
|
| 203 |
+
"model.layers.44.mlp.router*",
|
| 204 |
+
"model.layers.44.self_attn*",
|
| 205 |
+
"model.layers.45.mlp.router*",
|
| 206 |
+
"model.layers.45.self_attn*",
|
| 207 |
+
"model.layers.46.mlp.router*",
|
| 208 |
+
"model.layers.46.self_attn*",
|
| 209 |
+
"model.layers.47.mlp.router*",
|
| 210 |
+
"model.layers.47.self_attn*",
|
| 211 |
+
"model.layers.48.mlp.router*",
|
| 212 |
+
"model.layers.48.self_attn*",
|
| 213 |
+
"model.layers.49.mlp.router*",
|
| 214 |
+
"model.layers.49.self_attn*",
|
| 215 |
+
"model.layers.5.self_attn*",
|
| 216 |
+
"model.layers.50.mlp.router*",
|
| 217 |
+
"model.layers.50.self_attn*",
|
| 218 |
+
"model.layers.51.mlp.router*",
|
| 219 |
+
"model.layers.51.self_attn*",
|
| 220 |
+
"model.layers.52.mlp.router*",
|
| 221 |
+
"model.layers.52.self_attn*",
|
| 222 |
+
"model.layers.53.mlp.router*",
|
| 223 |
+
"model.layers.53.self_attn*",
|
| 224 |
+
"model.layers.54.mlp.router*",
|
| 225 |
+
"model.layers.54.self_attn*",
|
| 226 |
+
"model.layers.55.mlp.router*",
|
| 227 |
+
"model.layers.55.self_attn*",
|
| 228 |
+
"model.layers.56.mlp.router*",
|
| 229 |
+
"model.layers.56.self_attn*",
|
| 230 |
+
"model.layers.57.mlp.router*",
|
| 231 |
+
"model.layers.57.self_attn*",
|
| 232 |
+
"model.layers.58.mlp.router*",
|
| 233 |
+
"model.layers.58.self_attn*",
|
| 234 |
+
"model.layers.59.mlp.router*",
|
| 235 |
+
"model.layers.59.self_attn*",
|
| 236 |
+
"model.layers.6.mlp.router*",
|
| 237 |
+
"model.layers.6.self_attn*",
|
| 238 |
+
"model.layers.7.mlp.router*",
|
| 239 |
+
"model.layers.7.self_attn*",
|
| 240 |
+
"model.layers.8.mlp.router*",
|
| 241 |
+
"model.layers.8.self_attn*",
|
| 242 |
+
"model.layers.9.mlp.router*",
|
| 243 |
+
"model.layers.9.self_attn*"
|
| 244 |
+
],
|
| 245 |
+
"quant_algo": "NVFP4",
|
| 246 |
+
"producer": {
|
| 247 |
+
"name": "modelopt",
|
| 248 |
+
"version": "0.43.0rc2.dev24+gd69886413.d20260326"
|
| 249 |
+
},
|
| 250 |
+
"quant_method": "modelopt"
|
| 251 |
+
}
|
| 252 |
+
}
|
configuration_afmoe.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 16 |
+
from transformers.modeling_rope_utils import rope_config_validation
|
| 17 |
+
from transformers.configuration_utils import layer_type_validation
|
| 18 |
+
from transformers.utils import logging
|
| 19 |
+
|
| 20 |
+
logger = logging.get_logger(__name__)
|
| 21 |
+
|
| 22 |
+
class AfmoeConfig(PretrainedConfig):
|
| 23 |
+
"""
|
| 24 |
+
n_group (`int`, *optional*, defaults to 1):
|
| 25 |
+
Number of groups for routed experts.
|
| 26 |
+
topk_group (`int`, *optional*, defaults to 1):
|
| 27 |
+
Number of selected groups for each token(for each token, ensuring the selected experts is only within `topk_group` groups).
|
| 28 |
+
"""
|
| 29 |
+
model_type = "afmoe"
|
| 30 |
+
base_model_pp_plan = {
|
| 31 |
+
"embed_tokens": (["input_ids"], ["inputs_embeds"]),
|
| 32 |
+
"layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
|
| 33 |
+
"norm": (["hidden_states"], ["hidden_states"]),
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
def __init__(
|
| 37 |
+
self,
|
| 38 |
+
num_hidden_layers: int = 32,
|
| 39 |
+
vocab_size: int = 200192,
|
| 40 |
+
hidden_size: int = 2048,
|
| 41 |
+
intermediate_size: int = 6144,
|
| 42 |
+
moe_intermediate_size=1408,
|
| 43 |
+
num_dense_layers=1,
|
| 44 |
+
num_attention_heads=16,
|
| 45 |
+
num_key_value_heads=None,
|
| 46 |
+
head_dim=128,
|
| 47 |
+
hidden_act="silu",
|
| 48 |
+
max_position_embeddings=16384,
|
| 49 |
+
initializer_range=0.02,
|
| 50 |
+
rms_norm_eps=1e-5,
|
| 51 |
+
use_cache=True,
|
| 52 |
+
tie_word_embeddings=False,
|
| 53 |
+
rope_theta=10000.0,
|
| 54 |
+
rope_scaling=None,
|
| 55 |
+
num_experts=64,
|
| 56 |
+
num_experts_per_tok=6,
|
| 57 |
+
num_shared_experts=2,
|
| 58 |
+
num_expert_groups=1,
|
| 59 |
+
num_limited_groups=1,
|
| 60 |
+
score_func="sigmoid",
|
| 61 |
+
route_norm=True,
|
| 62 |
+
route_scale=1.0,
|
| 63 |
+
global_attn_every_n_layers=4,
|
| 64 |
+
sliding_window=1024,
|
| 65 |
+
mup_enabled=False,
|
| 66 |
+
layer_types=None,
|
| 67 |
+
attention_dropout: float = 0.0,
|
| 68 |
+
n_group: int = 1,
|
| 69 |
+
topk_group: int = 1,
|
| 70 |
+
**kwargs,
|
| 71 |
+
):
|
| 72 |
+
self.vocab_size = vocab_size
|
| 73 |
+
self.max_position_embeddings = max_position_embeddings
|
| 74 |
+
self.hidden_size = hidden_size
|
| 75 |
+
self.intermediate_size = intermediate_size
|
| 76 |
+
self.num_hidden_layers = num_hidden_layers
|
| 77 |
+
self.num_dense_layers = num_dense_layers
|
| 78 |
+
self.num_attention_heads = num_attention_heads
|
| 79 |
+
self.head_dim = head_dim
|
| 80 |
+
self.hidden_act = hidden_act
|
| 81 |
+
self.initializer_range = initializer_range
|
| 82 |
+
self.rms_norm_eps = rms_norm_eps
|
| 83 |
+
self.use_cache = use_cache
|
| 84 |
+
self.rope_theta = rope_theta
|
| 85 |
+
self.rope_scaling = rope_scaling
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# MoE specific
|
| 89 |
+
self.moe_intermediate_size = moe_intermediate_size
|
| 90 |
+
self.num_experts_per_tok = num_experts_per_tok
|
| 91 |
+
self.n_group = n_group
|
| 92 |
+
self.topk_group = topk_group
|
| 93 |
+
self.num_experts = num_experts
|
| 94 |
+
self.num_shared_experts = num_shared_experts
|
| 95 |
+
self.num_expert_groups = num_expert_groups
|
| 96 |
+
self.num_limited_groups = num_limited_groups
|
| 97 |
+
self.score_func = score_func
|
| 98 |
+
self.route_norm = route_norm
|
| 99 |
+
self.route_scale = route_scale
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# Attention specific
|
| 103 |
+
self.attention_dropout = attention_dropout
|
| 104 |
+
self.global_attn_every_n_layers = global_attn_every_n_layers
|
| 105 |
+
self.sliding_window = sliding_window
|
| 106 |
+
self.layer_types = layer_types
|
| 107 |
+
if self.layer_types is None:
|
| 108 |
+
self.layer_types = [
|
| 109 |
+
"sliding_attention" if bool((i + 1) % global_attn_every_n_layers) else "full_attention" for i in range(self.num_hidden_layers)
|
| 110 |
+
]
|
| 111 |
+
layer_type_validation(self.layer_types)
|
| 112 |
+
|
| 113 |
+
# muP specific
|
| 114 |
+
self.mup_enabled = mup_enabled
|
| 115 |
+
|
| 116 |
+
if num_key_value_heads is None:
|
| 117 |
+
num_key_value_heads = num_attention_heads
|
| 118 |
+
|
| 119 |
+
self.num_key_value_heads = num_key_value_heads
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# Validate rope configs
|
| 123 |
+
if self.rope_scaling is not None and "type" in self.rope_scaling:
|
| 124 |
+
self.rope_scaling["rope_type"] = self.rope_scaling["type"]
|
| 125 |
+
rope_config_validation(self)
|
| 126 |
+
|
| 127 |
+
super().__init__(
|
| 128 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 129 |
+
**kwargs,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
__all__ = ["AfmoeConfig"]
|
generation_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 0,
|
| 4 |
+
"eos_token_id": 3,
|
| 5 |
+
"pad_token_id": 12,
|
| 6 |
+
"transformers_version": "4.57.3",
|
| 7 |
+
"temperature": 0.8,
|
| 8 |
+
"top_p": 0.8
|
| 9 |
+
}
|
hf_quant_config.json
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"producer": {
|
| 3 |
+
"name": "modelopt",
|
| 4 |
+
"version": "0.43.0rc2.dev24+gd69886413.d20260326"
|
| 5 |
+
},
|
| 6 |
+
"quantization": {
|
| 7 |
+
"quant_algo": "NVFP4",
|
| 8 |
+
"kv_cache_quant_algo": null,
|
| 9 |
+
"group_size": 16,
|
| 10 |
+
"exclude_modules": [
|
| 11 |
+
"lm_head",
|
| 12 |
+
"model.layers.0.self_attn*",
|
| 13 |
+
"model.layers.1.self_attn*",
|
| 14 |
+
"model.layers.10.mlp.router*",
|
| 15 |
+
"model.layers.10.self_attn*",
|
| 16 |
+
"model.layers.11.mlp.router*",
|
| 17 |
+
"model.layers.11.self_attn*",
|
| 18 |
+
"model.layers.12.mlp.router*",
|
| 19 |
+
"model.layers.12.self_attn*",
|
| 20 |
+
"model.layers.13.mlp.router*",
|
| 21 |
+
"model.layers.13.self_attn*",
|
| 22 |
+
"model.layers.14.mlp.router*",
|
| 23 |
+
"model.layers.14.self_attn*",
|
| 24 |
+
"model.layers.15.mlp.router*",
|
| 25 |
+
"model.layers.15.self_attn*",
|
| 26 |
+
"model.layers.16.mlp.router*",
|
| 27 |
+
"model.layers.16.self_attn*",
|
| 28 |
+
"model.layers.17.mlp.router*",
|
| 29 |
+
"model.layers.17.self_attn*",
|
| 30 |
+
"model.layers.18.mlp.router*",
|
| 31 |
+
"model.layers.18.self_attn*",
|
| 32 |
+
"model.layers.19.mlp.router*",
|
| 33 |
+
"model.layers.19.self_attn*",
|
| 34 |
+
"model.layers.2.self_attn*",
|
| 35 |
+
"model.layers.20.mlp.router*",
|
| 36 |
+
"model.layers.20.self_attn*",
|
| 37 |
+
"model.layers.21.mlp.router*",
|
| 38 |
+
"model.layers.21.self_attn*",
|
| 39 |
+
"model.layers.22.mlp.router*",
|
| 40 |
+
"model.layers.22.self_attn*",
|
| 41 |
+
"model.layers.23.mlp.router*",
|
| 42 |
+
"model.layers.23.self_attn*",
|
| 43 |
+
"model.layers.24.mlp.router*",
|
| 44 |
+
"model.layers.24.self_attn*",
|
| 45 |
+
"model.layers.25.mlp.router*",
|
| 46 |
+
"model.layers.25.self_attn*",
|
| 47 |
+
"model.layers.26.mlp.router*",
|
| 48 |
+
"model.layers.26.self_attn*",
|
| 49 |
+
"model.layers.27.mlp.router*",
|
| 50 |
+
"model.layers.27.self_attn*",
|
| 51 |
+
"model.layers.28.mlp.router*",
|
| 52 |
+
"model.layers.28.self_attn*",
|
| 53 |
+
"model.layers.29.mlp.router*",
|
| 54 |
+
"model.layers.29.self_attn*",
|
| 55 |
+
"model.layers.3.self_attn*",
|
| 56 |
+
"model.layers.30.mlp.router*",
|
| 57 |
+
"model.layers.30.self_attn*",
|
| 58 |
+
"model.layers.31.mlp.router*",
|
| 59 |
+
"model.layers.31.self_attn*",
|
| 60 |
+
"model.layers.32.mlp.router*",
|
| 61 |
+
"model.layers.32.self_attn*",
|
| 62 |
+
"model.layers.33.mlp.router*",
|
| 63 |
+
"model.layers.33.self_attn*",
|
| 64 |
+
"model.layers.34.mlp.router*",
|
| 65 |
+
"model.layers.34.self_attn*",
|
| 66 |
+
"model.layers.35.mlp.router*",
|
| 67 |
+
"model.layers.35.self_attn*",
|
| 68 |
+
"model.layers.36.mlp.router*",
|
| 69 |
+
"model.layers.36.self_attn*",
|
| 70 |
+
"model.layers.37.mlp.router*",
|
| 71 |
+
"model.layers.37.self_attn*",
|
| 72 |
+
"model.layers.38.mlp.router*",
|
| 73 |
+
"model.layers.38.self_attn*",
|
| 74 |
+
"model.layers.39.mlp.router*",
|
| 75 |
+
"model.layers.39.self_attn*",
|
| 76 |
+
"model.layers.4.self_attn*",
|
| 77 |
+
"model.layers.40.mlp.router*",
|
| 78 |
+
"model.layers.40.self_attn*",
|
| 79 |
+
"model.layers.41.mlp.router*",
|
| 80 |
+
"model.layers.41.self_attn*",
|
| 81 |
+
"model.layers.42.mlp.router*",
|
| 82 |
+
"model.layers.42.self_attn*",
|
| 83 |
+
"model.layers.43.mlp.router*",
|
| 84 |
+
"model.layers.43.self_attn*",
|
| 85 |
+
"model.layers.44.mlp.router*",
|
| 86 |
+
"model.layers.44.self_attn*",
|
| 87 |
+
"model.layers.45.mlp.router*",
|
| 88 |
+
"model.layers.45.self_attn*",
|
| 89 |
+
"model.layers.46.mlp.router*",
|
| 90 |
+
"model.layers.46.self_attn*",
|
| 91 |
+
"model.layers.47.mlp.router*",
|
| 92 |
+
"model.layers.47.self_attn*",
|
| 93 |
+
"model.layers.48.mlp.router*",
|
| 94 |
+
"model.layers.48.self_attn*",
|
| 95 |
+
"model.layers.49.mlp.router*",
|
| 96 |
+
"model.layers.49.self_attn*",
|
| 97 |
+
"model.layers.5.self_attn*",
|
| 98 |
+
"model.layers.50.mlp.router*",
|
| 99 |
+
"model.layers.50.self_attn*",
|
| 100 |
+
"model.layers.51.mlp.router*",
|
| 101 |
+
"model.layers.51.self_attn*",
|
| 102 |
+
"model.layers.52.mlp.router*",
|
| 103 |
+
"model.layers.52.self_attn*",
|
| 104 |
+
"model.layers.53.mlp.router*",
|
| 105 |
+
"model.layers.53.self_attn*",
|
| 106 |
+
"model.layers.54.mlp.router*",
|
| 107 |
+
"model.layers.54.self_attn*",
|
| 108 |
+
"model.layers.55.mlp.router*",
|
| 109 |
+
"model.layers.55.self_attn*",
|
| 110 |
+
"model.layers.56.mlp.router*",
|
| 111 |
+
"model.layers.56.self_attn*",
|
| 112 |
+
"model.layers.57.mlp.router*",
|
| 113 |
+
"model.layers.57.self_attn*",
|
| 114 |
+
"model.layers.58.mlp.router*",
|
| 115 |
+
"model.layers.58.self_attn*",
|
| 116 |
+
"model.layers.59.mlp.router*",
|
| 117 |
+
"model.layers.59.self_attn*",
|
| 118 |
+
"model.layers.6.mlp.router*",
|
| 119 |
+
"model.layers.6.self_attn*",
|
| 120 |
+
"model.layers.7.mlp.router*",
|
| 121 |
+
"model.layers.7.self_attn*",
|
| 122 |
+
"model.layers.8.mlp.router*",
|
| 123 |
+
"model.layers.8.self_attn*",
|
| 124 |
+
"model.layers.9.mlp.router*",
|
| 125 |
+
"model.layers.9.self_attn*"
|
| 126 |
+
]
|
| 127 |
+
}
|
| 128 |
+
}
|
model-00001-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b6ffb224fe53cb4c3824b0066b47ee6b1d18fa39726b25150f9bb3619e491416
|
| 3 |
+
size 9996247136
|
model-00002-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0370cf86c5a73288d83f15ad5ad2ed2f2a1978008f52b2c37f3c9b9df348654c
|
| 3 |
+
size 9996705688
|
model-00003-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f72209ccd2afa2f1cdbdd0cc0e2e2f7702e74881e48ce4566b432b1ea03518a1
|
| 3 |
+
size 9996695040
|
model-00004-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8d6836a8924458a09754bef7cff5083f8841ba0c6edfe92a00e1df11eda1e313
|
| 3 |
+
size 9996695776
|
model-00005-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:986f661fac054447dd8a64c41a1f1346eba3fc07d068f13c2c6a0da3f9b92808
|
| 3 |
+
size 9996711832
|
model-00006-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dc5ee7f52b7981134e1a1d8fa7605b150c2d6de469ef1937f02bb2150f44cb05
|
| 3 |
+
size 9996695376
|
model-00007-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1029c19b27f299d1aea9cc938350c42ca7371917efc828cfb257f08f4479dd18
|
| 3 |
+
size 9967394608
|
model-00008-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:297c619c4060b6096335bbc7209885e8b5862ff74811d89b771889d7a76b422f
|
| 3 |
+
size 9999468752
|
model-00009-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6f8cde0554422b28bc9166c34a6112f4246a5d34de57251085ae518eae7f2d3a
|
| 3 |
+
size 9996695680
|
model-00010-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:11450a2c0c910515debb12df1c6ec81cae7279665769877338c357f27a46b26c
|
| 3 |
+
size 9996712136
|
model-00011-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b32acec188b6af22a176d74a280e39221458b3340127bca116ac50a757760f02
|
| 3 |
+
size 9996694848
|
model-00012-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fa347cf750b9c59496fa340b5fe0c7c746d34315a9e959d02d3764740da429e3
|
| 3 |
+
size 9996695776
|
model-00013-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5dfd4e53f0f5aac73208164081186a781584586bc9ccee2e7e3c44b81d4b3d57
|
| 3 |
+
size 9996711840
|
model-00014-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f1acc4902db943191bc60b4444183577b3366760998d696f05c2e87c879468be
|
| 3 |
+
size 9996695184
|
model-00015-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3a4eb8bdd2de4d94e3658d68fd058152027d0558c0ef9bf0ec5a3b68c406905b
|
| 3 |
+
size 9996695880
|
model-00016-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1af30e9f6b3ef3811e6bba2bb7773e9a6e971dd267b299739c293ab656629013
|
| 3 |
+
size 9996711840
|
model-00017-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9bb3ffae19a4a79c0e91c25a83fa4207202df340f38239d38e6da9c2bfc8d97c
|
| 3 |
+
size 9996695520
|
model-00018-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7bfde4207356ad39ac6fa614a10e12a26a19404cdbd62ca8cb550fb36be018af
|
| 3 |
+
size 9996712240
|
model-00019-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05a18238318beaab222649402165e09a4a2b5b192381f88f37b29fbe24647fe0
|
| 3 |
+
size 9996694672
|
model-00020-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:34b46c5f8c726557400d704cc28e46912ffecd889fcb3a896196e78e8e10527a
|
| 3 |
+
size 9996695776
|
model-00021-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fb6564440c0c0a8ac8528058e9a9aa85ad2f6f4d8070d712da1a7cdf3fd060c3
|
| 3 |
+
size 9996711952
|
model-00022-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:11516b0e7f74a84f1936b5b9d140e33a3577656f8d44493f45c91d22127787ce
|
| 3 |
+
size 9996695024
|
model-00023-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:09b14da4b063738cd601b3f1e3d7f954f178dcef3b3fc3ad9336b53202b991bf
|
| 3 |
+
size 9996695776
|
model-00024-of-00024.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9f5366f944d4797d51a74b144d5ebb1252427f3ef1a14cd04ee61003a93575eb
|
| 3 |
+
size 1612226784
|
model.safetensors.index.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bfd62d9d18246680b8819d456994af8bf5a35bcdfde935ffdc6a8b8f29eb1167
|
| 3 |
+
size 15989867
|
modeling_afmoe.py
ADDED
|
@@ -0,0 +1,680 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Callable, Optional, Tuple, Union
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from torch import nn
|
| 6 |
+
|
| 7 |
+
from transformers.activations import ACT2FN
|
| 8 |
+
from transformers.generation import GenerationMixin
|
| 9 |
+
from transformers.modeling_outputs import (
|
| 10 |
+
MoeCausalLMOutputWithPast,
|
| 11 |
+
MoeModelOutputWithPast,
|
| 12 |
+
)
|
| 13 |
+
from transformers.modeling_utils import PreTrainedModel, ALL_ATTENTION_FUNCTIONS
|
| 14 |
+
from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update
|
| 15 |
+
from transformers.masking_utils import (
|
| 16 |
+
create_causal_mask,
|
| 17 |
+
create_sliding_window_causal_mask,
|
| 18 |
+
)
|
| 19 |
+
from transformers.modeling_layers import GradientCheckpointingLayer
|
| 20 |
+
from transformers.processing_utils import Unpack
|
| 21 |
+
from transformers.utils import TransformersKwargs
|
| 22 |
+
from transformers.cache_utils import Cache, DynamicCache
|
| 23 |
+
from transformers.integrations import use_kernel_forward_from_hub
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
from .configuration_afmoe import AfmoeConfig
|
| 28 |
+
except:
|
| 29 |
+
from configuration_afmoe import AfmoeConfig
|
| 30 |
+
|
| 31 |
+
class AfmoeRotaryEmbedding(nn.Module):
|
| 32 |
+
|
| 33 |
+
def __init__(self, config: AfmoeConfig, device=None):
|
| 34 |
+
super().__init__()
|
| 35 |
+
# BC: "rope_type" was originally "type"
|
| 36 |
+
if hasattr(config, "rope_scaling") and config.rope_scaling is not None:
|
| 37 |
+
self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))
|
| 38 |
+
else:
|
| 39 |
+
self.rope_type = "default"
|
| 40 |
+
self.max_seq_len_cached = config.max_position_embeddings
|
| 41 |
+
self.original_max_seq_len = config.max_position_embeddings
|
| 42 |
+
|
| 43 |
+
self.config = config
|
| 44 |
+
self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
|
| 45 |
+
|
| 46 |
+
inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)
|
| 47 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 48 |
+
self.original_inv_freq = self.inv_freq
|
| 49 |
+
|
| 50 |
+
def _dynamic_frequency_update(self, position_ids, device):
|
| 51 |
+
"""
|
| 52 |
+
dynamic RoPE layers should recompute `inv_freq` in the following situations:
|
| 53 |
+
1 - growing beyond the cached sequence length (allow scaling)
|
| 54 |
+
2 - the current sequence length is in the original scale (avoid losing precision with small sequences)
|
| 55 |
+
"""
|
| 56 |
+
seq_len = torch.max(position_ids) + 1
|
| 57 |
+
if seq_len > self.max_seq_len_cached: # growth
|
| 58 |
+
inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device, seq_len=seq_len)
|
| 59 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False) # TODO joao: may break with compilation
|
| 60 |
+
self.max_seq_len_cached = seq_len
|
| 61 |
+
|
| 62 |
+
if seq_len < self.original_max_seq_len and self.max_seq_len_cached > self.original_max_seq_len: # reset
|
| 63 |
+
# This .to() is needed if the model has been moved to a device after being initialized (because
|
| 64 |
+
# the buffer is automatically moved, but not the original copy)
|
| 65 |
+
self.original_inv_freq = self.original_inv_freq.to(device)
|
| 66 |
+
self.register_buffer("inv_freq", self.original_inv_freq, persistent=False)
|
| 67 |
+
self.max_seq_len_cached = self.original_max_seq_len
|
| 68 |
+
|
| 69 |
+
@torch.no_grad()
|
| 70 |
+
def forward(self, x, position_ids):
|
| 71 |
+
if "dynamic" in self.rope_type:
|
| 72 |
+
self._dynamic_frequency_update(position_ids, device=x.device)
|
| 73 |
+
|
| 74 |
+
# Core RoPE block
|
| 75 |
+
inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1)
|
| 76 |
+
position_ids_expanded = position_ids[:, None, :].float()
|
| 77 |
+
# Force float32 (see https://github.com/huggingface/transformers/pull/29285)
|
| 78 |
+
device_type = x.device.type
|
| 79 |
+
device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu"
|
| 80 |
+
with torch.autocast(device_type=device_type, enabled=False):
|
| 81 |
+
freqs = (inv_freq_expanded.float().to(x.device) @ position_ids_expanded.float()).transpose(1, 2)
|
| 82 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
| 83 |
+
cos = emb.cos()
|
| 84 |
+
sin = emb.sin()
|
| 85 |
+
|
| 86 |
+
# Advanced RoPE types (e.g. yarn) apply a post-processing scaling factor, equivalent to scaling attention
|
| 87 |
+
cos = cos * self.attention_scaling
|
| 88 |
+
sin = sin * self.attention_scaling
|
| 89 |
+
|
| 90 |
+
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def rotate_half(x):
|
| 94 |
+
"""Rotates half the hidden dims of the input."""
|
| 95 |
+
x1 = x[..., : x.shape[-1] // 2]
|
| 96 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
| 97 |
+
return torch.cat((-x2, x1), dim=-1)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
|
| 101 |
+
"""Applies Rotary Position Embedding to the query and key tensors.
|
| 102 |
+
|
| 103 |
+
Args:
|
| 104 |
+
q (`torch.Tensor`): The query tensor.
|
| 105 |
+
k (`torch.Tensor`): The key tensor.
|
| 106 |
+
cos (`torch.Tensor`): The cosine part of the rotary embedding.
|
| 107 |
+
sin (`torch.Tensor`): The sine part of the rotary embedding.
|
| 108 |
+
position_ids (`torch.Tensor`, *optional*):
|
| 109 |
+
Deprecated and unused.
|
| 110 |
+
unsqueeze_dim (`int`, *optional*, defaults to 1):
|
| 111 |
+
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
|
| 112 |
+
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
|
| 113 |
+
that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
|
| 114 |
+
k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
|
| 115 |
+
cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
|
| 116 |
+
the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
|
| 117 |
+
Returns:
|
| 118 |
+
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
|
| 119 |
+
"""
|
| 120 |
+
cos = cos.unsqueeze(unsqueeze_dim)
|
| 121 |
+
sin = sin.unsqueeze(unsqueeze_dim)
|
| 122 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
| 123 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
| 124 |
+
return q_embed, k_embed
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
| 128 |
+
"""
|
| 129 |
+
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
| 130 |
+
num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
|
| 131 |
+
"""
|
| 132 |
+
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
|
| 133 |
+
if n_rep == 1:
|
| 134 |
+
return hidden_states
|
| 135 |
+
hidden_states = hidden_states[:, :, None, :, :].expand(
|
| 136 |
+
batch, num_key_value_heads, n_rep, slen, head_dim
|
| 137 |
+
)
|
| 138 |
+
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
| 139 |
+
|
| 140 |
+
@use_kernel_forward_from_hub("RMSNorm")
|
| 141 |
+
class AfmoeRMSNorm(nn.Module):
|
| 142 |
+
def __init__(self, hidden_size: int, eps: float):
|
| 143 |
+
"""
|
| 144 |
+
AfmoeRMSNorm is equivalent to T5LayerNorm
|
| 145 |
+
"""
|
| 146 |
+
super().__init__()
|
| 147 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
| 148 |
+
self.variance_epsilon = eps
|
| 149 |
+
|
| 150 |
+
def forward(self, hidden_states):
|
| 151 |
+
input_dtype = hidden_states.dtype
|
| 152 |
+
hidden_states = hidden_states.to(torch.float32)
|
| 153 |
+
variance = hidden_states.pow(2).mean(-1, keepdim=True)
|
| 154 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
| 155 |
+
return self.weight * hidden_states.to(input_dtype)
|
| 156 |
+
|
| 157 |
+
def extra_repr(self):
|
| 158 |
+
return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}"
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def eager_attention_forward(
|
| 163 |
+
module: nn.Module,
|
| 164 |
+
query: torch.Tensor,
|
| 165 |
+
key: torch.Tensor,
|
| 166 |
+
value: torch.Tensor,
|
| 167 |
+
attention_mask: Optional[torch.Tensor],
|
| 168 |
+
scaling: float,
|
| 169 |
+
dropout: float = 0.0,
|
| 170 |
+
**kwargs,
|
| 171 |
+
):
|
| 172 |
+
key_states = repeat_kv(key, module.num_key_value_groups)
|
| 173 |
+
value_states = repeat_kv(value, module.num_key_value_groups)
|
| 174 |
+
|
| 175 |
+
attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling
|
| 176 |
+
if attention_mask is not None:
|
| 177 |
+
causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
|
| 178 |
+
attn_weights = attn_weights + causal_mask
|
| 179 |
+
|
| 180 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(
|
| 181 |
+
query.dtype
|
| 182 |
+
)
|
| 183 |
+
attn_weights = nn.functional.dropout(
|
| 184 |
+
attn_weights, p=dropout, training=module.training
|
| 185 |
+
)
|
| 186 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
| 187 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
| 188 |
+
|
| 189 |
+
return attn_output, attn_weights
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
class AfmoeMLP(nn.Module):
|
| 193 |
+
def __init__(self, config, intermediate_size=None):
|
| 194 |
+
super().__init__()
|
| 195 |
+
self.config = config
|
| 196 |
+
self.hidden_size = config.hidden_size
|
| 197 |
+
self.intermediate_size = intermediate_size or config.intermediate_size
|
| 198 |
+
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 199 |
+
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 200 |
+
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
|
| 201 |
+
self.act_fn = ACT2FN[config.hidden_act]
|
| 202 |
+
|
| 203 |
+
def forward(self, x):
|
| 204 |
+
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
class AfmoeTokenChoiceRouter(nn.Module):
|
| 208 |
+
"""Token-choice top-K router for MoE routing."""
|
| 209 |
+
|
| 210 |
+
def __init__(self, config):
|
| 211 |
+
super().__init__()
|
| 212 |
+
self.config = config
|
| 213 |
+
self.top_k = config.num_experts_per_tok
|
| 214 |
+
self.num_experts = config.num_experts
|
| 215 |
+
self.score_func = config.score_func
|
| 216 |
+
self.route_norm = config.route_norm
|
| 217 |
+
self.route_scale = config.route_scale
|
| 218 |
+
self.gate = nn.Linear(config.hidden_size, config.num_experts, bias=False)
|
| 219 |
+
|
| 220 |
+
def forward(self, hidden_states, expert_bias: torch.Tensor | None):
|
| 221 |
+
_, _, hidden_dim = hidden_states.shape
|
| 222 |
+
hidden_states = hidden_states.view(-1, hidden_dim)
|
| 223 |
+
|
| 224 |
+
scores = self.gate(hidden_states)
|
| 225 |
+
|
| 226 |
+
# Apply scoring function in float32 for stability
|
| 227 |
+
if self.score_func == "sigmoid":
|
| 228 |
+
scores = torch.sigmoid(scores.to(torch.float32))
|
| 229 |
+
else:
|
| 230 |
+
scores = F.softmax(scores.to(torch.float32), dim=-1)
|
| 231 |
+
|
| 232 |
+
if expert_bias is not None:
|
| 233 |
+
_, selected_experts = torch.topk(scores + expert_bias, k=self.top_k, dim=1)
|
| 234 |
+
top_scores = scores.gather(dim=1, index=selected_experts)
|
| 235 |
+
else:
|
| 236 |
+
top_scores, selected_experts = torch.topk(scores, k=self.top_k, dim=1)
|
| 237 |
+
|
| 238 |
+
# Normalize weights if using sigmoid
|
| 239 |
+
if self.score_func == "sigmoid" and self.route_norm:
|
| 240 |
+
denominator = top_scores.sum(dim=-1, keepdim=True) + 1e-20
|
| 241 |
+
top_scores = top_scores / denominator
|
| 242 |
+
|
| 243 |
+
top_scores = top_scores * self.route_scale
|
| 244 |
+
return top_scores, selected_experts
|
| 245 |
+
|
| 246 |
+
class AfmoeMoE(nn.Module):
|
| 247 |
+
def __init__(self, config):
|
| 248 |
+
super().__init__()
|
| 249 |
+
self.config = config
|
| 250 |
+
self.router = AfmoeTokenChoiceRouter(config)
|
| 251 |
+
|
| 252 |
+
self.shared_experts = None
|
| 253 |
+
if config.num_shared_experts > 0:
|
| 254 |
+
self.shared_experts = AfmoeMLP(
|
| 255 |
+
config, config.moe_intermediate_size * config.num_shared_experts
|
| 256 |
+
)
|
| 257 |
+
self.experts = nn.ModuleList(
|
| 258 |
+
[AfmoeMLP(
|
| 259 |
+
config, intermediate_size=config.moe_intermediate_size
|
| 260 |
+
) for _ in range(config.num_experts)]
|
| 261 |
+
)
|
| 262 |
+
self.expert_bias = nn.Parameter(torch.zeros(config.num_experts, dtype=torch.float32), requires_grad=False)
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
def forward(self, hidden_states):
|
| 266 |
+
batch_size, seq_len, hidden_dim = hidden_states.shape
|
| 267 |
+
hidden_states_flat = hidden_states.view(-1, hidden_dim)
|
| 268 |
+
|
| 269 |
+
# Get routing decisions
|
| 270 |
+
top_scores, selected_experts = self.router(hidden_states, self.expert_bias)
|
| 271 |
+
|
| 272 |
+
# Process through shared experts
|
| 273 |
+
if self.shared_experts is not None:
|
| 274 |
+
shared_output = self.shared_experts(hidden_states_flat)
|
| 275 |
+
else:
|
| 276 |
+
shared_output = torch.zeros_like(hidden_states_flat)
|
| 277 |
+
|
| 278 |
+
# Reorder tokens by expert for efficient processing
|
| 279 |
+
token_indices_sorted = torch.argsort(selected_experts.view(-1), stable=True)
|
| 280 |
+
top_scores_sorted = top_scores.view(-1)[token_indices_sorted]
|
| 281 |
+
token_to_expert = selected_experts.view(-1)[token_indices_sorted]
|
| 282 |
+
token_indices_sorted = token_indices_sorted // self.config.num_experts_per_tok
|
| 283 |
+
|
| 284 |
+
# Gather input tokens
|
| 285 |
+
token_indices_expanded = token_indices_sorted.unsqueeze(-1).expand(
|
| 286 |
+
-1, hidden_dim
|
| 287 |
+
)
|
| 288 |
+
routed_input = torch.gather(
|
| 289 |
+
hidden_states_flat, dim=0, index=token_indices_expanded
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
routed_output = torch.zeros_like(routed_input)
|
| 293 |
+
for expert_id in range(self.config.num_experts):
|
| 294 |
+
mask = token_to_expert == expert_id
|
| 295 |
+
if mask.any():
|
| 296 |
+
expert_input = routed_input[mask]
|
| 297 |
+
expert_out = self.experts[expert_id](expert_input)
|
| 298 |
+
routed_output[mask] = expert_out
|
| 299 |
+
|
| 300 |
+
routed_output = (
|
| 301 |
+
routed_output.to(torch.float32) * top_scores_sorted.unsqueeze(-1)
|
| 302 |
+
).to(hidden_states.dtype)
|
| 303 |
+
|
| 304 |
+
# Scatter back to original positions
|
| 305 |
+
output = shared_output.scatter_add(
|
| 306 |
+
dim=0, index=token_indices_expanded, src=routed_output
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
return output.view(batch_size, seq_len, hidden_dim)
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
class AfmoeAttention(nn.Module):
|
| 313 |
+
"""Multi-headed attention with local/global pattern and gating."""
|
| 314 |
+
|
| 315 |
+
def __init__(self, config: AfmoeConfig, layer_idx: int):
|
| 316 |
+
super().__init__()
|
| 317 |
+
self.config = config
|
| 318 |
+
self.layer_idx = layer_idx
|
| 319 |
+
self.head_dim = getattr(config, "head_dim", config.hidden_size // config.num_attention_heads)
|
| 320 |
+
self.num_heads = config.num_attention_heads
|
| 321 |
+
self.num_key_value_heads = config.num_key_value_heads
|
| 322 |
+
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
| 323 |
+
|
| 324 |
+
self.scaling = self.head_dim**-0.5
|
| 325 |
+
self.attention_dropout = config.attention_dropout
|
| 326 |
+
self.is_local_attention = config.layer_types[layer_idx] == "sliding_attention"
|
| 327 |
+
self.sliding_window = config.sliding_window if self.is_local_attention else None
|
| 328 |
+
|
| 329 |
+
self.q_proj = nn.Linear(
|
| 330 |
+
config.hidden_size, self.num_heads * self.head_dim, bias=False
|
| 331 |
+
)
|
| 332 |
+
self.k_proj = nn.Linear(
|
| 333 |
+
config.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
|
| 334 |
+
)
|
| 335 |
+
self.v_proj = nn.Linear(
|
| 336 |
+
config.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
|
| 337 |
+
)
|
| 338 |
+
self.o_proj = nn.Linear(
|
| 339 |
+
self.num_heads * self.head_dim, config.hidden_size, bias=False
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
self.q_norm = AfmoeRMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
| 343 |
+
self.k_norm = AfmoeRMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
| 344 |
+
|
| 345 |
+
self.gate_proj = nn.Linear(
|
| 346 |
+
config.hidden_size, self.num_heads * self.head_dim, bias=False
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
def forward(
|
| 350 |
+
self,
|
| 351 |
+
hidden_states: torch.Tensor,
|
| 352 |
+
position_embeddings: tuple[torch.Tensor, torch.Tensor],
|
| 353 |
+
attention_mask: Optional[torch.Tensor],
|
| 354 |
+
past_key_value: Optional[Cache] = None,
|
| 355 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 356 |
+
**kwargs: Unpack[TransformersKwargs],
|
| 357 |
+
) -> torch.Tensor:
|
| 358 |
+
|
| 359 |
+
input_shape = hidden_states.shape[:-1]
|
| 360 |
+
hidden_shape = (*input_shape, -1, self.head_dim)
|
| 361 |
+
|
| 362 |
+
query_states = self.q_proj(hidden_states).view(hidden_shape)
|
| 363 |
+
key_states = self.k_proj(hidden_states).view(hidden_shape)
|
| 364 |
+
value_states = self.v_proj(hidden_states).view(hidden_shape)
|
| 365 |
+
gate_states = self.gate_proj(hidden_states)
|
| 366 |
+
|
| 367 |
+
query_states = self.q_norm(query_states)
|
| 368 |
+
key_states = self.k_norm(key_states)
|
| 369 |
+
|
| 370 |
+
query_states = query_states.transpose(1, 2)
|
| 371 |
+
key_states = key_states.transpose(1, 2)
|
| 372 |
+
value_states = value_states.transpose(1, 2)
|
| 373 |
+
|
| 374 |
+
if self.is_local_attention:
|
| 375 |
+
cos, sin = position_embeddings
|
| 376 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
|
| 377 |
+
|
| 378 |
+
if past_key_value is not None:
|
| 379 |
+
cache_kwargs = {"cache_position": cache_position}
|
| 380 |
+
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
| 381 |
+
|
| 382 |
+
attention_interface: Callable = eager_attention_forward
|
| 383 |
+
if self.config._attn_implementation != "eager":
|
| 384 |
+
attention_interface = ALL_ATTENTION_FUNCTIONS[
|
| 385 |
+
self.config._attn_implementation
|
| 386 |
+
]
|
| 387 |
+
|
| 388 |
+
output, _ = attention_interface(
|
| 389 |
+
self,
|
| 390 |
+
query_states,
|
| 391 |
+
key_states,
|
| 392 |
+
value_states,
|
| 393 |
+
attention_mask=attention_mask,
|
| 394 |
+
dropout=0.0 if not self.training else self.attention_dropout,
|
| 395 |
+
scaling=self.scaling,
|
| 396 |
+
sliding_window=self.sliding_window,
|
| 397 |
+
**kwargs,
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
output = output.view(*input_shape, -1).contiguous()
|
| 401 |
+
output = output * F.sigmoid(gate_states)
|
| 402 |
+
return self.o_proj(output)
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
class AfmoeDecoderLayer(GradientCheckpointingLayer):
|
| 406 |
+
def __init__(self, config: AfmoeConfig, layer_idx: int):
|
| 407 |
+
super().__init__()
|
| 408 |
+
self.hidden_size = config.hidden_size
|
| 409 |
+
self.layer_idx = layer_idx
|
| 410 |
+
|
| 411 |
+
self.self_attn = AfmoeAttention(config=config, layer_idx=layer_idx)
|
| 412 |
+
self.attention_type = config.layer_types[layer_idx]
|
| 413 |
+
|
| 414 |
+
# Dual normalization for attention
|
| 415 |
+
self.input_layernorm = AfmoeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 416 |
+
self.post_attention_layernorm = AfmoeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 417 |
+
|
| 418 |
+
# Dual normalization for FFN
|
| 419 |
+
self.pre_mlp_layernorm = AfmoeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 420 |
+
self.post_mlp_layernorm = AfmoeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 421 |
+
|
| 422 |
+
# MoE or dense FFN
|
| 423 |
+
self.moe_enabled = layer_idx >= config.num_dense_layers
|
| 424 |
+
if self.moe_enabled:
|
| 425 |
+
self.mlp = AfmoeMoE(config)
|
| 426 |
+
else:
|
| 427 |
+
self.mlp = AfmoeMLP(config)
|
| 428 |
+
|
| 429 |
+
def forward(
|
| 430 |
+
self,
|
| 431 |
+
hidden_states: torch.Tensor,
|
| 432 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 433 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 434 |
+
past_key_value: Optional[Cache] = None,
|
| 435 |
+
use_cache: Optional[bool] = None,
|
| 436 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 437 |
+
position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None,
|
| 438 |
+
**kwargs: Unpack[TransformersKwargs],
|
| 439 |
+
) -> torch.FloatTensor:
|
| 440 |
+
residual = hidden_states
|
| 441 |
+
|
| 442 |
+
# Self Attention with dual normalization
|
| 443 |
+
hidden_states = self.input_layernorm(hidden_states)
|
| 444 |
+
hidden_states = self.self_attn(
|
| 445 |
+
hidden_states=hidden_states,
|
| 446 |
+
attention_mask=attention_mask,
|
| 447 |
+
position_ids=position_ids,
|
| 448 |
+
past_key_value=past_key_value,
|
| 449 |
+
use_cache=use_cache,
|
| 450 |
+
cache_position=cache_position,
|
| 451 |
+
position_embeddings=position_embeddings,
|
| 452 |
+
**kwargs,
|
| 453 |
+
)
|
| 454 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
| 455 |
+
hidden_states = residual + hidden_states
|
| 456 |
+
|
| 457 |
+
# FFN with dual normalization
|
| 458 |
+
residual = hidden_states
|
| 459 |
+
hidden_states = self.pre_mlp_layernorm(hidden_states)
|
| 460 |
+
|
| 461 |
+
if self.moe_enabled:
|
| 462 |
+
hidden_states = self.mlp(hidden_states)
|
| 463 |
+
else:
|
| 464 |
+
hidden_states = self.mlp(hidden_states)
|
| 465 |
+
|
| 466 |
+
hidden_states = self.post_mlp_layernorm(hidden_states)
|
| 467 |
+
hidden_states = residual + hidden_states
|
| 468 |
+
return hidden_states
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
class AfmoePreTrainedModel(PreTrainedModel):
|
| 472 |
+
config_class = AfmoeConfig
|
| 473 |
+
base_model_prefix = "model"
|
| 474 |
+
_no_split_modules = ["AfmoeDecoderLayer"]
|
| 475 |
+
_skip_keys_device_placement = ["past_key_values"]
|
| 476 |
+
_keep_in_fp32_modules = [
|
| 477 |
+
"input_layernorm",
|
| 478 |
+
"post_attention_layernorm",
|
| 479 |
+
"pre_mlp_layernorm",
|
| 480 |
+
"post_mlp_layernorm",
|
| 481 |
+
"q_norm",
|
| 482 |
+
"k_norm",
|
| 483 |
+
"norm",
|
| 484 |
+
]
|
| 485 |
+
_supports_sdpa = True
|
| 486 |
+
_supports_attention_backend = True
|
| 487 |
+
supports_gradient_checkpointing = True
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
class AfmoeModel(AfmoePreTrainedModel):
|
| 491 |
+
_no_split_modules = ["AfmoeDecoderLayer"]
|
| 492 |
+
|
| 493 |
+
def __init__(self, config: AfmoeConfig):
|
| 494 |
+
super().__init__(config)
|
| 495 |
+
self.padding_idx = config.pad_token_id
|
| 496 |
+
self.vocab_size = config.vocab_size
|
| 497 |
+
|
| 498 |
+
self.embed_tokens = nn.Embedding(
|
| 499 |
+
config.vocab_size, config.hidden_size, self.padding_idx
|
| 500 |
+
)
|
| 501 |
+
self.layers = nn.ModuleList(
|
| 502 |
+
[
|
| 503 |
+
AfmoeDecoderLayer(config, layer_idx)
|
| 504 |
+
for layer_idx in range(config.num_hidden_layers)
|
| 505 |
+
]
|
| 506 |
+
)
|
| 507 |
+
self.norm = AfmoeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 508 |
+
self.rotary_emb = AfmoeRotaryEmbedding(config=config)
|
| 509 |
+
self.gradient_checkpointing = False
|
| 510 |
+
|
| 511 |
+
self.post_init()
|
| 512 |
+
|
| 513 |
+
def get_input_embeddings(self):
|
| 514 |
+
return self.embed_tokens
|
| 515 |
+
|
| 516 |
+
def set_input_embeddings(self, value):
|
| 517 |
+
self.embed_tokens = value
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
def forward(
|
| 521 |
+
self,
|
| 522 |
+
input_ids: torch.LongTensor,
|
| 523 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 524 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 525 |
+
past_key_values: Optional[list[torch.FloatTensor]] = None,
|
| 526 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 527 |
+
use_cache: Optional[bool] = None,
|
| 528 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 529 |
+
**kwargs: Unpack[TransformersKwargs],
|
| 530 |
+
) -> MoeModelOutputWithPast:
|
| 531 |
+
if (input_ids is None) ^ (inputs_embeds is not None):
|
| 532 |
+
raise ValueError(
|
| 533 |
+
"You must specify exactly one of input_ids or inputs_embeds"
|
| 534 |
+
)
|
| 535 |
+
|
| 536 |
+
if use_cache and past_key_values is None:
|
| 537 |
+
past_key_values = DynamicCache()
|
| 538 |
+
|
| 539 |
+
if inputs_embeds is None:
|
| 540 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
| 541 |
+
|
| 542 |
+
if cache_position is None:
|
| 543 |
+
past_seen_tokens = (
|
| 544 |
+
past_key_values.get_seq_length() if past_key_values is not None else 0
|
| 545 |
+
)
|
| 546 |
+
cache_position = torch.arange(
|
| 547 |
+
past_seen_tokens,
|
| 548 |
+
past_seen_tokens + inputs_embeds.shape[1],
|
| 549 |
+
device=inputs_embeds.device,
|
| 550 |
+
)
|
| 551 |
+
if position_ids is None:
|
| 552 |
+
position_ids = cache_position.unsqueeze(0)
|
| 553 |
+
|
| 554 |
+
# It may already have been prepared by e.g. `generate`
|
| 555 |
+
if not isinstance(causal_mask_mapping := attention_mask, dict):
|
| 556 |
+
mask_kwargs = {
|
| 557 |
+
"config": self.config,
|
| 558 |
+
"input_embeds": inputs_embeds,
|
| 559 |
+
"attention_mask": attention_mask,
|
| 560 |
+
"cache_position": cache_position,
|
| 561 |
+
"past_key_values": past_key_values,
|
| 562 |
+
}
|
| 563 |
+
causal_mask_mapping = {
|
| 564 |
+
"full_attention": create_causal_mask(**mask_kwargs),
|
| 565 |
+
"sliding_attention": create_sliding_window_causal_mask(**mask_kwargs),
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
hidden_states = inputs_embeds
|
| 569 |
+
|
| 570 |
+
# Apply muP input scaling if enabled
|
| 571 |
+
if self.config.mup_enabled:
|
| 572 |
+
hidden_states = hidden_states * (self.config.hidden_size**0.5)
|
| 573 |
+
|
| 574 |
+
position_embeddings = self.rotary_emb(hidden_states, position_ids)
|
| 575 |
+
|
| 576 |
+
for decoder_layer in self.layers:
|
| 577 |
+
hidden_states = decoder_layer(
|
| 578 |
+
hidden_states,
|
| 579 |
+
attention_mask=causal_mask_mapping[decoder_layer.attention_type],
|
| 580 |
+
position_ids=position_ids,
|
| 581 |
+
past_key_value=past_key_values,
|
| 582 |
+
use_cache=use_cache,
|
| 583 |
+
cache_position=cache_position,
|
| 584 |
+
position_embeddings=position_embeddings,
|
| 585 |
+
**kwargs,
|
| 586 |
+
)
|
| 587 |
+
|
| 588 |
+
hidden_states = self.norm(hidden_states)
|
| 589 |
+
return MoeModelOutputWithPast(
|
| 590 |
+
last_hidden_state=hidden_states,
|
| 591 |
+
past_key_values=past_key_values,
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
|
| 595 |
+
class AfmoeForCausalLM(AfmoePreTrainedModel, GenerationMixin):
|
| 596 |
+
_tied_weights_keys = ["lm_head.weight"]
|
| 597 |
+
_tp_plan = {"lm_head": "colwise_rep"}
|
| 598 |
+
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
|
| 599 |
+
|
| 600 |
+
def __init__(self, config):
|
| 601 |
+
super().__init__(config)
|
| 602 |
+
self.model = AfmoeModel(config)
|
| 603 |
+
self.vocab_size = config.vocab_size
|
| 604 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 605 |
+
|
| 606 |
+
# Initialize weights and apply final processing
|
| 607 |
+
self.post_init()
|
| 608 |
+
|
| 609 |
+
def get_input_embeddings(self):
|
| 610 |
+
return self.model.embed_tokens
|
| 611 |
+
|
| 612 |
+
def set_input_embeddings(self, value):
|
| 613 |
+
self.model.embed_tokens = value
|
| 614 |
+
|
| 615 |
+
def get_output_embeddings(self):
|
| 616 |
+
return self.lm_head
|
| 617 |
+
|
| 618 |
+
def set_output_embeddings(self, new_embeddings):
|
| 619 |
+
self.lm_head = new_embeddings
|
| 620 |
+
|
| 621 |
+
def set_decoder(self, decoder):
|
| 622 |
+
self.model = decoder
|
| 623 |
+
|
| 624 |
+
def get_decoder(self):
|
| 625 |
+
return self.model
|
| 626 |
+
|
| 627 |
+
def forward(
|
| 628 |
+
self,
|
| 629 |
+
input_ids: torch.LongTensor,
|
| 630 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 631 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 632 |
+
past_key_values: Optional[Cache] = None,
|
| 633 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 634 |
+
labels: Optional[torch.LongTensor] = None,
|
| 635 |
+
use_cache: Optional[bool] = None,
|
| 636 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 637 |
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
| 638 |
+
token_type_ids: Optional[torch.Tensor] = None, # will be ignored
|
| 639 |
+
**kwargs: Unpack[TransformersKwargs],
|
| 640 |
+
) -> Union[Tuple, MoeCausalLMOutputWithPast]:
|
| 641 |
+
outputs: MoeModelOutputWithPast = self.model(
|
| 642 |
+
input_ids=input_ids,
|
| 643 |
+
attention_mask=attention_mask,
|
| 644 |
+
position_ids=position_ids,
|
| 645 |
+
past_key_values=past_key_values,
|
| 646 |
+
inputs_embeds=inputs_embeds,
|
| 647 |
+
use_cache=use_cache,
|
| 648 |
+
cache_position=cache_position,
|
| 649 |
+
**kwargs,
|
| 650 |
+
)
|
| 651 |
+
|
| 652 |
+
hidden_states = outputs.last_hidden_state
|
| 653 |
+
# Only compute necessary logits
|
| 654 |
+
slice_indices = (
|
| 655 |
+
slice(-logits_to_keep, None)
|
| 656 |
+
if isinstance(logits_to_keep, int)
|
| 657 |
+
else logits_to_keep
|
| 658 |
+
)
|
| 659 |
+
logits = self.lm_head(hidden_states[:, slice_indices, :])
|
| 660 |
+
|
| 661 |
+
loss = None
|
| 662 |
+
if labels is not None:
|
| 663 |
+
loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)
|
| 664 |
+
|
| 665 |
+
|
| 666 |
+
return MoeCausalLMOutputWithPast(
|
| 667 |
+
loss=loss,
|
| 668 |
+
logits=logits,
|
| 669 |
+
past_key_values=outputs.past_key_values,
|
| 670 |
+
hidden_states=outputs.hidden_states,
|
| 671 |
+
attentions=outputs.attentions,
|
| 672 |
+
router_logits=outputs.router_logits,
|
| 673 |
+
)
|
| 674 |
+
|
| 675 |
+
|
| 676 |
+
__all__ = [
|
| 677 |
+
"AfmoeForCausalLM",
|
| 678 |
+
"AfmoeModel",
|
| 679 |
+
"AfmoePreTrainedModel",
|
| 680 |
+
]
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<|begin_of_text|>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"eos_token": {
|
| 10 |
+
"content": "<|im_end|>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "<|pad|>",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
}
|
| 23 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4d864fe0d9c300d44c54006f5960548946d507d8ec05a082a3bff3e49de58208
|
| 3 |
+
size 14614721
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_bos_token": false,
|
| 3 |
+
"add_eos_token": false,
|
| 4 |
+
"add_prefix_space": null,
|
| 5 |
+
"added_tokens_decoder": {
|
| 6 |
+
"0": {
|
| 7 |
+
"content": "<|begin_of_text|>",
|
| 8 |
+
"lstrip": false,
|
| 9 |
+
"normalized": false,
|
| 10 |
+
"rstrip": false,
|
| 11 |
+
"single_word": false,
|
| 12 |
+
"special": true
|
| 13 |
+
},
|
| 14 |
+
"1": {
|
| 15 |
+
"content": "<|end_of_text|>",
|
| 16 |
+
"lstrip": false,
|
| 17 |
+
"normalized": false,
|
| 18 |
+
"rstrip": false,
|
| 19 |
+
"single_word": false,
|
| 20 |
+
"special": true
|
| 21 |
+
},
|
| 22 |
+
"2": {
|
| 23 |
+
"content": "<|im_start|>",
|
| 24 |
+
"lstrip": false,
|
| 25 |
+
"normalized": false,
|
| 26 |
+
"rstrip": false,
|
| 27 |
+
"single_word": false,
|
| 28 |
+
"special": true
|
| 29 |
+
},
|
| 30 |
+
"3": {
|
| 31 |
+
"content": "<|im_end|>",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": false,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false,
|
| 36 |
+
"special": true
|
| 37 |
+
},
|
| 38 |
+
"4": {
|
| 39 |
+
"content": "<|eot_id|>",
|
| 40 |
+
"lstrip": false,
|
| 41 |
+
"normalized": false,
|
| 42 |
+
"rstrip": false,
|
| 43 |
+
"single_word": false,
|
| 44 |
+
"special": true
|
| 45 |
+
},
|
| 46 |
+
"5": {
|
| 47 |
+
"content": "<|start|>",
|
| 48 |
+
"lstrip": false,
|
| 49 |
+
"normalized": false,
|
| 50 |
+
"rstrip": false,
|
| 51 |
+
"single_word": false,
|
| 52 |
+
"special": true
|
| 53 |
+
},
|
| 54 |
+
"6": {
|
| 55 |
+
"content": "<|channel|>",
|
| 56 |
+
"lstrip": false,
|
| 57 |
+
"normalized": false,
|
| 58 |
+
"rstrip": false,
|
| 59 |
+
"single_word": false,
|
| 60 |
+
"special": true
|
| 61 |
+
},
|
| 62 |
+
"7": {
|
| 63 |
+
"content": "<|message|>",
|
| 64 |
+
"lstrip": false,
|
| 65 |
+
"normalized": false,
|
| 66 |
+
"rstrip": false,
|
| 67 |
+
"single_word": false,
|
| 68 |
+
"special": true
|
| 69 |
+
},
|
| 70 |
+
"8": {
|
| 71 |
+
"content": "<|end|>",
|
| 72 |
+
"lstrip": false,
|
| 73 |
+
"normalized": false,
|
| 74 |
+
"rstrip": false,
|
| 75 |
+
"single_word": false,
|
| 76 |
+
"special": true
|
| 77 |
+
},
|
| 78 |
+
"9": {
|
| 79 |
+
"content": "<|fitm_start|>",
|
| 80 |
+
"lstrip": false,
|
| 81 |
+
"normalized": false,
|
| 82 |
+
"rstrip": false,
|
| 83 |
+
"single_word": false,
|
| 84 |
+
"special": true
|
| 85 |
+
},
|
| 86 |
+
"10": {
|
| 87 |
+
"content": "<|fitm_end|>",
|
| 88 |
+
"lstrip": false,
|
| 89 |
+
"normalized": false,
|
| 90 |
+
"rstrip": false,
|
| 91 |
+
"single_word": false,
|
| 92 |
+
"special": true
|
| 93 |
+
},
|
| 94 |
+
"11": {
|
| 95 |
+
"content": "<|fitm_hole|>",
|
| 96 |
+
"lstrip": false,
|
| 97 |
+
"normalized": false,
|
| 98 |
+
"rstrip": false,
|
| 99 |
+
"single_word": false,
|
| 100 |
+
"special": true
|
| 101 |
+
},
|
| 102 |
+
"12": {
|
| 103 |
+
"content": "<|pad|>",
|
| 104 |
+
"lstrip": false,
|
| 105 |
+
"normalized": false,
|
| 106 |
+
"rstrip": false,
|
| 107 |
+
"single_word": false,
|
| 108 |
+
"special": true
|
| 109 |
+
},
|
| 110 |
+
"13": {
|
| 111 |
+
"content": "<think>",
|
| 112 |
+
"lstrip": false,
|
| 113 |
+
"normalized": false,
|
| 114 |
+
"rstrip": false,
|
| 115 |
+
"single_word": false,
|
| 116 |
+
"special": false
|
| 117 |
+
},
|
| 118 |
+
"14": {
|
| 119 |
+
"content": "</think>",
|
| 120 |
+
"lstrip": false,
|
| 121 |
+
"normalized": false,
|
| 122 |
+
"rstrip": false,
|
| 123 |
+
"single_word": false,
|
| 124 |
+
"special": false
|
| 125 |
+
},
|
| 126 |
+
"15": {
|
| 127 |
+
"content": "<tools>",
|
| 128 |
+
"lstrip": false,
|
| 129 |
+
"normalized": false,
|
| 130 |
+
"rstrip": false,
|
| 131 |
+
"single_word": false,
|
| 132 |
+
"special": false
|
| 133 |
+
},
|
| 134 |
+
"16": {
|
| 135 |
+
"content": "</tools>",
|
| 136 |
+
"lstrip": false,
|
| 137 |
+
"normalized": false,
|
| 138 |
+
"rstrip": false,
|
| 139 |
+
"single_word": false,
|
| 140 |
+
"special": false
|
| 141 |
+
},
|
| 142 |
+
"17": {
|
| 143 |
+
"content": "<tool_call>",
|
| 144 |
+
"lstrip": false,
|
| 145 |
+
"normalized": false,
|
| 146 |
+
"rstrip": false,
|
| 147 |
+
"single_word": false,
|
| 148 |
+
"special": false
|
| 149 |
+
},
|
| 150 |
+
"18": {
|
| 151 |
+
"content": "</tool_call>",
|
| 152 |
+
"lstrip": false,
|
| 153 |
+
"normalized": false,
|
| 154 |
+
"rstrip": false,
|
| 155 |
+
"single_word": false,
|
| 156 |
+
"special": false
|
| 157 |
+
},
|
| 158 |
+
"19": {
|
| 159 |
+
"content": "<tool_response>",
|
| 160 |
+
"lstrip": false,
|
| 161 |
+
"normalized": false,
|
| 162 |
+
"rstrip": false,
|
| 163 |
+
"single_word": false,
|
| 164 |
+
"special": false
|
| 165 |
+
},
|
| 166 |
+
"20": {
|
| 167 |
+
"content": "</tool_response>",
|
| 168 |
+
"lstrip": false,
|
| 169 |
+
"normalized": false,
|
| 170 |
+
"rstrip": false,
|
| 171 |
+
"single_word": false,
|
| 172 |
+
"special": false
|
| 173 |
+
},
|
| 174 |
+
"21": {
|
| 175 |
+
"content": "<|reserved_special_8|>",
|
| 176 |
+
"lstrip": false,
|
| 177 |
+
"normalized": false,
|
| 178 |
+
"rstrip": false,
|
| 179 |
+
"single_word": false,
|
| 180 |
+
"special": true
|
| 181 |
+
},
|
| 182 |
+
"22": {
|
| 183 |
+
"content": "<|reserved_special_9|>",
|
| 184 |
+
"lstrip": false,
|
| 185 |
+
"normalized": false,
|
| 186 |
+
"rstrip": false,
|
| 187 |
+
"single_word": false,
|
| 188 |
+
"special": true
|
| 189 |
+
},
|
| 190 |
+
"23": {
|
| 191 |
+
"content": "<|reserved_special_10|>",
|
| 192 |
+
"lstrip": false,
|
| 193 |
+
"normalized": false,
|
| 194 |
+
"rstrip": false,
|
| 195 |
+
"single_word": false,
|
| 196 |
+
"special": true
|
| 197 |
+
},
|
| 198 |
+
"24": {
|
| 199 |
+
"content": "<|reserved_special_11|>",
|
| 200 |
+
"lstrip": false,
|
| 201 |
+
"normalized": false,
|
| 202 |
+
"rstrip": false,
|
| 203 |
+
"single_word": false,
|
| 204 |
+
"special": true
|
| 205 |
+
},
|
| 206 |
+
"25": {
|
| 207 |
+
"content": "<|reserved_special_12|>",
|
| 208 |
+
"lstrip": false,
|
| 209 |
+
"normalized": false,
|
| 210 |
+
"rstrip": false,
|
| 211 |
+
"single_word": false,
|
| 212 |
+
"special": true
|
| 213 |
+
},
|
| 214 |
+
"26": {
|
| 215 |
+
"content": "<|reserved_special_13|>",
|
| 216 |
+
"lstrip": false,
|
| 217 |
+
"normalized": false,
|
| 218 |
+
"rstrip": false,
|
| 219 |
+
"single_word": false,
|
| 220 |
+
"special": true
|
| 221 |
+
},
|
| 222 |
+
"27": {
|
| 223 |
+
"content": "<|reserved_special_14|>",
|
| 224 |
+
"lstrip": false,
|
| 225 |
+
"normalized": false,
|
| 226 |
+
"rstrip": false,
|
| 227 |
+
"single_word": false,
|
| 228 |
+
"special": true
|
| 229 |
+
},
|
| 230 |
+
"28": {
|
| 231 |
+
"content": "<|reserved_special_15|>",
|
| 232 |
+
"lstrip": false,
|
| 233 |
+
"normalized": false,
|
| 234 |
+
"rstrip": false,
|
| 235 |
+
"single_word": false,
|
| 236 |
+
"special": true
|
| 237 |
+
},
|
| 238 |
+
"29": {
|
| 239 |
+
"content": "<|reserved_special_16|>",
|
| 240 |
+
"lstrip": false,
|
| 241 |
+
"normalized": false,
|
| 242 |
+
"rstrip": false,
|
| 243 |
+
"single_word": false,
|
| 244 |
+
"special": true
|
| 245 |
+
},
|
| 246 |
+
"30": {
|
| 247 |
+
"content": "<|reserved_special_17|>",
|
| 248 |
+
"lstrip": false,
|
| 249 |
+
"normalized": false,
|
| 250 |
+
"rstrip": false,
|
| 251 |
+
"single_word": false,
|
| 252 |
+
"special": true
|
| 253 |
+
},
|
| 254 |
+
"31": {
|
| 255 |
+
"content": "<|reserved_special_18|>",
|
| 256 |
+
"lstrip": false,
|
| 257 |
+
"normalized": false,
|
| 258 |
+
"rstrip": false,
|
| 259 |
+
"single_word": false,
|
| 260 |
+
"special": true
|
| 261 |
+
}
|
| 262 |
+
},
|
| 263 |
+
"bos_token": "<|begin_of_text|>",
|
| 264 |
+
"clean_up_tokenization_spaces": false,
|
| 265 |
+
"eos_token": "<|im_end|>",
|
| 266 |
+
"extra_special_tokens": {},
|
| 267 |
+
"model_max_length": 65536,
|
| 268 |
+
"pad_token": "<|pad|>",
|
| 269 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 270 |
+
"use_default_system_prompt": false,
|
| 271 |
+
"chat_template": "{{ bos_token }}{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0].role == 'system' %}\n {{- messages[0].content + '\\n\\n' }}\n {%- endif %}\n {{- \"# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\n</tools>\\n\\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\\n<tool_call>\\n{\\\"name\\\": <function-name>, \\\"arguments\\\": <args-json-object>}\\n</tool_call><|im_end|>\\n\" }}\n{%- else %}\n {%- if messages[0].role == 'system' %}\n {{- '<|im_start|>system\\n' + messages[0].content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- for message in messages %}\n {%- if message.content is string %}\n {%- set content = message.content %}\n {%- else %}\n {%- set content = '' %}\n {%- endif %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) %}\n {{- '<|im_start|>' + message.role + '\\n' + content + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {{- '<|im_start|>' + message.role + '\\n' }}\n {{- content}}\n {%- if message.tool_calls %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>' }}\n {{- '\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.first or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- content }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n{%- endif %}"
|
| 272 |
+
}
|