-
Notifications
You must be signed in to change notification settings - Fork 443
[Feat]:Support DPace #1724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[Feat]:Support DPace #1724
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,36 @@ | |
| __all__ = ["HFDFlashModel"] | ||
|
|
||
|
|
||
| def _dpace_position_weights(confidences: torch.Tensor, alpha: float) -> torch.Tensor: | ||
| """Compute detached D-PACE per-position weights from draft confidences. | ||
|
|
||
| Implements D-PACE (arXiv:2605.18810) Eq.7-8: each draft confidence ``q_i`` is | ||
| smoothed toward 1 with ``q~_i = (1 - alpha) * q_i + alpha`` (Eq.7), then the | ||
| per-position weight is the suffix-sum of the prefix products of the smoothed | ||
| confidences, ``w_j = sum_{m >= j} prod_{i <= m} q~_i`` (Eq.8). This factors into | ||
| the prefix acceptance probability times the remaining accepted-length value, so | ||
| the loss tracks each position's contribution to expected accepted block length. | ||
|
|
||
| Args: | ||
| confidences: ``[..., L]`` draft confidence ``q_i = exp(-CE)`` per position. | ||
| alpha: smoothing factor in (0, 1]; raises if outside [0, 1]. | ||
|
|
||
| Returns: | ||
| Detached weights with the same shape and dtype as ``confidences``. | ||
| """ | ||
| if not 0.0 <= alpha <= 1.0: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [SUGGESTION] Docstring says |
||
| raise ValueError(f"dflash_dpace_alpha must be in [0, 1], got {alpha}") | ||
|
|
||
| with torch.no_grad(): | ||
| smoothed = (1.0 - alpha) * confidences.float() + alpha | ||
| prefix_products = torch.cumprod(smoothed, dim=-1) | ||
| # Suffix sum over positions: reverse -> cumsum -> reverse. | ||
| weights = torch.flip( | ||
| torch.cumsum(torch.flip(prefix_products, dims=[-1]), dim=-1), dims=[-1] | ||
| ) | ||
| return weights.to(dtype=confidences.dtype) | ||
|
|
||
|
|
||
| @DFlashDMRegistry.register({PreTrainedModel: "hf.PreTrainedModel"}) | ||
| class HFDFlashModel(DFlashModel): | ||
| """DFlash Model for HuggingFace transformers.""" | ||
|
|
@@ -349,8 +379,21 @@ def _compute_loss( | |
|
|
||
| binary_eval_mask = weight_mask.view(-1) | ||
|
|
||
| # Optional loss decay | ||
| if self.dflash_loss_decay_factor > 0: | ||
| # Block-position loss weighting: dynamic D-PACE weights or static exponential decay. | ||
| if self.dflash_loss_objective == "dpace" and block_size > 1: | ||
| # Draft confidence q_i = exp(-CE) on the target-selected token, over the | ||
| # predicted positions (slot 0 is the given anchor, already masked above). | ||
| # Weights are detached (paper Eq.9), so this adds the documented ~2.3% | ||
| # training overhead without altering the cross-entropy gradient. | ||
| with torch.no_grad(): | ||
| conf_ce = F.cross_entropy( | ||
| logits.view(-1, logits.size(-1)), target_ids.view(-1), reduction="none" | ||
| ).view(bsz, n_blocks, block_size) | ||
| confidences = torch.exp(-conf_ce[..., 1:].float()) | ||
| dpace = torch.ones_like(weight_mask) | ||
| dpace[..., 1:] = _dpace_position_weights(confidences, self.dflash_dpace_alpha) | ||
| weight_mask = weight_mask * dpace | ||
| elif self.dflash_loss_decay_factor > 0: | ||
| k = torch.arange(block_size, device=device).view(1, 1, -1) | ||
| decay = torch.exp(-(k - 1).clamp(min=0).float() / self.dflash_loss_decay_factor) | ||
| weight_mask = weight_mask * decay | ||
|
Comment on lines
+388
to
399
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [SUGGESTION] When Why it matters: small but free win on training throughput; CE is one of the more expensive ops in the inner training loop because of the vocab-size matmul. How to apply: hoist a single |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[SUGGESTION] Consider warning (or rejecting) when
dflash_loss_objective == "dpace"anddflash_loss_decay_factor != 0.0(i.e. the user has explicitly set both). The default recipemodelopt_recipes/general/speculative_decoding/dflash.yamlalready setsdflash_loss_decay_factor: 4.0, so a user who only flipsdflash_loss_objective: dpacewon't realize their non-default decay value is silently ignored (the doc notes the mutual exclusion, but the runtime is silent). Alogger.warning(...)here would surface the misconfiguration without blocking the run.