Skip to content

Commit 05994b9

Browse files
RomneyDaclaude
andcommitted
fix(inference): join NVML and DXGI on PCI identity, not adapter name
Shared GPU memory is discovered through DXGI and attached to the NVML device it belongs to by matching adapter *name*. The two APIs do not always report the same name for the same physical adapter, and when they disagree the match silently fails, SharedGpuMemoryBytes stays null, and the adapter is assessed on its dedicated pool alone. On a unified-memory part that is the difference between working and not. Measured on a DGX Spark (GB10) engineering unit: NVML name : 'JMJWOA-Generic-GPU' shared: null DXGI name : 'RTX Spark GPU' SharedSystemMemory: 30.07 GiB => no match, 30.07 GiB discarded The adapter is then judged to have 15.94 GiB instead of 46.01 GiB, and every model in the catalog fails eligibility with InsufficientGpuMemory: Qwen3.6 35B-A3B required 34.11 GiB detected 15.94 GiB Unsupported Qwen3.6 27B required 39.93 GiB detected 15.94 GiB Unsupported Qwen3.5 9B required 21.47 GiB detected 15.94 GiB Unsupported There is no diagnostic pointing at the cause: the failure reads as "this machine is too small", not "the join failed". DXGI already reads VendorId, DeviceId and SubSystemId in DXGI_ADAPTER_DESC1 and discards them. NVML exposes the same identity via nvmlDeviceGetPciInfo_v3, which packs pciDeviceId as (deviceId << 16) | vendorId. This joins on that identity and keeps the name match as a fallback, so behaviour is unchanged wherever the names already agree or PCI identity is unavailable. The identity match requires exactly one candidate, so an ambiguous set still attributes nothing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01BE8kMbmSjPCwd8i9GJEsqx
1 parent ff3fb35 commit 05994b9

2 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎src/OpenClaw.Shared/Inference/DxgiGpuMemoryProbe.cs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,21 @@ private static void AddNvidiaAdapterMemory(
8686

8787
long? sharedMemoryBytes = ToInt64(description.SharedSystemMemory);
8888
long? freeSharedMemoryBytes = QueryFreeSharedMemory(adapter);
89+
90+
// NVML packs PCI identity as (deviceId << 16) | vendorId. Carry the same
91+
// shape so the two probes can be joined on adapter identity rather than on
92+
// a display name, which the two APIs do not always agree on.
93+
uint pciDeviceId = (description.DeviceId << 16) | (description.VendorId & 0xFFFF);
94+
8995
AddMemoryByName(
9096
results,
9197
ambiguousNames,
9298
description.Description,
93-
new DxgiGpuMemoryInfo(sharedMemoryBytes, freeSharedMemoryBytes));
99+
new DxgiGpuMemoryInfo(
100+
sharedMemoryBytes,
101+
freeSharedMemoryBytes,
102+
pciDeviceId,
103+
description.SubSystemId));
94104
}
95105

96106
internal static void AddMemoryByName(
@@ -209,4 +219,6 @@ private struct DxgiVideoMemoryInfo
209219

210220
internal sealed record DxgiGpuMemoryInfo(
211221
long? SharedMemoryBytes,
212-
long? FreeSharedMemoryBytes);
222+
long? FreeSharedMemoryBytes,
223+
uint? PciDeviceId = null,
224+
uint? PciSubSystemId = null);

‎src/OpenClaw.Shared/Inference/NvmlHostHardwareProbe.cs‎

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,14 @@ public HostHardwareInfo Probe()
8888
{
8989
string name = device.Name.Trim();
9090
string normalizedName = NormalizeGpuName(name);
91-
DxgiGpuMemoryInfo? dxgiMemory = nvmlNameCounts[normalizedName] == 1
91+
92+
// Prefer PCI identity. NVML and DXGI can report different display
93+
// names for the same adapter, and on a unified-memory part that costs
94+
// the entire shared pool: the adapter looks like a small dedicated
95+
// card instead. Fall back to the name match when either side cannot
96+
// supply identity.
97+
DxgiGpuMemoryInfo? dxgiMemory = FindDxgiMemoryByPciIdentity(dxgiMemoryByName, device);
98+
dxgiMemory ??= nvmlNameCounts[normalizedName] == 1
9299
? FindDxgiMemoryByName(dxgiMemoryByName, name)
93100
: null;
94101
return new GpuInfo(
@@ -115,6 +122,27 @@ public HostHardwareInfo Probe()
115122
private static string NormalizeGpuName(string value) =>
116123
string.Join(' ', value.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries));
117124

125+
/// <summary>
126+
/// Match an NVML device to a DXGI adapter on PCI identity. Returns null unless
127+
/// exactly one adapter matches, so an ambiguous set never attributes another
128+
/// adapter's shared memory to this device.
129+
/// </summary>
130+
private static DxgiGpuMemoryInfo? FindDxgiMemoryByPciIdentity(
131+
IReadOnlyDictionary<string, DxgiGpuMemoryInfo> dxgiMemoryByName,
132+
NvmlGpuSnapshot device)
133+
{
134+
if (device.PciDeviceId is not { } pciDeviceId)
135+
return null;
136+
137+
DxgiGpuMemoryInfo[] matches = dxgiMemoryByName.Values
138+
.Where(entry =>
139+
entry.PciDeviceId == pciDeviceId &&
140+
entry.PciSubSystemId == device.PciSubSystemId)
141+
.ToArray();
142+
143+
return matches.Length == 1 ? matches[0] : null;
144+
}
145+
118146
private static DxgiGpuMemoryInfo? FindDxgiMemoryByName(
119147
IReadOnlyDictionary<string, DxgiGpuMemoryInfo> dxgiMemoryByName,
120148
string nvmlName)
@@ -181,6 +209,18 @@ private static NvmlProbeResult CaptureNvml()
181209
var getDriver = GetDelegate<NvmlSystemGetString>(library, "nvmlSystemGetDriverVersion");
182210
var getCuda = GetDelegate<NvmlSystemGetCudaDriverVersion>(library, "nvmlSystemGetCudaDriverVersion_v2");
183211

212+
// Older NVML builds may not export this. It is optional: without it the
213+
// join falls back to matching on adapter name as before.
214+
NvmlDeviceGetPciInfo? getPciInfo = null;
215+
try
216+
{
217+
getPciInfo = GetDelegate<NvmlDeviceGetPciInfo>(library, "nvmlDeviceGetPciInfo_v3");
218+
}
219+
catch (EntryPointNotFoundException)
220+
{
221+
getPciInfo = null;
222+
}
223+
184224
if (initialize() != NvmlSuccess)
185225
return NvmlProbeResult.Empty;
186226
initialized = true;
@@ -207,7 +247,27 @@ private static NvmlProbeResult CaptureNvml()
207247
if (string.IsNullOrWhiteSpace(name))
208248
continue;
209249
string? uuid = ReadDeviceString(device, getUuid, DeviceUuidCapacity);
210-
devices.Add(new NvmlGpuSnapshot(name, uuid, memory.Total, memory.Free));
250+
251+
// PCI identity is optional. It is the join key against DXGI, which
252+
// reports the same vendor, device, and subsystem ids for the adapter
253+
// but can report a different display name for it.
254+
uint? pciDeviceId = null;
255+
uint? pciSubSystemId = null;
256+
if (getPciInfo is not null &&
257+
getPciInfo(device, out NvmlPciInfo pciInfo) == NvmlSuccess &&
258+
pciInfo.PciDeviceId != 0)
259+
{
260+
pciDeviceId = pciInfo.PciDeviceId;
261+
pciSubSystemId = pciInfo.PciSubSystemId;
262+
}
263+
264+
devices.Add(new NvmlGpuSnapshot(
265+
name,
266+
uuid,
267+
memory.Total,
268+
memory.Free,
269+
pciDeviceId,
270+
pciSubSystemId));
211271
}
212272

213273
return new NvmlProbeResult(devices, driverVersion, cudaMajorVersion);
@@ -311,13 +371,39 @@ private struct NvmlMemory
311371

312372
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
313373
private delegate int NvmlDeviceGetMemoryInfo(IntPtr device, out NvmlMemory memory);
374+
375+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
376+
private delegate int NvmlDeviceGetPciInfo(IntPtr device, out NvmlPciInfo pciInfo);
377+
378+
/// <summary>
379+
/// nvmlPciInfo_v3_t. Only the PCI identity fields are consumed; the bus id
380+
/// buffers are declared so the struct layout matches what NVML writes.
381+
/// </summary>
382+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
383+
private struct NvmlPciInfo
384+
{
385+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
386+
public byte[] BusIdLegacy;
387+
public uint Domain;
388+
public uint Bus;
389+
public uint Device;
390+
391+
/// <summary>Packed as (deviceId &lt;&lt; 16) | vendorId.</summary>
392+
public uint PciDeviceId;
393+
public uint PciSubSystemId;
394+
395+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
396+
public byte[] BusId;
397+
}
314398
}
315399

316400
internal sealed record NvmlGpuSnapshot(
317401
string Name,
318402
string? Uuid,
319403
ulong TotalMemoryBytes,
320-
ulong FreeMemoryBytes);
404+
ulong FreeMemoryBytes,
405+
uint? PciDeviceId = null,
406+
uint? PciSubSystemId = null);
321407

322408
internal sealed record NvmlProbeResult(
323409
IReadOnlyList<NvmlGpuSnapshot> Devices,

0 commit comments

Comments
 (0)