Open
【硬件解耦任务】torch_npu CompileThreadPool 不传播 thread-local config,strict_static_triton_launcher 在 worker 中失效 #3975
王刚Add on Aug 13
Aug 13 Added label: bug,enhancement
Aug 13 associated Kanban: FrameworkPTAdapter 版本issue看板
Aug 13 Added label: triage-review
Aug 17 Added label: help-wantedbot-triagedevent: accelerator-agnostic; Removed label: triage-review
Aug 17 Added label: help-wantedbot-triagedevent: accelerator-agnostic; Removed label: triage-review
TorchNPU-Bot
Aug 17 Comments:
Aug 17 Comments:
检测到社区任务相关 issue,自动添加标签:event: accelerator-agnostic、help-wanted


环境信息
问题描述
torch_npu 的 inductor 编译使用自己的
CompileThreadPool(ThreadPoolExecutor),不传播torch._inductor.config的 thread-local 设置到 worker 线程。导致strict_static_triton_launcher等 config.patch 设置在 worker 中失效。此外,NPU 的
_precompile_config直接返回TritonCompileResultNpu,不调用 upstream 的can_statically_launch,导致 static launcher 路径完全被跳过。复现步骤
import torch import torch_npu import torch_npu._inductor import torch._inductor.config from torch._inductor.runtime.triton_compat import triton, tl torch._inductor.config.comprehensive_padding = True torch._inductor.config.use_static_triton_launcher = True @triton.jit def custom_kernel(arg_0, arg_1): x = tl.load(arg_0) y = arg_1 tl.store(arg_0, x + y) with torch._inductor.config.patch({ "use_static_triton_launcher": True, "strict_static_triton_launcher": True, "compile_threads": 1, }): @torch.compile def foo(x): custom_kernel[1,](x, 5) return x x = torch.randn(1, device="npu:0") # 期望抛出 InductorError: CannotStaticallyLaunchKernel: User defined triton kernel # 实际不抛异常 result = foo(x)错误信息
测试期望
torch._inductor.exc.InductorError(包含CannotStaticallyLaunchKernel: User defined triton kernel),但实际不抛异常。根因分析
根因 1: CompileThreadPool 不传播 thread-local config
torch_npu/_inductor/runtime/triton_heuristics.py第 102-123 行:class CompileThreadPool: def __init__(self): self.pool = ThreadPoolExecutor(max_workers=npu_config.max_precompiled_thread_num) ... compile_thread_pool = CompileThreadPool()torch_npu 使用自己的
CompileThreadPool,不受TORCHINDUCTOR_COMPILE_THREADS=1控制。torch._inductor.config是 thread-local 的,config.patch的设置不传播到 worker 线程。实测验证:在 worker 线程中
torch._inductor.config.strict_static_triton_launcher为False(即使主线程设了True)。根因 2: _precompile_config 不调用 can_statically_launch
triton_heuristics.py第 1207/1302 行,NPU 的_precompile_config直接返回TritonCompileResultNpu:def _precompile_config(self, cfg: Config) -> TritonCompileResultNpu: ... return TritonCompileResultNpu(binary, cfg, compile_meta, self.inductor_meta)upstream 的
_precompile_config(第 1459 行)会调用can_statically_launch,如果返回非 None 则创建StaticTritonCompileResult(走 static launcher 路径)。NPU 重写了这个方法但跳过了这一步。影响
strict_static_triton_launcher在 worker 中为False(即使主线程设了True)can_statically_launch的 except 分支不会 re-raiseCannotStaticallyLaunchKerneltest_incompatible_code期望InductorError但实际不抛修复建议
CompileThreadPool需要像 upstream 的async_compile一样传播 thread-local config 到 worker 线程_precompile_config应该调用can_statically_launch(目前直接返回TritonCompileResultNpu,不走 static launcher 路径)影响的测试
test/inductor/test_static_triton_launcher.py::TestStaticTritonCompileResult::test_incompatible_code