Co-authored-by: nomiz<1606135114@qq.com> # message auto-generated for no-merge-commit merge: !45296 merge task_queue_master into master feat: support dynamic task queue enable mode switching Created-by: nomiz Commit-by: nomiz Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> https://gitcode.com/Ascend/pytorch/issues/3678 # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [x] 需求 - [ ] 问题单 - [ ] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) Python侧灵活控制taskqueue设置的能力,具体为: (1) 新增Python接口用户设置和获取taskqueue状态值(一个原子变量,默认-1),设置时需要清queue; (2) 在get_task_enable的公共接口中,先读取该全局变量的值,如果为非-1,则以该值的结果为准,不再从环境变量读取; (3) 环境变量的读取仍然是一次性的返回常量,保证默认状态下的性能 ```python # 设置模式 torch.npu.set_task_queue_enable(0) # 关闭 task queue torch.npu.set_task_queue_enable(1) # Level 1 优化 (默认) torch.npu.set_task_queue_enable(2) # Level 2 优化 (纯推理) # 查询当前模式 mode = torch.npu.get_task_queue_enable() # 返回 0/1/2 ``` # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” 是,新增接口set_task_queue_enable和get_task_queue_enable # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 ut测试验证结果  RL模型修改前后性能验证无劣化  # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [ ] 代码注释完备,正确记录错误日志 - [ ] 代码实现进行了返回值、空指针等校验 - [ ] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!45296
200 lines
7.1 KiB
Python
200 lines
7.1 KiB
Python
import os
|
|
import time
|
|
import threading
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
import torch
|
|
import torch_npu # noqa: F401
|
|
from torch.testing._internal.common_utils import TestCase, run_tests
|
|
|
|
|
|
class TestTaskQueueEnableEnv(TestCase):
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.original_mode = torch.npu.get_task_queue_enable()
|
|
|
|
def tearDown(self):
|
|
torch.npu.set_task_queue_enable(self.original_mode)
|
|
super().tearDown()
|
|
|
|
def test_blocking_override(self):
|
|
blocking = os.environ.get("ASCEND_LAUNCH_BLOCKING", "0")
|
|
if blocking != "1":
|
|
print(" Skipped: ASCEND_LAUNCH_BLOCKING != 1")
|
|
return
|
|
torch.npu.set_task_queue_enable(2)
|
|
self.assertEqual(torch.npu.get_task_queue_enable(), 0,
|
|
"ASCEND_LAUNCH_BLOCKING=1 should force mode to 0")
|
|
|
|
def test_invalid_mode(self):
|
|
for invalid_mode in (-1, 3, -2):
|
|
with self.assertRaises(RuntimeError):
|
|
torch.npu.set_task_queue_enable(invalid_mode)
|
|
|
|
def test_mode_roundtrip_and_switch(self):
|
|
for mode in (0, 1, 2):
|
|
torch.npu.set_task_queue_enable(mode)
|
|
self.assertEqual(torch.npu.get_task_queue_enable(), mode)
|
|
|
|
def test_npu_graph_capture_rejected_at_mode2(self):
|
|
torch.npu.set_task_queue_enable(2)
|
|
g = torch.npu.NPUGraph()
|
|
static_in = torch.randn(16, 16, device="npu")
|
|
with self.assertRaisesRegex(RuntimeError, "TASK_QUEUE_ENABLE"):
|
|
g.capture_begin()
|
|
static_out = static_in * 2
|
|
g.capture_end()
|
|
|
|
def test_per_stream_queue_interaction_mode0(self):
|
|
env = os.environ.copy()
|
|
env["TASK_QUEUE_ENABLE"] = "1"
|
|
env["PER_STREAM_QUEUE"] = "1"
|
|
env.pop("ASCEND_LAUNCH_BLOCKING", None)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
script = os.path.join(tmp, "psq_mode0.py")
|
|
with open(script, "w") as f:
|
|
f.write(
|
|
"import torch\n"
|
|
"import torch_npu\n"
|
|
"s = torch.npu.Stream()\n"
|
|
"x = torch.randn(32, 32, device='npu')\n"
|
|
"with torch.npu.stream(s):\n"
|
|
" z = x + 1\n"
|
|
"s.synchronize()\n"
|
|
"assert torch.equal(z.cpu(), (x + 1).cpu())\n"
|
|
"torch.npu.set_task_queue_enable(0)\n"
|
|
"z2 = x + 2\n"
|
|
"torch.npu.synchronize()\n"
|
|
"assert torch.equal(z2.cpu(), (x + 2).cpu())\n"
|
|
"print('OK')\n"
|
|
)
|
|
res = subprocess.run(
|
|
[sys.executable, script],
|
|
capture_output=True, text=True, env=env, timeout=120)
|
|
self.assertEqual(
|
|
res.returncode, 0,
|
|
f"per-stream queue + dynamic switch failed:\n{res.stderr[-2000:]}")
|
|
|
|
def test_deterministic_level_not_reset_by_mode_switch(self):
|
|
original_level = torch_npu._C._npu_get_deterministic_level()
|
|
original_det = torch.are_deterministic_algorithms_enabled()
|
|
try:
|
|
for level in (1, 2):
|
|
torch_npu.npu.set_deterministic_level(level)
|
|
self.assertEqual(torch_npu._C._npu_get_deterministic_level(), level)
|
|
for mode in (0, 1, 2):
|
|
torch.npu.set_task_queue_enable(mode)
|
|
self.assertEqual(
|
|
torch_npu._C._npu_get_deterministic_level(), level,
|
|
f"deterministic level changed after switching task queue mode={mode}")
|
|
finally:
|
|
torch_npu.npu.set_deterministic_level(original_level)
|
|
torch.use_deterministic_algorithms(original_det)
|
|
|
|
|
|
class TestGetTaskQueueEnableTiming(TestCase):
|
|
|
|
GET_REPEATS = 3
|
|
WARMUP = 1000
|
|
REPEATS = 200
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
|
|
def tearDown(self):
|
|
super().tearDown()
|
|
|
|
def test_get_task_queue_enable_timing_all_paths(self):
|
|
for _ in range(self.GET_REPEATS):
|
|
_ = torch.npu.get_task_queue_enable()
|
|
|
|
torch.npu.set_task_queue_enable(1)
|
|
torch.npu.synchronize()
|
|
for _ in range(self.GET_REPEATS):
|
|
_ = torch.npu.get_task_queue_enable()
|
|
|
|
def test_get_task_queue_enable_call_latency(self):
|
|
print("\n" + "=" * 70)
|
|
print("[Timing] get_task_queue_enable() call latency")
|
|
print("=" * 70)
|
|
|
|
torch.npu.set_task_queue_enable(1)
|
|
for _ in range(self.WARMUP):
|
|
_ = torch.npu.get_task_queue_enable()
|
|
|
|
start = time.perf_counter_ns()
|
|
for _ in range(self.REPEATS):
|
|
_ = torch.npu.get_task_queue_enable()
|
|
fast_ns = (time.perf_counter_ns() - start) / self.REPEATS
|
|
print(f" fast path (set=1): {fast_ns:.1f} ns/call ({self.REPEATS} calls)")
|
|
|
|
torch.npu.set_task_queue_enable(2)
|
|
for _ in range(self.WARMUP):
|
|
_ = torch.npu.get_task_queue_enable()
|
|
|
|
start = time.perf_counter_ns()
|
|
for _ in range(self.REPEATS):
|
|
_ = torch.npu.get_task_queue_enable()
|
|
cold_ns = (time.perf_counter_ns() - start) / self.REPEATS
|
|
print(f" set=2 path: {cold_ns:.1f} ns/call ({self.REPEATS} calls)")
|
|
|
|
delta = cold_ns - fast_ns
|
|
ratio = cold_ns / fast_ns if fast_ns > 0 else 0
|
|
print(f" delta={delta:.1f} ns, ratio={ratio:.2f}x")
|
|
|
|
def test_multithread_get_dispatch(self):
|
|
print("\n" + "=" * 70)
|
|
print("[Multi] concurrent get_task_queue_enable under mode switching")
|
|
print("=" * 70)
|
|
|
|
for num_readers in [1, 2, 4]:
|
|
stop_event = threading.Event()
|
|
errors = []
|
|
get_counts = {}
|
|
|
|
def writer():
|
|
try:
|
|
for i in range(200):
|
|
torch.npu.set_task_queue_enable(i % 3)
|
|
time.sleep(0.001)
|
|
except Exception as e:
|
|
errors.append(f"writer: {e}")
|
|
finally:
|
|
stop_event.set()
|
|
|
|
def reader(tid):
|
|
try:
|
|
count = 0
|
|
while not stop_event.is_set():
|
|
mode = torch.npu.get_task_queue_enable()
|
|
if mode not in (0, 1, 2):
|
|
errors.append(f"reader {tid}: invalid mode {mode}")
|
|
count += 1
|
|
get_counts[tid] = count
|
|
except Exception as e:
|
|
errors.append(f"reader {tid}: {e}")
|
|
|
|
wt = threading.Thread(target=writer)
|
|
rts = [threading.Thread(target=reader, args=(i,)) for i in range(num_readers)]
|
|
|
|
start = time.perf_counter()
|
|
wt.start()
|
|
for t in rts:
|
|
t.start()
|
|
wt.join()
|
|
for t in rts:
|
|
t.join()
|
|
elapsed = time.perf_counter() - start
|
|
|
|
total_gets = sum(get_counts.values())
|
|
avg_ns = (elapsed / total_gets) * 1e9 if total_gets > 0 else 0
|
|
print(f" {num_readers} reader(s): {total_gets} gets, avg={avg_ns:.1f} ns/get")
|
|
self.assertEqual(len(errors), 0, f"Errors: {errors}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|