From 12e94e67640a46ac088ee94d5a60fa367292da2c Mon Sep 17 00:00:00 2001 From: huan <3174348550@qq.com> Date: Tue, 5 Aug 2025 15:02:37 +0800 Subject: [PATCH] modify contents --- .../mindspore/source_zh_cn/features/index.rst | 5 +- docs/mindspore/source_zh_cn/features/view.md | 96 ++++++++++--------- tutorials/source_en/debug/profiler.md | 2 +- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/docs/mindspore/source_zh_cn/features/index.rst b/docs/mindspore/source_zh_cn/features/index.rst index 1b046a7b6c..657f6d0bca 100644 --- a/docs/mindspore/source_zh_cn/features/index.rst +++ b/docs/mindspore/source_zh_cn/features/index.rst @@ -17,6 +17,7 @@ Developer Notes runtime/memory_manager runtime/multilevel_pipeline runtime/multistream_concurrency - amp - data_engine mint + view + data_engine + amp diff --git a/docs/mindspore/source_zh_cn/features/view.md b/docs/mindspore/source_zh_cn/features/view.md index f9d989fcfe..30e1897938 100644 --- a/docs/mindspore/source_zh_cn/features/view.md +++ b/docs/mindspore/source_zh_cn/features/view.md @@ -1,13 +1,17 @@ ## Tensor View 机制 -View操作是指创建一个新的张量,该张量与原始张量共享相同的数据存储(data storage),但具有不同的形状或排列方式。换句话说,view操作不会复制数据,而是通过不同的视角来持有现有的数据 +[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.7.0/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/r2.7.0/docs/mindspore/source_zh_cn/features/view.md) -核心特点 -- 内存共享: View操作创建的新张量与原始张量共享底层数据存储 -- 零拷贝: 不进行数据复制,避免内存分配开销 -- 形状变换: 可以改变张量的形状而不改变数据内容 +View操作是指创建一个新的张量,该张量与原始张量共享相同的数据存储(data storage),但具有不同的形状或排列方式。换句话说,view操作不会复制数据,而是通过不同的视角来持有现有的数据。 + +核心特点: + +- 内存共享:View操作创建的新张量与原始张量共享底层数据存储。 +- 零拷贝:不进行数据复制,避免内存分配开销。 +- 形状变换:可以改变张量的形状而不改变数据内容。 + +Tensor.view()方法: -Tensor.view()方法: ```python import mindspore @@ -27,7 +31,9 @@ z = x.view(3, 2) # 改为3x2张量 print("改后:", z) print("新形状:", z.shape) ``` -Tensor.view_as()方法: + +Tensor.view_as()方法: + ```python import mindspore @@ -41,50 +47,54 @@ print("目标形状:", target.shape) print("结果形状:", y.shape) ``` -需要注意的是: +需要注意的是: -1、view操作要求张量在内存中是连续的,如果不连续,需要先调用contiguous()方法 -```python -import mindspore -from mindspore import ops +1. view操作要求张量在内存中是连续的,如果不连续,需要先调用contiguous()方法 -x = mindspore.tensor([[1, 2, 3], [4, 5, 6]]) -y = mindspore.mint.transpose(x, (1, 0)) # 创建非连续张量 + ```python + import mindspore + from mindspore import ops -# 检查连续性 -print("y是否连续:", y.is_contiguous()) + x = mindspore.tensor([[1, 2, 3], [4, 5, 6]]) + y = mindspore.mint.transpose(x, (1, 0)) # 创建非连续张量 -# 使用contiguous()确保连续性 -z = y.contiguous().view(-1) -print("z是否连续:", z.is_contiguous()) -``` + # 检查连续性 + print("y是否连续:", y.is_contiguous()) -2、view操作要求新形状的元素总数与原始张量相同 -```python -import mindspore + # 使用contiguous()确保连续性 + z = y.contiguous().view(-1) + print("z是否连续:", z.is_contiguous()) + ``` -x = mindspore.tensor([1, 2, 3, 4, 5, 6]) -print("原始张量元素数:", x.numel()) +2. view操作要求新形状的元素总数与原始张量相同 -# 正确:6 = 2 * 3 -y = x.view(2, 3) -print("改为2x3:", y) + ```python + import mindspore -# 错误:6 ≠ 2 * 4 -try: - z = x.view(2, 4) -except RuntimeError as e: - print("形状不匹配错误:", e) -``` + x = mindspore.tensor([1, 2, 3, 4, 5, 6]) + print("原始张量元素数:", x.numel()) + + # 正确:6 = 2 * 3 + y = x.view(2, 3) + print("改为2x3:", y) + + # 错误:6 ≠ 2 * 4 + try: + z = x.view(2, 4) + except RuntimeError as e: + print("形状不匹配错误:", e) + ``` + +view与reshape区别: + +- view操作: -view与reshape区别: + - 严格要求连续性: View操作要求张量在内存中必须是连续的。 + - 失败机制: 如果张量不连续,view操作会抛出错误。 + - 解决方案: 需要先调用contiguous()方法。 -view操作 - - 严格要求连续性: View操作要求张量在内存中必须是连续的 - - 失败机制: 如果张量不连续,view操作会抛出错误 - - 解决方案: 需要先调用contiguous()方法 +- reshape操作: -reshape操作 - - 灵活处理: reshape操作更灵活,不要求张量必须连续 - - 自动处理: 如果张量不连续,reshape会自动创建新拷贝 - - 始终成功: 只要形状匹配,reshape操作总是能成功 \ No newline at end of file + - 灵活处理: reshape操作更灵活,不要求张量必须连续。 + - 自动处理: 如果张量不连续,reshape会自动创建新拷贝。 + - 始终成功: 只要形状匹配,reshape操作总是能成功。 \ No newline at end of file diff --git a/tutorials/source_en/debug/profiler.md b/tutorials/source_en/debug/profiler.md index 9d2e60c4f5..1454550cf1 100644 --- a/tutorials/source_en/debug/profiler.md +++ b/tutorials/source_en/debug/profiler.md @@ -22,7 +22,7 @@ There are five ways to collect training performance data, and the following desc ### Method 1: mindspore.Profiler Interface Enabling -Add the MindSpore Profiler related interfaces in the training script, users can refer to [MindSpore Profiler parameter details](https://www.mindspore.cn/docs/en/r2.7.0/api_python/mindspore/mindspore.Profiler.html) and [_ExperimentalConfig Parameter Details](https://www.mindspore.cn/docs/en/r2.7.0/api_python/mindspore/mindspore.profiler._ExperimentalConfig.html) to configure parameters such as profiler_level according to their data requirements. +Add the MindSpore Profiler related interfaces in the training script, users can refer to [MindSpore Profiler parameter details](https://www.mindspore.cn/docs/en/r2.7.0/api_python/mindspore/mindspore.Profiler.html) and [_ExperimentalConfig Parameter Details](https://www.mindspore.cn/docs/en/r2.7.0/api_python/mindspore/mindspore.profiler._ExperimentalConfig.html) to configure parameters such as profiler_level according to their data requirements. The interface supports two collection modes: CallBack mode and custom for loop mode, and supports both Graph and PyNative modes. -- Gitee