Skip to content

feat(profiler): support Ascend NPU profiling via torch_npu.profiler#1093

Open
brandneway wants to merge 2 commits into
vipshop:devfrom
brandneway:feat/profiler-npu-support
Open

feat(profiler): support Ascend NPU profiling via torch_npu.profiler#1093
brandneway wants to merge 2 commits into
vipshop:devfrom
brandneway:feat/profiler-npu-support

Conversation

@brandneway

Copy link
Copy Markdown

Summary

cache_dit.generate ... --profile crashed immediately on Ascend NPU because ProfilerContext hard-asserted device_type == "cuda". This PR adds NPU profiling support so --profile works end-to-end on Ascend devices.

Root cause: src/cache_dit/profiler.py asserted CUDA-only in both __init__ and __enter__, and used CUDA-specific APIs (torch.profiler.ProfilerActivity.CUDA, torch.cuda.synchronize(), torch.cuda.memory._record_memory_history).

Changes (src/cache_dit/profiler.py)

  • Backend selection: on NPU use torch_npu.profiler (which mirrors the torch.profiler API); keep torch.profiler on CUDA. Lazy-import torch_npu only when on NPU — zero impact on CUDA/CPU.
  • Relaxed guard: assert now allows cuda or npu (clearer error message otherwise). CPU remains unsupported.
  • Activity mapping: "GPU" maps to the platform accelerator activity (CUDA on CUDA, NPU on NPU); the accelerator name ("NPU") is also accepted as a synonym. Default activities ["CPU","GPU"] just work on NPU.
  • Synchronize: current_platform.synchronize() instead of torch.cuda.synchronize().
  • Memory: NPU records memory via the profiler's own profile_memory=True (when MEM requested); the CUDA-only torch.cuda.memory history/snapshot path is preserved unchanged for CUDA.
  • Trace export: torch_npu.profiler.export_chrome_trace only writes a plain .json, so on NPU the trace is exported to a temp json and gzip-compressed to keep the .trace.json.gz artifact convention used on CUDA.

Tests

  • Added tests/utils/test_profiler.py (CPU-safe): covers _supported_device(), _resolve_profiler_backend() shape, and the unsupported-device assertion guard. Passes on CPU CI (device-required case is skipped).

Verification (Ascend 910B4, container cache-dit-gty)

  • Before: --profile raised AssertionError: Torch ProfilerContext currently only supports CUDA devices.
  • After: python -m cache_dit.generate wan2.2_t2v --model-path ... --cpu-offload --attn _mindiesd_laser --compile --profile now enters/exits the profiler, collects CANN data, and exports a valid chrome trace (.trace.json.gz, parseable json).
  • Quick NPU smoke test: valid gzipped chrome trace produced (325 events) with no residual temp files.

Notes

  • No CUDA behavior change; all CUDA/NPU-specific branches are gated by current_platform.device_type.
  • For very heavy models (e.g. full Wan2.2-T2V with --profile-with-stack + --profile-record-shapes + 30 steps), NPU traces can be large and torch_npu export parsing is the dominant cost — inherent to the trace content, not this change.

changetheway added 2 commits July 24, 2026 02:08
Switch the NPU path from export_chrome_trace (single json) to
on_trace_ready=tensorboard_trace_handler + experimental_config, so the
full Ascend profiling output directory is produced: ASCEND_PROFILER_OUTPUT
(kernel_details.csv, step_trace_time.csv, trace_view.json, op_summary.csv,
op_statistic.csv, operator_details.csv, .db) plus PROF_XXX, FRAMEWORK and
logs. data_simplification=False keeps every generated file.

- profiler_level configurable via CACHE_DIT_NPU_PROFILER_LEVEL (Level0 default)
- sanitize worker_name to the allowed [A-Za-z0-9_-] charset
- locate the produced *_ascend_pt dir and expose it via trace_path
- CUDA path unchanged
@DefTruth

Copy link
Copy Markdown
Member

Thanks for you contribution! By the way, can you should some test logs about this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants