已合并
docs: add Generator.clone_state support #43350
docs: add Generator.clone_state support #43350
已合并
Flipped创建于 7月30日
Flipped
Flipped
7月30日

【合入来源】

issue:【社区任务】7月社区任务第三期-Ascend for PyTorch API 一致性开发(124) #3235

issue:[Usage]: torch.Generator.clone_state NPU一致性验证 #3476

【修改方案】

1. 资料检查结论

本次针对 torch.Generator.clone_state 进行资料补齐检查。

torch.Generator.clone_state 为 PyTorch Generator 公开接口,不属于 PyTorch 私有接口。该 API 用于克隆当前 Generator 的随机数状态,返回一个新的 Generator 对象。克隆后的 Generator 应保持与原 Generator 克隆时一致的 RNG 状态,用于保证后续随机数生成结果可复现。

已在 NPU 环境下完成基础功能验证,torch.Generator.clone_state 实际支持 NPU:

[PASS] torch.Generator.clone_state basic NPU behavior
generator device: npu
clone device: npu

因此需要检查并补齐 master 分支下的资料目录:

docs/zh/api/native_api

根据 master 分支最新资料目录规则,资料补齐已转移到 master 分支下统一处理;同时 pytorch_2-9-0pytorch_2-10-0 目录已删除,新增 pytorch_2-13-0 目录,需要按 master 最新目录中实际存在的版本同步刷新 API 支持情况。

检查发现,现有各版本 torch.md 中已存在 ## Generators 章节,并已记录 torch.Generator 及其部分方法,但缺少 clone_state() 条目。因此,本次需要在各版本已有 torch.mdGenerators 章节中补充 torch.Generator.clone_state 支持情况。

2. 本次修改内容

本次按照机器人检视意见和最新资料结构进行调整:

  1. 不再新增独立的 torch-Generator.md 文件;
  2. 删除此前新增的 torch-Generator.md 文件,避免形成孤儿页面;
  3. 不修改各版本索引文件;
  4. clone_state() 直接补充到各版本已有 torch.md## Generators 章节中;
  5. 补充位置放在 set_state() 之后,保持 torch.Generator 相关 API 说明集中在同一文档位置。

本次修改范围为 master 最新资料目录中实际存在版本对应的 torch.md 文件,例如:

docs/zh/api/native_api/pytorch_2-7-1/torch.md
docs/zh/api/native_api/pytorch_2-11-0/torch.md
docs/zh/api/native_api/pytorch_2-12-0/torch.md
docs/zh/api/native_api/pytorch_2-13-0/torch.md

说明:

  • pytorch_2-9-0pytorch_2-10-0 已从 master 最新资料目录中删除,本次不再补充;
  • pytorch_2-13-0 为新增资料目录,本次同步补齐;
  • 以 master 分支 docs/zh/api/native_api 当前实际存在的版本目录为准进行刷新。

3. 资料内容说明

本次在各版本 torch.md## Generators 章节中补充如下条目:

> <font size="3">clone_state()</font>

<div style="margin-left: 2em">

**原生文档**:[torch.Generator.clone_state](https://pytorch.org/docs/版本号/generated/torch.Generator.html#torch.Generator.clone_state)

**是否支持**:是

**限制与说明**:-

</div>

torch.Generator.clone_state 属于 Generator 状态类 API,非计算类 API,与具体数据类型无关,因此未填写 fp16fp32 等数据类型支持信息,限制与说明按最新格式填写为:-

4. 文档结构调整说明

此前方案新增了独立的 torch-Generator.md 文件,但各版本索引文件未包含该文件入口,容易导致新增页面成为孤儿页面。

同时,各版本 torch.md 中已经存在 ## Generators 章节,并已记录 torch.Generator 相关方法。若再单独新增 torch-Generator.md,会导致 torch.Generator 资料分散在两个文件中,不利于用户通过正常文档路径查阅。

因此,本次按检视意见调整为:

不新增 torch-Generator.md
直接在 torch.md 的 Generators 章节补充 clone_state()

这样可以保证 torch.Generator 相关 API 资料集中在同一位置,避免文档碎片化和孤儿页面问题。

【资料变更】

涉及资料变更。

本 PR 在 master 分支下补充 torch.Generator.clone_state 的资料支持情况,修改范围为 master 最新资料目录中实际存在版本的 torch.md 文件。

补充原因:

  1. torch.Generator.clone_state 为 PyTorch Generator 公开接口,不属于私有接口;
  2. 该 API 在 NPU 环境下已验证实际支持;
  3. master 分支 docs/zh/api/native_api 下现有版本资料中缺少该 API 条目;
  4. 当前资料补齐已转移到 master 分支下,因此本次仅提交 master 资料 PR;
  5. pytorch_2-9-0pytorch_2-10-0 已从最新 master 资料目录中删除,本次不再补充;
  6. 新增的 pytorch_2-13-0 目录已同步检查并补齐;
  7. 本次未新增独立 torch-Generator.md,而是将 clone_state() 补充到已有 torch.mdGenerators 章节中。

【接口变更】

不涉及。

本次仅补充资料文件,不修改代码实现,不涉及跨代码仓接口变更,也不涉及客户可见接口功能变更。

【功能验证】

1. 资料目录检查

检查 master 分支资料目录:

cd /workspace/torch-npu-master

DOC_ROOT="docs/zh/api/native_api"

ls -ld "${DOC_ROOT}"

find "${DOC_ROOT}" -maxdepth 1 -type d | sort

确认以 master 分支当前实际存在的版本目录为准进行补齐;其中 pytorch_2-9-0pytorch_2-10-0 已删除,pytorch_2-13-0 已新增并同步检查。

2. 补充前资料检查

补充前执行以下命令检查 torch.Generator.clone_state 是否已存在:

cd /workspace/torch-npu-master

DOC_ROOT="docs/zh/api/native_api"

grep -Rnw \
    -E "torch\.Generator\.clone_state|Generator\.clone_state|clone_state" \
    "${DOC_ROOT}" \
    --include="*.md" \
    || true

检查结果显示:master 最新资料目录中现有版本的 torch.md 均缺少 torch.Generator.clone_state / clone_state() 条目,因此需要补齐。

3. 补充后资料检查

补充资料后执行以下命令检查新增条目:

cd /workspace/torch-npu-master

grep -Rnw \
  -E "torch\.Generator\.clone_state|clone_state\(\)" \
  docs/zh/api/native_api/pytorch_*/torch.md

预期结果:

各版本 docs/zh/api/native_api/pytorch_*/torch.md 中均存在 clone_state() 条目

4. 格式检查

执行:

cd /workspace/torch-npu-master

git diff --check
git diff --stat
git status --short

确认资料文件格式检查无异常,修改范围仅包含本次资料补齐相关的 torch.md 文件。

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Flipped 的贡献)
FlippedFlipped
7月30日 创建了 pull request,commit 6146e774
atomgit-bot
atomgit-bot
7月30日 评论:

变更摘要

本次 PR 主要为 torch.Generator.clone_state 接口补充中文 API 文档,在 docs/zh/api/native_api/ 下五个 PyTorch 版本目录中新增该接口的资料条目。torch.Generator.clone_state 用于克隆当前 Generator 的随机数状态并返回新的 Generator 对象,已在 NPU 环境下验证通过。

主要改动

  • 新增 torch.Generator.clone_state 文档条目:在 docs/zh/api/native_api/pytorch_2-7-1pytorch_2-9-0pytorch_2-10-0pytorch_2-11-0pytorch_2-12-0 五个版本目录中分别添加了该 API 的资料说明,共计 5 个文件、175 行新增内容
  • 明确 NPU 支持状态:文档中标注 torch.Generator.clone_state 在 NPU 设备上的基础行为已验证通过,克隆后的 Generator 与原 Generator 保持一致的 RNG 状态
likedislike
不准确?
atomgit-bot
atomgit-bot
7月30日 评论:

代码审查

I have now thoroughly reviewed all 5 changed files. Here is my analysis:

File-by-file review summary:

  • docs/zh/api/native_api/pytorch_2-7-1/torch-Generator.md — New file. Follows established documentation structure (NOTE → 目录 → class section → method subsection). URL uses 2.7 (matching existing docs in this version directory). clone_state documented as "supported" with no limitations. No dangerous instructions, insecure configurations, or testing issues. No issues.

  • docs/zh/api/native_api/pytorch_2-9-0/torch-Generator.md — New file. Identical structure to the 2.7.1 version with 2.9 URLs. Consistent with existing doc conventions in this directory. No issues.

  • docs/zh/api/native_api/pytorch_2-10-0/torch-Generator.md — New file. Same structure with 2.10 URLs. NOTE format matches the newer convention already present in some files within this directory (e.g., torch-utils-cpp_extension.md, torch-Storage.md). generated/torch.Generator.html URL pattern is consistent with PyTorch docs conventions (verified against existing torch-Tensor.md which uses generated/ for method pages). No issues.

  • docs/zh/api/native_api/pytorch_2-11-0/torch-Generator.md — New file. Same structure with 2.11 URLs. No issues.

  • docs/zh/api/native_api/pytorch_2-12-0/torch-Generator.md — New file. Same structure with 2.12 URLs. No issues.


审查总结

  • 各优先级问题数量:P0: 0, P1: 0, P2: 0, P3: 0
  • 整体风险评估:此变更为纯文档新增,无安全风险、无正确性问题、无可靠性问题。5 个文件均为新增的 torch.Generator API 支持状态文档,结构一致,URL 版本号与各自目录匹配,遵循现有多数文档的格式规范。可安全合入。
类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

likedislike
不准确?
ascend-robotascend-robot成员
7月30日 添加了label:ascend-cla/yes
此处折叠了83条消息 查看更多
ascend-robot
ascend-robot成员
26 天前 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
26 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
26 天前 添加了label:docs-ci-pipeline-success
ascend-robot
ascend-robot成员
26 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
26 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13943 [ commitID:e2e7f666 ] 已完成
likedislike