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/runtime/serializer.h>
36 
37 #include <atomic>
38 #include <functional>
39 #include <utility>
40 #include <vector>
41 
42 namespace tvm {
43 namespace runtime {
44 
45 using ffi::GetDataSize;
46 using ffi::IsAligned;
47 using ffi::IsContiguous;
48 
53 class Tensor : public tvm::ffi::Tensor {
54  public:
55  using Container = ffi::TensorObj;
56  Tensor() = default;
61  explicit Tensor(ObjectPtr<ffi::TensorObj> data) : tvm::ffi::Tensor(data) {}
62  explicit Tensor(ffi::UnsafeInit tag) : tvm::ffi::Tensor(tag) {}
63  Tensor(ffi::Tensor&& other) : tvm::ffi::Tensor(std::move(other)) {} // NOLINT(*)
64  Tensor(const ffi::Tensor& other) : tvm::ffi::Tensor(other) {} // NOLINT(*)
65 
66  ffi::Shape Shape() const { return this->shape(); }
67  runtime::DataType DataType() const { return runtime::DataType(this->dtype()); }
68 
69  // DLPack handling
70  static Tensor FromDLPack(DLManagedTensor* tensor) {
71  return tvm::ffi::Tensor::FromDLPack(tensor, kAllocAlignment, true);
72  }
73 
74  static Tensor FromDLPackVersioned(DLManagedTensorVersioned* tensor) {
75  return tvm::ffi::Tensor::FromDLPackVersioned(tensor, kAllocAlignment, true);
76  }
83  inline void CopyFrom(const DLTensor* other);
84  inline void CopyFrom(const Tensor& other);
92  TVM_DLL void CopyFromBytes(const void* data, size_t nbytes);
99  inline void CopyTo(DLTensor* other) const;
100  inline void CopyTo(const Tensor& other) const;
108  TVM_DLL void CopyToBytes(void* data, size_t nbytes) const;
116  TVM_DLL Tensor CopyTo(const Device& dev,
117  ffi::Optional<ffi::String> mem_scope = std::nullopt) const;
123  inline bool Load(dmlc::Stream* stream);
128  inline void Save(dmlc::Stream* stream) const;
129 
150  TVM_DLL Tensor CreateView(ffi::Shape shape, DLDataType dtype,
151  uint64_t relative_byte_offset = 0) const;
160  TVM_DLL static Tensor Empty(ffi::Shape shape, DLDataType dtype, Device dev,
161  ffi::Optional<ffi::String> mem_scope = std::nullopt);
168  TVM_DLL static void CopyFromTo(const DLTensor* from, DLTensor* to,
169  TVMStreamHandle stream = nullptr);
170 
178  TVM_DLL static void CopyToBytes(const DLTensor* from, void* to, size_t nbytes,
179  TVMStreamHandle stream = nullptr);
180 };
181 
187 inline bool SaveDLTensor(dmlc::Stream* strm, const DLTensor* tensor);
188 
189 inline void Tensor::CopyFrom(const DLTensor* other) {
190  ICHECK(data_ != nullptr);
191  CopyFromTo(other, get_mutable());
192 }
193 
194 inline void Tensor::CopyFrom(const Tensor& other) {
195  ICHECK(data_ != nullptr);
196  ICHECK(other.data_ != nullptr);
197  CopyFromTo(other.get_mutable(), get_mutable());
198 }
199 
200 inline void Tensor::CopyTo(DLTensor* other) const {
201  ICHECK(data_ != nullptr);
202  CopyFromTo(get_mutable(), other);
203 }
204 
205 inline void Tensor::CopyTo(const Tensor& other) const {
206  ICHECK(data_ != nullptr);
207  ICHECK(other.data_ != nullptr);
208  CopyFromTo(get_mutable(), other.get_mutable());
209 }
210 
212 constexpr uint64_t kTVMTensorMagic = 0xDD5E40F096B4A13F;
213 
214 inline bool SaveDLTensor(dmlc::Stream* strm, const DLTensor* tensor) {
215  uint64_t header = kTVMTensorMagic, reserved = 0;
216  strm->Write(header);
217  strm->Write(reserved);
218  // Always save data as CPU context
219  //
220  // Parameters that get serialized should be in CPU by default.
221  // So even the array's context is GPU, it will be stored as CPU array.
222  // This is used to prevent case when another user loads the parameters
223  // back on machine that do not have GPU or related context.
224  //
225  // We can always do array.CopyTo(target_dev) to get a corresponding
226  // array in the target context.
227  Device cpu_dev;
228  cpu_dev.device_type = kDLCPU;
229  cpu_dev.device_id = 0;
230  strm->Write(cpu_dev);
231  strm->Write(tensor->ndim);
232  strm->Write(tensor->dtype);
233  int ndim = tensor->ndim;
234  strm->WriteArray(tensor->shape, ndim);
235  int type_bytes = (tensor->dtype.bits + 7) / 8;
236  int64_t num_elems = 1;
237  for (int i = 0; i < ndim; ++i) {
238  num_elems *= tensor->shape[i];
239  }
240  int64_t data_byte_size = type_bytes * num_elems;
241  strm->Write(data_byte_size);
242 
243  if (DMLC_IO_NO_ENDIAN_SWAP && tensor->device.device_type == kDLCPU &&
244  ffi::IsContiguous(*tensor) && tensor->byte_offset == 0) {
245  // quick path
246  strm->Write(tensor->data, data_byte_size);
247  } else {
248  std::vector<uint8_t> bytes(data_byte_size);
249  Tensor::CopyToBytes(const_cast<DLTensor*>(tensor), dmlc::BeginPtr(bytes), data_byte_size);
250  if (!DMLC_IO_NO_ENDIAN_SWAP) {
251  dmlc::ByteSwap(dmlc::BeginPtr(bytes), type_bytes, num_elems);
252  }
253  strm->Write(dmlc::BeginPtr(bytes), data_byte_size);
254  }
255  return true;
256 }
257 
258 inline void Tensor::Save(dmlc::Stream* strm) const { SaveDLTensor(strm, operator->()); }
259 
260 inline bool Tensor::Load(dmlc::Stream* strm) {
261  uint64_t header, reserved;
262  ICHECK(strm->Read(&header)) << "Invalid DLTensor file format";
263  ICHECK(strm->Read(&reserved)) << "Invalid DLTensor file format";
264  ICHECK(header == kTVMTensorMagic) << "Invalid DLTensor file format";
265  Device dev;
266  int ndim;
267  DLDataType dtype;
268  ICHECK(strm->Read(&dev)) << "Invalid DLTensor file format";
269  ICHECK(strm->Read(&ndim)) << "Invalid DLTensor file format";
270  ICHECK(strm->Read(&dtype)) << "Invalid DLTensor file format";
271  ICHECK_EQ(dev.device_type, kDLCPU) << "Invalid DLTensor device: can only save as CPU tensor";
272  std::vector<int64_t> shape(ndim);
273  if (ndim != 0) {
274  ICHECK(strm->ReadArray(&shape[0], ndim)) << "Invalid DLTensor file format";
275  }
276  Tensor ret = Tensor::Empty(ffi::Shape(shape), dtype, dev);
277  int64_t num_elems = 1;
278  int elem_bytes = (ret->dtype.bits + 7) / 8;
279  for (int i = 0; i < ret->ndim; ++i) {
280  num_elems *= ret->shape[i];
281  }
282  int64_t data_byte_size;
283  ICHECK(strm->Read(&data_byte_size)) << "Invalid DLTensor file format";
284  ICHECK(data_byte_size == num_elems * elem_bytes) << "Invalid DLTensor file format";
285  auto read_ret = strm->Read(ret->data, data_byte_size);
286  // Only check non-empty data
287  if (ndim > 0 && shape[0] != 0) {
288  ICHECK(read_ret) << "Invalid DLTensor file format";
289  }
290  if (!DMLC_IO_NO_ENDIAN_SWAP) {
291  dmlc::ByteSwap(ret->data, elem_bytes, num_elems);
292  }
293  *this = ret;
294  return true;
295 }
296 
304  if (device.device_type == DLDeviceType::kDLCUDA) {
305  return Device{DLDeviceType::kDLCUDAHost, 0};
306  } else if (device.device_type == DLDeviceType::kDLROCM) {
307  return Device{DLDeviceType::kDLROCMHost, 0};
308  } else {
309  // Fallback to CPU.
310  return Device{DLDeviceType::kDLCPU, 0};
311  }
312 }
313 
314 } // namespace runtime
315 } // namespace tvm
316 
317 namespace std {
318 template <>
319 struct hash<tvm::Device> {
320  std::size_t operator()(const tvm::Device& dev) const {
321  return ((dev.device_id << 8) | dev.device_type);
322  }
323 };
324 
325 template <>
326 struct equal_to<tvm::Device> {
327  bool operator()(const tvm::Device& lhs, const tvm::Device& rhs) const {
328  return (lhs.device_type == rhs.device_type && lhs.device_id == rhs.device_id);
329  }
330 };
331 } // namespace std
332 
333 #endif // TVM_RUNTIME_TENSOR_H_
DataType dtype() const
Definition: expr.h:138
Runtime primitive data type.
Definition: data_type.h:47
int bits() const
Definition: data_type.h:115
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:53
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:189
Tensor(ffi::UnsafeInit tag)
Definition: tensor.h:62
ffi::Shape Shape() const
Definition: tensor.h:66
void CopyTo(DLTensor *other) const
Copy data content into another array.
Definition: tensor.h:200
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
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:258
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:260
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:303
bool SaveDLTensor(dmlc::Stream *strm, const DLTensor *tensor)
Save a DLTensor to stream.
Definition: tensor.h:214
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:212
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:1960
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...