-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[FFI] Construct NDArray.strides by default #18272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,6 @@ namespace ffi { | |
| * \return The check result. | ||
| */ | ||
| inline bool IsContiguous(const DLTensor& arr) { | ||
| if (arr.strides == nullptr) return true; | ||
| int64_t expected_stride = 1; | ||
| for (int32_t i = arr.ndim; i != 0; --i) { | ||
| int32_t k = i - 1; | ||
|
|
@@ -110,6 +109,21 @@ inline size_t GetDataSize(const DLTensor& arr) { | |
| return GetDataSize(size, arr.dtype); | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Infer the stride from shape | ||
| * | ||
| * \param shape the input Shape | ||
| * \return the inferred stride | ||
| */ | ||
| inline Shape InferStrideFromShape(Shape shape) { | ||
| size_t ndim = shape.size(); | ||
| Array<int64_t> strides(ndim, 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not use array, instead, use https://github.com/apache/tvm/blob/main/ffi/include/tvm/ffi/container/shape.h#L71 do something like We can also move this function to shape.h details:: |
||
| for (int i = ndim - 2; i >= 0; --i) { | ||
| strides.Set(i, shape[i + 1] * strides[i + 1]); | ||
| } | ||
| return Shape(strides); | ||
| } | ||
|
|
||
| /*! \brief An object representing an NDArray. */ | ||
| class NDArrayObj : public Object, public DLTensor { | ||
| public: | ||
|
|
@@ -151,6 +165,7 @@ class NDArrayObj : public Object, public DLTensor { | |
| protected: | ||
| // backs up the shape of the NDArray | ||
| Optional<Shape> shape_data_; | ||
| Optional<Shape> stride_data_; | ||
|
|
||
| static void DLManagedTensorDeleter(DLManagedTensor* tensor) { | ||
| NDArrayObj* obj = static_cast<NDArrayObj*>(tensor->manager_ctx); | ||
|
|
@@ -184,9 +199,11 @@ class NDArrayObjFromNDAlloc : public NDArrayObj { | |
| this->ndim = static_cast<int>(shape.size()); | ||
| this->dtype = dtype; | ||
| this->shape = const_cast<int64_t*>(shape.data()); | ||
| this->strides = nullptr; | ||
| Shape strides = InferStrideFromShape(shape); | ||
| this->strides = const_cast<int64_t*>(strides.data());; | ||
| this->byte_offset = 0; | ||
| this->shape_data_ = std::move(shape); | ||
| this->stride_data_ = std::move(strides); | ||
| alloc_.AllocData(static_cast<DLTensor*>(this), std::forward<ExtraArgs>(extra_args)...); | ||
| } | ||
|
|
||
|
|
@@ -202,10 +219,6 @@ class NDArrayObjFromDLPack : public NDArrayObj { | |
| public: | ||
| explicit NDArrayObjFromDLPack(TDLPackManagedTensor* tensor) : tensor_(tensor) { | ||
| *static_cast<DLTensor*>(this) = tensor_->dl_tensor; | ||
| // set strides to nullptr if the tensor is contiguous. | ||
| if (IsContiguous(tensor->dl_tensor)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also need to check |
||
| this->strides = nullptr; | ||
| } | ||
| } | ||
|
|
||
| ~NDArrayObjFromDLPack() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep this for now as this function is defined per DLTensor