fix: resolve audited dispatch, kernel, and rollout bugs

- re-register the linear family with the operator dispatcher (ASTR_OPS / op_backend / resolve)
- fix bf16 gemv misaligned-address faults and element mispairing for offset weights
- reject misaligned bf16_swiglu inputs with a clear error and fall back in the backend gate
- make the rollout reuse decision, validation, and return atomic under one policy snapshot
- add the documented post-scoring rollout version check
- derive live+1 under the scheduler lock in optimizer_step via apply_weight_update(None, ...)
- reject rollout_max_policy_lag below rollout_interval - 1 at config time
- sync gemv stream-test inputs before switching streams; drop dead loader imports
This commit is contained in:
2026-09-03 16:58:14 +08:00
parent 736d1acb2e
commit 7e98a419a7
17 changed files with 485 additions and 49 deletions
+38
View File
@@ -130,6 +130,7 @@ def test_bf16_gemv_small_batch_fuses_bias():
def test_bf16_gemv_uses_current_stream():
x = torch.randn(1536, device="cuda", dtype=torch.bfloat16)
weight = torch.randn(256, 1536, device="cuda", dtype=torch.bfloat16)
torch.cuda.synchronize()
stream = torch.cuda.Stream()
with torch.cuda.stream(stream):
actual = bf16_gemv(x, weight)
@@ -171,6 +172,43 @@ def test_bf16_gemv_handles_unaligned_k(n, k):
torch.testing.assert_close(actual3, F.linear(x3, weight), rtol=0.02, atol=0.5)
@skip_no_gemv
@pytest.mark.parametrize("m", [1, 2, 3, 4])
def test_bf16_gemv_accepts_complementary_misalignment(m):
"""Misaligned weight rows plus an x base chosen so the vectorized branch
is entered with a non-16B-aligned ``x`` pointer (regression: the branch
guard checked ``x + whead`` alignment but the uint4 view was rooted at
``x`` itself, faulting with a misaligned-address CUDA error)."""
torch.manual_seed(37)
n, k = 256, 1536
# offset 5 elements = +10 bytes: weight rows land at 10 % 16 (whead=3)
# and x at 10 % 16, so (x + 2*whead) % 16 == 0 selects the fast path.
big_w = torch.randn(n * k + 8, device="cuda", dtype=torch.bfloat16)
weight = big_w[5 : 5 + n * k].view(n, k)
big_x = torch.randn(m * k + 8, device="cuda", dtype=torch.bfloat16)
x = big_x[5 : 5 + m * k].view(m, k) if m > 1 else big_x[5 : 5 + k]
assert (x.data_ptr() & 15) == 10 and (weight.data_ptr() & 15) == 10
actual = bf16_gemv(x, weight)
expected = F.linear(x, weight)
torch.testing.assert_close(actual, expected, rtol=0.02, atol=0.5 if m > 1 else 0.25)
@skip_no_gemv
def test_bf16_gemv_scalar_path_handles_misaligned_weight_only():
"""Weight rows misaligned while x stays 16B-aligned take the scalar-x
middle and must stay exact."""
torch.manual_seed(41)
n, k = 256, 1536
big_w = torch.randn(n * k + 8, device="cuda", dtype=torch.bfloat16)
weight = big_w[5 : 5 + n * k].view(n, k)
x = torch.randn(2, k, device="cuda", dtype=torch.bfloat16)
assert (weight.data_ptr() & 15) == 10 and (x.data_ptr() & 15) == 0
actual = bf16_gemv(x, weight)
torch.testing.assert_close(actual, F.linear(x, weight), rtol=0.02, atol=0.5)
@skip_no_gemv
def test_bf16_gemv_small_batch_cuda_graph_replay():
torch.manual_seed(31)
+58
View File
@@ -7,6 +7,7 @@ import torch.nn.functional as F
from astrai.extension import is_available, linear
from astrai.extension.backend import linear as public_linear
from astrai.extension.dispatch import explain, op_backend, resolve
# The package attribute ``linear`` is the dispatched function; reach the
# module object explicitly for monkeypatching its private helpers.
@@ -166,3 +167,60 @@ def test_dispatched_linear_cuda_graph_replay(monkeypatch):
graph.replay()
expected = F.linear(x, weight)
torch.testing.assert_close(actual, expected, rtol=0.02, atol=0.25)
def test_linear_family_is_registered_with_shared_dispatcher():
from astrai.extension.dispatch import _FAMILIES
assert "linear" in _FAMILIES
x = torch.randn(2, 8)
weight = torch.randn(4, 8)
resolution = resolve("linear", x, weight)
assert resolution.record.family == "linear"
assert resolution.origin in ("chain", "fallback")
assert "linear" in explain("linear", x, weight)
@skip_no_gemv
def test_ops_env_override_forces_torch_for_capable_call(monkeypatch):
"""ASTR_OPS=linear=torch must keep working after the M-band rewrite
(regression: the family was silently dropped from the dispatcher, so
the override warned, fell through, and the gemv kernel still ran)."""
monkeypatch.setenv("ASTR_OPS", "linear=torch")
x = torch.randn(2, 1536, device="cuda", dtype=torch.bfloat16)
weight = torch.randn(1536, 1536, device="cuda", dtype=torch.bfloat16)
with torch.no_grad():
assert not _routes_to_gemv(monkeypatch, x, weight)
torch.testing.assert_close(
linear(x, weight), F.linear(x, weight), rtol=0.02, atol=0.25
)
@skip_no_gemv
def test_ops_env_override_forces_gemv(monkeypatch):
monkeypatch.setenv("ASTR_OPS", "linear=gemv")
x = torch.randn(1, 1536, device="cuda", dtype=torch.bfloat16)
weight = torch.randn(256, 1536, device="cuda", dtype=torch.bfloat16)
with torch.no_grad():
# M=1 is outside the auto band but inside the forced gemv record.
assert _routes_to_gemv(monkeypatch, x, weight)
@skip_no_gemv
def test_op_backend_context_selects_torch(monkeypatch):
monkeypatch.setenv("ASTRAI_GEMV", "1")
x = torch.randn(2, 1536, device="cuda", dtype=torch.bfloat16)
weight = torch.randn(256, 1536, device="cuda", dtype=torch.bfloat16)
with torch.no_grad(), op_backend(linear="torch"):
assert not _routes_to_gemv(monkeypatch, x, weight)
torch.testing.assert_close(
linear(x, weight), F.linear(x, weight), rtol=0.02, atol=0.25
)
# The override is scoped: the forced mode applies again afterwards.
with torch.no_grad():
assert _routes_to_gemv(monkeypatch, x, weight)
def test_op_backend_rejects_unknown_linear_handle():
with pytest.raises(ValueError, match="Unknown linear implementation"):
op_backend(linear="nonexistent").__enter__()
+13
View File
@@ -97,3 +97,16 @@ def test_bf16_swiglu_uses_current_stream_and_cuda_graph():
def test_bf16_swiglu_rejects_unsupported_inputs(make_args, error):
with pytest.raises(RuntimeError, match=error):
bf16_swiglu(*make_args())
@skip_no_swiglu
def test_bf16_swiglu_rejects_misaligned_storage_with_clear_error():
"""Contiguous-but-offset views must fail the wrapper's TORCH_CHECK with
an actionable message instead of a sticky CUDA misaligned-address error
(regression: the kernel casts x directly to uint4 without checking)."""
k = 1536
base = torch.randn(k + 1, device="cuda", dtype=torch.bfloat16)
x = base[1:] # +2 bytes: contiguous but not 16B-aligned
weights = torch.randn(8, k, device="cuda", dtype=torch.bfloat16)
with pytest.raises(RuntimeError, match="16-byte aligned"):
bf16_swiglu(x, weights, weights)
+19
View File
@@ -95,3 +95,22 @@ def test_auto_uses_unfused_chain_until_shape_is_qualified(monkeypatch):
actual = swiglu(x, up_weight, gate_weight)
expected = reference_swiglu(x, up_weight, gate_weight)
torch.testing.assert_close(actual, expected, rtol=0.03, atol=0.1)
@skip_no_swiglu
def test_mode_one_falls_back_for_misaligned_storage(monkeypatch):
"""Contiguous-but-offset views must route to the unfused torch chain
even with ASTRAI_SWIGLU=1 instead of reaching the uint4-only kernel
(regression: the fused primitive faulted with a misaligned-address
CUDA error for such inputs)."""
monkeypatch.setenv("ASTRAI_SWIGLU", "1")
k = 1536
x_base = torch.randn(2 * k + 8, device="cuda", dtype=torch.bfloat16) * 0.1
x = x_base[1 : 1 + 2 * k].view(2, k)
assert x.is_contiguous() and (x.data_ptr() & 15) != 0
up_weight = torch.randn(64, k, device="cuda", dtype=torch.bfloat16) * 0.02
gate_weight = torch.randn_like(up_weight)
with torch.no_grad():
actual = swiglu(x, up_weight, gate_weight)
expected = reference_swiglu(x, up_weight, gate_weight)
torch.testing.assert_close(actual, expected, rtol=0.03, atol=0.01)
+26
View File
@@ -583,6 +583,32 @@ def test_scheduler_applies_weight_mutation_and_version_atomically(device):
with pytest.raises(RuntimeError, match="optimizer failed"):
scheduler.apply_weight_update(2, failed_mutation)
assert scheduler.policy_version == 1
# None derives live+1 under the lock: no read-compute-write race
# on the current version for advance-by-one callers.
assert scheduler.apply_weight_update(None, mutate) == "updated"
assert scheduler.policy_version == 2
finally:
scheduler.stop()
def test_scheduler_atomic_advance_survives_interleaved_publish(device):
"""A concurrent publish between reading the live version and applying
the update must not fail ``require_advance`` (regression: callers
computed live+1 outside the lock, a TOCTOU that raised spuriously)."""
scheduler, _tok, _model = _make_real_scheduler(device)
try:
# Simulate the race directly: a version read that goes stale before
# apply_weight_update acquires the lock. With None the scheduler
# re-derives live+1 inside the critical section.
stale_read = scheduler.policy_version + 1
scheduler.update_weights(1)
assert stale_read == 1 # now equals live -> explicit form would raise
with pytest.raises(ValueError, match="must advance"):
scheduler.apply_weight_update(stale_read, lambda: "ok")
assert scheduler.apply_weight_update(None, lambda: "ok") == "ok"
assert scheduler.policy_version == 2
finally:
scheduler.stop()
+35
View File
@@ -144,3 +144,38 @@ def test_online_rollout_end_to_end(
checkpoint = Checkpoint.load(checkpoint_dir)
assert checkpoint.meta["policy_version"] == 2
assert len(created_reference_models) == 1
def _minimal_online_config(**overrides):
"""A TrainConfig for online GRPO that only needs field overrides."""
defaults = dict(
strategy="online_grpo",
model_fn=lambda: torch.nn.Linear(2, 2),
dataset=InstructionDataset(),
optimizer_fn=lambda m: torch.optim.SGD(m.parameters(), lr=0.0),
scheduler_fn=lambda o: SchedulerFactory.create(
"cosine", o, warmup_steps=1, lr_decay_steps=4, min_rate=0.05
),
reward_model_fn=LengthRewardModel,
)
defaults.update(overrides)
return TrainConfig(**defaults)
def test_online_config_rejects_contradictory_policy_lag():
"""rollout_max_policy_lag below rollout_interval - 1 guarantees a fatal
RolloutVersionError mid-training; it must fail at config time instead."""
with pytest.raises(ValueError, match="rollout_max_policy_lag=0"):
_minimal_online_config(rollout_interval=3, rollout_max_policy_lag=0)
# lag == interval - 1 (including the derived default) stays valid.
config = _minimal_online_config(rollout_interval=3, rollout_max_policy_lag=2)
assert config.rollout_max_policy_lag == 2
config = _minimal_online_config(rollout_interval=3)
assert config.rollout_max_policy_lag is None
# Offline strategies never consult the rollout window.
config = _minimal_online_config(
strategy="sft", rollout_interval=3, rollout_max_policy_lag=0
)
assert config.rollout_max_policy_lag == 0
+3
View File
@@ -62,6 +62,9 @@ class _RecordingRunner:
def apply_weight_update(self, policy_version, update):
result = update()
if policy_version is None:
# Mirror the scheduler: None derives live+1 under the lock.
policy_version = self.policy_version + 1
self.update_weights(policy_version)
return result
+53 -1
View File
@@ -459,7 +459,7 @@ def test_rollout_runner_publishes_cache_before_concurrent_policy_update(device):
nonlocal validation_calls
validation_calls += 1
original_validate(result, live_version=live_version)
if validation_calls == 2:
if validation_calls == 3:
final_validation_started.set()
assert allow_final_validation_to_finish.wait(timeout=5)
@@ -503,6 +503,58 @@ def test_rollout_runner_derives_default_policy_lag_from_interval(device):
assert runner.max_policy_lag == 3
def _interleave_before_snapshot(runner, callback):
"""Wrap ``with_policy_snapshot`` so ``callback`` runs just before a
named snapshot callback enters the generator/scheduler locks."""
original_snapshot = runner.generator.with_policy_snapshot
def wrapper(inspect):
if inspect.__name__ == "reuse":
callback()
return original_snapshot(inspect)
runner.generator.with_policy_snapshot = wrapper
def test_rollout_runner_reuse_reads_cache_inside_the_snapshot(device):
"""The reuse decision must observe the cache under the policy snapshot
(regression: the cache was read outside the lock, so a concurrent
commit between the read and the lock silently handed the trainer a
stale rollout — a lost update)."""
import dataclasses
runner, _ = _make_runner(device, rollout_interval=100)
batch = _make_instruction_batch(n=1)
first, _ = runner(batch)
assert first.policy_version == 0
def concurrent_refresh():
runner.update_weights(1)
runner._cache = dataclasses.replace(first, policy_version=1)
runner._steps_since_rollout = 0
_interleave_before_snapshot(runner, concurrent_refresh)
result, fresh = runner(batch)
assert fresh is False
assert result is not first
assert result.policy_version == 1
def test_rollout_runner_recovers_when_cache_cleared_before_reuse_snapshot(device):
"""A cache clear between the reuse decision and the snapshot must
trigger a fresh rollout instead of an assertion failure (regression:
``assert cached is not None`` fired because the object was captured
outside the lock)."""
runner, _ = _make_runner(device, rollout_interval=100)
batch = _make_instruction_batch(n=1)
first, _ = runner(batch)
_interleave_before_snapshot(runner, runner.clear_cache)
result, fresh = runner(batch)
assert fresh is True
assert result is not first
@pytest.mark.parametrize(
("kwargs", "message"),
[