Repository navigation
Expand file tree
/
Copy pathdevice.h
More file actions
137 lines (127 loc) · 5.59 KB
/
Copy pathdevice.h
File metadata and controls
137 lines (127 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// vllm.cpp original (vt runtime, inventory deviation §9.1); no upstream mirror.
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
namespace vt {
// Queue identities are process-unique and monotonic. A native stream handle is
// not an identity by itself: CUDA's per-device default streams are all null and
// destroyed streams may later reuse the same handle value.
uint64_t NextQueueId() noexcept;
// Open device enum (.agents/backends.md): reserved entries for platforms we
// have not implemented yet keep engine-visible types backend-agnostic.
enum class DeviceType : uint8_t {
kCPU = 0,
kCUDA = 1,
kMETAL = 2,
kVULKAN = 3,
kXPU = 4,
kROCM = 5,
// Tenstorrent Blackhole (P100/P150 PCIe cards). Named after the vendor/stack
// (kROCM precedent), not the chip family: this codebase's CUDA layer already
// uses "Blackwell" (sm_120/121, GB10) pervasively, and kBLACKHOLE next to
// those would be a near-miss for both humans and grep.
// (.agents/specs/tenstorrent-backend.md, BACKEND-TENSTORRENT)
kTENSTORRENT = 6
};
constexpr size_t kNumDeviceTypes = 7;
// The canonical lowercase spelling of a device, for user-facing messages (and
// the docs that quote them). Lives here, beside the enum, rather than in the
// shared vllm layer: it names every platform EQUALLY, so it is a data list like
// the platform priority walk, not a device-specific branch — and keeping it in
// vt means adding a platform touches one enum and one switch, both in this file.
constexpr const char* DeviceTypeName(DeviceType device) {
switch (device) {
case DeviceType::kCPU:
return "cpu";
case DeviceType::kCUDA:
return "cuda";
case DeviceType::kMETAL:
return "metal";
case DeviceType::kVULKAN:
return "vulkan";
case DeviceType::kXPU:
return "xpu";
case DeviceType::kROCM:
// Upstream `vllm/platforms/rocm.py` sets `device_name = "rocm"` while its
// torch-facing `device_type` stays "cuda" (rocm.py:447-449) because ROCm
// reuses the CUDA dispatch key. We have no torch, so only the honest name
// survives here; the HIP-reuses-CUDA-spelling question does not arise.
return "rocm";
case DeviceType::kTENSTORRENT:
return "tenstorrent";
}
return "unknown";
}
// The inverse of `DeviceTypeName`: resolves a lowercase spelling back to its
// enum, returning false when nothing matches. It lives here, beside the forward
// direction, for the same reason that one does — it names every platform
// EQUALLY, so it is a data list rather than a device-specific branch, and adding
// a platform still touches one enum and this one file.
//
// It exists because the shared `vllm` layer has to honour a device NAME an
// operator typed (`VLLM_CPP_VOCODER_DEVICE`, #672) WITHOUT either spelling a
// device enumerator or casting an integer into one. Both are exactly what
// `scripts/check-device-leakage.py` counts, and it is right to: a cast hardcodes
// a device by ENUM VALUE, never writes the token, and silently re-points if the
// enum is ever reordered. Enumerating the list HERE keeps that hazard inside the
// seam that owns the enum, and the static_assert makes adding a platform without
// listing it a build error rather than a silent gap.
// Every canonical device spelling, space-separated, for a refusal message that
// has to name what IS legal. It lives here for the same reason its two
// neighbours do: enumerating `DeviceType` anywhere else is what
// `scripts/check-device-leakage.py` counts, and it is right to — a caller that
// walked the values with `static_cast<DeviceType>(i)` would hardcode devices by
// enum VALUE, never write the token, and silently re-point if the enum were
// reordered. Keeping the walk inside the file that owns the enum keeps that
// hazard in one place, and the static_assert makes adding a platform without
// listing it a build error.
inline std::string DeviceTypeNameList() {
constexpr DeviceType kAll[] = {DeviceType::kCPU, DeviceType::kCUDA, DeviceType::kMETAL,
DeviceType::kVULKAN, DeviceType::kXPU, DeviceType::kROCM,
DeviceType::kTENSTORRENT};
static_assert(sizeof(kAll) / sizeof(kAll[0]) == kNumDeviceTypes,
"DeviceTypeNameList must list every DeviceType");
std::string out;
for (const DeviceType device : kAll) {
if (!out.empty()) out += " ";
out += DeviceTypeName(device);
}
return out;
}
inline bool DeviceTypeFromName(const char* name, DeviceType* out) {
if (name == nullptr || out == nullptr) return false;
constexpr DeviceType kAll[] = {DeviceType::kCPU, DeviceType::kCUDA, DeviceType::kMETAL,
DeviceType::kVULKAN, DeviceType::kXPU, DeviceType::kROCM,
DeviceType::kTENSTORRENT};
static_assert(sizeof(kAll) / sizeof(kAll[0]) == kNumDeviceTypes,
"DeviceTypeFromName must list every DeviceType");
for (const DeviceType device : kAll) {
const char* a = name;
const char* b = DeviceTypeName(device);
while (*a != '\0' && *a == *b) {
++a;
++b;
}
if (*a == '\0' && *b == '\0') {
*out = device;
return true;
}
}
return false;
}
struct Device {
DeviceType type = DeviceType::kCPU;
int32_t index = 0;
friend bool operator==(const Device& a, const Device& b) {
return a.type == b.type && a.index == b.index;
}
};
// Per-device execution queue (CUDA stream / Metal command queue / SYCL queue).
// CPU uses handle == nullptr. Ops never assume a global stream.
struct Queue {
Device device;
void* handle = nullptr;
uint64_t id = NextQueueId();
};
} // namespace vt