Files
tokenspeed/test/runtime/test_post_model_init_gpu.py
LightSeek Foundation 4469a8a531 test(runtime): compare cross-launch source lifetime logprobs approximately (#3975)
`test_post_model_init_source_lifetime[graphs]` asserts bitwise-equal
logprobs from two separate engine launches. On B200 the CUDA-graph
decode logprobs differ across launches by about 4e-5, and flip between
the same two values whichever launch retires its source. So the test
fails on about half of the `ut-runtime-1gpu-part-ii` runs, on `main`
too, for example [this run on
`2c7e6b5`](https://github.com/lightseekorg/tokenspeed/actions/runs/46283682733).
It also failed [this
job](https://github.com/lightseekorg/tokenspeed/actions/runs/46312679209/job/141502523160)
on #3974, which only changes the CLI.

Each launch still checks its reloads exactly, and the generated token
ids must still match across launches exactly. Only the logprobs compared
across launches use a tolerance (`abs=1e-3`). The eager `[draft]` case
was already passing.

The cause of the variation between launches was not investigated,
because no B200 was available for this change. The new assertion has not
run on GPU yet; this PR's CI is its first run. `pre-commit run
--all-files` passed, with `clang-format` skipped because no C++ files
changed.

Requested in
https://github.com/lightseekorg/tokenspeed/pull/3974#issuecomment-7263862195

Signed-off-by: lightseek-bot <243258330+lightseek-bot@users.noreply.github.com>
2026-11-29 15:04:30 -08:00

257 lines
9.0 KiB
Python

# Copyright (c) 2026 LightSeek Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Cache metadata is prepared once and survives retirement of the checkpoint source."""
import json
import os
import shutil
import sys
from importlib.metadata import entry_points
from pathlib import Path
import pytest
import torch
from safetensors.torch import load_file, save_file
from tokenizers import Tokenizer, models
from transformers import (
LlamaConfig,
LlamaForCausalLM,
PreTrainedTokenizerFast,
)
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ci_system.ci_register import register_cuda_ci
register_cuda_ci(est_time=180, suite="runtime-1gpu")
from tokenspeed.runtime.engine.io_struct import (
PauseSchedulerReqInput,
ResumeSchedulerReqInput,
UpdateWeightFromDiskReqInput,
)
from tokenspeed.runtime.entrypoints.wire_driver import WireDriver, generate_request
from tokenspeed.runtime.utils.network import get_free_port
pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA")
def _checkpoint(directory):
torch.manual_seed(17)
model = LlamaForCausalLM(
LlamaConfig(
vocab_size=64,
hidden_size=128,
intermediate_size=256,
num_hidden_layers=2,
num_attention_heads=2,
num_key_value_heads=2,
head_dim=64,
max_position_embeddings=128,
bos_token_id=0,
eos_token_id=1,
pad_token_id=2,
)
)
model.save_pretrained(directory)
tokenizer = Tokenizer(
models.WordLevel({f"t{i}": i for i in range(64)}, unk_token="t0")
)
PreTrainedTokenizerFast(
tokenizer_object=tokenizer, unk_token="t0", eos_token="t1", pad_token="t2"
).save_pretrained(directory)
def _draft_checkpoint(directory):
directory.mkdir()
LlamaConfig(
architectures=["LlamaForCausalLMEagle3"],
vocab_size=64,
hidden_size=128,
intermediate_size=256,
num_hidden_layers=1,
num_attention_heads=2,
num_key_value_heads=2,
head_dim=64,
max_position_embeddings=128,
eagle_aux_hidden_state_layer_ids=[0],
).save_pretrained(directory)
generator = torch.Generator().manual_seed(23)
shapes = {
"fc.weight": (128, 128),
"midlayer.self_attn.q_proj.weight": (128, 256),
"midlayer.self_attn.k_proj.weight": (128, 256),
"midlayer.self_attn.v_proj.weight": (128, 256),
"midlayer.self_attn.o_proj.weight": (128, 128),
"midlayer.mlp.gate_proj.weight": (256, 128),
"midlayer.mlp.up_proj.weight": (256, 128),
"midlayer.mlp.down_proj.weight": (128, 256),
}
weights = {
name: torch.randn(shape, generator=generator) * 0.02
for name, shape in shapes.items()
}
for name in (
"norm",
"midlayer.input_layernorm",
"midlayer.hidden_norm",
"midlayer.post_attention_layernorm",
):
weights[f"{name}.weight"] = torch.ones(128)
save_file(weights, str(directory / "model.safetensors"))
@pytest.fixture
def launch(tmp_path):
fixture = tmp_path / "plugin"
fixture.mkdir()
shutil.copyfile(
Path(__file__).with_name("models") / "source_lifetime_plugin.py",
fixture / "source_lifetime_plugin.py",
)
metadata = fixture / "source_lifetime_fixture-1.0.dist-info"
metadata.mkdir()
(metadata / "METADATA").write_text(
"Metadata-Version: 2.1\nName: source-lifetime-fixture\nVersion: 1.0\n"
)
(metadata / "entry_points.txt").write_text(
"[tokenspeed.plugins]\nsource-lifetime-fixture = source_lifetime_plugin:register\n"
)
def start(source, *, with_draft, mode):
args = [
"--model",
str(source),
"--dist-init-addr",
f"127.0.0.1:{get_free_port()}",
"--dtype",
"bfloat16",
"--max-model-len",
"128",
"--max-num-seqs",
"4",
"--max-total-tokens",
"256",
"--chunked-prefill-size",
"64",
"--gpu-memory-utilization",
"0.2",
"--sampling-backend",
"greedy",
"--enable-output-logprobs",
"--disable-kvstore",
"--retraction-snapshot-ratio",
"0.5",
]
if with_draft:
args += [
"--speculative-algorithm",
"EAGLE3",
"--speculative-draft-model-path",
str(source / "draft"),
"--speculative-num-steps",
"1",
"--speculative-num-draft-tokens",
"2",
"--enforce-eager",
]
else:
args += ["--cudagraph-capture-sizes", "1", "2"]
return WireDriver(
args,
num_engines=1,
routing="round_robin",
startup_timeout_s=180,
env={
"PYTHONPATH": f"{fixture}:{os.environ.get('PYTHONPATH', '')}",
"TOKENSPEED_DISABLE_PLUGINS": ",".join(
ep.name for ep in entry_points(group="tokenspeed.plugins")
),
"SOURCE_TEST_METADATA": str(source.with_suffix(".json")),
"SOURCE_TEST_MODE": mode,
},
)
return start
def _generate(driver):
[result] = driver.generate(
[
generate_request(
[3, 4, 5],
{"temperature": 0.0, "max_new_tokens": 4, "ignore_eos": True},
return_logprob=True,
)
]
)
return result.output_ids, result.output_token_logprobs
@pytest.mark.parametrize("with_draft", [False, True], ids=["graphs", "draft"])
def test_post_model_init_source_lifetime(tmp_path, launch, with_draft):
original = tmp_path / "original"
_checkpoint(original)
if with_draft:
_draft_checkpoint(original / "draft")
bad = tmp_path / "bad"
bad.mkdir()
tensors = load_file(original / "model.safetensors")
tensors["model.embed_tokens.weight"] = torch.zeros(32, 128)
save_file(tensors, str(bad / "model.safetensors"))
outputs = []
for mode in ("keep", "retire"):
source = tmp_path / mode
shutil.copytree(original, source)
with launch(source, with_draft=with_draft, mode=mode) as driver:
assert source.exists() == (mode == "keep")
metadata = json.loads(source.with_suffix(".json").read_text())
assert metadata["has_draft"] == with_draft
outputs.append(_generate(driver))
[paused] = driver.control(PauseSchedulerReqInput(mode="wait"))
assert paused.success, paused.message
[rejected] = driver.control(
UpdateWeightFromDiskReqInput(model_path=str(bad))
)
assert not rejected.success and "has shape" in rejected.message
[loaded] = driver.control(
UpdateWeightFromDiskReqInput(model_path=str(original))
)
assert loaded.success, loaded.message
[resumed] = driver.control(ResumeSchedulerReqInput())
assert resumed.success, resumed.message
assert _generate(driver) == outputs[-1]
# Separate launches are not bitwise reproducible on every GPU: B200 CUDA
# graph decode logprobs differ across launches by ~4e-5. Within a launch,
# reloads are compared exactly above.
(keep_ids, keep_logprobs), (retire_ids, retire_logprobs) = outputs
assert retire_ids == keep_ids
assert retire_logprobs == pytest.approx(keep_logprobs, abs=1e-3)
def test_post_model_init_failure_stops_startup(tmp_path, launch, capfd):
source = tmp_path / "source"
_checkpoint(source)
with pytest.raises(RuntimeError, match="the engine (exited|did not register)"):
with launch(source, with_draft=False, mode="fail"):
pytest.fail("an engine with a failed hook must not serve")
assert "source lifetime hook refused startup" in capfd.readouterr().err