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 710 forks source link

[Bug] When there is a negative number in index, 'mindspore.ops.index_select' should throw an error message #270

Closed Redmept1on closed 7 months ago

Redmept1on commented 7 months ago

Environment

Hardware Environment(Ascend/GPU/CPU):

device gpu

Software Environment:

Describe the current behavior

[[7. 8. 9.]]

Describe the expected behavior

IndexError: index out of range in self

Steps to reproduce the issue

import mindspore as mind
import numpy as np

input_tensor = mind.tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32))

index_tensor = mind.tensor([-1])

output_tensor = mind.ops.index_select(input_tensor, axis=-2, index=index_tensor)

print(output_tensor)

image

pytorch v2.0.0+cu117

import torch
import numpy as np

input_tensor = torch.tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))

index_tensor = torch.tensor([-1])

output_tensor = torch.index_select(input_tensor, axis=-2, index=index_tensor)

print(output_tensor)

image

Special notes for this issue

The index parameter lacks a range check

Ash-Lee233 commented 7 months ago

did you test the case on cpu? we test the case on gpu, it reports the same info as torch but the cpu gave [[7,8,9]], we will fix it in the next version

Redmept1on commented 7 months ago

Thanks for your quick response, sorry for wrong information, after verification I found that my code was running on the CPU.