tvm
device_api.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_DEVICE_API_H_
25 #define TVM_RUNTIME_DEVICE_API_H_
26 
28 #include <tvm/runtime/ndarray.h>
30 
31 #include <string>
32 
33 namespace tvm {
34 namespace runtime {
38 enum DeviceAttrKind : int {
39  kExist = 0,
41  kWarpSize = 2,
49  kGcnArch = 10,
52 };
53 
55 constexpr int kAllocAlignment = 128;
56 
58 constexpr int kTempAllocaAlignment = 128;
59 
61 constexpr int kMaxStackAlloca = 1024;
62 
65 constexpr int kDefaultWorkspaceAlignment = 1;
66 
71 class TVM_DLL DeviceAPI {
72  public:
74  virtual ~DeviceAPI() {}
79  virtual void SetDevice(Device dev) = 0;
87  virtual void GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue* rv) = 0;
88 
95  virtual void GetTargetProperty(Device dev, const std::string& property, TVMRetValue* rv) {}
96 
106  virtual void* AllocDataSpace(Device dev, size_t nbytes, size_t alignment,
107  DLDataType type_hint) = 0;
117  virtual void* AllocDataSpace(Device dev, int ndim, const int64_t* shape, DLDataType dtype,
118  Optional<String> mem_scope = NullOpt);
124  virtual void FreeDataSpace(Device dev, void* ptr) = 0;
133  virtual void CopyDataFromTo(DLTensor* from, DLTensor* to, TVMStreamHandle stream);
139  virtual TVMStreamHandle CreateStream(Device dev);
140 
147  virtual void FreeStream(Device dev, TVMStreamHandle stream);
148 
154  virtual void StreamSync(Device dev, TVMStreamHandle stream) = 0;
160  virtual void SetStream(Device dev, TVMStreamHandle stream) {}
173  virtual void SyncStreamFromTo(Device dev, TVMStreamHandle event_src, TVMStreamHandle event_dst);
190  virtual void* AllocWorkspace(Device dev, size_t nbytes, DLDataType type_hint = {});
197  virtual void FreeWorkspace(Device dev, void* ptr);
198 
205  static DeviceAPI* Get(Device dev, bool allow_missing = false);
206 
212  static bool NeedSetDevice(int device_type) {
213  return device_type != kDLCPU && device_type != kDLMicroDev;
214  }
215 
216  protected:
230  virtual void CopyDataFromTo(const void* from, size_t from_offset, void* to, size_t to_offset,
231  size_t num_bytes, Device dev_from, Device dev_to,
232  DLDataType type_hint, TVMStreamHandle stream);
233 };
234 
236 constexpr int kRPCSessMask = 128;
237 
243 inline const char* DeviceName(int type) {
244  switch (type) {
245  case kDLCPU:
246  return "cpu";
247  case kDLCUDA:
248  return "cuda";
249  case kDLCUDAHost:
250  return "cuda_host";
251  case kDLOpenCL:
252  return "opencl";
253  case kDLSDAccel:
254  return "sdaccel";
255  case kDLAOCL:
256  return "aocl";
257  case kDLVulkan:
258  return "vulkan";
259  case kDLMetal:
260  return "metal";
261  case kDLVPI:
262  return "vpi";
263  case kDLROCM:
264  return "rocm";
265  case kDLExtDev:
266  return "ext_dev";
267  case kDLWebGPU:
268  return "webgpu";
269  case kDLHexagon:
270  return "hexagon";
271  default:
272  LOG(FATAL) << "unknown type =" << type;
273  return "Unknown";
274  }
275 }
276 
280 inline bool IsRPCSessionDevice(Device dev) { return (dev.device_type / kRPCSessMask) > 0; }
281 
286 inline int GetRPCSessionIndex(Device dev) {
287  ICHECK(IsRPCSessionDevice(dev)) << "GetRPCSessionIndex: dev has no RPC session";
288  return dev.device_type / kRPCSessMask - 1;
289 }
290 
299  dev.device_type = static_cast<DLDeviceType>(dev.device_type % kRPCSessMask);
300  return dev;
301 }
302 
303 inline std::ostream& operator<<(std::ostream& os, DLDevice dev) { // NOLINT(*)
305  os << "remote[" << tvm::runtime::GetRPCSessionIndex(dev) << "]-";
307  }
308  os << tvm::runtime::DeviceName(static_cast<int>(dev.device_type)) << "(" << dev.device_id << ")";
309  return os;
310 }
311 
319 inline Device AddRPCSessionMask(Device dev, int session_table_index) {
320  CHECK(!IsRPCSessionDevice(dev)) << "AddRPCSessionMask: dev already non-zero RPCSessionIndex: "
321  << dev;
322  dev.device_type =
323  static_cast<DLDeviceType>(dev.device_type | (kRPCSessMask * (session_table_index + 1)));
324  return dev;
325 }
326 
327 } // namespace runtime
328 } // namespace tvm
329 
330 #endif // TVM_RUNTIME_DEVICE_API_H_
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:69
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
constexpr const char * device_type
The device type.
Definition: stmt.h:1357
Definition: device_api.h:50
constexpr int kDefaultWorkspaceAlignment
Number of bytes each allocation must align to by default in the workspace buffer to service intermedi...
Definition: device_api.h:65
Definition: c_runtime_api.h:86
constexpr int kMaxStackAlloca
Maximum size that can be allocated on stack.
Definition: device_api.h:61
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
TVM Runtime Device API, abstracts the device specific interface for memory management.
Definition: device_api.h:71
Definition: c_runtime_api.h:85
Device AddRPCSessionMask(Device dev, int session_table_index)
Add a RPC session mask to a Device. RPC clients typically do this when decoding a Device received fro...
Definition: device_api.h:319
int GetRPCSessionIndex(Device dev)
Return the RPCSessTable index of the RPC Session that owns this device.
Definition: device_api.h:286
virtual void GetTargetProperty(Device dev, const std::string &property, TVMRetValue *rv)
Query the device for specified properties.
Definition: device_api.h:95
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition: c_runtime_api.h:172
constexpr int kRPCSessMask
The device type bigger than this is RPC device.
Definition: device_api.h:236
Definition: c_runtime_api.h:90
virtual ~DeviceAPI()
virtual destructor
Definition: device_api.h:74
bool IsRPCSessionDevice(Device dev)
Return true if a Device is owned by an RPC session.
Definition: device_api.h:280
Definition: c_runtime_api.h:89
constexpr int kTempAllocaAlignment
Number of bytes each allocation must align to in temporary allocation.
Definition: device_api.h:58
static bool NeedSetDevice(int device_type)
Whether a certian device type requires set device device before launching the kernel function...
Definition: device_api.h:212
Device RemoveRPCSessionMask(Device dev)
Remove the RPC session mask from a Device. RPC clients typically do this when encoding a Device for t...
Definition: device_api.h:298
A device-independent managed NDArray abstraction.
Definition: device_api.h:39
Definition: c_runtime_api.h:88
Definition: device_api.h:42
Definition: device_api.h:45
Definition: device_api.h:51
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:1758
DLDevice Device
Definition: ndarray.h:43
Definition: device_api.h:41
Definition: device_api.h:49
const char * DeviceName(int type)
The name of Device API factory.
Definition: device_api.h:243
Definition: device_api.h:47
Definition: device_api.h:40
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition: device_api.h:55
DeviceAttrKind
the query type into GetAttr
Definition: device_api.h:38
Definition: device_api.h:44
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
Definition: device_api.h:46
Definition: device_api.h:48
virtual void SetStream(Device dev, TVMStreamHandle stream)
Set the stream.
Definition: device_api.h:160
Type-erased function used across TVM API.
Definition: device_api.h:43