|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""run_with_frame_stack_reserve: call semantics, version gate and data-stack churn (no GPU).""" |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +import threading |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from tensorrt_llm._utils import ( |
| 25 | + _FRAME_STACK_RESERVE_DISABLE_ENV, |
| 26 | + _FRAME_STACK_RESERVE_SLOTS, |
| 27 | + _frame_stack_reserve_needed, |
| 28 | + _needs_frame_stack_reserve, |
| 29 | + run_with_frame_stack_reserve, |
| 30 | +) |
| 31 | + |
| 32 | +pytestmark = pytest.mark.cpu_only |
| 33 | + |
| 34 | +_CALLS_PER_DEPTH = 2000 |
| 35 | +# Enough depths to cross several ordinary data-stack chunks. |
| 36 | +_DEPTHS = 300 |
| 37 | + |
| 38 | + |
| 39 | +def _leaf(): |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +# A larger leaf frame makes it cross a chunk boundary during the sweep. |
| 44 | +_leaf.__code__ = _leaf.__code__.replace(co_stacksize=64) |
| 45 | + |
| 46 | + |
| 47 | +def _call_leaf_at_depth(depth, calls): |
| 48 | + if depth: |
| 49 | + return _call_leaf_at_depth(depth - 1, calls) |
| 50 | + for _ in range(calls): |
| 51 | + _leaf() |
| 52 | + |
| 53 | + |
| 54 | +def _sweep_depths(): |
| 55 | + for depth in range(_DEPTHS): |
| 56 | + _call_leaf_at_depth(depth, _CALLS_PER_DEPTH) |
| 57 | + |
| 58 | + |
| 59 | +def _minor_faults_in_new_thread(fn): |
| 60 | + # Each freshly mapped data-stack chunk shows up as a minor page fault. |
| 61 | + resource = pytest.importorskip("resource") |
| 62 | + result = {} |
| 63 | + |
| 64 | + def target(): |
| 65 | + before = resource.getrusage(resource.RUSAGE_THREAD).ru_minflt |
| 66 | + fn() |
| 67 | + result["faults"] = resource.getrusage(resource.RUSAGE_THREAD).ru_minflt - before |
| 68 | + |
| 69 | + thread = threading.Thread(target=target) |
| 70 | + thread.start() |
| 71 | + thread.join() |
| 72 | + return result["faults"] |
| 73 | + |
| 74 | + |
| 75 | +def test_forwards_arguments_and_result(): |
| 76 | + def echo(*args, **kwargs): |
| 77 | + return args, kwargs |
| 78 | + |
| 79 | + # ``fn`` is positional-only, so a keyword argument of the same name reaches the callee. |
| 80 | + assert run_with_frame_stack_reserve(echo, 1, 2, fn="x") == ((1, 2), {"fn": "x"}) |
| 81 | + |
| 82 | + |
| 83 | +def test_exception_propagates_unchanged(): |
| 84 | + error = ValueError("boom") |
| 85 | + |
| 86 | + def crash(): |
| 87 | + raise error |
| 88 | + |
| 89 | + with pytest.raises(ValueError) as excinfo: |
| 90 | + run_with_frame_stack_reserve(crash) |
| 91 | + assert excinfo.value is error |
| 92 | + assert excinfo.value.__context__ is None |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize( |
| 96 | + "implementation, version, expected", |
| 97 | + [ |
| 98 | + ("cpython", (3, 10, 16), False), |
| 99 | + ("cpython", (3, 11, 0), True), |
| 100 | + ("cpython", (3, 12, 3, "final", 0), True), |
| 101 | + ("cpython", (3, 13, 12), True), |
| 102 | + ("cpython", (3, 13, 13), False), |
| 103 | + ("cpython", (3, 14, 3), True), |
| 104 | + ("cpython", (3, 14, 4), False), |
| 105 | + ("cpython", (3, 15, 0), False), |
| 106 | + ("pypy", (3, 12, 3), False), |
| 107 | + ], |
| 108 | +) |
| 109 | +def test_gate(implementation, version, expected): |
| 110 | + assert _frame_stack_reserve_needed(implementation, version) is expected |
| 111 | + |
| 112 | + |
| 113 | +def test_reserve_applied_at_import_iff_gate_holds(): |
| 114 | + reserved = run_with_frame_stack_reserve.__code__.co_stacksize >= _FRAME_STACK_RESERVE_SLOTS |
| 115 | + assert reserved is _needs_frame_stack_reserve() |
| 116 | + |
| 117 | + |
| 118 | +def test_env_var_disables_reserve_at_import(): |
| 119 | + env = dict(os.environ, **{_FRAME_STACK_RESERVE_DISABLE_ENV: "1"}) |
| 120 | + code = ( |
| 121 | + "from tensorrt_llm import _utils as u\n" |
| 122 | + "reserved = u.run_with_frame_stack_reserve.__code__.co_stacksize" |
| 123 | + " >= u._FRAME_STACK_RESERVE_SLOTS\n" |
| 124 | + "print('RESULT', int(reserved))\n" |
| 125 | + ) |
| 126 | + proc = subprocess.run( |
| 127 | + [sys.executable, "-c", code], |
| 128 | + env=env, |
| 129 | + capture_output=True, |
| 130 | + text=True, |
| 131 | + timeout=600, |
| 132 | + check=True, |
| 133 | + ) |
| 134 | + assert proc.stdout.strip().splitlines()[-1] == "RESULT 0" |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.skipif( |
| 138 | + sys.platform != "linux" or not _needs_frame_stack_reserve(), |
| 139 | + reason="needs an affected CPython and per-thread rusage", |
| 140 | +) |
| 141 | +def test_reserve_removes_chunk_churn(): |
| 142 | + # Without the reserve, the sweep must hit a chunk boundary, or the second check proves nothing. |
| 143 | + assert _minor_faults_in_new_thread(_sweep_depths) > _CALLS_PER_DEPTH |
| 144 | + faults = _minor_faults_in_new_thread(lambda: run_with_frame_stack_reserve(_sweep_depths)) |
| 145 | + assert faults < _CALLS_PER_DEPTH |
0 commit comments