21 inline std::string
toString(
const DLDataType& t) {
23 CHECK(
false) <<
"NYI: toString for >1 lanes";
26 case DLDataTypeCode::kDLFloat:
36 case DLDataTypeCode::kDLInt:
48 case DLDataTypeCode::kDLUInt:
55 CHECK(
false) <<
"NYI: toString for type: " << t.code <<
", bits: " << t.bits;
62 res.device_type = DLDeviceType::kDLCPU;
70 res.device_id = device_id;
71 res.device_type = DLDeviceType::kDLGPU;
81 res.code = DLDataTypeCode::kDLFloat;
90 res.code = DLDataTypeCode::kDLInt;
96 inline void SetSizes(DLTensor& t,
const std::vector<int64_t>& sizes) {
97 auto ndim = sizes.size();
98 for (
size_t i = 0; i < ndim; ++i) {
99 t.shape[i] = sizes[i];
103 inline void SetStrides(DLTensor& t,
const std::vector<int64_t>& strides) {
104 auto ndim = strides.size();
105 for (
size_t i = 0; i < ndim; ++i) {
106 t.strides[i] = strides[i];
112 t.strides[ndim - 1] = 1;
113 for (
int i = ndim - 2; i >= 0; --i) {
114 t.strides[i] = t.strides[i + 1] * t.shape[i + 1];
121 const std::vector<int64_t>& sizes) {
125 auto ndim = sizes.size();
128 res->shape =
new int64_t[ndim];
130 res->strides =
new int64_t[ndim];
132 res->byte_offset = 0;
137 const std::vector<DLTensorUPtr>& uptrs) {
138 std::vector<const DLTensor*> res;
139 res.reserve(uptrs.size());
140 for (
const auto& uptr : uptrs) {
141 res.push_back(uptr.get());
147 const std::vector<DLTensor*>& ptrs) {
148 std::vector<const DLTensor*> res;
149 res.reserve(ptrs.size());
150 for (
auto p : ptrs) {
159 res->data = ptr->data;
161 res->ndim = ptr->ndim;
162 res->dtype = ptr->dtype;
164 res->shape =
new int64_t[ptr->ndim];
165 for (
int i = 0; i < ptr->ndim; ++i) {
166 res->shape[i] = ptr->shape[i];
169 res->strides =
new int64_t[ptr->ndim];
170 for (
int i = 0; i < ptr->ndim; ++i) {
171 res->strides[i] = ptr->strides[i];
176 res->byte_offset = ptr->byte_offset;
180 template <
typename T>
182 std::vector<DLTensorUPtr> res;
183 for (
auto p : ptrs) {
190 std::stringstream ss;
191 ss <<
"DLTensor(@" << t.data <<
", dim=" << t.ndim <<
")";
193 for (
int i = 0; i < t.ndim; ++i) {
195 if (i < t.ndim - 1) {
199 ss <<
"], strides [";
200 for (
int i = 0; i < t.ndim; ++i) {
202 if (i < t.ndim - 1) {
210 inline bool operator==(
const DLDataType& t1,
const DLDataType& t2) {
211 return t1.code == t2.code && t1.bits == t2.bits && t1.lanes == t2.lanes;
214 inline std::ostream&
operator<<(std::ostream& os,
const DLDataType& t) {
215 return os <<
"typecode: " << t.code <<
" bits: " << t.bits
216 <<
" lanes: " << t.lanes;
220 if (t1.ndim != t2.ndim) {
223 if (!(t1.dtype == t2.dtype)) {
226 if ((t1.strides == NULL) ^ (t2.strides == NULL)) {
229 for (
int i = 0; i < t1.ndim; ++i) {
230 if (t1.shape[i] != t2.shape[i]) {
233 if (t1.strides && t1.strides[i] != t2.strides[i]) {
241 template <
typename T,
typename TT>
243 const std::vector<T*>& v1,
244 const std::vector<TT*>& v2) {
245 if (v1.size() != v2.size()) {
248 for (
size_t i = 0; i < v1.size(); ++i) {
void SetSizes(DLTensor &t, const std::vector< int64_t > &sizes)
Definition: dlpack-inl.h:96
bool operator==(const DLDataType &t1, const DLDataType &t2)
Definition: dlpack-inl.h:210
std::vector< const DLTensor * > extractRawPtrs(const std::vector< DLTensorUPtr > &uptrs)
Definition: dlpack-inl.h:136
bool compareDLTensorMetadata(const DLTensor &t1, const DLTensor &t2)
Definition: dlpack-inl.h:219
std::ostream & operator<<(std::ostream &os, const DLDataType &t)
Definition: dlpack-inl.h:214
std::string toString(const DLDataType &t)
Definition: dlpack-inl.h:21
bool compareDLTensorVectorMetadata(const std::vector< T * > &v1, const std::vector< TT * > &v2)
Definition: dlpack-inl.h:242
std::vector< const DLTensor * > constPtrs(const std::vector< DLTensor * > &ptrs)
Definition: dlpack-inl.h:146
DLTensorUPtr makeDLTensorWithSizes(DLContext ctx, DLDataType dtype, const std::vector< int64_t > &sizes)
Definition: dlpack-inl.h:118
DLContext getCPUDLContext()
Definition: dlpack-inl.h:59
DLDataType getDLDataType()
std::unique_ptr< DLTensor, DLTensorDeleter > DLTensorUPtr
Definition: dlpack.h:52
DLDataType getDLDataType< int >()
Definition: dlpack-inl.h:88
void SetStridesFromSizes(DLTensor &t, const std::vector< int64_t > &)
Definition: dlpack-inl.h:110
DLContext getGPUDLContext(int device_id)
Definition: dlpack-inl.h:68
std::vector< DLTensorUPtr > makeDLTensorVector(const std::vector< T * > &ptrs)
Definition: dlpack-inl.h:181
DLTensorUPtr makeDLTensor(const DLTensor *ptr)
Definition: dlpack-inl.h:156
DLDataType getDLDataType< float >()
Definition: dlpack-inl.h:79
void SetStrides(DLTensor &t, const std::vector< int64_t > &strides)
Definition: dlpack-inl.h:103