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/device.h>
29#include <tvm/ffi/error.h>
30#include <tvm/ffi/optional.h>
31#include <tvm/ffi/string.h>
32#include <tvm/runtime/base.h>
33
34#include <string>
39typedef void* TVMStreamHandle;
40
41namespace tvm {
42
43// alias DLDevice
45
46namespace runtime {
47
71#ifdef __cplusplus
72typedef enum : int32_t {
73#else
74typedef enum {
75#endif
76 // To help avoid accidental conflicts between `DLDeviceType`
77 // and this enumeration, start numbering the new enumerators
78 // a little higher than (currently) seems necessary.
79 TVMDeviceExtType_End = 36, // sentinel value
81
104
105#ifdef TVM_KALLOC_ALIGNMENT
108
111#else
113constexpr int kAllocAlignment = 64;
114
116constexpr int kTempAllocaAlignment = 64;
117#endif // TVM_KALLOC_ALIGNMENT
118
120constexpr int kMaxStackAlloca = 1024;
121
125
131 public:
133 virtual ~DeviceAPI() {}
138 virtual void SetDevice(Device dev) = 0;
146 virtual void GetAttr(Device dev, DeviceAttrKind kind, ffi::Any* rv) = 0;
147
154 virtual size_t GetDataSize(const DLTensor& arr,
155 ffi::Optional<ffi::String> mem_scope = std::nullopt);
156
163 virtual void GetTargetProperty(Device dev, const std::string& property, ffi::Any* rv) {}
164
174 virtual void* AllocDataSpace(Device dev, size_t nbytes, size_t alignment,
185 virtual void* AllocDataSpace(Device dev, int ndim, const int64_t* shape, DLDataType dtype,
186 ffi::Optional<ffi::String> mem_scope = std::nullopt);
192 virtual void FreeDataSpace(Device dev, void* ptr) = 0;
210
218
266 virtual void* AllocWorkspace(Device dev, size_t nbytes, DLDataType type_hint = {});
273 virtual void FreeWorkspace(Device dev, void* ptr);
274
281 static DeviceAPI* Get(Device dev, bool allow_missing = false);
282
288 static bool NeedSetDevice(int device_type) { return device_type != kDLCPU; }
289
293 virtual bool SupportsDevicePointerArithmeticsOnHost() { return false; }
294
295 protected:
309 virtual void CopyDataFromTo(const void* from, size_t from_offset, void* to, size_t to_offset,
312};
313
319inline const char* DLDeviceType2Str(int type) {
320 switch (type) {
321 case kDLCPU:
322 return "cpu";
323 case kDLCUDA:
324 return "cuda";
325 case kDLCUDAHost:
326 return "cuda_host";
327 case kDLCUDAManaged:
328 return "cuda_managed";
329 case kDLOpenCL:
330 return "opencl";
331 case kDLVulkan:
332 return "vulkan";
333 case kDLMetal:
334 return "metal";
335 case kDLVPI:
336 return "vpi";
337 case kDLROCM:
338 return "rocm";
339 case kDLROCMHost:
340 return "rocm_host";
341 case kDLExtDev:
342 return "ext_dev";
343 case kDLOneAPI:
344 return "oneapi";
345 case kDLWebGPU:
346 return "webgpu";
347 case kDLHexagon:
348 return "hexagon";
349 case kDLTrn:
350 return "trn";
351 default:
352 TVM_FFI_THROW(InternalError) << "unknown type = " << type;
353 }
354 throw;
355}
356
358constexpr int kRPCSessMask = 128;
359static_assert(kRPCSessMask >= TVMDeviceExtType_End);
360
364inline bool IsRPCSessionDevice(Device dev) { return (dev.device_type / kRPCSessMask) > 0; }
365
371 TVM_FFI_ICHECK(IsRPCSessionDevice(dev)) << "GetRPCSessionIndex: dev has no RPC session";
372 return dev.device_type / kRPCSessMask - 1;
373}
374
383 dev.device_type = static_cast<DLDeviceType>(dev.device_type % kRPCSessMask);
384 return dev;
385}
386
387inline std::ostream& operator<<(std::ostream& os, DLDevice dev) { // NOLINT(*)
389 os << "remote[" << tvm::runtime::GetRPCSessionIndex(dev) << "]-";
391 }
392 os << tvm::runtime::DLDeviceType2Str(static_cast<int>(dev.device_type)) << ":" << dev.device_id;
393 return os;
394}
395
405 << "AddRPCSessionMask: dev already non-zero RPCSessionIndex: " << dev;
406 dev.device_type =
407 static_cast<DLDeviceType>(dev.device_type | (kRPCSessMask * (session_table_index + 1)));
408 return dev;
409}
410
416TVM_RUNTIME_DLL bool RuntimeEnabled(const ffi::String& target);
417
419namespace symbol {
420constexpr const char* tvm_global_barrier_state = "__tvm_global_barrier_state";
422constexpr const char* tvm_set_device = "__tvm_set_device";
423} // namespace symbol
424
425} // namespace runtime
426} // namespace tvm
427
428#endif // TVM_RUNTIME_DEVICE_API_H_
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
TVM Runtime Device API, abstracts the device specific interface for memory management.
Definition device_api.h:130
static bool NeedSetDevice(int device_type)
Whether a certian device type requires set device device before launching the kernel function.
Definition device_api.h:288
virtual void GetTargetProperty(Device dev, const std::string &property, ffi::Any *rv)
Query the device for specified properties.
Definition device_api.h:163
virtual size_t GetDataSize(const DLTensor &arr, ffi::Optional< ffi::String > mem_scope=std::nullopt)
Get the physical memory size required.
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:293
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 void GetAttr(Device dev, DeviceAttrKind kind, ffi::Any *rv)=0
Get attribute of specified device.
virtual void * AllocDataSpace(Device dev, size_t nbytes, size_t alignment, DLDataType type_hint)=0
Allocate a data space on 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.
virtual void * AllocDataSpace(Device dev, int ndim, const int64_t *shape, DLDataType dtype, ffi::Optional< ffi::String > mem_scope=std::nullopt)
Allocate a data space on device with memory scope support.
virtual ~DeviceAPI()
virtual destructor
Definition device_api.h:133
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition device_api.h:39
constexpr const char * tvm_set_device
global function to set device
Definition device_api.h:422
constexpr const char * tvm_global_barrier_state
Definition device_api.h:420
TVMDeviceExtType
Extension device types in TVM.
Definition device_api.h:74
@ TVMDeviceExtType_End
Definition device_api.h:79
constexpr int kMaxStackAlloca
Maximum size that can be allocated on stack.
Definition device_api.h:120
DeviceAttrKind
the query type into GetAttr
Definition device_api.h:85
@ kDeviceName
Definition device_api.h:91
@ kDriverVersion
Definition device_api.h:98
@ kMaxThreadsPerBlock
Definition device_api.h:87
@ kMultiProcessorCount
Definition device_api.h:93
@ kMaxThreadDimensions
Definition device_api.h:94
@ kApiVersion
Definition device_api.h:97
@ kImagePitchAlignment
Definition device_api.h:102
@ kMaxClockRate
Definition device_api.h:92
@ kWarpSize
Definition device_api.h:88
@ kTotalGlobalMemory
Definition device_api.h:100
@ kAvailableGlobalMemory
Definition device_api.h:101
@ kMaxRegistersPerBlock
Definition device_api.h:95
@ kComputeVersion
Definition device_api.h:90
@ kGcnArch
Definition device_api.h:96
@ kMaxSharedMemoryPerBlock
Definition device_api.h:89
@ kExist
Definition device_api.h:86
@ kL2CacheSizeBytes
Definition device_api.h:99
constexpr int kRPCSessMask
The device type bigger than this is RPC device.
Definition device_api.h:358
constexpr int kDefaultWorkspaceAlignment
Number of bytes each allocation must align to by default in the workspace buffer to service intermedi...
Definition device_api.h:124
const char * DLDeviceType2Str(int type)
The name of DLDeviceType.
Definition device_api.h:319
TVM_RUNTIME_DLL bool RuntimeEnabled(const ffi::String &target)
Check if runtime module is enabled for target.
int GetRPCSessionIndex(Device dev)
Return the RPCSessTable index of the RPC Session that owns this device.
Definition device_api.h:370
constexpr int kTempAllocaAlignment
Number of bytes each allocation must align to in temporary allocation.
Definition device_api.h:116
bool IsRPCSessionDevice(Device dev)
Return true if a Device is owned by an RPC session.
Definition device_api.h:364
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:403
constexpr int kAllocAlignment
Number of bytes each allocation must align to.
Definition device_api.h:113
std::ostream & operator<<(std::ostream &os, DLDevice dev)
Definition device_api.h:387
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:382
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
#define TVM_RUNTIME_DLL
Definition base.h:92