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.

31.2 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
"""Unsloth's rotary caches must be built only on the device that reads them.

llama.py needs an accelerator to import, so the classes are `ast`-extracted and run against a
CPU-pinned prelude in which device index i is cpu:i.
"""

from __future__ import annotations

import ast
import math
import pathlib
import types

import pytest
import torch
from real_accelerator import has_real_cuda

REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent
LLAMA = REPO_ROOT / "unsloth" / "models" / "llama.py"
GEMMA = REPO_ROOT / "unsloth" / "models" / "gemma.py"

DEVICES = 4
DIM = 8
BASE = 10000


class _CPUTorch:
    """`torch`, with a bare-int device pinned to CPU so the test runs anywhere."""

    def __getattr__(self, name):
        return getattr(torch, name)

    def device(
        self,
        spec,
        index = None,
    ):
        if isinstance(spec, int) and not isinstance(spec, bool):
            return torch.device("cpu", spec)
        return torch.device(spec) if index is None else torch.device(spec, index)

    def empty(self, *args, **kwargs):
        kwargs["device"] = "cpu"
        return torch.empty(*args, **kwargs)


_HELPERS = ("_round_rope_size", "_PerDeviceRopeCache")


def _load(
    path,
    names,
    extra_ns = None,
):
    tree = ast.parse(path.read_text(encoding = "utf-8"))
    nodes = []
    for name in names:
        node = next(
            (
                n
                for n in ast.walk(tree)
                if isinstance(n, (ast.ClassDef, ast.FunctionDef)) and n.name == name
            ),
            None,
        )
        if node is None:
            # Helpers only exist once the cache is per device; the classes must always exist.
            if name in _HELPERS:
                continue
            pytest.fail(
                f"{name} is gone from {path.name}; repoint this test rather than deleting it."
            )
        nodes.append(node)
    ns = {
        "torch": _CPUTorch(),
        "math": math,
        "DEVICE_COUNT": DEVICES,
        "DEVICE_TYPE_TORCH": "cpu",
        "get_current_device": lambda: 0,
        "is_bfloat16_supported": lambda: False,
        "_get_rope_theta": lambda config, default = 10000: default,
        "_compute_config_rope_inv_freq": lambda config, rope_scaling: (None, 1.0),
        "_rope_scaling_as_dict": lambda value: dict(value or {}),
    }
    ns.update(extra_ns or {})
    exec(compile(ast.Module(body = nodes, type_ignores = []), str(path), "exec"), ns)
    return ns


def _llama_ns():
    return _load(
        LLAMA,
        list(_HELPERS) + ["LlamaRotaryEmbedding", "LongRopeRotaryEmbedding"],
    )


def _built(cache):
    return [i for i, c in enumerate(cache) if c is not None]


def _x(device_index, dtype = torch.float32):
    # extend_rope_embedding / forward only read .device and .dtype.
    return types.SimpleNamespace(device = torch.device("cpu", device_index), dtype = dtype)


def _reference(rows, dtype = torch.float32):
    inv_freq = 1.0 / (BASE ** (torch.arange(0, DIM, 2, dtype = torch.int64).float() / DIM))
    t = torch.arange(rows, dtype = torch.int64).float()
    emb = torch.cat((torch.outer(t, inv_freq),) * 2, dim = -1)
    return emb.cos().to(dtype), emb.sin().to(dtype)


def _llama(max_position):
    return _llama_ns()["LlamaRotaryEmbedding"](
        dim = DIM, max_position_embeddings = max_position, base = BASE
    )


def test_init_builds_no_device():
    rope = _llama(1 << 20)
    assert _built(rope.multi_gpu_cos_cached) == []
    assert _built(rope.multi_gpu_sin_cached) == []


def test_get_cached_builds_only_the_requested_device():
    rope = _llama(1 << 20)
    cos, sin = rope.get_cached(100, 2)
    assert _built(rope.multi_gpu_cos_cached) == [2]
    ref_cos, ref_sin = _reference(cos.shape[0])
    assert cos.shape[0] == 4 * 8192
    assert torch.equal(cos, ref_cos) and torch.equal(sin, ref_sin)


def test_model_forward_extend_builds_needed_rows_on_one_device():
    # LlamaModel_fast_forward: extend to max_position_embeddings, materialise this batch's rows.
    max_position = 1 << 20
    rope = _llama(max_position)
    x = _x(1, torch.float16)
    rope.extend_rope_embedding(x, max_position, materialize_len = 40000)
    # Size and dtype move exactly as the eager extend left them.
    assert rope.current_rope_size == max_position
    assert _built(rope.multi_gpu_cos_cached) == [1]
    cos, sin = rope.get_cached(40000, 1)
    # 40000 rounded up to the 8192 growth step, not 1M rows.
    assert cos.shape[0] == 40960
    assert cos.dtype == torch.float16
    ref_cos, ref_sin = _reference(40960, torch.float16)
    assert torch.equal(cos, ref_cos) and torch.equal(sin, ref_sin)


def test_extend_grows_used_device_and_stale_peers_rebuild_lazily():
    rope = _llama(1 << 17)
    rope.get_cached(10, 0)
    rope.get_cached(10, 3)
    rope.extend_rope_embedding(_x(3, torch.float16), 50000)
    assert rope.current_rope_size == 57344
    assert rope.multi_gpu_cos_cached[3].shape[0] == 57344
    assert rope.multi_gpu_cos_cached[3].dtype == torch.float16
    # Device 0 is untouched until it is read, then matches what the eager rebuild produced.
    assert rope.multi_gpu_cos_cached[0].shape[0] == 4 * 8192
    cos, sin = rope.get_cached(50000, 0)
    ref_cos, ref_sin = _reference(57344, torch.float16)
    assert torch.equal(cos, ref_cos) and torch.equal(sin, ref_sin)
    assert _built(rope.multi_gpu_cos_cached) == [0, 3]


def test_get_cached_materialises_the_length_it_is_asked_for():
    # Gemma2 / Cohere decode steps read get_cached(kv_seq_len) with no extend first, past the rows
    # the model forward built for max_seq_length.
    rope = _llama(1 << 17)
    rope.extend_rope_embedding(_x(0), 1 << 17, materialize_len = 2048)
    assert rope.multi_gpu_cos_cached[0].shape[0] == 4 * 8192
    cos, sin = rope.get_cached(40000, 0)
    assert cos.shape[0] >= 40000
    ref_cos, ref_sin = _reference(cos.shape[0])
    # ARM trig vectorization can round differently across cache chunk sizes.
    torch.testing.assert_close(cos, ref_cos, rtol = 0, atol = 2 * torch.finfo(cos.dtype).eps)
    torch.testing.assert_close(sin, ref_sin, rtol = 0, atol = 2 * torch.finfo(sin.dtype).eps)


def test_forward_builds_only_input_device():
    rope = _llama(4096)
    cos, sin = rope(_x(2), seq_len = 100)
    assert _built(rope.multi_gpu_cos_cached) == [2]
    ref_cos, ref_sin = _reference(100)
    assert torch.equal(cos, ref_cos) and torch.equal(sin, ref_sin)


def test_longrope_builds_only_the_requested_device():
    rope = _llama_ns()["LongRopeRotaryEmbedding"](
        dim = DIM,
        max_position_embeddings = 128,
        original_max_position_embeddings = 16,
        short_factor = [1.0] * (DIM // 2),
        long_factor = [2.0] * (DIM // 2),
    )
    assert _built(rope.multi_gpu_short_cos_cached) == []
    rope.get_cached(8, 1)
    assert _built(rope.multi_gpu_short_cos_cached) == [1]
    assert _built(rope.multi_gpu_long_cos_cached) == []
    rope.extend_rope_embedding(_x(1, torch.float16), 100)
    assert _built(rope.multi_gpu_long_cos_cached) == [1]
    assert _built(rope.multi_gpu_short_cos_cached) == [1]


def test_gemma_builds_only_the_requested_device():
    llama_ns = _llama_ns()
    if "_PerDeviceRopeCache" not in llama_ns:
        pytest.fail("Gemma's rotary cache is built for every device")
    ns = _load(
        GEMMA,
        ["GemmaFixedRotaryEmbedding"],
        {"_PerDeviceRopeCache": llama_ns["_PerDeviceRopeCache"]},
    )
    rope = ns["GemmaFixedRotaryEmbedding"](dim = DIM, max_position_embeddings = 1 << 20, base = BASE)
    assert _built(rope.multi_gpu_cos_cached) == []
    rope.extend_rope_embedding(_x(3, torch.bfloat16), 70000)
    assert _built(rope.multi_gpu_cos_cached) == [3]
    assert rope.multi_gpu_cos_cached[3].shape[0] == 73728
    # Gemma keeps its cache in float32 whatever the input dtype.
    assert rope.multi_gpu_cos_cached[3].dtype == torch.float32


def test_granite_decode_reads_rows_past_max_seq_length():
    # GraniteModel_fast_forward_inference indexes the cache it fetches once per step at the decode
    # position, which runs past max_seq_length; the eager cache held max_position_embeddings rows.
    tree = ast.parse((REPO_ROOT / "unsloth" / "models" / "granite.py").read_text(encoding = "utf-8"))
    fn = next(
        n
        for n in ast.walk(tree)
        if isinstance(n, ast.FunctionDef) and n.name == "GraniteModel_fast_forward_inference"
    )
    call = next(
        n
        for n in ast.walk(fn)
        if isinstance(n, ast.Call)
        and isinstance(n.func, ast.Attribute)
        and n.func.attr == "get_cached"
    )
    max_seq_length, seq_len, q_len = 32768, 32790, 1
    rows = eval(
        compile(ast.Expression(call.args[0]), "granite.py", "eval"),
        {"max": max},
        {
            "self": types.SimpleNamespace(max_seq_length = max_seq_length),
            "seq_len": seq_len,
            "q_len": q_len,
            "rotary_seq_len": seq_len + q_len,
        },
    )
    rope = _llama(1 << 17)
    # Prefill of a 32760 token prompt through LlamaModel_fast_forward.
    rope.extend_rope_embedding(_x(0), 1 << 17, materialize_len = max(32760, max_seq_length))
    cos, sin = rope.get_cached(rows, 0)
    assert cos.shape[0] > seq_len
    ref_cos, _ = _reference(cos.shape[0])
    assert torch.equal(cos[seq_len], ref_cos[seq_len])


def test_covered_reads_still_rebuild_a_device_left_stale_by_growth():
    rope = _llama(1 << 17)
    rope.get_cached(10, 1)
    rope.extend_rope_embedding(_x(0, torch.float16), 50000)
    # Device 1 already holds 32768 rows covering 10, but growth changed the dtype.
    cos, _ = rope.get_cached(10, 1)
    assert cos.dtype == torch.float16 and cos.shape[0] == 57344
    ref_cos, _ = _reference(57344, torch.float16)
    assert torch.equal(cos, ref_cos)
    rope.extend_rope_embedding(_x(1, torch.float16), 11)
    assert rope.multi_gpu_cos_cached[1] is cos


def _model_forward_rope(
    rope,
    seq_length_with_past,
    position_ids,
    max_position,
    max_seq_length = 2048,
):
    # Runs the rotary block of LlamaModel_fast_forward as written in llama.py.
    tree = ast.parse(LLAMA.read_text(encoding = "utf-8"))
    fn = next(
        n
        for n in ast.walk(tree)
        if isinstance(n, ast.FunctionDef) and n.name == "LlamaModel_fast_forward"
    )
    block = next(
        n
        for n in fn.body
        if isinstance(n, ast.If) and "IS_ATTENTION_REFACTOR" in ast.unparse(n.test)
    )
    scope = {
        "self": types.SimpleNamespace(
            rotary_emb = rope,
            max_seq_length = max_seq_length,
            config = types.SimpleNamespace(max_position_embeddings = max_position),
        ),
        "hidden_states": _x(0, torch.float16),
        "seq_length_with_past": seq_length_with_past,
        "position_ids": position_ids,
        "IS_ATTENTION_REFACTOR": True,
        "IS_GRANITE": False,
        "torch": torch,
        # isinstance needs the class this rope was built from; other ropes never match.
        "LongRopeRotaryEmbedding": type(rope)
        if type(rope).__name__ == "LongRopeRotaryEmbedding"
        else type("LongRopeRotaryEmbedding", (), {}),
        "max": max,
        "getattr": getattr,
    }
    exec(compile(ast.Module(body = [block], type_ignores = []), str(LLAMA), "exec"), scope)
    return scope["position_embeddings"]


def test_model_forward_covers_explicit_offset_position_ids():
    # Valid positions past the batch length (offsets, generate, packing) must index real rows.
    max_position = 1 << 17
    position_ids = torch.tensor([[40000, 40001]])
    cos, sin = _model_forward_rope(_llama(max_position), 2, position_ids, max_position)
    assert cos.shape[0] >= int(position_ids.max()) + 1
    ref_cos, ref_sin = _reference(cos.shape[0], torch.float16)
    assert torch.equal(cos[position_ids], ref_cos[position_ids])
    assert torch.equal(sin[position_ids], ref_sin[position_ids])


def test_model_forward_without_position_ids_stays_short():
    # Implicit positions are 0..seq_length_with_past-1, so the short cache is enough.
    max_position = 1 << 20
    rope = _llama(max_position)
    cos, _ = _model_forward_rope(rope, 2, None, max_position)
    assert cos.shape[0] == 4 * 8192
    assert _built(rope.multi_gpu_cos_cached) == [0]


def test_starting_cache_is_computed_on_cpu():
    # The eager __init__ computed the starting cache on CPU even when loading later moved inv_freq to
    # an accelerator; computing it there instead raises the first forward's peak memory.
    rope = _llama(4096)
    rope.inv_freq = rope.inv_freq.to("meta")
    cos, sin = rope.get_cached(10, 0)
    ref_cos, ref_sin = _reference(4096)
    assert torch.equal(cos, ref_cos) and torch.equal(sin, ref_sin)


def test_model_forward_position_ids_keep_longrope_short_factors():
    # Building every row for explicit positions must not switch a short input to the long factors.
    rope = _llama_ns()["LongRopeRotaryEmbedding"](
        dim = DIM,
        max_position_embeddings = 128,
        original_max_position_embeddings = 16,
        short_factor = [1.0] * (DIM // 2),
        long_factor = [2.0] * (DIM // 2),
    )
    cos, sin = _model_forward_rope(rope, 4, torch.tensor([[0, 1, 2, 3]]), 128, max_seq_length = 8)
    assert cos is rope.multi_gpu_short_cos_cached[0]
    assert sin is rope.multi_gpu_short_sin_cached[0]


def _longrope(max_position, original_max_position):
    return _llama_ns()["LongRopeRotaryEmbedding"](
        dim = DIM,
        max_position_embeddings = max_position,
        original_max_position_embeddings = original_max_position,
        short_factor = [1.0] * (DIM // 2),
        long_factor = [2.0] * (DIM // 2),
    )


def test_model_forward_longrope_offset_positions_match_transformers():
    # transformers picks LongRoPE's factors from max(position_ids) + 1, so 4 tokens at 40000 with a 4096
    # pretrained window use the long factors; the short cache has no row 40000.
    from transformers import Phi3Config
    from transformers.models.phi3.modeling_phi3 import Phi3RotaryEmbedding

    max_position, original_max_position = 1 << 17, 4096
    position_ids = torch.tensor([[40000, 40001, 40002, 40003]])
    rope = _longrope(max_position, original_max_position)
    cos, sin = _model_forward_rope(rope, 4, position_ids, max_position)
    assert cos.shape[0] > int(position_ids.max())

    config = Phi3Config(
        hidden_size = DIM,
        num_attention_heads = 1,
        num_key_value_heads = 1,
        max_position_embeddings = max_position,
        original_max_position_embeddings = original_max_position,
        rope_scaling = {
            "type": "longrope",
            "short_factor": [1.0] * (DIM // 2),
            "long_factor": [2.0] * (DIM // 2),
        },
    )
    ref_cos, ref_sin = Phi3RotaryEmbedding(config)(torch.zeros(1, 4, DIM), position_ids)
    torch.testing.assert_close(cos[position_ids[0]].float(), ref_cos[0], atol = 2e-3, rtol = 0)
    torch.testing.assert_close(sin[position_ids[0]].float(), ref_sin[0], atol = 2e-3, rtol = 0)


@pytest.mark.parametrize("start", [0, 40000])
def test_model_forward_longrope_under_compile_matches_eager(monkeypatch, start):
    # While tracing, the choice still follows max(position_ids) + 1, so compiled runs match eager.
    position_ids = torch.arange(start, start + 4)[None]
    eager_cos, eager_sin = _model_forward_rope(_longrope(1 << 17, 4096), 4, position_ids, 1 << 17)
    monkeypatch.setattr(torch.compiler, "is_compiling", lambda: True)
    cos, sin = _model_forward_rope(_longrope(1 << 17, 4096), 4, position_ids, 1 << 17)
    assert torch.equal(cos[position_ids[0]], eager_cos[position_ids[0]])
    assert torch.equal(sin[position_ids[0]], eager_sin[position_ids[0]])


def test_first_grown_cache_is_computed_on_inv_freq_device():
    # The lazy build must run on inv_freq's device, as the eager code did, since a CPU build rounds
    # cos/sin differently from every earlier release.
    rope = _llama(1 << 17)
    rope.inv_freq = rope.inv_freq.to("meta")
    with pytest.raises(NotImplementedError):
        _model_forward_rope(rope, 2, None, 1 << 17)


@pytest.mark.parametrize("rows", [40, 64])
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
def test_cache_built_in_chunks_matches_one_shot(rows, dtype):
    # Building row chunks into the output must keep every row bit-identical to the one-shot build.
    rope = _llama(rows)
    rope._rope_build_chunk_rows = 16
    cos, sin = rope._set_cos_sin_cache(rows, torch.device("cpu", 0), dtype)
    ref_cos, ref_sin = _reference(rows, dtype)
    assert cos.shape == ref_cos.shape and cos.dtype == dtype
    assert torch.equal(cos, ref_cos) and torch.equal(sin, ref_sin)


def _assert_trainable(*tensors):
    for t in tensors:
        assert not t.is_inference()
    cos = tensors[0]
    x = torch.ones(cos.shape, dtype = cos.dtype, requires_grad = True)
    (x * cos).sum().backward()
    assert torch.equal(x.grad, cos)


def _gemma(max_position):
    llama_ns = _llama_ns()
    ns = _load(
        GEMMA,
        ["GemmaFixedRotaryEmbedding"],
        {"_PerDeviceRopeCache": llama_ns["_PerDeviceRopeCache"]},
    )
    return ns["GemmaFixedRotaryEmbedding"](dim = DIM, max_position_embeddings = max_position, base = BASE)


@pytest.mark.parametrize("kind", ["llama", "gemma"])
def test_cache_first_built_in_inference_mode_is_trainable(kind):
    rope = _llama(1 << 17) if kind == "llama" else _gemma(1 << 17)
    reference = _llama(1 << 17) if kind == "llama" else _gemma(1 << 17)
    with torch.inference_mode():
        cos, sin = rope.get_cached(100, 1)
        assert torch.is_inference_mode_enabled()
    _assert_trainable(cos, sin, *([rope.inv_freq] if kind == "llama" else []))
    ref_cos, ref_sin = reference.get_cached(100, 1)
    assert torch.equal(cos, ref_cos) and torch.equal(sin, ref_sin)


def test_cache_grown_in_inference_mode_is_trainable():
    rope = _llama(1 << 17)
    rope.get_cached(10, 0)
    with torch.inference_mode():
        rope.extend_rope_embedding(_x(0, torch.float16), 50000)
        cos, sin = rope(_x(0, torch.float16), seq_len = 50000)
    _assert_trainable(cos, sin, rope.inv_freq)
    ref_cos, ref_sin = _reference(57344, torch.float16)
    assert torch.equal(rope.multi_gpu_cos_cached[0], ref_cos)
    assert torch.equal(rope.multi_gpu_sin_cached[0], ref_sin)


def test_longrope_cache_built_in_inference_mode_is_trainable():
    rope = _llama_ns()["LongRopeRotaryEmbedding"](
        dim = DIM,
        max_position_embeddings = 128,
        original_max_position_embeddings = 16,
        short_factor = [1.0] * (DIM // 2),
        long_factor = [2.0] * (DIM // 2),
    )
    with torch.inference_mode():
        rope.get_cached(8, 1)
        rope.extend_rope_embedding(_x(1, torch.float16), 100)
    _assert_trainable(rope.multi_gpu_short_cos_cached[1], rope.multi_gpu_short_sin_cached[1])
    _assert_trainable(rope.multi_gpu_long_cos_cached[1], rope.multi_gpu_long_sin_cached[1])


def test_cache_build_keeps_grad_mode():
    rope = _llama(1 << 17)
    with torch.inference_mode():
        rope.get_cached(10, 0)
        assert torch.is_inference_mode_enabled() and not torch.is_grad_enabled()
    rope = _llama(1 << 17)
    rope.get_cached(10, 0)
    assert torch.is_grad_enabled() and not rope.multi_gpu_cos_cached[0].requires_grad


@pytest.mark.parametrize("kind", ["llama", "gemma", "longrope"])
def test_cold_build_under_compile_never_asks_for_inference_mode(kind, monkeypatch):
    # A compiled block that builds a device's cache first (a device_map split) must not reach
    # torch.is_inference_mode_enabled: Dynamo cannot trace it (graph break; fullgraph error).
    if kind == "longrope":
        rope = _llama_ns()["LongRopeRotaryEmbedding"](
            dim = DIM,
            max_position_embeddings = 128,
            original_max_position_embeddings = 16,
            short_factor = [1.0] * (DIM // 2),
            long_factor = [2.0] * (DIM // 2),
        )
    else:
        rope = _llama(1 << 17) if kind == "llama" else _gemma(1 << 17)

    def untraceable():
        raise AssertionError("is_inference_mode_enabled reached while compiling")

    monkeypatch.setattr(torch.compiler, "is_compiling", lambda: True)
    monkeypatch.setattr(torch, "is_inference_mode_enabled", untraceable)
    cos, sin = rope.get_cached(8, 1)
    assert cos is not None and sin is not None
    if kind == "longrope":
        rope.extend_rope_embedding(_x(1), 100)
    else:
        rope.extend_rope_embedding(_x(2), 50000)


def _rope_kind(kind):
    if kind == "longrope":
        return _longrope(128, 8)
    return _llama(1 << 12) if kind == "llama" else _gemma(1 << 12)


def _rope_cos_sin(rope, kind, device_index):
    if kind == "longrope":
        return rope.multi_gpu_long_cos_cached[device_index], rope.multi_gpu_long_sin_cached[
            device_index
        ]
    return rope.multi_gpu_cos_cached[device_index], rope.multi_gpu_sin_cached[device_index]


@pytest.mark.parametrize("kind", ["llama", "gemma", "longrope"])
def test_compiled_inference_build_rebuilds_for_training(kind, monkeypatch):
    # A compiled block under inference_mode cannot leave it inside the graph, so its cold build
    # holds inference tensors; the next grad-enabled read must rebuild instead of serving them.
    rope = _rope_kind(kind)
    monkeypatch.setattr(torch.compiler, "is_compiling", lambda: True)
    with torch.inference_mode():
        # Stand-in for the compiled graph, which runs its build in inference mode.
        monkeypatch.setattr(rope, "_rope_build_mode", torch.inference_mode)
        rope.extend_rope_embedding(_x(1), 16)
        cos, sin = rope.get_cached(16, 1)
        assert cos.is_inference() and _rope_cos_sin(rope, kind, 1)[0].is_inference()
        # Inference reads keep the cache.
        assert rope.get_cached(16, 1)[0] is cos
    monkeypatch.undo()
    for compiling in (True, False):
        rope = _rope_kind(kind)
        monkeypatch.setattr(torch.compiler, "is_compiling", lambda: True)
        with torch.inference_mode():
            monkeypatch.setattr(rope, "_rope_build_mode", torch.inference_mode)
            rope.extend_rope_embedding(_x(1), 16)
            rope.get_cached(16, 1)
        monkeypatch.undo()
        monkeypatch.setattr(torch.compiler, "is_compiling", lambda: compiling)
        rope.extend_rope_embedding(_x(1), 16)
        cos, sin = rope.get_cached(16, 1)
        _assert_trainable(cos, sin, *_rope_cos_sin(rope, kind, 1))
        monkeypatch.undo()


@pytest.mark.parametrize("kind", ["llama", "gemma", "longrope"])
def test_compiled_inference_build_then_training_under_dynamo(kind):
    # Compiled generation (inference_mode) builds the cache cold, then a compiled training block
    # and an eager one run backward through it; fullgraph with no graph break throughout.
    from torch._dynamo.utils import counters

    rope = _rope_kind(kind)

    def block(V):
        seq = V.shape[-2]
        rope.extend_rope_embedding(V, seq)
        cos, sin = rope.get_cached(seq, 0)
        # The CPU prelude names device 0 cpu:0; V is plain cpu.
        return V * cos[:seq].to(V.device) + sin[:seq].to(V.device)

    torch._dynamo.reset()
    counters.clear()
    compiled = torch.compile(block, backend = "aot_eager", fullgraph = True)
    with torch.inference_mode():
        compiled(torch.randn(2, 16, DIM))
    for fn in (compiled, block):
        V = torch.randn(2, 16, DIM, requires_grad = True)
        fn(V).sum().backward()
        cos = _rope_cos_sin(rope, kind, 0)[0][:16]
        torch.testing.assert_close(V.grad, cos.expand_as(V))
    assert sum(counters["graph_break"].values()) == 0
    torch._dynamo.reset()


def test_longrope_long_rebuild_keeps_short_marked_for_training(monkeypatch):
    # Compiled inference builds only the short cache (inference tensors); an eager inference request
    # then grows the long one. Rebuilding long must not clear the short cache's grad-rebuild mark.
    rope = _longrope(128, 8)
    monkeypatch.setattr(torch.compiler, "is_compiling", lambda: True)
    with torch.inference_mode():
        monkeypatch.setattr(rope, "_rope_build_mode", torch.inference_mode)
        rope.get_cached(4, 1)
        assert rope.multi_gpu_short_cos_cached[1].is_inference()
    monkeypatch.undo()
    with torch.inference_mode():
        rope.extend_rope_embedding(_x(1), 100)
    assert not rope.multi_gpu_long_cos_cached[1].is_inference()
    cos, sin = rope.get_cached(4, 1)
    _assert_trainable(cos, sin)
    _assert_trainable(rope.multi_gpu_long_cos_cached[1], rope.multi_gpu_long_sin_cached[1])


def test_stock_call_compiles_as_one_graph():
    rope = _llama(1 << 17)
    x = torch.zeros(1, 4, DIM)
    position_ids = torch.tensor([[3, 4, 5, 6]])
    eager_cos, eager_sin = rope(x, position_ids)
    torch._dynamo.reset()
    explained = torch._dynamo.explain(lambda x, p: rope(x, p))(x, position_ids)
    assert explained.graph_break_count == 0, explained.break_reasons
    cos, sin = torch.compile(rope, backend = "eager", fullgraph = True)(x, position_ids + 1)
    ref_cos, ref_sin = _reference(8)
    assert torch.equal(eager_cos[0], ref_cos[3:7]) and torch.equal(eager_sin[0], ref_sin[3:7])
    assert torch.equal(cos[0], ref_cos[4:8]) and torch.equal(sin[0], ref_sin[4:8])


@pytest.mark.parametrize("backend", ["eager", "aot_eager"])
def test_compiled_stock_call_serves_every_position_below_max_position(backend):
    max_position = 1 << 16
    rope = _llama(max_position)
    torch._dynamo.reset()
    compiled = torch.compile(rope, backend = backend, fullgraph = True)
    ref_cos, ref_sin = _reference(max_position)
    x = torch.zeros(1, 4, DIM)
    for start in (0, 32766, 32768, max_position - 4):
        position_ids = torch.arange(start, start + 4)[None]
        cos, sin = compiled(x, position_ids)
        torch.testing.assert_close(
            cos[0], ref_cos[start : start + 4], rtol = 0, atol = 2 * torch.finfo(cos.dtype).eps
        )
        torch.testing.assert_close(
            sin[0], ref_sin[start : start + 4], rtol = 0, atol = 2 * torch.finfo(sin.dtype).eps
        )
    explained = torch._dynamo.explain(lambda x, p: rope(x, p))(x, torch.tensor([[40000, 40001]]))
    assert explained.graph_break_count == 0, explained.break_reasons


def test_compiled_stock_call_preserves_eager_cache_dtype():
    max_position = 1 << 16
    rope = _llama(max_position)
    torch._dynamo.reset()
    compiled = torch.compile(rope, backend = "eager", fullgraph = True)
    position_ids = torch.arange(3, 7)[None]
    compiled(torch.zeros(1, 4, DIM, dtype = torch.bfloat16), position_ids)
    torch._dynamo.reset()
    cos, sin = rope(torch.zeros(1, 4, DIM), position_ids)
    ref_cos, ref_sin = _reference(max_position)
    assert cos.dtype == torch.float32
    assert torch.equal(cos[0], ref_cos[3:7])
    assert torch.equal(sin[0], ref_sin[3:7])


@pytest.mark.parametrize("backend", ["eager", "aot_eager"])
@pytest.mark.parametrize("kind", ["llama", "linear", "extended", "gemma", "gemma_linear"])
def test_compiled_stock_positions_cross_configured_extent(kind, backend):
    ns = _load(
        LLAMA,
        list(_HELPERS)
        + [
            "LlamaRotaryEmbedding",
            "LlamaLinearScalingRotaryEmbedding",
            "LlamaExtendedRotaryEmbedding",
        ],
    )
    if kind.startswith("gemma"):
        ns = _load(
            GEMMA,
            ["GemmaFixedRotaryEmbedding", "GemmaFixedLinearScalingRotaryEmbedding"],
            {"_PerDeviceRopeCache": ns["_PerDeviceRopeCache"]},
        )
    name = {
        "llama": "LlamaRotaryEmbedding",
        "linear": "LlamaLinearScalingRotaryEmbedding",
        "extended": "LlamaExtendedRotaryEmbedding",
        "gemma": "GemmaFixedRotaryEmbedding",
        "gemma_linear": "GemmaFixedLinearScalingRotaryEmbedding",
    }[kind]
    kwargs = {"dim": DIM, "max_position_embeddings": 16, "base": BASE}
    if kind.endswith("linear"):
        kwargs["scaling_factor"] = 1.3 if kind == "gemma_linear" else 2.0
    rope = ns[name](**kwargs)
    reference = ns[name](**kwargs)
    torch._dynamo.reset()
    compiled = torch.compile(rope, backend = backend, fullgraph = True, dynamic = True)
    for start in (0, 15, 32768):
        positions = torch.arange(start, start + 4)[None]
        x = torch.ones(1, 4, DIM, requires_grad = True)
        cos, sin = compiled(x, positions)
        ref_cos, ref_sin = reference(x, positions)
        assert torch.equal(cos, ref_cos)
        assert torch.equal(sin, ref_sin)
        (x * cos + x * sin).sum().backward()
        assert torch.equal(x.grad, cos + sin)
    assert _built(rope.multi_gpu_cos_cached) == []


@pytest.mark.parametrize("rope_type", ["llama3", "yarn", "linear", "dynamic"])
def test_scaled_config_compiles_without_tracing_config_initialization(rope_type):
    import contextlib
    from transformers import LlamaConfig

    names = list(_HELPERS) + [
        "_get_rope_theta",
        "_rope_scaling_as_dict",
        "_llama3_inv_freq_from_config",
        "_vanilla_inv_freq_from_config",
        "_compute_config_rope_inv_freq",
        "LlamaRotaryEmbedding",
    ]
    ns = _load(LLAMA, names, {"contextlib": contextlib})
    scaling = {"rope_type": rope_type, "factor": 8.0}
    if rope_type == "llama3":
        scaling.update(
            low_freq_factor = 1.0, high_freq_factor = 4.0, original_max_position_embeddings = 8192
        )
    config = LlamaConfig(
        hidden_size = DIM,
        num_attention_heads = 1,
        head_dim = DIM,
        max_position_embeddings = 1 << 17,
        rope_scaling = scaling,
    )
    rope = ns["LlamaRotaryEmbedding"](config = config)
    positions = torch.tensor([[0, 32768, 131071, 131072]])
    inv_freq = rope.inv_freq.clone()
    freqs = positions[..., None].float() * inv_freq
    emb = torch.cat((freqs, freqs), dim = -1)
    expected_cos = emb.cos() * rope.attention_scaling
    expected_sin = emb.sin() * rope.attention_scaling
    rope.inv_freq.fill_(float("nan"))
    torch._dynamo.reset()
    compiled = torch.compile(rope, backend = "aot_eager", fullgraph = True)
    x = torch.ones(1, 4, DIM, requires_grad = True)
    cos, sin = compiled(x, positions)
    assert torch.equal(cos, expected_cos)
    assert torch.equal(sin, expected_sin)
    (x * cos + x * sin).sum().backward()
    assert torch.equal(x.grad, cos + sin)


@pytest.mark.gpu
@pytest.mark.skipif(not has_real_cuda(), reason = "CUDA/ROCm accelerator required")
@pytest.mark.parametrize(
    "kind", ["GemmaFixedRotaryEmbedding", "GemmaFixedLinearScalingRotaryEmbedding"]
)
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16])
def test_compiled_gemma_stock_cuda_preserves_cpu_timescale(kind, dtype):
    ns = _load(LLAMA, list(_HELPERS), {"torch": torch, "get_current_device": lambda: 0})
    ns = _load(
        GEMMA,
        ["GemmaFixedRotaryEmbedding", "GemmaFixedLinearScalingRotaryEmbedding"],
        {
            "torch": torch,
            "get_current_device": lambda: 0,
            "_PerDeviceRopeCache": ns["_PerDeviceRopeCache"],
        },
    )
    kwargs = {"dim": 128, "max_position_embeddings": 16}
    if kind == "GemmaFixedLinearScalingRotaryEmbedding":
        kwargs["scaling_factor"] = 1.3
    rope = ns[kind](**kwargs)
    reference = ns[kind](**kwargs)
    positions = torch.tensor([[0, 15, 32767, 131071]], device = "cuda")
    x = torch.ones(1, 4, 128, device = "cuda", dtype = dtype, requires_grad = True)
    torch._dynamo.reset()
    cos, sin = torch.compile(rope, fullgraph = True, dynamic = True)(x, positions)
    ref_cos, ref_sin = reference(x, positions)
    tolerance = 1e-5 if dtype == torch.float32 else 2e-3
    torch.testing.assert_close(cos, ref_cos, rtol = tolerance, atol = tolerance)
    torch.testing.assert_close(sin, ref_sin, rtol = tolerance, atol = tolerance)
    ((cos + sin) * x).sum().backward()
    assert torch.isfinite(x.grad).all()