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
13 changes: 13 additions & 0 deletions aspec/config/train_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ class TrainingConfig:
# Domino mixes corrected and backbone-only CE as
# (1 - lambda_base) * corrected + lambda_base * backbone.
domino_lambda_base: float = 0.3
# Optional linear curriculum for lambda_base, mirroring the reference
# SpecForge schedule: start * max(0, 1 - step / (total_steps * ratio)).
# Leave both unset to keep the constant domino_lambda_base value.
domino_lambda_base_start: Optional[float] = None
domino_lambda_base_decay_ratio: Optional[float] = None
# Report backbone-only per-position held-out metrics alongside corrected.
domino_eval_backbone: bool = False
# Pack multiple complete documents into one fixed row. The controller owns
# grouping; the trainer preserves document boundaries for attention/RoPE.
dflash_packing: bool = False
Expand Down Expand Up @@ -300,6 +307,9 @@ class Config:
mooncake: dict[str, Any] = field(default_factory=dict)
training: TrainingConfig = field(default_factory=TrainingConfig)
cache_dir: str = "./cache"
# Tokenizer worker processes for dataset preparation. Lower this on hosts
# where fork-based multiprocessing is constrained.
num_proc: int = 64
cache_key: Optional[str] = None
model_download_dir: Optional[str] = None
output_dir: str = ""
Expand Down Expand Up @@ -367,6 +377,9 @@ def _validate_vllm_config(config: DictConfig) -> None:

def _validate_current_scope(config: DictConfig) -> None:
"""Reject backend selections outside ASpec's current executable path."""
num_proc = config.get("num_proc", 64)
if isinstance(num_proc, bool) or not isinstance(num_proc, int) or num_proc < 1:
raise ValueError("num_proc must be an integer >= 1")
engine_type = config.inference.inference_engine_type
target_backend = config.model.target_model_backend
if engine_type not in {"sgl", "offline"} or target_backend != "sglang":
Expand Down
25 changes: 25 additions & 0 deletions aspec/models/dflash.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ def _compute_logits(
return self.draft_model.lm_head(draft_hidden)
return F.linear(draft_hidden, lm_head_weight)

collect_extra_position_metrics = False

def _extra_training_loss(
self,
draft_hidden: torch.Tensor,
Expand All @@ -488,6 +490,18 @@ def _extra_training_loss(
) -> tuple[torch.Tensor, dict]:
return logits.new_zeros(()), {}

def _extra_position_metrics(
self,
*,
bsz: int,
n_blocks: int,
target_ids: torch.Tensor,
binary_weights: torch.Tensor,
count_per_position: torch.Tensor,
) -> dict:
"""Algorithm-specific additional per-position metrics for evaluation."""
return {}

def _training_loss(
self,
*,
Expand Down Expand Up @@ -1368,6 +1382,17 @@ def forward(
dim=(0, 1)
) / count_per_pos

if self.collect_extra_position_metrics:
self._last_extra_position_metrics = self._extra_position_metrics(
bsz=bsz,
n_blocks=n_blocks,
target_ids=target_ids,
binary_weights=binary_weights,
count_per_position=count_per_position,
)
else:
self._last_extra_position_metrics = {}

return (
loss,
accuracy,
Expand Down
44 changes: 44 additions & 0 deletions aspec/models/domino.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def __init__(self, *args, lambda_base=0.0, **kwargs):
# 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
# Held-out evaluation can collect backbone-only per-position metrics
# from the same forward and anchors as the corrected branch.
self.collect_extra_position_metrics = False
self._last_backbone_report = None
self.set_lambda_base(lambda_base)

def _prepare_draft_prefix(
Expand Down Expand Up @@ -143,10 +147,50 @@ def _training_loss(
"backbone_ce": base_per_token.detach(),
"corrected_ce": corrected_per_token.detach(),
}
if self.collect_extra_position_metrics:
# Keep the same-forward backbone values for evaluation; the mixed
# loss and corrected reporting branch are unchanged.
self._last_backbone_report = (base_per_token.detach(), base_logits.detach())
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
else:
# Never retain the last evaluation logits across training.
self._last_backbone_report = None
# Report the corrected objective's per-position value; accuracy uses the
# corrected logits as well.
return loss_per_token, corrected_per_token, corrected_logits

def _extra_position_metrics(
self,
*,
bsz,
n_blocks,
target_ids,
binary_weights,
count_per_position,
):
if not self.collect_extra_position_metrics or self._last_backbone_report is None:
return {}
base_per_token, base_logits = self._last_backbone_report
try:
block_size = self.block_size
base_loss_pp = base_per_token.reshape(bsz, n_blocks, block_size)
vocab_size = base_logits.size(-1)
base_pred = base_logits.reshape(-1, vocab_size).argmax(-1)
correct = (base_pred.view(bsz, n_blocks, block_size) == target_ids) & (
binary_weights > 0.5
)
safe_count = count_per_position.clamp(min=1.0)
loss_pp = (base_loss_pp * binary_weights).sum(dim=(0, 1)) / safe_count
acc_pp = correct.float().sum(dim=(0, 1)) / safe_count
return {
"backbone_loss_pp": loss_pp.detach(),
"backbone_acc_pp": acc_pp.detach(),
"backbone_count_pp": count_per_position.detach(),
}
finally:
# The full logits share storage with the forward graph; keep only
# the reduced position tensors and release the report immediately.
self._last_backbone_report = None

def _extra_training_loss(
self,
draft_hidden: torch.Tensor,
Expand Down
31 changes: 31 additions & 0 deletions aspec/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,30 @@ def _load_draft_descriptor(config: DictConfig) -> dict[str, Any]:
if (isinstance(lambda_base, bool) or not isinstance(lambda_base, (int, float))
or not math.isfinite(lambda_base) or not 0.0 <= lambda_base <= 1.0):
raise PlanCompileError("domino_lambda_base must be a finite number in [0, 1]")
lambda_start = _select(config, "training.domino_lambda_base_start", None)
lambda_ratio = _select(config, "training.domino_lambda_base_decay_ratio", None)
if (lambda_start is None) != (lambda_ratio is None):
raise PlanCompileError(
"domino_lambda_base_start and domino_lambda_base_decay_ratio "
"must be set together"
)
if lambda_start is not None:
if (isinstance(lambda_start, bool)
or not isinstance(lambda_start, (int, float))
or not math.isfinite(lambda_start)
or not 0.0 <= lambda_start <= 1.0):
raise PlanCompileError(
"domino_lambda_base_start must be a finite number in [0, 1]"
)
if (isinstance(lambda_ratio, bool)
or not isinstance(lambda_ratio, (int, float))
or not math.isfinite(lambda_ratio)
or lambda_ratio <= 0.0):
raise PlanCompileError(
"domino_lambda_base_decay_ratio must be a finite number > 0"
)
if not isinstance(_select(config, "training.domino_eval_backbone", False), bool):
raise PlanCompileError("domino_eval_backbone must be boolean")

target_layer_ids = raw.get("target_layer_ids")
if not isinstance(target_layer_ids, list) or not target_layer_ids:
Expand Down Expand Up @@ -1442,6 +1466,13 @@ def _workload(
"ce_loss_alpha": 1.0,
"l1_loss_alpha": 0.0,
"lambda_base": _select(config, "training.domino_lambda_base", 0.3),
"lambda_base_start": _select(
config, "training.domino_lambda_base_start", None
),
"lambda_base_decay_ratio": _select(
config, "training.domino_lambda_base_decay_ratio", None
),
"eval_backbone": _select(config, "training.domino_eval_backbone", False),
"shift_label": draft["domino_shift_label"],
"pure_draft_prefix_len": draft["domino_pure_draft_prefix_len"],
"correction_head": {
Expand Down
36 changes: 36 additions & 0 deletions aspec/training/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
RNG_STATE_SCHEMA_VERSION = "aspec.rng-state/v1"


def _domino_lambda_horizon(args: Any) -> int | None:
value = getattr(args, "lr_total_steps", None) or getattr(
args, "num_train_steps", None
)
if not value:
return None
return int(value)


def _logical_training_topology(args: Any, world_size: int) -> dict[str, Any]:
"""Return resume-relevant logical topology without physical host identity."""

Expand Down Expand Up @@ -108,6 +117,20 @@ def _logical_training_topology(args: Any, world_size: int) -> dict[str, Any]:
topology["training_objective"]["lambda_base"] = getattr(
args, "domino_lambda_base", 0.3
)
topology["training_objective"]["lambda_base_start"] = getattr(
args, "domino_lambda_base_start", None
)
topology["training_objective"]["lambda_base_decay_ratio"] = getattr(
args, "domino_lambda_base_decay_ratio", None
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if getattr(args, "domino_lambda_base_start", None) is not None:
horizon = _domino_lambda_horizon(args)
if horizon is None:
raise RuntimeError(
"Domino lambda_base curriculum requires an effective "
"lr_total_steps or num_train_steps horizon"
)
topology["training_objective"]["lambda_base_total_steps"] = horizon
if "training_objective" in topology:
fields = ("max_seq_length", "micro_batch_size", "draft_accumulation_steps",
"max_grad_norm", "seed")
Expand Down Expand Up @@ -218,6 +241,10 @@ def _validate_exact_resume_topology(args: Any, metadata: dict[str, Any], *, chec
key for key in ("lambda_base", "block_size", "loss", "loss_decay_gamma")
if key not in objective
]
if objective.get("lambda_base_start") is not None and (
"lambda_base_total_steps" not in objective
):
missing.append("lambda_base_total_steps")
missing += [
key for key in ("shift_label", "pure_draft_prefix_len")
if key not in semantics
Expand All @@ -228,6 +255,15 @@ def _validate_exact_resume_topology(args: Any, metadata: dict[str, Any], *, chec
f"missing {missing}. Use continual_training=true for a "
"weights-only new stage."
)
if (
"lambda_base_start" not in objective
or "lambda_base_decay_ratio" not in objective
) and getattr(args, "domino_lambda_base_start", None) is not None:
raise RuntimeError(
"Exact resume cannot add a lambda_base curriculum to a legacy "
"constant-lambda checkpoint; use continual_training=true for a "
"new phase."
)
if current_policy.get("dsa") is not None:
# Chunk workspace is an execution bound, not the DSA objective. Preserve
# every recorded semantic field while allowing a different memory budget.
Expand Down
4 changes: 4 additions & 0 deletions aspec/training/dflash_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,15 @@ def eval_forward(self, batch: dict) -> dict:
loss_components,
_,
) = self._forward(batch)
extra_position_metrics = getattr(
self.dflash, "_last_extra_position_metrics", {}
) or {}
return {
"loss_pp": loss_per_position.detach(),
"acc_pp": acc_per_position.detach(),
"count_pp": count_per_position.detach(),
**loss_components,
**extra_position_metrics,
}

def _reduce_position_metrics(
Expand Down
Loading