Memory Bug, Possible Undefined Baheviour
- Dominant language
- Python
- Stars
- 595
- Forks
- 163
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 60
Description
Create the following unit test file, which is based on `program_strides_2()` from `tests/numpy/array_creation_test.py`.
```python
import dace
import numpy as np
import copy
@dace.program
def program():
# Was originally `program_strides_2()` in `tests/numpy/array_creation_test.py`.
A = dace.ndarray((2, 2), dtype=dace.int32, strides=(1, 2))
for i, j in dace.map[0:2, 0:2]:
A[i, j] = j + i
return A
def _impl_of_memory_test():
sdfg = program.to_sdfg()
csdfg = sdfg.compile()
A1 = csdfg()
Res1 = copy.deepcopy(A1)
A2 = csdfg()
assert A1 is A2
Res2 = copy.deepcopy(A2)
assert A2.strides == (4, 8)
assert np.allclose(Res2, [[0, 1], [1, 2]]), "Never expected that this fails."
assert np.allclose(Res1, [[0, 1], [1, 2]]), "Expected that this fails."
def test():
_impl_of_memory_test()
if __name__ == "__main__":
test()
```
Note that the bug does not surfaces every time, so you have to call the test in the following way:
```bash
for i in $(seq 100) ; do echo "ITERATION ${i}" ; pytest memory_issue_test.py --pdb ; done
```
It usually happens within the first 10 to 20 iterations.
I tried the following:
- If the Maps are serial, then it works as expected.
- If we add an additional operation, i.e. at the end we add `A[0, 0] += 1` the error still surfaces.
- If we set `may_alias` of `__return` to `True` it still fails.
- if we change the assignment to any of `A[i, j] = 0`, `A[i, j] = i` or `A[i, j] = j` then the test passes.
- If we move the for loop that calls the test, i.e. the bash loop suggested above, into python, then it seems that the bug is not triggered.
- Passing `A` as an argument, i.e. allocating it explicitly, the result is still the same, it fails; See below for the code.
- I do not observe the issue if `A` is allocated in `C` order (for that I used the version that passes `A` as argument).
I tried it on commit 8c24a345277ecf5d0e8f9a9c3f85a2630c367a9a, which was main at the point.
I am using Python 3.9.20 and 3.12.3 (but I have the impression that it happens less often for it).
Contributor guide
Research direction
Create the unit test from the issue and compare it with program_strides_2() in tests/numpy/array_creation_test.py. Run memory_issue_test.py repeatedly with the provided pytest loop to reproduce the intermittent result, then inspect the allocation and compiled execution path. Done means the test consistently preserves strides (4, 8) and both returned arrays equal [[0, 1], [1, 2]].
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- compilers, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100