Instructions to use ServiceNow-AI/SuperApriel-15B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ServiceNow-AI/SuperApriel-15B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ServiceNow-AI/SuperApriel-15B-Instruct", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForImageTextToText model = AutoModelForImageTextToText.from_pretrained("ServiceNow-AI/SuperApriel-15B-Instruct", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ServiceNow-AI/SuperApriel-15B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ServiceNow-AI/SuperApriel-15B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ServiceNow-AI/SuperApriel-15B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/ServiceNow-AI/SuperApriel-15B-Instruct
- SGLang
How to use ServiceNow-AI/SuperApriel-15B-Instruct 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 "ServiceNow-AI/SuperApriel-15B-Instruct" \ --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": "ServiceNow-AI/SuperApriel-15B-Instruct", "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 "ServiceNow-AI/SuperApriel-15B-Instruct" \ --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": "ServiceNow-AI/SuperApriel-15B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use ServiceNow-AI/SuperApriel-15B-Instruct with Docker Model Runner:
docker model run hf.co/ServiceNow-AI/SuperApriel-15B-Instruct
Add Transformers support with AutoModelForCausalLM and preset selection
Browse files- Add AutoModelForCausalLM to auto_map in all config.json files (main + 9 presets)
- Add config_class = Apriel2Config to Apriel2ForCausalLM for compatibility
- Add Apriel2PatternMixerAdapter to match supernet checkpoint weight paths
(blocks.{i}.mixer.mixers.{type}.{param}) when loading pattern preset configs
- Update README: move Transformers section after vLLM, add install deps,
document preset selection workflow with config.json copy
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- README.md +65 -45
- config.json +141 -140
- modeling_apriel2.py +40 -2
- preset_configs/Idealized_All-18/config.json +1 -0
- preset_configs/Idealized_All-6/config.json +1 -0
- preset_configs/Idealized_Lklhd-6/config.json +1 -0
- preset_configs/Reg_Lklhd-10/config.json +1 -0
- preset_configs/Reg_Lklhd-13/config.json +1 -0
- preset_configs/Reg_Lklhd-18/config.json +1 -0
- preset_configs/Reg_Lklhd-26/config.json +1 -0
- preset_configs/all-attention/config.json +1 -0
- preset_configs/extra_bayesian-mix-7/config.json +1 -0
|
@@ -107,51 +107,7 @@ SuperApriel-15b-Instruct is trained in two stages:
|
|
| 107 |
|
| 108 |
## How to Use
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
```bash
|
| 113 |
-
pip install transformers
|
| 114 |
-
```
|
| 115 |
-
|
| 116 |
-
> **🔴 TODO: There is currently no mechanism to select a preset when using Transformers directly. The model defaults to the all-attention preset during inference. Preset selection requires vLLM with the Fast-LLM plugin (see below). We need to add a Transformers API for preset switching.**
|
| 117 |
-
|
| 118 |
-
Basic usage with Transformers (all-attention preset):
|
| 119 |
-
|
| 120 |
-
```python
|
| 121 |
-
import re
|
| 122 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 123 |
-
|
| 124 |
-
model_name = "ServiceNow-AI/SuperApriel-15b-Instruct"
|
| 125 |
-
|
| 126 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 127 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 128 |
-
model_name,
|
| 129 |
-
torch_dtype="auto",
|
| 130 |
-
device_map="auto",
|
| 131 |
-
trust_remote_code=True,
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
prompt = "Positive real numbers $x$ and $y$ satisfy $y^3=x^2$ and $(y-x)^2=4y^2$. What is $x+y$?\nMark your solution with \\boxed"
|
| 135 |
-
messages = [
|
| 136 |
-
{"role": "user", "content": prompt}
|
| 137 |
-
]
|
| 138 |
-
|
| 139 |
-
text = tokenizer.apply_chat_template(
|
| 140 |
-
messages,
|
| 141 |
-
tokenize=False,
|
| 142 |
-
add_generation_prompt=True,
|
| 143 |
-
tools=[]
|
| 144 |
-
)
|
| 145 |
-
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 146 |
-
|
| 147 |
-
generated_ids = model.generate(**model_inputs, max_new_tokens=1024)
|
| 148 |
-
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 149 |
-
|
| 150 |
-
response = re.findall(r"\[BEGIN FINAL RESPONSE\](.*?)\[END FINAL RESPONSE\]", output, re.DOTALL)[0].strip()
|
| 151 |
-
print("response:", response)
|
| 152 |
-
```
|
| 153 |
-
|
| 154 |
-
See [Apriel-1.6-15b-Thinker](https://huggingface.co/ServiceNow-AI/Apriel-1.6-15b-Thinker) for recommended inference settings.
|
| 155 |
|
| 156 |
## Use with vLLM
|
| 157 |
|
|
@@ -265,6 +221,70 @@ The supernet mode above switches the active placement globally for all requests.
|
|
| 265 |
|
| 266 |
> **🔴 TODO: Add per-request placement selection via the vLLM serving API (e.g. `placement_id` field in the request body). This is managed separately from the global `collective_rpc` switching shown above.**
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
## Chat Template
|
| 269 |
|
| 270 |
```
|
|
|
|
| 107 |
|
| 108 |
## How to Use
|
| 109 |
|
| 110 |
+
The recommended serving backend is **vLLM** with the Fast-LLM plugin, which supports preset selection and runtime switching. For simpler use cases, Transformers is also supported (see [Use with Transformers](#use-with-transformers) below).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
## Use with vLLM
|
| 113 |
|
|
|
|
| 221 |
|
| 222 |
> **🔴 TODO: Add per-request placement selection via the vLLM serving API (e.g. `placement_id` field in the request body). This is managed separately from the global `collective_rpc` switching shown above.**
|
| 223 |
|
| 224 |
+
## Use with Transformers
|
| 225 |
+
|
| 226 |
+
The model works with `AutoModelForCausalLM` for standard Transformers inference. Each preset config selects one mixer per layer, and the model automatically loads only the relevant weights from the supernet checkpoint.
|
| 227 |
+
|
| 228 |
+
### Installation
|
| 229 |
+
|
| 230 |
+
```bash
|
| 231 |
+
pip install transformers torch einops accelerate
|
| 232 |
+
```
|
| 233 |
+
|
| 234 |
+
Presets that include GDN or KDA mixers additionally require:
|
| 235 |
+
|
| 236 |
+
```bash
|
| 237 |
+
pip install causal-conv1d mamba-ssm
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
Attention-only presets (`all-attention`, and others using only FA/SWA) work without these extra packages.
|
| 241 |
+
|
| 242 |
+
### Usage
|
| 243 |
+
|
| 244 |
+
The repo ships with the supernet config as default (for vLLM). Before loading with Transformers, you must select a preset by copying its `config.json`:
|
| 245 |
+
|
| 246 |
+
```python
|
| 247 |
+
import re
|
| 248 |
+
import shutil
|
| 249 |
+
import torch
|
| 250 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 251 |
+
|
| 252 |
+
model_dir = "ServiceNow-AI/SuperApriel-15b-Instruct" # or local path
|
| 253 |
+
|
| 254 |
+
# Select a preset (required — the default supernet config is for vLLM only)
|
| 255 |
+
preset = "all-attention" # or any preset from preset_configs/
|
| 256 |
+
shutil.copy(f"{model_dir}/preset_configs/{preset}/config.json",
|
| 257 |
+
f"{model_dir}/config.json")
|
| 258 |
+
|
| 259 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
|
| 260 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 261 |
+
model_dir,
|
| 262 |
+
trust_remote_code=True,
|
| 263 |
+
torch_dtype=torch.bfloat16,
|
| 264 |
+
device_map="auto",
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
prompt = "Positive real numbers $x$ and $y$ satisfy $y^3=x^2$ and $(y-x)^2=4y^2$. What is $x+y$?\nMark your solution with \\boxed"
|
| 268 |
+
messages = [{"role": "user", "content": prompt}]
|
| 269 |
+
|
| 270 |
+
text = tokenizer.apply_chat_template(
|
| 271 |
+
messages, tokenize=False, add_generation_prompt=True, tools=[]
|
| 272 |
+
)
|
| 273 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 274 |
+
|
| 275 |
+
generated_ids = model.generate(**model_inputs, max_new_tokens=1024)
|
| 276 |
+
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 277 |
+
|
| 278 |
+
response = re.findall(
|
| 279 |
+
r"\[BEGIN FINAL RESPONSE\](.*?)\[END FINAL RESPONSE\]", output, re.DOTALL
|
| 280 |
+
)[0].strip()
|
| 281 |
+
print("response:", response)
|
| 282 |
+
```
|
| 283 |
+
|
| 284 |
+
Available presets: `all-attention`, `Reg_Lklhd-26`, `Reg_Lklhd-18`, `Reg_Lklhd-13`, `Reg_Lklhd-10`, `Idealized_All-18`, `Idealized_All-6`, `Idealized_Lklhd-6`, `extra_bayesian-mix-7`.
|
| 285 |
+
|
| 286 |
+
See [Apriel-1.6-15b-Thinker](https://huggingface.co/ServiceNow-AI/Apriel-1.6-15b-Thinker) for recommended inference settings.
|
| 287 |
+
|
| 288 |
## Chat Template
|
| 289 |
|
| 290 |
```
|
|
@@ -1,151 +1,152 @@
|
|
| 1 |
{
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
},
|
| 32 |
-
"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"kda": {
|
| 39 |
-
"convolution_layer": {
|
| 40 |
-
"kernel_size": 4
|
| 41 |
},
|
| 42 |
-
"head_dim": 128,
|
| 43 |
-
"heads": 32,
|
| 44 |
"normalization": {
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
},
|
| 49 |
-
"sliding_window": {
|
| 50 |
-
"add_linear_biases": false,
|
| 51 |
-
"head_groups": 8,
|
| 52 |
-
"head_size": 128,
|
| 53 |
-
"heads": 32,
|
| 54 |
-
"rotary": {
|
| 55 |
-
"theta": 1000000000.0,
|
| 56 |
-
"type": "mistral_1d"
|
| 57 |
-
},
|
| 58 |
-
"type": "attention",
|
| 59 |
-
"window_size": 4096
|
| 60 |
-
}
|
| 61 |
},
|
| 62 |
-
"
|
| 63 |
-
"type": "
|
| 64 |
-
},
|
| 65 |
-
"mlp": {
|
| 66 |
-
"activation": "silu",
|
| 67 |
-
"add_linear_biases": false,
|
| 68 |
-
"gated": true,
|
| 69 |
-
"intermediate_size": 14336,
|
| 70 |
-
"type": "mlp"
|
| 71 |
-
},
|
| 72 |
-
"normalization": {
|
| 73 |
-
"epsilon": 1e-05,
|
| 74 |
-
"type": "rms_norm"
|
| 75 |
-
}
|
| 76 |
-
},
|
| 77 |
-
"num_blocks": 48,
|
| 78 |
-
"type": "fixed"
|
| 79 |
-
},
|
| 80 |
-
"embeddings": {
|
| 81 |
-
"max_position_embeddings": 120000
|
| 82 |
-
},
|
| 83 |
-
"eos_token_id": 2,
|
| 84 |
-
"head": {
|
| 85 |
-
"normalization": {
|
| 86 |
-
"epsilon": 1e-05,
|
| 87 |
-
"type": "rms_norm"
|
| 88 |
-
}
|
| 89 |
-
},
|
| 90 |
-
"hidden_size": 5120,
|
| 91 |
-
"image_token_index": 10,
|
| 92 |
-
"model_type": "apriel2",
|
| 93 |
-
"tie_word_embeddings": false,
|
| 94 |
-
"transformers_version": "4.57.3",
|
| 95 |
-
"use_cache": true,
|
| 96 |
-
"vision_encoder": {
|
| 97 |
-
"adapter": {
|
| 98 |
-
"activation": "gelu_pytorch_tanh",
|
| 99 |
-
"add_linear_biases": true,
|
| 100 |
-
"gated": false,
|
| 101 |
-
"intermediate_size": 5120,
|
| 102 |
-
"type": "mlp"
|
| 103 |
},
|
| 104 |
"embeddings": {
|
| 105 |
-
|
| 106 |
-
"normalization": {
|
| 107 |
-
"epsilon": 1e-05,
|
| 108 |
-
"type": "rms_norm"
|
| 109 |
-
},
|
| 110 |
-
"patch_height": 16,
|
| 111 |
-
"patch_width": 16
|
| 112 |
},
|
| 113 |
-
"
|
| 114 |
-
|
| 115 |
-
"mixer": {
|
| 116 |
-
"add_linear_biases": false,
|
| 117 |
-
"causal": false,
|
| 118 |
-
"cross_document_attention": false,
|
| 119 |
-
"head_groups": 16,
|
| 120 |
-
"head_size": 64,
|
| 121 |
-
"heads": 16,
|
| 122 |
-
"rotary": {
|
| 123 |
-
"max_image_size": 1024,
|
| 124 |
-
"patch_size": 16,
|
| 125 |
-
"theta": 10000.0,
|
| 126 |
-
"type": "pixtral_2d"
|
| 127 |
-
},
|
| 128 |
-
"type": "attention"
|
| 129 |
-
},
|
| 130 |
-
"mlp": {
|
| 131 |
-
"activation": "silu",
|
| 132 |
-
"add_linear_biases": false,
|
| 133 |
-
"gated": true,
|
| 134 |
-
"intermediate_size": 4096,
|
| 135 |
-
"type": "mlp"
|
| 136 |
-
},
|
| 137 |
"normalization": {
|
| 138 |
-
|
| 139 |
-
|
| 140 |
}
|
| 141 |
-
},
|
| 142 |
-
"num_blocks": 24,
|
| 143 |
-
"type": "fixed"
|
| 144 |
},
|
| 145 |
-
"hidden_size":
|
| 146 |
-
"
|
| 147 |
-
"
|
| 148 |
-
"
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Apriel2ForConditionalGeneration"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
+
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
+
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
+
},
|
| 11 |
+
"bos_token_id": 1,
|
| 12 |
+
"decoder": {
|
| 13 |
+
"block": {
|
| 14 |
+
"mixer": {
|
| 15 |
+
"main_mixer_name": "attention",
|
| 16 |
+
"mixers": {
|
| 17 |
+
"attention": {
|
| 18 |
+
"add_linear_biases": false,
|
| 19 |
+
"head_groups": 8,
|
| 20 |
+
"head_size": 128,
|
| 21 |
+
"heads": 32,
|
| 22 |
+
"rotary": {
|
| 23 |
+
"theta": 1000000000.0,
|
| 24 |
+
"type": "mistral_1d"
|
| 25 |
+
},
|
| 26 |
+
"type": "attention",
|
| 27 |
+
"window_size": null
|
| 28 |
+
},
|
| 29 |
+
"gdn": {
|
| 30 |
+
"convolution_layer": {
|
| 31 |
+
"kernel_size": 4
|
| 32 |
+
},
|
| 33 |
+
"key_head_dim": 128,
|
| 34 |
+
"key_heads": 8,
|
| 35 |
+
"type": "gdn",
|
| 36 |
+
"value_head_dim": 128,
|
| 37 |
+
"value_heads": 32
|
| 38 |
+
},
|
| 39 |
+
"kda": {
|
| 40 |
+
"convolution_layer": {
|
| 41 |
+
"kernel_size": 4
|
| 42 |
+
},
|
| 43 |
+
"head_dim": 128,
|
| 44 |
+
"heads": 32,
|
| 45 |
+
"normalization": {
|
| 46 |
+
"epsilon": 1e-05
|
| 47 |
+
},
|
| 48 |
+
"type": "kda"
|
| 49 |
+
},
|
| 50 |
+
"sliding_window": {
|
| 51 |
+
"add_linear_biases": false,
|
| 52 |
+
"head_groups": 8,
|
| 53 |
+
"head_size": 128,
|
| 54 |
+
"heads": 32,
|
| 55 |
+
"rotary": {
|
| 56 |
+
"theta": 1000000000.0,
|
| 57 |
+
"type": "mistral_1d"
|
| 58 |
+
},
|
| 59 |
+
"type": "attention",
|
| 60 |
+
"window_size": 4096
|
| 61 |
+
}
|
| 62 |
+
},
|
| 63 |
+
"sampling_strategy": "full_layout",
|
| 64 |
+
"type": "stochastic"
|
| 65 |
},
|
| 66 |
+
"mlp": {
|
| 67 |
+
"activation": "silu",
|
| 68 |
+
"add_linear_biases": false,
|
| 69 |
+
"gated": true,
|
| 70 |
+
"intermediate_size": 14336,
|
| 71 |
+
"type": "mlp"
|
|
|
|
|
|
|
|
|
|
| 72 |
},
|
|
|
|
|
|
|
| 73 |
"normalization": {
|
| 74 |
+
"epsilon": 1e-05,
|
| 75 |
+
"type": "rms_norm"
|
| 76 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
},
|
| 78 |
+
"num_blocks": 48,
|
| 79 |
+
"type": "fixed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
},
|
| 81 |
"embeddings": {
|
| 82 |
+
"max_position_embeddings": 120000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
},
|
| 84 |
+
"eos_token_id": 2,
|
| 85 |
+
"head": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
"normalization": {
|
| 87 |
+
"epsilon": 1e-05,
|
| 88 |
+
"type": "rms_norm"
|
| 89 |
}
|
|
|
|
|
|
|
|
|
|
| 90 |
},
|
| 91 |
+
"hidden_size": 5120,
|
| 92 |
+
"image_token_index": 10,
|
| 93 |
+
"model_type": "apriel2",
|
| 94 |
+
"tie_word_embeddings": false,
|
| 95 |
+
"transformers_version": "4.57.3",
|
| 96 |
+
"use_cache": true,
|
| 97 |
+
"vision_encoder": {
|
| 98 |
+
"adapter": {
|
| 99 |
+
"activation": "gelu_pytorch_tanh",
|
| 100 |
+
"add_linear_biases": true,
|
| 101 |
+
"gated": false,
|
| 102 |
+
"intermediate_size": 5120,
|
| 103 |
+
"type": "mlp"
|
| 104 |
+
},
|
| 105 |
+
"embeddings": {
|
| 106 |
+
"input_channels": 3,
|
| 107 |
+
"normalization": {
|
| 108 |
+
"epsilon": 1e-05,
|
| 109 |
+
"type": "rms_norm"
|
| 110 |
+
},
|
| 111 |
+
"patch_height": 16,
|
| 112 |
+
"patch_width": 16
|
| 113 |
+
},
|
| 114 |
+
"encoder": {
|
| 115 |
+
"block": {
|
| 116 |
+
"mixer": {
|
| 117 |
+
"add_linear_biases": false,
|
| 118 |
+
"causal": false,
|
| 119 |
+
"cross_document_attention": false,
|
| 120 |
+
"head_groups": 16,
|
| 121 |
+
"head_size": 64,
|
| 122 |
+
"heads": 16,
|
| 123 |
+
"rotary": {
|
| 124 |
+
"max_image_size": 1024,
|
| 125 |
+
"patch_size": 16,
|
| 126 |
+
"theta": 10000.0,
|
| 127 |
+
"type": "pixtral_2d"
|
| 128 |
+
},
|
| 129 |
+
"type": "attention"
|
| 130 |
+
},
|
| 131 |
+
"mlp": {
|
| 132 |
+
"activation": "silu",
|
| 133 |
+
"add_linear_biases": false,
|
| 134 |
+
"gated": true,
|
| 135 |
+
"intermediate_size": 4096,
|
| 136 |
+
"type": "mlp"
|
| 137 |
+
},
|
| 138 |
+
"normalization": {
|
| 139 |
+
"epsilon": 1e-05,
|
| 140 |
+
"type": "rms_norm"
|
| 141 |
+
}
|
| 142 |
+
},
|
| 143 |
+
"num_blocks": 24,
|
| 144 |
+
"type": "fixed"
|
| 145 |
+
},
|
| 146 |
+
"hidden_size": 1024,
|
| 147 |
+
"num_channels": 3,
|
| 148 |
+
"num_hidden_layers": 24,
|
| 149 |
+
"patch_size": 16
|
| 150 |
+
},
|
| 151 |
+
"vocab_size": 131072
|
| 152 |
+
}
|
|
@@ -972,6 +972,32 @@ def create_mixer(mixer_config: dict, hidden_size: int, layer_idx: int, config, a
|
|
| 972 |
return mixer_class(hidden_size, mixer_config, layer_idx=layer_idx)
|
| 973 |
|
| 974 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
class Apriel2Mamba(nn.Module):
|
| 976 |
"""Mamba mixer."""
|
| 977 |
|
|
@@ -1906,14 +1932,16 @@ class Apriel2BlockSequence(nn.Module):
|
|
| 1906 |
|
| 1907 |
blocks = []
|
| 1908 |
for layer_idx in range(num_blocks):
|
| 1909 |
-
# Get block_config for this layer
|
| 1910 |
if seq_type == "fixed":
|
| 1911 |
block_config = self.sequence_config.get("block", {})
|
|
|
|
| 1912 |
elif seq_type == "pattern":
|
| 1913 |
pattern = self.sequence_config.get("pattern", [])
|
| 1914 |
blocks_config = self.sequence_config.get("blocks", {})
|
| 1915 |
block_name = pattern[layer_idx % len(pattern)]
|
| 1916 |
block_config = blocks_config[block_name]
|
|
|
|
| 1917 |
else:
|
| 1918 |
raise ValueError(f"Unknown sequence type: {seq_type}")
|
| 1919 |
|
|
@@ -1925,6 +1953,7 @@ class Apriel2BlockSequence(nn.Module):
|
|
| 1925 |
layer_idx=layer_idx,
|
| 1926 |
rms_norm_eps=rms_norm_eps,
|
| 1927 |
config=self.config,
|
|
|
|
| 1928 |
)
|
| 1929 |
)
|
| 1930 |
|
|
@@ -2031,6 +2060,7 @@ class Apriel2Block(nn.Module):
|
|
| 2031 |
layer_idx: int,
|
| 2032 |
rms_norm_eps: float,
|
| 2033 |
config: Apriel2TextConfig,
|
|
|
|
| 2034 |
):
|
| 2035 |
"""
|
| 2036 |
Args:
|
|
@@ -2039,6 +2069,7 @@ class Apriel2Block(nn.Module):
|
|
| 2039 |
layer_idx: Layer index in the sequence
|
| 2040 |
rms_norm_eps: Epsilon for RMS normalization
|
| 2041 |
config: Model config (passed to mixers that need it)
|
|
|
|
| 2042 |
"""
|
| 2043 |
super().__init__()
|
| 2044 |
self.hidden_size = hidden_size
|
|
@@ -2046,7 +2077,13 @@ class Apriel2Block(nn.Module):
|
|
| 2046 |
|
| 2047 |
# Create mixer based on type
|
| 2048 |
mixer_config = block_config.get("mixer", {"type": "attention"})
|
| 2049 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2050 |
|
| 2051 |
# Create MLP
|
| 2052 |
mlp_config = block_config.get("mlp", {"type": "mlp"})
|
|
@@ -2435,6 +2472,7 @@ class Apriel2TextModel(Apriel2PreTrainedModel):
|
|
| 2435 |
class Apriel2ForCausalLM(Apriel2PreTrainedModel, GenerationMixin):
|
| 2436 |
"""Apriel2 model with a language modeling head (text-only)."""
|
| 2437 |
|
|
|
|
| 2438 |
_tied_weights_keys = ["lm_head.weight"]
|
| 2439 |
|
| 2440 |
def __init__(self, config: Apriel2TextConfig):
|
|
|
|
| 972 |
return mixer_class(hidden_size, mixer_config, layer_idx=layer_idx)
|
| 973 |
|
| 974 |
|
| 975 |
+
class Apriel2PatternMixerAdapter(nn.Module):
|
| 976 |
+
"""Adapter that wraps a single mixer under mixers.{name} to match supernet weight paths.
|
| 977 |
+
|
| 978 |
+
The supernet checkpoint stores weights as blocks.{i}.mixer.mixers.{type}.{param},
|
| 979 |
+
but a bare mixer creates blocks.{i}.mixer.{param}. This adapter adds the intermediate
|
| 980 |
+
mixers.{name} level so pattern configs can load from supernet checkpoints.
|
| 981 |
+
"""
|
| 982 |
+
|
| 983 |
+
def __init__(self, mixer_name: str, mixer: nn.Module):
|
| 984 |
+
super().__init__()
|
| 985 |
+
self.mixers = nn.ModuleDict({mixer_name: mixer})
|
| 986 |
+
self._mixer_name = mixer_name
|
| 987 |
+
|
| 988 |
+
def forward(self, *args, **kwargs):
|
| 989 |
+
return self.mixers[self._mixer_name](*args, **kwargs)
|
| 990 |
+
|
| 991 |
+
def preprocess(self, *args, **kwargs):
|
| 992 |
+
return self.mixers[self._mixer_name].preprocess(*args, **kwargs)
|
| 993 |
+
|
| 994 |
+
@classmethod
|
| 995 |
+
def setup(cls, mixer_name: str, mixer_config: dict, hidden_size: int, max_position_embeddings: int) -> nn.ModuleDict:
|
| 996 |
+
mixer_type = mixer_config.get("type", "attention")
|
| 997 |
+
mixer_class = get_mixer_class(mixer_type)
|
| 998 |
+
return mixer_class.setup(mixer_config, hidden_size, max_position_embeddings)
|
| 999 |
+
|
| 1000 |
+
|
| 1001 |
class Apriel2Mamba(nn.Module):
|
| 1002 |
"""Mamba mixer."""
|
| 1003 |
|
|
|
|
| 1932 |
|
| 1933 |
blocks = []
|
| 1934 |
for layer_idx in range(num_blocks):
|
| 1935 |
+
# Get block_config and block_name for this layer
|
| 1936 |
if seq_type == "fixed":
|
| 1937 |
block_config = self.sequence_config.get("block", {})
|
| 1938 |
+
block_name_for_layer = None # No adapter needed for fixed type
|
| 1939 |
elif seq_type == "pattern":
|
| 1940 |
pattern = self.sequence_config.get("pattern", [])
|
| 1941 |
blocks_config = self.sequence_config.get("blocks", {})
|
| 1942 |
block_name = pattern[layer_idx % len(pattern)]
|
| 1943 |
block_config = blocks_config[block_name]
|
| 1944 |
+
block_name_for_layer = block_name # Pass to Apriel2Block for weight path matching
|
| 1945 |
else:
|
| 1946 |
raise ValueError(f"Unknown sequence type: {seq_type}")
|
| 1947 |
|
|
|
|
| 1953 |
layer_idx=layer_idx,
|
| 1954 |
rms_norm_eps=rms_norm_eps,
|
| 1955 |
config=self.config,
|
| 1956 |
+
block_name=block_name_for_layer,
|
| 1957 |
)
|
| 1958 |
)
|
| 1959 |
|
|
|
|
| 2060 |
layer_idx: int,
|
| 2061 |
rms_norm_eps: float,
|
| 2062 |
config: Apriel2TextConfig,
|
| 2063 |
+
block_name: Optional[str] = None,
|
| 2064 |
):
|
| 2065 |
"""
|
| 2066 |
Args:
|
|
|
|
| 2069 |
layer_idx: Layer index in the sequence
|
| 2070 |
rms_norm_eps: Epsilon for RMS normalization
|
| 2071 |
config: Model config (passed to mixers that need it)
|
| 2072 |
+
block_name: For pattern configs, the mixer name (e.g. "attention") to match supernet weight paths
|
| 2073 |
"""
|
| 2074 |
super().__init__()
|
| 2075 |
self.hidden_size = hidden_size
|
|
|
|
| 2077 |
|
| 2078 |
# Create mixer based on type
|
| 2079 |
mixer_config = block_config.get("mixer", {"type": "attention"})
|
| 2080 |
+
raw_mixer = create_mixer(mixer_config, hidden_size, layer_idx, config, allow_stochastic=True)
|
| 2081 |
+
|
| 2082 |
+
# For pattern configs, wrap in adapter to match supernet checkpoint weight paths
|
| 2083 |
+
if block_name is not None:
|
| 2084 |
+
self.mixer = Apriel2PatternMixerAdapter(block_name, raw_mixer)
|
| 2085 |
+
else:
|
| 2086 |
+
self.mixer = raw_mixer
|
| 2087 |
|
| 2088 |
# Create MLP
|
| 2089 |
mlp_config = block_config.get("mlp", {"type": "mlp"})
|
|
|
|
| 2472 |
class Apriel2ForCausalLM(Apriel2PreTrainedModel, GenerationMixin):
|
| 2473 |
"""Apriel2 model with a language modeling head (text-only)."""
|
| 2474 |
|
| 2475 |
+
config_class = Apriel2Config
|
| 2476 |
_tied_weights_keys = ["lm_head.weight"]
|
| 2477 |
|
| 2478 |
def __init__(self, config: Apriel2TextConfig):
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|
|
@@ -5,6 +5,7 @@
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
|
|
|
| 8 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 9 |
},
|
| 10 |
"bos_token_id": 1,
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_apriel2.Apriel2Config",
|
| 7 |
"AutoModel": "modeling_apriel2.Apriel2Model",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_apriel2.Apriel2ForCausalLM",
|
| 9 |
"AutoModelForImageTextToText": "modeling_apriel2.Apriel2ForConditionalGeneration"
|
| 10 |
},
|
| 11 |
"bos_token_id": 1,
|