334 Star 1.5K Fork 863

MindSpore / docs

 / 详情

文档反馈-MindSpore

WIP
Documentation
创建于  
2024-05-07 17:27
  1. 【Document Link】/【文档链接】

https://www.mindspore.cn/docs/zh-CN/r2.2/note/api_mapping/pytorch_api_mapping.html#api%E6%98%A0%E5%B0%84%E4%B8%80%E8%87%B4%E6%A0%87%E5%87%86%E5%8F%8A%E4%BE%8B%E5%A4%96%E5%9C%BA%E6%99%AF

  1. 【Issues Section】/【问题文档片段】

PyTorch 与 MindSpore API 映射表(https://www.mindspore.cn/docs/zh-CN/r2.2/note/api_mapping/pytorch_api_mapping.html)中 mindspore.ops.roll 与 torch.roll 一致

  1. 【Issues Section】/【问题文档片段】

mindspore.ops.roll 与 torch.roll 的行为不一致,但是在 PyTorch 与 MindSpore API 映射表(https://www.mindspore.cn/docs/zh>CN/r2.2/note/api_mapping/pytorch_api_mapping.html)。mindspore.numpy.roll 与 torch.roll 的行为一致。

  1. 【Expected Result】【预期结果】
    4.1 MindSpore 环境
  • MinsSpore r2.2.11
  • CANN 7.0.1
  • Ascend 910B3

from mindspore import Tensor
from mindspore import numpy as mnp
from mindspore.ops import functional as F
x = Tensor.from_numpy((np.arange(16) + 1).reshape(4, 4).astype(np.int32))
expected = F.concat((x[:, 1:], x[:, :1]), axis=1)
rolled_x = F.roll(x, shifts=-1, dims=1) # type: ignore
x, expected, rolled_x
(Tensor(shape=[4, 4], dtype=Int32, value=
[[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]]),
Tensor(shape=[4, 4], dtype=Int32, value=
[[14, 15, 16, 1],
[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]]),
Tensor(shape=[4, 4], dtype=Int32, value=
[[ 2, 3, 4, 1],
[ 6, 7, 8, 5],
[10, 11, 12, 9],
[14, 15, 16, 13]]))
mnp_rolled = mnp.roll(x, shift=-1, axis=1)
mnp_rolled
Tensor(shape=[4, 4], dtype=Int32, value=
[[ 2, 3, 4, 1],
[ 6, 7, 8, 5],
[10, 11, 12, 9],
[14, 15, 16, 13]])

4.2 PyTorch 环境

  • torch 1.8.1
  • CUDA 12.2
  • NVIDIA RTX 4090 Laptop

import numpy as np
import torch
from torch import Tensor
x = Tensor((np.arange(16) + 1).reshape(4, 4).astype(np.int32))
rolled = torch.roll(x, shifts=-1, dims=1)
x, rolled
x = Tensor((np.arange(16) + 1).reshape(4, 4).astype(np.int32))
rolled = torch.roll(x, shifts=-1, dims=1)
x, rolled
(tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]]),
tensor([[ 2., 3., 4., 1.],
[ 6., 7., 8., 5.],
[10., 11., 12., 9.],
[14., 15., 16., 13.]]))


评论 (8)

kuhsinyv 创建了Documentation

Please assign maintainer to check this issue.
请为此issue分配处理人。
@fangwenyi @chengxiaoli @Shawny

感谢您的提问,您可以评论//mindspore-assistant更快获取帮助:

  1. 如果您刚刚接触MindSpore,或许您可以在教程找到答案
  2. 如果您是资深Pytorch用户,您或许需要:
  1. 如果您遇到动态图问题,可以设置set_context(pynative_synchronize=True)查看报错栈协助定位
  2. 模型精度调优问题可参考官网调优指南
  3. 如果您反馈的是框架BUG,请确认您在ISSUE中提供了MindSpore版本、使用的后端类型(CPU、GPU、Ascend)、环境、训练的代码官方链接以及可以复现报错的代码的启动方式等必要的定位信息
  4. 如果您已经定位出问题根因,欢迎提交PR参与MindSpore开源社区,我们会尽快review
kuhsinyv 修改了描述
TingWang 负责人设置为changzherui

您好,根据你提供的代码,如果你指的是返回值类型不一致,那是因为pytorch的Tensor接口,会默认给tensor返回值类型强转为fp32,你应该使用torch.tensor接口来对标mindspore的Tensor接口,这样才能保持入参类型及返回值类型一致。

不好意思,我粘贴结果的时候出错了,现在重新描述:

MindSpore + CANN + NPU

from mindspore import Tensor
from mindspore import numpy as mnp
from mindspore.ops import functional as F
x = Tensor.from_numpy((np.arange(16) + 1).reshape(4, 4).astype(np.int32))
expected = F.concat((x[:, 1:], x[:, :1]), axis=1)
rolled_x = F.roll(x, shifts=-1, dims=1) # type: ignore
x, expected, rolled_x
(Tensor(shape=[4, 4], dtype=Int32, value=
[[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]]),
Tensor(shape=[4, 4], dtype=Int32, value=
[[ 2, 3, 4, 1],
[ 6, 7, 8, 5],
[10, 11, 12, 9],
[14, 15, 16, 13]]),
Tensor(shape=[4, 4], dtype=Int32, value=
[[14, 15, 16, 1],
[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]]))


PyTorch + CUDA + GPU

import numpy as np
import torch
from torch import Tensor
x = Tensor((np.arange(16) + 1).reshape(4, 4).astype(np.int32))
rolled = torch.roll(x, shifts=-1, dims=1)
x, rolled
(tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]]),
tensor([[ 2., 3., 4., 1.],
[ 6., 7., 8., 5.],
[10., 11., 12., 9.],
[14., 15., 16., 13.]]))


也就是说,MindSpore 的 roll 和 PyTorch 的 roll 的行为似乎是不太一致的,但是文档中描述为一致。

不好意思,我粘贴结果的时候出错了,现在重新描述:

MindSpore + CANN + NPU
(Tensor(shape=[4, 4], dtype=Int32, value=
[[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]]),
Tensor(shape=[4, 4], dtype=Int32, value=
[[ 2, 3, 4, 1],
[ 6, 7, 8, 5],
[10, 11, 12, 9],
[14, 15, 16, 13]]),
Tensor(shape=[4, 4], dtype=Int32, value=
[[14, 15, 16, 1],
[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]]))


PyTorch + CUDA + GPU
(tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]]),
tensor([[ 2., 3., 4., 1.],
[ 6., 7., 8., 5.],
[10., 11., 12., 9.],
[14., 15., 16., 13.]]))


也就是说,MindSpore 的 roll 和 PyTorch 的 roll 的行为似乎是不太一致的,但是文档中描述为一致。

@kuhsinyv
我还是没太明白你说的不一样的点,是结果不一致还是类型不一致,而且ms部分里面的expected是干什么的

ms 部分里面 expected 是预期结果(相当于把矩阵的第一列移到矩阵最右边,与 torch.roll 的结果一致),ms 部分的 rolled_x 是实际上 ms.ops.roll 的结果,与预期不一致。也就是 ms.ops.roll 和 torch.roll 的结果不一致(先忽略类型不一致)。

ms 部分里面 expected 是预期结果(相当于把矩阵的第一列移到矩阵最右边,与 torch.roll 的结果一致),ms 部分的 rolled_x 是实际上 ms.ops.roll 的结果,与预期不一致。也就是 ms.ops.roll 和 torch.roll 的结果不一致(先忽略类型不一致)。

收到,我这边暂时没有昇腾2.2的机器,在gpu上试了一下,roll这个接口在gpu上结果应该是正确的,和你给的expect以及pytorch都是一致的,应该不是文档问题,根据你的描述,应该是昇腾上出现了计算结果错误,已经反馈给了相应的算子责任人。
@kuhsinyv

TingWang 任务状态TODO 修改为WIP
kuhsinyv 任务状态WIP 修改为VALIDATION
kuhsinyv 任务状态VALIDATION 修改为WIP

得分:5
类型:正确性
活动链接(可查询积分):https://www.mindspore.cn/feedback
欢迎您提交更多issue或PR,获得更多积分。

i-robot 添加了
 
www
标签
Shawny 负责人changzherui 修改为liuchengji
Shawny 添加协作者changzherui

登录 后才可以发表评论

状态
负责人
项目
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
预计工期 (小时)
参与者(5)
8996751 cooinga 1685590095 7347217 changzherui 1584948547
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs

搜索帮助