Add checkpoint-57
Browse filesFiles changed (17)
- checkpoint-57/added_tokens.jsonAdded+29
- checkpoint-57/chat_template.jinjaAdded+85
- checkpoint-57/config.jsonAdded+43
- checkpoint-57/configuration_sdar.pyAdded+212
- checkpoint-57/fused_linear_diffusion_cross_entropy.pyAdded+682
- checkpoint-57/generation_config.jsonAdded+13
- checkpoint-57/merges.txtAdded
- checkpoint-57/model-00001-of-00002.safetensorsAdded+3
- checkpoint-57/model-00002-of-00002.safetensorsAdded+3
- checkpoint-57/model.safetensors.index.jsonAdded+406
- checkpoint-57/modeling_sdar.pyAdded+1542
- checkpoint-57/special_tokens_map.jsonAdded+39
- checkpoint-57/tokenization_qwen2.pyAdded+342
- checkpoint-57/tokenizer_config.jsonAdded+256
- checkpoint-57/trainer_state.jsonAdded+111
- checkpoint-57/training_args.binAdded+3
- checkpoint-57/vocab.jsonAdded
1234567891011121314151617181920212223242526272829{ "</think>": 151668, "</tool_call>": 151658, "</tool_response>": 151666, "<think>": 151667, "<tool_call>": 151657, "<tool_response>": 151665, "<|MASK|>": 151669, "<|box_end|>": 151649, "<|box_start|>": 151648, "<|endoftext|>": 151643, "<|file_sep|>": 151664, "<|fim_middle|>": 151660, "<|fim_pad|>": 151662, "<|fim_prefix|>": 151659, "<|fim_suffix|>": 151661, "<|im_end|>": 151645, "<|im_start|>": 151644, "<|image_pad|>": 151655, "<|object_ref_end|>": 151647, "<|object_ref_start|>": 151646, "<|quad_end|>": 151651, "<|quad_start|>": 151650, "<|repo_name|>": 151663, "<|video_pad|>": 151656, "<|vision_end|>": 151653, "<|vision_pad|>": 151654, "<|vision_start|>": 151652}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485{%- if tools %} {{- '<|im_start|>system\n' }} {%- if messages[0].role == 'system' %} {{- messages[0].content + '\n\n' }} {%- endif %} {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }} {%- for tool in tools %} {{- "\n" }} {{- tool | tojson }} {%- endfor %} {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}{%- else %} {%- if messages[0].role == 'system' %} {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }} {%- endif %}{%- endif %}{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}{%- for message in messages[::-1] %} {%- set index = (messages|length - 1) - loop.index0 %} {%- if ns.multi_step_tool and message.role == "user" and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %} {%- set ns.multi_step_tool = false %} {%- set ns.last_query_index = index %} {%- endif %}{%- endfor %}{%- for message in messages %} {%- if (message.role == "user") or (message.role == "system" and not loop.first) %} {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }} {%- elif message.role == "assistant" %} {%- set content = message.content %} {%- set reasoning_content = '' %} {%- if message.reasoning_content is defined and message.reasoning_content is not none %} {%- set reasoning_content = message.reasoning_content %} {%- else %} {%- if '</think>' in message.content %} {%- set content = message.content.split('</think>')[-1].lstrip('\n') %} {%- set reasoning_content = message.content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %} {%- endif %} {%- endif %} {%- if loop.index0 > ns.last_query_index %} {%- if loop.last or (not loop.last and reasoning_content) %} {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }} {%- else %} {{- '<|im_start|>' + message.role + '\n' + content }} {%- endif %} {%- else %} {{- '<|im_start|>' + message.role + '\n' + content }} {%- endif %} {%- if message.tool_calls %} {%- for tool_call in message.tool_calls %} {%- if (loop.first and content) or (not loop.first) %} {{- '\n' }} {%- endif %} {%- if tool_call.function %} {%- set tool_call = tool_call.function %} {%- endif %} {{- '<tool_call>\n{"name": "' }} {{- tool_call.name }} {{- '", "arguments": ' }} {%- if tool_call.arguments is string %} {{- tool_call.arguments }} {%- else %} {{- tool_call.arguments | tojson }} {%- endif %} {{- '}\n</tool_call>' }} {%- endfor %} {%- endif %} {{- '<|im_end|>\n' }} {%- elif message.role == "tool" %} {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %} {{- '<|im_start|>user' }} {%- endif %} {{- '\n<tool_response>\n' }} {{- message.content }} {{- '\n</tool_response>' }} {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %} {{- '<|im_end|>\n' }} {%- endif %} {%- endif %}{%- endfor %}{%- if add_generation_prompt %} {{- '<|im_start|>assistant\n' }} {%- if enable_thinking is defined and enable_thinking is false %} {{- '<think>\n\n</think>\n\n' }} {%- endif %}{%- endif %}12345678910111213141516171819202122232425262728293031323334353637383940414243{ "architectures": [ "SDARForCausalLM" ], "attention_bias": false, "attention_dropout": 0.0, "auto_map": { "AutoConfig": "configuration_sdar.SDARConfig", "AutoModel": "modeling_sdar.SDARForCausalLM", "AutoModelForCausalLM": "modeling_sdar.SDARForCausalLM" }, "block_size": 4, "bos_token_id": 151643, "debug": false, "eos_token_id": 151643, "ep_size": 1, "fuse_cross_entropy": true, "head_dim": 128, "hidden_act": "silu", "hidden_size": 2560, "initializer_range": 0.02, "intermediate_size": 9728, "mask_token_id": 151669, "max_position_embeddings": 32768, "max_window_layers": 36, "micro_forward": false, "model_type": "sdar", "num_attention_heads": 32, "num_hidden_layers": 36, "num_key_value_heads": 8, "rms_norm_eps": 1e-06, "rope_scaling": null, "rope_theta": 1000000, "skip_checkpoint": false, "sliding_window": null, "tie_word_embeddings": false, "torch_dtype": "bfloat16", "transformers_version": "4.52.4", "use_cache": false, "use_deepep": false, "use_sliding_window": false, "vocab_size": 151936}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212# coding=utf-8# Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License."""SDAR model configuration"""
from transformers.configuration_utils import PretrainedConfigfrom transformers.modeling_rope_utils import rope_config_validationfrom transformers.utils import logging
logger = logging.get_logger(__name__)
class SDARConfig(PretrainedConfig): r""" This is the configuration class to store the configuration of a [`SDARModel`]. It is used to instantiate a SDAR model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of SDAR-1.7B [DiffuOpen/SDAR-1.7B-Chat](https://huggingface.co/DiffuOpen/SDAR-1.7B-Chat/).
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.
Args: vocab_size (`int`, *optional*, defaults to 151936): Vocabulary size of the SDAR model. Defines the number of different tokens that can be represented by the `inputs_ids` passed when calling [`SDARModel`] hidden_size (`int`, *optional*, defaults to 4096): Dimension of the hidden representations. intermediate_size (`int`, *optional*, defaults to 22016): Dimension of the MLP representations. num_hidden_layers (`int`, *optional*, defaults to 32): Number of hidden layers in the Transformer encoder. num_attention_heads (`int`, *optional*, defaults to 32): Number of attention heads for each attention layer in the Transformer encoder. num_key_value_heads (`int`, *optional*, defaults to 32): This is the number of key_value heads that should be used to implement Grouped Query Attention. If `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed by meanpooling all the original heads within that group. For more details checkout [this paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `32`. head_dim (`int`, *optional*, defaults to 128): The attention head dimension. hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the decoder. max_position_embeddings (`int`, *optional*, defaults to 32768): The maximum sequence length that this model might ever be used with. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. rms_norm_eps (`float`, *optional*, defaults to 1e-06): The epsilon used by the rms normalization layers. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). Only relevant if `config.is_decoder=True`. tie_word_embeddings (`bool`, *optional*, defaults to `False`): Whether the model's input and output word embeddings should be tied. rope_theta (`float`, *optional*, defaults to 10000.0): The base period of the RoPE embeddings. rope_scaling (`Dict`, *optional*): Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value accordingly. Expected contents: `rope_type` (`str`): The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope', 'llama3'], with 'default' being the original RoPE implementation. `factor` (`float`, *optional*): Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In most scaling types, a `factor` of x will enable the model to handle sequences of length x * original maximum pre-trained length. `original_max_position_embeddings` (`int`, *optional*): Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during pretraining. `attention_factor` (`float`, *optional*): Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention computation. If unspecified, it defaults to value recommended by the implementation, using the `factor` field to infer the suggested value. `beta_fast` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear ramp function. If unspecified, it defaults to 32. `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. `short_factor` (`List[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 `long_factor` (`List[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 `low_freq_factor` (`float`, *optional*): Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE `high_freq_factor` (`float`, *optional*): Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`): Whether to use a bias in the query, key, value and output projection layers during self-attention. use_sliding_window (`bool`, *optional*, defaults to `False`): Whether to use sliding window attention. sliding_window (`int`, *optional*, defaults to 4096): Sliding window attention (SWA) window size. If not specified, will default to `4096`. max_window_layers (`int`, *optional*, defaults to 28): The number of layers that use SWA (Sliding Window Attention). The bottom layers use SWA while the top use full attention. attention_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for the attention probabilities.
```python >>> from transformers import SDARModel, SDARConfig
>>> # Initializing a SDAR style configuration >>> configuration = SDARConfig()
>>> # Initializing a model from the SDAR-8B style configuration >>> model = SDARModel(configuration)
>>> # Accessing the model configuration >>> configuration = model.config ```"""
model_type = "sdar" keys_to_ignore_at_inference = ["past_key_values"]
# Default tensor parallel plan for base model `SDAR` base_model_tp_plan = { "layers.*.self_attn.q_proj": "colwise", "layers.*.self_attn.k_proj": "colwise", "layers.*.self_attn.v_proj": "colwise", "layers.*.self_attn.o_proj": "rowwise", "layers.*.mlp.gate_proj": "colwise", "layers.*.mlp.up_proj": "colwise", "layers.*.mlp.down_proj": "rowwise", } base_model_pp_plan = { "embed_tokens": (["input_ids"], ["inputs_embeds"]), "layers": (["hidden_states", "attention_mask"], ["hidden_states"]), "norm": (["hidden_states"], ["hidden_states"]), }
def __init__( self, vocab_size=151936, hidden_size=4096, intermediate_size=22016, num_hidden_layers=32, num_attention_heads=32, num_key_value_heads=32, head_dim=128, hidden_act="silu", max_position_embeddings=32768, initializer_range=0.02, rms_norm_eps=1e-6, use_cache=True, tie_word_embeddings=False, rope_theta=10000.0, rope_scaling=None, attention_bias=False, use_sliding_window=False, sliding_window=4096, max_window_layers=28, attention_dropout=0.0, **kwargs, ): self.vocab_size = vocab_size self.max_position_embeddings = max_position_embeddings self.hidden_size = hidden_size self.intermediate_size = intermediate_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.use_sliding_window = use_sliding_window self.sliding_window = sliding_window # we check `use_sliding_window` in the modeling code self.max_window_layers = max_window_layers
# for backward compatibility if num_key_value_heads is None: num_key_value_heads = num_attention_heads
self.num_key_value_heads = num_key_value_heads self.head_dim = head_dim self.hidden_act = hidden_act self.initializer_range = initializer_range self.rms_norm_eps = rms_norm_eps self.use_cache = use_cache self.rope_theta = rope_theta self.rope_scaling = rope_scaling self.attention_bias = attention_bias self.attention_dropout = attention_dropout # Validate the correctness of rotary position embeddings parameters # BC: if there is a 'type' field, move it to 'rope_type'. if self.rope_scaling is not None and "type" in self.rope_scaling: self.rope_scaling["rope_type"] = self.rope_scaling["type"] rope_config_validation(self)
super().__init__( tie_word_embeddings=tie_word_embeddings, **kwargs, )
__all__ = ["SDARConfig"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682# -*- coding: utf-8 -*-
# Code adapted from# https://github.com/fla-org/flash-linear-attention/blob/main/fla/modules/fused_linear_cross_entropy.py# Implementation of element-wise division of cross entropy loss
# Code adapted from# https://github.com/linkedin/Liger-Kernel/blob/main/src/liger_kernel/ops/fused_linear_cross_entropy.py
from functools import partialfrom typing import Optional, Tuple
import torchimport torch.nn as nnimport torch.nn.functional as Fimport tritonimport triton.language as tlfrom torch.distributed import DeviceMeshfrom torch.distributed.tensor import DTensor, Replicate, Shard, distribute_modulefrom torch.distributed.tensor.parallel import ParallelStyle
# The hard limit of TRITON_MAX_TENSOR_NUMEL is 1048576# https://github.com/triton-lang/triton/blob/ba42a5c68fd0505f8c42f4202d53be0f8d9a5fe0/python/triton/language/core.py#L19# However, setting limit as 65536 as in LayerNorm tutorial is faster because of less register spilling# The optimal maximum block size depends on your hardware, your kernel, and your dtypeMAX_FUSED_SIZE = 65536 // 2
@triton.heuristics({ 'HAS_SCALE': lambda args: args['scale'] is not None})@triton.autotune( configs=[ triton.Config({}, num_warps=num_warps) for num_warps in [1, 2, 4, 8, 16, 32] ], key=['D'])@triton.jitdef logsumexp_fwd_kernel( x, z, scale, D: tl.constexpr, B: tl.constexpr, HAS_SCALE: tl.constexpr): i_n, i_d = tl.program_id(0).to(tl.int64), tl.program_id(1).to(tl.int64) o_d = i_d * B + tl.arange(0, B) m_d = o_d < D
b_x = tl.load(x + i_n * D + o_d, mask=m_d, other=-float('inf')) if HAS_SCALE: b_x = b_x * scale b_m = tl.max(b_x, 0) b_z = tl.log(tl.sum(tl.exp(b_x - b_m), 0)) + b_m tl.store(z + i_n * tl.cdiv(D, B) + i_d, b_z)
def logsumexp_fwd( x, scale: Optional[float] = None, dtype: Optional[torch.dtype] = None): r""" Compute the logsumexp of the input tensor over the last dimension.
Args: x (Tensor): The input tensor of any shape. scale (Optional[float]): The scale applied to the input tensor. Default: `None`. dtype (Optional[torch.dtype]): The data type of the output tensor. Default: `None`. Returns: Tensor: The logsumexp of the input tensor. """
shape = x.shape x = x.view(-1, shape[-1]) N, D = x.shape B = min(triton.next_power_of_2(D), 64 * 1024) ND = triton.cdiv(D, B)
z = x.new_empty(N, ND, dtype=torch.float) logsumexp_fwd_kernel[(N, ND)]( x=x, z=z, scale=scale, D=D, B=B ) z = z.logsumexp(-1).view(*shape[:-1]) if dtype is not None and dtype != torch.float: z = z.to(dtype) return z
@triton.jitdef cross_entropy_kernel( logits, lse, target, p_mask, loss, total, ignore_index, label_smoothing: tl.constexpr, logit_scale: tl.constexpr, reduction: tl.constexpr, V: tl.constexpr, BV: tl.constexpr): """ This kernel computes both cross entropy loss and the gradient of the input. We only consider hard label + mean reduction for now. Please refer to https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html for the math.
Args: logits: Pointer to logits tensor. lse: Pointer to logsumexp tensor. target: Pointer to target tensor. loss: Pointer to tensor to store the loss. V (int): The number of columns in the input tensor. total (int): The number of non-ignored classes. ignore_index (int): The index to ignore in the target. label_smoothing (float): The amount of smoothing when computing the loss, where 0.0 means no smoothing. reduction (str): The string for the reduction to apply BV (int): The block size for vocab. """
# https://github.com/triton-lang/triton/issues/1058 # If B*T*V is too large, i_n * stride will overflow out of int32, so we convert to int64 i_n = tl.program_id(0).to(tl.int64) NV = tl.cdiv(V, BV)
# 1. Load target first because if the target is ignore_index, we can return right away b_y = tl.load(target + i_n) # load p_mask b_p_mask = tl.load(p_mask + i_n)
# 2. locate the start index logits += i_n * V
if b_y == ignore_index: # set all x as 0 for i in range(0, V, BV): o_v = i + tl.arange(0, BV) tl.store(logits + o_v, 0.0, mask=o_v < V) return
# Online softmax: 2 loads + 1 store (compared with 3 loads + 1 store for the safe softmax) # Refer to Algorithm 3 in the paper: https://arxiv.org/pdf/1805.02867
# 3. [Online softmax] first pass: compute logsumexp # we did this in anouter kernel b_l = tl.load(logits + b_y) * logit_scale b_lse = tl.load(lse + i_n)
# 4. Calculate the loss # loss = lse - logits_l # celoss = -log(q_y) = -log(softmax(x_y)) b_loss = (b_lse - b_l) / b_p_mask # Diffusion Scaled '1/t'
# Label smoothing is a general case of normal cross entropy # See the full derivation at https://github.com/linkedin/Liger-Kernel/pull/198#issue-2503665310 b_z = 0.0 eps = label_smoothing / V
# We need tl.debug_barrier() as mentioned in # https://github.com/triton-lang/triton/blob/ba42a5c68fd0505f8c42f4202d53be0f8d9a5fe0/python/triton/ops/cross_entropy.py#L34 tl.debug_barrier()
# 5. [Online Softmax] Second pass: compute gradients # For 'mean' reduction, gradients are normalized by number of non-ignored elements # dx_y = (softmax(x_y) - 1) / N # dx_i = softmax(x_i) / N, i != y # For label smoothing: # dx_i = (softmax(x_y) - label_smoothing / V) / N, i != y # dx_y = (softmax(x_y) - label_smoothing / V - (1 - label_smoothing)) / N # = dx_i - (1 - label_smoothing) / N for iv in range(0, NV): o_v = iv * BV + tl.arange(0, BV) b_logits = tl.load(logits + o_v, mask=o_v < V, other=float('-inf')) * logit_scale if label_smoothing > 0: # scale X beforehand to avoid overflow b_z += tl.sum(tl.where(o_v < V, -eps * b_logits, 0.0)) b_p = (tl.exp(b_logits - b_lse) - eps) * logit_scale b_p /= b_p_mask # 修改 if reduction == "mean": b_p = b_p / total tl.store(logits + o_v, b_p, mask=o_v < V)
tl.debug_barrier()
# Orginal loss = H(q, p), with label smoothing regularization = H(q', p) and (label_smoothing / V) = eps # H(q', p) = (1 - label_smoothing) * H(q, p) + label_smoothing * H(u, p) # = (1 - label_smoothing) * H(q, p) + eps * sum(logsoftmax(x_i)) # By using m (global max of xi) and d (sum of e^(xi-m)), we can simplify as: # = (1 - label_smoothing) * H(q, p) + (-sum(x_i * eps) + label_smoothing * (m + logd)) # Refer to H(q', p) in section 7 of the paper: # https://arxiv.org/pdf/1512.00567 # pytorch: # https://github.com/pytorch/pytorch/blob/2981534f54d49fa3a9755c9b0855e7929c2527f0/aten/src/ATen/native/LossNLL.cpp#L516 # See full derivation at https://github.com/linkedin/Liger-Kernel/pull/198#issuecomment-2333753087 if label_smoothing > 0: b_loss = b_loss * (1 - label_smoothing) + (b_z + label_smoothing * b_lse)
# 6. Specially handle the i==y case where `dx_y = (softmax(x_y) - (1 - label_smoothing) / N` b_l = tl.load(logits + b_y)
# Normalize the loss by the number of non-ignored elements if reduction is "mean" if reduction == 'mean': b_loss = b_loss / total # b_l += (label_smoothing - 1) / total * logit_scale # b_l has already been divided by b_p_mask and total b_l += (label_smoothing - 1) / b_p_mask / total * logit_scale else: # b_l += (label_smoothing - 1) * logit_scale b_l += (label_smoothing - 1) / b_p_mask * logit_scale
tl.store(loss + i_n, b_loss) tl.store(logits + b_y, b_l)
@triton.jitdef elementwise_mul_kernel( x, g, N: tl.constexpr, B: tl.constexpr): """ This function multiplies each element of the tensor pointed by x with the value pointed by g. The multiplication is performed in-place on the tensor pointed by x.
Parameters: x: Pointer to the input tensor. g: Pointer to the gradient output value. N (int): The number of columns in the input tensor. B (int): The block size for Triton operations. """
# Get the program ID and convert it to int64 to avoid overflow i_x = tl.program_id(0).to(tl.int64) o_x = i_x * B + tl.arange(0, B)
# Load the gradient output value b_g = tl.load(g) b_x = tl.load(x + o_x, mask=o_x < N) tl.store(x + o_x, b_x * b_g, mask=o_x < N)
def fused_linear_cross_entropy_forward( x: torch.Tensor, target: torch.LongTensor, weight: torch.Tensor, bias: torch.Tensor = None, p_mask: torch.Tensor = None, ignore_index: int = -100, label_smoothing: float = 0.0, logit_scale: float = 1.0, num_chunks: int = 8, reduction: str = "mean"): device = x.device # inputs have shape: [N, H] # materialized activations will have shape: [N, V] # the increase in memory = [N, V] # reduction can be achieved by partitioning the number of tokens N into smaller chunks.
# ideally, we would like to achieve the same memory consumption as [N, H], # so the expected chunk size should be: # NC = ceil(V / H) # C = ceil(N / NC) # for ex: N = 4096*4, V = 32000, H = 4096 ==> NC = 8, C = ceil(N / NC) = 2048 N, H, V = *x.shape, weight.shape[0] BV = min(MAX_FUSED_SIZE, triton.next_power_of_2(V)) # TODO: in real cases, we may need to limit the number of chunks NC to # ensure the precisions of accumulated gradients NC = min(num_chunks, triton.cdiv(V, H)) C = triton.next_power_of_2(triton.cdiv(N, NC)) NC = triton.cdiv(N, C)
# [N, H] dx = torch.zeros_like(x, device=device) # [V, H] dw = torch.zeros_like(weight, device=device, dtype=torch.float) if weight is not None else None # [V] db = torch.zeros_like(bias, device=device, dtype=torch.float) if bias is not None else None # [N] loss = torch.zeros(N, device=device, dtype=torch.float)
total = target.ne(ignore_index).sum().item()
for ic in range(NC): start, end = ic * C, min((ic + 1) * C, N) # [C, N] c_x = x[start:end] # when doing matmul, use the original precision # [C, V] c_logits = F.linear(c_x, weight, bias) c_target = target[start:end] c_p_mask = p_mask[start:end] # [C] # keep lse in fp32 to maintain precision c_lse = logsumexp_fwd(c_logits, scale=logit_scale, dtype=torch.float)
# unreduced loss c_loss = loss[start:end]
# Here we calculate the gradient of c_logits in place so we can save memory. cross_entropy_kernel[(c_logits.shape[0],)]( logits=c_logits, lse=c_lse, target=c_target, p_mask=c_p_mask, loss=c_loss, total=total, ignore_index=ignore_index, label_smoothing=label_smoothing, logit_scale=logit_scale, reduction=reduction, V=V, BV=BV, num_warps=32 )
# gradient of logits is computed in-place by the above triton kernel and is of shape: C x V # thus dx should be of shape: C x H dx[start:end] = torch.mm(c_logits, weight)
# keep dw in fp32 to maintain precision if weight is not None: dw += c_logits.t() @ c_x
if bias is not None: torch.add(input=db, other=c_logits.sum(0), out=db)
loss = loss.sum() if dw is not None: dw = dw.to(weight) if db is not None: db = db.to(bias) return loss, dx, dw, db
def fused_linear_cross_entropy_backward( do: torch.Tensor, dx: torch.Tensor, dw: torch.Tensor, db: torch.Tensor): # If cross entropy is the last layer, do is 1.0. Skip the mul to save time if torch.ne(do, torch.tensor(1.0, device=do.device)): # We use a Triton kernel instead of a PyTorch operation because modifying inputs in-place # for gradient storage and backward multiple times causes anomalies with PyTorch but not with Triton. N, H = dx.shape B = min(MAX_FUSED_SIZE, triton.next_power_of_2(H))
elementwise_mul_kernel[(triton.cdiv(N * H, B),)]( x=dx, g=do, N=N*H, B=B, num_warps=32, )
# handle dw if dw is not None: V, H = dw.shape elementwise_mul_kernel[(triton.cdiv(V * H, B),)]( x=dw, g=do, N=V*H, B=B, num_warps=32, )
if db is not None: V = db.shape[0] elementwise_mul_kernel[(triton.cdiv(V, B),)]( x=db, g=do, N=V, B=B, num_warps=32, ) return dx, dw, db
class FusedLinearCrossEntropyFunction(torch.autograd.Function):
@staticmethod def forward( ctx, x: torch.Tensor, target: torch.LongTensor, weight: torch.Tensor, bias: torch.Tensor = None, p_mask: torch.Tensor = None, ignore_index: int = -100, label_smoothing: float = 0.0, logit_scale: float = 1.0, num_chunks: int = 8, reduction: str = "mean" ): """ Fusing the last linear layer with cross-entropy loss Reference: https://github.com/mgmalek/efficient_cross_entropy
Handle the forward and backward pass of the final linear layer via cross-entropy loss by avoiding the materialization of the large logits tensor. Since Cross Entropy Loss is the last layer, we can compute the gradient at the forward pass. By doing so, we don't have to store the x and target for the backward pass.
x (torch.Tensor): [batch_size * seq_len, hidden_size] target (torch.LongTensor): [batch_size * seq_len] where each value is in [0, vocab_size). weight (torch.Tensor): [vocab_size, hidden_size] where `vocab_size` is the number of classes. bias (Optional[torch.Tensor]): [vocab_size] where `vocab_size` is the number of classes. p_mask(torch.Tensor): [batch_size * seq_len] Its shape should be same as target. ignore_index: the index to ignore in the target. label_smoothing: the amount of smoothing when computing the loss, where 0.0 means no smoothing. logit_scale: float = 1.0, A scaling factor applied to the logits. Default: 1.0 num_chunks: int The number of chunks to split the input tensor into for processing. This can help optimize memory usage and computation speed. Default: 8 reduction: Specifies the reduction to apply to the output: 'mean' | 'sum'. 'mean': the weighted mean of the output is taken, 'sum': the output will be summed. Default: 'mean'. """ loss, dx, dw, db = fused_linear_cross_entropy_forward( x, target, weight, bias, p_mask, ignore_index, label_smoothing, logit_scale, num_chunks, reduction ) # downcast to dtype and store for backward ctx.save_for_backward( dx.detach(), dw.detach() if weight is not None else None, db.detach() if bias is not None else None, ) return loss
@staticmethod def backward(ctx, do): dx, dw, db = ctx.saved_tensors dx, dw, db = fused_linear_cross_entropy_backward(do, dx, dw, db) # 10 gradients should be returned, with `p_mask` having no grads # Check the number of arguments in the `forward` method return dx, None, dw, db, None, None, None, None, None, None
def fused_linear_cross_entropy_loss( x: torch.Tensor, target: torch.LongTensor, weight: torch.Tensor, bias: torch.Tensor = None, p_mask: torch.Tensor = None, ignore_index: int = -100, label_smoothing: float = 0.0, logit_scale: float = 1.0, num_chunks: int = 8, reduction: str = "mean") -> Tuple[torch.Tensor, torch.Tensor]: """ Args: x (torch.Tensor): [batch_size * seq_len, hidden_size] target (torch.LongTensor): [batch_size * seq_len] where each value is in [0, vocab_size). weight (torch.Tensor): [vocab_size, hidden_size] where `vocab_size` is the number of classes. bias (Optional[torch.Tensor]): [vocab_size] where `vocab_size` is the number of classes. p_mask(torch.Tensor): [batch_size * seq_len] Its shape should be same as target. ignore_index: int. If target == ignore_index, the loss is set to 0.0. label_smoothing: float logit_scale: float A scaling factor applied to the logits. Default: 1.0 num_chunks: int The number of chunks to split the input tensor into for processing. This can help optimize memory usage and computation speed. Default: 8 reduction: Specifies the reduction to apply to the output: 'mean' | 'sum'. 'mean': the weighted mean of the output is taken, 'sum': the output will be summed. Default: 'mean'. Returns: losses: [batch,], float """ return FusedLinearCrossEntropyFunction.apply( x, target, weight, bias, p_mask, ignore_index, label_smoothing, logit_scale, num_chunks, reduction )
class FusedLinearDiffusionCrossEntropyLoss(nn.Module):
def __init__( self, ignore_index: int = -100, label_smoothing: float = 0.0, logit_scale: float = 1.0, num_chunks: int = 8, reduction: str = "mean" ): """ Args: ignore_index: int. If target == ignore_index, the loss is set to 0.0. label_smoothing: float logit_scale: float A scaling factor applied to the logits. Default: 1.0 num_chunks: int The number of chunks to split the input tensor into for processing. This can help optimize memory usage and computation speed. Default: 8 reduction: Specifies the reduction to apply to the output: 'mean' | 'sum'. 'mean': the weighted mean of the output is taken, 'sum': the output will be summed. Default: 'mean'. """ super().__init__()
assert reduction in ["mean", "sum"], f"reduction: {reduction} is not supported"
self.ignore_index = ignore_index self.label_smoothing = label_smoothing self.logit_scale = logit_scale self.num_chunks = num_chunks self.reduction = reduction
@torch.compiler.disable def forward( self, x: torch.Tensor, target: torch.LongTensor, weight: torch.Tensor, bias: Optional[torch.Tensor] = None, p_mask: torch.Tensor = None ): """ Args: x (torch.Tensor): [batch_size, seq_len, hidden_size] target (torch.LongTensor): [batch_size, seq_len] where each value is in [0, V). weight (torch.Tensor): [vocab_size, hidden_size] where `vocab_size` is the number of classes. bias (Optional[torch.Tensor]): [vocab_size] where `vocab_size` is the number of classes. p_mask(torch.Tensor): [batch_size, seq_len] Its shape is same as target. Shape: (1, packed_length) when varlen attn is used. Returns: loss TODO: follow https://github.com/ML-GSAI/LLaDA/blob/main/GUIDELINES.md#pre-training ```py unreduced_loss /= p_mask ``` Scale the values of `unreduced_loss at different positions """ if p_mask is None: p_mask = torch.ones_like(target, dtype=torch.float, device=x.device) x = x.contiguous().view(-1, x.shape[-1]) target = target.contiguous().view(-1) weight = weight.contiguous() bias = bias.contiguous() if bias else None p_mask = p_mask.contiguous().view(-1) l, d = x.shape assert l == target.shape[0] == p_mask.shape[0], f"{x.shape=}, {target.shape=}, {p_mask.shape=}" loss = fused_linear_cross_entropy_loss( x, target, weight=weight, bias=bias, p_mask=p_mask, ignore_index=self.ignore_index, label_smoothing=self.label_smoothing, logit_scale=self.logit_scale, num_chunks=self.num_chunks, reduction=self.reduction ) return loss
class LinearLossParallel(ParallelStyle): def __init__( self, *, sequence_dim: int = 1, use_local_output: bool = False, ): super().__init__()
self.sequence_sharding = (Shard(sequence_dim),) self.use_local_output = use_local_output
@staticmethod def _prepare_input_fn(sequence_sharding, mod, inputs, device_mesh): x, target, weight, bias = inputs
if not isinstance(x, DTensor): # assume the input passed in already sharded on the sequence dim and create the DTensor x = DTensor.from_local(x, device_mesh, sequence_sharding) if x.placements != sequence_sharding: x = x.redistribute(placements=sequence_sharding, async_op=True) if not isinstance(target, DTensor): target = DTensor.from_local(target, device_mesh, [Replicate()]) if target.placements != sequence_sharding: target = target.redistribute(placements=sequence_sharding, async_op=True)
if not isinstance(weight, DTensor): weight = DTensor.from_local(weight, device_mesh, [Replicate()]) if weight.placements != [Replicate()]: # we replicate the weight/bias in FLCE weight = weight.redistribute(placements=[Replicate()], async_op=True)
if bias is not None and not isinstance(bias, DTensor): bias = DTensor.from_local(bias, device_mesh, [Replicate()]) if bias is not None and bias.placements != [Replicate()]: bias = bias.redistribute(placements=[Replicate()], async_op=True)
return x.to_local(), target.to_local(), weight.to_local(), bias.to_local() if bias is not None else bias
@staticmethod def _prepare_output_fn(use_local_output, mod, outputs, device_mesh): return outputs.to_local() if use_local_output else outputs
def _apply(self, module: nn.Module, device_mesh: DeviceMesh) -> nn.Module: return distribute_module( module, device_mesh, partition_fn=None, input_fn=partial(self._prepare_input_fn, self.sequence_sharding), output_fn=partial(self._prepare_output_fn, self.use_local_output) )12345678910111213{ "bos_token_id": 151643, "do_sample": true, "eos_token_id": [ 151645, 151643 ], "pad_token_id": 151643, "temperature": 0.6, "top_k": 20, "top_p": 0.95, "transformers_version": "4.52.4"}123version https://git-lfs.github.com/spec/v1oid sha256:1d47b118dc6f1e0a580bbac5c85193edf575db8beec2c802ee77dced04cb296asize 4967215360123version https://git-lfs.github.com/spec/v1oid sha256:1b85b5c9b73b80d40edea97aeccf11d859837c5aca04234f00262bd7e12bf5dcsize 3855679144123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406{ "metadata": { "total_size": 8822848512 }, "weight_map": { "lm_head.weight": "model-00002-of-00002.safetensors", "model.embed_tokens.weight": "model-00001-of-00002.safetensors", "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.20.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.20.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.21.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.21.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.21.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.21.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.21.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.21.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.21.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.21.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.21.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.21.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.22.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.22.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.22.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.22.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.22.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.22.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.22.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.22.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.22.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.22.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.22.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.23.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.28.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.28.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.28.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.28.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.28.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.28.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.28.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.28.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.28.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.28.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.28.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.29.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.29.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.29.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.29.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.29.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.29.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.29.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.29.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.29.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.29.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.29.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.3.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.3.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.3.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.3.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.3.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.3.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.3.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.30.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.30.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.30.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.30.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.30.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.30.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.30.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.30.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.30.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.30.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.30.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.31.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.31.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.31.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.31.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.31.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.31.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.31.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.31.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.31.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.31.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.31.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.32.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.32.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.32.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.32.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.32.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.32.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.32.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.32.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.32.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.32.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.32.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.33.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.33.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.33.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.33.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.33.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.33.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.33.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.33.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.33.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.33.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.33.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.34.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.34.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.34.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.34.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.34.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.34.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.34.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.34.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.34.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.34.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.34.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.35.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.35.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.35.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.35.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.35.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.35.self_attn.k_norm.weight": "model-00002-of-00002.safetensors", "model.layers.35.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.35.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.35.self_attn.q_norm.weight": "model-00002-of-00002.safetensors", "model.layers.35.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.35.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.4.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.4.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.4.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.4.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.4.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.4.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.4.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.5.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.5.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.5.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.5.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.5.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.5.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.5.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.5.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.6.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.6.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.6.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.6.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.6.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.6.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.6.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.6.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.6.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.6.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.6.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.7.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.7.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.7.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.7.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.7.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.7.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.7.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.7.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.7.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.7.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.7.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.8.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.8.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.8.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.8.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.8.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.8.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.8.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.8.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.8.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.8.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.8.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.9.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.9.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.9.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.9.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.9.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.9.self_attn.k_norm.weight": "model-00001-of-00002.safetensors", "model.layers.9.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.9.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.9.self_attn.q_norm.weight": "model-00001-of-00002.safetensors", "model.layers.9.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.9.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.norm.weight": "model-00002-of-00002.safetensors" }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542# This file is modified based on https://github.com/huggingface/transformers/blob/v4.52.4/src/transformers/models/qwen3/modeling_qwen3.py.## 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨# This file was automatically generated from src/transformers/models/qwen3/modular_qwen3.py.# Do NOT edit this file manually as any edits will be overwritten by the generation of# the file from the modular. If any change should be done, please apply the change to the# modular_qwen3.py file directly. One of our CI enforces this.# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨# coding=utf-8# Copyright 2025 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from typing import Callable, Optional, Tuple, Union, List
import torchfrom torch import nnfrom einops import rearrange
from transformers.activations import ACT2FNfrom transformers.cache_utils import Cache, DynamicCache, SlidingWindowCache, StaticCachefrom transformers.generation import GenerationMixinfrom transformers.integrations import use_kernel_forward_from_hubfrom transformers.modeling_attn_mask_utils import AttentionMaskConverterfrom transformers.modeling_flash_attention_utils import FlashAttentionKwargsfrom transformers.modeling_layers import GradientCheckpointingLayerfrom transformers.modeling_outputs import ( BaseModelOutputWithPast, CausalLMOutputWithPast, QuestionAnsweringModelOutput, SequenceClassifierOutputWithPast, TokenClassifierOutput,)from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_updatefrom transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModelfrom transformers.processing_utils import Unpackfrom transformers.utils import LossKwargs, auto_docstring, can_return_tuple, is_torch_flex_attn_available, loggingfrom .configuration_sdar import SDARConfigfrom .fused_linear_diffusion_cross_entropy import FusedLinearDiffusionCrossEntropyLoss
from flash_attn.ops.triton.layer_norm import rms_norm_fn as flash_rms_norm
import torch.nn.functional as Ftry: from flash_attn import flash_attn_func, flash_attn_varlen_func from flash_attn.bert_padding import index_first_axis, pad_input, unpad_inputexcept: pass
try: from liger_kernel.ops.swiglu import LigerSiLUMulFunction # noqa: F401 liger_kernel_is_available = Trueexcept ImportError: liger_kernel_is_available = False
if is_torch_flex_attn_available(): from torch.nn.attention.flex_attention import BlockMask, create_block_mask, flex_attention from transformers.integrations.flex_attention import make_flex_block_causal_mask
logger = logging.get_logger(__name__)
def modify_padded_position_ids_2d(position_ids: torch.LongTensor) -> torch.LongTensor: """ 使用完全向量化的 PyTorch 操作修改一个 batch 的 packed position_ids。 这个函数假设输入是一个 2D Tensor,形状为 (batch_size, sequence_length)。 它会独立地处理 batch 中的每一行。
Args: position_ids: 二维 PyTorch Tensor, shape (batch_size, sequence_length).
Returns: 修改后的 position_ids Tensor, shape (batch_size, sequence_length). """ if position_ids.dim() != 2: raise ValueError(f"Input tensor must be 2D, but got {position_ids.dim()} dimensions.") batch_size, seq_len = position_ids.shape device = position_ids.device
col_indices = torch.arange(seq_len, device=device, dtype=position_ids.dtype).expand(batch_size, -1) mask = (position_ids != 0)
masked_indices = col_indices * mask last_nonzero_idx = torch.max(masked_indices, dim=1).values has_nonzero = torch.any(mask, dim=1) pad_start_idx = torch.where(has_nonzero, last_nonzero_idx + 1, torch.tensor(0, device=device, dtype=position_ids.dtype))
padding_mask = col_indices >= pad_start_idx.unsqueeze(1) new_pad_values = col_indices - pad_start_idx.unsqueeze(1) position_ids = torch.where(padding_mask, new_pad_values, position_ids)
return position_ids
def calculate_token_nums(position_ids: torch.Tensor): """ 使用 PyTorch 高效计算一个批次中每个打包序列的长度。
Args: position_ids (torch.Tensor): 一个 2D Tensor,形状为 (batch_size, sequence_length)。 例如:tensor([[0,1,2,3,4,0,1,2,3,4,5,0,1,2,3,0,0,0]]) Returns: list[list[int]]: 一个嵌套列表,包含每个批次项中各个序列的长度。 例如:[[5, 6, 4, 1, 1, 1]] """ # 检查输入是否为 2D Tensor if position_ids.dim() != 2: raise ValueError(f"输入必须是 2D Tensor,但得到了 {position_ids.dim()}D")
all_lengths = [] # 我们按批次逐行处理。因为每行的序列长度数量不同(ragged), # 所以 Python 循环在批次维度上是最高效且最清晰的写法。 # 循环内部的操作是完全向量化的。 for pids_row in position_ids: # 获取当前行的总长度 seq_len = pids_row.shape[0] # 1. 找到所有值为 0 的元素的索引 # pids_row == 0 会返回一个布尔 Tensor: [True, False, ..., True, ...] # torch.nonzero 会返回这些 True 值的索引 # .flatten() 将其从 (N, 1) 形状的 Tensor 变为 (N,) 形状 zero_indices = torch.nonzero(pids_row == 0).flatten() # 2. 将序列的总长度作为一个额外的切分点添加到末尾 # 这对于计算最后一个序列的长度至关重要 # 注意:要确保新创建的 tensor 和原始 tensor 在同一个设备上 (cpu/cuda) split_points = torch.cat([ zero_indices, torch.tensor([seq_len], device=pids_row.device, dtype=zero_indices.dtype) ]) # 3. 计算相邻切分点之间的差值,这就是我们想要的长度 # torch.diff([a, b, c, d]) 会返回 [b-a, c-b, d-c] lengths = torch.diff(split_points)
all_lengths.append(lengths)
return all_lengths
def forward_add_noise_packed( inputs_ids: torch.Tensor, num_tokens_list: List[torch.Tensor], prompt_mask: torch.Tensor, mask_id: int, eps: float = 1e-3, max_tries: int = 10,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ 为一批打包(packed)序列的 token ID 添加噪声。
此函数保留了为每个逻辑样本(在每个批次项内拼接)生成独立随机噪声率的逻辑。 它会随机将一部分 token 的 ID 替换为 mask_id。 这个过程会避开被 prompt_mask 标记的位置。
Args: inputs_ids (torch.Tensor): 输入的 token ID 张量,形状为 (bsz, total_tokens)。 num_tokens_list (List[torch.Tensor]): 一个张量列表,长度为 bsz。列表中的每个张量记录了对应批次项中 每个逻辑样本的长度。例如: [tensor([len1, len2]), tensor([len3, len4, len5])]. prompt_mask (torch.Tensor): 布尔型张量,形状为 (bsz, total_tokens),值为 True 的位置表示是 prompt, 不应添加噪声。 mask_id (int): 用于替换的 mask token 的 ID。 eps (float): 微小值,用于防止噪声率 t 恰好为 0,确保 p_mask > 0。 max_tries (int): 为确保至少一个非 prompt token 被 mask,对每个批次项尝试的最大次数。
Returns: Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - noisy_input_ids (torch.Tensor): 添加噪声后的 token ID 张量,形状为 (bsz, total_tokens)。 - final_masked_indices (torch.Tensor): 布尔型张量,标记了哪些位置被实际 mask 了,形状为 (bsz, total_tokens)。 - p_masks (torch.Tensor): 一个一维张量,包含了被 mask 的 token 对应的实际噪声率。 """ # 1. 验证和获取形状 bsz, total_tokens = inputs_ids.shape device = inputs_ids.device
# 检查输入的一致性 assert len(num_tokens_list) == bsz, f"num_tokens_list 的长度 ({len(num_tokens_list)}) 必须等于 bsz ({bsz})" assert prompt_mask.shape == (bsz, total_tokens), f"prompt_mask 形状不匹配, 期望 {(bsz, total_tokens)}, 得到 {prompt_mask.shape}"
# 准备结果容器 noisy_ids_list = [] final_masked_indices_list = [] p_masks_per_token_list = []
# 2. 在批次维度上迭代 # 这是处理不同打包结构最直接有效的方法 for i in range(bsz): # 提取当前批次项的数据 current_ids = inputs_ids[i:i+1] # shape: (1, total_tokens) current_num_tokens = num_tokens_list[i] current_prompt_mask = prompt_mask[i:i+1] # shape: (1, total_tokens) num_samples_in_item = len(current_num_tokens) # 验证当前批次项的 token 总数是否匹配 assert total_tokens == torch.sum(current_num_tokens), \ f"批次项 {i} 的 num_tokens 之和 ({torch.sum(current_num_tokens)}) 与 total_tokens ({total_tokens}) 不匹配"
eligible_for_masking = ~current_prompt_mask
# 如果没有任何 token 可以被 mask,直接使用原始输入,并设置 p_mask 为 eps if not eligible_for_masking.any(): noisy_ids_list.append(current_ids) final_masked_indices_list.append(torch.zeros_like(current_prompt_mask, dtype=torch.bool)) # p_mask_per_token 的形状应为 (1, total_tokens) 以便后续拼接 p_masks_per_token_list.append(torch.full((1, total_tokens), eps, device=device, dtype=torch.float)) continue
# --- 尝试生成 mask,确保至少 mask 一个 token --- final_masked_indices_item = torch.zeros_like(current_prompt_mask, dtype=torch.bool) p_mask_per_token = None for _ in range(max_tries): # 为每个逻辑样本生成一个独立的噪声率 t t = torch.rand(num_samples_in_item, device=device) p_mask_per_sample = (1 - eps) * t + eps
# 将每个样本的噪声率扩展到其所有 token 上 p_mask_per_token_1d = torch.repeat_interleave(p_mask_per_sample, current_num_tokens) p_mask_per_token = p_mask_per_token_1d.unsqueeze(0) # shape: (1, total_tokens)
# 根据噪声率生成随机 mask masked_indices = torch.rand_like(p_mask_per_token) < p_mask_per_token # 应用 prompt mask,确保 prompt 不被 mask final_masked_indices_item = masked_indices & eligible_for_masking
# 如果成功 mask 了至少一个 token,则跳出尝试循环 if final_masked_indices_item.any(): break # 如果 max_tries 之后仍然没有 mask 任何 token (极小概率),就强制 mask 一个可 mask 的 token if not final_masked_indices_item.any(): eligible_indices = torch.nonzero(eligible_for_masking.squeeze(0), as_tuple=True)[0] if len(eligible_indices) > 0: # 随机选择一个可 mask 的位置 random_choice = torch.randint(0, len(eligible_indices), (1,)).item() force_mask_idx = eligible_indices[random_choice] final_masked_indices_item[0, force_mask_idx] = True
# --- 根据最终的 mask 生成带噪声的 IDs --- noisy_ids_item = torch.where( final_masked_indices_item, mask_id, current_ids ) # 保存这个批次项的结果 noisy_ids_list.append(noisy_ids_item) final_masked_indices_list.append(final_masked_indices_item) p_masks_per_token_list.append(p_mask_per_token)
# 3. 将列表中的结果堆叠成最终的批处理张量 noisy_input_ids = torch.cat(noisy_ids_list, dim=0) final_masked_indices = torch.cat(final_masked_indices_list, dim=0) p_mask_full = torch.cat(p_masks_per_token_list, dim=0) # 4. 提取被 mask 位置对应的噪声率 p_masks = p_mask_full[final_masked_indices] return noisy_input_ids, final_masked_indices, p_masks
def block_diff_mask(b, h, q_idx, kv_idx, block_size=None, n=None): """ Constructs the specialized block diffusion attention mask for training composed of three masks: - **Block Diagonal Mask (M_BD)**: Self-attention within noised blocks - **Offset Block Causal Mask (M_OBC)**: Cross-attention for conditional context - **Block Causal Mask (M_BC)**: Attention to update x0
Args: b, h: Batch and head indices (ignored for mask logic). q_idx, kv_idx: Query and Key indices. seq_len: Total sequence length. block_size: Defines the block structure.
Returns: A boolean attention mask. """
# Indicate whether token belongs to xt or x0 x0_flag_q = q_idx >= n x0_flag_kv = kv_idx >= n
# Compute block indices block_q = torch.where( x0_flag_q == 1, (q_idx - n) // block_size, q_idx // block_size ) block_kv = torch.where( x0_flag_kv == 1, (kv_idx - n) // block_size, kv_idx // block_size )
# **1. Block Diagonal Mask (M_BD) ** block_diagonal = (block_q == block_kv) & (x0_flag_q == x0_flag_kv)
# **2. Offset Block-Causal Mask (M_OBC) ** offset_block_causal = (block_q > block_kv) & ( x0_flag_kv == 1) & (x0_flag_q == 0)
# **3. Block-Causal Mask (M_BC) ** block_causal = (block_q >= block_kv) & (x0_flag_kv == 1) & (x0_flag_q == 1)
# **4. Combine Masks ** return block_diagonal | offset_block_causal | block_causal
def block_attn_mask(num_tokens, block_size, device): masks = [] for i in range(len(num_tokens)): cur_masks = [] for num in num_tokens[i]: # 全部返回 n*n 而非 2n*2n single_mask = block_diff_mask( b=None, h=None, q_idx=torch.arange(num * 2, device=device)[:, None], kv_idx=torch.arange(num * 2, device=device)[None, :], block_size=block_size, n=num, ) cur_masks.append(single_mask) masks.append(torch.block_diag(*cur_masks)) masks = torch.stack(masks, dim=0) return masks
def create_causal_mask_from_labels(token_labels: torch.LongTensor, block_size: int) -> torch.Tensor: """ Build a causal mask from token_labels for token-label SFT.
token_labels shape: (batch_size, seq_len) - 0: prompt - 1..block_size: clean block labels (generation steps) - block_size + 1: mask block labels - -1: padding """ if token_labels.dim() != 2: raise ValueError(f"`token_labels` must be 2D, got shape {tuple(token_labels.shape)}.")
bsz, _ = token_labels.shape device = token_labels.device
is_prompt = token_labels == 0 is_data = (token_labels > 0) & (token_labels <= block_size) is_mask = token_labels == (block_size + 1) is_pad = token_labels == -1
time_steps = token_labels.clone().float() for b in range(bsz): data_vals = time_steps[b, is_data[b]] mask_indices = torch.nonzero(is_mask[b], as_tuple=True)[0] if mask_indices.numel() == 0: continue if mask_indices.numel() == data_vals.numel(): time_steps[b, mask_indices] = data_vals else: min_len = min(mask_indices.numel(), data_vals.numel()) time_steps[b, mask_indices[:min_len]] = data_vals[:min_len]
time_steps[is_pad] = float("inf")
type_i = torch.zeros_like(token_labels) # 1=data, 2=mask type_i[is_data] = 1 type_i[is_mask] = 2 type_i = type_i.unsqueeze(1).unsqueeze(2) # (B, 1, L, 1) type_j = type_i.view(bsz, 1, 1, -1) # (B, 1, 1, L)
time_i = time_steps.unsqueeze(1).unsqueeze(2) time_j = time_steps.unsqueeze(1).unsqueeze(1)
is_prompt_j = is_prompt.view(bsz, 1, 1, -1) is_pad_i = is_pad.view(bsz, 1, -1, 1) is_pad_j = is_pad.view(bsz, 1, 1, -1)
mask_prompt = is_prompt_j mask_data_data = (type_i == 1) & (type_j == 1) & (time_j <= time_i) mask_data_mask = (type_i == 1) & (type_j == 2) & (time_j > time_i) mask_mask_data = (type_i == 2) & (type_j == 1) & (time_j < time_i) mask_mask_mask = (type_i == 2) & (type_j == 2) & (time_j >= time_i) mask_prompt_internal = (token_labels.unsqueeze(1).unsqueeze(2) == 0) & is_prompt_j
final_mask = ( mask_prompt | mask_data_data | mask_data_mask | mask_mask_data | mask_mask_mask | mask_prompt_internal ) final_mask = final_mask & (~is_pad_i) & (~is_pad_j) return final_mask.squeeze(1).to(dtype=torch.bool, device=device)
def create_multi_block_causal_mask( token_labels: torch.LongTensor, block_ids: torch.LongTensor, block_size: int, block_causal_prompt: bool = True,) -> torch.Tensor: """ Generate attention mask for multi-block causal mask training.
Args: token_labels: (B, L) — 0=prompt, 1..block_size=data step, block_size+1=mask, -1=pad block_ids: (B, L) — -1=prompt/pad, 0,1,2,...=block index block_size: denoising steps per block block_causal_prompt: True: prompt block-level causal (SDAR-style), prompt 不看 data/mask False: 原版, prompt 看所有 prompt + mask, 所有人看所有 prompt
Returns: attn_mask: (B, L, L) bool tensor (squeezed from (B,1,L,L)), True = visible """ B, L = token_labels.shape device = token_labels.device
is_prompt = (token_labels == 0) is_data = (token_labels > 0) & (token_labels <= block_size) is_mask = (token_labels == (block_size + 1)) is_pad = (token_labels == -1)
time_steps = token_labels.clone().float() time_steps[is_pad] = float("inf") time_steps[is_prompt] = 0
for b in range(B): blk_vals = block_ids[b][block_ids[b] >= 0].unique() for blk in blk_vals: blk_mask = (block_ids[b] == blk) data_in_blk = blk_mask & is_data[b] mask_in_blk = blk_mask & is_mask[b] data_steps = time_steps[b, data_in_blk] mask_indices = torch.nonzero(mask_in_blk, as_tuple=True)[0] n_data = data_steps.shape[0] n_mask = mask_indices.shape[0] if n_mask > 0 and n_data > 0: min_len = min(n_data, n_mask) time_steps[b, mask_indices[:min_len]] = data_steps[:min_len]
type_vals = torch.zeros_like(token_labels) type_vals[is_data] = 1 type_vals[is_mask] = 2
type_i = type_vals[:, None, :, None] type_j = type_vals[:, None, None, :] time_i = time_steps[:, None, :, None] time_j = time_steps[:, None, None, :] blkid_i = block_ids[:, None, :, None].float() blkid_j = block_ids[:, None, None, :].float()
is_prompt_i = is_prompt.view(B, 1, L, 1) is_prompt_j = is_prompt.view(B, 1, 1, L) is_pad_i = is_pad.view(B, 1, L, 1) is_pad_j = is_pad.view(B, 1, 1, L)
if block_causal_prompt: # 新版: prompt block-level causal (SDAR-style) # prompt 按 block_size 分块, 每个 block 只看前面的 block, 不看 data/mask prompt_cumpos = is_prompt.long().cumsum(dim=1) - 1 prompt_blk = prompt_cumpos // block_size prompt_blk_i = prompt_blk[:, None, :, None] prompt_blk_j = prompt_blk[:, None, None, :] rule_prompt = is_prompt_i & is_prompt_j & (prompt_blk_j <= prompt_blk_i) rule_see_prompt = (~is_prompt_i) & (~is_pad_i) & is_prompt_j else: # 原版: prompt 看所有 prompt + mask, 所有人看所有 prompt rule_prompt = is_prompt_i & is_prompt_j is_mask_j = is_mask.view(B, 1, 1, L) rule_prompt = rule_prompt | (is_prompt_i & is_mask_j) rule_see_prompt = is_prompt_j.expand(B, 1, L, L)
same_block = (blkid_i == blkid_j) & (blkid_i >= 0) intra_dd = same_block & (type_i == 1) & (type_j == 1) & (time_j <= time_i) intra_dm = same_block & (type_i == 1) & (type_j == 2) & (time_j > time_i) intra_md = same_block & (type_i == 2) & (type_j == 1) & (time_j < time_i) intra_mm = same_block & (type_i == 2) & (type_j == 2) & (time_j >= time_i)
cross_block_data = (blkid_i > blkid_j) & (blkid_j >= 0) & (type_j == 1)
final_mask = ( rule_prompt | rule_see_prompt | intra_dd | intra_dm | intra_md | intra_mm | cross_block_data ) final_mask = final_mask & (~is_pad_i) & (~is_pad_j) return final_mask.squeeze(1).to(dtype=torch.bool, device=device)
@torch.compile(fullgraph=True, mode="max-autotune-no-cudagraphs")def fused_flex_attention(query, key, value, attention_mask, **kwargs): # Force small backward-kernel tiles so the Triton kernel fits in the # 99KB / 101376 B opt-in shared-memory budget on sm_120 (RTX PRO 6000 # Blackwell). Default autotuner picks tiles that each require >100KB # SRAM, so every backward config gets rejected. opts = kwargs.pop("kernel_options", None) or {} # Forward kernel: the surviving autotune winner uses these tile sizes; set them # explicitly to skip the noisy ~24 rejected configs on every first run. opts.setdefault("fwd_BLOCK_M", 128) opts.setdefault("fwd_BLOCK_N", 64) opts.setdefault("fwd_num_warps", 8) opts.setdefault("fwd_num_stages", 1) # Backward kernel: smaller tiles so dK/dV and dQ passes fit in 99KB SMEM. opts.setdefault("bwd_BLOCK_M1", 32) opts.setdefault("bwd_BLOCK_N1", 64) opts.setdefault("bwd_BLOCK_M2", 64) opts.setdefault("bwd_BLOCK_N2", 32) opts.setdefault("bwd_num_warps", 4) opts.setdefault("bwd_num_stages", 1) return flex_attention( query, key, value, block_mask=attention_mask, kernel_options=opts, **kwargs, )
@use_kernel_forward_from_hub("RMSNorm")class SDARRMSNorm(nn.Module): def __init__(self, hidden_size, eps=1e-6): """ SDARRMSNorm is equivalent to T5LayerNorm """ super().__init__() self.weight = nn.Parameter(torch.ones(hidden_size)) self.variance_epsilon = eps
def forward(self, hidden_states): return flash_rms_norm( hidden_states, weight=self.weight, bias=None, eps=self.variance_epsilon) ''' input_dtype = hidden_states.dtype hidden_states = hidden_states.to(torch.float32) variance = hidden_states.pow(2).mean(-1, keepdim=True) hidden_states = hidden_states * \ torch.rsqrt(variance + self.variance_epsilon) return self.weight * hidden_states.to(input_dtype) '''
def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}"
class SDARMLP(nn.Module): def __init__(self, config): super().__init__() self.config = config self.hidden_size = config.hidden_size self.intermediate_size = config.intermediate_size self.gate_proj = nn.Linear( self.hidden_size, self.intermediate_size, bias=False) self.up_proj = nn.Linear( self.hidden_size, self.intermediate_size, bias=False) self.down_proj = nn.Linear( self.intermediate_size, self.hidden_size, bias=False) self.act_fn = ACT2FN[config.hidden_act]
def forward(self, x): if liger_kernel_is_available: return self.down_proj(LigerSiLUMulFunction.apply(self.gate_proj(x), self.up_proj(x))) else: down_proj = self.down_proj(self.act_fn( self.gate_proj(x)) * self.up_proj(x)) return down_proj
def rotate_half(x): """Rotates half the hidden dims of the input.""" x1 = x[..., : x.shape[-1] // 2] x2 = x[..., x.shape[-1] // 2:] return torch.cat((-x2, x1), dim=-1)
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): """Applies Rotary Position Embedding to the query and key tensors.
Args: q (`torch.Tensor`): The query tensor. k (`torch.Tensor`): The key tensor. cos (`torch.Tensor`): The cosine part of the rotary embedding. sin (`torch.Tensor`): The sine part of the rotary embedding. position_ids (`torch.Tensor`, *optional*): Deprecated and unused. unsqueeze_dim (`int`, *optional*, defaults to 1): The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. Returns: `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. """ cos = cos.unsqueeze(unsqueeze_dim) sin = sin.unsqueeze(unsqueeze_dim) q_embed = (q * cos) + (rotate_half(q) * sin) k_embed = (k * cos) + (rotate_half(k) * sin) return q_embed, k_embed
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: """ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) """ batch, num_key_value_heads, slen, head_dim = hidden_states.shape if n_rep == 1: return hidden_states hidden_states = hidden_states[:, :, None, :, :].expand( batch, num_key_value_heads, n_rep, slen, head_dim) return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
def eager_attention_forward( module: nn.Module, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attention_mask: Optional[torch.Tensor], scaling: float, dropout: float = 0.0, **kwargs,): key_states = repeat_kv(key, module.num_key_value_groups) value_states = repeat_kv(value, module.num_key_value_groups)
attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling if attention_mask is not None: causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] attn_weights = attn_weights + causal_mask
attn_weights = nn.functional.softmax( attn_weights, dim=-1, dtype=torch.float32).to(query.dtype) attn_weights = nn.functional.dropout( attn_weights, p=dropout, training=module.training) attn_output = torch.matmul(attn_weights, value_states) attn_output = attn_output.transpose(1, 2).contiguous()
return attn_output, attn_weights
class SDARAttention(nn.Module): """Multi-headed attention from 'Attention Is All You Need' paper"""
def __init__(self, config: SDARConfig, layer_idx: int): super().__init__() self.config = config self.layer_idx = layer_idx self.head_dim = getattr( config, "head_dim", config.hidden_size // config.num_attention_heads) self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads self.scaling = self.head_dim**-0.5 self.attention_dropout = config.attention_dropout self.is_causal = True
self.hidden_size = config.hidden_size self.num_attention_heads = config.num_attention_heads self.num_key_value_heads = config.num_key_value_heads
self.q_proj = nn.Linear( config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.attention_bias ) self.k_proj = nn.Linear( config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias ) self.v_proj = nn.Linear( config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias ) self.o_proj = nn.Linear( config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias ) # unlike olmo, only on the head dim! self.q_norm = SDARRMSNorm(self.head_dim, eps=config.rms_norm_eps) # thus post q_norm does not need reshape self.k_norm = SDARRMSNorm(self.head_dim, eps=config.rms_norm_eps) self.sliding_window = config.sliding_window if not ( self.config.use_sliding_window and getattr(self.config, "sliding_window", None) is not None and self.layer_idx >= self.config.max_window_layers ): self.sliding_window = None
def forward( self, hidden_states: torch.Tensor, position_embeddings: Tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] bsz, q_len = input_shape hidden_shape = (*input_shape, -1, self.head_dim)
query_states = self.q_norm(self.q_proj( hidden_states).view(hidden_shape)).transpose(1, 2) key_states = self.k_norm(self.k_proj( hidden_states).view(hidden_shape)).transpose(1, 2) value_states = self.v_proj(hidden_states).view( hidden_shape).transpose(1, 2)
cos, sin = position_embeddings query_states, key_states = apply_rotary_pos_emb( query_states, key_states, cos, sin)
if past_key_value is not None and kwargs.get("store_kv", False): # sin and cos are specific to RoPE models; cache_position needed for the static cache key_states, value_states = past_key_value.update( key_states, value_states, self.layer_idx) elif past_key_value is not None and not kwargs.get("store_kv", False) and len(past_key_value) > self.layer_idx: # only retrive, do not store kv past_key_states, past_value_states = past_key_value[self.layer_idx] key_states = torch.cat( [past_key_states, key_states], dim=-2) value_states = torch.cat( [past_value_states, value_states], dim=-2)
if self.training: attn_output, attn_weights = fused_flex_attention( query=query_states, key=key_states, value=value_states, attention_mask=attention_mask, enable_gqa=True, scale=self.scaling, return_lse=True ) attn_weights = attn_weights.to( value_states.dtype) if attn_weights is not None else None attn_output = rearrange(attn_output, 'b h l d -> b l (h d)') else: attention_mask = attention_mask.bool() if attention_mask is not None else None attn_weights = None if torch.all(attention_mask): # decoding query_states = query_states.transpose(1, 2) key_states = key_states.transpose(1, 2) value_states = value_states.transpose(1, 2) attn_output = flash_attn_func( query_states, key_states, value_states, causal=False, softmax_scale=self.scaling ) attn_output = rearrange(attn_output, 'b l h d -> b l (h d)') else: # prefilling attn_output = F.scaled_dot_product_attention( query=query_states, key=key_states, value=value_states, attn_mask=attention_mask, is_causal=False, scale=self.scaling, enable_gqa=True ) attn_output = rearrange(attn_output, 'b h l d -> b l (h d)') attn_output = self.o_proj(attn_output) return attn_output, attn_weights # , attn_weights
class SDARDecoderLayer(GradientCheckpointingLayer): def __init__(self, config: SDARConfig, layer_idx: int): super().__init__() self.hidden_size = config.hidden_size self.self_attn = SDARAttention(config=config, layer_idx=layer_idx) self.mlp = SDARMLP(config) self.input_layernorm = SDARRMSNorm( config.hidden_size, eps=config.rms_norm_eps) self.post_attention_layernorm = SDARRMSNorm( config.hidden_size, eps=config.rms_norm_eps) if ( config.sliding_window and config._attn_implementation != "flash_attention_2" ): # diff with Llama is this warning logger.warning_once( f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; " "unexpected results may be encountered." )
def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, store_kv: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, # necessary, but kept here for BC position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, **kwargs: Unpack[FlashAttentionKwargs], ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states)
# Self Attention hidden_states, self_attn_weights = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, store_kv=store_kv, cache_position=cache_position, position_embeddings=position_embeddings, **kwargs, ) hidden_states = residual + hidden_states
# Fully Connected residual = hidden_states hidden_states = self.post_attention_layernorm(hidden_states) hidden_states = self.mlp(hidden_states) hidden_states = residual + hidden_states
outputs = (hidden_states,) if output_attentions: outputs += (self_attn_weights,)
return outputs
@auto_docstringclass SDARPreTrainedModel(PreTrainedModel): config_class = SDARConfig base_model_prefix = "model" supports_gradient_checkpointing = True _no_split_modules = ["SDARDecoderLayer"] _skip_keys_device_placement = ["past_key_values"] _supports_flash_attn_2 = True _supports_sdpa = True _supports_flex_attn = True _supports_cache_class = True _supports_quantized_cache = True _supports_static_cache = True _supports_attention_backend = True
def _init_weights(self, module): std = self.config.initializer_range if isinstance(module, nn.Linear): module.weight.data.normal_(mean=0.0, std=std) if module.bias is not None: module.bias.data.zero_() elif isinstance(module, nn.Embedding): module.weight.data.normal_(mean=0.0, std=std) if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() elif isinstance(module, SDARRMSNorm): module.weight.data.fill_(1.0)
class SDARRotaryEmbedding(nn.Module): def __init__(self, config: SDARConfig, device=None): super().__init__() # BC: "rope_type" was originally "type" if hasattr(config, "rope_scaling") and config.rope_scaling is not None: self.rope_type = config.rope_scaling.get( "rope_type", config.rope_scaling.get("type")) else: self.rope_type = "default" self.max_seq_len_cached = config.max_position_embeddings self.original_max_seq_len = config.max_position_embeddings
self.config = config self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
inv_freq, self.attention_scaling = self.rope_init_fn( self.config, device) self.register_buffer("inv_freq", inv_freq, persistent=False) self.original_inv_freq = self.inv_freq
@torch.no_grad() # power user: used with advanced RoPE types (e.g. dynamic rope) @dynamic_rope_update def forward(self, x, position_ids, token_labels: Optional[torch.LongTensor] = None): inv_freq_expanded = self.inv_freq[None, :, None].float().expand( position_ids.shape[0], -1, 1).to(x.device) position_ids_expanded = position_ids[:, None, :].float()
device_type = x.device.type if isinstance( x.device.type, str) and x.device.type != "mps" else "cpu" with torch.autocast(device_type=device_type, enabled=False): # Force float32 freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) emb = torch.cat((freqs, freqs), dim=-1) cos = emb.cos() * self.attention_scaling sin = emb.sin() * self.attention_scaling
cos = cos.to(dtype=x.dtype) sin = sin.to(dtype=x.dtype)
if token_labels is not None: if token_labels.shape != position_ids.shape: raise ValueError( f"`token_labels` shape {tuple(token_labels.shape)} must match `position_ids` shape {tuple(position_ids.shape)}." )
clean_min_label = 1 clean_max_label = self.config.block_size mask_label = self.config.block_size + 1 token_labels = token_labels.to(position_ids.device) for batch_idx in range(token_labels.size(0)): clean_indices = torch.nonzero( (token_labels[batch_idx] >= clean_min_label) & (token_labels[batch_idx] <= clean_max_label), as_tuple=True, )[0] mask_indices = torch.nonzero(token_labels[batch_idx] == mask_label, as_tuple=True)[0] if mask_indices.numel() == 0: continue if clean_indices.numel() != mask_indices.numel(): raise ValueError( "The clean block and mask block must have equal lengths for RoPE frequency copy." ) cos[batch_idx, mask_indices] = cos[batch_idx, clean_indices] sin[batch_idx, mask_indices] = sin[batch_idx, clean_indices]
return cos, sin
@auto_docstringclass SDARModel(SDARPreTrainedModel): def __init__(self, config: SDARConfig): super().__init__(config) self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size
self.embed_tokens = nn.Embedding( config.vocab_size, config.hidden_size, self.padding_idx) self.layers = nn.ModuleList( [SDARDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)] ) self.norm = SDARRMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.rotary_emb = SDARRotaryEmbedding(config=config) self.gradient_checkpointing = False
# Initialize weights and apply final processing self.post_init()
def get_input_embeddings(self): return self.embed_tokens
def set_input_embeddings(self, value): self.embed_tokens = value
@can_return_tuple @auto_docstring def forward( self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, token_labels: Optional[torch.LongTensor] = None, past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, store_kv: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> BaseModelOutputWithPast: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) use_cache = use_cache if use_cache is not None else self.config.use_cache
if (input_ids is None) ^ (inputs_embeds is not None): raise ValueError( "You must specify exactly one of input_ids or inputs_embeds")
if self.gradient_checkpointing and self.training and use_cache: logger.warning_once( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`." ) use_cache = False
# TODO (joao): remove this exception in v4.56 -- it exists for users that try to pass a legacy cache if not isinstance(past_key_values, (type(None), Cache)): raise ValueError( "The `past_key_values` should be either a `Cache` object or `None`.")
if inputs_embeds is None: inputs_embeds = self.embed_tokens(input_ids)
if use_cache and past_key_values is None: past_key_values = DynamicCache()
if cache_position is None: past_seen_tokens = past_key_values.get_seq_length( ) if past_key_values is not None else 0 cache_position = torch.arange( past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device )
if position_ids is None: position_ids = cache_position.unsqueeze(0).expand(inputs_embeds.shape[0], -1)
# causal_mask = self._update_causal_mask( # attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions # )
hidden_states = inputs_embeds
# create position embeddings to be shared across the decoder layers position_embeddings = self.rotary_emb(hidden_states, position_ids)
# RoPE frequency copy: for single-block training (without block_ids), # data and mask have different position_ids, so copy data's RoPE to mask. # For multi-block training (with block_ids), position_ids are already shared # between data and mask, so no copy is needed. if token_labels is not None and not hasattr(self, '_skip_rope_copy'): cos, sin = position_embeddings block_size = self.config.block_size clean_min_label, clean_max_label = 1, block_size mask_label = block_size + 1 tl = token_labels.to(position_ids.device) for b_idx in range(tl.size(0)): clean_idx = torch.nonzero( (tl[b_idx] >= clean_min_label) & (tl[b_idx] <= clean_max_label), as_tuple=True )[0] mask_idx = torch.nonzero(tl[b_idx] == mask_label, as_tuple=True)[0] if mask_idx.numel() > 0 and clean_idx.numel() == mask_idx.numel(): cos[b_idx, mask_idx] = cos[b_idx, clean_idx] sin[b_idx, mask_idx] = sin[b_idx, clean_idx] position_embeddings = (cos, sin)
# decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None
for decoder_layer in self.layers[: self.config.num_hidden_layers]: if output_hidden_states: all_hidden_states += (hidden_states,)
layer_outputs = decoder_layer( hidden_states, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_values, output_attentions=output_attentions, use_cache=use_cache, store_kv=store_kv, cache_position=cache_position, position_embeddings=position_embeddings, **flash_attn_kwargs, )
hidden_states = layer_outputs[0]
if output_attentions: all_self_attns += (layer_outputs[1],)
hidden_states = self.norm(hidden_states)
# add hidden states from the last decoder layer if output_hidden_states: all_hidden_states += (hidden_states,)
return BaseModelOutputWithPast( last_hidden_state=hidden_states, past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, )
def _update_causal_mask( self, attention_mask: Union[torch.Tensor, "BlockMask"], input_tensor: torch.Tensor, cache_position: torch.Tensor, past_key_values: Cache, output_attentions: bool = False, ): if self.config._attn_implementation == "flash_attention_2": if attention_mask is not None and past_key_values is not None: is_padding_right = attention_mask[:, - 1].sum().item() != input_tensor.size()[0] if is_padding_right: raise ValueError( "You are attempting to perform batched generation with padding_side='right'" " this may lead to unexpected behaviour for Flash Attention version of Qwen3. Make sure to " " call `tokenizer.padding_side = 'left'` before tokenizing the input. " ) if attention_mask is not None and 0.0 in attention_mask: return attention_mask return None if self.config._attn_implementation == "flex_attention": if isinstance(attention_mask, torch.Tensor): seq_len_q, seq_len_kv = attention_mask.shape assert seq_len_q == seq_len_kv, f"got {attention_mask.shape=}" attention_mask = create_block_mask( # 2d bool tensor, shape: [2*seqlen, 2*seqlen] lambda b, h, q_idx, kv_idx: attention_mask[q_idx, kv_idx], B=None, H=None, Q_LEN=seq_len_q, KV_LEN=seq_len_kv, ) else: # Here we pass in flex mask computed externally assert isinstance(attention_mask, BlockMask) return attention_mask
# For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail # to infer the attention mask. past_seen_tokens = past_key_values.get_seq_length( ) if past_key_values is not None else 0 using_static_cache = isinstance(past_key_values, StaticCache) using_sliding_window_cache = isinstance( past_key_values, SlidingWindowCache)
# When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward if ( self.config._attn_implementation == "sdpa" and not (using_static_cache or using_sliding_window_cache) and not output_attentions ): if AttentionMaskConverter._ignore_causal_mask_sdpa( attention_mask, inputs_embeds=input_tensor, past_key_values_length=past_seen_tokens, sliding_window=self.config.sliding_window, is_training=self.training, ): return None
dtype = input_tensor.dtype min_dtype = torch.finfo(dtype).min sequence_length = input_tensor.shape[1] # SlidingWindowCache or StaticCache if using_sliding_window_cache or using_static_cache: target_length = past_key_values.get_max_cache_shape() # DynamicCache or no cache else: target_length = ( attention_mask.shape[-1] if isinstance(attention_mask, torch.Tensor) else past_seen_tokens + sequence_length + 1 )
# In case the provided `attention` mask is 2D, we generate a causal mask here (4D). causal_mask = self._prepare_4d_causal_attention_mask_with_cache_position( attention_mask, sequence_length=sequence_length, target_length=target_length, dtype=dtype, cache_position=cache_position, batch_size=input_tensor.shape[0], config=self.config, past_key_values=past_key_values, )
if ( self.config._attn_implementation == "sdpa" and attention_mask is not None and attention_mask.device.type in ["cuda", "xpu", "npu"] and not output_attentions ): # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. # Details: https://github.com/pytorch/pytorch/issues/110213 causal_mask = AttentionMaskConverter._unmask_unattended( causal_mask, min_dtype)
return causal_mask
@staticmethod def _prepare_4d_causal_attention_mask_with_cache_position( attention_mask: torch.Tensor, sequence_length: int, target_length: int, dtype: torch.dtype, cache_position: torch.Tensor, batch_size: int, config: SDARConfig, past_key_values: Cache, ): """ Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.
Args: attention_mask (`torch.Tensor`): A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`. sequence_length (`int`): The sequence length being processed. target_length (`int`): The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet. dtype (`torch.dtype`): The dtype to use for the 4D attention mask. cache_position (`torch.Tensor`): Indices depicting the position of the input sequence tokens in the sequence. batch_size (`torch.Tensor`): Batch size. config (`SDARConfig`): The model's configuration class past_key_values (`Cache`): The cache class that is being used currently to generate """ if attention_mask is not None and attention_mask.dim() == 4: # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing. causal_mask = attention_mask else: min_dtype = torch.finfo(dtype).min causal_mask = torch.full( (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=cache_position.device ) diagonal_attend_mask = torch.arange(target_length, device=cache_position.device) > cache_position.reshape( -1, 1 ) text_config = config.get_text_config() if getattr(text_config, "use_sliding_window", True) and text_config.sliding_window is not None: # if we have sliding window, we should not attend to tokens beyond sliding window length, so we mask them out also # the check is needed to verify is current checkpoint was trained with sliding window or not if not isinstance(past_key_values, SlidingWindowCache) or sequence_length > target_length: sliding_attend_mask = torch.arange(target_length, device=cache_position.device) <= ( cache_position.reshape(-1, 1) - text_config.sliding_window ) diagonal_attend_mask.bitwise_or_(sliding_attend_mask) causal_mask *= diagonal_attend_mask causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1) if attention_mask is not None: causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit if attention_mask.shape[-1] > target_length: attention_mask = attention_mask[:, :target_length] mask_length = attention_mask.shape[-1] padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :].to( causal_mask.device ) padding_mask = padding_mask == 0 causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( padding_mask, min_dtype ) return causal_mask
class KwargsForCausalLM(FlashAttentionKwargs, LossKwargs): ...
@auto_docstringclass SDARForCausalLM(SDARPreTrainedModel, GenerationMixin): _tied_weights_keys = ["lm_head.weight"] _tp_plan = {"lm_head": "colwise_rep"} _pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config): super().__init__(config) self.model = SDARModel(config) self.vocab_size = config.vocab_size self.lm_head = nn.Linear( config.hidden_size, config.vocab_size, bias=False)
# Initialize weights and apply final processing self.post_init()
def get_input_embeddings(self): return self.model.embed_tokens
def set_input_embeddings(self, value): self.model.embed_tokens = value
def get_output_embeddings(self): return self.lm_head
def set_output_embeddings(self, new_embeddings): self.lm_head = new_embeddings
def set_decoder(self, decoder): self.model = decoder
def get_decoder(self): return self.model
def prepare_for_bd_training(self, inputs_ids, position_ids, prompt_mask): bsz, seq_len = inputs_ids.shape num_tokens = calculate_token_nums(position_ids) # List[torch.Tensor] noisy_inputs_ids, logits_to_keep_half, p_mask = forward_add_noise_packed( inputs_ids=inputs_ids, num_tokens_list=num_tokens, prompt_mask=prompt_mask, mask_id=self.config.mask_token_id, ) router_noisy_part_list = [] for i in range(bsz): cur_router_noisy_part = (torch.arange(num_tokens[i].shape[0] *2) % 2 == 0).to(inputs_ids.device) cur_router_noisy_part = cur_router_noisy_part.repeat_interleave(num_tokens[i].repeat_interleave(2)) router_noisy_part_list.append(cur_router_noisy_part) router_noisy_part = torch.stack(router_noisy_part_list, dim=0)
# concated inputs_ids: (bzs, seq_len x 2) concat_inputs_ids = inputs_ids.repeat(1, 2) # concated logits_to_keep: (bsz, seq_len x 2) logits_to_keep = torch.zeros( bsz, 2 * seq_len, dtype=torch.bool, device=inputs_ids.device) # concated position_ids: (bsz, seq_len x 2) concat_position_ids = torch.zeros( bsz, 2 * seq_len, dtype=position_ids.dtype, device=position_ids.device) for i in range(bsz): concat_inputs_ids[i][router_noisy_part[i]] = noisy_inputs_ids[i] concat_inputs_ids[i][~router_noisy_part[i]] = inputs_ids[i]
logits_to_keep[i][router_noisy_part[i]] = logits_to_keep_half[i]
concat_position_ids[i][router_noisy_part[i]] = position_ids[i] concat_position_ids[i][~router_noisy_part[i]] = position_ids[i]
# create flex_attention mask attention_mask = block_attn_mask(num_tokens, self.config.block_size, inputs_ids.device) flex_attention_mask_3d = create_block_mask( lambda b, h, q_idx, kv_idx: attention_mask[b, q_idx, kv_idx], B=attention_mask.size(0), H=None, Q_LEN=attention_mask.size(1), KV_LEN=attention_mask.size(2), )
return concat_inputs_ids, concat_position_ids, flex_attention_mask_3d, logits_to_keep_half, logits_to_keep, p_mask
@can_return_tuple @auto_docstring def forward( self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, token_labels: Optional[torch.LongTensor] = None, block_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], ) -> CausalLMOutputWithPast: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
Example:
```python >>> from transformers import AutoTokenizer, SDARForCausalLM
>>> model = SDARForCausalLM.from_pretrained("DiffuOpen/SDAR-1.7B-Chat") >>> tokenizer = AutoTokenizer.from_pretrained("DiffuOpen/SDAR-1.7B-Chat")
>>> prompt = "Hey, are you conscious? Can you talk to me?" >>> inputs = tokenizer(prompt, return_tensors="pt")
>>> # Generate >>> generate_ids = model.generate(inputs.input_ids, max_length=30) >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you." ```""" output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) if self.training: assert inputs_embeds is None, "only support input_ids during training" assert labels is not None, "Labels must be provided for training." # Trace SFT path: pre-computed block attention mask provided via kwargs block_attention_mask = kwargs.pop("block_attention_mask", None) if block_attention_mask is not None: # block_attention_mask: (B, L, L) boolean tensor flex_attention_mask_3d = create_block_mask( lambda b, h, q_idx, kv_idx: block_attention_mask[b, q_idx, kv_idx], B=block_attention_mask.size(0), H=None, Q_LEN=block_attention_mask.size(1), KV_LEN=block_attention_mask.size(2), ) outputs = self.model( input_ids=input_ids, attention_mask=flex_attention_mask_3d, position_ids=position_ids, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=True, cache_position=cache_position, ) hidden_states = outputs.last_hidden_state logits = self.lm_head(hidden_states) # Unshifted cross-entropy loss (diffusion-style) loss = nn.CrossEntropyLoss(ignore_index=-100)( logits.view(-1, self.config.vocab_size), labels.view(-1) ) logits = None elif token_labels is not None: if input_ids is None: raise ValueError("`input_ids` is required in token-label SFT training.") if token_labels.shape != input_ids.shape: raise ValueError( f"`token_labels` shape {tuple(token_labels.shape)} must match `input_ids` shape {tuple(input_ids.shape)}." ) # Multi-block mask when block_ids provided, else single-block if block_ids is not None: bcp = getattr(self.config, "block_causal_prompt", True) token_label_mask = create_multi_block_causal_mask( token_labels, block_ids, self.config.block_size, block_causal_prompt=bcp, ) else: token_label_mask = create_causal_mask_from_labels(token_labels, self.config.block_size) flex_attention_mask_3d = create_block_mask( lambda b, h, q_idx, kv_idx: token_label_mask[b, q_idx, kv_idx], B=token_label_mask.size(0), H=None, Q_LEN=token_label_mask.size(1), KV_LEN=token_label_mask.size(2), ) outputs = self.model( input_ids=input_ids, attention_mask=flex_attention_mask_3d, position_ids=position_ids, token_labels=token_labels, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=True, cache_position=cache_position, **kwargs, ) hidden_states = outputs.last_hidden_state logits = self.lm_head(hidden_states) masked_labels = labels.masked_fill(token_labels != (self.config.block_size + 1), -100) if not torch.any(masked_labels != -100): raise ValueError("No valid supervision token found for token-label SFT loss.") loss = nn.CrossEntropyLoss(ignore_index=-100)( logits.view(-1, self.config.vocab_size), masked_labels.view(-1) ) logits = None else: prompt_mask = labels == -100 position_ids = modify_padded_position_ids_2d(position_ids) concat_inputs_ids, concat_position_ids, flex_attention_mask_3d, logits_to_keep_half, logits_to_keep, p_mask = self.prepare_for_bd_training(input_ids, position_ids, prompt_mask) outputs = self.model( input_ids=concat_inputs_ids, attention_mask=flex_attention_mask_3d, position_ids=concat_position_ids, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=True, cache_position=cache_position, **kwargs, ) hidden_states = outputs.last_hidden_state hidden_states = hidden_states[logits_to_keep].contiguous() answer_len = (labels != -100).sum() loss_fct = FusedLinearDiffusionCrossEntropyLoss(reduction='sum') loss = loss_fct( # it will return (sum_loss, unreduced_loss) # conduct `view(-1, V)` inside the function x=hidden_states, target=labels[logits_to_keep_half].contiguous(), weight=self.lm_head.weight, bias=self.lm_head.bias, p_mask=p_mask, ) loss = loss / answer_len logits = None else: # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn) outputs: BaseModelOutputWithPast = self.model( input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, token_labels=token_labels, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, cache_position=cache_position, **kwargs, )
hidden_states = outputs.last_hidden_state # Only compute necessary logits, and do not upcast them to float if we are not computing the loss slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep hidden_states = hidden_states[:, slice_indices, :].contiguous() fuse_linear_and_cross_entropy = self.config.fuse_cross_entropy and self.training if fuse_linear_and_cross_entropy: # When using fused_linear_ce_loss, we do not compute the whole logits on HBM logits = None else: logits = self.lm_head(hidden_states)
loss = None if labels is not None: # FusedLinearCrossEntropyLoss will be implemented by monkey patch when training # We don't use it when inferencing loss_fct = nn.CrossEntropyLoss() # nn.CE loss = loss_fct( logits.view(-1, self.config.vocab_size), labels.view(-1))
return CausalLMOutputWithPast( loss=loss, logits=logits, past_key_values=outputs.past_key_values, hidden_states=outputs.hidden_states, attentions=outputs.attentions, )
__all__ = [ "SDARForCausalLM", "SDARModel", "SDARPreTrainedModel",]123456789101112131415161718192021222324252627282930313233343536373839{ "additional_special_tokens": [ "<|im_start|>", "<|im_end|>", "<|object_ref_start|>", "<|object_ref_end|>", "<|box_start|>", "<|box_end|>", "<|quad_start|>", "<|quad_end|>", "<|vision_start|>", "<|vision_end|>", "<|vision_pad|>", "<|image_pad|>", "<|video_pad|>", "<|MASK|>" ], "eos_token": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "mask_token": { "content": "<|MASK|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342# coding=utf-8# Copyright 2024 The Qwen team, Alibaba Group and The HuggingFace Inc. team. All rights reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License."""Tokenization classes for Qwen2."""
import jsonimport osimport unicodedatafrom functools import lru_cachefrom typing import Optional, Tuple
import regex as re
from transformers.tokenization_utils import AddedToken, PreTrainedTokenizerfrom transformers.utils import logging
logger = logging.get_logger(__name__)
VOCAB_FILES_NAMES = { "vocab_file": "vocab.json", "merges_file": "merges.txt",}
MAX_MODEL_INPUT_SIZES = {"qwen/qwen-tokenizer": 32768}
PRETOKENIZE_REGEX = r"""(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"""
@lru_cache()# Copied from transformers.models.gpt2.tokenization_gpt2.bytes_to_unicodedef bytes_to_unicode(): """ Returns list of utf-8 byte and a mapping to unicode strings. We specifically avoids mapping to whitespace/control characters the bpe code barfs on.
The reversible bpe codes work on unicode strings. This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. This is a significant percentage of your normal, say, 32K bpe vocab. To avoid that, we want lookup tables between utf-8 bytes and unicode strings. """ bs = ( list(range(ord("!"), ord("~") + 1)) + list(range(ord("¡"), ord("¬") + 1)) + list(range(ord("®"), ord("ÿ") + 1)) ) cs = bs[:] n = 0 for b in range(2**8): if b not in bs: bs.append(b) cs.append(2**8 + n) n += 1 cs = [chr(n) for n in cs] return dict(zip(bs, cs))
# Copied from transformers.models.gpt2.tokenization_gpt2.get_pairsdef get_pairs(word): """ Return set of symbol pairs in a word.
Word is represented as tuple of symbols (symbols being variable-length strings). """ pairs = set() prev_char = word[0] for char in word[1:]: pairs.add((prev_char, char)) prev_char = char return pairs
class Qwen2Tokenizer(PreTrainedTokenizer): """ Construct a Qwen2 tokenizer. Based on byte-level Byte-Pair-Encoding.
Same with GPT2Tokenizer, this tokenizer has been trained to treat spaces like parts of the tokens so a word will be encoded differently whether it is at the beginning of the sentence (without space) or not:
```python >>> from transformers import Qwen2Tokenizer
>>> tokenizer = Qwen2Tokenizer.from_pretrained("Qwen/Qwen-tokenizer") >>> tokenizer("Hello world")["input_ids"] [9707, 1879]
>>> tokenizer(" Hello world")["input_ids"] [21927, 1879] ``` This is expected.
You should not use GPT2Tokenizer instead, because of the different pretokenization rules.
This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to this superclass for more information regarding those methods.
Args: vocab_file (`str`): Path to the vocabulary file. merges_file (`str`): Path to the merges file. errors (`str`, *optional*, defaults to `"replace"`): Paradigm to follow when decoding bytes to UTF-8. See [bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information. unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`): The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead. bos_token (`str`, *optional*): The beginning of sequence token. Not applicable for this tokenizer. eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`): The end of sequence token. pad_token (`str`, *optional*, defaults to `"<|endoftext|>"`): The token used for padding, for example when batching sequences of different lengths. clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`): Whether or not the model should cleanup the spaces that were added when splitting the input text during the tokenization process. Not applicable to this tokenizer, since tokenization does not add spaces. split_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the special tokens should be split during the tokenization process. The default behavior is to not split special tokens. This means that if `<|endoftext|>` is the `eos_token`, then `tokenizer.tokenize("<|endoftext|>") = ['<|endoftext|>`]. Otherwise, if `split_special_tokens=True`, then `tokenizer.tokenize("<|endoftext|>")` will be give `['<', '|', 'endo', 'ft', 'ext', '|', '>']`. This argument is only supported for `slow` tokenizers for the moment. """
vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"]
def __init__( self, vocab_file, merges_file, errors="replace", unk_token="<|endoftext|>", bos_token=None, eos_token="<|endoftext|>", pad_token="<|endoftext|>", clean_up_tokenization_spaces=False, split_special_tokens=False, **kwargs, ): # Qwen vocab does not contain control tokens; added tokens need to be special bos_token = ( AddedToken(bos_token, lstrip=False, rstrip=False, special=True, normalized=False) if isinstance(bos_token, str) else bos_token ) eos_token = ( AddedToken(eos_token, lstrip=False, rstrip=False, special=True, normalized=False) if isinstance(eos_token, str) else eos_token ) unk_token = ( AddedToken(unk_token, lstrip=False, rstrip=False, special=True, normalized=False) if isinstance(unk_token, str) else unk_token ) pad_token = ( AddedToken(pad_token, lstrip=False, rstrip=False, special=True, normalized=False) if isinstance(pad_token, str) else pad_token )
with open(vocab_file, encoding="utf-8") as vocab_handle: self.encoder = json.load(vocab_handle) self.decoder = {v: k for k, v in self.encoder.items()} self.errors = errors # how to handle errors in decoding self.byte_encoder = bytes_to_unicode() self.byte_decoder = {v: k for k, v in self.byte_encoder.items()} bpe_merges = [] with open(merges_file, encoding="utf-8") as merges_handle: for i, line in enumerate(merges_handle): line = line.strip() if (i == 0 and line.startswith("#version:")) or not line: continue bpe_merges.append(tuple(line.split())) self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges)))) # NOTE: the cache can grow without bound and will get really large for long running processes # (esp. for texts of language that do not use space between word, e.g. Chinese); technically # not a memory leak but appears as one. # GPT2Tokenizer has the same problem, so let's be consistent. self.cache = {}
self.pat = re.compile(PRETOKENIZE_REGEX)
if kwargs.get("add_prefix_space", False): logger.warning_once( f"{self.__class__.__name} does not support `add_prefix_space`, setting it to True has no effect." )
super().__init__( errors=errors, bos_token=bos_token, eos_token=eos_token, pad_token=pad_token, unk_token=unk_token, clean_up_tokenization_spaces=clean_up_tokenization_spaces, split_special_tokens=split_special_tokens, **kwargs, )
@property def vocab_size(self) -> int: return len(self.encoder)
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.get_vocab def get_vocab(self): return dict(self.encoder, **self.added_tokens_encoder)
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.bpe def bpe(self, token): if token in self.cache: return self.cache[token] word = tuple(token) pairs = get_pairs(word)
if not pairs: return token
while True: bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf"))) if bigram not in self.bpe_ranks: break first, second = bigram new_word = [] i = 0 while i < len(word): try: j = word.index(first, i) except ValueError: new_word.extend(word[i:]) break else: new_word.extend(word[i:j]) i = j
if word[i] == first and i < len(word) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 new_word = tuple(new_word) word = new_word if len(word) == 1: break else: pairs = get_pairs(word) word = " ".join(word) self.cache[token] = word return word
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer._tokenize def _tokenize(self, text): """Tokenize a string.""" bpe_tokens = [] for token in re.findall(self.pat, text): token = "".join( self.byte_encoder[b] for b in token.encode("utf-8") ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(token).split(" ")) return bpe_tokens
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer._convert_token_to_id def _convert_token_to_id(self, token): """Converts a token (str) in an id using the vocab.""" return self.encoder.get(token, self.encoder.get(self.unk_token))
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer._convert_id_to_token def _convert_id_to_token(self, index): """Converts an index (integer) in a token (str) using the vocab.""" return self.decoder.get(index)
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.convert_tokens_to_string def convert_tokens_to_string(self, tokens): """Converts a sequence of tokens (string) in a single string.""" text = "".join(tokens) text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text
def decode( self, token_ids, skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = False, spaces_between_special_tokens: bool = False, **kwargs, ) -> str: # `spaces_between_special_tokens` defaults to True for _decode in slow tokenizers # and cannot be configured elsewhere, but it should default to False for Qwen2Tokenizer return super().decode( token_ids, skip_special_tokens=skip_special_tokens, clean_up_tokenization_spaces=clean_up_tokenization_spaces, spaces_between_special_tokens=spaces_between_special_tokens, **kwargs, )
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.save_vocabulary def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return vocab_file = os.path.join( save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) merge_file = os.path.join( save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] )
with open(vocab_file, "w", encoding="utf-8") as f: f.write(json.dumps(self.encoder, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
index = 0 with open(merge_file, "w", encoding="utf-8") as writer: writer.write("#version: 0.2\n") for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]): if index != token_index: logger.warning( f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!" ) index = token_index writer.write(" ".join(bpe_tokens) + "\n") index += 1
return vocab_file, merge_file
def prepare_for_tokenization(self, text, **kwargs): text = unicodedata.normalize("NFC", text) return (text, kwargs)
__all__ = ["Qwen2Tokenizer"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256{ "add_bos_token": false, "add_prefix_space": false, "added_tokens_decoder": { "151643": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151644": { "content": "<|im_start|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151645": { "content": "<|im_end|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151646": { "content": "<|object_ref_start|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151647": { "content": "<|object_ref_end|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151648": { "content": "<|box_start|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151649": { "content": "<|box_end|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151650": { "content": "<|quad_start|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151651": { "content": "<|quad_end|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151652": { "content": "<|vision_start|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151653": { "content": "<|vision_end|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151654": { "content": "<|vision_pad|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151655": { "content": "<|image_pad|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151656": { "content": "<|video_pad|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "151657": { "content": "<tool_call>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151658": { "content": "</tool_call>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151659": { "content": "<|fim_prefix|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151660": { "content": "<|fim_middle|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151661": { "content": "<|fim_suffix|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151662": { "content": "<|fim_pad|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151663": { "content": "<|repo_name|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151664": { "content": "<|file_sep|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151665": { "content": "<tool_response>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151666": { "content": "</tool_response>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151667": { "content": "<think>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151668": { "content": "</think>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": false }, "151669": { "content": "<|MASK|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "additional_special_tokens": [ "<|im_start|>", "<|im_end|>", "<|object_ref_start|>", "<|object_ref_end|>", "<|box_start|>", "<|box_end|>", "<|quad_start|>", "<|quad_end|>", "<|vision_start|>", "<|vision_end|>", "<|vision_pad|>", "<|image_pad|>", "<|video_pad|>", "<|MASK|>" ], "auto_map": { "AutoTokenizer": [ "tokenization_qwen2.Qwen2Tokenizer", null ] }, "bos_token": null, "clean_up_tokenization_spaces": false, "eos_token": "<|endoftext|>", "errors": "replace", "extra_special_tokens": {}, "mask_token": "<|MASK|>", "model_max_length": 131072, "pad_token": "<|endoftext|>", "padding_side": "right", "split_special_tokens": false, "tokenizer_class": "Qwen2Tokenizer", "unk_token": null}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111{ "best_global_step": null, "best_metric": null, "best_model_checkpoint": null, "epoch": 1.0, "eval_steps": 500, "global_step": 57, "is_hyper_param_search": false, "is_local_process_zero": true, "is_world_process_zero": true, "log_history": [ { "epoch": 0.08771929824561403, "grad_norm": 9.328929901123047, "learning_rate": 9.986505025774137e-06, "loss": 1.0312, "step": 5 }, { "epoch": 0.17543859649122806, "grad_norm": 3.4733898639678955, "learning_rate": 9.931806517013612e-06, "loss": 0.194, "step": 10 }, { "epoch": 0.2631578947368421, "grad_norm": 2.149510383605957, "learning_rate": 9.835521911952554e-06, "loss": 0.1074, "step": 15 }, { "epoch": 0.3508771929824561, "grad_norm": 3.1549360752105713, "learning_rate": 9.698463103929542e-06, "loss": 0.1067, "step": 20 }, { "epoch": 0.43859649122807015, "grad_norm": 2.0819382667541504, "learning_rate": 9.521785803487888e-06, "loss": 0.0831, "step": 25 }, { "epoch": 0.5263157894736842, "grad_norm": 2.512042999267578, "learning_rate": 9.306979793165682e-06, "loss": 0.0777, "step": 30 }, { "epoch": 0.6140350877192983, "grad_norm": 1.5260273218154907, "learning_rate": 9.055856365316012e-06, "loss": 0.0689, "step": 35 }, { "epoch": 0.7017543859649122, "grad_norm": 1.9269893169403076, "learning_rate": 8.770533048884483e-06, "loss": 0.0631, "step": 40 }, { "epoch": 0.7894736842105263, "grad_norm": 1.1491817235946655, "learning_rate": 8.453415753931223e-06, "loss": 0.0516, "step": 45 }, { "epoch": 0.8771929824561403, "grad_norm": 1.4330883026123047, "learning_rate": 8.107178484458825e-06, "loss": 0.0599, "step": 50 }, { "epoch": 0.9649122807017544, "grad_norm": 1.450872778892517, "learning_rate": 7.734740790612137e-06, "loss": 0.0568, "step": 55 } ], "logging_steps": 5, "max_steps": 171, "num_input_tokens_seen": 0, "num_train_epochs": 3, "save_steps": 500, "stateful_callbacks": { "TrainerControl": { "args": { "should_epoch_stop": false, "should_evaluate": false, "should_log": false, "should_save": true, "should_training_stop": false }, "attributes": {} } }, "total_flos": 1.8031366314655744e+17, "train_batch_size": 16, "trial_name": null, "trial_params": null}123version https://git-lfs.github.com/spec/v1oid sha256:b0a1efc365e214608e030bd8db6c7bf512ddf4f78cf4d3d97efdf36f7182f86dsize 7889checkpoint-57/added_tokens.jsonAdded+29
checkpoint-57/chat_template.jinjaAdded+85
checkpoint-57/config.jsonAdded+43
checkpoint-57/configuration_sdar.pyAdded+212
checkpoint-57/fused_linear_diffusion_cross_entropy.pyAdded+682
checkpoint-57/generation_config.jsonAdded+13
checkpoint-57/merges.txtAdded
checkpoint-57/model-00001-of-00002.safetensorsAdded+3
checkpoint-57/model-00002-of-00002.safetensorsAdded+3
checkpoint-57/model.safetensors.index.jsonAdded+406
checkpoint-57/modeling_sdar.pyAdded+1542
checkpoint-57/special_tokens_map.jsonAdded+39
checkpoint-57/tokenization_qwen2.pyAdded+342
checkpoint-57/tokenizer_config.jsonAdded+256
checkpoint-57/trainer_state.jsonAdded+111
checkpoint-57/training_args.binAdded+3
checkpoint-57/vocab.jsonAdded