microsoft / microsoft/onnxruntime
OrtValue.update_inplace does not handle non-contiguous numpy arrays
- Dominant language
- C++
- Stars
- 21.9k
- Forks
- 4.2k
- Avg merge
- 4d 11h
- Merged PRs (30d)
- 184
Description
### Describe the issue
In the python API, `OrtValue.update_inplace(np_array)` seems to copy directly from memory, ignoring any stride information from numpy. It seems like an easy fix for this would be to call `np.ascontiguousarray(input)` as the first line of the `update_inplace` function - which I imagine should be a no-op if the array is already contiguous.
The result below was generated with:
```
onnxruntime: 1.13.1
numpy: 1.22.3
python: 3.8.5
```
### To reproduce
Reproduce with the following script:
```python
import onnxruntime as ort
import numpy as np
# NCHW -> NHWC , maybe
# initialize a non-contiguous numpy array
a = np.arange(8).reshape((1, 2, 2, 2)).astype(dtype=np.float32).transpose(0, 2, 3, 1)
print('a original')
print(a.shape)
print(a)
# first: copy non-contiguous numpy array to ort value, and then back to numpy.
ort_a = ort.OrtValue.ortvalue_from_shape_and_type([1, 2, 2, 2], np.float32, 'cuda', 0)
ort_a.update_inplace(a)
ort_a_out_bad = ort_a.numpy()
a_bad_matches_original = np.all(a == ort_a_out_bad)
print('\na after copy to tensor and back')
# result doesn't match!!
print(f'matches original: {a_bad_matches_original}')
print(ort_a_out_bad.shape)
print(ort_a_out_bad)
# fix: use np.ascontiguousarray - but this shouldn't be necessary???
ort_a.update_inplace(np.ascontiguousarray(a))
ort_a_out_good = ort_a.numpy()
a_good_matches_original = np.all(a == ort_a_out_good)
print('\na contiguous after copy to tensor and back')
print(f'matches original: {a_good_matches_original}')
print(ort_a_out_good.shape)
print(ort_a_out_good)
```
Output:
```
a original
(1, 2, 2, 2)
[[[[0. 4.]
[1. 5.]]
[[2. 6.]
[3. 7.]]]]
a after copy to tensor and back
matches original: False
(1, 2, 2, 2)
[[[[0. 1.]
[2. 3.]]
[[4. 5.]
[6. 7.]]]]
a contiguous after copy to tensor and back
matches original: True
(1, 2, 2, 2)
[[[[0. 4.]
[1. 5.]]
[[2. 6.]
[3. 7.]]]]
```
### Urgency
_No response_
### Platform
Windows
### OS Version
10
### ONNX Runtime Installation
Released Package
### ONNX Runtime Version or Commit ID
1.13.1
### ONNX Runtime API
Python
### Architecture
X64
### Execution Provider
CUDA
### Execution Provider Library Version
11.3
Contributor guide
Assessment
This issue has not been assessed yet.