Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 71 additions & 29 deletions aspec/models/dflash.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class DFlashModel(nn.Module):
"""

prediction_start = 1
# First label offset relative to an anchor. Base DFlash predicts the token at
# the anchor itself (masked out); shifted-layout variants start at anchor + 1.
label_start = 0

def __init__(
self,
Expand Down Expand Up @@ -438,6 +441,53 @@ def _extra_training_loss(
) -> tuple[torch.Tensor, dict]:
return logits.new_zeros(()), {}

def _training_loss(
self,
*,
logits: torch.Tensor,
draft_hidden: torch.Tensor,
target_ids: torch.Tensor,
last_hidden_states: Optional[torch.Tensor],
lm_head_weight: torch.Tensor,
safe_label_indices: torch.Tensor,
packed_context_positions: Optional[torch.Tensor],
sequence_length: int,
input_ids: torch.Tensor,
anchor_positions: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""Per-token objective and the logits used for reporting.

Base DFlash applies ``ce_loss_alpha * CE + l1_loss_alpha * L1``.
Algorithm wrappers may replace the objective (and the logits used for
accuracy) without duplicating anchor, mask or reduction bookkeeping.
"""
vocab_size = logits.size(-1)
flat_logits = logits.view(-1, vocab_size)
flat_targets = target_ids.view(-1)
ce_per_token = F.cross_entropy(flat_logits, flat_targets, reduction="none")

loss_per_token = self.ce_loss_alpha * ce_per_token
if self.uses_target_distribution:
if last_hidden_states is None:
raise ValueError(
"DFlash L1 distillation (l1_loss_alpha > 0) requires target "
"last_hidden_states; set inference.store_last_hidden_states=true in the "
"run config."
)
tgt_idx = (safe_label_indices - 1).clamp(min=0)
aligned_hidden = self._align_target_hidden_states(
last_hidden_states,
tgt_idx,
global_sequence_length=sequence_length,
packed_context_positions=packed_context_positions,
)
target_logits = F.linear(aligned_hidden, lm_head_weight).view(-1, vocab_size)
target_probs = torch.softmax(target_logits.float(), dim=-1)
draft_probs = torch.softmax(flat_logits.float(), dim=-1)
l1_per_token = (draft_probs - target_probs).abs().sum(dim=-1)
loss_per_token = loss_per_token + self.l1_loss_alpha * l1_per_token
return loss_per_token, ce_per_token, logits

def _align_target_hidden_states(
self,
last_hidden_states: torch.Tensor,
Expand Down Expand Up @@ -1094,8 +1144,10 @@ def forward(
logits = self._compute_logits(draft_hidden, lm_head_weight)

# 8. Compute labels and weight mask (SpecForge pattern)
# Labels: same-position prediction (position k predicts token at anchor+k)
label_offsets = torch.arange(0, self.block_size, device=device).view(1, 1, -1)
# Labels: same-position prediction (position k predicts token at anchor + label_start + k)
label_offsets = torch.arange(
self.label_start, self.label_start + self.block_size, device=device
Comment thread
zhouaihui marked this conversation as resolved.
Resolved
).view(1, 1, -1)
label_indices = anchor_positions.unsqueeze(-1) + label_offsets # [B, n_blocks, block_size]
valid_label_mask = label_indices < seq_len
safe_label_indices = label_indices.clamp(max=seq_len - 1)
Expand Down Expand Up @@ -1127,34 +1179,24 @@ def forward(
# our objective weighting is an addition to the training signal, not the metric.
binary_eval_mask = weight_mask.view(-1)

# 9. Per-token loss: ce_loss_alpha*CE + l1_loss_alpha*L1.
vocab_size = logits.size(-1)
flat_logits = logits.view(-1, vocab_size)
# 9. Per-token objective, algorithm-specific logits and reporting values.
loss_per_token, loss_per_token_by_position, logits = self._training_loss(
logits=logits,
draft_hidden=draft_hidden,
target_ids=target_ids,
last_hidden_states=last_hidden_states,
lm_head_weight=lm_head_weight,
safe_label_indices=safe_label_indices,
packed_context_positions=packed_context_positions,
sequence_length=seq_len,
input_ids=input_ids,
anchor_positions=anchor_positions,
)
loss_per_token_by_position = loss_per_token_by_position.view(
bsz, n_blocks, self.block_size
)
flat_logits = logits.view(-1, logits.size(-1))
flat_targets = target_ids.view(-1)
ce_per_token = F.cross_entropy(flat_logits, flat_targets, reduction="none")

loss_per_token = self.ce_loss_alpha * ce_per_token
if self.uses_target_distribution:
if last_hidden_states is None:
raise ValueError(
"DFlash L1 distillation (l1_loss_alpha > 0) requires target "
"last_hidden_states; set inference.store_last_hidden_states=true in the "
"run config."
)
tgt_idx = (safe_label_indices - 1).clamp(min=0)
aligned_hidden = self._align_target_hidden_states(
last_hidden_states,
tgt_idx,
global_sequence_length=seq_len,
packed_context_positions=packed_context_positions,
)
target_logits = F.linear(aligned_hidden, lm_head_weight).view(-1, vocab_size)
target_probs = torch.softmax(target_logits.float(), dim=-1)
draft_probs = torch.softmax(flat_logits.float(), dim=-1)
l1_per_token = (draft_probs - target_probs).abs().sum(dim=-1)
loss_per_token = loss_per_token + self.l1_loss_alpha * l1_per_token

loss_per_token_by_position = ce_per_token.view(bsz, n_blocks, self.block_size)

objective_weights = weight_mask
if (
Expand Down
135 changes: 135 additions & 0 deletions aspec/models/domino.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright 2026 Ant Group Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

# Adapted from the merged SpecForge Domino training path
# (sgl-project/SpecForge PR #571, specforge/core/domino.py, MIT; see
# licenses/SpecForge-MIT.txt). The objective, predecessor alignment and
# label-shift handling follow that implementation and the public Domino method
# (arXiv:2605.29707). Anchor sampling, block masks, reductions and the DFlash
# training workflow stay in the retained ASpec DFlash path.
"""Domino correction-head training objective on the DFlash block backbone."""

import math

from aspec.models.dflash import DFlashModel


class DominoModel(DFlashModel):
"""Block-parallel DFlash objective mixed with a causal correction head."""

def __init__(self, *args, lambda_base=0.0, **kwargs):
super().__init__(*args, **kwargs)
config = getattr(self.draft_model, "config", None)
if getattr(config, "projector_type", None) != "domino":
raise ValueError("Domino training requires a Domino draft config")
if not hasattr(self.draft_model, "correct_logits"):
raise ValueError("Domino training requires a DominoDraftModel")
if config.block_size != self.block_size:
raise ValueError("Domino training block_size must match its Draft config")
if self.mtp_conditioning is not None:
raise ValueError("Domino does not use the native-MTP conditioning contract")
if self.draft_context_parallel_size != 1:
raise NotImplementedError("Domino currently requires draft_context_parallel_size=1")
if self.loss_objective != "decay":
raise ValueError("Domino supports the decay/uniform weighting objective only")
if self.ce_loss_alpha != 1 or self.l1_loss_alpha != 0:
raise ValueError(
"Domino replaces the CE/L1 mix with its own objective; set "
"ce_loss_alpha=1 and l1_loss_alpha=0"
)
# Shifted labels start at anchor + 1 and supervise position 0; the
# unshifted layout keeps DFlash's same-position labels and skips the
# anchor slot. Both match the two documented Domino layouts.
self.label_start = 1 if self.draft_model.shift_label else 0
self.prediction_start = 1 - self.label_start
self.set_lambda_base(lambda_base)

def _prepare_draft_prefix(
self,
input_ids,
last_hidden_states,
attention_mask,
draft_prefix,
*packed_metadata,
):
ctx_doc_ids, _, packed_context_positions, packed_document_lengths = packed_metadata
if any(
value is not None
for value in (ctx_doc_ids, packed_context_positions, packed_document_lengths)
):
raise NotImplementedError(
"Domino supports padded, non-packed inputs only; packed documents "
"or sharded context are rejected before backbone execution"
)
return super()._prepare_draft_prefix(
input_ids,
last_hidden_states,
attention_mask,
draft_prefix,
*packed_metadata,
)

def set_lambda_base(self, lambda_base) -> None:
"""Set the backbone-only loss weight for the current step."""
if (
isinstance(lambda_base, bool)
or not isinstance(lambda_base, (int, float))
or not math.isfinite(float(lambda_base))
):
raise ValueError("Domino lambda_base must be a finite number in [0, 1]")
lambda_base = float(lambda_base)
if not 0.0 <= lambda_base <= 1.0:
raise ValueError("Domino lambda_base must be a finite number in [0, 1]")
self.lambda_base = lambda_base

def _training_loss(
self,
*,
logits,
draft_hidden,
target_ids,
last_hidden_states,
lm_head_weight,
safe_label_indices,
packed_context_positions,
sequence_length,
input_ids,
anchor_positions,
):
base_logits = logits
corrected_logits = self.draft_model.correct_logits(
draft_hidden, base_logits, input_ids, anchor_positions
)
# The retained CE path supplies the reduction-ready per-token values for
# both the corrected and backbone-only predictions.
_, corrected_per_token, _ = super()._training_loss(
logits=corrected_logits,
draft_hidden=draft_hidden,
target_ids=target_ids,
last_hidden_states=last_hidden_states,
lm_head_weight=lm_head_weight,
safe_label_indices=safe_label_indices,
packed_context_positions=packed_context_positions,
sequence_length=sequence_length,
input_ids=input_ids,
anchor_positions=anchor_positions,
)
_, base_per_token, _ = super()._training_loss(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Avoid autograd work for zero-weight loss branches

lambda_base defaults to 0, but this call still computes the full-vocabulary backbone CE and retains its backward path before the result is multiplied by zero. Likewise, at lambda_base=1, the GRU/correction/CE branch remains in the autograd graph even though it only contributes reporting values. This adds avoidable CE/backward work and saved activations at the curriculum endpoints.

Consider skipping base CE at lambda_base=0, and computing corrected reporting values under no_grad at lambda_base=1 while keeping the backbone loss differentiable. That preserves the corrected-metric contract. This is a non-blocking optimization suggestion; no GPU performance measurement was made in this review.

logits=base_logits,
draft_hidden=draft_hidden,
target_ids=target_ids,
last_hidden_states=last_hidden_states,
lm_head_weight=lm_head_weight,
safe_label_indices=safe_label_indices,
packed_context_positions=packed_context_positions,
sequence_length=sequence_length,
input_ids=input_ids,
anchor_positions=anchor_positions,
)
lambda_base = self.lambda_base
loss_per_token = (
(1.0 - lambda_base) * corrected_per_token + lambda_base * base_per_token
)
# Report the corrected objective's per-position value; accuracy uses the
# corrected logits as well.
return loss_per_token, corrected_per_token, corrected_logits
Loading
Loading