kali20gakki/msAgent / skills/profiler/ascend-profiler-collect-adaption
ascend-profiler-collect-adaption
VerifiedAdapt torch_npu.profiler interface for AI frameworks and training/inference code on Ascend NPU. Use this skill whenever users need to integrate profiling, add performance data collection, or instrument PyTorch code with torch_npu.profiler on Huawei Ascend NPU platforms. Triggers on: "torch_npu profiler", "NPU profiling", "Ascend profiler", "profiler adaptation", "profile training", "profile inference", "add profiler to framework", "性能采集", "profiling 数据", "昇腾 profiler", and any request to add performance monitoring to Ascend NPU code.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.1 KB |
| references/api_reference.md | markdown | 2.1 KB |
| references/inference_best_practices.md | markdown | 5.2 KB |
| references/rl_best_practices.md | markdown | 7.3 KB |
| references/training_best_practices.md | markdown | 7.0 KB |
references/inference_best_practices.md
View on GitHub# 推理场景 — torch_npu.profiler 适配最佳实践
> 代表框架:vLLM-Ascend、SGLang
## 采集控制方式:运行时 HTTP API(curl 命令)
涉及文件(以 `vLLM-Ascend` 为例):
| 文件 | 内容 |
|---|---|
| [`vllm_ascend/worker/profiler.py`](https://github.com/vllm-project/vllm-ascend/blob/main/vllm_ascend/worker/profiler.py) | `TorchNPUProfilerWrapper` 类,封装 `torch_npu.profiler.profile` 的创建与启停 |
| [`vllm_ascend/worker/worker.py`](https://github.com/vllm-project/vllm-ascend/blob/main/vllm_ascend/worker/worker.py) | `Worker.profile()` 方法,按 `is_start` 参数控制 profiler 生命周期 |
| [`vllm_ascend/entrypoints/api_server.py`](https://github.com/vllm-project/vllm-ascend/blob/main/vllm_ascend/entrypoints/api_server.py) | HTTP 路由 `/start_profile` / `/stop_profile`,转发到 engine → executor → worker |
vLLM 服务启动时通过 `--profiler-config` 命令行参数传入 Profiler 相关配置,该参数在启动阶段被解析并存储于 `vllm_config.profiler_config` 中。`vLLM-Ascend` 的 `NPUWorker` 在初始化时接收完整的 `vllm_config` 对象,从中提取 `profiler_config` 并缓存为成员变量 `self.profiler_config`。至此,Profiler 配置参数完成了从 CLI 到 Worker 实例的完整透传链路,实现了外部对采集行为的灵活控制。
```python
# vLLM服务启动时 profiler 配置示例
python3 -m vllm.entrypoints.openai.api_server ... --profiler-config '{"profiler": "torch", "torch_profiler_dir": "./vllm_profile", "torch_profiler_with_stack": false}'
```
vLLM 对外提供 `/start_profile` 和 `/stop_profile` 两个 HTTP 接口,用于接收来自 curl 命令的 Profiler 启停请求。请求首先由上游 `vLLM` 的 HTTP 路由层拦截并转发至引擎层,引擎层通过 `collective_rpc("profile", ...)` 将控制指令广播至所有 `NPUWorker` 实例。每个 Worker 在接收到 profile RPC 请求后,根据 `is_start` 参数的值执行相应操作:若 `is_start=True`,则调用 `self.profiler.start()` 启动采集;若 `is_start=False`,则调用 `self.profiler.stop()` 停止采集并落盘。该机制实现了通过外部 curl 命令远程控制 Profiler 启停的能力,无需重启服务即可灵活进行性能数据的按需采集。
```bash
# 启动采集
curl -X POST http://localhost:8000/start_profile
# 停止采集
curl -X POST http://localhost:8000/stop_profile
```
`TorchNPUProfilerWrapper` 继承自 `WorkerProfiler` 基类,负责封装 `torch_npu.profiler.profile` 的创建、启动与停止逻辑。基类 `WorkerProfiler` 已对外暴露 `start()`、`stop()`、`step()` 三个标准接口,其内部实现采用模板方法模式,分别调用派生类需实现的 `_start()`、`_stop()`、`_profiler_step()` 钩子方法。因此,`TorchNPUProfilerWrapper` 只需重写这三个私有钩子方法即可完成具体采集行为的注入,外部调用方统一通过基类的 `start()`/`stop()`/`step()` 接口与 `Profiler` 交互,无需感知底层实现差异:
```python
class TorchNPUProfilerWrapper(WorkerProfiler):
def __init__(self, profiler_config, trace_name):
super().__init__(profiler_config)
self.profiler = self._create_profiler(profiler_config, trace_name)
def _create_profiler(self, profiler_config, trace_name):
experimental_config = _ExperimentalConfig(
export_type=ExportType.Text,
profiler_level=ProfilerLevel.Level1,
aic_metrics=AiCMetrics.PipeUtilization,
data_simplification=True,
)
return torch_npu.profiler.profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.NPU],
profile_memory=profiler_config.torch_profiler_with_memory,
with_modules=profiler_config.torch_profiler_with_stack,
experimental_config=experimental_config,
on_trace_ready=tensorboard_trace_handler(
profiler_config.torch_profiler_dir, worker_name=trace_name,
),
)
def _start(self): self.profiler.start()
def _stop(self): self.profiler.stop()
def _profiler_step(self): return True
```
`Worker.profile()` 按 `is_start` 参数控制 profiler 生命周期,`execute_model` 中调用 `step()`:
```python
class Worker:
def __init__(self, ...):
self.profiler = None
self.profiler_config = profiler_config # 启动时注入的配置
def profile(self, is_start=True, profile_prefix=None):
if profiling_not_enabled(self.profiler_config):
raise RuntimeError("profiling not enabled")
if is_start:
trace_name = build_trace_name(profile_prefix, self.rank)
if self.profiler is None:
self.profiler = TorchNPUProfilerWrapper(self.profiler_config, trace_name)
self.profiler.start()
else:
if self.profiler is None: return
self.profiler.stop()
def execute_model(self, scheduler_output, ...):
if self.profiler is not None:
self.profiler.step()
return self.model_runner.execute_model(scheduler_output, ...)
```
HTTP API 调用链:`POST /start_profile` → `engine.start_profile()` → `executor.profile(True)` → `worker.profile(True)`
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |