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>
35
36#include <string>
41typedef void* TVMStreamHandle;
42
43namespace tvm {
44
45// alias DLDevice
46using Device = DLDevice;
47
48namespace runtime {
49
73#ifdef __cplusplus
74typedef enum : int32_t {
75#else
76typedef enum {
77#endif
78 // To help avoid accidental conflicts between `DLDeviceType`
79 // and this enumeration, start numbering the new enumerators
80 // a little higher than (currently) seems necessary.
81 TVMDeviceExtType_End = 36, // sentinel value
83
84#ifdef TVM_KALLOC_ALIGNMENT
86constexpr int kAllocAlignment = TVM_KALLOC_ALIGNMENT;
87
89constexpr int kTempAllocaAlignment = TVM_KALLOC_ALIGNMENT;
90#else
92constexpr int kAllocAlignment = 64;
93
95constexpr int kTempAllocaAlignment = 64;
96#endif // TVM_KALLOC_ALIGNMENT
97
99constexpr int kMaxStackAlloca = 1024;
100
106 public:
108 virtual ~DeviceAPI() {}
113 virtual void SetDevice(Device dev) = 0;
125 virtual int GetDeviceIndex();
133 virtual ffi::Any GetAttr(DLDevice dev, const ffi::String& name) = 0;
134
143 virtual ffi::Tensor Empty(ffi::ShapeView shape, DLDataType dtype, DLDevice device,
144 ffi::Optional<MemoryScope> mem_scope = std::nullopt) = 0;
145
156 virtual void CopyDataFromTo(DLTensor* from, DLTensor* to, TVMStreamHandle stream);
163
170 virtual void FreeStream(Device dev, TVMStreamHandle stream);
171
177 virtual void StreamSync(Device dev, TVMStreamHandle stream) = 0;
183 virtual void SetStream(Device dev, TVMStreamHandle stream);
202 virtual void SyncStreamFromTo(Device dev, TVMStreamHandle event_src, TVMStreamHandle event_dst);
219 virtual void* AllocWorkspace(Device dev, size_t nbytes, DLDataType type_hint = {});
226 virtual void FreeWorkspace(Device dev, void* ptr);
227
234 static DeviceAPI* Get(Device dev, bool allow_missing = false);
235
241 static bool NeedSetDevice(int device_type) { return device_type != kDLCPU; }
242
246 virtual bool SupportsDevicePtrArithmeticOnHost() { return false; }
247
258 virtual void* GetMappedHostPtrIfAvailable(void* data) { return nullptr; }
259
260 protected:
274 virtual void CopyDataFromTo(const void* from, size_t from_offset, void* to, size_t to_offset,
275 size_t num_bytes, Device dev_from, Device dev_to,
276 DLDataType type_hint, TVMStreamHandle stream);
277};
278
284inline const char* DLDeviceType2Str(int type) {
285 switch (type) {
286 case kDLCPU:
287 return "cpu";
288 case kDLCUDA:
289 return "cuda";
290 case kDLCUDAHost:
291 return "cuda_host";
292 case kDLCUDAManaged:
293 return "cuda_managed";
294 case kDLOpenCL:
295 return "opencl";
296 case kDLVulkan:
297 return "vulkan";
298 case kDLMetal:
299 return "metal";
300 case kDLVPI:
301 return "vpi";
302 case kDLROCM:
303 return "rocm";
304 case kDLROCMHost:
305 return "rocm_host";
306 case kDLExtDev:
307 return "ext_dev";
308 case kDLOneAPI:
309 return "oneapi";
310 case kDLWebGPU:
311 return "webgpu";
312 case kDLHexagon:
313 return "hexagon";
314 case kDLTrn:
315 return "trn";
316 default:
317 TVM_FFI_THROW(InternalError) << "unknown type = " << type;
318 }
319 throw;
320}
321
322inline std::ostream& operator<<(std::ostream& os, DLDevice dev) { // NOLINT(*)
323 os << DLDeviceType2Str(static_cast<int>(dev.device_type)) << ":" << dev.device_id;
324 return os;
325}
326
332TVM_RUNTIME_DLL bool RuntimeEnabled(const ffi::String& target);
333
334} // namespace runtime
335} // namespace tvm
336
337#endif // TVM_RUNTIME_DEVICE_API_H_
#define TVM_RUNTIME_DLL
Definition base.h:73
TVM Runtime Device API, abstracts the device specific interface for memory management.
Definition device_api.h:105
static bool NeedSetDevice(int device_type)
Whether a certain device type requires setting the device before launching the kernel function.
Definition device_api.h:241
virtual bool SupportsDevicePtrArithmeticOnHost()
Whether pointer arithmetics on a device owned pointer may be performed on the host.
Definition device_api.h:246
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 * GetMappedHostPtrIfAvailable(void *data)
Query an existing host mapping of a device allocation, if available.
Definition device_api.h:258
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 * 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< MemoryScope > 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:108
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition device_api.h:41
Memory scopes and allocation properties for runtime storage.
TVMDeviceExtType
Extension device types in TVM.
Definition device_api.h:76
@ TVMDeviceExtType_End
Definition device_api.h:81
constexpr int kMaxStackAlloca
Maximum size that can be allocated on stack.
Definition device_api.h:99
const char * DLDeviceType2Str(int type)
The name of DLDeviceType.
Definition device_api.h:284
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:95
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition device_api.h:92
std::ostream & operator<<(std::ostream &os, DLDevice dev)
Definition device_api.h:322
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
DLDevice Device
Definition device_api.h:46