def get_pipeline_boundaries(
config: Any, stage_ranges: list[tuple[int, int]]
) -> tuple[PipelineBoundary, ...]:
"""Resolve cache, top-k and candidate lifetimes at every stage cut."""
num_layers = config.num_hidden_layers
if (
not stage_ranges
or stage_ranges[0][0] != 0
or stage_ranges[-1][1] != num_layers
or any(start >= end for start, end in stage_ranges)
or any(a[1] != b[0] for a, b in zip(stage_ranges, stage_ranges[1:]))
):
raise ValueError("DeepSeek-V4.1 pipeline stages must partition all layers")
ratios = config.compress_ratios
if len(ratios) < num_layers or any(r not in (0, 1, 2) for r in ratios):
raise ValueError("Invalid DeepSeek-V4.1 compress_ratios")
sources = {}
for kind, field in (
("kv", "kv_source_layer_ids"),
("index", "index_source_layer_ids"),
):
values = tuple(getattr(config, field, ()) or ())
if (
values != tuple(sorted(set(values)))
or any(type(s) is not int or not 0 <= s < num_layers for s in values)
or any(ratios[s] == 0 for s in values)
):
raise ValueError(f"Invalid DeepSeek-V4.1 {field}")
sources[kind] = values
if not set(sources["kv"]).issubset(sources["index"]):
raise ValueError("KV sources must also publish index keys")
dependencies: dict[str, list[tuple[int, int]]] = {"kv": [], "index": []}
for layer in range(num_layers):
if ratios[layer] == 0:
continue
for kind, values in sources.items():
preceding = [s for s in values if s <= layer]
if not preceding:
raise ValueError(f"Layer {layer} has no preceding {kind} source")
source = preceding[-1]
if ratios[source] != ratios[layer]:
raise ValueError(
f"Layer {layer} and its {kind} source disagree on ratio"
)
dependencies[kind].append((source, layer))
kv_source_of = {layer: source for source, layer in dependencies["kv"]}
dependencies["indexer_k"] = [
(kv_source_of[layer], layer)
for layer in sources["index"]
if layer not in sources["kv"]
]
candidate = getattr(config, "candidate_source_layer_id", -1)
candidate_enabled = (
candidate >= 0 and getattr(config, "candidate_topk_blocks", 0) > 0
)
if candidate_enabled and candidate not in sources["index"]:
raise ValueError("Candidate source must be an index source")
boundaries = []
for _, cut in stage_ranges[:-1]:
live = {}
for kind, edges in dependencies.items():
crossing = {s for s, consumer in edges if s < cut <= consumer}
assert len(crossing) <= 1
live[kind] = next(iter(crossing), None)
boundaries.append(
PipelineBoundary(
kv_source=live["kv"],
index_source=live["index"],
candidate_source=(
candidate
if candidate_enabled
and candidate < cut
and any(s >= cut for s in sources["index"])
else None
),
indexer_k_source=live["indexer_k"],
)
)
return tuple(boundaries)