tvm
tensor.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_RUNTIME_TENSOR_H_
25 #define TVM_RUNTIME_TENSOR_H_
26 
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>
31 #include <tvm/runtime/base.h>
32 #include <tvm/runtime/data_type.h>
33 #include <tvm/runtime/device_api.h>
34 #include <tvm/runtime/object.h>
35 #include <tvm/support/io.h>
36 #include <tvm/support/serializer.h>
37 
38 #include <atomic>
39 #include <functional>
40 #include <utility>
41 #include <vector>
42 
43 namespace tvm {
44 namespace runtime {
45 
46 using ffi::GetDataSize;
47 using ffi::IsAligned;
48 using ffi::IsContiguous;
49 
54 class Tensor : public tvm::ffi::Tensor {
55  public:
56  using Container = ffi::TensorObj;
57  Tensor() = default;
62  explicit Tensor(ObjectPtr<ffi::TensorObj> data) : tvm::ffi::Tensor(data) {}
63  explicit Tensor(ffi::UnsafeInit tag) : tvm::ffi::Tensor(tag) {}
64  Tensor(ffi::Tensor&& other) : tvm::ffi::Tensor(std::move(other)) {} // NOLINT(*)
65  Tensor(const ffi::Tensor& other) : tvm::ffi::Tensor(other) {} // NOLINT(*)
66 
67  ffi::ShapeView Shape() const { return this->shape(); }
68  runtime::DataType DataType() const { return runtime::DataType(this->dtype()); }
69 
70  // DLPack handling
71  static Tensor FromDLPack(DLManagedTensor* tensor) {
72  return tvm::ffi::Tensor::FromDLPack(tensor, kAllocAlignment, true);
73  }
74 
75  static Tensor FromDLPackVersioned(DLManagedTensorVersioned* tensor) {
76  return tvm::ffi::Tensor::FromDLPackVersioned(tensor, kAllocAlignment, true);
77  }
78  inline const DLTensor* operator->() const { return this->get(); }
85  inline void CopyFrom(const DLTensor* other);
86  inline void CopyFrom(const Tensor& other);
94  TVM_DLL void CopyFromBytes(const void* data, size_t nbytes);
101  inline void CopyTo(DLTensor* other) const;
102  inline void CopyTo(const Tensor& other) const;
110  TVM_DLL void CopyToBytes(void* data, size_t nbytes) const;
118  TVM_DLL Tensor CopyTo(const Device& dev,
119  ffi::Optional<ffi::String> mem_scope = std::nullopt) const;
125  inline bool Load(support::Stream* stream);
130  inline void Save(support::Stream* stream) const;
131 
152  TVM_DLL Tensor CreateView(ffi::Shape shape, DLDataType dtype,
153  uint64_t relative_byte_offset = 0) const;
162  TVM_DLL static Tensor Empty(ffi::Shape shape, DLDataType dtype, Device dev,
163  ffi::Optional<ffi::String> mem_scope = std::nullopt);
170  TVM_DLL static void CopyFromTo(const DLTensor* from, DLTensor* to,
171  TVMStreamHandle stream = nullptr);
172 
180  TVM_DLL static void CopyToBytes(const DLTensor* from, void* to, size_t nbytes,
181  TVMStreamHandle stream = nullptr);
182 
190  TVM_DLL static void CopyFromBytes(const DLTensor* to, void* from, size_t nbytes,
191  TVMStreamHandle stream = nullptr);
192 };
193 
199 inline bool SaveDLTensor(support::Stream* strm, const DLTensor* tensor);
200 
201 inline void Tensor::CopyFrom(const DLTensor* other) {
202  TVM_FFI_ICHECK(data_ != nullptr);
203  CopyFromTo(other, get_mutable());
204 }
205 
206 inline void Tensor::CopyFrom(const Tensor& other) {
207  TVM_FFI_ICHECK(data_ != nullptr);
208  TVM_FFI_ICHECK(other.data_ != nullptr);
209  CopyFromTo(other.get_mutable(), get_mutable());
210 }
211 
212 inline void Tensor::CopyTo(DLTensor* other) const {
213  TVM_FFI_ICHECK(data_ != nullptr);
214  CopyFromTo(get_mutable(), other);
215 }
216 
217 inline void Tensor::CopyTo(const Tensor& other) const {
218  TVM_FFI_ICHECK(data_ != nullptr);
219  TVM_FFI_ICHECK(other.data_ != nullptr);
220  CopyFromTo(get_mutable(), other.get_mutable());
221 }
222 
224 constexpr uint64_t kTVMTensorMagic = 0xDD5E40F096B4A13F;
225 
226 inline bool SaveDLTensor(support::Stream* strm, const DLTensor* tensor) {
227  uint64_t header = kTVMTensorMagic, reserved = 0;
228  strm->Write(header);
229  strm->Write(reserved);
230  // Always save data as CPU context
231  //
232  // Parameters that get serialized should be in CPU by default.
233  // So even the array's context is GPU, it will be stored as CPU array.
234  // This is used to prevent case when another user loads the parameters
235  // back on machine that do not have GPU or related context.
236  //
237  // We can always do array.CopyTo(target_dev) to get a corresponding
238  // array in the target context.
239  Device cpu_dev;
240  cpu_dev.device_type = kDLCPU;
241  cpu_dev.device_id = 0;
242  strm->Write(cpu_dev);
243  strm->Write(tensor->ndim);
244  strm->Write(tensor->dtype);
245  int ndim = tensor->ndim;
246  strm->WriteArray(tensor->shape, ndim);
247  int type_bytes = (tensor->dtype.bits + 7) / 8;
248  int64_t num_elems = 1;
249  for (int i = 0; i < ndim; ++i) {
250  num_elems *= tensor->shape[i];
251  }
252  int64_t data_byte_size = type_bytes * num_elems;
253  strm->Write(data_byte_size);
254 
255  if (TVM_FFI_IO_NO_ENDIAN_SWAP && tensor->device.device_type == kDLCPU &&
256  ffi::IsContiguous(*tensor) && tensor->byte_offset == 0) {
257  // quick path
258  strm->Write(tensor->data, data_byte_size);
259  } else {
260  std::vector<uint8_t> bytes(data_byte_size);
261  Tensor::CopyToBytes(const_cast<DLTensor*>(tensor), bytes.data(), data_byte_size);
262  if (!TVM_FFI_IO_NO_ENDIAN_SWAP) {
263  ffi::ByteSwap(bytes.data(), type_bytes, num_elems);
264  }
265  strm->Write(bytes.data(), data_byte_size);
266  }
267  return true;
268 }
269 
270 inline void Tensor::Save(support::Stream* strm) const { SaveDLTensor(strm, operator->()); }
271 
272 inline bool Tensor::Load(support::Stream* strm) {
273  uint64_t header, reserved;
274  TVM_FFI_ICHECK(strm->Read(&header)) << "Invalid DLTensor file format";
275  TVM_FFI_ICHECK(strm->Read(&reserved)) << "Invalid DLTensor file format";
276  TVM_FFI_ICHECK(header == kTVMTensorMagic) << "Invalid DLTensor file format";
277  Device dev;
278  int ndim;
279  DLDataType dtype;
280  TVM_FFI_ICHECK(strm->Read(&dev)) << "Invalid DLTensor file format";
281  TVM_FFI_ICHECK(strm->Read(&ndim)) << "Invalid DLTensor file format";
282  TVM_FFI_ICHECK(strm->Read(&dtype)) << "Invalid DLTensor file format";
283  TVM_FFI_ICHECK_EQ(dev.device_type, kDLCPU)
284  << "Invalid DLTensor device: can only save as CPU tensor";
285  std::vector<int64_t> shape(ndim);
286  if (ndim != 0) {
287  TVM_FFI_ICHECK(strm->ReadArray(&shape[0], ndim)) << "Invalid DLTensor file format";
288  }
289  Tensor ret = Tensor::Empty(ffi::Shape(shape), dtype, dev);
290  int64_t num_elems = 1;
291  int elem_bytes = (ret->dtype.bits + 7) / 8;
292  for (int i = 0; i < ret->ndim; ++i) {
293  num_elems *= ret->shape[i];
294  }
295  int64_t data_byte_size;
296  TVM_FFI_ICHECK(strm->Read(&data_byte_size)) << "Invalid DLTensor file format";
297  TVM_FFI_ICHECK(data_byte_size == num_elems * elem_bytes) << "Invalid DLTensor file format";
298  auto read_ret = strm->Read(ret->data, data_byte_size);
299  // Only check non-empty data
300  if (ndim > 0 && shape[0] != 0) {
301  TVM_FFI_ICHECK(read_ret) << "Invalid DLTensor file format";
302  }
303  if (!TVM_FFI_IO_NO_ENDIAN_SWAP) {
304  ffi::ByteSwap(ret->data, elem_bytes, num_elems);
305  }
306  *this = ret;
307  return true;
308 }
309 
317  if (device.device_type == DLDeviceType::kDLCUDA) {
318  return Device{DLDeviceType::kDLCUDAHost, 0};
319  } else if (device.device_type == DLDeviceType::kDLROCM) {
320  return Device{DLDeviceType::kDLROCMHost, 0};
321  } else {
322  // Fallback to CPU.
323  return Device{DLDeviceType::kDLCPU, 0};
324  }
325 }
326 
327 } // namespace runtime
328 } // namespace tvm
329 
330 namespace std {
331 template <>
332 struct hash<tvm::Device> {
333  std::size_t operator()(const tvm::Device& dev) const {
334  return ((dev.device_id << 8) | dev.device_type);
335  }
336 };
337 
338 template <>
339 struct equal_to<tvm::Device> {
340  bool operator()(const tvm::Device& lhs, const tvm::Device& rhs) const {
341  return (lhs.device_type == rhs.device_type && lhs.device_id == rhs.device_id);
342  }
343 };
344 } // namespace std
345 
346 #endif // TVM_RUNTIME_TENSOR_H_
DataType dtype() const
Definition: expr.h:140
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:54
const DLTensor * operator->() const
Definition: tensor.h:78
static Tensor FromDLPackVersioned(DLManagedTensorVersioned *tensor)
Definition: tensor.h:75
bool Load(support::Stream *stream)
Load Tensor from stream.
Definition: tensor.h:272
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:201
void Save(support::Stream *stream) const
Save Tensor to stream.
Definition: tensor.h:270
ffi::ShapeView Shape() const
Definition: tensor.h:67
Tensor(ffi::UnsafeInit tag)
Definition: tensor.h:63
void CopyTo(DLTensor *other) const
Copy data content into another array.
Definition: tensor.h:212
void CopyToBytes(void *data, size_t nbytes) const
Copy data content into another array.
Tensor(const ffi::Tensor &other)
Definition: tensor.h:65
Tensor(ffi::Tensor &&other)
Definition: tensor.h:64
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:68
static Tensor FromDLPack(DLManagedTensor *tensor)
Definition: tensor.h:71
Tensor(ObjectPtr< ffi::TensorObj > data)
constructor.
Definition: tensor.h:62
Tensor CopyTo(const Device &dev, ffi::Optional< ffi::String > mem_scope=std::nullopt) const
Copy the data to another device.
ffi::TensorObj Container
Definition: tensor.h:56
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 binary stream for serialization.
Definition: io.h:57
void WriteArray(const T *data, size_t num_elems)
Write an array of typed values element by element.
Definition: io.h:107
virtual size_t Read(void *ptr, size_t size)=0
Read raw bytes from the stream.
bool ReadArray(T *data, size_t num_elems)
Read an array of typed values element by element.
Definition: io.h:118
virtual size_t Write(const void *ptr, size_t size)=0
Write raw bytes to the stream.
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
Binary stream I/O interface.
bool SaveDLTensor(support::Stream *strm, const DLTensor *tensor)
Save a DLTensor to stream.
Definition: tensor.h:226
Device GetPreferredHostDevice(Device device)
Get the preferred host device from the input device.
Definition: tensor.h:316
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:224
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:1981
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
runtime::DataType DataType
Definition: data_type.h:462
DLDevice Device
Definition: device_api.h:42
A managed object in the TVM runtime.
Serializer<T> specializations for tvm::support::Stream.