已合并
test(distributed):add test for torch.distributed.elastic.metrics.api.ConsoleMetricHandler, torch.distributed.elastic.metrics.api.NullMetricHandler and torch.distributed.elastic.metrics.configure #34726
m0_73361278创建于 4月29日
test(distributed):add test for torch.distributed.elastic.metrics.api.ConsoleMetricHandler, torch.distributed.elastic.metrics.api.NullMetricHandler and torch.distributed.elastic.metrics.configure #34726
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 m0_73361278 的贡献)4月29日 创建了 pull request,commit 515893ec
ascend-robot
4月29日 评论:
4月29日 评论:
ascend-robot
4月29日 评论:
4月29日 评论:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| test | ✅ 李伟, huangjingwei (2/2) | ✅ 李伟 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
m0_73361278, thanks for your pull request. All authors of the commits have signed the CLA. 👍


4月29日 添加了label:ascend-cla/yes
ascend-robot
4月29日 评论:
4月29日 评论:
当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.7.1 | ||
| v2.11.0 | ||
| v2.10.0 | ||
| v2.9.0 | ||
| v2.8.0 |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


m0_73361278
4月29日 评论:
4月29日 评论:
compile


4月29日 添加了label:ci-pipeline-running
4月29日 删除了label:ci-pipeline-running
4月29日 添加了label:ci-pipeline-failed
ascend-robot
4月29日 评论:
4月29日 评论:
流水线 PR-pipeline_pytorch#18426 运行失败
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | 🕚 | >>> |
| Build_ARM | 🕚 | >>> | |
| Build_LibTorch_x86 | 🕚 | >>> | |
| Build_LibTorch_ARM | 🕚 | >>> | |
| Build_X86_torchair | 🕚 | >>> | |
| Build_ARM_torchair | 🕚 | >>> | |
| patch_test | 🕚 | >>> | |
| 恶意代码检查 | Antipoison | ❌ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | 🕚 | >>> |
| UT_X86_Part_02 | 🕚 | >>> | |
| UT_ARM_A3_Part_01 | 🕚 | >>> | |
| UT_ARM_A3_Part_02 | 🕚 | >>> | |
| UT_DIST_X86_Part_01 | 🕚 | >>> | |
| UT_DIST_X86_Part_02 | 🕚 | >>> | |
| UT_DIST_X86_Part_03 | 🕚 | >>> | |
| UT_DIST_X86_Part_04 | 🕚 | >>> | |
| UT_inductor_Part_01 | 🕚 | >>> | |
| UT_inductor_Part_02 | 🕚 | >>> | |
| UT_inductor_Part_03 | 🕚 | >>> | |
| UT_inductor_Part_04 | 🕚 | >>> | |
| UT_ARM_A2_Part_01 | 🕚 | >>> | |
| UT_ARM_A2_Part_02 | 🕚 | >>> | |
| UT_ARM_A2_Part_03 | 🕚 | >>> | |
| 流水线 | PR-pipeline_pytorch | ❌ | >>> |


m0_73361278
4月29日 评论:
4月29日 评论:
compile


4月29日 删除了label:ci-pipeline-failed
4月29日 添加了label:ci-pipeline-running
4月29日 删除了label:ci-pipeline-running
4月29日 添加了label:ci-pipeline-passed
ascend-robot
4月29日 评论:
4月29日 评论:
流水线 PR-pipeline_pytorch#18457 已完成
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | 🛑 | >>> |
| UT_X86_Part_02 | 🛑 | >>> | |
| UT_ARM_A3_Part_01 | 🛑 | >>> | |
| UT_ARM_A3_Part_02 | 🛑 | >>> | |
| UT_DIST_X86_Part_01 | ✅ | >>> | |
| UT_DIST_X86_Part_02 | ✅ | >>> | |
| UT_DIST_X86_Part_03 | ✅ | >>> | |
| UT_DIST_X86_Part_04 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |


Jingwei Huang
4月30日 评论:
4月30日 评论:
/lgtm


4月30日 添加了label:approvedlgtm
ascend-robot
4月30日 评论:
4月30日 评论:
Review Guide
This pull-request passes review.
Committers who wrote a comment of /approve are: 李伟.
Reviewers who wrote a comment of /lgtm are: 李伟, huangjingwei.


4月30日 合入了pull request
【合入来源】
【修改方案】
一、API 功能说明
是 PyTorch 分布式弹性训练 metrics 模块下的指标输出处理器,核心功能如下:
控制台输出能力:通过 emit(metric_data) 接收 MetricData 指标对象,并将指标内容输出到控制台。
指标字段承载能力:可处理包含 timestamp、group_name、name、value 等字段的 MetricData 对象。
Handler 基类兼容:ConsoleMetricHandler 是 MetricHandler 类型实例,可作为 metrics 模块统一的指标处理器使用。
与 configure 联动:配置为默认 handler 或指定 group handler 后,可通过 metrics_api.getStream(...).add_value(...) 触发指标输出。
2.torch.distributed.elastic.metrics.api.NullMetricHandler
是 PyTorch 分布式弹性训练 metrics 模块下的空指标处理器,核心功能如下:
空处理能力:通过 emit(metric_data) 接收 MetricData 指标对象,但不产生控制台输出。
无副作用返回:调用 emit 后返回 None,不会打印内容。
Handler 基类兼容:NullMetricHandler 是 MetricHandler 类型实例,可作为 metrics 模块统一的指标处理器使用。
与 configure 联动:配置为默认 handler 或指定 group handler 后,可屏蔽对应指标流的输出。
3.torch.distributed.elastic.metrics.configure
是 PyTorch 分布式弹性训练 metrics 模块下的指标处理器配置 API,核心功能如下:
默认 handler 配置:调用 configure(handler) 时,可配置全局默认 metrics handler。
group 级 handler 配置:调用 configure(handler, group="xxx") 时,可为指定 group 单独配置 metrics handler。
指标流分发能力:通过 metrics_api.getStream(group_name).add_value(metric_name, value) 上报指标时,会按照 group 配置选择对应 handler。
默认与 group 配置隔离:指定 group 的 handler 只影响对应 group,不影响其他 group 使用默认 handler。
二、测试文件 test_metrics_api.py 完整验证该 API 的原因
该测试用例针对 ConsoleMetricHandler、NullMetricHandler 和 configure 三个 API 进行验证,覆盖 handler 基础行为、默认配置、group 级配置和测试隔离性。
对 ConsoleMetricHandler 的验证
test_console_metric_handler_emit:验证 ConsoleMetricHandler.emit 能正常接收 MetricData,返回值为 None,并通过 print 输出包含 timestamp/value、group name 和 metric name 的指标信息;同时验证 ConsoleMetricHandler 是 MetricHandler 的实例。
对 NullMetricHandler 的验证
test_null_metric_handler_emit:验证 NullMetricHandler.emit 能正常接收 MetricData,返回值为 None,且不会触发任何控制台输出;同时验证 NullMetricHandler 是 MetricHandler 的实例。
对 configure 默认 handler 配置的验证
test_configure_default_console_metric_handler:验证通过 configure(handler) 将默认 handler 配置为 ConsoleMetricHandler 后,调用 metrics_api.getStream(...).add_value(...) 能正常触发控制台输出。
test_configure_default_null_metric_handler:验证通过 configure(handler) 将默认 handler 配置为 NullMetricHandler 后,调用 metrics_api.getStream(...).add_value(...) 不会产生控制台输出,且返回值为 None。
对 configure group 级 handler 配置的验证
test_configure_group_specific_console_metric_handler:验证为指定 group 配置 ConsoleMetricHandler 后,仅该 group 的指标会上报到控制台,默认 group 仍使用默认 handler,二者互不干扰。
test_configure_group_specific_null_metric_handler:验证为指定 group 配置 NullMetricHandler 后,仅该 group 的指标不会输出,其他 group 仍可使用默认 ConsoleMetricHandler 正常输出。
测试隔离性保障
setUp 和 _restore_metrics_state:在每个测试前保存原始 _default_metrics_handler 和 _metrics_map,测试结束后恢复全局 metrics 状态,避免不同测试用例之间的 handler 配置互相污染。
综上,该文件覆盖了 ConsoleMetricHandler 的控制台输出行为、NullMetricHandler 的空处理行为、configure 的默认 handler 配置能力、group 级 handler 配置能力,以及测试间全局状态隔离能力,是对这三个 metrics API 的基础功能验证。
三、NPU适配
torch.distributed.elastic.metrics.api.ConsoleMetricHandler、torch.distributed.elastic.metrics.api.NullMetricHandler 和 torch.distributed.elastic.metrics.configure 均具备硬件无关性、纯 Python 层实现、无底层算子依赖三大核心特征,决定了其无需针对昇腾 NPU 做修改
【资料变更】
不涉及
【接口变更】
不涉及
【功能验证】
在2.10.0版本上执行该用例,均通过,日志如下:
[root@82bc9d38ec90 workspace]# python /workspace/2.10.0/pytorch/test/distributed/elastic/metrics/test_metrics_api.py
......
Ran 6 tests in 0.037s
OK
【CheckList】