kainzhong commented on code in PR #604:
URL: https://github.com/apache/tvm-ffi/pull/604#discussion_r3352763175
##########
python/tvm_ffi/cython/tensor.pxi:
##########
@@ -276,6 +276,52 @@ cdef class Tensor(CObject):
"""Tensor shape as a tuple of integers."""
return tuple(self.cdltensor.shape[i] for i in
range(self.cdltensor.ndim))
+ @property
+ def ndim(self) -> int:
+ """Number of dimensions of the tensor."""
+ return self.cdltensor.ndim
+
+ def numel(self) -> int:
+ """Total number of elements in the tensor."""
+ cdef int64_t count = 1
+ cdef int i
+ for i in range(self.cdltensor.ndim):
+ count *= self.cdltensor.shape[i]
+ return count
+
+ def size(self, idx: int) -> int:
+ """Get the size of the ``idx``-th dimension. Negative ``idx`` counts
from the last dimension.
+ """
+ cdef int ndim = self.cdltensor.ndim
+ if idx < -ndim or idx >= ndim:
+ raise IndexError(
+ f"Dimension {idx} out of range for tensor with {ndim}
dimensions"
+ )
+ if idx < 0:
+ idx += ndim
+ return self.cdltensor.shape[idx]
+
+ def is_contiguous(self) -> bool:
+ """True if the Tensor is C-contiguous (row-major), False otherwise."""
+ if self.cdltensor.strides == NULL:
+ return True
Review Comment:
I think this matches the C++ API implementation in
https://github.com/apache/tvm-ffi/blob/8ccfe1b9d05fb7e067f8d5b98ac60159c23bb35e/include/tvm/ffi/container/tensor.h#L57
Looks like `strides` will handle this case correctly. In my (4, 0, 4) case
the strides is (0, 4, 1), which matches `is_contiguous`'s expected stride so it
still returns True
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]