tvm
Loading...
Searching...
No Matches
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
27#include <tvm/ffi/any.h>
28#include <tvm/ffi/container/tensor.h>
29#include <tvm/ffi/device.h>
30#include <tvm/ffi/error.h>
31#include <tvm/ffi/optional.h>
32#include <tvm/ffi/string.h>
33#include <tvm/runtime/base.h>
34
35#include <string>
40typedef void* TVMStreamHandle;
41
42namespace tvm {
43
44// alias DLDevice
45using Device = DLDevice;
46
47namespace runtime {
48
72#ifdef __cplusplus
73typedef enum : int32_t {
74#else
75typedef enum {
76#endif
77 // To help avoid accidental conflicts between `DLDeviceType`
78 // and this enumeration, start numbering the new enumerators
79 // a little higher than (currently) seems necessary.
80 TVMDeviceExtType_End = 36, // sentinel value
82
83#ifdef TVM_KALLOC_ALIGNMENT
85constexpr int kAllocAlignment = TVM_KALLOC_ALIGNMENT;
86
88constexpr int kTempAllocaAlignment = TVM_KALLOC_ALIGNMENT;
89#else
91constexpr int kAllocAlignment = 64;
92
94constexpr int kTempAllocaAlignment = 64;
95#endif // TVM_KALLOC_ALIGNMENT
96
98constexpr int kMaxStackAlloca = 1024;
99
103
109 public:
111 virtual ~DeviceAPI() {}
116 virtual void SetDevice(Device dev) = 0;
128 virtual int GetDeviceIndex();
136 virtual ffi::Any GetAttr(DLDevice dev, const ffi::String& name) = 0;
137
146 virtual ffi::Tensor Empty(ffi::ShapeView shape, DLDataType dtype, DLDevice device,
147 ffi::Optional<ffi::String> mem_scope = std::nullopt) = 0;
148
159 virtual void CopyDataFromTo(DLTensor* from, DLTensor* to, TVMStreamHandle stream);
166
173 virtual void FreeStream(Device dev, TVMStreamHandle stream);
174
180 virtual void StreamSync(Device dev, TVMStreamHandle stream) = 0;
186 virtual void SetStream(Device dev, TVMStreamHandle stream);
205 virtual void SyncStreamFromTo(Device dev, TVMStreamHandle event_src, TVMStreamHandle event_dst);
222 virtual void* AllocWorkspace(Device dev, size_t nbytes, DLDataType type_hint = {});
229 virtual void FreeWorkspace(Device dev, void* ptr);
230
237 static DeviceAPI* Get(Device dev, bool allow_missing = false);
238
244 static bool NeedSetDevice(int device_type) { return device_type != kDLCPU; }
245
249 virtual bool SupportsDevicePointerArithmeticsOnHost() { return false; }
250
251 protected:
265 virtual void CopyDataFromTo(const void* from, size_t from_offset, void* to, size_t to_offset,
266 size_t num_bytes, Device dev_from, Device dev_to,
267 DLDataType type_hint, TVMStreamHandle stream);
268};
269
275inline const char* DLDeviceType2Str(int type) {
276 switch (type) {
277 case kDLCPU:
278 return "cpu";
279 case kDLCUDA:
280 return "cuda";
281 case kDLCUDAHost:
282 return "cuda_host";
283 case kDLCUDAManaged:
284 return "cuda_managed";
285 case kDLOpenCL:
286 return "opencl";
287 case kDLVulkan:
288 return "vulkan";
289 case kDLMetal:
290 return "metal";
291 case kDLVPI:
292 return "vpi";
293 case kDLROCM:
294 return "rocm";
295 case kDLROCMHost:
296 return "rocm_host";
297 case kDLExtDev:
298 return "ext_dev";
299 case kDLOneAPI:
300 return "oneapi";
301 case kDLWebGPU:
302 return "webgpu";
303 case kDLHexagon:
304 return "hexagon";
305 case kDLTrn:
306 return "trn";
307 default:
308 TVM_FFI_THROW(InternalError) << "unknown type = " << type;
309 }
310 throw;
311}
312
313inline std::ostream& operator<<(std::ostream& os, DLDevice dev) { // NOLINT(*)
314 os << DLDeviceType2Str(static_cast<int>(dev.device_type)) << ":" << dev.device_id;
315 return os;
316}
317
323TVM_RUNTIME_DLL bool RuntimeEnabled(const ffi::String& target);
324
325} // namespace runtime
326} // namespace tvm
327
328#endif // TVM_RUNTIME_DEVICE_API_H_
TVM Runtime Device API, abstracts the device specific interface for memory management.
Definition device_api.h:108
static bool NeedSetDevice(int device_type)
Whether a certian device type requires set device device before launching the kernel function.
Definition device_api.h:244
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 bool SupportsDevicePointerArithmeticsOnHost()
Whether pointer arithmetics on a device owned pointer may be performed on the host.
Definition device_api.h:249
virtual void * AllocWorkspace(Device dev, size_t nbytes, DLDataType type_hint={})
Allocate temporal workspace for backend execution.
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 TVMStreamHandle GetCurrentStream(Device dev)
Get the current stream.
virtual ffi::Tensor Empty(ffi::ShapeView shape, DLDataType dtype, DLDevice device, ffi::Optional< ffi::String > mem_scope=std::nullopt)=0
Create an owning, compact tensor with uninitialized contents.
virtual void StreamSync(Device dev, TVMStreamHandle stream)=0
Synchronize the stream.
virtual ffi::Any GetAttr(DLDevice dev, const ffi::String &name)=0
Query an attribute of a 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.
virtual int GetDeviceIndex()
Get the currently selected runtime device index on the calling host thread.
virtual ~DeviceAPI()
virtual destructor
Definition device_api.h:111
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition device_api.h:40
TVMDeviceExtType
Extension device types in TVM.
Definition device_api.h:75
@ TVMDeviceExtType_End
Definition device_api.h:80
constexpr int kMaxStackAlloca
Maximum size that can be allocated on stack.
Definition device_api.h:98
constexpr int kDefaultWorkspaceAlignment
Number of bytes each allocation must align to by default in the workspace buffer to service intermedi...
Definition device_api.h:102
const char * DLDeviceType2Str(int type)
The name of DLDeviceType.
Definition device_api.h:275
TVM_RUNTIME_DLL bool RuntimeEnabled(const ffi::String &target)
Check if runtime module is enabled for target.
constexpr int kTempAllocaAlignment
Number of bytes each allocation must align to in temporary allocation.
Definition device_api.h:94
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition device_api.h:91
std::ostream & operator<<(std::ostream &os, DLDevice dev)
Definition device_api.h:313
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
DLDevice Device
Definition device_api.h:45
#define TVM_RUNTIME_DLL
Definition base.h:100