面向 PyTorch 训练/推理工作流的实用工具集,核心覆盖:
- 性能采集与分析(NSYS / NCU / 统一 metrics)
- 运行时追踪与 Hook(NVTX / module hooks)
- 分布式辅助(时钟同步 / etcd barrier / sequence parallel padding)
- 内存诊断(snapshot / OOM flag / GPU tracker)
- 产物落盘与离线分析(dump / CSV)
flowchart TD
A[开始: 我要做什么] --> B{目标}
B -->|训练全局性能| C[profiling/templates + nsys]
B -->|单kernel瓶颈| D[profiling/ncu]
B -->|代码内埋点追踪| E[tracing + hooks]
B -->|分布式辅助能力| F[distributed]
B -->|内存诊断| G[memory]
B -->|基础工具| H[core]
cd my_utils
pip install -e .可选依赖(按需):
pip install -e .[profiling,tensordict,etcd,nvml,nvtx,system,megatron]常用组合:
# 仅安装 my_utils,不动你现有 torch/cuDNN 环境
pip install -e .
# 安装所有可选依赖(不含 torch)
pip install -e .[all]
# 安装所有可选依赖(含 torch)
pip install -e .[all_with_torch]NSYS 快速采集:
bash my_utils/profiling/templates/run_nsys_quick.sh -- python train.py --config cfg.yamlNSYS 离线分析:
myutils-profile nsys-analyze --sqlite ./train_rank0.sqlite --output ./nsys_analyze.jsonNCU 完整采集:
python my_utils/profiling/ncu/run_ncu_quick_yaml.py \
--config my_utils/profiling/ncu/ncu_full_collection.yamlNCU 报告分析:
myutils-profile ncu-report-analyze --report ./run.ncu-rep --top-k 20 --prettyfrom my_utils.core import setup_logging_and_timer
logger, timer = setup_logging_and_timer(
logger_name="train",
log_file="train.log",
use_cuda=True,
rank=0,
)
timer.start("forward")
# ... your forward ...
timer.stop("forward")from my_utils.tracing import create_labeler
labeler = create_labeler(preferred="auto")
with labeler.range("train_step"):
# ... your step ...
pass- my_utils/profiling: 统一 profiling 入口(NSYS/NCU/metrics)
- my_utils/core: logger/timer/通用工具
- my_utils/tracing: NVTX labeler 与 trace 辅助
- my_utils/hooks: forward hook / module trace / module profiler
- my_utils/distributed: clock sync / etcd barrier / pad helpers
- my_utils/memory: snapshot / OOM / GPU memory tracker
- my_utils/artifacts: dump 与 CSV 离线分析
- my_utils/legacy_profilers: 历史 profiler 兼容层
- 旧导入路径(例如
from my_utils.utils import MyTimer)仍可用。 - 新代码建议使用分层路径(例如
from my_utils.core import MyTimer)。 my_utils/__init__.py内置了 legacy module aliases,便于旧项目平滑迁移。