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;
122 inline bool Load(dmlc::Stream* stream);
127 inline void Save(dmlc::Stream* stream)
const;
188 TVM_DLL
static void CopyFromTo(
const DLTensor* from, DLTensor* to,
208 TVM_DLL
static bool IsAligned(
const DLTensor& tensor);
248 inline bool SaveDLTensor(dmlc::Stream* strm,
const DLTensor* tensor);
327 static constexpr
const char*
_type_key =
"runtime.NDArray";
345 size *=
static_cast<size_t>(arr.shape[i]);
347 size *= (arr.dtype.bits * arr.dtype.lanes + 7) / 8;
356 static inline bool IsContiguous(
const DLTensor& arr) {
357 if (arr.strides ==
nullptr)
return true;
358 int64_t expected_stride = 1;
359 for (int32_t i = arr.ndim; i != 0; --i) {
361 if (arr.shape[k] == 1) {
370 if (arr.strides[k] != expected_stride)
return false;
371 expected_stride *= arr.shape[k];
377 return ::tvm::runtime::IsContiguous(
get_mutable()->dl_tensor);
381 ICHECK(
data_ !=
nullptr);
386 ICHECK(
data_ !=
nullptr);
387 ICHECK(other.
data_ !=
nullptr);
392 ICHECK(
data_ !=
nullptr);
397 ICHECK(
data_ !=
nullptr);
398 ICHECK(other.
data_ !=
nullptr);
403 ICHECK(
data_ !=
nullptr);
406 Empty(
ShapeTuple(dptr->shape, dptr->shape + dptr->ndim), dptr->dtype, dev, mem_scope);
420 return GetObjectPtr<Object>(
446 strm->Write(reserved);
457 cpu_dev.device_type = kDLCPU;
458 cpu_dev.device_id = 0;
459 strm->Write(cpu_dev);
460 strm->Write(tensor->ndim);
461 strm->Write(tensor->dtype);
462 int ndim = tensor->ndim;
463 strm->WriteArray(tensor->shape, ndim);
464 int type_bytes = (tensor->dtype.bits + 7) / 8;
465 int64_t num_elems = 1;
466 for (
int i = 0; i < ndim; ++i) {
467 num_elems *= tensor->shape[i];
469 int64_t data_byte_size = type_bytes * num_elems;
470 strm->Write(data_byte_size);
472 if (DMLC_IO_NO_ENDIAN_SWAP && tensor->device.device_type == kDLCPU &&
473 tensor->strides ==
nullptr && tensor->byte_offset == 0) {
475 strm->Write(tensor->data, data_byte_size);
477 std::vector<uint8_t> bytes(data_byte_size);
482 if (!DMLC_IO_NO_ENDIAN_SWAP) {
483 dmlc::ByteSwap(dmlc::BeginPtr(bytes), type_bytes, num_elems);
485 strm->Write(dmlc::BeginPtr(bytes), data_byte_size);
493 uint64_t header, reserved;
494 ICHECK(strm->Read(&header)) <<
"Invalid DLTensor file format";
495 ICHECK(strm->Read(&reserved)) <<
"Invalid DLTensor file format";
500 ICHECK(strm->Read(&dev)) <<
"Invalid DLTensor file format";
501 ICHECK(strm->Read(&ndim)) <<
"Invalid DLTensor file format";
502 ICHECK(strm->Read(&dtype)) <<
"Invalid DLTensor file format";
503 ICHECK_EQ(dev.device_type, kDLCPU) <<
"Invalid DLTensor device: can only save as CPU tensor";
504 std::vector<int64_t>
shape(ndim);
506 ICHECK(strm->ReadArray(&
shape[0], ndim)) <<
"Invalid DLTensor file format";
509 int64_t num_elems = 1;
511 for (
int i = 0; i <
ret->ndim; ++i) {
512 num_elems *=
ret->shape[i];
514 int64_t data_byte_size;
515 ICHECK(strm->Read(&data_byte_size)) <<
"Invalid DLTensor file format";
516 ICHECK(data_byte_size == num_elems * elem_bytes) <<
"Invalid DLTensor file format";
517 auto read_ret = strm->Read(
ret->data, data_byte_size);
519 if (ndim > 0 &&
shape[0] != 0) {
520 ICHECK(read_ret) <<
"Invalid DLTensor file format";
522 if (!DMLC_IO_NO_ENDIAN_SWAP) {
523 dmlc::ByteSwap(
ret->data, elem_bytes, num_elems);
535 std::size_t operator()(
const tvm::Device& dev)
const {
536 return ((dev.device_id << 8) | dev.device_type);
543 return (lhs.device_type == rhs.device_type && lhs.device_id == rhs.device_id);
DLTensor * TVMArrayHandle
the array handle
Definition: c_runtime_api.h:202
const char * TVMGetLastError(void)
return str message of the last error all function in this file will return 0 when success and nonzero...
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition: c_runtime_api.h:236
int64_t tvm_index_t
type of array index.
Definition: c_runtime_api.h:88
int TVMArrayCopyToBytes(TVMArrayHandle handle, void *data, size_t nbytes)
Copy array data to CPU byte array.
DataType dtype() const
Definition: expr.h:128
Runtime primitive data type.
Definition: data_type.h:42
int bits() const
Definition: data_type.h:89
The container base structure contains all the fields except for the Object header.
Definition: ndarray.h:257
DLTensor dl_tensor
The corresponding dl_tensor field.
Definition: ndarray.h:265
void * manager_ctx
additional context, reserved for recycling
Definition: ndarray.h:273
ShapeTuple shape_
The shape container, can be used for shape data.
Definition: ndarray.h:280
Object container class that backs NDArray.
Definition: ndarray.h:287
static constexpr const uint32_t _type_index
Definition: ndarray.h:324
Container()
default constructor
Definition: ndarray.h:290
static constexpr const uint32_t _type_child_slots_can_overflow
Definition: ndarray.h:326
void SetDeleter(FDeleter deleter)
Set the deleter field.
Definition: ndarray.h:316
Container(void *data, ShapeTuple shape, DLDataType dtype, Device dev)
Definition: ndarray.h:300
static constexpr const char * _type_key
Definition: ndarray.h:327
static constexpr const uint32_t _type_child_slots
Definition: ndarray.h:325
friend class RPCWrappedFunc
Definition: ndarray.h:331
void DecRef()
developer function, decrease reference counter.
Definition: object.h:830
TVM_DECLARE_BASE_OBJECT_INFO(NDArray::Container, Object)
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
static TVMArrayHandle FFIGetHandle(const ObjectRef &nd)
Get FFI Array handle from ndarray.
Definition: ndarray.h:424
void CopyFrom(const DLTensor *other)
Copy data content from another array.
Definition: ndarray.h:380
bool IsContiguous() const
Definition: ndarray.h:376
static NDArray FromExternalDLTensor(const DLTensor &dl_tensor)
Create a NDArray backed by an external DLTensor without memory copying.
NDArray()
default constructor
Definition: ndarray.h:60
static NDArray Empty(ShapeTuple shape, DLDataType dtype, Device dev, Optional< String > mem_scope=NullOpt)
Create an empty NDArray.
static NDArray NewFromDLTensor(DLTensor *dl_tensor, const Device &dev)
Create new NDArray, data is copied from DLTensor.
void CopyTo(DLTensor *other) const
Copy data content into another array.
Definition: ndarray.h:391
runtime::DataType DataType() const
int use_count() const
Definition: ndarray.h:411
DLManagedTensor * ToDLPack() const
Create a reference view of NDArray that represents as DLManagedTensor.
static ObjectPtr< Object > FFIDataFromHandle(TVMArrayHandle handle)
Construct NDArray's Data field from array handle in FFI.
Definition: ndarray.h:419
void CopyToBytes(void *data, size_t nbytes) const
Copy data content into another array.
static NDArray FromDLPack(DLManagedTensor *tensor)
Create a NDArray backed by a dlpack tensor.
const DLTensor * operator->() const
Definition: ndarray.h:413
Container * get_mutable() const
Get mutable internal container pointer.
Definition: ndarray.h:415
bool Load(dmlc::Stream *stream)
Load NDArray from stream.
Definition: ndarray.h:492
static void FFIDecRef(TVMArrayHandle handle)
DecRef resource managed by an FFI array handle.
Definition: ndarray.h:432
static bool AbilityOfZeroCopyForDLTensor(DLTensor *tensor, const Device &dev)
Check conditions for construction NDArray over DLTensor without copying. There are three conditions t...
NDArray CreateView(ShapeTuple shape, DLDataType dtype)
Create a NDArray that shares the data memory with the current one.
static void CopyFromTo(const DLTensor *from, DLTensor *to, TVMStreamHandle stream=nullptr)
Function to copy data from one array to another.
void Save(dmlc::Stream *stream) const
Save NDArray to stream.
Definition: ndarray.h:490
void reset()
reset the content of NDArray to be nullptr
void CopyFromBytes(const void *data, size_t nbytes)
Copy data content from a byte buffer.
NDArray(ObjectPtr< Object > data)
constructor.
Definition: ndarray.h:65
A custom smart pointer for Object.
Definition: object.h:360
Base class of all object reference.
Definition: object.h:517
const Object * get() const
Definition: object.h:552
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:603
base class of all object containers.
Definition: object.h:169
uint32_t type_index_
Type index(tag) that indicates the type of the object.
Definition: object.h:263
void DecRef()
developer function, decrease reference counter.
Definition: object.h:830
void(* FDeleter)(Object *self)
Object deleter.
Definition: object.h:175
void IncRef()
developer function, increases reference counter.
Definition: object.h:828
static uint32_t RuntimeTypeIndex()
Definition: object.h:227
FDeleter deleter_
deleter of this object to enable customized allocation. If the deleter is nullptr,...
Definition: object.h:271
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to shape tuple objects.
Definition: shape_tuple.h:81
const index_type * data() const
Return the data pointer.
Definition: shape_tuple.h:121
size_t size() const
Return the size of the shape tuple.
Definition: shape_tuple.h:128
ShapeTupleObj::index_type index_type
The type of shape index element.
Definition: shape_tuple.h:84
Definition: packed_func.h:1666
Internal base class to handle conversion to POD values.
Definition: packed_func.h:544
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
size_t GetDataSize(const DLTensor &arr)
return the size of data the DLTensor hold, in term of number of bytes
Definition: ndarray.h:342
bool SaveDLTensor(dmlc::Stream *strm, const DLTensor *tensor)
Save a DLTensor to stream.
Definition: ndarray.h:443
constexpr uint64_t kTVMNDArrayMagic
Magic number for NDArray file.
Definition: ndarray.h:441
Object * TVMArrayHandleToObjectHandle(TVMArrayHandle handle)
Definition: ndarray.h:436
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:1766
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
DLDevice Device
Definition: ndarray.h:43
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
A managed object in the TVM runtime.
Runtime Optional container types.
Serializer extension to support TVM data types Include this file to enable serialization of DLDataTyp...
Runtime ShapeTuple container types.
Runtime String container types.
@ kRuntimeNDArray
runtime::NDArray.
Definition: object.h:64