[Perf][DSv4.1] Trim the last KV-source layer in decoder replay (#59894)
Signed-off-by: Canlin <canlinguosdu@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Juntian Liu <Juntianl777@gmail.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
Juntian Liu
parent
8cbd5d0300
commit
ad73a4740d
@@ -67,10 +67,8 @@ def _states(num_tokens: int):
|
||||
)
|
||||
|
||||
|
||||
def test_run_gathers_states_and_realigns_shared_indexer_buffers():
|
||||
topk = torch.arange(NUM_TOKENS * 4, device=DEVICE).view(NUM_TOKENS, 4).int()
|
||||
candidates = torch.arange(NUM_TOKENS * 3, device=DEVICE).view(NUM_TOKENS, 3).int()
|
||||
topk_before, candidates_before = topk.clone(), candidates.clone()
|
||||
def test_run_gathers_states():
|
||||
"""Every row's KV is written first; the layers then run on the replay rows."""
|
||||
seen = {}
|
||||
|
||||
def run_layers(hidden_states, positions, input_ids, pre_mix, post, res, residual):
|
||||
@@ -80,7 +78,8 @@ def test_run_gathers_states_and_realigns_shared_indexer_buffers():
|
||||
seen["is_padding"] = replay_context.is_padding
|
||||
return residual, pre_mix
|
||||
|
||||
layers = DecoderReplayLayers(WINDOW, run_layers, [topk, candidates], set())
|
||||
write_batch_kv = MagicMock()
|
||||
layers = DecoderReplayLayers(WINDOW, run_layers, write_batch_kv, set(), "swa")
|
||||
states = _states(NUM_TOKENS)
|
||||
hidden, residual = states[0], states[-1]
|
||||
full, sub = object(), object()
|
||||
@@ -90,11 +89,10 @@ def test_run_gathers_states_and_realigns_shared_indexer_buffers():
|
||||
outputs = layers(*states)
|
||||
assert get_forward_context() is context
|
||||
|
||||
write_batch_kv.assert_called_once_with(*states)
|
||||
rows = torch.tensor(REPLAY_ROWS, device=DEVICE)
|
||||
assert torch.equal(seen["hidden_states"], hidden[rows])
|
||||
assert seen["attn_metadata"] is sub and seen["is_padding"] is None
|
||||
assert torch.equal(topk[: len(REPLAY_ROWS)], topk_before[rows])
|
||||
assert torch.equal(candidates[: len(REPLAY_ROWS)], candidates_before[rows])
|
||||
assert len(outputs) == 2 and outputs[0].shape == residual.shape
|
||||
assert torch.equal(outputs[0][rows], residual[rows])
|
||||
assert outputs[0][1:173].abs().sum() == 0
|
||||
@@ -107,12 +105,14 @@ def test_no_replay_batch_runs_the_whole_batch():
|
||||
seen["attn_metadata"] = get_forward_context().attn_metadata
|
||||
return (hidden_states,)
|
||||
|
||||
layers = DecoderReplayLayers(WINDOW, run_layers, [], set())
|
||||
write_batch_kv = MagicMock()
|
||||
layers = DecoderReplayLayers(WINDOW, run_layers, write_batch_kv, set(), "swa")
|
||||
states = _states(NUM_TOKENS)
|
||||
full = object()
|
||||
with override_forward_context(_context(full)):
|
||||
(output,) = layers(*states)
|
||||
assert output is states[0] and seen["attn_metadata"] is full
|
||||
write_batch_kv.assert_not_called()
|
||||
|
||||
|
||||
def _graph_context(num_tokens):
|
||||
@@ -125,6 +125,25 @@ def _graph_context(num_tokens):
|
||||
)
|
||||
|
||||
|
||||
def test_piecewise_graphs_break_out_by_size():
|
||||
"""A PIECEWISE graph's KV write and break are fixed at capture, when no replay
|
||||
batch is set, so both follow the graph's size rather than the replay batch."""
|
||||
write_batch_kv = MagicMock()
|
||||
layers = DecoderReplayLayers(
|
||||
WINDOW, lambda *states: ("whole",), write_batch_kv, set(), "swa"
|
||||
)
|
||||
layers._run_in_graph_break = lambda *states: ("break",) # type: ignore[method-assign]
|
||||
states = _states(NUM_TOKENS)
|
||||
with override_forward_context(_graph_context(1024)):
|
||||
assert layers(*states) == ("whole",) # no threshold: no break
|
||||
layers.trim_threshold = 768
|
||||
assert layers(*states) == ("break",)
|
||||
_set_replay_batch(layers, REPLAY_ROWS, object())
|
||||
with override_forward_context(_graph_context(576)):
|
||||
assert not layers.uses_replay_batch() # a replay batch alone does not break out
|
||||
write_batch_kv.assert_called_once_with(*states)
|
||||
|
||||
|
||||
def test_replay_graph_matches_eager(monkeypatch):
|
||||
"""The replay graph of the next captured size pads and runs the rows."""
|
||||
monkeypatch.setattr(cudagraph_utils, "get_pp_group", MagicMock)
|
||||
@@ -142,7 +161,7 @@ def test_replay_graph_matches_eager(monkeypatch):
|
||||
def run_layers(hidden, positions, ids, pre, post, mix, residual):
|
||||
return residual + hidden[:, None] + positions[:, None, None], pre + ids[:, None]
|
||||
|
||||
layers = DecoderReplayLayers(4, run_layers, [], set())
|
||||
layers = DecoderReplayLayers(4, run_layers, MagicMock(), set(), "swa")
|
||||
manager = DecoderReplayCudaGraphManager(cfg, DEVICE, layers)
|
||||
|
||||
def prepare(desc):
|
||||
|
||||
Reference in New Issue
Block a user