Official Inference Code of EAGLE-4
# Copyright 2026 Eagle AI Corp.
# SPDX-License-Identifier: LicenseRef-Eagle-4-Source-Available
"""EAGLE-Harness cost measurement through generic TokenSpeed hooks.
The measurement driver writes ``<run dir>/config/measure_config.json``
(``{"batch_size": n}``) and sends ``n`` identical greedy requests.
Every decode round then drafts the full depth at one draft width and
verifies at one verify width, both cycled round-robin from a counter over the
batch size's sweep (:func:`draft_widths`, :func:`verify_widths`;
:class:`MeasurePolicy`), and commits exactly one fixed token per request,
so the context grows by one token a round. Rank 0 times each round's draft
depths (one draft step per sample, wall time between drained GPUs and a TP
barrier) and the next forward's verify (target forward through the accept,
events recorded inside the decode graph), and appends the samples
:mod:`eagle_harness.core.calibration` processes.
Only the rounds of a batch at the configured size count, and each prefill
starts them afresh. Every rank counts them. The driver bounds request length and checks every
width has samples before fitting.
"""
from __future__ import annotations
import json
import os
import torch
import torch.distributed as dist
from eagle_harness.core.calibration import CalibrationPaths, MeasureParams, MeasureRecord, save_model_timings
from eagle_harness.core.limits import ComputeIntensity, draft_query_lens, verify_buckets
from eagle_harness.tokenspeed.tree_round.device_policy import write_round_slots
__all__ = [
"FORCED_TOKEN",
"MEASURED_ROUNDS",
"CostMeasurement",
"MeasurePolicy",
"draft_widths",
"verify_widths",
]
# The token every request commits each measured round.
FORCED_TOKEN = 888
# Verify (and draft) samples a batch records before it ends.
MEASURED_ROUNDS = 512
def draft_widths(batch_size: int, compute_intensity: str, node_top_k: int) -> list[int]:
return [w for w in draft_query_lens(batch_size, ComputeIntensity(compute_intensity)) if w <= node_top_k]
def verify_widths(batch_size: int, compute_intensity: str) -> list[int]:
return verify_buckets(batch_size, ComputeIntensity(compute_intensity))
class MeasurePolicy:
"""Exec mode 2 decisions, taken on the host into device slots laid out as
:class:`DevicePolicy`'s: every depth at the round's draft width (the first at the
previous round's, as EAGLE-4 decides the first-step width before
the round's width), no early stop, every request verified at the round's
verify width. The device steps of a round have nothing to decide."""
def __init__(
self,
*,
verify_num: int,
node_top_k: int,
tree_depth: int,
compute_intensity: str,
device: torch.device,
) -> None:
self.chain = compute_intensity == "chain_intensity"
self.chain_width_table = ()
self.eager_select_verify = False
self.verify_num_limit = verify_num
self.node_top_k = node_top_k
self.tree_depth = tree_depth
self.compute_intensity = compute_intensity
self.counter = 0
self.draft_width = node_top_k
self.previous_draft_width = node_top_k
self.verify_width_now = verify_num
# Per depth: width, pool start, work list size, go (DevicePolicy's layout).
self._slots = torch.zeros((4, tree_depth + 1), dtype=torch.int32, device=device)
self.widths, self.cumu_starts, self.work_sizes, _ = self._slots.unbind(0)
self._counts = torch.zeros((4,), dtype=torch.int32, device=device)
self.past_verify_num, self.cumu_count, self.verify_count, self.verify_num = (
self._counts.split(1)
)
def next_widths(self, batch_size: int, measured_batch_size: int) -> None:
"""Advance the counter and pick the round's widths, verifying at most
the measured batch's widest verify width."""
self.counter += 1
ci = self.compute_intensity
drafts = draft_widths(batch_size, ci, self.node_top_k)
limit = min(max(verify_widths(measured_batch_size, ci)), self.verify_num_limit)
verifies = [w for w in verify_widths(batch_size, ci) if w <= limit]
self.previous_draft_width = self.draft_width
self.draft_width = drafts[self.counter % len(drafts)]
self.verify_width_now = verifies[self.counter % len(verifies)]
def write_round(self) -> None:
"""The round's slots at the current widths."""
widths = [self.previous_draft_width]
widths += [self.draft_width] * (self.tree_depth - 2)
self._write(widths, self.verify_width_now)
def write_full_round(self) -> None:
"""Slots of a round drafting every depth at full width."""
widths = [self.node_top_k] * (self.tree_depth - 1)
self._write(widths, self.verify_num_limit)
def _write(self, widths: list[int], verify_num: int) -> None:
start = write_round_slots(self._slots, self.node_top_k, widths, lambda d, s: 0)
verify_count = min(self.verify_num_limit, start)
# The tree holds only the ranked nodes; the verify forward runs every row anyway.
verify_num = min(verify_num, verify_count)
self._counts.copy_(
torch.tensor(
[self.verify_num_limit, start, verify_count, verify_num],
dtype=torch.int32,
)
)
def go_ready(self, depth: int, seq: int = 0) -> bool:
return depth < self.tree_depth
def table_width(self, batch_size: int) -> int:
return 0
def observe_batch_size(self, batch_size: int) -> None:
pass
def deltas_depth(self, depth: int) -> bool:
return False
def begin_round(
self, live_rows: torch.Tensor, frontier: torch.Tensor, sampled: torch.Tensor
) -> None:
"""The round's slots are written before it."""
def decide(self, depth: int, work_deltas: torch.Tensor) -> None:
"""Measured widths are decided before the round."""
def select_verify(self, top_scores: torch.Tensor) -> None:
"""Measured rounds verify at the width written before the round."""
class CostMeasurement:
"""Round timers and samples. Only rank zero writes the shared profile.
The synchronous driver keeps the measurement config immutable while
requests run, so each TP rank reads the same batch size on prefill.
"""
def __init__(
self,
*,
run_dir: str,
tree_depth: int,
tp_group,
rank: int,
device: torch.device,
) -> None:
self.run_dir = run_dir
self.config_path = os.path.join(run_dir, "config", "measure_config.json")
self.tree_depth = tree_depth
self.tp_group = tp_group
self._barrier = torch.zeros((1,), dtype=torch.int32, device=device)
self.recorder = rank == 0
# The batch size the driver measures (None before it wrote one).
self.measured_batch_size: int | None = None
self.params: MeasureParams | None = None
self.records: list[MeasureRecord] = []
# Committed token of every request, read in-graph: FORCED_TOKEN throughout the fixed-length request.
self.commit_token = torch.full(
(1,), FORCED_TOKEN, dtype=torch.int32, device=device
)
# Recorded inside the decode graph around the target forward and verify.
self.verify_start = torch.cuda.Event(enable_timing=True, external=True)
self.verify_end = torch.cuda.Event(enable_timing=True, external=True)
self.draft_start = torch.cuda.Event(enable_timing=True)
self.draft_end = torch.cuda.Event(enable_timing=True)
# (verify width, context) of the tree the next forward verifies.
self.pending_verify: tuple[int, int] | None = None
# Rounds of the batch counted on every rank (rank 0 also keeps their samples).
self.verify_rounds = 0
self.draft_rounds = 0
self.written = False
def on_extend(self) -> None:
"""A prefill: reload the measured batch size and start the batch afresh."""
path = self.config_path
# The synchronous driver writes before generate and never mutates the file in flight.
self.measured_batch_size = None
if os.path.exists(path):
with open(path, encoding="utf-8") as f:
self.params = MeasureParams(**json.load(f))
self.measured_batch_size = self.params.batch_size
self.records = []
self.pending_verify = None
self.verify_rounds = 0
self.draft_rounds = 0
self.written = False
self.commit_token.fill_(FORCED_TOKEN)
@property
def finished(self) -> bool:
"""Whether the batch has its rounds (the same on every rank)."""
return self.verify_rounds >= MEASURED_ROUNDS
def recording(self, batch_size: int) -> bool:
return batch_size == self.measured_batch_size and not self.finished
def _drain(self) -> None:
if self.tp_group is not None:
# Device group: the control plane's CPU collectives run on the event-loop thread.
dist.all_reduce(self._barrier, group=self.tp_group)
torch.cuda.synchronize()
def record_verify(self, batch_size: int) -> None:
"""The verify this forward ran, if it verified a measured round."""
pending, self.pending_verify = self.pending_verify, None
if pending is None or not self.recording(batch_size):
return
self.verify_rounds += 1
if self.recorder:
self.verify_end.synchronize()
elapsed = int(self.verify_start.elapsed_time(self.verify_end) * 1000)
self._append(elapsed, 1, pending[0], pending[1], "model_llm", batch_size)
def begin_draft(self) -> None:
self._drain()
self.draft_start.record()
def end_draft(
self, batch_size: int, draft_width: int, verify_width: int, cache_len: int
) -> None:
self._drain()
self.draft_end.record()
if not self.recording(batch_size) or self.draft_rounds >= MEASURED_ROUNDS:
return
self.draft_rounds += 1
self.pending_verify = (verify_width, cache_len)
if self.recorder:
self.draft_end.synchronize()
elapsed = int(self.draft_start.elapsed_time(self.draft_end) * 1000)
self._append(
elapsed,
self.tree_depth - 1,
draft_width,
cache_len,
"model_slm",
batch_size,
)
def _append(
self,
elapsed: int,
count: int,
width: int,
cache_len: int,
category: str,
batch_size: int,
) -> None:
if elapsed <= 0:
raise RuntimeError(f"{category} sample of {elapsed} us")
self.records.append(
MeasureRecord(elapsed, count, width, cache_len, category, batch_size)
)
def complete(self) -> bool:
"""Whether rank 0 holds the finished batch's samples, not written yet."""
return self.recorder and self.finished and not self.written
def write_model_records(self) -> str:
"""Write the batch's raw stage samples under ``test/bs_<n>``; returns that directory."""
test_dir = os.path.join(self.run_dir, "test", f"bs_{self.measured_batch_size}")
assert self.params is not None
save_model_timings(CalibrationPaths(self.run_dir), self.params, self.records)
self.written = True
return test_dir