tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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,
51  kDriverVersion = 12
52 };
53 
54 #ifdef TVM_KALLOC_ALIGNMENT
56 constexpr int kAllocAlignment = TVM_KALLOC_ALIGNMENT;
57 
59 constexpr int kTempAllocaAlignment = TVM_KALLOC_ALIGNMENT;
60 #else
62 constexpr int kAllocAlignment = 64;
63 
65 constexpr int kTempAllocaAlignment = 64;
66 #endif // TVM_KALLOC_ALIGNMENT
67 
69 constexpr int kMaxStackAlloca = 1024;
70 
73 constexpr int kDefaultWorkspaceAlignment = 1;
74 
79 class TVM_DLL DeviceAPI {
80  public:
82  virtual ~DeviceAPI() {}
87  virtual void SetDevice(Device dev) = 0;
95  virtual void GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue* rv) = 0;
96 
103  virtual void GetTargetProperty(Device dev, const std::string& property, TVMRetValue* rv) {}
104 
114  virtual void* AllocDataSpace(Device dev, size_t nbytes, size_t alignment,
115  DLDataType type_hint) = 0;
125  virtual void* AllocDataSpace(Device dev, int ndim, const int64_t* shape, DLDataType dtype,
126  Optional<String> mem_scope = NullOpt);
132  virtual void FreeDataSpace(Device dev, void* ptr) = 0;
141  virtual void CopyDataFromTo(DLTensor* from, DLTensor* to, TVMStreamHandle stream);
148 
155  virtual void FreeStream(Device dev, TVMStreamHandle stream);
156 
162  virtual void StreamSync(Device dev, TVMStreamHandle stream) = 0;
168  virtual void SetStream(Device dev, TVMStreamHandle stream) {}
181  virtual void SyncStreamFromTo(Device dev, TVMStreamHandle event_src, TVMStreamHandle event_dst);
198  virtual void* AllocWorkspace(Device dev, size_t nbytes, DLDataType type_hint = {});
205  virtual void FreeWorkspace(Device dev, void* ptr);
206 
213  static DeviceAPI* Get(Device dev, bool allow_missing = false);
214 
220  static bool NeedSetDevice(int device_type) {
221  return device_type != kDLCPU && device_type != kDLMicroDev;
222  }
223 
224  protected:
238  virtual void CopyDataFromTo(const void* from, size_t from_offset, void* to, size_t to_offset,
239  size_t num_bytes, Device dev_from, Device dev_to,
240  DLDataType type_hint, TVMStreamHandle stream);
241 };
242 
244 constexpr int kRPCSessMask = 128;
245 static_assert(kRPCSessMask >= TVMDeviceExtType_End);
246 
252 inline const char* DeviceName(int type) {
253  switch (type) {
254  case kDLCPU:
255  return "cpu";
256  case kDLCUDA:
257  return "cuda";
258  case kDLCUDAHost:
259  return "cuda_host";
260  case kDLCUDAManaged:
261  return "cuda_managed";
262  case kDLOpenCL:
263  return "opencl";
264  case kDLSDAccel:
265  return "sdaccel";
266  case kDLAOCL:
267  return "aocl";
268  case kDLVulkan:
269  return "vulkan";
270  case kDLMetal:
271  return "metal";
272  case kDLVPI:
273  return "vpi";
274  case kDLROCM:
275  return "rocm";
276  case kDLROCMHost:
277  return "rocm_host";
278  case kDLExtDev:
279  return "ext_dev";
280  case kDLOneAPI:
281  return "oneapi";
282  case kDLWebGPU:
283  return "webgpu";
284  case kDLHexagon:
285  return "hexagon";
286  case kOpenGL:
287  return "opengl";
288  case kDLMicroDev:
289  return "microdev";
290  default:
291  LOG(FATAL) << "unknown type =" << type;
292  }
293 }
294 
298 inline bool IsRPCSessionDevice(Device dev) { return (dev.device_type / kRPCSessMask) > 0; }
299 
304 inline int GetRPCSessionIndex(Device dev) {
305  ICHECK(IsRPCSessionDevice(dev)) << "GetRPCSessionIndex: dev has no RPC session";
306  return dev.device_type / kRPCSessMask - 1;
307 }
308 
317  dev.device_type = static_cast<DLDeviceType>(dev.device_type % kRPCSessMask);
318  return dev;
319 }
320 
321 inline std::ostream& operator<<(std::ostream& os, DLDevice dev) { // NOLINT(*)
323  os << "remote[" << tvm::runtime::GetRPCSessionIndex(dev) << "]-";
325  }
326  os << tvm::runtime::DeviceName(static_cast<int>(dev.device_type)) << "(" << dev.device_id << ")";
327  return os;
328 }
329 
337 inline Device AddRPCSessionMask(Device dev, int session_table_index) {
338  CHECK(!IsRPCSessionDevice(dev)) << "AddRPCSessionMask: dev already non-zero RPCSessionIndex: "
339  << dev;
340  dev.device_type =
341  static_cast<DLDeviceType>(dev.device_type | (kRPCSessMask * (session_table_index + 1)));
342  return dev;
343 }
344 
345 } // namespace runtime
346 } // namespace tvm
347 
348 #endif // TVM_RUNTIME_DEVICE_API_H_
@ kDLMicroDev
Definition: c_runtime_api.h:124
@ TVMDeviceExtType_End
Definition: c_runtime_api.h:125
@ kOpenGL
Definition: c_runtime_api.h:123
@ kDLSDAccel
Definition: c_runtime_api.h:122
@ kDLAOCL
Definition: c_runtime_api.h:121
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition: c_runtime_api.h:236
TVM Runtime Device API, abstracts the device specific interface for memory management.
Definition: device_api.h:79
static bool NeedSetDevice(int device_type)
Whether a certian device type requires set device device before launching the kernel function.
Definition: device_api.h:220
static DeviceAPI * Get(Device dev, bool allow_missing=false)
Get device API based on device.
virtual void CopyDataFromTo(DLTensor *from, DLTensor *to, TVMStreamHandle stream)
copy data from one place to another
virtual TVMStreamHandle CreateStream(Device dev)
Create a new stream of execution.
virtual void SyncStreamFromTo(Device dev, TVMStreamHandle event_src, TVMStreamHandle event_dst)
Synchronize 2 streams of execution.
virtual void FreeWorkspace(Device dev, void *ptr)
Free temporal workspace in backend execution.
virtual void * AllocDataSpace(Device dev, int ndim, const int64_t *shape, DLDataType dtype, Optional< String > mem_scope=NullOpt)
Allocate a data space on device with memory scope support.
virtual void SetDevice(Device dev)=0
Set the environment device id to device.
virtual void FreeStream(Device dev, TVMStreamHandle stream)
Free a stream of execution.
virtual void GetTargetProperty(Device dev, const std::string &property, TVMRetValue *rv)
Query the device for specified properties.
Definition: device_api.h:103
virtual void * AllocWorkspace(Device dev, size_t nbytes, DLDataType type_hint={})
Allocate temporal workspace for backend execution.
virtual void GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue *rv)=0
Get attribute of specified device.
virtual void StreamSync(Device dev, TVMStreamHandle stream)=0
Synchronize the stream.
virtual void FreeDataSpace(Device dev, void *ptr)=0
Free a data space on device.
virtual void CopyDataFromTo(const void *from, size_t from_offset, void *to, size_t to_offset, size_t num_bytes, Device dev_from, Device dev_to, DLDataType type_hint, TVMStreamHandle stream)
copy data from one place to another
virtual void SetStream(Device dev, TVMStreamHandle stream)
Set the stream.
Definition: device_api.h:168
virtual void * AllocDataSpace(Device dev, size_t nbytes, size_t alignment, DLDataType type_hint)=0
Allocate a data space on device.
virtual ~DeviceAPI()
virtual destructor
Definition: device_api.h:82
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
constexpr int kMaxStackAlloca
Maximum size that can be allocated on stack.
Definition: device_api.h:69
DeviceAttrKind
the query type into GetAttr
Definition: device_api.h:38
@ kDeviceName
Definition: device_api.h:44
@ kDriverVersion
Definition: device_api.h:51
@ kMaxThreadsPerBlock
Definition: device_api.h:40
@ kMultiProcessorCount
Definition: device_api.h:46
@ kMaxThreadDimensions
Definition: device_api.h:47
@ kApiVersion
Definition: device_api.h:50
@ kMaxClockRate
Definition: device_api.h:45
@ kWarpSize
Definition: device_api.h:41
@ kMaxRegistersPerBlock
Definition: device_api.h:48
@ kComputeVersion
Definition: device_api.h:43
@ kGcnArch
Definition: device_api.h:49
@ kMaxSharedMemoryPerBlock
Definition: device_api.h:42
@ kExist
Definition: device_api.h:39
constexpr int kRPCSessMask
The device type bigger than this is RPC device.
Definition: device_api.h:244
constexpr int kDefaultWorkspaceAlignment
Number of bytes each allocation must align to by default in the workspace buffer to service intermedi...
Definition: device_api.h:73
int GetRPCSessionIndex(Device dev)
Return the RPCSessTable index of the RPC Session that owns this device.
Definition: device_api.h:304
constexpr int kTempAllocaAlignment
Number of bytes each allocation must align to in temporary allocation.
Definition: device_api.h:65
const char * DeviceName(int type)
The name of Device API factory.
Definition: device_api.h:252
bool IsRPCSessionDevice(Device dev)
Return true if a Device is owned by an RPC session.
Definition: device_api.h:298
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:337
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition: device_api.h:62
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
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:316
constexpr const char * device_type
The device type.
Definition: stmt.h:1418
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:1763
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
DLDevice Device
Definition: ndarray.h:43
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
A device-independent managed NDArray abstraction.
Type-erased function used across TVM API.