Pull Request已成功合入, 合并人@CANN-robot
(感谢 dengguojie 的贡献)变更摘要
本 PR 针对 numpy.random.uniform 大 tensor 生成时的内存峰值问题做性能优化:原先单次全量生成会产生 float64 中间数组(8B/元素)及 astype 副本,大 shape 下峰值约 12B/元素(fp32)。改动在 ttk/utilities/data.py 的 RandomData 中新增静态方法 _gen_uniform_data,复用 CHUNK_ELEMS=4M 分块直写预分配 typed 缓冲,并将 _random 的 uniform 分支与 float8 e8m0 分支统一改走该路径;因 uniform 逐元素消费 RandomState 流,分块输出与单次全量 uniform(...).astype(dtype) 逐位相同(固定 seed 可复现)。实测 177M 元素 fp32 峰值内存由 2032MB 降至 747MB(约 12B/元素 -> 4.4B/元素),同时修掉了 e8m0 分支缺失 copy=False 的多余拷贝。
主要改动
- 新增
_gen_uniform_data静态方法:按CHUNK_ELEMS分块向预分配缓冲写入numpy.random.uniform结果,避免 float64 中间数组与astype副本;elem_count <= 2 * CHUNK_ELEMS时保持原单次全量路径(astype(dtype, copy=False)),行为与改动前一致。 _random的 uniform 分支接入分块路径:将array = numpy.random.uniform(low, high, shape).astype(dtype, copy=False)替换为array = self._gen_uniform_data(low, high, dtype, shape),使 uniform 分布生成复用分块直写逻辑;fp16/bf16 的 torch 快路径与 float64 分支不受影响。- float8 e8m0 分支同步替换:将
f32 = numpy.random.uniform(low, high, self._shape).astype("float32")改为self._gen_uniform_data(low, high, "float32", self._shape),顺带消除原先无copy=False带来的多余拷贝。 - 新增 uniform 分块测试覆盖:
tests/utilities/test_data.py增加 8 种 dtype(float16/bfloat16/float32/float64/int32/int64/uint8/bool)的逐位等价测试、非 4M 对齐尾块(1、CHUNK-1)与整除(3*CHUNK)边界、多维 shape、小 tensor 保持单次路径、通过 spy 校验大 tensor 真实走分块(uniform 调用次数 >= 2)以及generate入口 dtype 保持。


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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-cann/ops-test-kit | ✅ 周奇龙, 王瑞 (2/2) | ✅ 周奇龙, 王瑞 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
dengguojie, thanks for your pull request. All authors of the commits have signed the CLA. 👍


/compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


当前PR是否有AI参与:
[x] 是
__1. AI Agent 平台: OpenCode
__2. AI 模型: GLM
__3. Prompt上下文 :
PR功能描述 / 为什么需要这个合入**:
该PR关联的issue
(格式为fixes #<issue号>, 或者resolves #<issue号>): fixes #
希望检视人员了解:
测试
文档更新
类型标签