mindspore-ai / mindspore

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.
https://gitee.com/mindspore/mindspore
Apache License 2.0
4.31k stars 709 forks source link

[Bug] Missing range check for index parameter in mindspore.ops.scatter. #271

Closed Redmept1on closed 7 months ago

Redmept1on commented 7 months ago

Environment

Hardware Environment(Ascend/GPU/CPU):

device cpu

Software Environment:

Describe the current behavior

when index as a negative or positive number exceeds the range of size, no error is reported.

Describe the expected behavior

RuntimeError: index 10000 is out of bounds for dimension 1 with size 5

Steps to reproduce the issue

import mindspore as mind
import numpy as np

input = mind.ops.ones((3,5), mind.int64)*2
index = mind.tensor(np.array([[0,-10000,2],[0,1,4]],))
src = mind.tensor(np.array([[0,10,20],[50,60,70]]))
out = mind.ops.scatter(input, 1, index, src)
print(out)

image

pytorch v2.0.0+cu117

import torch
import numpy as np

input = torch.ones((3,5), dtype=torch.int64)*2
index = torch.tensor(np.array([[0,-10000,2],[0,1,4]],))
src = torch.tensor(np.array([[0,10,20],[50,60,70]]))
out = torch.scatter(input, 1, index, src)
print(out)

image

Special notes for this issue

when set the number in index at 10000, the bug exists the same.

Redmept1on commented 7 months ago

mindspore.ops.tensor_scatter_elements exist the same bug, when number in indices parameter exceed the range of size, there's no error message.

import mindspore
from mindspore import Tensor, ops
from mindspore import Parameter
import numpy as np
input_x = Parameter(Tensor(np.array([[1, 2, 3, 4, 5]]), mindspore.int32), name="x")
indices = Tensor(np.array([[10000, 4]]), mindspore.int32)
updates = Tensor(np.array([[8, 8]]), mindspore.int32)
axis = 1
reduction = "none"
output = ops.tensor_scatter_elements(input_x, indices, updates, axis, reduction)
print(output)

image