PyTorch 与 MindSpore API 映射表(https://www.mindspore.cn/docs/zh-CN/r2.2/note/api_mapping/pytorch_api_mapping.html)中 mindspore.ops.roll 与 torch.roll 一致
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 的行为一致。
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 环境
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.]]))
Please assign maintainer to check this issue.
请为此issue分配处理人。
@fangwenyi @chengxiaoli @Shawny
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
感谢您的提问,您可以评论//mindspore-assistant更快获取帮助:
您好,根据你提供的代码,如果你指的是返回值类型不一致,那是因为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是干什么的
得分:5
类型:正确性
活动链接(可查询积分):https://www.mindspore.cn/feedback
欢迎您提交更多issue或PR,获得更多积分。
登录 后才可以发表评论