Buckets:
| import logging | |
| from typing import Callable, Optional | |
| import torch | |
| from torch import nn | |
| from transformers import DynamicCache | |
| from transformers.cache_utils import Cache | |
| from transformers.modeling_outputs import CausalLMOutputWithPast | |
| from transformers.models.qwen3.modeling_qwen3 import ( | |
| ALL_ATTENTION_FUNCTIONS, | |
| FlashAttentionKwargs, | |
| GradientCheckpointingLayer, | |
| Qwen3Config, | |
| Qwen3MLP, | |
| Qwen3PreTrainedModel, | |
| Qwen3RMSNorm, | |
| Qwen3RotaryEmbedding, | |
| eager_attention_forward, | |
| rotate_half, | |
| ) | |
| from typing_extensions import Tuple, Unpack | |
| logger = logging.getLogger(__name__) | |
| _token_top_inactive_logged = False | |
| def _log_token_top_inactive_once() -> None: | |
| global _token_top_inactive_logged | |
| if _token_top_inactive_logged: | |
| return | |
| logger.warning( | |
| "token_top_layer present but INACTIVE this forward (no prev-token input) " | |
| "— mask-mode/parallel semantics" | |
| ) | |
| _token_top_inactive_logged = True | |
| def sample(logits: torch.Tensor, temperature: float = 0.0) -> torch.Tensor: | |
| if temperature < 1e-5: | |
| return torch.argmax(logits, dim=-1) | |
| bsz, seq_len, vocab_size = logits.shape | |
| logits = logits.view(-1, vocab_size) | |
| logits = logits / temperature | |
| probs = torch.softmax(logits, dim=-1) | |
| return torch.multinomial(probs, num_samples=1).view(bsz, seq_len) | |
| def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): | |
| cos = cos.unsqueeze(unsqueeze_dim) | |
| sin = sin.unsqueeze(unsqueeze_dim) | |
| q_len = q.size(-2) | |
| q_embed = (q * cos[..., -q_len:, :]) + (rotate_half(q) * sin[..., -q_len:, :]) | |
| k_embed = (k * cos) + (rotate_half(k) * sin) | |
| return q_embed, k_embed | |
| def _to_additive_attention_mask( | |
| attention_mask: torch.Tensor, | |
| *, | |
| query_dtype: torch.dtype, | |
| device: torch.device, | |
| key_len: int, | |
| ) -> torch.Tensor: | |
| if attention_mask.ndim == 4: | |
| attention_mask = attention_mask[:, :, :, :key_len] | |
| if attention_mask.dtype == torch.bool: | |
| additive_mask = torch.zeros_like(attention_mask, dtype=query_dtype, device=device) | |
| return additive_mask.masked_fill( | |
| attention_mask.logical_not().to(device=device), | |
| torch.finfo(query_dtype).min, | |
| ) | |
| return attention_mask.to(device=device, dtype=query_dtype) | |
| def _build_dflash_causal_attention_mask( | |
| *, | |
| query: torch.Tensor, | |
| key: torch.Tensor, | |
| cached_kv_len: int, | |
| ctx_len: int, | |
| ) -> torch.Tensor: | |
| q_len = query.shape[-2] | |
| kv_len = key.shape[-2] | |
| key_positions = torch.arange(kv_len, device=query.device) | |
| query_positions = cached_kv_len + ctx_len + torch.arange(q_len, device=query.device) | |
| can_attend = key_positions.unsqueeze(0) <= query_positions.unsqueeze(1) | |
| mask = torch.zeros((1, 1, q_len, kv_len), dtype=query.dtype, device=query.device) | |
| return mask.masked_fill(can_attend.logical_not().unsqueeze(0).unsqueeze(0), torch.finfo(query.dtype).min) | |
| def build_token_top_causal_attention_mask( | |
| block_size: int, | |
| *, | |
| dtype: torch.dtype, | |
| device: torch.device, | |
| ) -> torch.Tensor: | |
| query_positions = torch.arange(block_size, device=device).view(block_size, 1) | |
| key_positions = torch.arange(block_size, device=device).view(1, block_size) | |
| can_attend = key_positions <= query_positions | |
| mask = torch.zeros((1, 1, block_size, block_size), dtype=dtype, device=device) | |
| return mask.masked_fill(can_attend.logical_not().unsqueeze(0).unsqueeze(0), torch.finfo(dtype).min) | |
| class Qwen3DFlashAttention(nn.Module): | |
| """Multi-headed attention from 'Attention Is All You Need' paper""" | |
| def __init__(self, config: Qwen3Config, 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 | |
| dflash_config = getattr(config, "dflash_config", {}) or {} | |
| self.is_causal = bool(dflash_config.get("causal_head", False)) | |
| 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, | |
| ) | |
| self.q_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps) | |
| self.k_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps) | |
| self.sliding_window = ( | |
| config.sliding_window | |
| if config.layer_types[layer_idx] == "sliding_attention" | |
| else None | |
| ) | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| target_hidden: torch.Tensor, | |
| position_embeddings: tuple[torch.Tensor, torch.Tensor], | |
| attention_mask: Optional[torch.Tensor], | |
| past_key_values: Optional[Cache] = None, | |
| cache_position: Optional[torch.LongTensor] = None, | |
| **kwargs: Unpack[FlashAttentionKwargs], | |
| ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: | |
| bsz, q_len = hidden_states.shape[:-1] | |
| ctx_len = target_hidden.shape[1] | |
| is_causal = kwargs.pop("is_causal", None) | |
| if is_causal is None: | |
| is_causal = self.is_causal | |
| q = self.q_proj(hidden_states) | |
| q = q.view(bsz, q_len, -1, self.head_dim) | |
| q = self.q_norm(q).transpose(1, 2) | |
| k_ctx = self.k_proj(target_hidden) | |
| k_noise = self.k_proj(hidden_states) | |
| v_ctx = self.v_proj(target_hidden) | |
| v_noise = self.v_proj(hidden_states) | |
| k = torch.cat([k_ctx, k_noise], dim=1).view( | |
| bsz, ctx_len + q_len, -1, self.head_dim | |
| ) | |
| v = torch.cat([v_ctx, v_noise], dim=1).view( | |
| bsz, ctx_len + q_len, -1, self.head_dim | |
| ) | |
| k = self.k_norm(k).transpose(1, 2) | |
| v = v.transpose(1, 2) | |
| cos, sin = position_embeddings | |
| q, k = apply_rotary_pos_emb(q, k, cos, sin) | |
| cached_kv_len = past_key_values.get_seq_length() if past_key_values is not None else 0 | |
| if past_key_values is not None: | |
| cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} | |
| k, v = past_key_values.update(k, v, self.layer_idx, cache_kwargs) | |
| attn_backend = self.config._attn_implementation | |
| use_explicit_dflash_causal_mask = bool(is_causal) and attn_backend in {"eager", "sdpa"} | |
| if use_explicit_dflash_causal_mask: | |
| dflash_causal_mask = _build_dflash_causal_attention_mask( | |
| query=q, key=k, cached_kv_len=cached_kv_len, ctx_len=ctx_len, | |
| ) | |
| if attention_mask is not None: | |
| dflash_causal_mask = dflash_causal_mask + _to_additive_attention_mask( | |
| attention_mask, query_dtype=q.dtype, device=q.device, key_len=k.shape[-2], | |
| ) | |
| attention_mask = dflash_causal_mask | |
| is_causal = False | |
| kwargs["is_causal"] = is_causal | |
| attn_fn: Callable = eager_attention_forward | |
| if self.config._attn_implementation != "eager": | |
| attn_fn = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation] | |
| attn_output, attn_weights = attn_fn( | |
| self, | |
| q, | |
| k, | |
| v, | |
| attention_mask, | |
| dropout=0.0 if not self.training else self.attention_dropout, | |
| scaling=self.scaling, | |
| sliding_window=self.sliding_window, | |
| **kwargs, | |
| ) | |
| attn_output = attn_output.reshape(bsz, q_len, -1) | |
| attn_output = self.o_proj(attn_output) | |
| return attn_output, attn_weights | |
| class Qwen3TokenTopAttention(nn.Module): | |
| """Block-local causal self-attention for the token-fed top layer.""" | |
| def __init__(self, config: Qwen3Config): | |
| super().__init__() | |
| self.config = config | |
| 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.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, | |
| ) | |
| self.q_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps) | |
| self.k_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps) | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| position_embeddings: tuple[torch.Tensor, torch.Tensor], | |
| attention_mask: torch.Tensor, | |
| ) -> torch.Tensor: | |
| bsz, q_len = hidden_states.shape[:-1] | |
| q = self.q_proj(hidden_states) | |
| k = self.k_proj(hidden_states) | |
| v = self.v_proj(hidden_states) | |
| q = q.view(bsz, q_len, -1, self.head_dim) | |
| k = k.view(bsz, q_len, -1, self.head_dim) | |
| v = v.view(bsz, q_len, -1, self.head_dim) | |
| q = self.q_norm(q).transpose(1, 2) | |
| k = self.k_norm(k).transpose(1, 2) | |
| v = v.transpose(1, 2) | |
| cos, sin = position_embeddings | |
| q, k = apply_rotary_pos_emb(q, k, cos, sin) | |
| attn_output, _ = eager_attention_forward( | |
| self, | |
| q, | |
| k, | |
| v, | |
| attention_mask, | |
| dropout=0.0 if not self.training else self.attention_dropout, | |
| scaling=self.scaling, | |
| ) | |
| attn_output = attn_output.reshape(bsz, q_len, -1) | |
| return attn_output | |
| class TokenTopLayer(nn.Module): | |
| """Token-fed block-local adapter that adds a zero-init delta to DFlash hidden states.""" | |
| def __init__(self, config: Qwen3Config, token_top_config: dict): | |
| super().__init__() | |
| self.hidden_size = int(config.hidden_size) | |
| self.block_size = int(config.block_size) | |
| self.width = int(token_top_config.get("width", self.hidden_size)) | |
| self.heads = int(token_top_config.get("heads", config.num_attention_heads)) | |
| self.init = str(token_top_config.get("init", "zero_output")) | |
| if self.width != self.hidden_size: | |
| raise ValueError( | |
| f"token_top_layer.width must match hidden_size={self.hidden_size}, got {self.width}." | |
| ) | |
| if self.heads != int(config.num_attention_heads): | |
| raise ValueError( | |
| f"token_top_layer.heads must match num_attention_heads={config.num_attention_heads}, got {self.heads}." | |
| ) | |
| self.fusion = nn.Linear(2 * self.hidden_size, self.hidden_size) | |
| self.input_layernorm = Qwen3RMSNorm(self.hidden_size, eps=config.rms_norm_eps) | |
| self.self_attn = Qwen3TokenTopAttention(config) | |
| self.output_norm = Qwen3RMSNorm(self.hidden_size, eps=config.rms_norm_eps) | |
| self.rotary_emb = Qwen3RotaryEmbedding(config) | |
| def maybe_zero_init_output(self) -> None: | |
| if self.init in ("zero_output", "zero"): | |
| nn.init.zeros_(self.self_attn.o_proj.weight) | |
| if self.self_attn.o_proj.bias is not None: | |
| nn.init.zeros_(self.self_attn.o_proj.bias) | |
| return | |
| if self.init != "default": | |
| raise ValueError( | |
| f"Unsupported token_top_layer.init={self.init!r}. Expected 'zero_output' or 'default'." | |
| ) | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| prev_token_embedding: torch.Tensor, | |
| position_ids: torch.LongTensor, | |
| block_keep_mask: torch.Tensor, | |
| ) -> torch.Tensor: | |
| bsz, flat_slots, hidden_size = hidden_states.shape | |
| if hidden_size != self.hidden_size: | |
| raise ValueError( | |
| f"Expected hidden size {self.hidden_size}, got {hidden_size}." | |
| ) | |
| if flat_slots % self.block_size != 0: | |
| raise ValueError( | |
| f"Expected flat_slots divisible by block_size={self.block_size}, got {flat_slots}." | |
| ) | |
| n_blocks = flat_slots // self.block_size | |
| fused = self.fusion(torch.cat([hidden_states, prev_token_embedding], dim=-1)) | |
| block_hidden = fused.reshape(bsz * n_blocks, self.block_size, self.hidden_size) | |
| block_positions = position_ids.reshape(bsz * n_blocks, self.block_size) | |
| position_embeddings = self.rotary_emb(block_hidden, block_positions) | |
| attention_mask = build_token_top_causal_attention_mask( | |
| self.block_size, | |
| dtype=block_hidden.dtype, | |
| device=block_hidden.device, | |
| ) | |
| attn_input = self.input_layernorm(block_hidden) | |
| attn_output = self.self_attn( | |
| hidden_states=attn_input, | |
| position_embeddings=position_embeddings, | |
| attention_mask=attention_mask, | |
| ) | |
| delta = self.self_attn.o_proj(self.output_norm(attn_output)) | |
| delta = delta.reshape(bsz, n_blocks, self.block_size, self.hidden_size) | |
| delta = delta * block_keep_mask.reshape(bsz, n_blocks, 1, 1).to(dtype=delta.dtype) | |
| return hidden_states + delta.reshape(bsz, flat_slots, self.hidden_size) | |
| class Qwen3DFlashDecoderLayer(GradientCheckpointingLayer): | |
| def __init__(self, config: Qwen3Config, layer_idx: int): | |
| super().__init__() | |
| self.hidden_size = config.hidden_size | |
| self.self_attn = Qwen3DFlashAttention(config=config, layer_idx=layer_idx) | |
| self.mlp = Qwen3MLP(config) | |
| self.input_layernorm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps) | |
| self.post_attention_layernorm = Qwen3RMSNorm( | |
| config.hidden_size, eps=config.rms_norm_eps | |
| ) | |
| def forward( | |
| self, | |
| target_hidden: Optional[torch.Tensor] = None, | |
| hidden_states: Optional[torch.Tensor] = None, | |
| 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, | |
| cache_position: Optional[torch.LongTensor] = None, | |
| position_embeddings: Optional[ | |
| Tuple[torch.Tensor, torch.Tensor] | |
| ] = None, # necessary, but kept here for BC | |
| **kwargs: Unpack[FlashAttentionKwargs], | |
| ) -> Tuple[ | |
| torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]] | |
| ]: | |
| residual = hidden_states | |
| hidden_states = self.input_layernorm(hidden_states) | |
| hidden_states = self.self_attn( | |
| hidden_states=hidden_states, | |
| target_hidden=target_hidden, | |
| attention_mask=attention_mask, | |
| position_ids=position_ids, | |
| past_key_values=past_key_value, | |
| output_attentions=output_attentions, | |
| use_cache=use_cache, | |
| cache_position=cache_position, | |
| position_embeddings=position_embeddings, | |
| **kwargs, | |
| )[0] | |
| hidden_states = residual + hidden_states | |
| residual = hidden_states | |
| hidden_states = self.post_attention_layernorm(hidden_states) | |
| hidden_states = self.mlp(hidden_states) | |
| hidden_states = residual + hidden_states | |
| return hidden_states | |
| def build_target_layer_ids(num_target_layers: int, num_draft_layers: int): | |
| if num_draft_layers == 1: | |
| return [(num_target_layers // 2)] | |
| start = 1 | |
| end = num_target_layers - 3 | |
| span = end - start | |
| target_layer_ids = [ | |
| int(round(start + (i * span) / (num_draft_layers - 1))) | |
| for i in range(num_draft_layers) | |
| ] | |
| return target_layer_ids | |
| def extract_context_feature( | |
| hidden_states: list[torch.Tensor], | |
| layer_ids: Optional[list[int]], | |
| ) -> torch.Tensor: | |
| offset = 1 | |
| selected_states = [] | |
| for layer_id in layer_ids: | |
| selected_states.append(hidden_states[layer_id + offset]) | |
| target_hidden = torch.cat(selected_states, dim=-1) | |
| return target_hidden | |
| class DFlashDraftModel(Qwen3PreTrainedModel): | |
| config_class = Qwen3Config | |
| _no_split_modules = ["Qwen3DFlashDecoderLayer", "TokenTopLayer"] | |
| def __init__(self, config) -> None: | |
| super().__init__(config) | |
| self.config = config | |
| if not hasattr(self.config, "dflash_config") or self.config.dflash_config is None: | |
| self.config.dflash_config = {} | |
| self.causal_head = bool(self.config.dflash_config.get("causal_head", False)) | |
| self.layers = nn.ModuleList( | |
| [ | |
| Qwen3DFlashDecoderLayer(config, layer_idx) | |
| for layer_idx in range(config.num_hidden_layers) | |
| ] | |
| ) | |
| dflash_config = getattr(config, "dflash_config", {}) or {} | |
| self.target_layer_ids = dflash_config.get( | |
| "target_layer_ids", | |
| build_target_layer_ids(config.num_target_layers, config.num_hidden_layers), | |
| ) | |
| self.norm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps) | |
| self.rotary_emb = Qwen3RotaryEmbedding(config) | |
| target_hidden_size = getattr(config, "target_hidden_size", config.hidden_size) | |
| self.fc = nn.Linear( | |
| len(self.target_layer_ids) * target_hidden_size, | |
| config.hidden_size, | |
| bias=False, | |
| ) | |
| self.hidden_norm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps) | |
| self.block_size = config.block_size | |
| self.mask_token_id = dflash_config.get("mask_token_id", None) | |
| self.token_top_config = self._normalize_token_top_config(config) | |
| self.token_top_layer = ( | |
| TokenTopLayer(config, self.token_top_config) | |
| if self.token_top_config.get("enabled", False) | |
| else None | |
| ) | |
| self.post_init() | |
| if self.token_top_layer is not None: | |
| self.token_top_layer.maybe_zero_init_output() | |
| def _normalize_token_top_config(config) -> dict: | |
| dflash_config = getattr(config, "dflash_config", None) or {} | |
| raw_config = getattr(config, "token_top_layer", None) | |
| if raw_config is None: | |
| raw_config = dflash_config.get("token_top_layer", None) | |
| if raw_config is None: | |
| return {"enabled": False} | |
| token_top_config = dict(raw_config) | |
| token_top_config["enabled"] = bool(token_top_config.get("enabled", False)) | |
| token_top_config.setdefault("width", int(config.hidden_size)) | |
| token_top_config.setdefault("heads", int(config.num_attention_heads)) | |
| token_top_config.setdefault("init", "zero_output") | |
| config.token_top_layer = token_top_config | |
| if not hasattr(config, "dflash_config") or config.dflash_config is None: | |
| config.dflash_config = {} | |
| config.dflash_config["token_top_layer"] = token_top_config | |
| return token_top_config | |
| def has_token_top_layer(self) -> bool: | |
| return self.token_top_layer is not None | |
| def apply_token_top_layer( | |
| self, | |
| hidden_states: torch.Tensor, | |
| prev_token_embedding: torch.Tensor, | |
| position_ids: torch.LongTensor, | |
| block_keep_mask: torch.Tensor, | |
| ) -> torch.Tensor: | |
| if self.token_top_layer is None: | |
| return hidden_states | |
| return self.token_top_layer( | |
| hidden_states=hidden_states, | |
| prev_token_embedding=prev_token_embedding, | |
| position_ids=position_ids, | |
| block_keep_mask=block_keep_mask, | |
| ) | |
| def resolve_causal_head(self, head_type: str = "auto") -> bool: | |
| if head_type == "auto": | |
| return bool(self.causal_head) | |
| if head_type == "bidirectional": | |
| return False | |
| if head_type == "causal": | |
| return True | |
| raise ValueError( | |
| f"Unsupported head_type={head_type!r}. Expected one of: auto, bidirectional, causal." | |
| ) | |
| def forward( | |
| self, | |
| position_ids: torch.LongTensor, | |
| attention_mask: Optional[torch.Tensor] = None, | |
| noise_embedding: Optional[torch.Tensor] = None, | |
| target_hidden: Optional[torch.Tensor] = None, | |
| token_top_prev_embedding: Optional[torch.Tensor] = None, | |
| token_top_block_keep_mask: Optional[torch.Tensor] = None, | |
| past_key_values: Optional[Cache] = None, | |
| use_cache: bool = False, | |
| **kwargs, | |
| ) -> CausalLMOutputWithPast: | |
| hidden_states = noise_embedding | |
| target_hidden = self.hidden_norm(self.fc(target_hidden)) | |
| position_embeddings = self.rotary_emb(hidden_states, position_ids) | |
| for layer in self.layers: | |
| hidden_states = layer( | |
| hidden_states=hidden_states, | |
| target_hidden=target_hidden, | |
| attention_mask=attention_mask, | |
| position_ids=position_ids, | |
| past_key_value=past_key_values, | |
| use_cache=use_cache, | |
| position_embeddings=position_embeddings, | |
| **kwargs, | |
| ) | |
| hidden_states = self.norm(hidden_states) | |
| if self.token_top_layer is not None: | |
| if token_top_prev_embedding is None: | |
| _log_token_top_inactive_once() | |
| else: | |
| if token_top_block_keep_mask is None: | |
| raise ValueError( | |
| "token_top_block_keep_mask is required when token_top_prev_embedding is provided." | |
| ) | |
| token_top_position_ids = position_ids[:, -hidden_states.shape[1] :] | |
| hidden_states = self.apply_token_top_layer( | |
| hidden_states=hidden_states, | |
| prev_token_embedding=token_top_prev_embedding.to(dtype=hidden_states.dtype), | |
| position_ids=token_top_position_ids, | |
| block_keep_mask=token_top_block_keep_mask, | |
| ) | |
| return hidden_states | |
| def spec_generate( | |
| self, | |
| target: nn.Module, | |
| input_ids: torch.LongTensor, | |
| max_new_tokens: int, | |
| stop_token_ids: list[int], | |
| temperature: float, | |
| ): | |
| self.eval() | |
| num_input_tokens = input_ids.shape[1] | |
| max_length = num_input_tokens + max_new_tokens | |
| block_size = self.block_size | |
| output_ids = torch.full( | |
| (1, max_length + block_size), | |
| self.mask_token_id, | |
| dtype=torch.long, | |
| device=target.device, | |
| ) | |
| position_ids = torch.arange( | |
| output_ids.shape[1], device=target.device | |
| ).unsqueeze(0) | |
| past_key_values_target = DynamicCache() | |
| past_key_values_draft = DynamicCache() | |
| # Prefill stage | |
| output = target( | |
| input_ids, | |
| position_ids=position_ids[:, :num_input_tokens], | |
| past_key_values=past_key_values_target, | |
| use_cache=True, | |
| logits_to_keep=1, | |
| output_hidden_states=True, | |
| ) | |
| output_ids[:, :num_input_tokens] = input_ids | |
| output_ids[:, num_input_tokens : num_input_tokens + 1] = sample( | |
| output.logits, temperature | |
| ) | |
| target_hidden = extract_context_feature( | |
| output.hidden_states, self.target_layer_ids | |
| ) | |
| # Decode stage | |
| acceptance_lengths = [] | |
| start = input_ids.shape[1] | |
| while start < max_length: | |
| block_output_ids = output_ids[:, start : start + block_size].clone() | |
| block_position_ids = position_ids[:, start : start + block_size] | |
| noise_embedding = target.model.embed_tokens(block_output_ids) | |
| draft_logits = target.lm_head( | |
| self( | |
| target_hidden=target_hidden, | |
| noise_embedding=noise_embedding, | |
| position_ids=position_ids[ | |
| :, past_key_values_draft.get_seq_length() : start + block_size | |
| ], | |
| past_key_values=past_key_values_draft, | |
| use_cache=True, | |
| is_causal=False, | |
| )[:, -block_size + 1 :, :] | |
| ) | |
| past_key_values_draft.crop(start) | |
| block_output_ids[:, 1:] = sample(draft_logits) | |
| output_ids[:, start + 1 : start + block_size] = block_output_ids[:, 1:] | |
| output = target( | |
| block_output_ids, | |
| position_ids=block_position_ids, | |
| past_key_values=past_key_values_target, | |
| use_cache=True, | |
| output_hidden_states=True, | |
| ) | |
| posterior = sample(output.logits, temperature) | |
| acceptance_length = ( | |
| (block_output_ids[:, 1:] == posterior[:, :-1]) | |
| .cumprod(dim=1) | |
| .sum(dim=1)[0] | |
| .item() | |
| ) | |
| output_ids[:, start : start + acceptance_length + 1] = block_output_ids[ | |
| :, : acceptance_length + 1 | |
| ] | |
| output_ids[:, start + acceptance_length + 1] = posterior[ | |
| :, acceptance_length | |
| ] | |
| start += acceptance_length + 1 | |
| past_key_values_target.crop(start) | |
| target_hidden = extract_context_feature( | |
| output.hidden_states, self.target_layer_ids | |
| )[:, : acceptance_length + 1, :] | |
| acceptance_lengths.append(acceptance_length + 1) | |
| if stop_token_ids is not None and any( | |
| stop_token_id in output_ids[:, num_input_tokens:] | |
| for stop_token_id in stop_token_ids | |
| ): | |
| break | |
| output_ids = output_ids[:, :max_length] | |
| output_ids = output_ids[:, output_ids[0] != self.mask_token_id] | |
| if stop_token_ids is not None: | |
| stop_token_ids = torch.tensor(stop_token_ids, device=output_ids.device) | |
| stop_token_indices = torch.isin( | |
| output_ids[0][num_input_tokens:], stop_token_ids | |
| ).nonzero(as_tuple=True)[0] | |
| if stop_token_indices.numel() > 0: | |
| output_ids = output_ids[ | |
| :, : num_input_tokens + stop_token_indices[0] + 1 | |
| ] | |
| return output_ids | |
Xet Storage Details
- Size:
- 27.2 kB
- Xet hash:
- 6853f53b60e1ca399079241a2adcc604cca8fa5d25348218f6760e1524563167
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.