24 #ifndef TVM_RUNTIME_NDARRAY_H_ 25 #define TVM_RUNTIME_NDARRAY_H_ 84 inline void CopyFrom(
const DLTensor* other);
100 inline void CopyTo(DLTensor* other)
const;
109 TVM_DLL
void CopyToBytes(
void* data,
size_t nbytes)
const;
121 inline bool Load(dmlc::Stream* stream);
126 inline void Save(dmlc::Stream* stream)
const;
139 TVM_DLL DLManagedTensor*
ToDLPack()
const;
187 TVM_DLL
static void CopyFromTo(
const DLTensor* from, DLTensor* to,
207 TVM_DLL
static bool IsAligned(
const DLTensor& tensor);
247 inline bool SaveDLTensor(dmlc::Stream* strm,
const DLTensor* tensor);
272 void* manager_ctx{
nullptr};
292 dl_tensor.data =
nullptr;
294 dl_tensor.shape =
nullptr;
295 dl_tensor.strides =
nullptr;
296 dl_tensor.byte_offset = 0;
302 dl_tensor.data = data;
303 shape_ = std::move(shape);
304 dl_tensor.ndim =
static_cast<int>(shape_.size());
306 dl_tensor.dtype = dtype;
307 dl_tensor.strides =
nullptr;
308 dl_tensor.byte_offset = 0;
309 dl_tensor.device = dev;
324 static constexpr
const uint32_t _type_child_slots = 0;
325 static constexpr
const uint32_t _type_child_slots_can_overflow =
true;
326 static constexpr
const char* _type_key =
"runtime.NDArray";
330 friend class RPCWrappedFunc;
344 size *=
static_cast<size_t>(arr.shape[i]);
346 size *= (arr.dtype.bits * arr.dtype.lanes + 7) / 8;
356 if (arr.strides ==
nullptr)
return true;
357 int64_t expected_stride = 1;
358 for (int32_t i = arr.ndim; i != 0; --i) {
360 if (arr.shape[k] == 1) {
369 if (arr.strides[k] != expected_stride)
return false;
370 expected_stride *= arr.shape[k];
376 return ::tvm::runtime::IsContiguous(
get_mutable()->dl_tensor);
380 ICHECK(
data_ !=
nullptr);
385 ICHECK(
data_ !=
nullptr);
386 ICHECK(other.
data_ !=
nullptr);
391 ICHECK(
data_ !=
nullptr);
396 ICHECK(
data_ !=
nullptr);
397 ICHECK(other.
data_ !=
nullptr);
402 ICHECK(
data_ !=
nullptr);
418 return GetObjectPtr<Object>(
444 strm->Write(reserved);
455 cpu_dev.device_type = kDLCPU;
456 cpu_dev.device_id = 0;
457 strm->Write(cpu_dev);
458 strm->Write(tensor->ndim);
459 strm->Write(tensor->dtype);
460 int ndim = tensor->ndim;
461 strm->WriteArray(tensor->shape, ndim);
462 int type_bytes = (tensor->dtype.bits + 7) / 8;
463 int64_t num_elems = 1;
464 for (
int i = 0; i < ndim; ++i) {
465 num_elems *= tensor->shape[i];
467 int64_t data_byte_size = type_bytes * num_elems;
468 strm->Write(data_byte_size);
470 if (DMLC_IO_NO_ENDIAN_SWAP && tensor->device.device_type == kDLCPU &&
471 tensor->strides ==
nullptr && tensor->byte_offset == 0) {
473 strm->Write(tensor->data, data_byte_size);
475 std::vector<uint8_t> bytes(data_byte_size);
480 if (!DMLC_IO_NO_ENDIAN_SWAP) {
481 dmlc::ByteSwap(dmlc::BeginPtr(bytes), type_bytes, num_elems);
483 strm->Write(dmlc::BeginPtr(bytes), data_byte_size);
491 uint64_t header, reserved;
492 ICHECK(strm->Read(&header)) <<
"Invalid DLTensor file format";
493 ICHECK(strm->Read(&reserved)) <<
"Invalid DLTensor file format";
494 ICHECK(header == kTVMNDArrayMagic) <<
"Invalid DLTensor file format";
498 ICHECK(strm->Read(&dev)) <<
"Invalid DLTensor file format";
499 ICHECK(strm->Read(&ndim)) <<
"Invalid DLTensor file format";
500 ICHECK(strm->Read(&dtype)) <<
"Invalid DLTensor file format";
501 ICHECK_EQ(dev.device_type, kDLCPU) <<
"Invalid DLTensor device: can only save as CPU tensor";
502 std::vector<int64_t>
shape(ndim);
504 ICHECK(strm->ReadArray(&shape[0], ndim)) <<
"Invalid DLTensor file format";
507 int64_t num_elems = 1;
508 int elem_bytes = (ret->dtype.bits + 7) / 8;
509 for (
int i = 0; i < ret->ndim; ++i) {
510 num_elems *= ret->shape[i];
512 int64_t data_byte_size;
513 ICHECK(strm->Read(&data_byte_size)) <<
"Invalid DLTensor file format";
514 ICHECK(data_byte_size == num_elems * elem_bytes) <<
"Invalid DLTensor file format";
515 auto read_ret = strm->Read(ret->data, data_byte_size);
517 if (ndim > 0 && shape[0] != 0) {
518 ICHECK(read_ret) <<
"Invalid DLTensor file format";
520 if (!DMLC_IO_NO_ENDIAN_SWAP) {
521 dmlc::ByteSwap(ret->data, elem_bytes, num_elems);
532 struct hash<tvm::
Device> {
533 std::size_t operator()(
const tvm::Device& dev)
const {
534 return ((dev.device_id << 8) | dev.device_type);
539 struct equal_to<tvm::
Device> {
541 return (lhs.device_type == rhs.device_type && lhs.device_id == rhs.device_id);
546 #endif // TVM_RUNTIME_NDARRAY_H_ The container base structure contains all the fields except for the Object header.
Definition: ndarray.h:256
DLManagedTensor * ToDLPack() const
Create a reference view of NDArray that represents as DLManagedTensor.
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
static void FFIDecRef(TVMArrayHandle handle)
DecRef resource managed by an FFI array handle.
Definition: ndarray.h:430
runtime::DataType DataType() const
A custom smart pointer for Object.
Definition: object.h:358
Runtime Optional container types.
bool Load(dmlc::Stream *stream)
Load NDArray from stream.
Definition: ndarray.h:490
Runtime String container types.
Internal base class to handle conversion to POD values.
Definition: packed_func.h:541
void CopyFrom(const DLTensor *other)
Copy data content from another array.
Definition: ndarray.h:379
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
static NDArray NewFromDLTensor(DLTensor *dl_tensor, const Device &dev)
Create new NDArray, data is copied from DLTensor.
DLTensor * TVMArrayHandle
the array handle
Definition: c_runtime_api.h:202
Serializer extension to support TVM data types Include this file to enable serialization of DLDataTyp...
NDArray CreateView(ShapeTuple shape, DLDataType dtype)
Create a NDArray that shares the data memory with the current one.
void IncRef()
developer function, increases reference counter.
Definition: object.h:799
Definition: loop_state.h:456
int TVMArrayCopyToBytes(TVMArrayHandle handle, void *data, size_t nbytes)
Copy array data to CPU byte array.
void CopyTo(DLTensor *other) const
Copy data content into another array.
Definition: ndarray.h:390
static NDArray Empty(ShapeTuple shape, DLDataType dtype, Device dev, Optional< String > mem_scope=NullOpt)
Create an empty NDArray.
base class of all object containers.
Definition: object.h:167
bool IsContiguous() const
Definition: ndarray.h:375
Object * TVMArrayHandleToObjectHandle(TVMArrayHandle handle)
Definition: ndarray.h:434
const char * TVMGetLastError(void)
return str message of the last error all function in this file will return 0 when success and nonzero...
void reset()
reset the content of NDArray to be nullptr
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition: c_runtime_api.h:236
void DecRef()
developer function, decrease reference counter.
Definition: object.h:801
static uint32_t RuntimeTypeIndex()
Definition: object.h:225
static void CopyFromTo(const DLTensor *from, DLTensor *to, TVMStreamHandle stream=nullptr)
Function to copy data from one array to another.
int use_count() const
Definition: ndarray.h:409
void Save(dmlc::Stream *stream) const
Save NDArray to stream.
Definition: ndarray.h:488
static bool AbilityOfZeroCopyForDLTensor(DLTensor *tensor, const Device &dev)
Check conditions for construction NDArray over DLTensor without copying. There are three conditions t...
const DLTensor * operator->() const
Definition: ndarray.h:411
static ObjectPtr< Object > FFIDataFromHandle(TVMArrayHandle handle)
Construct NDArray's Data field from array handle in FFI.
Definition: ndarray.h:417
NDArray(ObjectPtr< Object > data)
constructor.
Definition: ndarray.h:65
Runtime primitive data type.
Definition: data_type.h:41
Container * get_mutable() const
Get mutable internal container pointer.
Definition: ndarray.h:413
Object container class that backs NDArray.
Definition: ndarray.h:286
Container(void *data, ShapeTuple shape, DLDataType dtype, Device dev)
Definition: ndarray.h:299
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:574
DLTensor dl_tensor
The corresponding dl_tensor field.
Definition: ndarray.h:264
void SetDeleter(FDeleter deleter)
Set the deleter field.
Definition: ndarray.h:315
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1768
int64_t tvm_index_t
type of array index.
Definition: c_runtime_api.h:88
DLDevice Device
Definition: ndarray.h:43
const Object * get() const
Definition: object.h:546
Base class of all object reference.
Definition: object.h:511
static NDArray FromExternalDLTensor(const DLTensor &dl_tensor)
Create a NDArray backed by an external DLTensor without memory copying.
Runtime ShapeTuple container types.
A managed object in the TVM runtime.
static TVMArrayHandle FFIGetHandle(const ObjectRef &nd)
Get FFI Array handle from ndarray.
Definition: ndarray.h:422
Container()
default constructor
Definition: ndarray.h:289
constexpr uint64_t kTVMNDArrayMagic
Magic number for NDArray file.
Definition: ndarray.h:439
NDArray()
default constructor
Definition: ndarray.h:60
ShapeTupleObj::index_type index_type
The type of shape index element.
Definition: shape_tuple.h:84
void CopyFromBytes(const void *data, size_t nbytes)
Copy data content from a byte buffer.
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
bool SaveDLTensor(dmlc::Stream *strm, const DLTensor *tensor)
Save a DLTensor to stream.
Definition: ndarray.h:441
static NDArray FromDLPack(DLManagedTensor *tensor)
Create a NDArray backed by a dlpack tensor.
Definition: packed_func.h:1517
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
void CopyToBytes(void *data, size_t nbytes) const
Copy data content into another array.
#define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
helper macro to declare a base object type that can be inherited.
Definition: object.h:648
ShapeTuple shape_
The shape container, can be used used for shape data.
Definition: ndarray.h:279
size_t GetDataSize(const DLTensor &arr)
return the size of data the DLTensor hold, in term of number of bytes
Definition: ndarray.h:341
Reference to shape tuple objects.
Definition: shape_tuple.h:81
runtime::NDArray.
Definition: object.h:64