已开启
Introduce PrivateUse1 Profiler Manager Architecture #45369
Introduce PrivateUse1 Profiler Manager Architecture #45369
已开启
denis_g创建于 8月26日
denis_g
denis_g
8月26日

【Source of Change】

This PR introduces a common architecture for managing PrivateUse1 activity profiler backends in torch_npu.

The purpose is to decouple PrivateUse1 profiler registration and session management from the existing MSPTI implementation, provide a common foundation for multiple profiler backends, and extend the NPU-specific experimental configuration mechanism.

The existing MSPTI profiling behavior is preserved. ACLprof support is introduced as a nonfunctional backend stub for future implementation.

【Modification】

The following changes are included:

1. Common profiler abstraction

Introduced NPUActivityProfiler as the common abstraction for PrivateUse1 profiler backends.

2. PrivateUse1 profiler plugin

Added NPUActivityProfilerPlugin as the backend-neutral plugin registered through:

REGISTER_PRIVATEUSE1_PROFILER

3. Backend factory

Added NPUActivityProfilerFactory to select the profiler backend for each profiling session.

Backend selection behavior:

default / empty config -> MSPTI
backend=mspti          -> MSPTI
backend=aclprof        -> ACLprof stub
invalid backend        -> MSPTI fallback

Backend selection remains explicit.

The additional profiler_level and export_type parameters do not implicitly select ACLprof.

4. Profiler session manager

Added PrivateUse1ProfilerManager as the libkineto::IActivityProfilerSession implementation responsible for delegating profiler lifecycle and trace-processing operations to the selected backend.

The resulting architecture is:

REGISTER_PRIVATEUSE1_PROFILER
    -> NPUActivityProfilerPlugin
    -> NPUActivityProfilerFactory
    -> MSPTI / ACLprof
    -> PrivateUse1ProfilerManager
    -> libkineto::IActivityProfilerSession

5. MSPTI backend integration

Refactored MsptiActivityProfiler to inherit from the common NPUActivityProfiler interface.

Existing MSPTI activity collection, callbacks, buffer handling, draining, conversion, correlation, and metadata processing are not modified by this PR.

6. ACLprof backend stub

Added an ACLprof backend stub to establish the architectural boundary for future ACLprof implementation.

The stub supports the profiler lifecycle but does not collect or convert ACLprof activities.

Current behavior:

start()        -> RECORDING
stop()         -> PROCESSING
processTrace() -> completes without device activities

When trace processing is requested, the stub emits:

ACLprof activity collection is not implemented yet

Functional ACLprof collection and export are intentionally excluded from this PR.

7. Extended NPU experimental configuration

Extended _NPUExperimentalConfig to support three optional parameters:

  • backend
  • profiler_level
  • export_type

The existing backend-only configuration remains compatible.

Example:

from torch_npu.profiler import _NPUExperimentalConfig
from torch_npu.profiler.experimental_config import ProfilerLevel, ExportType

npu_config = _NPUExperimentalConfig(
    backend="aclprof",
    profiler_level=ProfilerLevel.Level1,
    export_type=ExportType.Db,
)

experimental_config = torch.profiler._ExperimentalConfig(
    custom_profiler_config=str(npu_config)
)

The serialized configuration is:

{"backend":"aclprof","profiler_level":"Level1","export_type":["db"]}

The configuration implementation provides:

  1. Backward-compatible JSON serialization.
  2. Deterministic export-type normalization.
  3. Validation of supported profiler levels and export types.
  4. Scalar and nonempty list support for export types.
  5. Python-to-C++ configuration propagation through custom_profiler_config.
  6. Per-session configuration ownership and isolation.
  7. Compatibility handling for different PyTorch experimental-configuration representations.

Backend selection depends exclusively on backend.

The default backend remains MSPTI, even when profiler_level or export_type is provided without an explicit backend.

The additional configuration values are validated, parsed, and retained by the selected profiler instance. They are not applied to ACLprof collection or export in this PR.

8. Extended test coverage

Added and extended C++ and Python tests covering:

  • Backend selection and fallback.
  • Plugin-to-manager creation.
  • Manager lifecycle and trace delegation.
  • Independent sequential ACLprof stub sessions.
  • NPU experimental configuration serialization.
  • Profiler-level validation.
  • Export-type validation and normalization.
  • Backward compatibility of backend-only configuration.
  • Standard PyTorch custom_profiler_config compatibility.
  • Configuration ownership and session isolation.
  • Invalid configuration handling.
  • Pickle round-trip compatibility.
  • Lazy serialization-state fallback behavior.

9. Scope limitation

This PR provides the PrivateUse1 profiler management architecture and configuration infrastructure only.

It does not implement functional ACLprof activity collection, device activity conversion, database export, or report generation.

【Documentation Changes】

No user-facing documentation changes.

This PR introduces internal profiler architecture and extends an experimental configuration helper. Functional ACLprof usage documentation is deferred until the corresponding backend implementation is available.

【Interface Changes】

No changes to stable customer-facing APIs or cross-repository interfaces are introduced.

The internal PrivateUse1 profiler architecture is extended, and the experimental Python helper _NPUExperimentalConfig now accepts optional profiler_level and export_type parameters in addition to backend.

The configuration is transferred through the existing PyTorch custom_profiler_config mechanism.

Existing backend-only configuration remains compatible:

_NPUExperimentalConfig(backend="aclprof")

The default backend remains MSPTI. ACLprof selection requires an explicit backend="aclprof".

【Functional Verification】

Linux / Ascend hardware integration

Integration validation was performed using:

PyTorch:  2.14.0.dev20260719+cpu
torch_npu: 2.14.0.dev20260719+gita58699e
CANN:     9.1.0
Platform: Linux aarch64 / Ascend NPU

The test uses the standard torch.profiler.profile() API with CPU and NPU activities, scheduled profiling, prof.step(), and a CPU trace callback.

Four configuration scenarios were executed:

All four scenarios completed without runtime errors.

The following behavior was observed:

  • Profiler start and stop operations completed successfully.
  • The scheduled trace callback executed successfully.
  • Each scenario produced 15 CPU trace events, including 3 tagged events.
  • The ACLprof scenario invoked AclprofActivityProfiler::processTrace(), confirmed by the expected stub warning.
  • MSPTI scenarios completed without the ACLprof stub warning.
  • Invalid profiler-level and export-type inputs were rejected as expected.

Verification limitations

The integration test confirms runtime execution of the ACLprof stub through the standard PyTorch profiling API.

MSPTI scenarios completed successfully, but runtime backend selection was not independently instrumented in those scenarios. Backend selection behavior is additionally covered by C++ unit tests.

No device activities were present in the collected traces. Therefore, full MSPTI hardware activity-collection regression is not claimed by these results.

The runtime test also does not directly inspect retained configuration values inside the C++ backend; parsing and retention are verified separately by C++ and bridge tests.

Functional ACLprof collection and DB export are outside the scope of this PR.

【CheckList】

likedislike
合并受阻
denis_gdenis_g
8月26日 创建了 pull request,commit 61a8b698
atomgit-bot
atomgit-bot
8月26日 评论:

变更摘要

本 PR 为 torch_npu 引入 PrivateUse1 活动 profiler 后端管理的基础架构,将 PrivateUse1 profiler 的注册与会话管理从原有 MSPTI 实现中解耦,形成可支持多 profiler 后端的通用架构。注册链路为 REGISTER_PRIVATEUSE1_PROFILER → NPUActivityProfilerPlugin → NPUActivityProfilerFactory → MSPTI/ACLprof → PrivateUse1ProfilerManager → libkineto::IActivityProfilerSession。后端选择由 CUSTOM_CONFIG 中的 backend 字段决定,默认及非法配置回退到 MSPTI,backend="aclprof" 选择 ACLprof 桩实现;同时新增 _NPUExperimentalConfig 用于在 Python 侧传递 NPU 特有后端配置,现有默认 MSPTI 行为保持不变。

主要改动

  • 新增公共后端抽象与工厂:新增 NPUActivityProfiler 接口(start/stop/processTrace/errors/设备信息等)与 NPUActivityProfilerFactory::create,根据 config.getCustomConfig() 解析的 backend 字段在 MsptiActivityProfiler 与 AclprofActivityProfiler 之间选择,未指定或非法值时回退 MSPTI。
  • 新增插件与管理器:NPUActivityProfilerPlugin 通过 REGISTER_PRIVATEUSE1_PROFILER(无宏时回退 libkineto::api().registerProfilerFactory)注册,其 configure 调用工厂创建后端;PrivateUse1ProfilerManager 作为 libkineto::IActivityProfilerSession 将生命周期、processTrace、相关 ID 推送/弹出等操作委托给所选后端。
  • 重构 MsptiActivityProfiler:改为继承 NPUActivityProfiler,删除原 MsptiActivityProfilerPoc 类及其 configure/注册逻辑(privateUse1Requested 移至 npu_activity_profiler.cpp),MSPTI 活动采集、回调与缓冲等既有逻辑不变。
  • 新增 ACLprof 后端桩:AclprofActivityProfiler 仅实现生命周期(start 进入 RECORDING、stop 进入 PROCESSING),processTrace 不产出设备活动并发出 ACLprof activity collection is not implemented yet 警告,为后续实现划定架构边界。
  • 新增 Python 配置与测试:新增 _NPUExperimentalConfig(继承 torch._C._profiler._ExperimentalConfig,通过 custom_profiler_config 序列化 {"backend": ...})并在 torch_npu/profiler/__init__.py 导出;新增 C++ 测试(后端选择、插件→管理器创建、生命周期与 trace 委托、顺序独立 ACLprof 桩会话)及 Python 测试(校验 _NPUExperimentalConfig 序列化内容)。
likedislike
不准确?
atomgit-bot
atomgit-bot
8月26日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
ascend-robotascend-robot成员
8月26日 添加了label:ascend-cla/yes
此处折叠了344条消息 查看更多
hhz0hhz0成员
1 天前 关联了里程碑:v26.3.0
chenhao_1209成员
1 天前 评论:

/lgtm

likedislike
梁松伟
梁松伟成员
1 天前 评论:

/approve

likedislike
ascend-robot
ascend-robot成员
1 天前 评论:

The following users do not have permission to comment /lgtm or /approve on any module in this PR:
/lgtm: eejiechu

likedislike
ascend-robotascend-robot成员
1 天前 添加了label:approved