24 #ifndef TVM_RUNTIME_NDARRAY_H_
25 #define TVM_RUNTIME_NDARRAY_H_
27 #include <tvm/ffi/container/ndarray.h>
28 #include <tvm/ffi/container/shape.h>
29 #include <tvm/ffi/optional.h>
30 #include <tvm/ffi/string.h>
45 using ffi::GetDataSize;
47 using ffi::IsContiguous;
53 class NDArray :
public tvm::ffi::NDArray {
74 return tvm::ffi::NDArray::FromDLPackVersioned(tensor,
kAllocAlignment,
true);
82 inline void CopyFrom(
const DLTensor* other);
98 inline void CopyTo(DLTensor* other)
const;
121 inline bool Load(dmlc::Stream* stream);
126 inline void Save(dmlc::Stream* stream)
const;
149 uint64_t relative_byte_offset = 0)
const;
159 Optional<String> mem_scope = std::nullopt);
166 TVM_DLL
static void CopyFromTo(
const DLTensor* from, DLTensor* to,
176 TVM_DLL
static void CopyToBytes(
const DLTensor* from,
void* to,
size_t nbytes,
185 inline bool SaveDLTensor(dmlc::Stream* strm,
const DLTensor* tensor);
188 ICHECK(data_ !=
nullptr);
193 ICHECK(data_ !=
nullptr);
194 ICHECK(other.data_ !=
nullptr);
195 CopyFromTo(other.get_mutable(), get_mutable());
199 ICHECK(data_ !=
nullptr);
204 ICHECK(data_ !=
nullptr);
205 ICHECK(other.data_ !=
nullptr);
206 CopyFromTo(get_mutable(), other.get_mutable());
215 strm->Write(reserved);
226 cpu_dev.device_type = kDLCPU;
227 cpu_dev.device_id = 0;
228 strm->Write(cpu_dev);
229 strm->Write(tensor->ndim);
230 strm->Write(tensor->dtype);
231 int ndim = tensor->ndim;
232 strm->WriteArray(tensor->shape, ndim);
233 int type_bytes = (tensor->dtype.bits + 7) / 8;
234 int64_t num_elems = 1;
235 for (
int i = 0; i < ndim; ++i) {
236 num_elems *= tensor->shape[i];
238 int64_t data_byte_size = type_bytes * num_elems;
239 strm->Write(data_byte_size);
241 if (DMLC_IO_NO_ENDIAN_SWAP && tensor->device.device_type == kDLCPU &&
242 tensor->strides ==
nullptr && tensor->byte_offset == 0) {
244 strm->Write(tensor->data, data_byte_size);
246 std::vector<uint8_t> bytes(data_byte_size);
248 if (!DMLC_IO_NO_ENDIAN_SWAP) {
249 dmlc::ByteSwap(dmlc::BeginPtr(bytes), type_bytes, num_elems);
251 strm->Write(dmlc::BeginPtr(bytes), data_byte_size);
259 uint64_t header, reserved;
260 ICHECK(strm->Read(&header)) <<
"Invalid DLTensor file format";
261 ICHECK(strm->Read(&reserved)) <<
"Invalid DLTensor file format";
266 ICHECK(strm->Read(&dev)) <<
"Invalid DLTensor file format";
267 ICHECK(strm->Read(&ndim)) <<
"Invalid DLTensor file format";
268 ICHECK(strm->Read(&dtype)) <<
"Invalid DLTensor file format";
269 ICHECK_EQ(dev.device_type, kDLCPU) <<
"Invalid DLTensor device: can only save as CPU tensor";
270 std::vector<int64_t>
shape(ndim);
272 ICHECK(strm->ReadArray(&
shape[0], ndim)) <<
"Invalid DLTensor file format";
275 int64_t num_elems = 1;
277 for (
int i = 0; i <
ret->ndim; ++i) {
278 num_elems *=
ret->shape[i];
280 int64_t data_byte_size;
281 ICHECK(strm->Read(&data_byte_size)) <<
"Invalid DLTensor file format";
282 ICHECK(data_byte_size == num_elems * elem_bytes) <<
"Invalid DLTensor file format";
283 auto read_ret = strm->Read(
ret->data, data_byte_size);
285 if (ndim > 0 &&
shape[0] != 0) {
286 ICHECK(read_ret) <<
"Invalid DLTensor file format";
288 if (!DMLC_IO_NO_ENDIAN_SWAP) {
289 dmlc::ByteSwap(
ret->data, elem_bytes, num_elems);
302 if (device.device_type == DLDeviceType::kDLCUDA) {
303 return Device{DLDeviceType::kDLCUDAHost, 0};
304 }
else if (device.device_type == DLDeviceType::kDLROCM) {
305 return Device{DLDeviceType::kDLROCMHost, 0};
308 return Device{DLDeviceType::kDLCPU, 0};
318 std::size_t operator()(
const tvm::Device& dev)
const {
319 return ((dev.device_id << 8) | dev.device_type);
326 return (lhs.device_type == rhs.device_type && lhs.device_id == rhs.device_id);
DataType dtype() const
Definition: expr.h:143
Runtime primitive data type.
Definition: data_type.h:47
int bits() const
Definition: data_type.h:115
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:53
void CopyFrom(const DLTensor *other)
Copy data content from another array.
Definition: ndarray.h:187
static void CopyToBytes(const DLTensor *from, void *to, size_t nbytes, TVMStreamHandle stream=nullptr)
Function to copy data from one array to a byte buffer.
ffi::Shape Shape() const
Definition: ndarray.h:65
NDArray CreateView(ffi::Shape shape, DLDataType dtype, uint64_t relative_byte_offset=0) const
Create a NDArray that shares the data memory with the current one.
ffi::NDArrayObj Container
Definition: ndarray.h:55
NDArray CopyTo(const Device &dev, Optional< String > mem_scope=std::nullopt) const
Copy the data to another device.
NDArray(ffi::NDArray &&other)
Definition: ndarray.h:62
void CopyTo(DLTensor *other) const
Copy data content into another array.
Definition: ndarray.h:198
runtime::DataType DataType() const
Definition: ndarray.h:66
static NDArray FromDLPackVersioned(DLManagedTensorVersioned *tensor)
Definition: ndarray.h:73
void CopyToBytes(void *data, size_t nbytes) const
Copy data content into another array.
static NDArray FromDLPack(DLManagedTensor *tensor)
Definition: ndarray.h:69
bool Load(dmlc::Stream *stream)
Load NDArray from stream.
Definition: ndarray.h:258
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:256
static NDArray Empty(ffi::Shape shape, DLDataType dtype, Device dev, Optional< String > mem_scope=std::nullopt)
Create an empty NDArray.
void CopyFromBytes(const void *data, size_t nbytes)
Copy data content from a byte buffer.
NDArray(ObjectPtr< Object > data)
constructor.
Definition: ndarray.h:61
NDArray(const ffi::NDArray &other)
Definition: ndarray.h:63
Abstract device memory management API.
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition: device_api.h:37
Device GetPreferredHostDevice(Device device)
Get the preferred host device from the input device.
Definition: ndarray.h:301
bool SaveDLTensor(dmlc::Stream *strm, const DLTensor *tensor)
Save a DLTensor to stream.
Definition: ndarray.h:212
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition: device_api.h:111
constexpr uint64_t kTVMNDArrayMagic
Magic number for NDArray file.
Definition: ndarray.h:210
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:1945
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
runtime::DataType DataType
Definition: data_type.h:458
DLDevice Device
Definition: device_api.h:42
A managed object in the TVM runtime.
Serializer extension to support TVM data types Include this file to enable serialization of DLDataTyp...