Skip to content
cccskills
FreePublished on GitHub

ascend-npu-snapshot-analyzer

分析 PyTorch memory snapshot pickle 文件(_dump_snapshot 导出的内存快照),提供内存峰值、碎片、泄漏、OOM 检测与交互式 HTML 报告。

仅当用户明确提及 memory snapshot、内存快照、_dump_snapshot、pickle 内存文件 或需要对 snapshot pickle 做内存分析时才触发,不处理一般的 NPU 性能分析、SQLite 查询或通用内存问题。

The English summary is being prepared. Showing the original description.

See how to install

Files included (8)

  • SKILL.md6.1 KB
  • references/analysis_methodology.md4.5 KB
  • references/analysis_templates.md5.4 KB
  • references/snapshot_schema.md2.7 KB
  • scripts/echarts.min.js1005.1 KB
  • scripts/snapshot_analyze.py41.9 KB
  • scripts/snapshot_queries.py16.0 KB
  • scripts/snapshot_to_db.py16.6 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

Ascend NPU Memory Snapshot Analyzer

1. 技能目标

分析 torch_npu.npu.memory._dump_snapshot() 导出的 pickle 文件,提供多维度的内存分析能力和交互式可视化报告。

2. 输入规范

  • 输入数据:torch_npu.npu.memory._dump_snapshot() 或 torch.cuda.memory._dump_snapshot() 导出的 pickle 文件(支持 dict 和 list 两种格式)
  • 前置条件:需要先通过 snapshot_to_db.py 将 pickle 转换为 SQLite DB

3. 分析能力

Track A:快速通道(CTE 宏)

以下 CTE 宏可直接嵌入 SQL 查询,覆盖 80% 常见分析场景:

-- 宏1:设备内存概览
WITH device_overview AS (
    SELECT
        d.device_index,
        SUM(s.total_size)      AS reserved_bytes,
        SUM(s.allocated_size)  AS allocated_bytes,
        SUM(s.active_size)     AS active_bytes,
        ROUND((SUM(s.total_size) - SUM(s.allocated_size)) * 100.0 / NULLIF(SUM(s.total_size), 0), 2) AS frag_pct,
        COUNT(s.id)            AS segment_count,
        SUM(s.is_expandable)   AS expandable_segments
    FROM segments s
    JOIN devices d ON s.device_id = d.id
    GROUP BY d.device_index
)

-- 宏2:块状态分布
WITH block_state_dist AS (
    SELECT
        d.device_index,
        b.state,
        COUNT(b.id)      AS block_count,
        SUM(b.size)      AS total_size,
        AVG(b.size)      AS avg_size
    FROM blocks b
    JOIN segments s ON b.segment_id = s.id
    JOIN devices d ON s.device_id = d.id
    GROUP BY d.device_index, b.state
)

-- 宏3:扩容事件时间线
WITH expansion_timeline AS (
    SELECT
        d.device_index,
        t.trace_index,
        t.action,
        t.size,
        t.addr,
        cs.frames_json
    FROM traces t
    JOIN devices d ON t.device_id = d.id
    LEFT JOIN call_stacks cs ON t.stack_id = cs.id
    WHERE t.action IN ('segment_alloc', 'segment_map', 'segment_free', 'segment_unmap')
    ORDER BY d.device_index, t.trace_index
)

-- 宏4:大块分配 TOP N
WITH top_allocations AS (
    SELECT
        d.device_index,
        b.size,
        b.requested_size,
        b.state,
        cs.frames_json
    FROM blocks b
    JOIN segments s ON b.segment_id = s.id
    JOIN devices d ON s.device_id = d.id
    LEFT JOIN call_stacks cs ON b.stack_id = cs.id
    WHERE b.state = 'active_allocated'
    ORDER BY b.size DESC
    LIMIT 20
)

Track B:深度分析(脚本调用)

# 总体概览
python scripts/snapshot_analyze.py snapshot.db --mode overview

# 峰值分析(时序重放 + 峰值贡献者)
python scripts/snapshot_analyze.py snapshot.db --mode peak

# 碎片分析(整体 + 逐段 + 假性碎片)
python scripts/snapshot_analyze.py snapshot.db --mode fragment

# 泄漏检测(单调增长 + 长生命周期 + 堆栈归因)
python scripts/snapshot_analyze.py snapshot.db --mode leak

# OOM 分析(上下文回溯 + 根因推断)
python scripts/snapshot_analyze.py snapshot.db --mode oom

# 跨快照对比(ATTACH DATABASE)
python scripts/snapshot_analyze.py snapshot.db --mode compare --ref other.db

# 全模式 + HTML 报告
python scripts/snapshot_analyze.py snapshot.db --mode all -o report.html

4. 工作流

初始分析流程

  1. 用户提供 snapshot pickle 文件路径
  2. 转换: python scripts/snapshot_to_db.py snapshot.pkl
  3. 总览: python scripts/snapshot_analyze.py snapshot.db --mode overview
  4. 根据总览结果,按需执行深度分析

问题诊断流程

用户问题
    │
    ├─ "峰值/最高内存" → --mode peak
    ├─ "碎片/内存利用率" → --mode fragment
    ├─ "泄漏/不释放" → --mode leak
    ├─ "OOM/崩溃" → --mode oom
    ├─ "对比/差异" → --mode compare --ref other.db
    └─ "全面分析" → --mode all -o report.html

5. 输出规范

问题 → 证据 → 建议 三段式

每个分析结果包含:

  • 问题描述:现象是什么
  • 数据证据:具体数值和来源
  • 可执行建议:优先级 + 预期效果 + 参考链接

报告模板路由

分析结果按五层金字塔组织:

  1. 总览 & 一键结论(健康状态 + 核心指标卡片)
  2. 设备详情(各设备对比 + Segment 类型分布)
  3. 深度分析(峰值/碎片/泄漏/OOM 按需展开)
  4. 堆栈归因(TOP 10 堆栈 + 分配量占比)
  5. 优化建议(高/中优先级 + 类别标签 + 预期效果)

详见 references/analysis_templates.md。

6. 数据格式参考

详见 references/snapshot_schema.md。

7. 分析方法论

详见 references/analysis_methodology.md。

8. 脚本工具

脚本用途
scripts/snapshot_to_db.pypickle → SQLite DB 转换
scripts/snapshot_queries.pySQL 查询函数库(供 Agent 直接调用)
scripts/snapshot_analyze.py高层分析(6 种模式 + HTML 报告)

9. 使用示例

# 1. 转换
python scripts/snapshot_to_db.py snapshot_before.pkl

# 2. 总览
python scripts/snapshot_analyze.py snapshot_before.db --mode overview

# 3. 碎片分析
python scripts/snapshot_analyze.py snapshot_before.db --mode fragment

# 4. 泄漏检测
python scripts/snapshot_analyze.py snapshot_before.db --mode leak

# 5. 生成完整报告
python scripts/snapshot_analyze.py snapshot_before.db --mode all -o report.html

10. 约束与限制

  • 仅支持离线 pickle 文件,不支持实时采集
  • Phase 1 不解析 device_traces 的时序关联(仅存储)
  • 大文件 (>1GB) 导入时建议使用 --no-indexes 先导入,后手动建索引

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

当用户直接要求进行数据集压缩测评,或在量化调优过程中需要使用 AISBench 评测工具输出数据集精度等场景时使用。 本 Skill 负责:使用 AISBench 数据集压缩代码、用 RBF Kernel Herding 算法生成压缩后的数据子集(coreset),并按用户目标交付两种产物之一: ① 接入量化调优主流程:产出独立的 coreset 数据集配置(不覆盖原始数据),回传「全集 config_name + coreset config_name」给编排层,供 evaluation.yaml 切换; ② 仅数据集压缩测评(不做量化调优):基于 example 模板生成「全集测试脚本」和「子集测试脚本」两份 shell 脚本。 暂时支持 AIME 2025 和 GPQA,其他数据集需要再结合 aisbench tools目录下 herding_coreset_selector 源码进行分析。

The English summary is being prepared. Showing the original description.

kali20gakki/msAgent32Updated Oct 22, 2026

Ascend cluster comparison tool. Invoke when user asks to compare two cluster datasets (DB or TEXT), or to generate cluster_analysis_output from raw profiling data via msprof-analyze and then compare.

The English summary is being prepared. Showing the original description.

kali20gakki/msAgent32Updated Oct 22, 2026

专门用于 Ascend 集群 Profiling 性能数据的“快慢卡”诊断专家技能。当用户提供【集群性能数据目录/路径】并要求分析【快慢卡】、【慢节点】、【负载不均衡】或【集群瓶颈】时,必须触发此技能。该技能会自动接收集群路径,调度相关工具输出快慢卡的宏观定性与微观根因(如 Host 下发瓶颈、算子计算劣化)。

The English summary is being prepared. Showing the original description.

kali20gakki/msAgent32Updated Oct 22, 2026

Analyze Ascend NPU collective communication profiling data with a DB-first workflow. Use when the user provides `cluster_analysis_output/cluster_analysis.db`, rank-level `analysis.db`, rank-level `ascend_pytorch_profiler_{rank_id}.db`, together with `profiler_info.json`, and asks about HCCL or hcom communication cost, collective communication TOP ops, wait time, slow rank/straggler, Notify Wait, bandwidth, retry, relay, SDMA/RDMA/HCCS links, communication matrix, or Ascend communication fault patterns.

The English summary is being prepared. Showing the original description.

kali20gakki/msAgent32Updated Oct 22, 2026

Analyze Ascend NPU computation-side profiling data for single-card runs or a selected rank from multi-card runs. Use this skill when the user asks to diagnose computation bottlenecks, AI Core / AI Vector / AICPU hotspots, dynamic shape overhead, block dim issues, redundant TransData/Transpose/Cast, cross-stream waits, frequency/runtime-state issues, or fusion opportunities.

The English summary is being prepared. Showing the original description.

kali20gakki/msAgent32Updated Oct 22, 2026

针对昇腾 NPU 上的 PyTorch 模型,识别可做融合算子替换的代码段、替换为 torch_npu 融合算子并做性能/精度验证。当用户提到融合算子、NPU 算子替换、RMSNorm/SwiGLU/RoPE/Attention 融合、torch_npu 亲和算子等关键词是,使用本 Skill。

The English summary is being prepared. Showing the original description.

kali20gakki/msAgent32Updated Oct 22, 2026

See all skills by kali20gakki

Report a problem with this skill