24 #ifndef TVM_RUNTIME_TENSOR_H_
25 #define TVM_RUNTIME_TENSOR_H_
27 #include <tvm/ffi/container/shape.h>
28 #include <tvm/ffi/container/tensor.h>
29 #include <tvm/ffi/optional.h>
30 #include <tvm/ffi/string.h>
45 using ffi::GetDataSize;
47 using ffi::IsContiguous;
53 class Tensor :
public tvm::ffi::Tensor {
75 return tvm::ffi::Tensor::FromDLPackVersioned(tensor,
kAllocAlignment,
true);
77 inline const DLTensor*
operator->()
const {
return this->get(); }
84 inline void CopyFrom(
const DLTensor* other);
100 inline void CopyTo(DLTensor* other)
const;
118 ffi::Optional<ffi::String> mem_scope = std::nullopt)
const;
124 inline bool Load(dmlc::Stream* stream);
129 inline void Save(dmlc::Stream* stream)
const;
152 uint64_t relative_byte_offset = 0)
const;
162 ffi::Optional<ffi::String> mem_scope = std::nullopt);
169 TVM_DLL
static void CopyFromTo(
const DLTensor* from, DLTensor* to,
179 TVM_DLL
static void CopyToBytes(
const DLTensor* from,
void* to,
size_t nbytes,
189 TVM_DLL
static void CopyFromBytes(
const DLTensor* to,
void* from,
size_t nbytes,
198 inline bool SaveDLTensor(dmlc::Stream* strm,
const DLTensor* tensor);
201 ICHECK(data_ !=
nullptr);
206 ICHECK(data_ !=
nullptr);
207 ICHECK(other.data_ !=
nullptr);
208 CopyFromTo(other.get_mutable(), get_mutable());
212 ICHECK(data_ !=
nullptr);
217 ICHECK(data_ !=
nullptr);
218 ICHECK(other.data_ !=
nullptr);
219 CopyFromTo(get_mutable(), other.get_mutable());
228 strm->Write(reserved);
239 cpu_dev.device_type = kDLCPU;
240 cpu_dev.device_id = 0;
241 strm->Write(cpu_dev);
242 strm->Write(tensor->ndim);
243 strm->Write(tensor->dtype);
244 int ndim = tensor->ndim;
245 strm->WriteArray(tensor->shape, ndim);
246 int type_bytes = (tensor->dtype.bits + 7) / 8;
247 int64_t num_elems = 1;
248 for (
int i = 0; i < ndim; ++i) {
249 num_elems *= tensor->shape[i];
251 int64_t data_byte_size = type_bytes * num_elems;
252 strm->Write(data_byte_size);
254 if (DMLC_IO_NO_ENDIAN_SWAP && tensor->device.device_type == kDLCPU &&
255 ffi::IsContiguous(*tensor) && tensor->byte_offset == 0) {
257 strm->Write(tensor->data, data_byte_size);
259 std::vector<uint8_t> bytes(data_byte_size);
261 if (!DMLC_IO_NO_ENDIAN_SWAP) {
262 dmlc::ByteSwap(dmlc::BeginPtr(bytes), type_bytes, num_elems);
264 strm->Write(dmlc::BeginPtr(bytes), data_byte_size);
272 uint64_t header, reserved;
273 ICHECK(strm->Read(&header)) <<
"Invalid DLTensor file format";
274 ICHECK(strm->Read(&reserved)) <<
"Invalid DLTensor file format";
279 ICHECK(strm->Read(&dev)) <<
"Invalid DLTensor file format";
280 ICHECK(strm->Read(&ndim)) <<
"Invalid DLTensor file format";
281 ICHECK(strm->Read(&dtype)) <<
"Invalid DLTensor file format";
282 ICHECK_EQ(dev.device_type, kDLCPU) <<
"Invalid DLTensor device: can only save as CPU tensor";
283 std::vector<int64_t>
shape(ndim);
285 ICHECK(strm->ReadArray(&
shape[0], ndim)) <<
"Invalid DLTensor file format";
288 int64_t num_elems = 1;
290 for (
int i = 0; i <
ret->ndim; ++i) {
291 num_elems *=
ret->shape[i];
293 int64_t data_byte_size;
294 ICHECK(strm->Read(&data_byte_size)) <<
"Invalid DLTensor file format";
295 ICHECK(data_byte_size == num_elems * elem_bytes) <<
"Invalid DLTensor file format";
296 auto read_ret = strm->Read(
ret->data, data_byte_size);
298 if (ndim > 0 &&
shape[0] != 0) {
299 ICHECK(read_ret) <<
"Invalid DLTensor file format";
301 if (!DMLC_IO_NO_ENDIAN_SWAP) {
302 dmlc::ByteSwap(
ret->data, elem_bytes, num_elems);
315 if (device.device_type == DLDeviceType::kDLCUDA) {
316 return Device{DLDeviceType::kDLCUDAHost, 0};
317 }
else if (device.device_type == DLDeviceType::kDLROCM) {
318 return Device{DLDeviceType::kDLROCMHost, 0};
321 return Device{DLDeviceType::kDLCPU, 0};
331 std::size_t operator()(
const tvm::Device& dev)
const {
332 return ((dev.device_id << 8) | dev.device_type);
339 return (lhs.device_type == rhs.device_type && lhs.device_id == rhs.device_id);
DataType dtype() const
Definition: expr.h:138
Runtime primitive data type.
Definition: data_type.h:47
int bits() const
Definition: data_type.h:116
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:53
const DLTensor * operator->() const
Definition: tensor.h:77
static Tensor FromDLPackVersioned(DLManagedTensorVersioned *tensor)
Definition: tensor.h:74
static Tensor Empty(ffi::Shape shape, DLDataType dtype, Device dev, ffi::Optional< ffi::String > mem_scope=std::nullopt)
Create an empty Tensor.
void CopyFrom(const DLTensor *other)
Copy data content from another array.
Definition: tensor.h:200
ffi::ShapeView Shape() const
Definition: tensor.h:66
Tensor(ffi::UnsafeInit tag)
Definition: tensor.h:62
void CopyTo(DLTensor *other) const
Copy data content into another array.
Definition: tensor.h:211
void CopyToBytes(void *data, size_t nbytes) const
Copy data content into another array.
Tensor(const ffi::Tensor &other)
Definition: tensor.h:64
Tensor(ffi::Tensor &&other)
Definition: tensor.h:63
static void CopyFromBytes(const DLTensor *to, void *from, size_t nbytes, TVMStreamHandle stream=nullptr)
Function to copy data from one array to a byte buffer.
runtime::DataType DataType() const
Definition: tensor.h:67
static Tensor FromDLPack(DLManagedTensor *tensor)
Definition: tensor.h:70
void Save(dmlc::Stream *stream) const
Save Tensor to stream.
Definition: tensor.h:269
Tensor(ObjectPtr< ffi::TensorObj > data)
constructor.
Definition: tensor.h:61
Tensor CopyTo(const Device &dev, ffi::Optional< ffi::String > mem_scope=std::nullopt) const
Copy the data to another device.
bool Load(dmlc::Stream *stream)
Load Tensor from stream.
Definition: tensor.h:271
ffi::TensorObj Container
Definition: tensor.h:55
void CopyFromBytes(const void *data, size_t nbytes)
Copy data content from a byte buffer.
Tensor CreateView(ffi::Shape shape, DLDataType dtype, uint64_t relative_byte_offset=0) const
Create a Tensor that shares the data memory with the current one.
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.
static void CopyFromTo(const DLTensor *from, DLTensor *to, TVMStreamHandle stream=nullptr)
Function to copy data from one array to another.
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: tensor.h:314
bool SaveDLTensor(dmlc::Stream *strm, const DLTensor *tensor)
Save a DLTensor to stream.
Definition: tensor.h:225
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition: device_api.h:111
constexpr uint64_t kTVMTensorMagic
Magic number for Tensor file.
Definition: tensor.h:223
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:1961
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:461
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...