|
| 1 | +# Copyright 2026 The xLLM Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://github.com/xLLM-AI/xllm/blob/main/LICENSE |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Vision encoder graph admission, layout keying and output ownership.""" |
| 16 | + |
| 17 | +from contextlib import nullcontext |
| 18 | +from types import SimpleNamespace |
| 19 | +from unittest.mock import Mock |
| 20 | + |
| 21 | +import pytest |
| 22 | +import torch |
| 23 | + |
| 24 | +from xllm.python.model_executor.runners.vision_acl_graph import VisionAclGraphRunner |
| 25 | +from xllm.python.models.qwen3_vl import Qwen3VLVisionTransformer, _host_grid_rows |
| 26 | + |
| 27 | +_PIXEL_COLUMNS = 8 |
| 28 | + |
| 29 | + |
| 30 | +def _pixels(patches: int, fill: float = 1.0) -> torch.Tensor: |
| 31 | + return torch.full((patches, _PIXEL_COLUMNS), fill) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def vision_model() -> SimpleNamespace: |
| 36 | + """A stand-in encoder: one output row per spatial-merge group of patches.""" |
| 37 | + |
| 38 | + def forward(pixel_values: torch.Tensor, grid_rows: list[list[int]]) -> torch.Tensor: |
| 39 | + rows = sum(t * h * w for t, h, w in grid_rows) // 4 |
| 40 | + return pixel_values[:rows, :2] * 2.0 |
| 41 | + |
| 42 | + return SimpleNamespace(side_effect=Mock(side_effect=forward)) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def runner(monkeypatch: pytest.MonkeyPatch, vision_model: SimpleNamespace) -> VisionAclGraphRunner: |
| 47 | + monkeypatch.setattr( |
| 48 | + torch, |
| 49 | + "npu", |
| 50 | + SimpleNamespace( |
| 51 | + current_stream=Mock(return_value=Mock()), |
| 52 | + Stream=Mock(return_value=Mock()), |
| 53 | + stream=Mock(side_effect=lambda *args: nullcontext()), |
| 54 | + NPUGraph=Mock(side_effect=Mock), |
| 55 | + graph=Mock(side_effect=lambda *args, **kwargs: nullcontext()), |
| 56 | + graph_pool_handle=Mock(return_value=(0, 0)), |
| 57 | + mem_get_info=Mock(return_value=(1 << 40, 1 << 40)), |
| 58 | + ), |
| 59 | + raising=False, |
| 60 | + ) |
| 61 | + # The runner only calls the encoder, so a plain callable stands in for it. |
| 62 | + model = vision_model.side_effect |
| 63 | + return VisionAclGraphRunner(model, torch.device("cpu"), max_graphs=2) |
| 64 | + |
| 65 | + |
| 66 | +def test_layout_is_captured_once_and_replayed(runner: VisionAclGraphRunner) -> None: |
| 67 | + grid = [[1, 2, 2]] |
| 68 | + first = runner.execute(_pixels(4), grid) |
| 69 | + captures = torch.npu.graph.call_count |
| 70 | + second = runner.execute(_pixels(4, fill=3.0), grid) |
| 71 | + |
| 72 | + assert runner.captured_graphs == 1 |
| 73 | + assert torch.npu.graph.call_count == captures, "a second call of a known layout must not capture again" |
| 74 | + assert first.shape == second.shape |
| 75 | + |
| 76 | + |
| 77 | +def test_distinct_layouts_get_distinct_graphs(runner: VisionAclGraphRunner) -> None: |
| 78 | + runner.execute(_pixels(4), [[1, 2, 2]]) |
| 79 | + runner.execute(_pixels(16), [[1, 4, 4]]) |
| 80 | + assert runner.captured_graphs == 2 |
| 81 | + |
| 82 | + # Two images of a captured geometry are a layout of their own: the grouped |
| 83 | + # encode is what fills the device, so it is what gets captured, and the |
| 84 | + # budget decides whether this one can be. |
| 85 | + captures = torch.npu.graph.call_count |
| 86 | + runner.execute(_pixels(8), [[1, 2, 2], [1, 2, 2]]) |
| 87 | + assert runner.captured_graphs == 2, "the budget is two graphs" |
| 88 | + assert torch.npu.graph.call_count == captures |
| 89 | + assert runner.eager_encodes == 1 |
| 90 | + |
| 91 | + |
| 92 | +def test_budget_exhaustion_keeps_a_layout_eager(runner: VisionAclGraphRunner, vision_model: SimpleNamespace) -> None: |
| 93 | + runner.execute(_pixels(4), [[1, 2, 2]]) |
| 94 | + runner.execute(_pixels(16), [[1, 4, 4]]) |
| 95 | + captures = torch.npu.graph.call_count |
| 96 | + calls = vision_model.side_effect.call_count |
| 97 | + |
| 98 | + rejected = runner.execute(_pixels(36), [[1, 6, 6]]) |
| 99 | + assert torch.npu.graph.call_count == captures |
| 100 | + assert vision_model.side_effect.call_count == calls + 1, "the over-budget layout must run the encoder eagerly" |
| 101 | + assert rejected.shape[0] == 9 |
| 102 | + assert runner.eager_encodes == 1 |
| 103 | + |
| 104 | + # An over-budget layout is not retried, and nothing is remembered per |
| 105 | + # layout: the budget never frees up, so such state would grow with the |
| 106 | + # variety of the traffic rather than with the deployment's geometry count. |
| 107 | + runner.execute(_pixels(36), [[1, 6, 6]]) |
| 108 | + runner.execute(_pixels(64), [[1, 8, 8]]) |
| 109 | + assert torch.npu.graph.call_count == captures |
| 110 | + assert runner.eager_encodes == 3 |
| 111 | + assert runner.captured_graphs == 2 |
| 112 | + assert not [name for name in vars(runner) if "reject" in name] |
| 113 | + |
| 114 | + |
| 115 | +def test_replay_output_is_not_the_captured_buffer(runner: VisionAclGraphRunner) -> None: |
| 116 | + grid = [[1, 2, 2]] |
| 117 | + runner.execute(_pixels(4), grid) |
| 118 | + kept = runner.execute(_pixels(4, fill=2.0), grid) |
| 119 | + captured = next(iter(runner._graphs.values())).static_output |
| 120 | + # The framework keeps the encoder output across prefill chunks and in its |
| 121 | + # encoder cache, while the next replay overwrites the captured buffer, so |
| 122 | + # the two must not be the same storage. |
| 123 | + assert kept.data_ptr() != captured.data_ptr() |
| 124 | + assert torch.equal(kept, captured) |
| 125 | + |
| 126 | + |
| 127 | +def test_pixel_rows_must_match_the_grid(runner: VisionAclGraphRunner) -> None: |
| 128 | + # A grid that does not describe these pixels would slice the wrong rows. |
| 129 | + with pytest.raises(ValueError, match="pixel rows"): |
| 130 | + runner.execute(_pixels(5), [[1, 2, 2]]) |
| 131 | + |
| 132 | + |
| 133 | +def test_frame_boundaries_match_the_device_formula() -> None: |
| 134 | + for grid_rows in ([[1, 2, 2]], [[1, 4, 6], [1, 2, 2]], [[3, 2, 4]], [[2, 4, 4], [1, 6, 6], [4, 2, 2]]): |
| 135 | + grid = torch.tensor(grid_rows, dtype=torch.int32) |
| 136 | + expected = torch.repeat_interleave(grid[:, 1] * grid[:, 2], grid[:, 0]).cumsum(dim=0, dtype=torch.int32) |
| 137 | + assert Qwen3VLVisionTransformer._compute_frame_boundaries(grid_rows) == expected.tolist() |
| 138 | + |
| 139 | + |
| 140 | +def test_host_grid_rows_accepts_both_grid_forms() -> None: |
| 141 | + # The captured path resolves the grid to Host rows; the Qwen3.5 composer |
| 142 | + # still passes a device tensor, and both have to reach the same rows. |
| 143 | + rows = [[1, 4, 6], [2, 2, 2]] |
| 144 | + assert _host_grid_rows(rows) is rows |
| 145 | + converted = _host_grid_rows(torch.tensor(rows, dtype=torch.int32)) |
| 146 | + assert converted == rows |
| 147 | + assert all(isinstance(value, int) for row in converted for value in row) |
0 commit comments