Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/dstack/_internal/core/backends/runpod/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 22 additions & 1 deletion src/dstack/_internal/core/backends/runpod/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"])
Expand Down
31 changes: 31 additions & 0 deletions src/tests/_internal/core/backends/runpod/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Loading