Skip to content

Commit

Permalink
fix: negative dimensions are not allowed (DeepLink-org#766)
Browse files Browse the repository at this point in the history
* fix: negative dimensions are not allowed

* fix: cover the scalar tensor
  • Loading branch information
yewentao256 authored Dec 21, 2023
1 parent 24ca8d0 commit f60cad1
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions diopi_test/python/conformance/diopi_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,14 @@ def from_numpy(cls, darray, context=None, device=Device.AIChip):
return tr

def numpy(self) -> np.ndarray:
if all(x == 0 for x in self.size().data) and self.numel == 0:
# cases when shape all 0, but not include the scalar tensor
return np.empty(self.size().data, to_numpy_dtype(self.get_dtype()))
data = np.empty((1,), to_numpy_dtype(self.get_dtype()))
element_size = data.itemsize
sumsize = int(
sum(
[
(s - 1) * st
for s, st in zip(
list(self.size().data),
[
int(stride * element_size)
for stride in self.get_stride().data
],
)
]
) / element_size + 1
)
stride_scaled = [int(stride * element_size) for stride in self.get_stride().data]
sum_of_products = sum((s - 1) * st for s, st in zip(self.size().data, stride_scaled))
sumsize = int(sum_of_products / element_size) + 1
darray = np.empty(sumsize, to_numpy_dtype(self.get_dtype()))
PyCapsule_Destructor = ctypes.CFUNCTYPE(None, ctypes.py_object)
PyCapsule_New = ctypes.pythonapi.PyCapsule_New
Expand Down

0 comments on commit f60cad1

Please sign in to comment.