From 74885cdd4f8e3fbbe0dbef233ce69d3efa2282fe Mon Sep 17 00:00:00 2001 From: Dmitry Meyer Date: Thu, 26 Nov 2026 15:35:31 +0000 Subject: [PATCH] Report allocated vCPU and RAM of Runpod pods GPU pod offers have the minimum vCPU and RAM of the hosts that fit the requirements, and pod creation uses them as placement filters, so the pod may get more. Take the actual allocation from the create pod response, which is returned after the pod is placed. Assisted-by: Claude Opus 5.5 --- .../core/backends/runpod/api_client.py | 20 ++++++++++++ .../_internal/core/backends/runpod/compute.py | 23 +++++++++++++- .../core/backends/runpod/test_compute.py | 31 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/dstack/_internal/core/backends/runpod/api_client.py b/src/dstack/_internal/core/backends/runpod/api_client.py index eafd3b1d0..3d3bfbad8 100644 --- a/src/dstack/_internal/core/backends/runpod/api_client.py +++ b/src/dstack/_internal/core/backends/runpod/api_client.py @@ -46,6 +46,26 @@ class V2Pod(TypedDict): mounts: "V2Mounts" createdAt: str dataCenterId: Optional[str] + gpu: NotRequired["V2GpuConfig"] + """Present for GPU pods""" + cpu: NotRequired["V2CpuConfig"] + """Present for CPU pods""" + + +class V2GpuConfig(TypedDict): + id: str + count: NotRequired[int] + vcpuCount: float + """Total vCPU count allocated to the pod across all GPUs""" + memory: float + """Total system RAM in GB allocated to the pod across all GPUs""" + + +class V2CpuConfig(TypedDict): + id: str + vcpuCount: int + memory: int + """System RAM in GB allocated to the pod""" class V2Pagination(TypedDict): diff --git a/src/dstack/_internal/core/backends/runpod/compute.py b/src/dstack/_internal/core/backends/runpod/compute.py index a5eeb7cd0..04e267780 100644 --- a/src/dstack/_internal/core/backends/runpod/compute.py +++ b/src/dstack/_internal/core/backends/runpod/compute.py @@ -35,6 +35,7 @@ InstanceAvailability, InstanceConfiguration, InstanceOfferWithAvailability, + InstanceType, ) from dstack._internal.core.models.placement import PlacementGroup from dstack._internal.core.models.resources import Memory, Range @@ -207,7 +208,7 @@ def run_job( return JobProvisioningData( backend=instance_offer.backend, - instance_type=instance_offer.instance, + instance_type=_get_pod_instance_type(pod, instance_offer), instance_id=instance_id, hostname=None, internal_ip=None, @@ -380,6 +381,26 @@ def _get_cpu_type_id(offer: InstanceOfferWithAvailability) -> str: return match.group("flavor_id") +def _get_pod_instance_type(pod: V2Pod, offer: InstanceOfferWithAvailability) -> InstanceType: + """ + Returns the offer's instance type with the vCPU count and RAM allocated to the pod. + A GPU pod may get more than the offer, as the offer has the minimums of the hosts that fit + the requirements, and the pod is placed on any of them. + """ + # The pod is placed synchronously, so the create response reports the allocation + config = pod.get("gpu") or pod.get("cpu") + vcpu_count = config.get("vcpuCount") if config is not None else None + memory_gb = config.get("memory") if config is not None else None + if not vcpu_count or not memory_gb: + # Not expected, but the pod is already created, so it must not fail provisioning + logger.warning("Pod %s does not report allocated vCPU and RAM", pod["id"]) + return offer.instance + instance_type = offer.instance.model_copy(deep=True) + instance_type.resources.cpus = int(vcpu_count) + instance_type.resources.memory_mib = round(memory_gb * 1024) + return instance_type + + def _parse_pod_created_at(pod: V2Pod) -> Optional[datetime]: try: return parse_datetime(pod["createdAt"]) diff --git a/src/tests/_internal/core/backends/runpod/test_compute.py b/src/tests/_internal/core/backends/runpod/test_compute.py index f79cbaef3..8f6e3bc49 100644 --- a/src/tests/_internal/core/backends/runpod/test_compute.py +++ b/src/tests/_internal/core/backends/runpod/test_compute.py @@ -239,6 +239,37 @@ def test_requests_per_gpu_minimums_rounded_down(self): assert kwargs["min_cpu_count_per_gpu"] == 7 assert kwargs["min_memory_in_gb_per_gpu"] == 125 + def test_reports_allocated_vcpu_and_ram(self): + compute, api_client = _compute_with_mocked_api_client() + api_client.create_pod.return_value = { + "id": "pod-1", + "gpu": {"id": "NVIDIA H100 80GB HBM3", "count": 1, "vcpuCount": 28, "memory": 251}, + } + offer = _gpu_offer(gpu_count=1, cpu_count=20, memory_gib=125) + + jpd = _run_job(compute, offer) + + resources = jpd.instance_type.resources + assert (resources.cpus, resources.memory_mib) == (28, 251 * 1024) + assert resources.gpus == offer.instance.resources.gpus + assert resources.disk == offer.instance.resources.disk + assert jpd.instance_type.name == offer.instance.name + assert jpd.price == offer.price + # The offer is left intact + assert (offer.instance.resources.cpus, offer.instance.resources.memory_mib) == ( + 20, + 125 * 1024, + ) + + def test_falls_back_to_offer_if_allocation_is_not_reported(self): + compute, api_client = _compute_with_mocked_api_client() + api_client.create_pod.return_value = {"id": "pod-1"} + offer = _gpu_offer() + + jpd = _run_job(compute, offer) + + assert jpd.instance_type == offer.instance + def test_failed_registry_credentials_cleanup_does_not_fail_provisioning(self): compute, api_client = _compute_with_mocked_api_client() api_client.create_pod.return_value = {"id": "pod-1"}