Open
【硬件解耦任务】torch_npu CompileThreadPool 不传播 thread-local config,strict_static_triton_launcher 在 worker 中失效 #3975
王刚Add on  Aug 13
王刚
王刚Collaborators
Aug 13 Create

环境信息

项目 版本
PyTorch 2.14.0.dev20260811+cpu
torch_npu 2.14.0+gitc775ad3
triton-ascend 3.6.0+git0cfb1708
CANN 9.1.0 GA
NPU Ascend 910B4
Python 3.12.13

问题描述

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)

错误信息

AssertionError: InductorError not raised by <lambda>

测试期望 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 重写了这个方法但跳过了这一步。

影响

  1. strict_static_triton_launcher 在 worker 中为 False(即使主线程设了 True)
  2. can_statically_launch 的 except 分支不会 re-raise CannotStaticallyLaunchKernel
  3. test_incompatible_code 期望 InductorError 但实际不抛

修复建议

  1. torch_npu 的 CompileThreadPool 需要像 upstream 的 async_compile 一样传播 thread-local config 到 worker 线程
  2. 或者:NPU 的 _precompile_config 应该调用 can_statically_launch(目前直接返回 TritonCompileResultNpu,不走 static launcher 路径)

影响的测试

test/inductor/test_static_triton_launcher.py::TestStaticTritonCompileResult::test_incompatible_code

likedislike
王刚王刚Collaborators
Aug 13 Added label: bug,enhancement
王刚王刚Collaborators
Aug 13 associated Kanban: FrameworkPTAdapter 版本issue看板
TorchNPU-BotTorchNPU-BotCollaborators
Aug 13 Added label: triage-review
TorchNPU-BotTorchNPU-BotCollaborators
Aug 17 Added label: help-wantedbot-triagedevent: accelerator-agnostic; Removed label: triage-review
TorchNPU-Bot
TorchNPU-BotCollaborators
Aug 17 Comments:

检测到社区任务相关 issue,自动添加标签:event: accelerator-agnostic、help-wanted

likedislike