Skip to content

Commit 7df7ca7

Browse files
authored
Merge pull request #459 from InferMatrixLab/feat/gpu-demand-wait-and-need
testing: wait for cards as long as it takes, and ask only for what a command needs
2 parents 14bee9e + 2ca822e commit 7df7ca7

9 files changed

Lines changed: 682 additions & 84 deletions

File tree

‎doc/architecture/SPEC/engine/steps/rebase_v3.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# engine/steps/rebase_v3.py —— 规范
22

3-
<!-- verified-against: 2026-11-25 -->
3+
<!-- verified-against: 2026-11-26 -->
44

55
`LOC ~2660 · step 库(v3 rebase 装配层) · refactor-status: oversized`
66

@@ -172,7 +172,9 @@ campaign 改为按命令向调度器取卡之后(`testing` 规范的"按命令
172172
经本文件启动的 GPU 命令各自声明要几张:
173173

174174
- `run_pytest`/`reproduce`(`_run_tests`)以 `TestJob(min_gpus=0, gpu_lock=True,
175-
gpus=1)` 运行,向调度器要 1 张卡。返回值新增 `skipped` 与 `skip_reason`;
175+
gpus=None)` 运行:卡数由 `TestRunner` 按 `command_gpus` 从命令及其测试的声明推导,
176+
至少 1 张(2026-11-26 起"按需要申请",此前固定 1 张)。返回值新增 `skipped` 与
177+
`skip_reason`;
176178
`passed` 只在 `rc == 0 and not skipped` 时为真——调度器没给卡的运行没有通过。
177179
- `run_precommit` 以 `gpus=0` 运行,不申请。
178180
- 两处 `ModuleRunConfig` 的 `cuda_devices` 改取 `testing.gpu_demand.session_devices()`:

‎doc/architecture/SPEC/rebase_engine.md‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# rebase_engine/ —— 规范
22

3-
<!-- verified-against: 2026-11-25 -->
3+
<!-- verified-against: 2026-11-26 -->
44

55
`LOC ~9530(30 个模块) · repo-rebase-v3 的原生 rebase 引擎 · refactor-status: ok`
66

@@ -269,14 +269,17 @@ campaign 不再从头到尾持有一份外层预约,每条 GPU 命令向调度
269269
`_run_shell`(原有的 Popen、后代快照与超时逻辑未变)。按需模式(读本进程环境的
270270
`IMX_GPU_MODE`)下,它把模式写进子进程环境,使命令启动的 `imx-omni-pytest`
271271
自己去申请;`testing.gpu_demand.shell_needs_gpu` 判定会起测试或 CUDA 代码的命令,
272-
用 `scheduler_wrap` 向调度器要 1 张卡(等待上限是工具超时的一半),并给子进程设
272+
用 `scheduler_wrap` 向调度器要卡,张数取 `command_gpus(command, 1, root)`——
273+
命令的实际 cwd,缺省是目标仓库;至少 1 张(等待上限是工具超时的一半),并给子进程设
273274
`IMX_GPU_HELD=1`,命令启动的东西不再申请。等待到期时返回
274275
`{"exit_code": 1, "gpu_unavailable": true, "stderr": "Not run: …"}`;其余失败
275276
在 stderr 前附上调度器的错误文本。2026-11-24/25 的 campaign 里,agent 的 1039 次
276277
工具调用有 882 次走 `run_shell`(计划评审之前 `run_pytest` 是锁住的),其中绝大多数
277278
是 `git`、`grep` 与读取,不受影响。
278-
- **`module_pytest.py`。** 包装器的 `TestJob` 带 `gpus=1`:测试可能用到卡,如同它
279-
过去在外层预约下运行时那样。包装器仍返回结果自己的 rc(既有契约,由
279+
- **`module_pytest.py`。** 包装器的 `TestJob` 带
280+
`gpus=command_gpus(command, 1, root=repo, maximum=…)`:以它所选测试声明的需要
281+
为准(`testing` 规范的"卡数 = 需要"),至少 1 张——测试可能用到卡,如同它过去在
282+
外层预约下运行时那样。包装器仍返回结果自己的 rc(既有契约,由
280283
`test_module_pytest_outcomes.py` 钉住),所以跳过时把
281284
`imx-omni-pytest: skipped — <原因>` 同时写到 stdout 和 stderr。
282285
- **提示词。** `build_module_prompt(..., cuda_devices=prompt_devices(config.cuda_devices))`:
@@ -295,3 +298,9 @@ campaign 不再从头到尾持有一份外层预约,每条 GPU 命令向调度
295298
final 只有 `cu130`。按目标的 variant 去取,测试循环用来区分既有失败与回归的基线
296299
运行时就建不出来,退回到让基线也跑在 campaign 的 venv 上。vllm-omni adapter 把
297300
`baseline_variant` 设为 `cu130`。测试:`test_rebase_release_target.py`。
301+
302+
2026-11-26:负责人定了"一直等到有卡、按需要申请、不做 reserve"。两处的卡数从固定
303+
1 张改为按需要推导:此前模块测试只申请 1 张,而外层预约时代每个模块的测试都看得到
304+
4 张卡,需要多张卡的测试因此失败(`tests/diffusion/models/hy_nomas/
305+
test_pixelumm_parallel.py`)。等待不再有缺省上限(`run_shell` 里同步起的测试除外,
306+
见 `testing` 规范)。

‎doc/architecture/SPEC/testing.md‎

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# testing/ —— 规范
22

3-
<!-- verified-against: 2026-11-25 -->
3+
<!-- verified-against: 2026-11-26 -->
44

5-
`LOC ~2190(9 个文件) · 测试执行底座(rebase agent shell 测试层的 Python 移植) · refactor-status: ok`
5+
`LOC ~2450(9 个文件) · 测试执行底座(rebase agent shell 测试层的 Python 移植) · refactor-status: ok`
66

77
## 职责
88
仓库中立的测试执行底座:gpu_lock.sh / kill_test_tree.sh / test_watchdog.sh /
@@ -42,8 +42,8 @@ test_runner.sh 的 Python 移植。仓库专属数据(watchdog 模式、artifa
4242
- `gpu_demand`:`GpuDemand`(`.from_env`)、`command_gpus`、`shell_needs_gpu`、
4343
`scheduler_wrap`、`idle_timeout`、`granted_gpus`、`scheduler_error`、
4444
`duration_seconds`、`session_devices`、`prompt_devices`;常量 `MODE_ENV`、
45-
`HELD_ENV`、`WAIT_ENV`、`NO_GPU_RC`、`SCHEDULER_TIMEOUT_MARGIN_SEC`、
46-
`IDLE_MAX_SEC`。
45+
`HELD_ENV`、`WAIT_ENV`、`MAX_ENV`、`NO_GPU_RC`、`SCHEDULER_TIMEOUT_MARGIN_SEC`、
46+
`IDLE_MAX_SEC`、`RUN_LEVEL_OPTION`、`RUN_LEVELS`、`DEFAULT_RUN_LEVEL`。
4747
- `watchdog`:`WatchdogPatterns(.from_yaml)`、`LogWatchdog(.start/.stop/.check_once/.result)`、`WatchdogResult`。
4848
- `watchdog_learn`:`record`、`harvest`、`eligible_patterns`、`promote`、
4949
`read_decisions`、`normalize_pattern`、`repair_tail`。
@@ -96,10 +96,11 @@ env 前缀 → `scrub_agent_shell_env(extra_safe_prefixes=…)`(manifest 数
9696
(TestRunner 对 shell 的 command-echo 金样)、`test_module_pytest_signals.py`
9797
(经包装器:`terminate_active` 停树、stdin=/dev/null);`watchdog_learn` 另由
9898
`test_curator.py`(harvest exactly-once)与 `test_knowledge_foundation.py`
99-
(record 身份字段、撕尾修复)钉住。`test_gpu_demand.py`(64 个)以一个替身 `gpu`
100-
可执行文件覆盖按需模式:卡数、shell 命令的判定、包装程序的输出路由与等待到期、
101-
空闲回收的单位与上限、调度器拒绝请求时的日志行、`TestRunner` 的 skip,以及 agent
102-
shell 与模块测试包装器。
99+
(record 身份字段、撕尾修复)钉住。`test_gpu_demand.py`(74 个)以一个替身 `gpu`
100+
可执行文件覆盖按需模式:卡数(含测试声明的读取与上限)、shell 命令的判定、
101+
包装程序的输出路由、不限等待与有界等待到期、给卡后才计时、锁仍有界、
102+
空闲回收的单位与上限、调度器拒绝请求时的日志行、`TestRunner` 的 skip,以及
103+
agent shell 与模块测试包装器。
103104

104105
## 重构备注
105106
`runner.py`(643L)是包内最重的一块:`_spawn` 把快照线程、双定时器、watchdog
@@ -127,19 +128,29 @@ GPU 锁下一次只跑一个,52 个只用 1 张卡。负责人决定:不再
127128
campaign,改为每条 GPU 命令向调度器申请自己需要的卡、只在运行期间持有。
128129

129130
- **开关。** `IMX_GPU_MODE=demand` 选择按需模式;未设置时行为不变(调用方已
130-
持有卡,命令继承)。`IMX_GPU_WAIT`(缺省 `30m`)是命令在调度器队列里的等待
131+
持有卡,命令继承)。`IMX_GPU_WAIT` 给命令在调度器队列里的等待设上限;不设则
132+
一直等到拿到卡(2026-11-26 起,见下文)。`IMX_GPU_MAX` 是一条命令申请卡数的
131133
上限。`IMX_GPU_HELD=1` 表示所在命令树已持有卡,其下任何 `TestRunner` 都不再
132134
申请。`TestRunner` 缺省从环境读取(`GpuDemand.from_env`),测试可显式传
133135
`gpu_demand=`。
134-
- **卡数。** `TestJob.gpus` 显式给出;缺省取三者最大值:`min_gpus`、命令里最大
135-
的 `cards_N` pytest marker(manifest 的每个 job 都是 `hw: any`,卡数只在 marker
136-
里)、以及"取 GPU 锁的运行算 1 张"。0 表示不申请(`run_precommit`)。agent 的
137-
`run_pytest`/`reproduce` 与 `imx-omni-pytest` 包装器各要 1 张。
136+
- **卡数 = 需要。** `TestJob.gpus` 显式给出;否则 `command_gpus(command, minimum,
137+
root, maximum)`:命令自己的 `-m` 表达式选中的 `cards_N` 以其中最大者为准
138+
(manifest 的每个 job 都是 `hw: any`,卡数只在 marker 里);被 `not` 否定的是
139+
**排除**,不是需要——目标仓库自己的 CPU job 写的就是
140+
`core_model and cpu and not (cards_2 or … or cards_8)`。没有选中任何 `cards_N`
141+
时读取命令所指定的测试文件,取其中**与命令同属一个运行级别**(`--run-level`,
142+
缺省 `core_model`)的测试所声明的最大卡数——`cards_N` 标记,或装饰器的
143+
`num_cards=`。其它级别的、以及被表达式排除的卡数的测试不计:卡不够时它们按
144+
自己的保护跳过,不会失败。没有声明算 1 张。
145+
结果不低于 `minimum`(取 GPU 锁的运行至少 1 张,`run_precommit` 为 0、不申请),
146+
不高于 `maximum`。agent 的 `run_pytest`/`reproduce`、`imx-<仓库>-pytest` 包装器
147+
与 agent 在 shell 里起的测试走同一套算法。
138148
- **包装程序。** `scheduler_wrap(command, gpus, timeout_sec, wait, note, side_log)`
139149
返回一段给 `bash --norc -c` 的 shell:命令的 stdout/stderr 留在调用方给的描述符
140150
(fd 3/4),调度器自己的输出写进 `<日志>.scheduler`。实测调度器把进度打到
141-
stdout、把 `Error: wait timeout exceeded` 打到 stderr,等待到期时退出码为 1;
142-
包装程序把它换成 `NO_GPU_RC`(97),其余失败保留调度器的退出码。调度器的超时设为
151+
stdout、把 `Error: wait timeout exceeded` 打到 stderr,有界等待到期时退出码为 1;
152+
包装程序把它换成 `NO_GPU_RC`(97),其余失败保留调度器的退出码。`wait` 为空时
153+
不传 `--wait`,调度器一直排队。调度器的超时设为
143154
`timeout_sec + SCHEDULER_TIMEOUT_MARGIN_SEC`(秒),让运行器自己的定时器先触发。
144155
空闲回收由 `idle_timeout()` 给出:同一个时长向上取整到**单一单位**(秒、分或小时),
145156
且不超过 `IDLE_MAX_SEC`(3 小时)。实测(2026-11-25)调度器只解析单一单位的空闲
@@ -153,9 +164,10 @@ campaign,改为每条 GPU 命令向调度器申请自己需要的卡、只在
153164
命令失败且调度器报了错时才附上它的错误文本。日志里没有 `Reserved …` 可依时写
154165
`[gpu] not run: the scheduler named no card(s) and said: …`——没有卡,命令就没跑;
155166
错误文本在它随后的 `Usage:` 处截断。
156-
- **排队到期是一次显式 skip。** `TestRunner.run` 返回 `rc=0, skipped=True`,
157-
`skip_reason` 写明要几张、等了多久;命令没有运行,也不写 pass marker。等待
158-
上限不超过 `timeout_sec` 的一半:运行器的定时器从 spawn 起算,排队时间也在内。
167+
- **有界等待到期是一次显式 skip**(只在设了 `IMX_GPU_WAIT` 时)。`TestRunner.run`
168+
返回 `rc=0, skipped=True`,`skip_reason` 写明要几张、等了多久;命令没有运行,
169+
也不写 pass marker。上限不超过 `timeout_sec` 的一半:那时运行器的定时器从
170+
spawn 起算,排队时间也在内。
159171
- **按需模式下由调度器决定有没有卡**:不做硬件门槛,丢掉继承的
160172
`CUDA_VISIBLE_DEVICES`(调度器授权时自己命名),并给子进程设 `IMX_GPU_HELD=1`。
161173
- **命令结束后不在已释放的卡上清理进程。** 外层预约时代
@@ -178,3 +190,45 @@ campaign,改为每条 GPU 命令向调度器申请自己需要的卡、只在
178190

179191
实测(2026-11-25):一条 `TestRunner` 命令在真实调度器下恰好持有 1 张卡 35 秒,
180192
前后都是 0;torch 看到 1 张卡;import vllm 及其编译扩展不建立 CUDA 上下文。
193+
194+
## 一直等到有卡、按需要申请(2026-11-26)
195+
196+
负责人在 2026-11-26 定了三条:排不到卡就一直等,不放弃也不跳过;不指定要哪几张
197+
卡,申请的数量与需要的一致;不做任何 reserve,只走调度器的队列。
198+
199+
- **一直等。** `IMX_GPU_WAIT` 不再设缺省值:`gpu run` 不带 `--wait`。运行器的
200+
定时器与看门狗改为在调度器给卡之后才启动——`_await_grant` 每秒读一次旁路文件,
201+
直到出现 `Reserved …` 或进程退出。排队时间因此不计入测试的超时;此前定时器从
202+
spawn 起算,一条排了两小时的命令会在队列里被自己的超时杀掉。旁路文件在启动前
203+
清空:上一次运行留下的给卡记录不能让这一次的等待提前结束。日志里写两行:
204+
`[gpu] queued for the scheduler's cards at …` 与 `[gpu] granted after N s in the
205+
queue: [..]`——空了几小时的日志在读的人看来是挂死。调度器**拒绝**请求不是等待:
206+
进程退出,日志照旧写 `not run`。GPU 锁保持它一小时的有界等待,没有跟着放开:
207+
持锁的那个运行自己可能在队列里排几个小时,后面等锁的若不设上限,agent 的会话
208+
就会无声地卡住;锁超时是它能据以行动的反馈。"一直等"说的是卡,锁是自己的串行。
209+
- **按需要。** 模块测试此前固定申请 1 张,而以前外层预约时代每个模块的测试都能
210+
看到 4 张卡:`tests/diffusion/models/hy_nomas/test_pixelumm_parallel.py` 按
211+
2、4 个进程 `mp.spawn`,没有 `cards_N` 标记也没有保护,1 张卡下必然失败。
212+
卡数于是改为读测试自己的声明:`command_gpus` 的规则见上文"卡数 = 需要"。读取
213+
用 AST,不认字符串里的名字——目标仓库自己的测试会引用写着 `cards_8` 的 CI 命令。
214+
只扫仓库根目录之内、路径名以 `test` 开头的目录,至多 4000 个文件;命令里出现的
215+
别的目录一律不碰(一个以 `/` 为根的测试里,命令说了 `cd /tmp`)。结果缓存 5 分钟:
216+
agent 一边工作一边改测试。
217+
- **上限。** `tests/e2e --run-level advanced_model` 声明的需求是 8 张,也就是整台
218+
机器;配上"一直等",这样一条命令可能永远排不上。`IMX_GPU_MAX` 封顶,launcher
219+
里取 4——以往每个 campaign 的配置,也是 `core_model` 级别上声明的最大需求。
220+
超出部分的测试按它们自己的保护跳过,和以前只看得到 4 张卡时一样。
221+
- **仍然有界的一处。** agent 在 `run_shell` 里同步起的测试,等待不超过该次工具
222+
调用超时的一半:工具调用的期限是 harness 定的,超了它等不下去;到期时返回
223+
`gpu_unavailable`,提示词本就要求长验证走包装器的分离运行。
224+
225+
实测(2026-11-25/26):`tests/` 全树在 `core_model` 级别上声明 1 张的 196 个文件、
226+
2 张的 14 个、4 张的 6 个;按模块,worker_runner / input_output / online_serving /
227+
model_config 需要 1 张(后者无声明),model_executor / scheduler / platform 的
228+
全量运行需要 4 张。AST 解析全树约 6 秒,单个模块目录 1–2 秒。
229+
230+
2026-11-26,实机上发现并补上:`-m` 表达式里被 `not` 否定的 `cards_N` 曾被当成需要
231+
——卡数是对整条命令做正则取最大值。campaign 自己的 agent 跑
232+
`tests/distributed/ -m 'core_model and cuda and L4 and not cards_2'` 时因此申请了
233+
两张卡,而 manifest 里 CPU job 的排除表达式会申请八张。现在按表达式解析
234+
(`_expression_cards`):`not` 作用于紧随其后的那一项,或一个括号组。

‎src/infermatrix_copilot/engine/steps/rebase_v3.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ def _run_tests(kw: Mapping, key: str) -> dict:
354354
outcome = runner.run(
355355
TestJob(key=f"{key}_{abs(hash(cmd)) % 10 ** 8}", command=cmd,
356356
timeout_sec=float(kw.get("timeout") or 1800),
357-
min_gpus=0, gpu_lock=True, gpus=1),
357+
# as many cards as the command's tests declare, one at least
358+
min_gpus=0, gpu_lock=True, gpus=None),
358359
_target_test_env(settings, manifest, host_env=host_env))
359360
tail = ""
360361
try:

‎src/infermatrix_copilot/rebase_engine/module_pytest.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import time
5555
from pathlib import Path
5656

57+
from ..testing.gpu_demand import GpuDemand, command_gpus
5758
from ..testing.runner import TestJob, TestOutcome, TestRunner
5859
from ..testing.watchdog import WatchdogPatterns
5960

@@ -459,9 +460,11 @@ def review_fn(test_name: str, snippet: str) -> str:
459460
# pass), while gpu_lock=True holds the lock for the run
460461
job = TestJob(key=_job_key(args), command=command, timeout_sec=timeout,
461462
min_gpus=0, gpu_lock=_needs_gpu_lock(args), index=0,
462-
# the tests may use a card, as they could under the
463+
# as many cards as the tests it runs declare, and one at
464+
# least: they may use a card, as they could under the
463465
# campaign-wide reservation this wrapper used to run in
464-
gpus=1)
466+
gpus=command_gpus(command, 1, root=repo,
467+
maximum=GpuDemand.from_env().maximum))
465468
if process_setup:
466469
process.install_signal_handlers(runner)
467470
outcome = runner.run(job, dict(os.environ))

‎src/infermatrix_copilot/rebase_engine/rebase_tools.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ def handle_run_shell(command: str, workdir: str | None = None,
181181
import tempfile
182182

183183
from ..testing.gpu_demand import (
184-
HELD_ENV, MODE_ENV, NO_GPU_RC, GpuDemand, scheduler_error,
185-
scheduler_wrap, shell_needs_gpu)
184+
HELD_ENV, MODE_ENV, NO_GPU_RC, GpuDemand, command_gpus,
185+
scheduler_error, scheduler_wrap, shell_needs_gpu)
186186

187187
env, side_log, wait = (dict(paths.env) or None), None, ""
188-
if GpuDemand.from_env(os.environ).enabled:
188+
root = workdir or paths.omni_path # where the command runs
189+
demand = GpuDemand.from_env(os.environ)
190+
if demand.enabled:
189191
# The mode is this process's (a bridge learns it from its env)
190192
# and must reach what the command starts: the test wrapper asks
191193
# for its own cards.
@@ -201,10 +203,12 @@ def handle_run_shell(command: str, workdir: str | None = None,
201203
os.close(fd)
202204
side_log, wait = Path(name), f"{max(int(timeout / 2), 1)}s"
203205
command = scheduler_wrap(
204-
command, gpus=1, timeout_sec=timeout + 60, wait=wait,
206+
command, gpus=command_gpus(command, 1, root=root,
207+
maximum=demand.maximum),
208+
timeout_sec=timeout + 60, wait=wait,
205209
note=f"imx agent shell {os.getpid()}", side_log=side_log)
206210
try:
207-
result = _run_shell(command, workdir, timeout, env)
211+
result = _run_shell(command, root, timeout, env)
208212
finally:
209213
scheduler = scheduler_error(side_log) if side_log else ""
210214
if side_log:
@@ -225,7 +229,7 @@ def _run_shell(command: str, workdir: str | None, timeout: int,
225229
from ..testing.process_tree import collect_descendants, kill_tree
226230
from ..testing.runner import TestRunner, _record_walk
227231

228-
cwd = workdir or paths.omni_path
232+
cwd = workdir # resolved by the caller
229233
try:
230234
with subprocess.Popen(
231235
# Match TestRunner's runtime protection: inherited SSH

0 commit comments

Comments
 (0)