Upload model
Browse files- README.md +199 -0
- config.json +3 -0
- dflash.py +188 -0
- model.safetensors +2 -2
README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags: []
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Model Card for Model ID
|
| 7 |
+
|
| 8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## Model Details
|
| 13 |
+
|
| 14 |
+
### Model Description
|
| 15 |
+
|
| 16 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
+
|
| 18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
+
|
| 20 |
+
- **Developed by:** [More Information Needed]
|
| 21 |
+
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
+
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
+
- **Model type:** [More Information Needed]
|
| 24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
+
- **License:** [More Information Needed]
|
| 26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
+
|
| 28 |
+
### Model Sources [optional]
|
| 29 |
+
|
| 30 |
+
<!-- Provide the basic links for the model. -->
|
| 31 |
+
|
| 32 |
+
- **Repository:** [More Information Needed]
|
| 33 |
+
- **Paper [optional]:** [More Information Needed]
|
| 34 |
+
- **Demo [optional]:** [More Information Needed]
|
| 35 |
+
|
| 36 |
+
## Uses
|
| 37 |
+
|
| 38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
+
|
| 40 |
+
### Direct Use
|
| 41 |
+
|
| 42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
+
|
| 44 |
+
[More Information Needed]
|
| 45 |
+
|
| 46 |
+
### Downstream Use [optional]
|
| 47 |
+
|
| 48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
+
|
| 50 |
+
[More Information Needed]
|
| 51 |
+
|
| 52 |
+
### Out-of-Scope Use
|
| 53 |
+
|
| 54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
+
|
| 56 |
+
[More Information Needed]
|
| 57 |
+
|
| 58 |
+
## Bias, Risks, and Limitations
|
| 59 |
+
|
| 60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
+
|
| 62 |
+
[More Information Needed]
|
| 63 |
+
|
| 64 |
+
### Recommendations
|
| 65 |
+
|
| 66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
+
|
| 68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
+
|
| 70 |
+
## How to Get Started with the Model
|
| 71 |
+
|
| 72 |
+
Use the code below to get started with the model.
|
| 73 |
+
|
| 74 |
+
[More Information Needed]
|
| 75 |
+
|
| 76 |
+
## Training Details
|
| 77 |
+
|
| 78 |
+
### Training Data
|
| 79 |
+
|
| 80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
+
|
| 82 |
+
[More Information Needed]
|
| 83 |
+
|
| 84 |
+
### Training Procedure
|
| 85 |
+
|
| 86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
+
|
| 88 |
+
#### Preprocessing [optional]
|
| 89 |
+
|
| 90 |
+
[More Information Needed]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
#### Training Hyperparameters
|
| 94 |
+
|
| 95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
+
|
| 97 |
+
#### Speeds, Sizes, Times [optional]
|
| 98 |
+
|
| 99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
+
|
| 101 |
+
[More Information Needed]
|
| 102 |
+
|
| 103 |
+
## Evaluation
|
| 104 |
+
|
| 105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
+
|
| 107 |
+
### Testing Data, Factors & Metrics
|
| 108 |
+
|
| 109 |
+
#### Testing Data
|
| 110 |
+
|
| 111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
+
|
| 113 |
+
[More Information Needed]
|
| 114 |
+
|
| 115 |
+
#### Factors
|
| 116 |
+
|
| 117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
+
|
| 119 |
+
[More Information Needed]
|
| 120 |
+
|
| 121 |
+
#### Metrics
|
| 122 |
+
|
| 123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
+
|
| 125 |
+
[More Information Needed]
|
| 126 |
+
|
| 127 |
+
### Results
|
| 128 |
+
|
| 129 |
+
[More Information Needed]
|
| 130 |
+
|
| 131 |
+
#### Summary
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
## Model Examination [optional]
|
| 136 |
+
|
| 137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
+
|
| 139 |
+
[More Information Needed]
|
| 140 |
+
|
| 141 |
+
## Environmental Impact
|
| 142 |
+
|
| 143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
+
|
| 145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
+
|
| 147 |
+
- **Hardware Type:** [More Information Needed]
|
| 148 |
+
- **Hours used:** [More Information Needed]
|
| 149 |
+
- **Cloud Provider:** [More Information Needed]
|
| 150 |
+
- **Compute Region:** [More Information Needed]
|
| 151 |
+
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
+
|
| 153 |
+
## Technical Specifications [optional]
|
| 154 |
+
|
| 155 |
+
### Model Architecture and Objective
|
| 156 |
+
|
| 157 |
+
[More Information Needed]
|
| 158 |
+
|
| 159 |
+
### Compute Infrastructure
|
| 160 |
+
|
| 161 |
+
[More Information Needed]
|
| 162 |
+
|
| 163 |
+
#### Hardware
|
| 164 |
+
|
| 165 |
+
[More Information Needed]
|
| 166 |
+
|
| 167 |
+
#### Software
|
| 168 |
+
|
| 169 |
+
[More Information Needed]
|
| 170 |
+
|
| 171 |
+
## Citation [optional]
|
| 172 |
+
|
| 173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
+
|
| 175 |
+
**BibTeX:**
|
| 176 |
+
|
| 177 |
+
[More Information Needed]
|
| 178 |
+
|
| 179 |
+
**APA:**
|
| 180 |
+
|
| 181 |
+
[More Information Needed]
|
| 182 |
+
|
| 183 |
+
## Glossary [optional]
|
| 184 |
+
|
| 185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
+
|
| 187 |
+
[More Information Needed]
|
| 188 |
+
|
| 189 |
+
## More Information [optional]
|
| 190 |
+
|
| 191 |
+
[More Information Needed]
|
| 192 |
+
|
| 193 |
+
## Model Card Authors [optional]
|
| 194 |
+
|
| 195 |
+
[More Information Needed]
|
| 196 |
+
|
| 197 |
+
## Model Card Contact
|
| 198 |
+
|
| 199 |
+
[More Information Needed]
|
config.json
CHANGED
|
@@ -4,6 +4,9 @@
|
|
| 4 |
],
|
| 5 |
"attention_bias": false,
|
| 6 |
"attention_dropout": 0.0,
|
|
|
|
|
|
|
|
|
|
| 7 |
"block_size": 16,
|
| 8 |
"dflash_config": {
|
| 9 |
"mask_token_id": 151669,
|
|
|
|
| 4 |
],
|
| 5 |
"attention_bias": false,
|
| 6 |
"attention_dropout": 0.0,
|
| 7 |
+
"auto_map": {
|
| 8 |
+
"AutoModel": "dflash.DFlashDraftModel"
|
| 9 |
+
},
|
| 10 |
"block_size": 16,
|
| 11 |
"dflash_config": {
|
| 12 |
"mask_token_id": 151669,
|
dflash.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional, Callable
|
| 2 |
+
from typing_extensions import Unpack, Tuple
|
| 3 |
+
import torch
|
| 4 |
+
from torch import nn
|
| 5 |
+
from transformers.models.qwen3.modeling_qwen3 import (
|
| 6 |
+
Qwen3RMSNorm,
|
| 7 |
+
Qwen3RotaryEmbedding,
|
| 8 |
+
Qwen3Config,
|
| 9 |
+
Qwen3PreTrainedModel,
|
| 10 |
+
Qwen3MLP,
|
| 11 |
+
GradientCheckpointingLayer,
|
| 12 |
+
FlashAttentionKwargs,
|
| 13 |
+
rotate_half,
|
| 14 |
+
eager_attention_forward,
|
| 15 |
+
ALL_ATTENTION_FUNCTIONS,
|
| 16 |
+
)
|
| 17 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 18 |
+
from transformers.cache_utils import Cache
|
| 19 |
+
|
| 20 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
|
| 21 |
+
cos = cos.unsqueeze(unsqueeze_dim)
|
| 22 |
+
sin = sin.unsqueeze(unsqueeze_dim)
|
| 23 |
+
q_len = q.size(-2)
|
| 24 |
+
q_embed = (q * cos[..., -q_len:, :]) + (rotate_half(q) * sin[..., -q_len:, :])
|
| 25 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
| 26 |
+
return q_embed, k_embed
|
| 27 |
+
|
| 28 |
+
class Qwen3DFlashAttention(nn.Module):
|
| 29 |
+
"""Multi-headed attention from 'Attention Is All You Need' paper"""
|
| 30 |
+
|
| 31 |
+
def __init__(self, config: Qwen3Config, layer_idx: int):
|
| 32 |
+
super().__init__()
|
| 33 |
+
self.config = config
|
| 34 |
+
self.layer_idx = layer_idx
|
| 35 |
+
self.head_dim = getattr(config, "head_dim", config.hidden_size // config.num_attention_heads)
|
| 36 |
+
self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
|
| 37 |
+
self.scaling = self.head_dim**-0.5
|
| 38 |
+
self.attention_dropout = config.attention_dropout
|
| 39 |
+
self.is_causal = False
|
| 40 |
+
self.q_proj = nn.Linear(
|
| 41 |
+
config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.attention_bias
|
| 42 |
+
)
|
| 43 |
+
self.k_proj = nn.Linear(
|
| 44 |
+
config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
|
| 45 |
+
)
|
| 46 |
+
self.v_proj = nn.Linear(
|
| 47 |
+
config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
|
| 48 |
+
)
|
| 49 |
+
self.o_proj = nn.Linear(
|
| 50 |
+
config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias
|
| 51 |
+
)
|
| 52 |
+
self.q_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
| 53 |
+
self.k_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
| 54 |
+
self.sliding_window = config.sliding_window if config.layer_types[layer_idx] == "sliding_attention" else None
|
| 55 |
+
|
| 56 |
+
def forward(
|
| 57 |
+
self,
|
| 58 |
+
hidden_states: torch.Tensor,
|
| 59 |
+
target_hidden: torch.Tensor,
|
| 60 |
+
position_embeddings: tuple[torch.Tensor, torch.Tensor],
|
| 61 |
+
attention_mask: Optional[torch.Tensor],
|
| 62 |
+
past_key_values: Optional[Cache] = None,
|
| 63 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 64 |
+
**kwargs: Unpack[FlashAttentionKwargs],
|
| 65 |
+
) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
|
| 66 |
+
bsz, q_len = hidden_states.shape[:-1]
|
| 67 |
+
ctx_len = target_hidden.shape[1]
|
| 68 |
+
q = self.q_proj(hidden_states)
|
| 69 |
+
q = q.view(bsz, q_len, -1, self.head_dim)
|
| 70 |
+
q = self.q_norm(q).transpose(1, 2)
|
| 71 |
+
k_ctx = self.k_proj(target_hidden)
|
| 72 |
+
k_noise = self.k_proj(hidden_states)
|
| 73 |
+
v_ctx = self.v_proj(target_hidden)
|
| 74 |
+
v_noise = self.v_proj(hidden_states)
|
| 75 |
+
k = torch.cat([k_ctx, k_noise], dim=1).view(bsz, ctx_len + q_len, -1, self.head_dim)
|
| 76 |
+
v = torch.cat([v_ctx, v_noise], dim=1).view(bsz, ctx_len + q_len, -1, self.head_dim)
|
| 77 |
+
k = self.k_norm(k).transpose(1, 2)
|
| 78 |
+
v = v.transpose(1, 2)
|
| 79 |
+
cos, sin = position_embeddings
|
| 80 |
+
q, k = apply_rotary_pos_emb(q, k, cos, sin)
|
| 81 |
+
if past_key_values is not None:
|
| 82 |
+
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
|
| 83 |
+
k, v = past_key_values.update(k, v, self.layer_idx, cache_kwargs)
|
| 84 |
+
attn_fn: Callable = eager_attention_forward
|
| 85 |
+
if self.config._attn_implementation != "eager":
|
| 86 |
+
attn_fn = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
|
| 87 |
+
attn_output, attn_weights = attn_fn(
|
| 88 |
+
self,
|
| 89 |
+
q,
|
| 90 |
+
k,
|
| 91 |
+
v,
|
| 92 |
+
attention_mask,
|
| 93 |
+
dropout=0.0 if not self.training else self.attention_dropout,
|
| 94 |
+
scaling=self.scaling,
|
| 95 |
+
sliding_window=self.sliding_window,
|
| 96 |
+
**kwargs,
|
| 97 |
+
)
|
| 98 |
+
attn_output = attn_output.reshape(bsz, q_len, -1)
|
| 99 |
+
attn_output = self.o_proj(attn_output)
|
| 100 |
+
return attn_output, attn_weights
|
| 101 |
+
|
| 102 |
+
class Qwen3DFlashDecoderLayer(GradientCheckpointingLayer):
|
| 103 |
+
def __init__(self, config: Qwen3Config, layer_idx: int):
|
| 104 |
+
super().__init__()
|
| 105 |
+
self.hidden_size = config.hidden_size
|
| 106 |
+
self.self_attn = Qwen3DFlashAttention(config=config, layer_idx=layer_idx)
|
| 107 |
+
self.mlp = Qwen3MLP(config)
|
| 108 |
+
self.input_layernorm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 109 |
+
self.post_attention_layernorm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 110 |
+
|
| 111 |
+
def forward(
|
| 112 |
+
self,
|
| 113 |
+
target_hidden: Optional[torch.Tensor] = None,
|
| 114 |
+
hidden_states: Optional[torch.Tensor] = None,
|
| 115 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 116 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 117 |
+
past_key_value: Optional[Cache] = None,
|
| 118 |
+
output_attentions: Optional[bool] = False,
|
| 119 |
+
use_cache: Optional[bool] = False,
|
| 120 |
+
cache_position: Optional[torch.LongTensor] = None,
|
| 121 |
+
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
|
| 122 |
+
**kwargs: Unpack[FlashAttentionKwargs],
|
| 123 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
| 124 |
+
residual = hidden_states
|
| 125 |
+
hidden_states = self.input_layernorm(hidden_states)
|
| 126 |
+
hidden_states = self.self_attn(
|
| 127 |
+
hidden_states=hidden_states,
|
| 128 |
+
target_hidden=target_hidden,
|
| 129 |
+
attention_mask=attention_mask,
|
| 130 |
+
position_ids=position_ids,
|
| 131 |
+
past_key_values=past_key_value,
|
| 132 |
+
output_attentions=output_attentions,
|
| 133 |
+
use_cache=use_cache,
|
| 134 |
+
cache_position=cache_position,
|
| 135 |
+
position_embeddings=position_embeddings,
|
| 136 |
+
**kwargs,
|
| 137 |
+
)[0]
|
| 138 |
+
hidden_states = residual + hidden_states
|
| 139 |
+
residual = hidden_states
|
| 140 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
| 141 |
+
hidden_states = self.mlp(hidden_states)
|
| 142 |
+
hidden_states = residual + hidden_states
|
| 143 |
+
return hidden_states
|
| 144 |
+
|
| 145 |
+
class DFlashDraftModel(Qwen3PreTrainedModel):
|
| 146 |
+
config_class = Qwen3Config
|
| 147 |
+
_no_split_modules = ["Qwen3DFlashDecoderLayer"]
|
| 148 |
+
|
| 149 |
+
def __init__(self, config) -> None:
|
| 150 |
+
super().__init__(config)
|
| 151 |
+
self.config = config
|
| 152 |
+
self.layers = nn.ModuleList(
|
| 153 |
+
[Qwen3DFlashDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
| 154 |
+
)
|
| 155 |
+
self.target_layer_ids = self.config.dflash_config.get("target_layer_ids", None)
|
| 156 |
+
self.norm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 157 |
+
self.rotary_emb = Qwen3RotaryEmbedding(config)
|
| 158 |
+
self.fc = nn.Linear(len(self.target_layer_ids) * config.hidden_size, config.hidden_size, bias=False)
|
| 159 |
+
self.hidden_norm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 160 |
+
self.block_size = config.block_size
|
| 161 |
+
self.mask_token_id = self.config.dflash_config.get("mask_token_id", None)
|
| 162 |
+
self.post_init()
|
| 163 |
+
|
| 164 |
+
def forward(
|
| 165 |
+
self,
|
| 166 |
+
position_ids: torch.LongTensor,
|
| 167 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 168 |
+
noise_embedding: Optional[torch.Tensor] = None,
|
| 169 |
+
target_hidden: Optional[torch.Tensor] = None,
|
| 170 |
+
past_key_values: Optional[Cache] = None,
|
| 171 |
+
use_cache: bool = False,
|
| 172 |
+
**kwargs,
|
| 173 |
+
) -> CausalLMOutputWithPast:
|
| 174 |
+
hidden_states = noise_embedding
|
| 175 |
+
target_hidden = self.hidden_norm(self.fc(target_hidden))
|
| 176 |
+
position_embeddings = self.rotary_emb(hidden_states, position_ids)
|
| 177 |
+
for layer in self.layers:
|
| 178 |
+
hidden_states = layer(
|
| 179 |
+
hidden_states=hidden_states,
|
| 180 |
+
target_hidden=target_hidden,
|
| 181 |
+
attention_mask=attention_mask,
|
| 182 |
+
position_ids=position_ids,
|
| 183 |
+
past_key_value=past_key_values,
|
| 184 |
+
use_cache=use_cache,
|
| 185 |
+
position_embeddings=position_embeddings,
|
| 186 |
+
**kwargs,
|
| 187 |
+
)
|
| 188 |
+
return self.norm(hidden_states)
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3daba9d790c38c2141376043dd8eff69b3699ea166a5c1003049aed2feb403ef
|
| 3 |
+
size 948000184
|