Skip to content

Official Inference Code of EAGLE-4

refactor(core): build every batch-size schedule with one range merge

Seyfal Sultanov344a02ff773c
diff --git a/eagle_harness/core/chain_width.py b/eagle_harness/core/chain_width.py
index 993c1ed..4199de4 100644
--- a/eagle_harness/core/chain_width.py
+++ b/eagle_harness/core/chain_width.py
@@ -42,7 +42,7 @@ import json
import os
import statistics
from dataclasses import dataclass
-from typing import Dict, List, Mapping, Optional, Sequence, Tuple
+from typing import Dict, Iterable, List, Mapping, Optional, Sequence, Tuple
from eagle_harness.core.cost_table import BatchCostTable, CostCurve, load_cost_tables
from eagle_harness.core.limits import nearest_cost_batch_size
@@ -247,18 +247,22 @@ def width_at(schedule: Sequence[Tuple[int, int, int]], batch_size: int) -> int:
raise ValueError(f"batch size {batch_size} is outside the chain width schedule {list(schedule)}")
-def _ranges(widths: Mapping[int, int], max_batch_size: int) -> List[Tuple[int, int, int]]:
- """Per-batch-size widths of the calibrated batch sizes as ranges over ``1..max_batch_size``."""
+def merge_ranges(rows: Iterable[Tuple[int, int, int]]) -> List[Tuple[int, int, int]]:
+ """``(first, last, value)`` rows in batch-size order, consecutive rows with the same value merged into one range."""
ranges: List[Tuple[int, int, int]] = []
- for bs in range(1, max_batch_size + 1):
- width = widths[nearest_cost_batch_size(bs, widths)]
- if ranges and ranges[-1][2] == width:
- ranges[-1] = (ranges[-1][0], bs, width)
+ for first, last, value in rows:
+ if ranges and ranges[-1][2] == value:
+ ranges[-1] = (ranges[-1][0], last, value)
else:
- ranges.append((bs, bs, width))
+ ranges.append((first, last, value))
return ranges
+def _ranges(widths: Mapping[int, int], max_batch_size: int) -> List[Tuple[int, int, int]]:
+ """Per-batch-size widths of the calibrated batch sizes as ranges over ``1..max_batch_size``."""
+ return merge_ranges((bs, bs, widths[nearest_cost_batch_size(bs, widths)]) for bs in range(1, max_batch_size + 1))
+
+
# ---------------------------------------------------------------- the tree's verify width under load
# A tree speculator on an engine whose scheduler fixes the verify width per batch size (vLLM) verifies the best
# ``width`` nodes of its draft tree. Wide trees win where batches are bound by weight reads and lose where the verify
@@ -350,10 +354,4 @@ def served_tree_schedule(
ranges = _ranges({bs: w for bs, w in choices.items() if bs >= switch}, max_batch_size)
kept = [(1, switch - 1, width)] if switch > 1 else []
clipped = [(max(first, switch), last, w) for first, last, w in ranges if last >= switch]
- merged: List[Tuple[int, int, int]] = []
- for first, last, w in kept + clipped:
- if merged and merged[-1][2] == w:
- merged[-1] = (merged[-1][0], last, w)
- else:
- merged.append((first, last, w))
- return merged
+ return merge_ranges(kept + clipped)
diff --git a/eagle_harness/vllm/config.py b/eagle_harness/vllm/config.py
index 5f7d360..bf107f4 100644
--- a/eagle_harness/vllm/config.py
+++ b/eagle_harness/vllm/config.py
@@ -42,7 +42,13 @@ import logging
from dataclasses import dataclass
from typing import Any, List, Literal, Mapping, Tuple
-from eagle_harness.core.chain_width import PROFILE_FILE, chain_widths, load_tree_rounds, served_tree_schedule
+from eagle_harness.core.chain_width import (
+ PROFILE_FILE,
+ chain_widths,
+ load_tree_rounds,
+ merge_ranges,
+ served_tree_schedule,
+)
from eagle_harness.core.config import HarnessConfig, harness_config_from_dict
from eagle_harness.core.limits import (
MAX_CHAIN_VERIFY_NUM,
@@ -170,15 +176,10 @@ def _verify_widths(
def _draft_token_schedule(widths: List[Tuple[int, int, int]], limit: int) -> List[Tuple[int, int, int]]:
- ranges: List[Tuple[int, int, int]] = []
- for _, last, width in widths:
- count = max(0, width - 1)
- if ranges and ranges[-1][2] == count:
- ranges[-1] = (ranges[-1][0], last, count)
- else:
- ranges.append((ranges[-1][1] + 1 if ranges else 1, last, count))
- ranges.append((limit + 1, 1 << 16, 0))
- return ranges
+ # each range starts after the previous one: tree widths come per calibrated batch size, with gaps between them
+ firsts = [1] + [last + 1 for _, last, _ in widths[:-1]]
+ ranges = merge_ranges((first, last, max(0, width - 1)) for first, (_, last, width) in zip(firsts, widths))
+ return ranges + [(limit + 1, 1 << 16, 0)]
def derived_chain_widths(