Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Decode: CUDA-graph replay for Qwen2 models (#16753)

Daniel Han73362477a86b
diff --git a/tests/test_decode_cuda_graphs.py b/tests/test_decode_cuda_graphs.py
index c6decab0e9f..4169c8e3e9d 100644
--- a/tests/test_decode_cuda_graphs.py
+++ b/tests/test_decode_cuda_graphs.py
@@ -18,6 +18,7 @@ cuda = pytest.mark.skipif(not torch.cuda.is_available(), reason = "CUDA only")
MODELS = (
"trl-internal-testing/tiny-LlamaForCausalLM-3.2",
"trl-internal-testing/tiny-Qwen3ForCausalLM",
+ "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5-Coder",
)
PROMPTS = [
"Once upon a time there was",
@@ -137,6 +138,26 @@ def test_graph_decode_is_bitwise_eager(name, load_in_4bit, bsz, monkeypatch):
assert torch.equal(eager[1], again[1])
+@cuda
[email protected]("bsz", [1, 4])
+def test_qwen2_sliding_window_graph_decode_is_bitwise_eager(bsz, monkeypatch):
+ """Past the sliding window, replay attends over the same trailing window as eager."""
+ model, tok = _load(MODELS[2], False)
+ for module in model.modules():
+ config = getattr(module, "config", None)
+ if config is not None and hasattr(config, "sliding_window"):
+ config.sliding_window = 16
+ enc = tok(PROMPTS[:bsz], return_tensors = "pt", padding = True).to("cuda")
+ eager = _generate(model, tok, enc, False, monkeypatch)
+ before = G.stats(_causal(model)).get("replays", 0)
+ graph = _generate(model, tok, enc, True, monkeypatch)
+ stats = G.stats(_causal(model))
+ assert stats["disabled"] is None, stats
+ assert stats["replays"] - before == 40 - 2
+ assert torch.equal(eager[0], graph[0])
+ assert torch.equal(eager[1], graph[1])
+
+
@cuda
def test_returned_cache_is_not_overwritten(monkeypatch):
"""A cache handed back by generate(return_dict_in_generate=True) keeps its values through a
diff --git a/tests/test_decode_graphs_memory.py b/tests/test_decode_graphs_memory.py
index fa465139b81..7d3ab0ca9e0 100644
--- a/tests/test_decode_graphs_memory.py
+++ b/tests/test_decode_graphs_memory.py
@@ -481,7 +481,7 @@ def test_keep_only_keeps_the_batch_size_decode_used(G):
assert [k[0] for k in state.graphs] == [4]
[email protected]("model_type", ["mistral", "qwen2", "gemma", "gemma2"])
[email protected]("model_type", ["mistral", "gemma", "gemma2"])
def test_a_model_type_that_never_graphs_keeps_eager_oom_and_planning(G, model_type):
model = _Model()
model.config.model_type = model_type
diff --git a/unsloth/models/_decode_graphs.py b/unsloth/models/_decode_graphs.py
index 08f1dde31ed..e5294f5a722 100644
--- a/unsloth/models/_decode_graphs.py
+++ b/unsloth/models/_decode_graphs.py
@@ -78,8 +78,8 @@ MAX_LEN_HINT = 0
# 1: size the graph KV cache for prompt + max_new_tokens up front (early stops still hold it all).
PREALLOC = os.environ.get("UNSLOTH_DECODE_GRAPH_PREALLOC", "0") == "1"
_SUPPORTED_ATTENTION = set()
-# Tested families only (Qwen2 shares the Llama attention but is untested here).
-_SUPPORTED_MODEL_TYPES = {"llama", "qwen3"}
+# Tested families only (Qwen2 reuses the Llama decode path, biases included).
+_SUPPORTED_MODEL_TYPES = {"llama", "qwen2", "qwen3"}
_PROJ_NAMES = ("q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj")