Repository navigation
Expand file tree
/
Copy pathrocm.cpp
More file actions
288 lines (271 loc) · 16.7 KB
/
Copy pathrocm.cpp
File metadata and controls
288 lines (271 loc) · 16.7 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// ROCm leg of the Platform seam (BACKEND-ROCM, W0 skeleton). Self-registers
// kROCM via a static Registrar, copying the src/vllm/platforms/cuda.cpp /
// metal.cpp / vulkan.cpp registrar idiom. Compiled only in a HIP build (CMake
// target_sources gate).
//
// COMPILE-VERIFIED, unlike the .hip TUs of this skeleton — this one is plain C++
// with no HIP header anywhere in it, reaching the device only through the vt::
// seam (vt::GetBackend + the two probes in include/vt/rocm/rocm_runtime.h). That
// is the same discipline vulkan.cpp follows, it is what keeps src/vllm/ free of
// vendor headers, and it is what lets a machine with no ROCm compile this file
// at all: CMakeLists.txt builds it as a never-linked OBJECT library in NON-HIP
// builds precisely so a later change to the Platform interface cannot break it
// silently for a contributor mid-bring-up. It has never RUN — that needs a GPU.
//
// UPSTREAM MIRROR: vllm/platforms/rocm.py @ pin 555967922 (`class RocmPlatform`,
// rocm.py:444). Every value below either cites the upstream line it mirrors or
// says outright that it is a W0 placeholder — there is no third category, and no
// value here is a guess dressed as a decision.
#include <cstddef>
#include <string>
#include <string_view>
#include <vector>
#include "vllm/platforms/interface.h"
#include "vt/backend.h"
#include "vt/rocm/rocm_attn_wmma_arch.h"
#include "vt/rocm/rocm_runtime.h"
namespace vllm::platforms {
namespace {
class RocmPlatform final : public Platform {
public:
// Issue #1934. `device_memory_total_bytes` is `vt::rocm::DeviceMemoryTotalBytes(0)`,
// probed once by the registrar below at static init, exactly mirroring how
// `CudaPlatform` threads its own `cudaMemGetInfo` probe through its
// constructor (`platforms/cuda.cpp`). 0 means the probe failed or no device
// is present; `residency_policy()` passes it through unexamined, and
// `gguf_device_fit.h` already reads 0 as UNKNOWN rather than "nothing fits".
explicit RocmPlatform(size_t device_memory_total_bytes)
: device_memory_total_bytes_(device_memory_total_bytes) {}
DeviceType device_type() const override { return DeviceType::kROCM; }
Backend& backend() const override { return vt::GetBackend(DeviceType::kROCM); }
// rocm.py:481-497 get_device_capability -> (major, minor) parsed from
// gcnArchName by _capability_from_gcn_arch (rocm.py:223). Our backend already
// did that parse at registration through the SAME ported function
// (include/vt/rocm/rocm_arch.h), so both layers answer from one source rather
// than parsing twice and risking two answers — the seam-gap #4 lesson the CUDA
// leg records at cuda_backend.cu:64-71.
DeviceCapability get_device_capability() const override {
Backend& b = backend();
return DeviceCapability{b.DeviceCapabilityMajor(), b.DeviceCapabilityMinor()};
}
// interface.py:181-187 supported_dtypes order (bf16 default first). All three
// are real on every gfx9/gfx11/gfx12 part vLLM supports; bf16 in particular is
// native on RDNA3, which is what all three boards on issue #41 are.
std::vector<DType> supported_dtypes() const override {
return {DType::kBF16, DType::kF16, DType::kF32};
}
// rocm.py:663 is_integrated_gpu. Answered from the backend's probed
// UnifiedMemory(), which is the STRICTER conjunction
// (integrated AND pageable-memory-access, see rocm_backend.hip). So a false
// here can mean "integrated but no pageable access" — conservative in the
// direction that matters, since every consumer of this predicate is asking
// whether it may treat host and device memory as one.
bool is_integrated_gpu() const override { return backend().UnifiedMemory(); }
// ENG-EXPERT-STREAM-DEVICE W0b (issue #1124). Answered from the two device
// attributes `ProbeDevice` already reads, through the HIP-free probe rather
// than through `UnifiedMemory()`: that one is widened by the managed-alloc
// branch, which makes the BACKEND's allocations migratable and says nothing
// about whether a kernel may follow a plain host pointer. So this is NARROWER
// than `is_integrated_gpu()` above, in the one direction that matters.
//
// Device 0, matching this leg's other single-device answers. NEVER RUN — no
// ROCm hardware is reachable from this project — which is why it reads the
// probe rather than hardcoding an answer for a board nobody here can check.
bool host_memory_is_device_addressable() const override {
return vt::rocm::HostMemoryIsDeviceAddressable(0);
}
bool support_static_graph_for_model(const std::vector<std::string>& architectures,
int device_index) const override {
return architectures == std::vector<std::string>{"Gemma3ForCausalLM"} &&
vt::rocm::GcnArchNameHasGemmaDecodeWmma(vt::rocm::DeviceArchName(device_index));
}
// --- W0 placeholders. Each is the BASE class answer, stated explicitly here
// --- with what would change it, so nobody has to guess whether the omission
// --- was considered.
//
// supports_fp8() stays false: gfx942/gfx950 have hardware fp8 and rocm.py lists
// "fp8" in supported_quantization (rocm.py:457-467), but we have no ROCm fp8
// kernel, and this predicate gates a fused path that would then not exist.
// GFX1100-TG200 (T2b): support_static_graph_mode() is now TRUE. The W1 note
// below recorded the two conditions for this flip: the vt::Backend hipGraph
// capture seam is implemented (rocm_backend.hip; BeginCapture/EndCaptureGraph/
// ReplayGraph mirror cuda_backend.cu call for call, and the mutate-src-then-
// replay test asserts replay never returns a snapshot), and a real model's
// decode-graph path had to be exercised. The Qwen3_5 dense decode driver
// (Qwen3_5DenseDecodeGraph) gates on this predicate plus SupportsGraphCapture()
// plus VLLM_CPP_CUDAGRAPH; with all three true it captures the uniform decode
// step per padded batch size and replays it. The keep-quant scratch pool is
// already capture-safe (hipMallocAsync, stream-ordered, never freed).
// The flip is PER-ARCH OPT-IN: static_graph_requires_opt_in() below returns
// false only on gfx1100 (the evidence arch) so the decode-graph gate engages
// there and stays eager everywhere else. See
// .agents/specs/gfx1100-tg200-t2b-static-graph-opt-in.md.
bool support_static_graph_mode() const override { return true; }
// GFX1100-TG200 (T2b): per-arch opt-in, mirroring #2910's Tenstorrent pattern.
// The decode-graph gate in qwen3_5_dense.cpp is:
// support_static_graph_mode() && !static_graph_requires_opt_in()
// so returning false here admits capture and returning true keeps it eager.
// gfx1100 (RX 7900 XTX, RDNA3) is the evidence arch — its captured arm has
// committed A/B evidence (docs/bench-evidence/gfx1100-tg200-t2b-20260823.md).
// Every other ROCm arch (gfx1151, gfx1103, gfx1200, gfx1201) returns true
// until it carries its own captured-arm evidence. The arch is read through
// the HIP-free vt::rocm::DeviceArchName(0) probe (same prefix-match
// discipline GcnArchNameIsGfx12PrefillWmma uses in rocm_arch.h); an empty
// string — no device present — does not match and degrades to opt-in (eager),
// the conservative answer.
bool static_graph_requires_opt_in() const override {
// Explicit escape hatch: an operator on a non-evidence arch can opt in
// deliberately. Without it the opt-in was impossible to satisfy off
// gfx1100 — the gate was eager everywhere else with no override.
if (const char* e = std::getenv("VLLM_CPP_ROCM_STATIC_GRAPH")) {
return !(e[0] == '1' && e[1] == '\0');
}
const std::string arch = vt::rocm::DeviceArchName(0);
return !IsGfx1100(arch);
}
// Architecture-aware overload: ROCm's evidence dimension is the GPU arch, not
// the model family (unlike Tenstorrent, whose DecodeCaptureDefaultArch
// scopes by model architecture). So this delegates to the no-arg overload.
// A future arch that needs per-model scoping can override this independently.
bool static_graph_requires_opt_in(
const std::vector<std::string>& /*architectures*/) const override {
return static_graph_requires_opt_in();
}
// needs_weight_staging() stays false: this is the memory-model POLICY that
// selects the FULLY-OPTIMIZED device-resident forward (indexed GDN state
// I/O with no op-registration fallback for a couple of its consumers,
// merged/packed GDN projections, fp8/bf16 GDN resident prep) over the
// host-resident reference path for THOSE specific kernels. Issue #1934
// measured that several of those consumers have no per-op fallback and
// would silently assume kernels this device might not register, so
// flipping this blindly was rejected — see that issue and
// `allocates_bounded_device_memory()` below, which answers the NARROWER
// question the load-time device-fit check actually needs without moving
// any of this flag's other consumers. `IndexedGdnStateIoEnabled` already
// takes ROCm's fast arm today regardless of this flag, by checking op
// registration directly rather than trusting this policy bit — the same
// move #1934 makes for the device-fit check.
// supports_fa2_attention() / opaque_attention_op() stay false: no ROCm
// attention kernel exists yet. See get_attn_backend_priority below.
// BACKEND-ROCM, issue #1934. The ONE narrow question the load-time
// device-fit refusal (`gguf_device_fit.h`, issue #1123) needs answered:
// does a load here allocate device memory `ResidentWeight` cannot exceed
// unnoticed? Yes, unconditionally, on any non-CPU platform since issue
// #125's fix (see the interface doc on this method) — independent of
// `needs_weight_staging()`, which this method deliberately does not touch.
bool allocates_bounded_device_memory() const override { return true; }
// The residency/memory-model policy. `device_memory_total_bytes` is now a
// REAL probe (issue #1934, mirrors `CudaPlatform`'s own `cudaMemGetInfo`
// probe) — the ONE field the device-fit check reads. The other two fields
// stay DEFAULT/false, unlike CUDA's: `release_host_weights_after_upload`
// and `uses_device_memory_pool` are separate policy questions (a discrete
// card's host-copy release and DevicePool reuse) this row does not touch.
//
// THE REASON THIS COMMENT USED TO GIVE IS FALSE, AND SAYING SO IS THE POINT.
// It read: "on a unified part (780M, Strix Halo) freeing the host copy after
// 'upload' would free the ONLY copy — the same answer CPU, Metal and Vulkan
// give for the same reason". #2511 falsified that premise. gfx1151 reports
// `pageableMemoryAccess = 0`, so `HostMemoryIsDeviceAddressable` answers
// false (`vt/rocm/rocm_backend.hip`, and `host_memory_is_device_addressable`
// above), `ResidentWeight`'s host-alias arm is not taken, and its staging
// branch always makes a real second copy on the device. The host copy has not
// been the only copy on this part since that change landed. Left uncorrected,
// that sentence is what kept 65.488 GiB of spent GGUF source pages resident
// on a 31 GiB host until the load wedged in `svm_range_set_attr`
// (.agents/specs/rocm-host-residency-after-upload.md).
//
// THE FLAG NONETHELESS STAYS FALSE, and the reason is that flipping it WOULD
// change behaviour on this board, not that it would change none.
//
// A first draft of this comment said the flag had two readers,
// `ShouldReleaseHostWeights` and `ShouldInterleaveLoadStream`
// (`platforms/interface.h:88-96`), that both also require `marlin_committed`,
// which no ROCm path sets, and that flipping it here was therefore inert.
// That is false and a fresh review measured it. There is a THIRD reader and it
// requires no `marlin_committed`: `DirectDeviceLoadEligible`
// (`qwen3_5_dense_weights.cpp:146-180`) returns
// `platform.residency_policy().release_host_weights_after_upload` as its last
// term, and it is what gates `StageAndReleaseLoadedDense` at
// `qwen3_5_dense_weights.cpp:1205-1212` for every Qwen3.5-dense safetensors
// load. `needs_weight_staging()` is true on ROCm, so flipping this bit would
// arm a whole staging-and-release load path on gfx1151 that nobody has
// measured there. Correcting a false comment and installing a new one is the
// failure this file has now produced twice; the enumeration above is the whole
// of it, from `grep -rn release_host_weights_after_upload src include tests`.
//
// The host-residency release that the defect needed is gated instead on the
// property that is load-bearing and checkable at the call site — the device
// cannot dereference host memory and the source is a re-faultable read-only
// file mapping — in `MaybeReleaseStagedBorrowSource` (qwen3_5_weights.h),
// called from BOTH staging arms (`qwen3_5.cpp`'s `ResidentWeight` and
// `dense_attn_block.h`'s). Flip these two when a board's release/pool
// behavior is actually measured, not as a side effect of making the budget
// check reachable.
ResidencyPolicy residency_policy() const override {
ResidencyPolicy p;
p.device_memory_total_bytes = device_memory_total_bytes_;
return p;
}
// Attention-backend priority — M3 (issue #41). Mirrors rocm.py:407-441
// `_get_backend_priorities` at pin 555967922. The dense branch is
// [ROCM_ATTN, (ROCM_AITER_FA if `rocm_aiter_ops.is_mha_enabled()`, :434),
// (ROCM_AITER_UNIFIED_ATTN if `is_aiter_found_and_supported()`, :436),
// TRITON_ATTN, TURBOQUANT] — mirrored VERBATIM below. Upstream appends
// ROCM_ATTN only `if not use_kv_connector` (:432-433); our registration uses
// the shared symmetric NHD layout (see the KV-LAYOUT DEVIATION record in
// backend.h), which is immune to the asymmetric-view concern that guard
// protects, so the guard does not apply here — recorded, not implied.
// SelectAttentionBackendName skips unregistered names, so the AITER entries
// and TRITON_ATTN/TURBOQUANT are placeholders that cost nothing and need no
// gfx9 reasoning: a dense request resolves to the first REGISTERED name,
// ROCM_ATTN. MLA mirrors rocm.py:414-419 ([TRITON_MLA] when AITER MLA is
// off); TRITON_MLA is unregistered for kROCM (registered for CUDA only), so
// a use_mla=true request throws loudly rather than silently falling back to
// a dense backend — the honest answer until a ROCm MLA kernel lands.
// use_sparse mirrors rocm.py:410 ([ROCM_AITER_MLA_SPARSE], AITER-only) and
// is likewise unregistered.
std::vector<std::string> get_attn_backend_priority(
const AttnSelectorConfig& cfg) const override {
if (cfg.use_sparse) return {"ROCM_AITER_MLA_SPARSE"};
if (cfg.use_mla) return {"TRITON_MLA"};
return {"ROCM_ATTN", "ROCM_AITER_FA", "ROCM_AITER_UNIFIED_ATTN",
"TRITON_ATTN", "TURBOQUANT"};
}
private:
// True iff `arch` is the gfx1100 gcnArchName prefix (e.g. "gfx1100" or
// "gfx1100:sramecc+:xnack-"). Prefix, not substring: after the six-char stem
// the next character must be end-of-string or a non-digit, so "gfx11000" does
// not match. Same discipline as GcnArchNameIsGfx12PrefillWmma (rocm_arch.h).
static bool IsGfx1100(std::string_view arch) {
constexpr std::string_view kStem = "gfx1100";
if (arch.size() < kStem.size()) return false;
if (arch.substr(0, kStem.size()) != kStem) return false;
if (arch.size() == kStem.size()) return true;
const char c = arch[kStem.size()];
return c < '0' || c > '9';
}
size_t device_memory_total_bytes_ = 0;
};
// Registers kROCM during static init (registration completes before main() per
// the interface.h contract). Probes the DEVICE itself rather than trusting
// another TU's initializer, because static-init order across TUs is unspecified
// — asking "did the backend registrar run?" would intermittently skip platform
// registration. Registering a platform whose backend() would throw is worse than
// not registering: CurrentPlatform() (src/vllm/platforms/platform.cpp:38-40)
// walks the device list and must be able to fall through to CPU on a machine
// that merely happens to have been built with -DVLLM_CPP_HIP=ON.
struct Registrar {
Registrar() noexcept {
if (!vt::rocm::DeviceAvailable()) return;
// Issue #1934. Device 0, matching this leg's other single-device probes
// (`host_memory_is_device_addressable()` above states the same choice).
// HIP-free free function, not `Backend::DeviceMemoryInfo`: the backend's
// OWN registrar (`rocm_backend.hip`) may not have run yet at this point —
// static-init order across TUs is unspecified, the same reason this
// registrar probes the device itself rather than trusting one.
static RocmPlatform platform(vt::rocm::DeviceMemoryTotalBytes(0));
RegisterPlatform(DeviceType::kROCM, &platform);
}
} registrar;
} // namespace
} // namespace vllm::platforms