Skip to content
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

Added DLPack make_tensor #811

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions include/matx/core/make_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "matx/core/nvtx.h"
#include "matx/core/storage.h"
#include "matx/core/tensor_desc.h"
#include "matx/core/dlpack.h"
namespace matx {

/**
Expand Down Expand Up @@ -619,4 +620,136 @@ auto make_static_tensor() {
return tensor_t<T, desc.Rank(), decltype(s), decltype(desc)>{std::move(s), std::move(desc)};
}

template <typename TensorType,
std::enable_if_t<is_tensor_view_v<TensorType>, bool> = true>
auto make_tensor( TensorType &tensor,
const DLManagedTensor dlp_tensor) {
MATX_NVTX_START("", matx::MATX_NVTX_LOG_API)

using T = typename TensorType::value_type;
const DLTensor &dt = dlp_tensor.dl_tensor;

// MatX doesn't track the memory type or device ID, so we don't need to copy it
MATX_ASSERT_STR_EXP(dt.ndim, TensorType::Rank(), matxInvalidDim, "DLPack rank doesn't match MatX rank!");

switch (dt.dtype.code) {
case kDLComplex: {
switch (dt.dtype.bits) {
case 128: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, cuda::std::complex<double>>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 64: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, cuda::std::complex<float>>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 32: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, matxFp16Complex> || std::is_same_v<T, matxBf16Complex>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
default:
MATX_THROW(matxInvalidSize, "Invalid complex float size from DLPack");
}
break;
}

case kDLFloat: {
switch (dt.dtype.bits) {
case 64: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, double>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 32: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, float>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 16: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, matxFp16> || std::is_same_v<T, matxBf16>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
default:
MATX_THROW(matxInvalidSize, "Invalid float size from DLPack");
}
break;
}
case kDLInt: {
switch (dt.dtype.bits) {
case 64: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, int64_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 32: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, int32_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 16: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, int16_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 8: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, int8_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
default:
MATX_THROW(matxInvalidSize, "Invalid signed integer size from DLPack");
}
break;
}
case kDLUInt: {
switch (dt.dtype.bits) {
case 64: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, uint64_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 32: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, uint32_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 16: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, uint16_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
case 8: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, uint8_t>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
default:
MATX_THROW(matxInvalidSize, "Invalid unsigned integer size from DLPack");
}
break;
}
case kDLBool: {
[[maybe_unused]] constexpr bool same = std::is_same_v<T, bool>;
MATX_ASSERT_STR(same, matxInvalidType, "DLPack/MatX type mismatch");
break;
}
}

index_t strides[TensorType::Rank()];
index_t shape[TensorType::Rank()];

for (int r = 0; r < TensorType::Rank(); r++) {
strides[r] = dt.strides[r];
shape[r] = dt.shape[r];
}

auto tmp = make_tensor<typename TensorType::value_type, TensorType::Rank()>(
reinterpret_cast<typename TensorType::value_type*>(dt.data), shape, strides, false);
tensor.Shallow(tmp);
}

} // namespace matx
3 changes: 1 addition & 2 deletions include/matx/core/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
*
* @returns Pointer to new DLManagedTensorVersioned pointer. The caller must call the deleter function when finished.
*/
DLManagedTensor *GetDLPackTensor() const {
DLManagedTensor *ToDlPack() const {
auto mt = new DLManagedTensor;
DLTensor *t = &mt->dl_tensor;
CUpointer_attribute attr[] = {CU_POINTER_ATTRIBUTE_MEMORY_TYPE, CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL};
Expand Down Expand Up @@ -1509,7 +1509,6 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
return mt;
}


private:
Storage storage_;
};
Expand Down
2 changes: 1 addition & 1 deletion test/00_tensor/BasicTensorTests.cu
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ TYPED_TEST(BasicTensorTestsAll, DLPack)
using TestType = cuda::std::tuple_element_t<0, TypeParam>;

auto t = make_tensor<TestType>({5,10,20});
auto dl = t.GetDLPackTensor();
auto dl = t.ToDlPack();

ASSERT_EQ(dl->dl_tensor.ndim, 3);
ASSERT_EQ(dl->dl_tensor.data, t.Data());
Expand Down