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/dtype.h>
30#include <tvm/ffi/optional.h>
31#include <tvm/ffi/string.h>
49class Tensor :
public tvm::ffi::Tensor {
61 ffi::ShapeView
Shape()
const {
return this->shape(); }
70 return tvm::ffi::Tensor::FromDLPackVersioned(tensor,
kAllocAlignment,
true);
113 ffi::Optional<ffi::String>
mem_scope = std::nullopt)
const;
157 ffi::Optional<ffi::String>
mem_scope = std::nullopt);
257 strm->Write(tensor->ndim);
258 strm->Write(tensor->dtype);
259 int ndim = tensor->ndim;
260 strm->WriteArray(tensor->shape, ndim);
261 int type_bytes = (tensor->dtype.bits + 7) / 8;
263 for (
int i = 0;
i < ndim; ++
i) {
270 ffi::IsContiguous(*tensor) && tensor->byte_offset == 0) {
298 <<
"Invalid DLTensor device: can only save as CPU tensor";
299 std::vector<int64_t> shape(ndim);
306 for (
int i = 0;
i < ret->ndim; ++
i) {
314 if (ndim > 0 && shape[0] != 0) {
331 if (device.device_type == DLDeviceType::kDLCUDA) {
332 return Device{DLDeviceType::kDLCUDAHost, 0};
333 }
else if (device.device_type == DLDeviceType::kDLROCM) {
334 return Device{DLDeviceType::kDLROCMHost, 0};
337 return Device{DLDeviceType::kDLCPU, 0};
347 std::size_t operator()(
const tvm::Device& dev)
const {
348 return ((dev.device_id << 8) | dev.device_type);
355 return (lhs.device_type == rhs.device_type && lhs.device_id == rhs.device_id);
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed Tensor. The array is backed by reference counted blocks.
Definition tensor.h:49
const DLTensor * operator->() const
Definition tensor.h:72
static bool IsStorageShared(const Tensor &a, const Tensor &b)
Tensor overload of IsStorageShared.
static TVM_RUNTIME_DLL 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 TVM_RUNTIME_DLL Tensor Empty(ffi::Shape shape, DLDataType dtype, Device dev, ffi::Optional< ffi::String > mem_scope=std::nullopt)
Create an empty Tensor.
static Tensor FromDLPackVersioned(DLManagedTensorVersioned *tensor)
Definition tensor.h:69
TVM_RUNTIME_DLL void CopyToBytes(void *data, size_t nbytes) const
Copy data content into another array.
DLDataType DataType() const
Definition tensor.h:62
bool Load(support::Stream *stream)
Load Tensor from stream.
Definition tensor.h:286
static TVM_RUNTIME_DLL bool IsStorageShared(const DLTensor *a, const DLTensor *b)
Check if two tensors share the same underlying storage.
void CopyFrom(const DLTensor *other)
Copy data content from another array.
Definition tensor.h:215
TVM_RUNTIME_DLL Tensor CopyTo(const Device &dev, ffi::Optional< ffi::String > mem_scope=std::nullopt) const
Copy the data to another device.
void Save(support::Stream *stream) const
Save Tensor to stream.
Definition tensor.h:284
ffi::ShapeView Shape() const
Definition tensor.h:61
Tensor(ffi::UnsafeInit tag)
Definition tensor.h:57
void CopyTo(DLTensor *other) const
Copy data content into another array.
Definition tensor.h:226
static TVM_RUNTIME_DLL void CopyFromTo(const DLTensor *from, DLTensor *to, TVMStreamHandle stream=nullptr)
Function to copy data from one array to another.
Tensor(const ffi::Tensor &other)
Definition tensor.h:59
Tensor(ffi::Tensor &&other)
Definition tensor.h:58
Tensor(ffi::ObjectPtr< ffi::TensorObj > data)
constructor.
Definition tensor.h:56
TVM_RUNTIME_DLL void CopyFromBytes(const void *data, size_t nbytes)
Copy data content from a byte buffer.
static Tensor FromDLPack(DLManagedTensor *tensor)
Definition tensor.h:65
TVM_RUNTIME_DLL 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 TVM_RUNTIME_DLL void CopyFromBytes(const DLTensor *to, void *from, size_t nbytes, TVMStreamHandle stream=nullptr)
Function to copy data from one array to a byte buffer.
Abstract binary stream for serialization.
Definition io.h:57
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:39
Binary stream I/O interface.
bool SaveDLTensor(support::Stream *strm, const DLTensor *tensor)
Save a DLTensor to stream.
Definition tensor.h:240
Device GetPreferredHostDevice(Device device)
Get the preferred host device from the input device.
Definition tensor.h:330
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition device_api.h:113
constexpr uint64_t kTVMTensorMagic
Magic number for Tensor file.
Definition tensor.h:238
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
DLDevice Device
Definition device_api.h:44
#define TVM_RUNTIME_DLL
Definition base.h:92
Serializer<T> specializations for tvm::support::Stream.