mars-project / mars-project/mars

[BUG] Inconsistency of __setitem__ with numpy

Open
#499 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

mod: tensor type: bug
Dominant language
Python
Stars
2.7k
Forks
325
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

The behavior is inconsistent with numpy.

**To Reproduce**
To help us reproducing this bug, please provide information below:
1. Your Python version: python 3.7
2. The version of Mars you use: master
3. Versions of crucial packages, such as numpy, scipy and protobuf
4. Full stack of the error.

```
Traceback (most recent call last):
File "", line 1, in
File "/Users/hetao/mars/mars/tensor/expressions/indexing/setitem.py", line 113, in _setitem
value = broadcast_to(value, shape).astype(a.dtype)
File "/Users/hetao/mars/mars/tensor/expressions/base/broadcast_to.py", line 103, in broadcast_to
raise ValueError('input operand has more dimensions than allowed by the axis remapping')
ValueError: input operand has more dimensions than allowed by the axis remapping
>>>
```

5. Minimized code to reproduce the error.

```python
import numpy as np
import mars.tensor as mt

x = np.zeros((4, 4))
x[1] = np.array([[1,2,3,4]]) # works

x = mt.zeros((4, 4))
x[1] = np.array([[1,2,3,4]]) # raise an exception
```

**Expected behavior**

The behavior should be consistent with numpy. The value `np.array([[1,2,3,4]])` should be firstly `broadcast_to` as `[1, 4]`, then `reshape` it as `[4]`.

**Additional context**
Add any other context about the problem here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with mars/tensor/expressions/indexing/setitem.py and trace its call to mars/tensor/expressions/base/broadcast_to.py. Run the provided NumPy and Mars reproducer, then verify that the Mars assignment accepts the [1, 4] value and produces the same result as NumPy.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.