Text Generation
MLX
Safetensors
PyTorch
nemotron_labs_diffusion
nvidia
conversational
custom_code
4-bit precision
Instructions to use smdesai/Nemotron-Labs-Diffusion-3B-4bit with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use smdesai/Nemotron-Labs-Diffusion-3B-4bit with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("smdesai/Nemotron-Labs-Diffusion-3B-4bit") prompt = "Write a story about Einstein" messages = [{"role": "user", "content": prompt}] prompt = tokenizer.apply_chat_template( messages, add_generation_prompt=True ) text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- LM Studio
- Pi new
How to use smdesai/Nemotron-Labs-Diffusion-3B-4bit with Pi:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "smdesai/Nemotron-Labs-Diffusion-3B-4bit"
Configure the model in Pi
# Install Pi: npm install -g @mariozechner/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "mlx-lm": { "baseUrl": "http://localhost:8080/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "smdesai/Nemotron-Labs-Diffusion-3B-4bit" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Hermes Agent new
How to use smdesai/Nemotron-Labs-Diffusion-3B-4bit with Hermes Agent:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "smdesai/Nemotron-Labs-Diffusion-3B-4bit"
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:8080/v1 hermes config set model.default smdesai/Nemotron-Labs-Diffusion-3B-4bit
Run Hermes
hermes
- MLX LM
How to use smdesai/Nemotron-Labs-Diffusion-3B-4bit with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Interactive chat REPL mlx_lm.chat --model "smdesai/Nemotron-Labs-Diffusion-3B-4bit"
Run an OpenAI-compatible server
# Install MLX LM uv tool install mlx-lm # Start the server mlx_lm.server --model "smdesai/Nemotron-Labs-Diffusion-3B-4bit" # Calling the OpenAI-compatible server with curl curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "smdesai/Nemotron-Labs-Diffusion-3B-4bit", "messages": [ {"role": "user", "content": "Hello"} ] }'
Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- README.md +12 -0
- chat_template.jinja +197 -0
- config.json +59 -0
- configuration_nemotron_labs_diffusion.py +186 -0
- generation_config.json +7 -0
- model.safetensors +3 -0
- model.safetensors.index.json +613 -0
- modeling_ministral.py +459 -0
- modeling_nemotron_labs_diffusion.py +870 -0
- tokenizer.json +3 -0
- tokenizer_config.json +16 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* 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
|
|
|
|
|
|
| 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 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: mlx
|
| 3 |
+
license: other
|
| 4 |
+
license_name: nvidia-nemotron-open-model-license
|
| 5 |
+
license_link: https://www.nvidia.com/en-us/agreements/enterprise-software/nvidia-nemotron-open-model-license/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% macro render_extra_keys(json_dict, handled_keys) %}
|
| 2 |
+
{%- if json_dict is mapping %}
|
| 3 |
+
{%- for json_key in json_dict if json_key not in handled_keys %}
|
| 4 |
+
{%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}
|
| 5 |
+
{{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}
|
| 6 |
+
{%- else %}
|
| 7 |
+
{{-'\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}
|
| 8 |
+
{%- endif %}
|
| 9 |
+
{%- endfor %}
|
| 10 |
+
{%- endif %}
|
| 11 |
+
{% endmacro %}
|
| 12 |
+
{%- set enable_thinking = enable_thinking if enable_thinking is defined else False %}
|
| 13 |
+
{%- set truncate_history_thinking = truncate_history_thinking if truncate_history_thinking is defined else True %}
|
| 14 |
+
{%- set ns = namespace(last_user_idx = -1) %}
|
| 15 |
+
{%- set loop_messages = messages %}
|
| 16 |
+
{%- for m in loop_messages %}
|
| 17 |
+
{%- if m["role"] == "user" %}
|
| 18 |
+
{%- set ns.last_user_idx = loop.index0 %}
|
| 19 |
+
{%- endif %}
|
| 20 |
+
{%- endfor %}
|
| 21 |
+
{%- if messages[0]["role"] == "system" %}
|
| 22 |
+
{%- set system_message = messages[0]["content"] %}
|
| 23 |
+
{%- set loop_messages = messages[1:] %}
|
| 24 |
+
{%- else %}
|
| 25 |
+
{%- set system_message = "" %}
|
| 26 |
+
{%- set loop_messages = messages %}
|
| 27 |
+
{%- endif %}
|
| 28 |
+
{%- if not tools is defined %}
|
| 29 |
+
{%- set tools = [] %}
|
| 30 |
+
{%- endif %}
|
| 31 |
+
{# Recompute last_user_idx relative to loop_messages after handling system #}
|
| 32 |
+
{%- set ns = namespace(last_user_idx = -1) %}
|
| 33 |
+
{%- for m in loop_messages %}
|
| 34 |
+
{%- if m["role"] == "user" %}
|
| 35 |
+
{%- set ns.last_user_idx = loop.index0 %}
|
| 36 |
+
{%- endif %}
|
| 37 |
+
{%- endfor %}
|
| 38 |
+
{%- if system_message is defined %}
|
| 39 |
+
{{- "<|im_start|>system\n" + system_message }}
|
| 40 |
+
{%- else %}
|
| 41 |
+
{%- if tools is iterable and tools | length > 0 %}
|
| 42 |
+
{{- "<|im_start|>system\n" }}
|
| 43 |
+
{%- endif %}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{%- if tools is iterable and tools | length > 0 %}
|
| 46 |
+
{%- if system_message is defined and system_message | length > 0 %}
|
| 47 |
+
{{- "\n\n" }}
|
| 48 |
+
{%- endif %}
|
| 49 |
+
{{- "# Tools\n\nYou have access to the following functions:\n\n" }}
|
| 50 |
+
{{- "<tools>" }}
|
| 51 |
+
{%- for tool in tools %}
|
| 52 |
+
{%- if tool.function is defined %}
|
| 53 |
+
{%- set tool = tool.function %}
|
| 54 |
+
{%- endif %}
|
| 55 |
+
{{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
|
| 56 |
+
{%- if tool.description is defined %}
|
| 57 |
+
{{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
|
| 58 |
+
{%- endif %}
|
| 59 |
+
{{- '\n<parameters>' }}
|
| 60 |
+
{%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}
|
| 61 |
+
{%- for param_name, param_fields in tool.parameters.properties|items %}
|
| 62 |
+
{{- '\n<parameter>' }}
|
| 63 |
+
{{- '\n<name>' ~ param_name ~ '</name>' }}
|
| 64 |
+
{%- if param_fields.type is defined %}
|
| 65 |
+
{{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- if param_fields.description is defined %}
|
| 68 |
+
{{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
|
| 69 |
+
{%- endif %}
|
| 70 |
+
{%- if param_fields.enum is defined %}
|
| 71 |
+
{{- '\n<enum>' ~ (param_fields.enum | tojson | safe) ~ '</enum>' }}
|
| 72 |
+
{%- endif %}
|
| 73 |
+
{%- set handled_keys = ['name', 'type', 'description', 'enum'] %}
|
| 74 |
+
{{- render_extra_keys(param_fields, handled_keys) }}
|
| 75 |
+
{{- '\n</parameter>' }}
|
| 76 |
+
{%- endfor %}
|
| 77 |
+
{%- endif %}
|
| 78 |
+
{% set handled_keys = ['type', 'properties', 'required'] %}
|
| 79 |
+
{{- render_extra_keys(tool.parameters, handled_keys) }}
|
| 80 |
+
{%- if tool.parameters is defined and tool.parameters.required is defined %}
|
| 81 |
+
{{- '\n<required>' ~ (tool.parameters.required | tojson | safe) ~ '</required>' }}
|
| 82 |
+
{%- endif %}
|
| 83 |
+
{{- '\n</parameters>' }}
|
| 84 |
+
{%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
|
| 85 |
+
{{- render_extra_keys(tool, handled_keys) }}
|
| 86 |
+
{{- '\n</function>' }}
|
| 87 |
+
{%- endfor %}
|
| 88 |
+
{{- "\n</tools>" }}
|
| 89 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 90 |
+
{%- endif %}
|
| 91 |
+
{%- if system_message is defined %}
|
| 92 |
+
{{- '<|im_end|>\n' }}
|
| 93 |
+
{%- else %}
|
| 94 |
+
{%- if tools is iterable and tools | length > 0 %}
|
| 95 |
+
{{- '<|im_end|>\n' }}
|
| 96 |
+
{%- endif %}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- for message in loop_messages %}
|
| 99 |
+
{%- if message.role == "assistant" %}
|
| 100 |
+
{# Add reasoning content in to content field for unified processing below. #}
|
| 101 |
+
{%- if message.reasoning_content is defined and message.reasoning_content is string and message.reasoning_content | trim | length > 0 %}
|
| 102 |
+
{%- set content = "<think>\n" ~ message.reasoning_content ~ "\n</think>\n" ~ (message.content | default('', true)) %}
|
| 103 |
+
{%- else %}
|
| 104 |
+
{%- set content = message.content | default('', true) %}
|
| 105 |
+
{%- if content is string -%}
|
| 106 |
+
{# Allow downstream logic to to take care of broken thought, only handle coherent reasoning here. #}
|
| 107 |
+
{%- if '<think>' not in content and '</think>' not in content -%}
|
| 108 |
+
{%- set content = "<think></think>" ~ content -%}
|
| 109 |
+
{%- endif -%}
|
| 110 |
+
{%- else -%}
|
| 111 |
+
{%- set content = content -%}
|
| 112 |
+
{%- endif -%}
|
| 113 |
+
{%- endif %}
|
| 114 |
+
{%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}
|
| 115 |
+
{# Assistant message has tool calls. #}
|
| 116 |
+
{{- '<|im_start|>assistant\n' }}
|
| 117 |
+
{%- set include_content = not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
|
| 118 |
+
{%- if content is string and content | trim | length > 0 %}
|
| 119 |
+
{%- if include_content %}
|
| 120 |
+
{{- (content | trim) ~ '\n' -}}
|
| 121 |
+
{%- else %}
|
| 122 |
+
{%- set c = (content | string) %}
|
| 123 |
+
{%- if '</think>' in c %}
|
| 124 |
+
{# Keep only content after the last closing think. Also generation prompt causes this. #}
|
| 125 |
+
{%- set c = c.split('</think>')[-1] %}
|
| 126 |
+
{%- elif '<think>' in c %}
|
| 127 |
+
{# If <think> was opened but never closed, drop the trailing think segment #}
|
| 128 |
+
{%- set c = c.split('<think>')[0] %}
|
| 129 |
+
{%- endif %}
|
| 130 |
+
{%- set c = "<think></think>" ~ c | trim %}
|
| 131 |
+
{%- if c | length > 0 %}
|
| 132 |
+
{{- c ~ '\n' -}}
|
| 133 |
+
{%- endif %}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{%- else %}
|
| 136 |
+
{{- "<think></think>" -}}
|
| 137 |
+
{%- endif %}
|
| 138 |
+
{%- for tool_call in message.tool_calls %}
|
| 139 |
+
{%- if tool_call.function is defined %}
|
| 140 |
+
{%- set tool_call = tool_call.function %}
|
| 141 |
+
{%- endif %}
|
| 142 |
+
{{- '<tool_call>\n<function=' ~ tool_call.name ~ '>\n' -}}
|
| 143 |
+
{%- if tool_call.arguments is defined %}
|
| 144 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 145 |
+
{{- '<parameter=' ~ args_name ~ '>\n' -}}
|
| 146 |
+
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
|
| 147 |
+
{{- args_value ~ '\n</parameter>\n' -}}
|
| 148 |
+
{%- endfor %}
|
| 149 |
+
{%- endif %}
|
| 150 |
+
{{- '</function>\n</tool_call>\n' -}}
|
| 151 |
+
{%- endfor %}
|
| 152 |
+
{{- '<|im_end|>\n' }}
|
| 153 |
+
{%- else %}
|
| 154 |
+
{# Assistant message doesn't have tool calls. #}
|
| 155 |
+
{%- if not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
|
| 156 |
+
{{- '<|im_start|>assistant\n' ~ (content | default('', true) | string | trim) ~ '<|im_end|>\n' }}
|
| 157 |
+
{%- else %}
|
| 158 |
+
{%- set c = (content | default('', true) | string) %}
|
| 159 |
+
{%- if '<think>' in c and '</think>' in c %}
|
| 160 |
+
{%- set c = "<think></think>" ~ c.split('</think>')[-1] %}
|
| 161 |
+
{%- endif %}
|
| 162 |
+
{%- set c = c | trim %}
|
| 163 |
+
{%- if c | length > 0 %}
|
| 164 |
+
{{- '<|im_start|>assistant\n' ~ c ~ '<|im_end|>\n' }}
|
| 165 |
+
{%- else %}
|
| 166 |
+
{{- '<|im_start|>assistant\n<|im_end|>\n' }}
|
| 167 |
+
{%- endif %}
|
| 168 |
+
{%- endif %}
|
| 169 |
+
{%- endif %}
|
| 170 |
+
{%- elif message.role == "user" or message.role == "system" %}
|
| 171 |
+
{{- '<|im_start|>' + message.role + '\n' }}
|
| 172 |
+
{%- set content = message.content | string %}
|
| 173 |
+
{{- content }}
|
| 174 |
+
{{- '<|im_end|>\n' }}
|
| 175 |
+
{%- elif message.role == "tool" %}
|
| 176 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 177 |
+
{{- '<|im_start|>user\n' }}
|
| 178 |
+
{%- endif %}
|
| 179 |
+
{{- '<tool_response>\n' }}
|
| 180 |
+
{{- message.content }}
|
| 181 |
+
{{- '\n</tool_response>\n' }}
|
| 182 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 183 |
+
{{- '<|im_end|>\n' }}
|
| 184 |
+
{%- elif loop.last %}
|
| 185 |
+
{{- '<|im_end|>\n' }}
|
| 186 |
+
{%- endif %}
|
| 187 |
+
{%- else %}
|
| 188 |
+
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' }}
|
| 189 |
+
{%- endif %}
|
| 190 |
+
{%- endfor %}
|
| 191 |
+
{%- if add_generation_prompt %}
|
| 192 |
+
{%- if enable_thinking %}
|
| 193 |
+
{{- '<|im_start|>assistant\n<think>\n' }}
|
| 194 |
+
{%- else %}
|
| 195 |
+
{{- '<|im_start|>assistant\n<think></think>' }}
|
| 196 |
+
{%- endif %}
|
| 197 |
+
{%- endif %}
|
config.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"ar_loss_weight": 1.0,
|
| 3 |
+
"architectures": [
|
| 4 |
+
"NemotronLabsDiffusionModel"
|
| 5 |
+
],
|
| 6 |
+
"attention_bias": false,
|
| 7 |
+
"attention_dropout": 0.0,
|
| 8 |
+
"attn_implementation": "sdpa",
|
| 9 |
+
"auto_map": {
|
| 10 |
+
"AutoConfig": "configuration_nemotron_labs_diffusion.NemotronLabsDiffusionConfig",
|
| 11 |
+
"AutoModel": "modeling_nemotron_labs_diffusion.NemotronLabsDiffusionModel"
|
| 12 |
+
},
|
| 13 |
+
"block_size": 32,
|
| 14 |
+
"bos_token_id": 1,
|
| 15 |
+
"dlm_loss_weight": null,
|
| 16 |
+
"dlm_paradigm": "bidirectional",
|
| 17 |
+
"dp_varying_mask_ratio": false,
|
| 18 |
+
"eos_token_id": 11,
|
| 19 |
+
"head_dim": 128,
|
| 20 |
+
"hidden_act": "silu",
|
| 21 |
+
"hidden_size": 3072,
|
| 22 |
+
"initializer_range": 0.02,
|
| 23 |
+
"intermediate_size": 9216,
|
| 24 |
+
"mask_token_id": 100,
|
| 25 |
+
"max_position_embeddings": 262144,
|
| 26 |
+
"mlp_bias": false,
|
| 27 |
+
"model_type": "nemotron_labs_diffusion",
|
| 28 |
+
"num_attention_heads": 32,
|
| 29 |
+
"num_hidden_layers": 26,
|
| 30 |
+
"num_key_value_heads": 8,
|
| 31 |
+
"quantization": {
|
| 32 |
+
"group_size": 64,
|
| 33 |
+
"bits": 4,
|
| 34 |
+
"mode": "affine"
|
| 35 |
+
},
|
| 36 |
+
"quantization_config": {
|
| 37 |
+
"group_size": 64,
|
| 38 |
+
"bits": 4,
|
| 39 |
+
"mode": "affine"
|
| 40 |
+
},
|
| 41 |
+
"rms_norm_eps": 1e-05,
|
| 42 |
+
"rope_parameters": {
|
| 43 |
+
"beta_fast": 32.0,
|
| 44 |
+
"beta_slow": 1.0,
|
| 45 |
+
"factor": 16.0,
|
| 46 |
+
"llama_4_scaling_beta": 0.1,
|
| 47 |
+
"mscale": 1.0,
|
| 48 |
+
"mscale_all_dim": 1.0,
|
| 49 |
+
"original_max_position_embeddings": 16384,
|
| 50 |
+
"rope_theta": 1000000.0,
|
| 51 |
+
"rope_type": "yarn"
|
| 52 |
+
},
|
| 53 |
+
"sliding_window": null,
|
| 54 |
+
"tie_word_embeddings": false,
|
| 55 |
+
"torch_dtype": "bfloat16",
|
| 56 |
+
"transformers_version": "5.0.0",
|
| 57 |
+
"use_cache": false,
|
| 58 |
+
"vocab_size": 131072
|
| 59 |
+
}
|
configuration_nemotron_labs_diffusion.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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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 |
+
"""Nemotron-Labs Diffusion model configuration"""
|
| 16 |
+
|
| 17 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 18 |
+
from transformers.modeling_rope_utils import rope_config_validation
|
| 19 |
+
from transformers.utils import logging
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
logger = logging.get_logger(__name__)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class NemotronLabsDiffusionConfig(PretrainedConfig):
|
| 26 |
+
r"""
|
| 27 |
+
This is the configuration class to store the configuration of a [`NemotronLabsDiffusionModel`] for diffusion language models.
|
| 28 |
+
It is used to instantiate a NemotronLabsDiffusionModel according to the specified arguments, defining the model architecture.
|
| 29 |
+
|
| 30 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 31 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
vocab_size (`int`, *optional*, defaults to 131072):
|
| 35 |
+
Vocabulary size of the Ministral model.
|
| 36 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
| 37 |
+
Dimension of the hidden representations.
|
| 38 |
+
intermediate_size (`int`, *optional*, defaults to 14336):
|
| 39 |
+
Dimension of the MLP representations.
|
| 40 |
+
num_hidden_layers (`int`, *optional*, defaults to 34):
|
| 41 |
+
Number of hidden layers in the Transformer decoder.
|
| 42 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 43 |
+
Number of attention heads for each attention layer.
|
| 44 |
+
num_key_value_heads (`int`, *optional*, defaults to 8):
|
| 45 |
+
Number of key_value heads for Grouped Query Attention.
|
| 46 |
+
head_dim (`int`, *optional*, defaults to 128):
|
| 47 |
+
The attention head dimension.
|
| 48 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 49 |
+
The non-linear activation function.
|
| 50 |
+
max_position_embeddings (`int`, *optional*, defaults to 262144):
|
| 51 |
+
The maximum sequence length.
|
| 52 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 53 |
+
The standard deviation of the truncated_normal_initializer.
|
| 54 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-05):
|
| 55 |
+
The epsilon used by the rms normalization layers.
|
| 56 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 57 |
+
Whether or not the model should return the last key/values attentions.
|
| 58 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
| 59 |
+
Whether the model's input and output word embeddings should be tied.
|
| 60 |
+
rope_theta (`float`, *optional*, defaults to 1000000.0):
|
| 61 |
+
The base period of the RoPE embeddings.
|
| 62 |
+
rope_parameters (`Dict`, *optional*):
|
| 63 |
+
Dictionary containing the scaling configuration for the RoPE embeddings.
|
| 64 |
+
Default uses YaRN scaling with factor=16, original_max_position_embeddings=16384.
|
| 65 |
+
attention_bias (`bool`, defaults to `False`):
|
| 66 |
+
Whether to use a bias in the query, key, value and output projection layers.
|
| 67 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 68 |
+
The dropout ratio for the attention probabilities.
|
| 69 |
+
mlp_bias (`bool`, *optional*, defaults to `False`):
|
| 70 |
+
Whether to use a bias in up_proj, down_proj and gate_proj layers.
|
| 71 |
+
sliding_window (`int`, *optional*, defaults to None):
|
| 72 |
+
Sliding window attention size.
|
| 73 |
+
mask_token_id (`int`, *optional*, defaults to -1):
|
| 74 |
+
Token ID for masking in diffusion.
|
| 75 |
+
dlm_paradigm (`str`, *optional*, defaults to 'bidirectional'):
|
| 76 |
+
Paradigm for diffusion ('bidirectional', 'autoregressive', 'block_diff').
|
| 77 |
+
block_size (`int`, *optional*, defaults to 32):
|
| 78 |
+
Block size for block diffusion paradigms.
|
| 79 |
+
dlm_loss_weight (`float`, *optional*):
|
| 80 |
+
Weight for diffusion LM loss.
|
| 81 |
+
ar_loss_weight (`float`, *optional*, defaults to 1.0):
|
| 82 |
+
Weight for autoregressive loss in block_diff paradigm. Use 10000 to only use AR loss.
|
| 83 |
+
dp_varying_mask_ratio (`bool`, *optional*, defaults to False):
|
| 84 |
+
Whether to use varying mask ratio for each DP rank during sampling.
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
model_type = "nemotron_labs_diffusion"
|
| 88 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 89 |
+
|
| 90 |
+
# Default tensor parallel plan for base model `Ministral`
|
| 91 |
+
base_model_tp_plan = {
|
| 92 |
+
"layers.*.self_attn.q_proj": "colwise",
|
| 93 |
+
"layers.*.self_attn.k_proj": "colwise",
|
| 94 |
+
"layers.*.self_attn.v_proj": "colwise",
|
| 95 |
+
"layers.*.self_attn.o_proj": "rowwise",
|
| 96 |
+
"layers.*.mlp.gate_proj": "colwise",
|
| 97 |
+
"layers.*.mlp.up_proj": "colwise",
|
| 98 |
+
"layers.*.mlp.down_proj": "rowwise",
|
| 99 |
+
}
|
| 100 |
+
base_model_pp_plan = {
|
| 101 |
+
"embed_tokens": (["input_ids"], ["inputs_embeds"]),
|
| 102 |
+
"layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
|
| 103 |
+
"norm": (["hidden_states"], ["hidden_states"]),
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
def __init__(
|
| 107 |
+
self,
|
| 108 |
+
vocab_size=131072,
|
| 109 |
+
hidden_size=4096,
|
| 110 |
+
intermediate_size=14336,
|
| 111 |
+
num_hidden_layers=34,
|
| 112 |
+
num_attention_heads=32,
|
| 113 |
+
num_key_value_heads=8,
|
| 114 |
+
head_dim=128,
|
| 115 |
+
hidden_act="silu",
|
| 116 |
+
max_position_embeddings=262144,
|
| 117 |
+
initializer_range=0.02,
|
| 118 |
+
rms_norm_eps=1e-05,
|
| 119 |
+
use_cache=True,
|
| 120 |
+
pad_token_id=None,
|
| 121 |
+
bos_token_id=1,
|
| 122 |
+
eos_token_id=2,
|
| 123 |
+
tie_word_embeddings=False,
|
| 124 |
+
rope_theta=1000000.0,
|
| 125 |
+
rope_parameters=None,
|
| 126 |
+
attention_bias=False,
|
| 127 |
+
attention_dropout=0.0,
|
| 128 |
+
mlp_bias=False,
|
| 129 |
+
sliding_window=None,
|
| 130 |
+
attn_implementation="sdpa",
|
| 131 |
+
mask_token_id=-1,
|
| 132 |
+
dlm_paradigm='bidirectional',
|
| 133 |
+
block_size=32,
|
| 134 |
+
dlm_loss_weight=None,
|
| 135 |
+
ar_loss_weight=1.0,
|
| 136 |
+
dp_varying_mask_ratio=False,
|
| 137 |
+
**kwargs,
|
| 138 |
+
):
|
| 139 |
+
self.vocab_size = vocab_size
|
| 140 |
+
self.max_position_embeddings = max_position_embeddings
|
| 141 |
+
self.hidden_size = hidden_size
|
| 142 |
+
self.intermediate_size = intermediate_size
|
| 143 |
+
self.num_hidden_layers = num_hidden_layers
|
| 144 |
+
self.num_attention_heads = num_attention_heads
|
| 145 |
+
|
| 146 |
+
# for backward compatibility
|
| 147 |
+
if num_key_value_heads is None:
|
| 148 |
+
num_key_value_heads = num_attention_heads
|
| 149 |
+
|
| 150 |
+
self.num_key_value_heads = num_key_value_heads
|
| 151 |
+
self.head_dim = head_dim
|
| 152 |
+
self.hidden_act = hidden_act
|
| 153 |
+
self.initializer_range = initializer_range
|
| 154 |
+
self.rms_norm_eps = rms_norm_eps
|
| 155 |
+
self.use_cache = use_cache
|
| 156 |
+
self.rope_parameters = rope_parameters
|
| 157 |
+
# `rope_theta` is read at the top level by transformers v4.55's yarn impl; mirror from rope_parameters when present.
|
| 158 |
+
self.rope_theta = (rope_parameters or {}).get("rope_theta", rope_theta)
|
| 159 |
+
# v4.55 reads rope params from `rope_scaling`; in v5.0 `rope_scaling` is a property alias for rope_parameters.
|
| 160 |
+
self.rope_scaling = rope_parameters
|
| 161 |
+
self.attention_bias = attention_bias
|
| 162 |
+
self.attention_dropout = attention_dropout
|
| 163 |
+
self.mlp_bias = mlp_bias
|
| 164 |
+
self.sliding_window = sliding_window
|
| 165 |
+
|
| 166 |
+
rope_config_validation(self)
|
| 167 |
+
|
| 168 |
+
self.attn_implementation = attn_implementation
|
| 169 |
+
|
| 170 |
+
self.mask_token_id = mask_token_id
|
| 171 |
+
self.dlm_paradigm = dlm_paradigm
|
| 172 |
+
self.block_size = block_size
|
| 173 |
+
self.dlm_loss_weight = dlm_loss_weight
|
| 174 |
+
self.ar_loss_weight = ar_loss_weight
|
| 175 |
+
self.dp_varying_mask_ratio = dp_varying_mask_ratio
|
| 176 |
+
super().__init__(
|
| 177 |
+
pad_token_id=pad_token_id,
|
| 178 |
+
bos_token_id=bos_token_id,
|
| 179 |
+
eos_token_id=eos_token_id,
|
| 180 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 181 |
+
**kwargs,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
__all__ = ["NemotronLabsDiffusionConfig"]
|
| 186 |
+
|
generation_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": 11,
|
| 5 |
+
"transformers_version": "5.0.0",
|
| 6 |
+
"use_cache": false
|
| 7 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5e24d9a9805079193511e1bc01592565a23e5ff6c0687860138883a355bfcc3f
|
| 3 |
+
size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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"metadata": {
|
| 3 |
+
"total_size": 2155542528,
|
| 4 |
+
"total_parameters": 3831659520
|
| 5 |
+
},
|
| 6 |
+
"weight_map": {
|
| 7 |
+
"diffusion_head.biases": "model.safetensors",
|
| 8 |
+
"diffusion_head.scales": "model.safetensors",
|
| 9 |
+
"diffusion_head.weight": "model.safetensors",
|
| 10 |
+
"encoder.embed_tokens.biases": "model.safetensors",
|
| 11 |
+
"encoder.embed_tokens.scales": "model.safetensors",
|
| 12 |
+
"encoder.embed_tokens.weight": "model.safetensors",
|
| 13 |
+
"encoder.layers.0.input_layernorm.weight": "model.safetensors",
|
| 14 |
+
"encoder.layers.0.mlp.down_proj.biases": "model.safetensors",
|
| 15 |
+
"encoder.layers.0.mlp.down_proj.scales": "model.safetensors",
|
| 16 |
+
"encoder.layers.0.mlp.down_proj.weight": "model.safetensors",
|
| 17 |
+
"encoder.layers.0.mlp.gate_proj.biases": "model.safetensors",
|
| 18 |
+
"encoder.layers.0.mlp.gate_proj.scales": "model.safetensors",
|
| 19 |
+
"encoder.layers.0.mlp.gate_proj.weight": "model.safetensors",
|
| 20 |
+
"encoder.layers.0.mlp.up_proj.biases": "model.safetensors",
|
| 21 |
+
"encoder.layers.0.mlp.up_proj.scales": "model.safetensors",
|
| 22 |
+
"encoder.layers.0.mlp.up_proj.weight": "model.safetensors",
|
| 23 |
+
"encoder.layers.0.post_attention_layernorm.weight": "model.safetensors",
|
| 24 |
+
"encoder.layers.0.self_attn.k_proj.biases": "model.safetensors",
|
| 25 |
+
"encoder.layers.0.self_attn.k_proj.scales": "model.safetensors",
|
| 26 |
+
"encoder.layers.0.self_attn.k_proj.weight": "model.safetensors",
|
| 27 |
+
"encoder.layers.0.self_attn.o_proj.biases": "model.safetensors",
|
| 28 |
+
"encoder.layers.0.self_attn.o_proj.scales": "model.safetensors",
|
| 29 |
+
"encoder.layers.0.self_attn.o_proj.weight": "model.safetensors",
|
| 30 |
+
"encoder.layers.0.self_attn.q_proj.biases": "model.safetensors",
|
| 31 |
+
"encoder.layers.0.self_attn.q_proj.scales": "model.safetensors",
|
| 32 |
+
"encoder.layers.0.self_attn.q_proj.weight": "model.safetensors",
|
| 33 |
+
"encoder.layers.0.self_attn.v_proj.biases": "model.safetensors",
|
| 34 |
+
"encoder.layers.0.self_attn.v_proj.scales": "model.safetensors",
|
| 35 |
+
"encoder.layers.0.self_attn.v_proj.weight": "model.safetensors",
|
| 36 |
+
"encoder.layers.1.input_layernorm.weight": "model.safetensors",
|
| 37 |
+
"encoder.layers.1.mlp.down_proj.biases": "model.safetensors",
|
| 38 |
+
"encoder.layers.1.mlp.down_proj.scales": "model.safetensors",
|
| 39 |
+
"encoder.layers.1.mlp.down_proj.weight": "model.safetensors",
|
| 40 |
+
"encoder.layers.1.mlp.gate_proj.biases": "model.safetensors",
|
| 41 |
+
"encoder.layers.1.mlp.gate_proj.scales": "model.safetensors",
|
| 42 |
+
"encoder.layers.1.mlp.gate_proj.weight": "model.safetensors",
|
| 43 |
+
"encoder.layers.1.mlp.up_proj.biases": "model.safetensors",
|
| 44 |
+
"encoder.layers.1.mlp.up_proj.scales": "model.safetensors",
|
| 45 |
+
"encoder.layers.1.mlp.up_proj.weight": "model.safetensors",
|
| 46 |
+
"encoder.layers.1.post_attention_layernorm.weight": "model.safetensors",
|
| 47 |
+
"encoder.layers.1.self_attn.k_proj.biases": "model.safetensors",
|
| 48 |
+
"encoder.layers.1.self_attn.k_proj.scales": "model.safetensors",
|
| 49 |
+
"encoder.layers.1.self_attn.k_proj.weight": "model.safetensors",
|
| 50 |
+
"encoder.layers.1.self_attn.o_proj.biases": "model.safetensors",
|
| 51 |
+
"encoder.layers.1.self_attn.o_proj.scales": "model.safetensors",
|
| 52 |
+
"encoder.layers.1.self_attn.o_proj.weight": "model.safetensors",
|
| 53 |
+
"encoder.layers.1.self_attn.q_proj.biases": "model.safetensors",
|
| 54 |
+
"encoder.layers.1.self_attn.q_proj.scales": "model.safetensors",
|
| 55 |
+
"encoder.layers.1.self_attn.q_proj.weight": "model.safetensors",
|
| 56 |
+
"encoder.layers.1.self_attn.v_proj.biases": "model.safetensors",
|
| 57 |
+
"encoder.layers.1.self_attn.v_proj.scales": "model.safetensors",
|
| 58 |
+
"encoder.layers.1.self_attn.v_proj.weight": "model.safetensors",
|
| 59 |
+
"encoder.layers.10.input_layernorm.weight": "model.safetensors",
|
| 60 |
+
"encoder.layers.10.mlp.down_proj.biases": "model.safetensors",
|
| 61 |
+
"encoder.layers.10.mlp.down_proj.scales": "model.safetensors",
|
| 62 |
+
"encoder.layers.10.mlp.down_proj.weight": "model.safetensors",
|
| 63 |
+
"encoder.layers.10.mlp.gate_proj.biases": "model.safetensors",
|
| 64 |
+
"encoder.layers.10.mlp.gate_proj.scales": "model.safetensors",
|
| 65 |
+
"encoder.layers.10.mlp.gate_proj.weight": "model.safetensors",
|
| 66 |
+
"encoder.layers.10.mlp.up_proj.biases": "model.safetensors",
|
| 67 |
+
"encoder.layers.10.mlp.up_proj.scales": "model.safetensors",
|
| 68 |
+
"encoder.layers.10.mlp.up_proj.weight": "model.safetensors",
|
| 69 |
+
"encoder.layers.10.post_attention_layernorm.weight": "model.safetensors",
|
| 70 |
+
"encoder.layers.10.self_attn.k_proj.biases": "model.safetensors",
|
| 71 |
+
"encoder.layers.10.self_attn.k_proj.scales": "model.safetensors",
|
| 72 |
+
"encoder.layers.10.self_attn.k_proj.weight": "model.safetensors",
|
| 73 |
+
"encoder.layers.10.self_attn.o_proj.biases": "model.safetensors",
|
| 74 |
+
"encoder.layers.10.self_attn.o_proj.scales": "model.safetensors",
|
| 75 |
+
"encoder.layers.10.self_attn.o_proj.weight": "model.safetensors",
|
| 76 |
+
"encoder.layers.10.self_attn.q_proj.biases": "model.safetensors",
|
| 77 |
+
"encoder.layers.10.self_attn.q_proj.scales": "model.safetensors",
|
| 78 |
+
"encoder.layers.10.self_attn.q_proj.weight": "model.safetensors",
|
| 79 |
+
"encoder.layers.10.self_attn.v_proj.biases": "model.safetensors",
|
| 80 |
+
"encoder.layers.10.self_attn.v_proj.scales": "model.safetensors",
|
| 81 |
+
"encoder.layers.10.self_attn.v_proj.weight": "model.safetensors",
|
| 82 |
+
"encoder.layers.11.input_layernorm.weight": "model.safetensors",
|
| 83 |
+
"encoder.layers.11.mlp.down_proj.biases": "model.safetensors",
|
| 84 |
+
"encoder.layers.11.mlp.down_proj.scales": "model.safetensors",
|
| 85 |
+
"encoder.layers.11.mlp.down_proj.weight": "model.safetensors",
|
| 86 |
+
"encoder.layers.11.mlp.gate_proj.biases": "model.safetensors",
|
| 87 |
+
"encoder.layers.11.mlp.gate_proj.scales": "model.safetensors",
|
| 88 |
+
"encoder.layers.11.mlp.gate_proj.weight": "model.safetensors",
|
| 89 |
+
"encoder.layers.11.mlp.up_proj.biases": "model.safetensors",
|
| 90 |
+
"encoder.layers.11.mlp.up_proj.scales": "model.safetensors",
|
| 91 |
+
"encoder.layers.11.mlp.up_proj.weight": "model.safetensors",
|
| 92 |
+
"encoder.layers.11.post_attention_layernorm.weight": "model.safetensors",
|
| 93 |
+
"encoder.layers.11.self_attn.k_proj.biases": "model.safetensors",
|
| 94 |
+
"encoder.layers.11.self_attn.k_proj.scales": "model.safetensors",
|
| 95 |
+
"encoder.layers.11.self_attn.k_proj.weight": "model.safetensors",
|
| 96 |
+
"encoder.layers.11.self_attn.o_proj.biases": "model.safetensors",
|
| 97 |
+
"encoder.layers.11.self_attn.o_proj.scales": "model.safetensors",
|
| 98 |
+
"encoder.layers.11.self_attn.o_proj.weight": "model.safetensors",
|
| 99 |
+
"encoder.layers.11.self_attn.q_proj.biases": "model.safetensors",
|
| 100 |
+
"encoder.layers.11.self_attn.q_proj.scales": "model.safetensors",
|
| 101 |
+
"encoder.layers.11.self_attn.q_proj.weight": "model.safetensors",
|
| 102 |
+
"encoder.layers.11.self_attn.v_proj.biases": "model.safetensors",
|
| 103 |
+
"encoder.layers.11.self_attn.v_proj.scales": "model.safetensors",
|
| 104 |
+
"encoder.layers.11.self_attn.v_proj.weight": "model.safetensors",
|
| 105 |
+
"encoder.layers.12.input_layernorm.weight": "model.safetensors",
|
| 106 |
+
"encoder.layers.12.mlp.down_proj.biases": "model.safetensors",
|
| 107 |
+
"encoder.layers.12.mlp.down_proj.scales": "model.safetensors",
|
| 108 |
+
"encoder.layers.12.mlp.down_proj.weight": "model.safetensors",
|
| 109 |
+
"encoder.layers.12.mlp.gate_proj.biases": "model.safetensors",
|
| 110 |
+
"encoder.layers.12.mlp.gate_proj.scales": "model.safetensors",
|
| 111 |
+
"encoder.layers.12.mlp.gate_proj.weight": "model.safetensors",
|
| 112 |
+
"encoder.layers.12.mlp.up_proj.biases": "model.safetensors",
|
| 113 |
+
"encoder.layers.12.mlp.up_proj.scales": "model.safetensors",
|
| 114 |
+
"encoder.layers.12.mlp.up_proj.weight": "model.safetensors",
|
| 115 |
+
"encoder.layers.12.post_attention_layernorm.weight": "model.safetensors",
|
| 116 |
+
"encoder.layers.12.self_attn.k_proj.biases": "model.safetensors",
|
| 117 |
+
"encoder.layers.12.self_attn.k_proj.scales": "model.safetensors",
|
| 118 |
+
"encoder.layers.12.self_attn.k_proj.weight": "model.safetensors",
|
| 119 |
+
"encoder.layers.12.self_attn.o_proj.biases": "model.safetensors",
|
| 120 |
+
"encoder.layers.12.self_attn.o_proj.scales": "model.safetensors",
|
| 121 |
+
"encoder.layers.12.self_attn.o_proj.weight": "model.safetensors",
|
| 122 |
+
"encoder.layers.12.self_attn.q_proj.biases": "model.safetensors",
|
| 123 |
+
"encoder.layers.12.self_attn.q_proj.scales": "model.safetensors",
|
| 124 |
+
"encoder.layers.12.self_attn.q_proj.weight": "model.safetensors",
|
| 125 |
+
"encoder.layers.12.self_attn.v_proj.biases": "model.safetensors",
|
| 126 |
+
"encoder.layers.12.self_attn.v_proj.scales": "model.safetensors",
|
| 127 |
+
"encoder.layers.12.self_attn.v_proj.weight": "model.safetensors",
|
| 128 |
+
"encoder.layers.13.input_layernorm.weight": "model.safetensors",
|
| 129 |
+
"encoder.layers.13.mlp.down_proj.biases": "model.safetensors",
|
| 130 |
+
"encoder.layers.13.mlp.down_proj.scales": "model.safetensors",
|
| 131 |
+
"encoder.layers.13.mlp.down_proj.weight": "model.safetensors",
|
| 132 |
+
"encoder.layers.13.mlp.gate_proj.biases": "model.safetensors",
|
| 133 |
+
"encoder.layers.13.mlp.gate_proj.scales": "model.safetensors",
|
| 134 |
+
"encoder.layers.13.mlp.gate_proj.weight": "model.safetensors",
|
| 135 |
+
"encoder.layers.13.mlp.up_proj.biases": "model.safetensors",
|
| 136 |
+
"encoder.layers.13.mlp.up_proj.scales": "model.safetensors",
|
| 137 |
+
"encoder.layers.13.mlp.up_proj.weight": "model.safetensors",
|
| 138 |
+
"encoder.layers.13.post_attention_layernorm.weight": "model.safetensors",
|
| 139 |
+
"encoder.layers.13.self_attn.k_proj.biases": "model.safetensors",
|
| 140 |
+
"encoder.layers.13.self_attn.k_proj.scales": "model.safetensors",
|
| 141 |
+
"encoder.layers.13.self_attn.k_proj.weight": "model.safetensors",
|
| 142 |
+
"encoder.layers.13.self_attn.o_proj.biases": "model.safetensors",
|
| 143 |
+
"encoder.layers.13.self_attn.o_proj.scales": "model.safetensors",
|
| 144 |
+
"encoder.layers.13.self_attn.o_proj.weight": "model.safetensors",
|
| 145 |
+
"encoder.layers.13.self_attn.q_proj.biases": "model.safetensors",
|
| 146 |
+
"encoder.layers.13.self_attn.q_proj.scales": "model.safetensors",
|
| 147 |
+
"encoder.layers.13.self_attn.q_proj.weight": "model.safetensors",
|
| 148 |
+
"encoder.layers.13.self_attn.v_proj.biases": "model.safetensors",
|
| 149 |
+
"encoder.layers.13.self_attn.v_proj.scales": "model.safetensors",
|
| 150 |
+
"encoder.layers.13.self_attn.v_proj.weight": "model.safetensors",
|
| 151 |
+
"encoder.layers.14.input_layernorm.weight": "model.safetensors",
|
| 152 |
+
"encoder.layers.14.mlp.down_proj.biases": "model.safetensors",
|
| 153 |
+
"encoder.layers.14.mlp.down_proj.scales": "model.safetensors",
|
| 154 |
+
"encoder.layers.14.mlp.down_proj.weight": "model.safetensors",
|
| 155 |
+
"encoder.layers.14.mlp.gate_proj.biases": "model.safetensors",
|
| 156 |
+
"encoder.layers.14.mlp.gate_proj.scales": "model.safetensors",
|
| 157 |
+
"encoder.layers.14.mlp.gate_proj.weight": "model.safetensors",
|
| 158 |
+
"encoder.layers.14.mlp.up_proj.biases": "model.safetensors",
|
| 159 |
+
"encoder.layers.14.mlp.up_proj.scales": "model.safetensors",
|
| 160 |
+
"encoder.layers.14.mlp.up_proj.weight": "model.safetensors",
|
| 161 |
+
"encoder.layers.14.post_attention_layernorm.weight": "model.safetensors",
|
| 162 |
+
"encoder.layers.14.self_attn.k_proj.biases": "model.safetensors",
|
| 163 |
+
"encoder.layers.14.self_attn.k_proj.scales": "model.safetensors",
|
| 164 |
+
"encoder.layers.14.self_attn.k_proj.weight": "model.safetensors",
|
| 165 |
+
"encoder.layers.14.self_attn.o_proj.biases": "model.safetensors",
|
| 166 |
+
"encoder.layers.14.self_attn.o_proj.scales": "model.safetensors",
|
| 167 |
+
"encoder.layers.14.self_attn.o_proj.weight": "model.safetensors",
|
| 168 |
+
"encoder.layers.14.self_attn.q_proj.biases": "model.safetensors",
|
| 169 |
+
"encoder.layers.14.self_attn.q_proj.scales": "model.safetensors",
|
| 170 |
+
"encoder.layers.14.self_attn.q_proj.weight": "model.safetensors",
|
| 171 |
+
"encoder.layers.14.self_attn.v_proj.biases": "model.safetensors",
|
| 172 |
+
"encoder.layers.14.self_attn.v_proj.scales": "model.safetensors",
|
| 173 |
+
"encoder.layers.14.self_attn.v_proj.weight": "model.safetensors",
|
| 174 |
+
"encoder.layers.15.input_layernorm.weight": "model.safetensors",
|
| 175 |
+
"encoder.layers.15.mlp.down_proj.biases": "model.safetensors",
|
| 176 |
+
"encoder.layers.15.mlp.down_proj.scales": "model.safetensors",
|
| 177 |
+
"encoder.layers.15.mlp.down_proj.weight": "model.safetensors",
|
| 178 |
+
"encoder.layers.15.mlp.gate_proj.biases": "model.safetensors",
|
| 179 |
+
"encoder.layers.15.mlp.gate_proj.scales": "model.safetensors",
|
| 180 |
+
"encoder.layers.15.mlp.gate_proj.weight": "model.safetensors",
|
| 181 |
+
"encoder.layers.15.mlp.up_proj.biases": "model.safetensors",
|
| 182 |
+
"encoder.layers.15.mlp.up_proj.scales": "model.safetensors",
|
| 183 |
+
"encoder.layers.15.mlp.up_proj.weight": "model.safetensors",
|
| 184 |
+
"encoder.layers.15.post_attention_layernorm.weight": "model.safetensors",
|
| 185 |
+
"encoder.layers.15.self_attn.k_proj.biases": "model.safetensors",
|
| 186 |
+
"encoder.layers.15.self_attn.k_proj.scales": "model.safetensors",
|
| 187 |
+
"encoder.layers.15.self_attn.k_proj.weight": "model.safetensors",
|
| 188 |
+
"encoder.layers.15.self_attn.o_proj.biases": "model.safetensors",
|
| 189 |
+
"encoder.layers.15.self_attn.o_proj.scales": "model.safetensors",
|
| 190 |
+
"encoder.layers.15.self_attn.o_proj.weight": "model.safetensors",
|
| 191 |
+
"encoder.layers.15.self_attn.q_proj.biases": "model.safetensors",
|
| 192 |
+
"encoder.layers.15.self_attn.q_proj.scales": "model.safetensors",
|
| 193 |
+
"encoder.layers.15.self_attn.q_proj.weight": "model.safetensors",
|
| 194 |
+
"encoder.layers.15.self_attn.v_proj.biases": "model.safetensors",
|
| 195 |
+
"encoder.layers.15.self_attn.v_proj.scales": "model.safetensors",
|
| 196 |
+
"encoder.layers.15.self_attn.v_proj.weight": "model.safetensors",
|
| 197 |
+
"encoder.layers.16.input_layernorm.weight": "model.safetensors",
|
| 198 |
+
"encoder.layers.16.mlp.down_proj.biases": "model.safetensors",
|
| 199 |
+
"encoder.layers.16.mlp.down_proj.scales": "model.safetensors",
|
| 200 |
+
"encoder.layers.16.mlp.down_proj.weight": "model.safetensors",
|
| 201 |
+
"encoder.layers.16.mlp.gate_proj.biases": "model.safetensors",
|
| 202 |
+
"encoder.layers.16.mlp.gate_proj.scales": "model.safetensors",
|
| 203 |
+
"encoder.layers.16.mlp.gate_proj.weight": "model.safetensors",
|
| 204 |
+
"encoder.layers.16.mlp.up_proj.biases": "model.safetensors",
|
| 205 |
+
"encoder.layers.16.mlp.up_proj.scales": "model.safetensors",
|
| 206 |
+
"encoder.layers.16.mlp.up_proj.weight": "model.safetensors",
|
| 207 |
+
"encoder.layers.16.post_attention_layernorm.weight": "model.safetensors",
|
| 208 |
+
"encoder.layers.16.self_attn.k_proj.biases": "model.safetensors",
|
| 209 |
+
"encoder.layers.16.self_attn.k_proj.scales": "model.safetensors",
|
| 210 |
+
"encoder.layers.16.self_attn.k_proj.weight": "model.safetensors",
|
| 211 |
+
"encoder.layers.16.self_attn.o_proj.biases": "model.safetensors",
|
| 212 |
+
"encoder.layers.16.self_attn.o_proj.scales": "model.safetensors",
|
| 213 |
+
"encoder.layers.16.self_attn.o_proj.weight": "model.safetensors",
|
| 214 |
+
"encoder.layers.16.self_attn.q_proj.biases": "model.safetensors",
|
| 215 |
+
"encoder.layers.16.self_attn.q_proj.scales": "model.safetensors",
|
| 216 |
+
"encoder.layers.16.self_attn.q_proj.weight": "model.safetensors",
|
| 217 |
+
"encoder.layers.16.self_attn.v_proj.biases": "model.safetensors",
|
| 218 |
+
"encoder.layers.16.self_attn.v_proj.scales": "model.safetensors",
|
| 219 |
+
"encoder.layers.16.self_attn.v_proj.weight": "model.safetensors",
|
| 220 |
+
"encoder.layers.17.input_layernorm.weight": "model.safetensors",
|
| 221 |
+
"encoder.layers.17.mlp.down_proj.biases": "model.safetensors",
|
| 222 |
+
"encoder.layers.17.mlp.down_proj.scales": "model.safetensors",
|
| 223 |
+
"encoder.layers.17.mlp.down_proj.weight": "model.safetensors",
|
| 224 |
+
"encoder.layers.17.mlp.gate_proj.biases": "model.safetensors",
|
| 225 |
+
"encoder.layers.17.mlp.gate_proj.scales": "model.safetensors",
|
| 226 |
+
"encoder.layers.17.mlp.gate_proj.weight": "model.safetensors",
|
| 227 |
+
"encoder.layers.17.mlp.up_proj.biases": "model.safetensors",
|
| 228 |
+
"encoder.layers.17.mlp.up_proj.scales": "model.safetensors",
|
| 229 |
+
"encoder.layers.17.mlp.up_proj.weight": "model.safetensors",
|
| 230 |
+
"encoder.layers.17.post_attention_layernorm.weight": "model.safetensors",
|
| 231 |
+
"encoder.layers.17.self_attn.k_proj.biases": "model.safetensors",
|
| 232 |
+
"encoder.layers.17.self_attn.k_proj.scales": "model.safetensors",
|
| 233 |
+
"encoder.layers.17.self_attn.k_proj.weight": "model.safetensors",
|
| 234 |
+
"encoder.layers.17.self_attn.o_proj.biases": "model.safetensors",
|
| 235 |
+
"encoder.layers.17.self_attn.o_proj.scales": "model.safetensors",
|
| 236 |
+
"encoder.layers.17.self_attn.o_proj.weight": "model.safetensors",
|
| 237 |
+
"encoder.layers.17.self_attn.q_proj.biases": "model.safetensors",
|
| 238 |
+
"encoder.layers.17.self_attn.q_proj.scales": "model.safetensors",
|
| 239 |
+
"encoder.layers.17.self_attn.q_proj.weight": "model.safetensors",
|
| 240 |
+
"encoder.layers.17.self_attn.v_proj.biases": "model.safetensors",
|
| 241 |
+
"encoder.layers.17.self_attn.v_proj.scales": "model.safetensors",
|
| 242 |
+
"encoder.layers.17.self_attn.v_proj.weight": "model.safetensors",
|
| 243 |
+
"encoder.layers.18.input_layernorm.weight": "model.safetensors",
|
| 244 |
+
"encoder.layers.18.mlp.down_proj.biases": "model.safetensors",
|
| 245 |
+
"encoder.layers.18.mlp.down_proj.scales": "model.safetensors",
|
| 246 |
+
"encoder.layers.18.mlp.down_proj.weight": "model.safetensors",
|
| 247 |
+
"encoder.layers.18.mlp.gate_proj.biases": "model.safetensors",
|
| 248 |
+
"encoder.layers.18.mlp.gate_proj.scales": "model.safetensors",
|
| 249 |
+
"encoder.layers.18.mlp.gate_proj.weight": "model.safetensors",
|
| 250 |
+
"encoder.layers.18.mlp.up_proj.biases": "model.safetensors",
|
| 251 |
+
"encoder.layers.18.mlp.up_proj.scales": "model.safetensors",
|
| 252 |
+
"encoder.layers.18.mlp.up_proj.weight": "model.safetensors",
|
| 253 |
+
"encoder.layers.18.post_attention_layernorm.weight": "model.safetensors",
|
| 254 |
+
"encoder.layers.18.self_attn.k_proj.biases": "model.safetensors",
|
| 255 |
+
"encoder.layers.18.self_attn.k_proj.scales": "model.safetensors",
|
| 256 |
+
"encoder.layers.18.self_attn.k_proj.weight": "model.safetensors",
|
| 257 |
+
"encoder.layers.18.self_attn.o_proj.biases": "model.safetensors",
|
| 258 |
+
"encoder.layers.18.self_attn.o_proj.scales": "model.safetensors",
|
| 259 |
+
"encoder.layers.18.self_attn.o_proj.weight": "model.safetensors",
|
| 260 |
+
"encoder.layers.18.self_attn.q_proj.biases": "model.safetensors",
|
| 261 |
+
"encoder.layers.18.self_attn.q_proj.scales": "model.safetensors",
|
| 262 |
+
"encoder.layers.18.self_attn.q_proj.weight": "model.safetensors",
|
| 263 |
+
"encoder.layers.18.self_attn.v_proj.biases": "model.safetensors",
|
| 264 |
+
"encoder.layers.18.self_attn.v_proj.scales": "model.safetensors",
|
| 265 |
+
"encoder.layers.18.self_attn.v_proj.weight": "model.safetensors",
|
| 266 |
+
"encoder.layers.19.input_layernorm.weight": "model.safetensors",
|
| 267 |
+
"encoder.layers.19.mlp.down_proj.biases": "model.safetensors",
|
| 268 |
+
"encoder.layers.19.mlp.down_proj.scales": "model.safetensors",
|
| 269 |
+
"encoder.layers.19.mlp.down_proj.weight": "model.safetensors",
|
| 270 |
+
"encoder.layers.19.mlp.gate_proj.biases": "model.safetensors",
|
| 271 |
+
"encoder.layers.19.mlp.gate_proj.scales": "model.safetensors",
|
| 272 |
+
"encoder.layers.19.mlp.gate_proj.weight": "model.safetensors",
|
| 273 |
+
"encoder.layers.19.mlp.up_proj.biases": "model.safetensors",
|
| 274 |
+
"encoder.layers.19.mlp.up_proj.scales": "model.safetensors",
|
| 275 |
+
"encoder.layers.19.mlp.up_proj.weight": "model.safetensors",
|
| 276 |
+
"encoder.layers.19.post_attention_layernorm.weight": "model.safetensors",
|
| 277 |
+
"encoder.layers.19.self_attn.k_proj.biases": "model.safetensors",
|
| 278 |
+
"encoder.layers.19.self_attn.k_proj.scales": "model.safetensors",
|
| 279 |
+
"encoder.layers.19.self_attn.k_proj.weight": "model.safetensors",
|
| 280 |
+
"encoder.layers.19.self_attn.o_proj.biases": "model.safetensors",
|
| 281 |
+
"encoder.layers.19.self_attn.o_proj.scales": "model.safetensors",
|
| 282 |
+
"encoder.layers.19.self_attn.o_proj.weight": "model.safetensors",
|
| 283 |
+
"encoder.layers.19.self_attn.q_proj.biases": "model.safetensors",
|
| 284 |
+
"encoder.layers.19.self_attn.q_proj.scales": "model.safetensors",
|
| 285 |
+
"encoder.layers.19.self_attn.q_proj.weight": "model.safetensors",
|
| 286 |
+
"encoder.layers.19.self_attn.v_proj.biases": "model.safetensors",
|
| 287 |
+
"encoder.layers.19.self_attn.v_proj.scales": "model.safetensors",
|
| 288 |
+
"encoder.layers.19.self_attn.v_proj.weight": "model.safetensors",
|
| 289 |
+
"encoder.layers.2.input_layernorm.weight": "model.safetensors",
|
| 290 |
+
"encoder.layers.2.mlp.down_proj.biases": "model.safetensors",
|
| 291 |
+
"encoder.layers.2.mlp.down_proj.scales": "model.safetensors",
|
| 292 |
+
"encoder.layers.2.mlp.down_proj.weight": "model.safetensors",
|
| 293 |
+
"encoder.layers.2.mlp.gate_proj.biases": "model.safetensors",
|
| 294 |
+
"encoder.layers.2.mlp.gate_proj.scales": "model.safetensors",
|
| 295 |
+
"encoder.layers.2.mlp.gate_proj.weight": "model.safetensors",
|
| 296 |
+
"encoder.layers.2.mlp.up_proj.biases": "model.safetensors",
|
| 297 |
+
"encoder.layers.2.mlp.up_proj.scales": "model.safetensors",
|
| 298 |
+
"encoder.layers.2.mlp.up_proj.weight": "model.safetensors",
|
| 299 |
+
"encoder.layers.2.post_attention_layernorm.weight": "model.safetensors",
|
| 300 |
+
"encoder.layers.2.self_attn.k_proj.biases": "model.safetensors",
|
| 301 |
+
"encoder.layers.2.self_attn.k_proj.scales": "model.safetensors",
|
| 302 |
+
"encoder.layers.2.self_attn.k_proj.weight": "model.safetensors",
|
| 303 |
+
"encoder.layers.2.self_attn.o_proj.biases": "model.safetensors",
|
| 304 |
+
"encoder.layers.2.self_attn.o_proj.scales": "model.safetensors",
|
| 305 |
+
"encoder.layers.2.self_attn.o_proj.weight": "model.safetensors",
|
| 306 |
+
"encoder.layers.2.self_attn.q_proj.biases": "model.safetensors",
|
| 307 |
+
"encoder.layers.2.self_attn.q_proj.scales": "model.safetensors",
|
| 308 |
+
"encoder.layers.2.self_attn.q_proj.weight": "model.safetensors",
|
| 309 |
+
"encoder.layers.2.self_attn.v_proj.biases": "model.safetensors",
|
| 310 |
+
"encoder.layers.2.self_attn.v_proj.scales": "model.safetensors",
|
| 311 |
+
"encoder.layers.2.self_attn.v_proj.weight": "model.safetensors",
|
| 312 |
+
"encoder.layers.20.input_layernorm.weight": "model.safetensors",
|
| 313 |
+
"encoder.layers.20.mlp.down_proj.biases": "model.safetensors",
|
| 314 |
+
"encoder.layers.20.mlp.down_proj.scales": "model.safetensors",
|
| 315 |
+
"encoder.layers.20.mlp.down_proj.weight": "model.safetensors",
|
| 316 |
+
"encoder.layers.20.mlp.gate_proj.biases": "model.safetensors",
|
| 317 |
+
"encoder.layers.20.mlp.gate_proj.scales": "model.safetensors",
|
| 318 |
+
"encoder.layers.20.mlp.gate_proj.weight": "model.safetensors",
|
| 319 |
+
"encoder.layers.20.mlp.up_proj.biases": "model.safetensors",
|
| 320 |
+
"encoder.layers.20.mlp.up_proj.scales": "model.safetensors",
|
| 321 |
+
"encoder.layers.20.mlp.up_proj.weight": "model.safetensors",
|
| 322 |
+
"encoder.layers.20.post_attention_layernorm.weight": "model.safetensors",
|
| 323 |
+
"encoder.layers.20.self_attn.k_proj.biases": "model.safetensors",
|
| 324 |
+
"encoder.layers.20.self_attn.k_proj.scales": "model.safetensors",
|
| 325 |
+
"encoder.layers.20.self_attn.k_proj.weight": "model.safetensors",
|
| 326 |
+
"encoder.layers.20.self_attn.o_proj.biases": "model.safetensors",
|
| 327 |
+
"encoder.layers.20.self_attn.o_proj.scales": "model.safetensors",
|
| 328 |
+
"encoder.layers.20.self_attn.o_proj.weight": "model.safetensors",
|
| 329 |
+
"encoder.layers.20.self_attn.q_proj.biases": "model.safetensors",
|
| 330 |
+
"encoder.layers.20.self_attn.q_proj.scales": "model.safetensors",
|
| 331 |
+
"encoder.layers.20.self_attn.q_proj.weight": "model.safetensors",
|
| 332 |
+
"encoder.layers.20.self_attn.v_proj.biases": "model.safetensors",
|
| 333 |
+
"encoder.layers.20.self_attn.v_proj.scales": "model.safetensors",
|
| 334 |
+
"encoder.layers.20.self_attn.v_proj.weight": "model.safetensors",
|
| 335 |
+
"encoder.layers.21.input_layernorm.weight": "model.safetensors",
|
| 336 |
+
"encoder.layers.21.mlp.down_proj.biases": "model.safetensors",
|
| 337 |
+
"encoder.layers.21.mlp.down_proj.scales": "model.safetensors",
|
| 338 |
+
"encoder.layers.21.mlp.down_proj.weight": "model.safetensors",
|
| 339 |
+
"encoder.layers.21.mlp.gate_proj.biases": "model.safetensors",
|
| 340 |
+
"encoder.layers.21.mlp.gate_proj.scales": "model.safetensors",
|
| 341 |
+
"encoder.layers.21.mlp.gate_proj.weight": "model.safetensors",
|
| 342 |
+
"encoder.layers.21.mlp.up_proj.biases": "model.safetensors",
|
| 343 |
+
"encoder.layers.21.mlp.up_proj.scales": "model.safetensors",
|
| 344 |
+
"encoder.layers.21.mlp.up_proj.weight": "model.safetensors",
|
| 345 |
+
"encoder.layers.21.post_attention_layernorm.weight": "model.safetensors",
|
| 346 |
+
"encoder.layers.21.self_attn.k_proj.biases": "model.safetensors",
|
| 347 |
+
"encoder.layers.21.self_attn.k_proj.scales": "model.safetensors",
|
| 348 |
+
"encoder.layers.21.self_attn.k_proj.weight": "model.safetensors",
|
| 349 |
+
"encoder.layers.21.self_attn.o_proj.biases": "model.safetensors",
|
| 350 |
+
"encoder.layers.21.self_attn.o_proj.scales": "model.safetensors",
|
| 351 |
+
"encoder.layers.21.self_attn.o_proj.weight": "model.safetensors",
|
| 352 |
+
"encoder.layers.21.self_attn.q_proj.biases": "model.safetensors",
|
| 353 |
+
"encoder.layers.21.self_attn.q_proj.scales": "model.safetensors",
|
| 354 |
+
"encoder.layers.21.self_attn.q_proj.weight": "model.safetensors",
|
| 355 |
+
"encoder.layers.21.self_attn.v_proj.biases": "model.safetensors",
|
| 356 |
+
"encoder.layers.21.self_attn.v_proj.scales": "model.safetensors",
|
| 357 |
+
"encoder.layers.21.self_attn.v_proj.weight": "model.safetensors",
|
| 358 |
+
"encoder.layers.22.input_layernorm.weight": "model.safetensors",
|
| 359 |
+
"encoder.layers.22.mlp.down_proj.biases": "model.safetensors",
|
| 360 |
+
"encoder.layers.22.mlp.down_proj.scales": "model.safetensors",
|
| 361 |
+
"encoder.layers.22.mlp.down_proj.weight": "model.safetensors",
|
| 362 |
+
"encoder.layers.22.mlp.gate_proj.biases": "model.safetensors",
|
| 363 |
+
"encoder.layers.22.mlp.gate_proj.scales": "model.safetensors",
|
| 364 |
+
"encoder.layers.22.mlp.gate_proj.weight": "model.safetensors",
|
| 365 |
+
"encoder.layers.22.mlp.up_proj.biases": "model.safetensors",
|
| 366 |
+
"encoder.layers.22.mlp.up_proj.scales": "model.safetensors",
|
| 367 |
+
"encoder.layers.22.mlp.up_proj.weight": "model.safetensors",
|
| 368 |
+
"encoder.layers.22.post_attention_layernorm.weight": "model.safetensors",
|
| 369 |
+
"encoder.layers.22.self_attn.k_proj.biases": "model.safetensors",
|
| 370 |
+
"encoder.layers.22.self_attn.k_proj.scales": "model.safetensors",
|
| 371 |
+
"encoder.layers.22.self_attn.k_proj.weight": "model.safetensors",
|
| 372 |
+
"encoder.layers.22.self_attn.o_proj.biases": "model.safetensors",
|
| 373 |
+
"encoder.layers.22.self_attn.o_proj.scales": "model.safetensors",
|
| 374 |
+
"encoder.layers.22.self_attn.o_proj.weight": "model.safetensors",
|
| 375 |
+
"encoder.layers.22.self_attn.q_proj.biases": "model.safetensors",
|
| 376 |
+
"encoder.layers.22.self_attn.q_proj.scales": "model.safetensors",
|
| 377 |
+
"encoder.layers.22.self_attn.q_proj.weight": "model.safetensors",
|
| 378 |
+
"encoder.layers.22.self_attn.v_proj.biases": "model.safetensors",
|
| 379 |
+
"encoder.layers.22.self_attn.v_proj.scales": "model.safetensors",
|
| 380 |
+
"encoder.layers.22.self_attn.v_proj.weight": "model.safetensors",
|
| 381 |
+
"encoder.layers.23.input_layernorm.weight": "model.safetensors",
|
| 382 |
+
"encoder.layers.23.mlp.down_proj.biases": "model.safetensors",
|
| 383 |
+
"encoder.layers.23.mlp.down_proj.scales": "model.safetensors",
|
| 384 |
+
"encoder.layers.23.mlp.down_proj.weight": "model.safetensors",
|
| 385 |
+
"encoder.layers.23.mlp.gate_proj.biases": "model.safetensors",
|
| 386 |
+
"encoder.layers.23.mlp.gate_proj.scales": "model.safetensors",
|
| 387 |
+
"encoder.layers.23.mlp.gate_proj.weight": "model.safetensors",
|
| 388 |
+
"encoder.layers.23.mlp.up_proj.biases": "model.safetensors",
|
| 389 |
+
"encoder.layers.23.mlp.up_proj.scales": "model.safetensors",
|
| 390 |
+
"encoder.layers.23.mlp.up_proj.weight": "model.safetensors",
|
| 391 |
+
"encoder.layers.23.post_attention_layernorm.weight": "model.safetensors",
|
| 392 |
+
"encoder.layers.23.self_attn.k_proj.biases": "model.safetensors",
|
| 393 |
+
"encoder.layers.23.self_attn.k_proj.scales": "model.safetensors",
|
| 394 |
+
"encoder.layers.23.self_attn.k_proj.weight": "model.safetensors",
|
| 395 |
+
"encoder.layers.23.self_attn.o_proj.biases": "model.safetensors",
|
| 396 |
+
"encoder.layers.23.self_attn.o_proj.scales": "model.safetensors",
|
| 397 |
+
"encoder.layers.23.self_attn.o_proj.weight": "model.safetensors",
|
| 398 |
+
"encoder.layers.23.self_attn.q_proj.biases": "model.safetensors",
|
| 399 |
+
"encoder.layers.23.self_attn.q_proj.scales": "model.safetensors",
|
| 400 |
+
"encoder.layers.23.self_attn.q_proj.weight": "model.safetensors",
|
| 401 |
+
"encoder.layers.23.self_attn.v_proj.biases": "model.safetensors",
|
| 402 |
+
"encoder.layers.23.self_attn.v_proj.scales": "model.safetensors",
|
| 403 |
+
"encoder.layers.23.self_attn.v_proj.weight": "model.safetensors",
|
| 404 |
+
"encoder.layers.24.input_layernorm.weight": "model.safetensors",
|
| 405 |
+
"encoder.layers.24.mlp.down_proj.biases": "model.safetensors",
|
| 406 |
+
"encoder.layers.24.mlp.down_proj.scales": "model.safetensors",
|
| 407 |
+
"encoder.layers.24.mlp.down_proj.weight": "model.safetensors",
|
| 408 |
+
"encoder.layers.24.mlp.gate_proj.biases": "model.safetensors",
|
| 409 |
+
"encoder.layers.24.mlp.gate_proj.scales": "model.safetensors",
|
| 410 |
+
"encoder.layers.24.mlp.gate_proj.weight": "model.safetensors",
|
| 411 |
+
"encoder.layers.24.mlp.up_proj.biases": "model.safetensors",
|
| 412 |
+
"encoder.layers.24.mlp.up_proj.scales": "model.safetensors",
|
| 413 |
+
"encoder.layers.24.mlp.up_proj.weight": "model.safetensors",
|
| 414 |
+
"encoder.layers.24.post_attention_layernorm.weight": "model.safetensors",
|
| 415 |
+
"encoder.layers.24.self_attn.k_proj.biases": "model.safetensors",
|
| 416 |
+
"encoder.layers.24.self_attn.k_proj.scales": "model.safetensors",
|
| 417 |
+
"encoder.layers.24.self_attn.k_proj.weight": "model.safetensors",
|
| 418 |
+
"encoder.layers.24.self_attn.o_proj.biases": "model.safetensors",
|
| 419 |
+
"encoder.layers.24.self_attn.o_proj.scales": "model.safetensors",
|
| 420 |
+
"encoder.layers.24.self_attn.o_proj.weight": "model.safetensors",
|
| 421 |
+
"encoder.layers.24.self_attn.q_proj.biases": "model.safetensors",
|
| 422 |
+
"encoder.layers.24.self_attn.q_proj.scales": "model.safetensors",
|
| 423 |
+
"encoder.layers.24.self_attn.q_proj.weight": "model.safetensors",
|
| 424 |
+
"encoder.layers.24.self_attn.v_proj.biases": "model.safetensors",
|
| 425 |
+
"encoder.layers.24.self_attn.v_proj.scales": "model.safetensors",
|
| 426 |
+
"encoder.layers.24.self_attn.v_proj.weight": "model.safetensors",
|
| 427 |
+
"encoder.layers.25.input_layernorm.weight": "model.safetensors",
|
| 428 |
+
"encoder.layers.25.mlp.down_proj.biases": "model.safetensors",
|
| 429 |
+
"encoder.layers.25.mlp.down_proj.scales": "model.safetensors",
|
| 430 |
+
"encoder.layers.25.mlp.down_proj.weight": "model.safetensors",
|
| 431 |
+
"encoder.layers.25.mlp.gate_proj.biases": "model.safetensors",
|
| 432 |
+
"encoder.layers.25.mlp.gate_proj.scales": "model.safetensors",
|
| 433 |
+
"encoder.layers.25.mlp.gate_proj.weight": "model.safetensors",
|
| 434 |
+
"encoder.layers.25.mlp.up_proj.biases": "model.safetensors",
|
| 435 |
+
"encoder.layers.25.mlp.up_proj.scales": "model.safetensors",
|
| 436 |
+
"encoder.layers.25.mlp.up_proj.weight": "model.safetensors",
|
| 437 |
+
"encoder.layers.25.post_attention_layernorm.weight": "model.safetensors",
|
| 438 |
+
"encoder.layers.25.self_attn.k_proj.biases": "model.safetensors",
|
| 439 |
+
"encoder.layers.25.self_attn.k_proj.scales": "model.safetensors",
|
| 440 |
+
"encoder.layers.25.self_attn.k_proj.weight": "model.safetensors",
|
| 441 |
+
"encoder.layers.25.self_attn.o_proj.biases": "model.safetensors",
|
| 442 |
+
"encoder.layers.25.self_attn.o_proj.scales": "model.safetensors",
|
| 443 |
+
"encoder.layers.25.self_attn.o_proj.weight": "model.safetensors",
|
| 444 |
+
"encoder.layers.25.self_attn.q_proj.biases": "model.safetensors",
|
| 445 |
+
"encoder.layers.25.self_attn.q_proj.scales": "model.safetensors",
|
| 446 |
+
"encoder.layers.25.self_attn.q_proj.weight": "model.safetensors",
|
| 447 |
+
"encoder.layers.25.self_attn.v_proj.biases": "model.safetensors",
|
| 448 |
+
"encoder.layers.25.self_attn.v_proj.scales": "model.safetensors",
|
| 449 |
+
"encoder.layers.25.self_attn.v_proj.weight": "model.safetensors",
|
| 450 |
+
"encoder.layers.3.input_layernorm.weight": "model.safetensors",
|
| 451 |
+
"encoder.layers.3.mlp.down_proj.biases": "model.safetensors",
|
| 452 |
+
"encoder.layers.3.mlp.down_proj.scales": "model.safetensors",
|
| 453 |
+
"encoder.layers.3.mlp.down_proj.weight": "model.safetensors",
|
| 454 |
+
"encoder.layers.3.mlp.gate_proj.biases": "model.safetensors",
|
| 455 |
+
"encoder.layers.3.mlp.gate_proj.scales": "model.safetensors",
|
| 456 |
+
"encoder.layers.3.mlp.gate_proj.weight": "model.safetensors",
|
| 457 |
+
"encoder.layers.3.mlp.up_proj.biases": "model.safetensors",
|
| 458 |
+
"encoder.layers.3.mlp.up_proj.scales": "model.safetensors",
|
| 459 |
+
"encoder.layers.3.mlp.up_proj.weight": "model.safetensors",
|
| 460 |
+
"encoder.layers.3.post_attention_layernorm.weight": "model.safetensors",
|
| 461 |
+
"encoder.layers.3.self_attn.k_proj.biases": "model.safetensors",
|
| 462 |
+
"encoder.layers.3.self_attn.k_proj.scales": "model.safetensors",
|
| 463 |
+
"encoder.layers.3.self_attn.k_proj.weight": "model.safetensors",
|
| 464 |
+
"encoder.layers.3.self_attn.o_proj.biases": "model.safetensors",
|
| 465 |
+
"encoder.layers.3.self_attn.o_proj.scales": "model.safetensors",
|
| 466 |
+
"encoder.layers.3.self_attn.o_proj.weight": "model.safetensors",
|
| 467 |
+
"encoder.layers.3.self_attn.q_proj.biases": "model.safetensors",
|
| 468 |
+
"encoder.layers.3.self_attn.q_proj.scales": "model.safetensors",
|
| 469 |
+
"encoder.layers.3.self_attn.q_proj.weight": "model.safetensors",
|
| 470 |
+
"encoder.layers.3.self_attn.v_proj.biases": "model.safetensors",
|
| 471 |
+
"encoder.layers.3.self_attn.v_proj.scales": "model.safetensors",
|
| 472 |
+
"encoder.layers.3.self_attn.v_proj.weight": "model.safetensors",
|
| 473 |
+
"encoder.layers.4.input_layernorm.weight": "model.safetensors",
|
| 474 |
+
"encoder.layers.4.mlp.down_proj.biases": "model.safetensors",
|
| 475 |
+
"encoder.layers.4.mlp.down_proj.scales": "model.safetensors",
|
| 476 |
+
"encoder.layers.4.mlp.down_proj.weight": "model.safetensors",
|
| 477 |
+
"encoder.layers.4.mlp.gate_proj.biases": "model.safetensors",
|
| 478 |
+
"encoder.layers.4.mlp.gate_proj.scales": "model.safetensors",
|
| 479 |
+
"encoder.layers.4.mlp.gate_proj.weight": "model.safetensors",
|
| 480 |
+
"encoder.layers.4.mlp.up_proj.biases": "model.safetensors",
|
| 481 |
+
"encoder.layers.4.mlp.up_proj.scales": "model.safetensors",
|
| 482 |
+
"encoder.layers.4.mlp.up_proj.weight": "model.safetensors",
|
| 483 |
+
"encoder.layers.4.post_attention_layernorm.weight": "model.safetensors",
|
| 484 |
+
"encoder.layers.4.self_attn.k_proj.biases": "model.safetensors",
|
| 485 |
+
"encoder.layers.4.self_attn.k_proj.scales": "model.safetensors",
|
| 486 |
+
"encoder.layers.4.self_attn.k_proj.weight": "model.safetensors",
|
| 487 |
+
"encoder.layers.4.self_attn.o_proj.biases": "model.safetensors",
|
| 488 |
+
"encoder.layers.4.self_attn.o_proj.scales": "model.safetensors",
|
| 489 |
+
"encoder.layers.4.self_attn.o_proj.weight": "model.safetensors",
|
| 490 |
+
"encoder.layers.4.self_attn.q_proj.biases": "model.safetensors",
|
| 491 |
+
"encoder.layers.4.self_attn.q_proj.scales": "model.safetensors",
|
| 492 |
+
"encoder.layers.4.self_attn.q_proj.weight": "model.safetensors",
|
| 493 |
+
"encoder.layers.4.self_attn.v_proj.biases": "model.safetensors",
|
| 494 |
+
"encoder.layers.4.self_attn.v_proj.scales": "model.safetensors",
|
| 495 |
+
"encoder.layers.4.self_attn.v_proj.weight": "model.safetensors",
|
| 496 |
+
"encoder.layers.5.input_layernorm.weight": "model.safetensors",
|
| 497 |
+
"encoder.layers.5.mlp.down_proj.biases": "model.safetensors",
|
| 498 |
+
"encoder.layers.5.mlp.down_proj.scales": "model.safetensors",
|
| 499 |
+
"encoder.layers.5.mlp.down_proj.weight": "model.safetensors",
|
| 500 |
+
"encoder.layers.5.mlp.gate_proj.biases": "model.safetensors",
|
| 501 |
+
"encoder.layers.5.mlp.gate_proj.scales": "model.safetensors",
|
| 502 |
+
"encoder.layers.5.mlp.gate_proj.weight": "model.safetensors",
|
| 503 |
+
"encoder.layers.5.mlp.up_proj.biases": "model.safetensors",
|
| 504 |
+
"encoder.layers.5.mlp.up_proj.scales": "model.safetensors",
|
| 505 |
+
"encoder.layers.5.mlp.up_proj.weight": "model.safetensors",
|
| 506 |
+
"encoder.layers.5.post_attention_layernorm.weight": "model.safetensors",
|
| 507 |
+
"encoder.layers.5.self_attn.k_proj.biases": "model.safetensors",
|
| 508 |
+
"encoder.layers.5.self_attn.k_proj.scales": "model.safetensors",
|
| 509 |
+
"encoder.layers.5.self_attn.k_proj.weight": "model.safetensors",
|
| 510 |
+
"encoder.layers.5.self_attn.o_proj.biases": "model.safetensors",
|
| 511 |
+
"encoder.layers.5.self_attn.o_proj.scales": "model.safetensors",
|
| 512 |
+
"encoder.layers.5.self_attn.o_proj.weight": "model.safetensors",
|
| 513 |
+
"encoder.layers.5.self_attn.q_proj.biases": "model.safetensors",
|
| 514 |
+
"encoder.layers.5.self_attn.q_proj.scales": "model.safetensors",
|
| 515 |
+
"encoder.layers.5.self_attn.q_proj.weight": "model.safetensors",
|
| 516 |
+
"encoder.layers.5.self_attn.v_proj.biases": "model.safetensors",
|
| 517 |
+
"encoder.layers.5.self_attn.v_proj.scales": "model.safetensors",
|
| 518 |
+
"encoder.layers.5.self_attn.v_proj.weight": "model.safetensors",
|
| 519 |
+
"encoder.layers.6.input_layernorm.weight": "model.safetensors",
|
| 520 |
+
"encoder.layers.6.mlp.down_proj.biases": "model.safetensors",
|
| 521 |
+
"encoder.layers.6.mlp.down_proj.scales": "model.safetensors",
|
| 522 |
+
"encoder.layers.6.mlp.down_proj.weight": "model.safetensors",
|
| 523 |
+
"encoder.layers.6.mlp.gate_proj.biases": "model.safetensors",
|
| 524 |
+
"encoder.layers.6.mlp.gate_proj.scales": "model.safetensors",
|
| 525 |
+
"encoder.layers.6.mlp.gate_proj.weight": "model.safetensors",
|
| 526 |
+
"encoder.layers.6.mlp.up_proj.biases": "model.safetensors",
|
| 527 |
+
"encoder.layers.6.mlp.up_proj.scales": "model.safetensors",
|
| 528 |
+
"encoder.layers.6.mlp.up_proj.weight": "model.safetensors",
|
| 529 |
+
"encoder.layers.6.post_attention_layernorm.weight": "model.safetensors",
|
| 530 |
+
"encoder.layers.6.self_attn.k_proj.biases": "model.safetensors",
|
| 531 |
+
"encoder.layers.6.self_attn.k_proj.scales": "model.safetensors",
|
| 532 |
+
"encoder.layers.6.self_attn.k_proj.weight": "model.safetensors",
|
| 533 |
+
"encoder.layers.6.self_attn.o_proj.biases": "model.safetensors",
|
| 534 |
+
"encoder.layers.6.self_attn.o_proj.scales": "model.safetensors",
|
| 535 |
+
"encoder.layers.6.self_attn.o_proj.weight": "model.safetensors",
|
| 536 |
+
"encoder.layers.6.self_attn.q_proj.biases": "model.safetensors",
|
| 537 |
+
"encoder.layers.6.self_attn.q_proj.scales": "model.safetensors",
|
| 538 |
+
"encoder.layers.6.self_attn.q_proj.weight": "model.safetensors",
|
| 539 |
+
"encoder.layers.6.self_attn.v_proj.biases": "model.safetensors",
|
| 540 |
+
"encoder.layers.6.self_attn.v_proj.scales": "model.safetensors",
|
| 541 |
+
"encoder.layers.6.self_attn.v_proj.weight": "model.safetensors",
|
| 542 |
+
"encoder.layers.7.input_layernorm.weight": "model.safetensors",
|
| 543 |
+
"encoder.layers.7.mlp.down_proj.biases": "model.safetensors",
|
| 544 |
+
"encoder.layers.7.mlp.down_proj.scales": "model.safetensors",
|
| 545 |
+
"encoder.layers.7.mlp.down_proj.weight": "model.safetensors",
|
| 546 |
+
"encoder.layers.7.mlp.gate_proj.biases": "model.safetensors",
|
| 547 |
+
"encoder.layers.7.mlp.gate_proj.scales": "model.safetensors",
|
| 548 |
+
"encoder.layers.7.mlp.gate_proj.weight": "model.safetensors",
|
| 549 |
+
"encoder.layers.7.mlp.up_proj.biases": "model.safetensors",
|
| 550 |
+
"encoder.layers.7.mlp.up_proj.scales": "model.safetensors",
|
| 551 |
+
"encoder.layers.7.mlp.up_proj.weight": "model.safetensors",
|
| 552 |
+
"encoder.layers.7.post_attention_layernorm.weight": "model.safetensors",
|
| 553 |
+
"encoder.layers.7.self_attn.k_proj.biases": "model.safetensors",
|
| 554 |
+
"encoder.layers.7.self_attn.k_proj.scales": "model.safetensors",
|
| 555 |
+
"encoder.layers.7.self_attn.k_proj.weight": "model.safetensors",
|
| 556 |
+
"encoder.layers.7.self_attn.o_proj.biases": "model.safetensors",
|
| 557 |
+
"encoder.layers.7.self_attn.o_proj.scales": "model.safetensors",
|
| 558 |
+
"encoder.layers.7.self_attn.o_proj.weight": "model.safetensors",
|
| 559 |
+
"encoder.layers.7.self_attn.q_proj.biases": "model.safetensors",
|
| 560 |
+
"encoder.layers.7.self_attn.q_proj.scales": "model.safetensors",
|
| 561 |
+
"encoder.layers.7.self_attn.q_proj.weight": "model.safetensors",
|
| 562 |
+
"encoder.layers.7.self_attn.v_proj.biases": "model.safetensors",
|
| 563 |
+
"encoder.layers.7.self_attn.v_proj.scales": "model.safetensors",
|
| 564 |
+
"encoder.layers.7.self_attn.v_proj.weight": "model.safetensors",
|
| 565 |
+
"encoder.layers.8.input_layernorm.weight": "model.safetensors",
|
| 566 |
+
"encoder.layers.8.mlp.down_proj.biases": "model.safetensors",
|
| 567 |
+
"encoder.layers.8.mlp.down_proj.scales": "model.safetensors",
|
| 568 |
+
"encoder.layers.8.mlp.down_proj.weight": "model.safetensors",
|
| 569 |
+
"encoder.layers.8.mlp.gate_proj.biases": "model.safetensors",
|
| 570 |
+
"encoder.layers.8.mlp.gate_proj.scales": "model.safetensors",
|
| 571 |
+
"encoder.layers.8.mlp.gate_proj.weight": "model.safetensors",
|
| 572 |
+
"encoder.layers.8.mlp.up_proj.biases": "model.safetensors",
|
| 573 |
+
"encoder.layers.8.mlp.up_proj.scales": "model.safetensors",
|
| 574 |
+
"encoder.layers.8.mlp.up_proj.weight": "model.safetensors",
|
| 575 |
+
"encoder.layers.8.post_attention_layernorm.weight": "model.safetensors",
|
| 576 |
+
"encoder.layers.8.self_attn.k_proj.biases": "model.safetensors",
|
| 577 |
+
"encoder.layers.8.self_attn.k_proj.scales": "model.safetensors",
|
| 578 |
+
"encoder.layers.8.self_attn.k_proj.weight": "model.safetensors",
|
| 579 |
+
"encoder.layers.8.self_attn.o_proj.biases": "model.safetensors",
|
| 580 |
+
"encoder.layers.8.self_attn.o_proj.scales": "model.safetensors",
|
| 581 |
+
"encoder.layers.8.self_attn.o_proj.weight": "model.safetensors",
|
| 582 |
+
"encoder.layers.8.self_attn.q_proj.biases": "model.safetensors",
|
| 583 |
+
"encoder.layers.8.self_attn.q_proj.scales": "model.safetensors",
|
| 584 |
+
"encoder.layers.8.self_attn.q_proj.weight": "model.safetensors",
|
| 585 |
+
"encoder.layers.8.self_attn.v_proj.biases": "model.safetensors",
|
| 586 |
+
"encoder.layers.8.self_attn.v_proj.scales": "model.safetensors",
|
| 587 |
+
"encoder.layers.8.self_attn.v_proj.weight": "model.safetensors",
|
| 588 |
+
"encoder.layers.9.input_layernorm.weight": "model.safetensors",
|
| 589 |
+
"encoder.layers.9.mlp.down_proj.biases": "model.safetensors",
|
| 590 |
+
"encoder.layers.9.mlp.down_proj.scales": "model.safetensors",
|
| 591 |
+
"encoder.layers.9.mlp.down_proj.weight": "model.safetensors",
|
| 592 |
+
"encoder.layers.9.mlp.gate_proj.biases": "model.safetensors",
|
| 593 |
+
"encoder.layers.9.mlp.gate_proj.scales": "model.safetensors",
|
| 594 |
+
"encoder.layers.9.mlp.gate_proj.weight": "model.safetensors",
|
| 595 |
+
"encoder.layers.9.mlp.up_proj.biases": "model.safetensors",
|
| 596 |
+
"encoder.layers.9.mlp.up_proj.scales": "model.safetensors",
|
| 597 |
+
"encoder.layers.9.mlp.up_proj.weight": "model.safetensors",
|
| 598 |
+
"encoder.layers.9.post_attention_layernorm.weight": "model.safetensors",
|
| 599 |
+
"encoder.layers.9.self_attn.k_proj.biases": "model.safetensors",
|
| 600 |
+
"encoder.layers.9.self_attn.k_proj.scales": "model.safetensors",
|
| 601 |
+
"encoder.layers.9.self_attn.k_proj.weight": "model.safetensors",
|
| 602 |
+
"encoder.layers.9.self_attn.o_proj.biases": "model.safetensors",
|
| 603 |
+
"encoder.layers.9.self_attn.o_proj.scales": "model.safetensors",
|
| 604 |
+
"encoder.layers.9.self_attn.o_proj.weight": "model.safetensors",
|
| 605 |
+
"encoder.layers.9.self_attn.q_proj.biases": "model.safetensors",
|
| 606 |
+
"encoder.layers.9.self_attn.q_proj.scales": "model.safetensors",
|
| 607 |
+
"encoder.layers.9.self_attn.q_proj.weight": "model.safetensors",
|
| 608 |
+
"encoder.layers.9.self_attn.v_proj.biases": "model.safetensors",
|
| 609 |
+
"encoder.layers.9.self_attn.v_proj.scales": "model.safetensors",
|
| 610 |
+
"encoder.layers.9.self_attn.v_proj.weight": "model.safetensors",
|
| 611 |
+
"encoder.norm.weight": "model.safetensors"
|
| 612 |
+
}
|
| 613 |
+
}
|
modeling_ministral.py
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
| 1 |
+
from collections.abc import Callable
|
| 2 |
+
from typing import Optional, Union
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from torch import nn
|
| 6 |
+
|
| 7 |
+
from transformers.utils.generic import check_model_inputs
|
| 8 |
+
|
| 9 |
+
from transformers.activations import ACT2FN
|
| 10 |
+
from transformers.cache_utils import Cache, DynamicCache
|
| 11 |
+
from transformers.generation import GenerationMixin
|
| 12 |
+
# from transformers.integrations import use_kernel_forward_from_hub, use_kernel_func_from_hub, use_kernelized_func
|
| 13 |
+
from transformers.integrations import use_kernel_forward_from_hub
|
| 14 |
+
from transformers.masking_utils import create_causal_mask, create_sliding_window_causal_mask, ALL_MASK_ATTENTION_FUNCTIONS
|
| 15 |
+
from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
|
| 16 |
+
from transformers.modeling_layers import (
|
| 17 |
+
GenericForQuestionAnswering,
|
| 18 |
+
GenericForSequenceClassification,
|
| 19 |
+
GenericForTokenClassification,
|
| 20 |
+
GradientCheckpointingLayer,
|
| 21 |
+
)
|
| 22 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
|
| 23 |
+
from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update
|
| 24 |
+
from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
|
| 25 |
+
from transformers.processing_utils import Unpack
|
| 26 |
+
from transformers.utils import TransformersKwargs, auto_docstring, can_return_tuple
|
| 27 |
+
# from transformers.utils.generic import maybe_autocast
|
| 28 |
+
from .configuration_nemotron_labs_diffusion import NemotronLabsDiffusionConfig
|
| 29 |
+
|
| 30 |
+
#ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['sdpa'] = sdpa_mask_older_torch
|
| 31 |
+
|
| 32 |
+
def rotate_half(x):
|
| 33 |
+
"""Rotates half the hidden dims of the input."""
|
| 34 |
+
x1 = x[..., : x.shape[-1] // 2]
|
| 35 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
| 36 |
+
return torch.cat((-x2, x1), dim=-1)
|
| 37 |
+
|
| 38 |
+
# @use_kernel_func_from_hub("rotary_pos_emb")
|
| 39 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
|
| 40 |
+
"""Applies Rotary Position Embedding to the query and key tensors.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
q (`torch.Tensor`): The query tensor.
|
| 44 |
+
k (`torch.Tensor`): The key tensor.
|
| 45 |
+
cos (`torch.Tensor`): The cosine part of the rotary embedding.
|
| 46 |
+
sin (`torch.Tensor`): The sine part of the rotary embedding.
|
| 47 |
+
position_ids (`torch.Tensor`, *optional*):
|
| 48 |
+
Deprecated and unused.
|
| 49 |
+
unsqueeze_dim (`int`, *optional*, defaults to 1):
|
| 50 |
+
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
|
| 51 |
+
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
|
| 52 |
+
that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
|
| 53 |
+
k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
|
| 54 |
+
cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
|
| 55 |
+
the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
|
| 56 |
+
Returns:
|
| 57 |
+
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
|
| 58 |
+
"""
|
| 59 |
+
cos = cos.unsqueeze(unsqueeze_dim)
|
| 60 |
+
sin = sin.unsqueeze(unsqueeze_dim)
|
| 61 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
| 62 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
| 63 |
+
return q_embed, k_embed
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
| 67 |
+
"""
|
| 68 |
+
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
| 69 |
+
num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
|
| 70 |
+
"""
|
| 71 |
+
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
|
| 72 |
+
if n_rep == 1:
|
| 73 |
+
return hidden_states
|
| 74 |
+
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
|
| 75 |
+
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def eager_attention_forward(
|
| 79 |
+
module: nn.Module,
|
| 80 |
+
query: torch.Tensor,
|
| 81 |
+
key: torch.Tensor,
|
| 82 |
+
value: torch.Tensor,
|
| 83 |
+
attention_mask: Optional[torch.Tensor],
|
| 84 |
+
scaling: float,
|
| 85 |
+
dropout: float = 0.0,
|
| 86 |
+
**kwargs: Unpack[TransformersKwargs],
|
| 87 |
+
):
|
| 88 |
+
key_states = repeat_kv(key, module.num_key_value_groups)
|
| 89 |
+
value_states = repeat_kv(value, module.num_key_value_groups)
|
| 90 |
+
|
| 91 |
+
attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling
|
| 92 |
+
if attention_mask is not None:
|
| 93 |
+
causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
|
| 94 |
+
attn_weights = attn_weights + causal_mask
|
| 95 |
+
|
| 96 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
|
| 97 |
+
attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)
|
| 98 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
| 99 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
| 100 |
+
|
| 101 |
+
return attn_output, attn_weights
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def _get_llama_4_attn_scale(positions_ids: torch.Tensor, beta: float, max_position_embeddings: int) -> torch.Tensor:
|
| 105 |
+
scaling = 1 + beta * torch.log(1 + torch.floor(positions_ids / max_position_embeddings))
|
| 106 |
+
return scaling.unsqueeze(-1)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# @use_kernelized_func(apply_rotary_pos_emb)
|
| 110 |
+
class Ministral3Attention(nn.Module):
|
| 111 |
+
"""Multi-headed attention from 'Attention Is All You Need' paper"""
|
| 112 |
+
|
| 113 |
+
def __init__(self, config: NemotronLabsDiffusionConfig, layer_idx: int):
|
| 114 |
+
super().__init__()
|
| 115 |
+
self.config = config
|
| 116 |
+
self.layer_idx = layer_idx
|
| 117 |
+
self.head_dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_heads
|
| 118 |
+
self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
|
| 119 |
+
self.scaling = self.head_dim**-0.5
|
| 120 |
+
self.attention_dropout = config.attention_dropout
|
| 121 |
+
self.is_causal = True
|
| 122 |
+
self.q_proj = nn.Linear(config.hidden_size, config.num_attention_heads * self.head_dim, bias=False)
|
| 123 |
+
self.k_proj = nn.Linear(config.hidden_size, config.num_key_value_heads * self.head_dim, bias=False)
|
| 124 |
+
self.v_proj = nn.Linear(config.hidden_size, config.num_key_value_heads * self.head_dim, bias=False)
|
| 125 |
+
self.o_proj = nn.Linear(config.num_attention_heads * self.head_dim, config.hidden_size, bias=False)
|
| 126 |
+
|
| 127 |
+
self.diffusion_lm = config.diffusion_lm
|
| 128 |
+
|
| 129 |
+
def forward(
|
| 130 |
+
self,
|
| 131 |
+
hidden_states: torch.Tensor,
|
| 132 |
+
position_embeddings: tuple[torch.Tensor, torch.Tensor],
|
| 133 |
+
attention_mask: Optional[torch.Tensor],
|
| 134 |
+
past_key_values: Optional[Cache] = None,
|
| 135 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 136 |
+
use_cache: Optional[bool] = False,
|
| 137 |
+
**kwargs: Unpack[FlashAttentionKwargs],
|
| 138 |
+
) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
|
| 139 |
+
input_shape = hidden_states.shape[:-1]
|
| 140 |
+
hidden_shape = (*input_shape, -1, self.head_dim)
|
| 141 |
+
|
| 142 |
+
query_states = self.q_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 143 |
+
key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 144 |
+
value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 145 |
+
|
| 146 |
+
cos, sin = position_embeddings
|
| 147 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
|
| 148 |
+
query_states = query_states * _get_llama_4_attn_scale(
|
| 149 |
+
cache_position,
|
| 150 |
+
self.config.rope_parameters.get("llama_4_scaling_beta"),
|
| 151 |
+
self.config.rope_parameters.get("original_max_position_embeddings"),
|
| 152 |
+
).to(query_states.dtype)
|
| 153 |
+
|
| 154 |
+
if past_key_values is not None:
|
| 155 |
+
if use_cache:
|
| 156 |
+
# sin and cos are specific to RoPE models; cache_position needed for the static cache
|
| 157 |
+
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
|
| 158 |
+
key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
| 159 |
+
else: ## if use_cache == False, do not update cache
|
| 160 |
+
old_k, old_v = past_key_values.layers[self.layer_idx].keys, past_key_values.layers[self.layer_idx].values
|
| 161 |
+
key_states = torch.cat([old_k, key_states], dim=-2)
|
| 162 |
+
value_states = torch.cat([old_v, value_states], dim=-2)
|
| 163 |
+
|
| 164 |
+
attention_interface: Callable = eager_attention_forward
|
| 165 |
+
if self.config._attn_implementation != "eager":
|
| 166 |
+
attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
|
| 167 |
+
|
| 168 |
+
if self.diffusion_lm:
|
| 169 |
+
attn_output, attn_weights = attention_interface(
|
| 170 |
+
self,
|
| 171 |
+
query_states,
|
| 172 |
+
key_states,
|
| 173 |
+
value_states,
|
| 174 |
+
None,
|
| 175 |
+
dropout=0.0 if not self.training else self.attention_dropout,
|
| 176 |
+
scaling=self.scaling,
|
| 177 |
+
is_causal=False,
|
| 178 |
+
**kwargs,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
else:
|
| 182 |
+
attn_output, attn_weights = attention_interface(
|
| 183 |
+
self,
|
| 184 |
+
query_states,
|
| 185 |
+
key_states,
|
| 186 |
+
value_states,
|
| 187 |
+
attention_mask,
|
| 188 |
+
dropout=0.0 if not self.training else self.attention_dropout,
|
| 189 |
+
scaling=self.scaling,
|
| 190 |
+
sliding_window=getattr(self.config, "sliding_window", None), # main diff with Llama
|
| 191 |
+
**kwargs,
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
attn_output = attn_output.reshape(*input_shape, -1).contiguous()
|
| 195 |
+
attn_output = self.o_proj(attn_output)
|
| 196 |
+
return attn_output, attn_weights
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
class Ministral3MLP(nn.Module):
|
| 200 |
+
def __init__(self, config):
|
| 201 |
+
super().__init__()
|
| 202 |
+
self.config = config
|
| 203 |
+
self.hidden_size = config.hidden_size
|
| 204 |
+
self.intermediate_size = config.intermediate_size
|
| 205 |
+
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 206 |
+
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
| 207 |
+
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
|
| 208 |
+
self.act_fn = ACT2FN[config.hidden_act]
|
| 209 |
+
|
| 210 |
+
def forward(self, x):
|
| 211 |
+
down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
| 212 |
+
return down_proj
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
@use_kernel_forward_from_hub("RMSNorm")
|
| 216 |
+
class Ministral3RMSNorm(nn.Module):
|
| 217 |
+
def __init__(self, hidden_size, eps=1e-6):
|
| 218 |
+
"""
|
| 219 |
+
Ministral3RMSNorm is equivalent to T5LayerNorm
|
| 220 |
+
"""
|
| 221 |
+
super().__init__()
|
| 222 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
| 223 |
+
self.variance_epsilon = eps
|
| 224 |
+
|
| 225 |
+
def forward(self, hidden_states):
|
| 226 |
+
input_dtype = hidden_states.dtype
|
| 227 |
+
hidden_states = hidden_states.to(torch.float32)
|
| 228 |
+
variance = hidden_states.pow(2).mean(-1, keepdim=True)
|
| 229 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
| 230 |
+
return self.weight * hidden_states.to(input_dtype)
|
| 231 |
+
|
| 232 |
+
def extra_repr(self):
|
| 233 |
+
return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}"
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
class Ministral3DecoderLayer(GradientCheckpointingLayer):
|
| 237 |
+
def __init__(self, config: NemotronLabsDiffusionConfig, layer_idx: int):
|
| 238 |
+
super().__init__()
|
| 239 |
+
self.hidden_size = config.hidden_size
|
| 240 |
+
|
| 241 |
+
if hasattr(config, 'attn_class'):
|
| 242 |
+
attn_class = config.attn_class
|
| 243 |
+
else:
|
| 244 |
+
attn_class = Ministral3Attention
|
| 245 |
+
|
| 246 |
+
self.self_attn = attn_class(config=config, layer_idx=layer_idx)
|
| 247 |
+
self.mlp = Ministral3MLP(config)
|
| 248 |
+
self.input_layernorm = Ministral3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 249 |
+
self.post_attention_layernorm = Ministral3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 250 |
+
|
| 251 |
+
def forward(
|
| 252 |
+
self,
|
| 253 |
+
hidden_states: torch.Tensor,
|
| 254 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 255 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 256 |
+
past_key_values: Optional[Cache] = None,
|
| 257 |
+
use_cache: Optional[bool] = False,
|
| 258 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 259 |
+
position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None,
|
| 260 |
+
**kwargs: Unpack[TransformersKwargs],
|
| 261 |
+
) -> torch.Tensor:
|
| 262 |
+
residual = hidden_states
|
| 263 |
+
hidden_states = self.input_layernorm(hidden_states)
|
| 264 |
+
# Self Attention
|
| 265 |
+
hidden_states, _ = self.self_attn(
|
| 266 |
+
hidden_states=hidden_states,
|
| 267 |
+
attention_mask=attention_mask,
|
| 268 |
+
position_ids=position_ids,
|
| 269 |
+
past_key_values=past_key_values,
|
| 270 |
+
use_cache=use_cache,
|
| 271 |
+
cache_position=cache_position,
|
| 272 |
+
position_embeddings=position_embeddings,
|
| 273 |
+
**kwargs,
|
| 274 |
+
)
|
| 275 |
+
hidden_states = residual + hidden_states
|
| 276 |
+
|
| 277 |
+
# Fully Connected
|
| 278 |
+
residual = hidden_states
|
| 279 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
| 280 |
+
hidden_states = self.mlp(hidden_states)
|
| 281 |
+
hidden_states = residual + hidden_states
|
| 282 |
+
return hidden_states
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
@auto_docstring
|
| 286 |
+
class Ministral3PreTrainedModel(PreTrainedModel):
|
| 287 |
+
config: NemotronLabsDiffusionConfig
|
| 288 |
+
base_model_prefix = "model"
|
| 289 |
+
supports_gradient_checkpointing = True
|
| 290 |
+
_no_split_modules = ["Ministral3DecoderLayer"]
|
| 291 |
+
_skip_keys_device_placement = ["past_key_values"]
|
| 292 |
+
_supports_flash_attn = True
|
| 293 |
+
_supports_sdpa = True
|
| 294 |
+
_supports_flex_attn = True
|
| 295 |
+
|
| 296 |
+
_can_compile_fullgraph = True
|
| 297 |
+
_supports_attention_backend = True
|
| 298 |
+
_can_record_outputs = {
|
| 299 |
+
"hidden_states": Ministral3DecoderLayer,
|
| 300 |
+
"attentions": Ministral3Attention,
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
class Ministral3RotaryEmbedding(nn.Module):
|
| 305 |
+
inv_freq: torch.Tensor # fix linting for `register_buffer`
|
| 306 |
+
|
| 307 |
+
def __init__(self, config: NemotronLabsDiffusionConfig, device=None):
|
| 308 |
+
super().__init__()
|
| 309 |
+
self.max_seq_len_cached = config.max_position_embeddings
|
| 310 |
+
self.original_max_seq_len = config.max_position_embeddings
|
| 311 |
+
|
| 312 |
+
self.config = config
|
| 313 |
+
|
| 314 |
+
self.rope_type = self.config.rope_parameters["rope_type"]
|
| 315 |
+
rope_init_fn: Callable = self.compute_default_rope_parameters
|
| 316 |
+
if self.rope_type != "default":
|
| 317 |
+
rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
|
| 318 |
+
inv_freq, self.attention_scaling = rope_init_fn(self.config, device)
|
| 319 |
+
|
| 320 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 321 |
+
self.original_inv_freq = inv_freq
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
@staticmethod
|
| 325 |
+
def compute_default_rope_parameters(
|
| 326 |
+
config: Optional[NemotronLabsDiffusionConfig] = None,
|
| 327 |
+
device: Optional["torch.device"] = None,
|
| 328 |
+
seq_len: Optional[int] = None,
|
| 329 |
+
) -> tuple["torch.Tensor", float]:
|
| 330 |
+
"""
|
| 331 |
+
Computes the inverse frequencies according to the original RoPE implementation
|
| 332 |
+
Args:
|
| 333 |
+
config ([`~transformers.PreTrainedConfig`]):
|
| 334 |
+
The model configuration.
|
| 335 |
+
device (`torch.device`):
|
| 336 |
+
The device to use for initialization of the inverse frequencies.
|
| 337 |
+
seq_len (`int`, *optional*):
|
| 338 |
+
The current sequence length. Unused for this type of RoPE.
|
| 339 |
+
Returns:
|
| 340 |
+
Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
|
| 341 |
+
post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).
|
| 342 |
+
"""
|
| 343 |
+
base = config.rope_parameters["rope_theta"]
|
| 344 |
+
dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_heads
|
| 345 |
+
|
| 346 |
+
attention_factor = 1.0 # Unused in this type of RoPE
|
| 347 |
+
|
| 348 |
+
# Compute the inverse frequencies
|
| 349 |
+
inv_freq = 1.0 / (
|
| 350 |
+
base ** (torch.arange(0, dim, 2, dtype=torch.int64).to(device=device, dtype=torch.float) / dim)
|
| 351 |
+
)
|
| 352 |
+
return inv_freq, attention_factor
|
| 353 |
+
|
| 354 |
+
@torch.no_grad()
|
| 355 |
+
@dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
|
| 356 |
+
def forward(self, x, position_ids):
|
| 357 |
+
inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)
|
| 358 |
+
position_ids_expanded = position_ids[:, None, :].float()
|
| 359 |
+
|
| 360 |
+
# device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"
|
| 361 |
+
# with maybe_autocast(device_type=device_type, enabled=False): # Force float32
|
| 362 |
+
|
| 363 |
+
freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
|
| 364 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
| 365 |
+
cos = emb.cos() * self.attention_scaling
|
| 366 |
+
sin = emb.sin() * self.attention_scaling
|
| 367 |
+
|
| 368 |
+
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
@auto_docstring
|
| 372 |
+
class Ministral3Model(Ministral3PreTrainedModel):
|
| 373 |
+
def __init__(self, config: NemotronLabsDiffusionConfig):
|
| 374 |
+
super().__init__(config)
|
| 375 |
+
self.padding_idx = config.pad_token_id
|
| 376 |
+
self.vocab_size = config.vocab_size
|
| 377 |
+
|
| 378 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
| 379 |
+
self.layers = nn.ModuleList(
|
| 380 |
+
[Ministral3DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
| 381 |
+
)
|
| 382 |
+
self.norm = Ministral3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 383 |
+
self.rotary_emb = Ministral3RotaryEmbedding(config=config)
|
| 384 |
+
self.gradient_checkpointing = False
|
| 385 |
+
|
| 386 |
+
# Initialize weights and apply final processing
|
| 387 |
+
self.post_init()
|
| 388 |
+
|
| 389 |
+
@check_model_inputs
|
| 390 |
+
@auto_docstring
|
| 391 |
+
def forward(
|
| 392 |
+
self,
|
| 393 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 394 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 395 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 396 |
+
past_key_values: Optional[Cache] = None,
|
| 397 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 398 |
+
use_cache: Optional[bool] = None,
|
| 399 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 400 |
+
**kwargs: Unpack[TransformersKwargs],
|
| 401 |
+
) -> BaseModelOutputWithPast:
|
| 402 |
+
if (input_ids is None) ^ (inputs_embeds is not None):
|
| 403 |
+
raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
|
| 404 |
+
|
| 405 |
+
if inputs_embeds is None:
|
| 406 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
| 407 |
+
|
| 408 |
+
if use_cache and past_key_values is None:
|
| 409 |
+
# past_key_values = DynamicCache(config=self.config)
|
| 410 |
+
past_key_values = DynamicCache()
|
| 411 |
+
|
| 412 |
+
if cache_position is None:
|
| 413 |
+
past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
|
| 414 |
+
cache_position = torch.arange(
|
| 415 |
+
past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
if position_ids is None:
|
| 419 |
+
position_ids = cache_position.unsqueeze(0)
|
| 420 |
+
|
| 421 |
+
if kwargs.get("use_causal_mask", False):
|
| 422 |
+
mask_function = create_causal_mask if self.config.sliding_window is None else create_sliding_window_causal_mask
|
| 423 |
+
causal_mask = mask_function(
|
| 424 |
+
config=self.config,
|
| 425 |
+
input_embeds=inputs_embeds,
|
| 426 |
+
attention_mask=attention_mask,
|
| 427 |
+
cache_position=cache_position,
|
| 428 |
+
past_key_values=past_key_values,
|
| 429 |
+
position_ids=position_ids,
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
else:
|
| 433 |
+
causal_mask = None
|
| 434 |
+
|
| 435 |
+
hidden_states = inputs_embeds
|
| 436 |
+
position_embeddings = self.rotary_emb(hidden_states, position_ids=position_ids)
|
| 437 |
+
|
| 438 |
+
for decoder_layer in self.layers[: self.config.num_hidden_layers]:
|
| 439 |
+
hidden_states = decoder_layer(
|
| 440 |
+
hidden_states,
|
| 441 |
+
attention_mask=causal_mask,
|
| 442 |
+
position_ids=position_ids,
|
| 443 |
+
past_key_values=past_key_values,
|
| 444 |
+
use_cache=use_cache,
|
| 445 |
+
cache_position=cache_position,
|
| 446 |
+
position_embeddings=position_embeddings,
|
| 447 |
+
**kwargs,
|
| 448 |
+
)
|
| 449 |
+
hidden_states = self.norm(hidden_states)
|
| 450 |
+
return BaseModelOutputWithPast(
|
| 451 |
+
last_hidden_state=hidden_states,
|
| 452 |
+
past_key_values=past_key_values if use_cache else None,
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
__all__ = [
|
| 457 |
+
"Ministral3Model",
|
| 458 |
+
"Ministral3PreTrainedModel",
|
| 459 |
+
]
|
modeling_nemotron_labs_diffusion.py
ADDED
|
@@ -0,0 +1,870 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
| 1 |
+
import copy
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Optional, Tuple
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
from torch import nn
|
| 9 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast, BaseModelOutput
|
| 10 |
+
from transformers.utils import ModelOutput
|
| 11 |
+
|
| 12 |
+
from torch.nn.attention.flex_attention import flex_attention, create_block_mask
|
| 13 |
+
|
| 14 |
+
from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
|
| 15 |
+
|
| 16 |
+
from transformers.processing_utils import Unpack
|
| 17 |
+
|
| 18 |
+
from transformers.cache_utils import Cache, DynamicCache
|
| 19 |
+
|
| 20 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
| 21 |
+
|
| 22 |
+
from transformers.generation import GenerationMixin
|
| 23 |
+
|
| 24 |
+
import math
|
| 25 |
+
|
| 26 |
+
from .modeling_ministral import Ministral3Model, Ministral3PreTrainedModel, Ministral3Attention, apply_rotary_pos_emb, repeat_kv, _get_llama_4_attn_scale
|
| 27 |
+
from .configuration_nemotron_labs_diffusion import NemotronLabsDiffusionConfig
|
| 28 |
+
|
| 29 |
+
__all__ = ["NemotronLabsDiffusionModel", "NemotronLabsDiffusionFlexAttention"]
|
| 30 |
+
|
| 31 |
+
@dataclass
|
| 32 |
+
class NemotronLabsDiffusionOutputWithPast(ModelOutput):
|
| 33 |
+
loss: torch.FloatTensor | None = None
|
| 34 |
+
logits: torch.FloatTensor | None = None
|
| 35 |
+
causal_logits: torch.FloatTensor | None = None
|
| 36 |
+
past_key_values: Cache | None = None
|
| 37 |
+
hidden_states: tuple[torch.FloatTensor, ...] | None = None
|
| 38 |
+
attentions: tuple[torch.FloatTensor, ...] | None = None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@torch.compile(fullgraph=True, mode="max-autotune-no-cudagraphs", dynamic=False)
|
| 42 |
+
def fused_flex_attention(q, k, v, block_mask=None):
|
| 43 |
+
return flex_attention(q, k, v, block_mask=block_mask)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class NemotronLabsDiffusionFlexAttention(Ministral3Attention):
|
| 47 |
+
def __init__(self, *args, **kwargs):
|
| 48 |
+
super().__init__(*args, **kwargs)
|
| 49 |
+
|
| 50 |
+
self.block_size = self.config.block_size
|
| 51 |
+
self.block_diff_mask = None
|
| 52 |
+
|
| 53 |
+
import torch._dynamo.config as dcfg
|
| 54 |
+
dcfg.cache_size_limit = 512
|
| 55 |
+
|
| 56 |
+
def compute_block_mask(self, mode, q_len, block_size=None):
|
| 57 |
+
|
| 58 |
+
def block_diff_mask(block_size, b, h, q_idx, kv_idx, n):
|
| 59 |
+
x0_flag_q = (q_idx >= n)
|
| 60 |
+
x0_flag_kv = (kv_idx >= n)
|
| 61 |
+
|
| 62 |
+
# Compute block indices
|
| 63 |
+
block_q = torch.where(x0_flag_q == 1,
|
| 64 |
+
(q_idx - n) // block_size,
|
| 65 |
+
q_idx // block_size)
|
| 66 |
+
block_kv = torch.where(x0_flag_kv == 1,
|
| 67 |
+
(kv_idx - n) // block_size,
|
| 68 |
+
kv_idx // block_size)
|
| 69 |
+
|
| 70 |
+
# **1. Block Diagonal Mask (M_BD) **
|
| 71 |
+
block_diagonal = (block_q == block_kv) & (x0_flag_kv == 0) & (x0_flag_q == 0)
|
| 72 |
+
|
| 73 |
+
# **2. Offset Block-Causal Mask (M_OBC) **
|
| 74 |
+
offset_block_causal = (
|
| 75 |
+
(block_q > block_kv)
|
| 76 |
+
& (x0_flag_kv == 1)
|
| 77 |
+
& (x0_flag_q == 0)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# **3. Fully Causal Mask (M_BC) **
|
| 81 |
+
fully_causal = (q_idx >= kv_idx) & (x0_flag_kv == 1) & (x0_flag_q == 1)
|
| 82 |
+
|
| 83 |
+
# **4. Combine Masks **
|
| 84 |
+
return block_diagonal | offset_block_causal | fully_causal
|
| 85 |
+
|
| 86 |
+
attn_mask = lambda b, h, q, kv: block_diff_mask(block_size, b, h, q, kv, q_len//2)
|
| 87 |
+
|
| 88 |
+
block_mask = create_block_mask(
|
| 89 |
+
attn_mask, B=None, H=None, Q_LEN=q_len, KV_LEN=q_len
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return block_mask
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def forward(
|
| 96 |
+
self,
|
| 97 |
+
hidden_states: torch.Tensor,
|
| 98 |
+
position_embeddings: Tuple[torch.Tensor, torch.Tensor],
|
| 99 |
+
attention_mask: Optional[torch.Tensor],
|
| 100 |
+
past_key_values: Optional[Cache] = None,
|
| 101 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 102 |
+
is_training: bool = True,
|
| 103 |
+
**kwargs: Unpack[FlashAttentionKwargs],
|
| 104 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
| 105 |
+
bsz, q_len, _ = hidden_states.size()
|
| 106 |
+
input_shape = hidden_states.shape[:-1]
|
| 107 |
+
hidden_shape = (*input_shape, -1, self.head_dim)
|
| 108 |
+
|
| 109 |
+
query_states = self.q_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 110 |
+
key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 111 |
+
value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
|
| 112 |
+
|
| 113 |
+
cos, sin = position_embeddings
|
| 114 |
+
|
| 115 |
+
if is_training:
|
| 116 |
+
# Split query and key states in half along sequence length dimension
|
| 117 |
+
q1, q2 = query_states.chunk(2, dim=2)
|
| 118 |
+
k1, k2 = key_states.chunk(2, dim=2)
|
| 119 |
+
|
| 120 |
+
# Apply RoPE independently to each half
|
| 121 |
+
q1, k1 = apply_rotary_pos_emb(q1, k1, cos, sin)
|
| 122 |
+
q2, k2 = apply_rotary_pos_emb(q2, k2, cos, sin)
|
| 123 |
+
|
| 124 |
+
# Recombine the halves
|
| 125 |
+
query_states = torch.cat([q1, q2], dim=2)
|
| 126 |
+
key_states = torch.cat([k1, k2], dim=2)
|
| 127 |
+
else:
|
| 128 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
|
| 129 |
+
|
| 130 |
+
query_states = query_states * _get_llama_4_attn_scale(
|
| 131 |
+
cache_position,
|
| 132 |
+
self.config.rope_parameters.get("llama_4_scaling_beta"),
|
| 133 |
+
self.config.rope_parameters.get("original_max_position_embeddings"),
|
| 134 |
+
).to(query_states.dtype)
|
| 135 |
+
|
| 136 |
+
if past_key_values is not None:
|
| 137 |
+
# sin and cos are specific to RoPE models; cache_position needed for the static cache
|
| 138 |
+
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
|
| 139 |
+
key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
| 140 |
+
|
| 141 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
| 142 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
| 143 |
+
|
| 144 |
+
if self.block_diff_mask is None or q_len != self.block_diff_mask.shape[-2]:
|
| 145 |
+
block_mask = self.compute_block_mask(mode='block_diff', block_size=self.block_size, q_len=q_len)
|
| 146 |
+
else:
|
| 147 |
+
block_mask = self.block_diff_mask
|
| 148 |
+
|
| 149 |
+
attn_output = fused_flex_attention(query_states, key_states, value_states, block_mask=block_mask)
|
| 150 |
+
attn_output = attn_output.transpose(1, 2).reshape(*input_shape, -1).contiguous()
|
| 151 |
+
|
| 152 |
+
attn_output = self.o_proj(attn_output)
|
| 153 |
+
|
| 154 |
+
return attn_output, None
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
class NemotronLabsDiffusionModel(Ministral3PreTrainedModel, GenerationMixin):
|
| 158 |
+
"""
|
| 159 |
+
A single model with:
|
| 160 |
+
- a bidirectional encoder + diffusion‐LM head over A
|
| 161 |
+
- a causal decoder + LM head over B, conditioned on F_A
|
| 162 |
+
"""
|
| 163 |
+
|
| 164 |
+
def __init__(self, config: NemotronLabsDiffusionConfig):
|
| 165 |
+
super().__init__(config)
|
| 166 |
+
|
| 167 |
+
self.mask_token_id = config.mask_token_id
|
| 168 |
+
|
| 169 |
+
diffusion_config = copy.deepcopy(config)
|
| 170 |
+
diffusion_config.diffusion_lm = True
|
| 171 |
+
|
| 172 |
+
if config.dlm_paradigm == 'block_diff':
|
| 173 |
+
diffusion_config.attn_class = NemotronLabsDiffusionFlexAttention
|
| 174 |
+
elif config.dlm_paradigm in ['bidirectional', 'autoregressive']:
|
| 175 |
+
diffusion_config.attn_class = Ministral3Attention
|
| 176 |
+
if config.dlm_paradigm == 'autoregressive':
|
| 177 |
+
diffusion_config.diffusion_lm = False
|
| 178 |
+
else:
|
| 179 |
+
raise ValueError(f"Unsupported DLM paradigm: {config.dlm_paradigm}")
|
| 180 |
+
|
| 181 |
+
self.encoder = Ministral3Model(diffusion_config)
|
| 182 |
+
self.diffusion_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 183 |
+
self.vocab_size = config.vocab_size
|
| 184 |
+
|
| 185 |
+
self.post_init()
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def get_input_embeddings(self):
|
| 189 |
+
return self.encoder.embed_tokens
|
| 190 |
+
|
| 191 |
+
def set_input_embeddings(self, value):
|
| 192 |
+
self.encoder.embed_tokens = value
|
| 193 |
+
|
| 194 |
+
def get_output_embeddings(self):
|
| 195 |
+
return self.diffusion_head
|
| 196 |
+
|
| 197 |
+
def set_output_embeddings(self, new_embeddings):
|
| 198 |
+
self.diffusion_head = new_embeddings
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def forward_process(self, input_ids, eps=1e-3, block_size=None, loss_mask=None):
|
| 202 |
+
b, l = input_ids.shape
|
| 203 |
+
device = input_ids.device
|
| 204 |
+
|
| 205 |
+
if self.config.dp_varying_mask_ratio:
|
| 206 |
+
# Enable different random seeds for each DP rank during sampling
|
| 207 |
+
import torch.distributed as dist
|
| 208 |
+
dp_rank = 0
|
| 209 |
+
if dist.is_initialized():
|
| 210 |
+
try:
|
| 211 |
+
dp_rank = dist.get_rank()
|
| 212 |
+
except Exception:
|
| 213 |
+
dp_rank = 0
|
| 214 |
+
# Use a local generator to avoid affecting global RNG state
|
| 215 |
+
generator = torch.Generator(device=device)
|
| 216 |
+
generator.manual_seed(torch.seed() + dp_rank)
|
| 217 |
+
else:
|
| 218 |
+
generator = None
|
| 219 |
+
|
| 220 |
+
t = torch.rand(b, device=device, generator=generator)
|
| 221 |
+
|
| 222 |
+
p_mask = (1 - eps) * t + eps # shape: (b,)
|
| 223 |
+
p_mask = p_mask[:, None].expand(-1, l) # shape: (b, l)
|
| 224 |
+
|
| 225 |
+
masked_indices = torch.rand((b, l), device=device) < p_mask
|
| 226 |
+
|
| 227 |
+
if loss_mask is not None:
|
| 228 |
+
masked_indices[loss_mask == 0] = 0
|
| 229 |
+
|
| 230 |
+
noisy_batch = torch.where(masked_indices, self.mask_token_id, input_ids)
|
| 231 |
+
|
| 232 |
+
return noisy_batch, masked_indices, p_mask
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def forward(
|
| 236 |
+
self,
|
| 237 |
+
input_ids: torch.LongTensor,
|
| 238 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 239 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 240 |
+
labels: Optional[torch.LongTensor] = None,
|
| 241 |
+
split_len: Optional[int] = None,
|
| 242 |
+
past_key_values: Optional[Cache] = None,
|
| 243 |
+
block_size: Optional[int] = None,
|
| 244 |
+
eps: float = 1e-3,
|
| 245 |
+
is_teacher: bool = False,
|
| 246 |
+
masked_indices: Optional[torch.Tensor] = None,
|
| 247 |
+
p_mask: Optional[torch.Tensor] = None,
|
| 248 |
+
teacher_logits: Optional[torch.Tensor] = None,
|
| 249 |
+
masked_indices_teacher: Optional[torch.Tensor] = None,
|
| 250 |
+
loss_mask: Optional[torch.Tensor] = None,
|
| 251 |
+
ce_loss_weight: float = 1.0,
|
| 252 |
+
output_last_hidden_states_only: bool = False,
|
| 253 |
+
skip_loss: bool = False,
|
| 254 |
+
**kwargs,
|
| 255 |
+
) -> CausalLMOutputWithPast:
|
| 256 |
+
|
| 257 |
+
batch_size, seq_len = input_ids.shape
|
| 258 |
+
|
| 259 |
+
if self.config.dlm_paradigm == 'block_diff':
|
| 260 |
+
if labels is not None and block_size is None:
|
| 261 |
+
block_size = self.config.block_size
|
| 262 |
+
elif self.config.dlm_paradigm not in ('bidirectional', 'autoregressive'):
|
| 263 |
+
raise ValueError(f"Unknown dLM paradigm: {self.config.dlm_paradigm}")
|
| 264 |
+
|
| 265 |
+
if labels is not None and self.config.dlm_paradigm != 'autoregressive':
|
| 266 |
+
if masked_indices is not None:
|
| 267 |
+
# assert p_mask is not None
|
| 268 |
+
|
| 269 |
+
if loss_mask is not None:
|
| 270 |
+
masked_indices[loss_mask == 0] = 0
|
| 271 |
+
|
| 272 |
+
noisy_inputs = torch.where(masked_indices, self.mask_token_id, input_ids)
|
| 273 |
+
|
| 274 |
+
else:
|
| 275 |
+
noisy_inputs, masked_indices, p_mask = self.forward_process(input_ids, eps=eps, block_size=block_size, loss_mask=loss_mask)
|
| 276 |
+
|
| 277 |
+
else:
|
| 278 |
+
noisy_inputs = input_ids
|
| 279 |
+
masked_indices = None
|
| 280 |
+
p_mask = None
|
| 281 |
+
|
| 282 |
+
input_ids_len = noisy_inputs.shape[1]
|
| 283 |
+
if labels is not None and self.config.dlm_paradigm == 'block_diff':
|
| 284 |
+
if position_ids is None:
|
| 285 |
+
position_ids = torch.arange(input_ids_len, device=noisy_inputs.device).unsqueeze(0)
|
| 286 |
+
noisy_inputs = torch.cat([noisy_inputs, input_ids], dim=1)
|
| 287 |
+
|
| 288 |
+
enc_out = self.encoder(
|
| 289 |
+
past_key_values=past_key_values,
|
| 290 |
+
input_ids=noisy_inputs,
|
| 291 |
+
attention_mask=attention_mask,
|
| 292 |
+
position_ids=position_ids,
|
| 293 |
+
is_training=(labels is not None),
|
| 294 |
+
**kwargs,
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
if output_last_hidden_states_only:
|
| 298 |
+
return BaseModelOutput(last_hidden_state=enc_out.last_hidden_state)
|
| 299 |
+
|
| 300 |
+
logits = self.diffusion_head(enc_out.last_hidden_state) # (batch, len_B, vocab)
|
| 301 |
+
causal_logits = None
|
| 302 |
+
|
| 303 |
+
if labels is not None and self.config.dlm_paradigm == 'block_diff':
|
| 304 |
+
causal_logits = logits[:, input_ids_len:]
|
| 305 |
+
logits = logits[:, :input_ids_len]
|
| 306 |
+
|
| 307 |
+
loss = None
|
| 308 |
+
if labels is not None and not skip_loss:
|
| 309 |
+
if self.config.dlm_paradigm == 'autoregressive':
|
| 310 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 311 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 312 |
+
|
| 313 |
+
if loss_mask is None:
|
| 314 |
+
loss_fct = CrossEntropyLoss()
|
| 315 |
+
shift_logits = shift_logits.view(-1, shift_logits.size(-1))
|
| 316 |
+
shift_labels = shift_labels.view(-1)
|
| 317 |
+
loss = loss_fct(shift_logits, shift_labels)
|
| 318 |
+
|
| 319 |
+
else:
|
| 320 |
+
loss_mask = loss_mask[..., 1:].contiguous()
|
| 321 |
+
|
| 322 |
+
loss_fct = CrossEntropyLoss(reduction='none')
|
| 323 |
+
shift_logits = shift_logits.view(-1, shift_logits.size(-1))
|
| 324 |
+
shift_labels = shift_labels.view(-1)
|
| 325 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
| 326 |
+
|
| 327 |
+
token_losses = loss_fct(shift_logits, shift_labels)
|
| 328 |
+
|
| 329 |
+
flat_loss_mask = loss_mask.reshape(-1)
|
| 330 |
+
loss = token_losses[flat_loss_mask == 1].sum() / flat_loss_mask.sum()
|
| 331 |
+
|
| 332 |
+
else:
|
| 333 |
+
# LLaDA-style diffusion loss on masked positions.
|
| 334 |
+
# Token-wise cross entropy loss on masked positions.
|
| 335 |
+
token_loss = torch.nn.functional.cross_entropy(
|
| 336 |
+
logits[masked_indices],
|
| 337 |
+
labels[masked_indices],
|
| 338 |
+
reduction='none'
|
| 339 |
+
) / p_mask[masked_indices]
|
| 340 |
+
|
| 341 |
+
num_mask_tokens = masked_indices.sum()
|
| 342 |
+
|
| 343 |
+
# global_loss_avg=True: loss is reduced externally by global token count.
|
| 344 |
+
loss = token_loss.sum()
|
| 345 |
+
|
| 346 |
+
if self.config.dlm_loss_weight is not None:
|
| 347 |
+
loss = self.config.dlm_loss_weight * loss
|
| 348 |
+
|
| 349 |
+
if self.config.dlm_paradigm == 'block_diff':
|
| 350 |
+
# AR-side loss for block-diffusion paradigm.
|
| 351 |
+
causal_logits = causal_logits[..., :-1, :].contiguous()
|
| 352 |
+
causal_logits = causal_logits.view(-1, causal_logits.size(-1))
|
| 353 |
+
causal_labels = labels[..., 1:].contiguous().view(-1)
|
| 354 |
+
|
| 355 |
+
loss_fct = CrossEntropyLoss(reduction='sum')
|
| 356 |
+
ar_loss = loss_fct(causal_logits, causal_labels)
|
| 357 |
+
|
| 358 |
+
self.loss_diffusion = loss.detach().item() / num_mask_tokens
|
| 359 |
+
self.loss_ar = ar_loss.detach().item() / seq_len
|
| 360 |
+
|
| 361 |
+
loss = loss + self.config.ar_loss_weight * ar_loss
|
| 362 |
+
|
| 363 |
+
# global_loss_avg=True: return (sum_loss, token_count) for external mean.
|
| 364 |
+
if self.config.dlm_paradigm == 'block_diff':
|
| 365 |
+
loss = (loss, num_mask_tokens + int(self.config.ar_loss_weight * seq_len))
|
| 366 |
+
else:
|
| 367 |
+
loss = (loss, num_mask_tokens)
|
| 368 |
+
|
| 369 |
+
return NemotronLabsDiffusionOutputWithPast(
|
| 370 |
+
loss=loss if not is_teacher else logits,
|
| 371 |
+
logits=logits,
|
| 372 |
+
causal_logits=causal_logits,
|
| 373 |
+
past_key_values=enc_out.past_key_values,
|
| 374 |
+
hidden_states=None,
|
| 375 |
+
attentions=None,
|
| 376 |
+
)
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
@torch.no_grad()
|
| 380 |
+
def generate(
|
| 381 |
+
self,
|
| 382 |
+
prompt_ids: torch.Tensor,
|
| 383 |
+
max_new_tokens: int,
|
| 384 |
+
block_length: int,
|
| 385 |
+
threshold: Optional[float] = None,
|
| 386 |
+
causal_context: bool = True,
|
| 387 |
+
temperature: float = 0.0,
|
| 388 |
+
eos_token_id: Optional[int] = None,
|
| 389 |
+
max_thinking_tokens: Optional[int] = None,
|
| 390 |
+
end_think_token_id: Optional[int] = None,
|
| 391 |
+
):
|
| 392 |
+
"""Block-wise diffusion decoding with prefix-cached KV (LLaDA-style).
|
| 393 |
+
|
| 394 |
+
Each block: append `block_length` mask tokens, then iteratively unmask
|
| 395 |
+
by confidence top-k (with optional threshold). When `causal_context`,
|
| 396 |
+
the KV cache and the next-block seed are produced via a causal forward
|
| 397 |
+
between blocks (flipping `self_attn.diffusion_lm`), matching the AR
|
| 398 |
+
objective at block boundaries.
|
| 399 |
+
|
| 400 |
+
Returns (output_ids, nfe) — output_ids includes the prompt.
|
| 401 |
+
"""
|
| 402 |
+
if eos_token_id is None:
|
| 403 |
+
eos_token_id = getattr(self.config, "eos_token_id", None)
|
| 404 |
+
mask_id = self.mask_token_id
|
| 405 |
+
|
| 406 |
+
x_accum = prompt_ids.clone()
|
| 407 |
+
B = prompt_ids.shape[0]
|
| 408 |
+
|
| 409 |
+
assert max_new_tokens % block_length == 0
|
| 410 |
+
num_blocks = max_new_tokens // block_length
|
| 411 |
+
# one denoising step per generated token (matches legacy chat_utils call)
|
| 412 |
+
steps_per_block = block_length
|
| 413 |
+
|
| 414 |
+
nfe = 0
|
| 415 |
+
|
| 416 |
+
def _set_diffusion_lm(val: bool):
|
| 417 |
+
for layer in self.encoder.layers:
|
| 418 |
+
if hasattr(layer.self_attn, "diffusion_lm"):
|
| 419 |
+
layer.self_attn.diffusion_lm = val
|
| 420 |
+
|
| 421 |
+
# Initial causal prefill produces the KV cache and the next-block seed.
|
| 422 |
+
if causal_context:
|
| 423 |
+
_set_diffusion_lm(False)
|
| 424 |
+
output = self(prompt_ids, use_cache=True, use_causal_mask=causal_context)
|
| 425 |
+
past_key_values = output.past_key_values
|
| 426 |
+
if causal_context:
|
| 427 |
+
_set_diffusion_lm(True)
|
| 428 |
+
|
| 429 |
+
next_token = None
|
| 430 |
+
if causal_context:
|
| 431 |
+
last_logit = output.logits[:, -1, :]
|
| 432 |
+
if temperature > 0:
|
| 433 |
+
next_token = torch.multinomial(torch.softmax(last_logit / temperature, dim=-1), num_samples=1)
|
| 434 |
+
else:
|
| 435 |
+
next_token = torch.argmax(last_logit, dim=-1, keepdim=True)
|
| 436 |
+
|
| 437 |
+
for num_block in range(num_blocks):
|
| 438 |
+
mask_block = torch.full(
|
| 439 |
+
(B, block_length), mask_id, dtype=prompt_ids.dtype, device=prompt_ids.device,
|
| 440 |
+
)
|
| 441 |
+
if causal_context:
|
| 442 |
+
mask_block[:, 0] = next_token[:, 0]
|
| 443 |
+
|
| 444 |
+
x_accum = torch.cat([x_accum, mask_block], dim=1)
|
| 445 |
+
block_start = prompt_ids.size(1) + num_block * block_length
|
| 446 |
+
block_slice = slice(block_start, block_start + block_length)
|
| 447 |
+
|
| 448 |
+
# Thinking-budget enforcement: if we've passed max_thinking_tokens
|
| 449 |
+
# without an end-think marker, inject one into this block.
|
| 450 |
+
if end_think_token_id is not None and max_thinking_tokens is not None:
|
| 451 |
+
tokens_before = num_block * block_length
|
| 452 |
+
tokens_after = tokens_before + block_length
|
| 453 |
+
if tokens_after > max_thinking_tokens:
|
| 454 |
+
gen_so_far = x_accum[:, prompt_ids.size(1):block_start]
|
| 455 |
+
has_end_think = (
|
| 456 |
+
(gen_so_far == end_think_token_id).any(dim=1)
|
| 457 |
+
if gen_so_far.size(1) > 0
|
| 458 |
+
else torch.zeros(B, dtype=torch.bool, device=prompt_ids.device)
|
| 459 |
+
)
|
| 460 |
+
if not has_end_think.all():
|
| 461 |
+
offset = max(0, max_thinking_tokens - tokens_before)
|
| 462 |
+
inject_pos = block_start + offset
|
| 463 |
+
for b in range(B):
|
| 464 |
+
if not has_end_think[b]:
|
| 465 |
+
x_accum[b, inject_pos] = end_think_token_id
|
| 466 |
+
|
| 467 |
+
mask_block_idx0 = x_accum[:, block_slice] == mask_id
|
| 468 |
+
num_transfer_tokens = _get_num_transfer_tokens(mask_block_idx0, steps_per_block)
|
| 469 |
+
|
| 470 |
+
# Denoise the current block by repeated confidence-based unmasking.
|
| 471 |
+
for i in range(steps_per_block):
|
| 472 |
+
mask_block_idx = x_accum[:, block_slice] == mask_id
|
| 473 |
+
if mask_block_idx.sum() == 0:
|
| 474 |
+
break
|
| 475 |
+
|
| 476 |
+
nfe += 1
|
| 477 |
+
logits_block = self(
|
| 478 |
+
x_accum[:, block_slice],
|
| 479 |
+
past_key_values=past_key_values,
|
| 480 |
+
use_cache=False,
|
| 481 |
+
).logits
|
| 482 |
+
|
| 483 |
+
x0, transfer_idx = _get_transfer_index(
|
| 484 |
+
logits_block, temperature, mask_block_idx, x_accum[:, block_slice],
|
| 485 |
+
num_transfer_tokens=num_transfer_tokens[:, i], threshold=threshold,
|
| 486 |
+
)
|
| 487 |
+
cur = x_accum[:, block_slice].clone()
|
| 488 |
+
cur[transfer_idx] = x0[transfer_idx]
|
| 489 |
+
x_accum[:, block_slice] = cur
|
| 490 |
+
|
| 491 |
+
if eos_token_id is not None:
|
| 492 |
+
block_tokens = x_accum[:, block_slice]
|
| 493 |
+
eos_mask = block_tokens == eos_token_id
|
| 494 |
+
if eos_mask.any(dim=1).any():
|
| 495 |
+
after_eos = eos_mask.cumsum(dim=1).bool()
|
| 496 |
+
mask_before = (block_tokens == mask_id) & ~after_eos
|
| 497 |
+
if (eos_mask.any(dim=1) & ~mask_before.any(dim=1)).any():
|
| 498 |
+
break
|
| 499 |
+
|
| 500 |
+
# Post-block: causal forward over the block to update the KV cache
|
| 501 |
+
# and (when causal_context) sample the seed for the next block.
|
| 502 |
+
if causal_context:
|
| 503 |
+
_set_diffusion_lm(False)
|
| 504 |
+
output = self(
|
| 505 |
+
x_accum[:, block_slice],
|
| 506 |
+
past_key_values=past_key_values,
|
| 507 |
+
use_cache=True,
|
| 508 |
+
use_causal_mask=causal_context,
|
| 509 |
+
)
|
| 510 |
+
past_key_values = output.past_key_values
|
| 511 |
+
nfe += 1
|
| 512 |
+
|
| 513 |
+
if causal_context:
|
| 514 |
+
_set_diffusion_lm(True)
|
| 515 |
+
last_logit = output.logits[:, -1, :]
|
| 516 |
+
if temperature > 0:
|
| 517 |
+
next_token = torch.multinomial(torch.softmax(last_logit / temperature, dim=-1), num_samples=1)
|
| 518 |
+
else:
|
| 519 |
+
next_token = torch.argmax(last_logit, dim=-1, keepdim=True)
|
| 520 |
+
|
| 521 |
+
if eos_token_id is not None:
|
| 522 |
+
gen_so_far = x_accum[:, prompt_ids.size(1):]
|
| 523 |
+
is_eos = gen_so_far == eos_token_id
|
| 524 |
+
if is_eos.any(dim=1).all():
|
| 525 |
+
first_eos = is_eos.to(torch.int64).argmax(dim=1)
|
| 526 |
+
max_eos = first_eos.max().item()
|
| 527 |
+
return x_accum[:, : prompt_ids.size(1) + max_eos + 1], nfe
|
| 528 |
+
|
| 529 |
+
return x_accum, nfe
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
@torch.no_grad()
|
| 534 |
+
def ar_generate(
|
| 535 |
+
self,
|
| 536 |
+
prompt_ids: torch.Tensor,
|
| 537 |
+
max_new_tokens: int = 128,
|
| 538 |
+
temperature: float = 0.0,
|
| 539 |
+
eos_token_id: Optional[int] = None,
|
| 540 |
+
max_thinking_tokens: Optional[int] = None,
|
| 541 |
+
end_think_token_id: Optional[int] = None,
|
| 542 |
+
) -> tuple:
|
| 543 |
+
"""Autoregressive generation calling the encoder directly (injected by build_hf_tidar_repo).
|
| 544 |
+
|
| 545 |
+
Bypasses NemotronLabsDiffusionModel.forward() to avoid diffusion-specific
|
| 546 |
+
code paths. Calls self.encoder (Ministral3Model) with explicit cache_position,
|
| 547 |
+
position_ids, and use_cache so the KV cache and causal masking behave
|
| 548 |
+
identically to MistralForCausalLM / vLLM.
|
| 549 |
+
|
| 550 |
+
Returns:
|
| 551 |
+
(output_ids, nfe) where output_ids includes the prompt.
|
| 552 |
+
"""
|
| 553 |
+
for layer in self.encoder.layers:
|
| 554 |
+
if hasattr(layer.self_attn, 'diffusion_lm'):
|
| 555 |
+
layer.self_attn.diffusion_lm = False
|
| 556 |
+
|
| 557 |
+
if eos_token_id is None:
|
| 558 |
+
eos_token_id = getattr(self.config, 'eos_token_id', None)
|
| 559 |
+
|
| 560 |
+
device = prompt_ids.device
|
| 561 |
+
batch_size, prompt_len = prompt_ids.shape
|
| 562 |
+
|
| 563 |
+
past_key_values = DynamicCache()
|
| 564 |
+
cache_position = torch.arange(prompt_len, device=device)
|
| 565 |
+
position_ids = cache_position.unsqueeze(0).expand(batch_size, -1)
|
| 566 |
+
|
| 567 |
+
enc_out = self.encoder(
|
| 568 |
+
input_ids=prompt_ids,
|
| 569 |
+
position_ids=position_ids,
|
| 570 |
+
past_key_values=past_key_values,
|
| 571 |
+
use_cache=True,
|
| 572 |
+
cache_position=cache_position,
|
| 573 |
+
)
|
| 574 |
+
past_key_values = enc_out.past_key_values
|
| 575 |
+
next_logit = self.diffusion_head(enc_out.last_hidden_state[:, -1:, :]).squeeze(1)
|
| 576 |
+
|
| 577 |
+
generated_tokens = []
|
| 578 |
+
nfe = 0
|
| 579 |
+
|
| 580 |
+
for step in range(max_new_tokens):
|
| 581 |
+
nfe += 1
|
| 582 |
+
|
| 583 |
+
if temperature > 0:
|
| 584 |
+
probs = torch.softmax(next_logit / temperature, dim=-1)
|
| 585 |
+
next_token = torch.multinomial(probs, num_samples=1)
|
| 586 |
+
else:
|
| 587 |
+
next_token = torch.argmax(next_logit, dim=-1, keepdim=True)
|
| 588 |
+
|
| 589 |
+
# ---- thinking budget enforcement ----
|
| 590 |
+
if end_think_token_id is not None and max_thinking_tokens is not None:
|
| 591 |
+
if step >= max_thinking_tokens:
|
| 592 |
+
if generated_tokens:
|
| 593 |
+
gen_tensor = torch.cat(generated_tokens, dim=1)
|
| 594 |
+
has_end_think = (gen_tensor == end_think_token_id).any(dim=1)
|
| 595 |
+
else:
|
| 596 |
+
has_end_think = torch.zeros(batch_size, dtype=torch.bool, device=device)
|
| 597 |
+
for b in range(batch_size):
|
| 598 |
+
if not has_end_think[b]:
|
| 599 |
+
next_token[b] = end_think_token_id
|
| 600 |
+
|
| 601 |
+
generated_tokens.append(next_token)
|
| 602 |
+
|
| 603 |
+
if eos_token_id is not None and (next_token == eos_token_id).all():
|
| 604 |
+
break
|
| 605 |
+
|
| 606 |
+
if step < max_new_tokens - 1:
|
| 607 |
+
cur_pos = prompt_len + step
|
| 608 |
+
step_cache_pos = torch.tensor([cur_pos], device=device)
|
| 609 |
+
step_pos_ids = step_cache_pos.unsqueeze(0).expand(batch_size, -1)
|
| 610 |
+
|
| 611 |
+
enc_out = self.encoder(
|
| 612 |
+
input_ids=next_token,
|
| 613 |
+
position_ids=step_pos_ids,
|
| 614 |
+
past_key_values=past_key_values,
|
| 615 |
+
use_cache=True,
|
| 616 |
+
cache_position=step_cache_pos,
|
| 617 |
+
)
|
| 618 |
+
past_key_values = enc_out.past_key_values
|
| 619 |
+
next_logit = self.diffusion_head(enc_out.last_hidden_state[:, -1:, :]).squeeze(1)
|
| 620 |
+
|
| 621 |
+
all_generated = torch.cat(generated_tokens, dim=1)
|
| 622 |
+
output_ids = torch.cat([prompt_ids, all_generated], dim=1)
|
| 623 |
+
return output_ids, nfe
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
@torch.no_grad()
|
| 627 |
+
def linear_spec_generate(
|
| 628 |
+
self,
|
| 629 |
+
prompt_ids: torch.Tensor,
|
| 630 |
+
max_new_tokens: int = 128,
|
| 631 |
+
block_length: int = 32,
|
| 632 |
+
temperature: float = 0.0,
|
| 633 |
+
mask_token_id: Optional[int] = None,
|
| 634 |
+
eos_token_id: Optional[int] = None,
|
| 635 |
+
max_thinking_tokens: Optional[int] = None,
|
| 636 |
+
end_think_token_id: Optional[int] = None,
|
| 637 |
+
threshold: float = 0.0,
|
| 638 |
+
):
|
| 639 |
+
"""Linear speculative decoding: diffusion draft + AR verify.
|
| 640 |
+
|
| 641 |
+
Each iteration: (1) draft the next block under bidirectional attention,
|
| 642 |
+
(2) verify the drafted block under causal attention, accept the longest
|
| 643 |
+
prefix where draft matches AR + one bonus token, advance the KV cache.
|
| 644 |
+
|
| 645 |
+
LoRA-aware: when a PEFT adapter is attached to the model (e.g.
|
| 646 |
+
``linear_spec_lora``), it is toggled ON for the bidirectional draft
|
| 647 |
+
phase and OFF for the causal prefill / verify phases — so the adapter
|
| 648 |
+
only specializes the diffusion-mode forward and AR semantics are
|
| 649 |
+
preserved. With no adapter loaded the calls are no-ops.
|
| 650 |
+
|
| 651 |
+
Returns ``(output_ids, nfe)`` — ``output_ids`` includes the prompt.
|
| 652 |
+
"""
|
| 653 |
+
if prompt_ids.shape[0] != 1:
|
| 654 |
+
raise ValueError("Linear speculative decoding requires batch_size == 1")
|
| 655 |
+
|
| 656 |
+
token_mask_id = mask_token_id if mask_token_id is not None else self.config.mask_token_id
|
| 657 |
+
if eos_token_id is None:
|
| 658 |
+
eos_token_id = getattr(self.config, "eos_token_id", None)
|
| 659 |
+
|
| 660 |
+
device = prompt_ids.device
|
| 661 |
+
|
| 662 |
+
def _set_diffusion_lm(val: bool):
|
| 663 |
+
for layer in self.encoder.layers:
|
| 664 |
+
if hasattr(layer.self_attn, "diffusion_lm"):
|
| 665 |
+
layer.self_attn.diffusion_lm = val
|
| 666 |
+
|
| 667 |
+
def _toggle_adapters(enable: bool):
|
| 668 |
+
# No-op when no PEFT/LoRA modules are attached.
|
| 669 |
+
for module in self.modules():
|
| 670 |
+
if hasattr(module, "_disable_adapters"):
|
| 671 |
+
module._disable_adapters = not enable
|
| 672 |
+
|
| 673 |
+
# Prefill (causal, LoRA OFF).
|
| 674 |
+
_set_diffusion_lm(False)
|
| 675 |
+
_toggle_adapters(False)
|
| 676 |
+
enc_out = self.encoder(
|
| 677 |
+
input_ids=prompt_ids,
|
| 678 |
+
past_key_values=DynamicCache(),
|
| 679 |
+
use_cache=True,
|
| 680 |
+
use_causal_mask=True,
|
| 681 |
+
)
|
| 682 |
+
past_key_values = enc_out.past_key_values
|
| 683 |
+
last_logit = self.diffusion_head(enc_out.last_hidden_state[:, -1:, :]).squeeze(1)
|
| 684 |
+
nfe = 1
|
| 685 |
+
|
| 686 |
+
if temperature > 0:
|
| 687 |
+
next_token = torch.multinomial(torch.softmax(last_logit / temperature, dim=-1), num_samples=1)
|
| 688 |
+
else:
|
| 689 |
+
next_token = torch.argmax(last_logit, dim=-1, keepdim=True)
|
| 690 |
+
|
| 691 |
+
if eos_token_id is not None and next_token.item() == eos_token_id:
|
| 692 |
+
return torch.cat([prompt_ids, next_token], dim=1), nfe
|
| 693 |
+
|
| 694 |
+
generated = [next_token]
|
| 695 |
+
total_gen = 1
|
| 696 |
+
|
| 697 |
+
while total_gen < max_new_tokens:
|
| 698 |
+
cache_len = past_key_values.get_seq_length()
|
| 699 |
+
|
| 700 |
+
block = torch.full((1, block_length), token_mask_id, dtype=torch.long, device=device)
|
| 701 |
+
block[0, 0] = next_token.item()
|
| 702 |
+
|
| 703 |
+
# Draft phase (bidirectional, LoRA ON) — iterate at threshold>0 so
|
| 704 |
+
# that even low-confidence blocks make progress.
|
| 705 |
+
_set_diffusion_lm(True)
|
| 706 |
+
_toggle_adapters(True)
|
| 707 |
+
while True:
|
| 708 |
+
is_mask = block == token_mask_id
|
| 709 |
+
if not is_mask.any():
|
| 710 |
+
break
|
| 711 |
+
|
| 712 |
+
enc_out = self.encoder(input_ids=block, past_key_values=past_key_values, use_cache=False)
|
| 713 |
+
nfe += 1
|
| 714 |
+
|
| 715 |
+
draft_logits = self.diffusion_head(enc_out.last_hidden_state)
|
| 716 |
+
# LLaDA: logit[i] directly predicts position i — no shift needed.
|
| 717 |
+
|
| 718 |
+
if temperature > 0:
|
| 719 |
+
draft_probs = torch.softmax(draft_logits / temperature, dim=-1)
|
| 720 |
+
draft_tokens = torch.multinomial(
|
| 721 |
+
draft_probs.view(-1, draft_probs.shape[-1]), num_samples=1
|
| 722 |
+
).view(1, block_length)
|
| 723 |
+
else:
|
| 724 |
+
draft_tokens = draft_logits.argmax(dim=-1)
|
| 725 |
+
draft_probs = torch.softmax(draft_logits, dim=-1)
|
| 726 |
+
|
| 727 |
+
if threshold > 0:
|
| 728 |
+
draft_conf = torch.gather(draft_probs, -1, draft_tokens.unsqueeze(-1)).squeeze(-1)
|
| 729 |
+
draft_conf = torch.where(is_mask, draft_conf, -torch.inf)
|
| 730 |
+
unmask = draft_conf >= threshold
|
| 731 |
+
# Force progress even when every masked position is below threshold.
|
| 732 |
+
if not unmask.any():
|
| 733 |
+
best_idx = draft_conf.view(-1).argmax()
|
| 734 |
+
unmask = torch.zeros_like(is_mask, dtype=torch.bool)
|
| 735 |
+
unmask.view(-1)[best_idx] = True
|
| 736 |
+
block[unmask] = draft_tokens[unmask]
|
| 737 |
+
else:
|
| 738 |
+
block[is_mask] = draft_tokens[is_mask]
|
| 739 |
+
break
|
| 740 |
+
|
| 741 |
+
# Verify phase (causal, LoRA OFF).
|
| 742 |
+
_set_diffusion_lm(False)
|
| 743 |
+
_toggle_adapters(False)
|
| 744 |
+
enc_out = self.encoder(
|
| 745 |
+
input_ids=block,
|
| 746 |
+
past_key_values=past_key_values,
|
| 747 |
+
use_cache=True,
|
| 748 |
+
use_causal_mask=True,
|
| 749 |
+
)
|
| 750 |
+
past_key_values = enc_out.past_key_values
|
| 751 |
+
nfe += 1
|
| 752 |
+
|
| 753 |
+
verify_logits = self.diffusion_head(enc_out.last_hidden_state)
|
| 754 |
+
if temperature > 0:
|
| 755 |
+
ar_tokens = torch.multinomial(
|
| 756 |
+
torch.softmax(verify_logits / temperature, dim=-1).view(-1, verify_logits.shape[-1]),
|
| 757 |
+
num_samples=1,
|
| 758 |
+
).view(1, block_length)
|
| 759 |
+
else:
|
| 760 |
+
ar_tokens = verify_logits.argmax(dim=-1)
|
| 761 |
+
|
| 762 |
+
# Accept consecutive matches; AR also gives one bonus token at the tail.
|
| 763 |
+
accepted = 0
|
| 764 |
+
for i in range(block_length - 1):
|
| 765 |
+
if ar_tokens[0, i].item() == block[0, i + 1].item():
|
| 766 |
+
accepted += 1
|
| 767 |
+
else:
|
| 768 |
+
break
|
| 769 |
+
accepted += 1
|
| 770 |
+
|
| 771 |
+
accepted_toks = ar_tokens[:, :accepted]
|
| 772 |
+
generated.append(accepted_toks)
|
| 773 |
+
total_gen += accepted
|
| 774 |
+
|
| 775 |
+
_crop_dynamic_cache(past_key_values, cache_len + accepted)
|
| 776 |
+
next_token = ar_tokens[:, accepted - 1 : accepted]
|
| 777 |
+
|
| 778 |
+
if eos_token_id is not None:
|
| 779 |
+
eos_pos = (accepted_toks[0] == eos_token_id).nonzero(as_tuple=True)[0]
|
| 780 |
+
if len(eos_pos) > 0:
|
| 781 |
+
first_eos = eos_pos[0].item()
|
| 782 |
+
generated[-1] = accepted_toks[:, : first_eos + 1]
|
| 783 |
+
total_gen = total_gen - accepted + first_eos + 1
|
| 784 |
+
break
|
| 785 |
+
|
| 786 |
+
# Thinking-budget enforcement: force end-think as next seed if budget exhausted.
|
| 787 |
+
if end_think_token_id is not None and max_thinking_tokens is not None:
|
| 788 |
+
if total_gen > max_thinking_tokens:
|
| 789 |
+
all_gen = torch.cat(generated, dim=1)
|
| 790 |
+
if not (all_gen == end_think_token_id).any():
|
| 791 |
+
next_token = torch.tensor([[end_think_token_id]], device=device)
|
| 792 |
+
|
| 793 |
+
if total_gen >= max_new_tokens:
|
| 794 |
+
break
|
| 795 |
+
|
| 796 |
+
all_generated = torch.cat(generated, dim=1)
|
| 797 |
+
output_ids = torch.cat([prompt_ids, all_generated], dim=1)
|
| 798 |
+
return output_ids, nfe
|
| 799 |
+
|
| 800 |
+
|
| 801 |
+
# ─── Module-level helpers used by `generate` and `linear_spec_generate` ──
|
| 802 |
+
|
| 803 |
+
def _crop_dynamic_cache(past_key_values: DynamicCache, max_length: int):
|
| 804 |
+
"""Crop a DynamicCache to max_length, compatible with both old and new transformers."""
|
| 805 |
+
if hasattr(past_key_values, 'crop'):
|
| 806 |
+
past_key_values.crop(max_length)
|
| 807 |
+
else:
|
| 808 |
+
for layer_idx in range(len(past_key_values)):
|
| 809 |
+
past_key_values.key_cache[layer_idx] = past_key_values.key_cache[layer_idx][:, :, :max_length]
|
| 810 |
+
past_key_values.value_cache[layer_idx] = past_key_values.value_cache[layer_idx][:, :, :max_length]
|
| 811 |
+
past_key_values._seen_tokens = max_length
|
| 812 |
+
|
| 813 |
+
|
| 814 |
+
def _add_gumbel_noise(logits, temperature):
|
| 815 |
+
"""Gumbel-max sampling in float64 (low-precision Gumbel hurts MDM quality)."""
|
| 816 |
+
if temperature == 0:
|
| 817 |
+
return logits
|
| 818 |
+
logits = logits.to(torch.float64)
|
| 819 |
+
noise = torch.rand_like(logits, dtype=torch.float64)
|
| 820 |
+
gumbel_noise = (- torch.log(noise)) ** temperature
|
| 821 |
+
return logits.exp() / gumbel_noise
|
| 822 |
+
|
| 823 |
+
|
| 824 |
+
def _get_num_transfer_tokens(mask_index, steps: int):
|
| 825 |
+
"""Even split of masked positions across `steps`, with remainder front-loaded."""
|
| 826 |
+
mask_num = mask_index.sum(dim=1, keepdim=True)
|
| 827 |
+
base = mask_num // steps
|
| 828 |
+
remainder = mask_num % steps
|
| 829 |
+
num_transfer_tokens = torch.zeros(mask_num.size(0), steps, device=mask_index.device, dtype=torch.int64) + base
|
| 830 |
+
for i in range(mask_num.size(0)):
|
| 831 |
+
num_transfer_tokens[i, : int(remainder[i])] += 1
|
| 832 |
+
return num_transfer_tokens
|
| 833 |
+
|
| 834 |
+
|
| 835 |
+
def _get_transfer_index(logits, temperature, mask_index, x, num_transfer_tokens, threshold=None):
|
| 836 |
+
"""Pick which masked positions to commit this denoising step.
|
| 837 |
+
|
| 838 |
+
Returns (x0, transfer_index): x0 is argmax tokens (clamped to original x at
|
| 839 |
+
non-masked positions); transfer_index is a bool mask over positions to
|
| 840 |
+
finalize, chosen by top-k confidence (and filtered by `threshold` if given).
|
| 841 |
+
"""
|
| 842 |
+
logits_with_noise = _add_gumbel_noise(logits, temperature=temperature)
|
| 843 |
+
x0 = torch.argmax(logits_with_noise, dim=-1)
|
| 844 |
+
|
| 845 |
+
p = F.softmax(logits, dim=-1)
|
| 846 |
+
x0_p = torch.squeeze(torch.gather(p, dim=-1, index=torch.unsqueeze(x0, -1)), -1)
|
| 847 |
+
|
| 848 |
+
x0 = torch.where(mask_index, x0, x)
|
| 849 |
+
confidence = torch.where(mask_index, x0_p, -np.inf)
|
| 850 |
+
|
| 851 |
+
transfer_index = torch.zeros_like(x0, dtype=torch.bool, device=x0.device)
|
| 852 |
+
if threshold is not None:
|
| 853 |
+
num_transfer_tokens = mask_index.sum(dim=1, keepdim=True)
|
| 854 |
+
for j in range(confidence.shape[0]):
|
| 855 |
+
_, select_index = torch.topk(confidence[j], k=num_transfer_tokens[j])
|
| 856 |
+
transfer_index[j, select_index] = True
|
| 857 |
+
if threshold is not None:
|
| 858 |
+
for k in range(1, num_transfer_tokens[j]):
|
| 859 |
+
if confidence[j, select_index[k]] < threshold:
|
| 860 |
+
transfer_index[j, select_index[k]] = False
|
| 861 |
+
return x0, transfer_index
|
| 862 |
+
|
| 863 |
+
|
| 864 |
+
def gumbel_topk(log_w: torch.Tensor, k: int) -> torch.Tensor:
|
| 865 |
+
"""Return a Bool mask of length len(log_w) with exactly k True."""
|
| 866 |
+
g = -torch.log(-torch.log(torch.rand_like(log_w) + 1e-9) + 1e-9)
|
| 867 |
+
topk = torch.topk(log_w + g, k).indices
|
| 868 |
+
mask = torch.zeros_like(log_w, dtype=torch.bool)
|
| 869 |
+
mask[topk] = True
|
| 870 |
+
return mask
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:623c34567aebb18582765289fbe23d901c62704d6518d71866e0e58db892b5b7
|
| 3 |
+
size 17077484
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": "<s>",
|
| 5 |
+
"clean_up_tokenization_spaces": false,
|
| 6 |
+
"eos_token": "<|im_end|>",
|
| 7 |
+
"is_local": true,
|
| 8 |
+
"model_input_names": [
|
| 9 |
+
"input_ids",
|
| 10 |
+
"attention_mask"
|
| 11 |
+
],
|
| 12 |
+
"model_max_length": 262144,
|
| 13 |
+
"tokenizer_class": "TokenizersBackend",
|
| 14 |
+
"tool_parser_type": "qwen3_coder",
|
| 15 |
+
"unk_token": "<unk>"
|
| 16 |
+
}
|