Skip to content

Commit

Permalink
Adjust Spacing With Pre-Commit for Cudo
Browse files Browse the repository at this point in the history
  • Loading branch information
Bihan Rana authored and Bihan Rana committed Feb 26, 2024
1 parent 8042c24 commit 90eed26
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
28 changes: 19 additions & 9 deletions src/gpuhunt/providers/cudo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging
from collections import namedtuple
from concurrent.futures import ThreadPoolExecutor, as_completed
from itertools import chain
from typing import Optional, List
from typing import List, Optional

from cudo_compute import cudo_api

from gpuhunt import QueryFilter, RawCatalogItem
from gpuhunt._internal.constraints import KNOWN_GPUS
from gpuhunt.providers import AbstractProvider
import logging

CpuMemoryGpu = namedtuple("CpuMemoryGpu", ["cpu", "memory", "gpu"])
logger = logging.getLogger(__name__)
Expand All @@ -16,22 +18,26 @@ class CudoProvider(AbstractProvider):
NAME = "cudo"

def get(
self, query_filter: Optional[QueryFilter] = None, balance_resources: bool = True
self, query_filter: Optional[QueryFilter] = None, balance_resources: bool = True
) -> List[RawCatalogItem]:
offers = self.fetch_all_vm_types()
return sorted(offers, key=lambda i: i.price)

def fetch_all_vm_types(self):
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(self.fetch_vm_type, cmg.cpu, cmg.memory, cmg.gpu) for cmg in GPU_MACHINES]
futures = [
executor.submit(self.fetch_vm_type, cmg.cpu, cmg.memory, cmg.gpu)
for cmg in GPU_MACHINES
]
results = []
for future in as_completed(futures):
try:
result = future.result()
results.append(result)
except Exception as e:
logger.info(
f"Unable to find VM type with vCPU: {e.vcpu}, Memory: {e.memory_gib} GiB, GPU: {e.gpu}.")
f"Unable to find VM type with vCPU: {e.vcpu}, Memory: {e.memory_gib} GiB, GPU: {e.gpu}."
)
return list(chain.from_iterable(results))

def get_raw_catalog_list(self, vm_machine_type_list, vcpu, memory, gpu):
Expand All @@ -41,20 +47,24 @@ def get_raw_catalog_list(self, vm_machine_type_list, vcpu, memory, gpu):
instance_name=vm.machine_type,
location=vm.data_center_id,
spot=False,
price=round((float(vm.total_price_hr.value) + float(vm.storage_gib_price_hr.value)), 5),
price=round(
(float(vm.total_price_hr.value) + float(vm.storage_gib_price_hr.value)), 5
),
cpu=vcpu,
memory=memory,
gpu_count=gpu,
gpu_name=gpu_name(vm.gpu_model),
gpu_memory=get_memory(gpu_name(vm.gpu_model)),
disk_size=None
disk_size=None,
)
raw_list.append(raw)
return raw_list

def fetch_vm_type(self, vcpu, memory_gib, gpu):
try:
result = cudo_api.virtual_machines().list_vm_machine_types(vcpu=vcpu, memory_gib=memory_gib, gpu=gpu)
result = cudo_api.virtual_machines().list_vm_machine_types(
vcpu=vcpu, memory_gib=memory_gib, gpu=gpu
)
return self.get_raw_catalog_list(result, vcpu, memory_gib, gpu)
except Exception as e:
raise VMTypeFetchError(f"Failed to fetch VM type: {e}", vcpu, memory_gib, gpu)
Expand Down Expand Up @@ -462,5 +472,5 @@ def get_memory(gpu_name: str) -> Optional[int]:
CpuMemoryGpu(128, 128, 8),
CpuMemoryGpu(128, 192, 8),
CpuMemoryGpu(128, 256, 8),
CpuMemoryGpu(128, 384, 8)
CpuMemoryGpu(128, 384, 8),
]
10 changes: 9 additions & 1 deletion src/integrity_tests/test_cudo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ def select_row(rows, name: str) -> List[str]:

def test_locations(data_rows):
expected = set(
("no-luster-1", "se-smedjebacken-1", "gb-london-1", "se-stockholm-1", "us-newyork-1", "us-santaclara-1",))
(
"no-luster-1",
"se-smedjebacken-1",
"gb-london-1",
"se-stockholm-1",
"us-newyork-1",
"us-santaclara-1",
)
)
locations = select_row(data_rows, "location")
assert set(locations) == expected

Expand Down

0 comments on commit 90eed26

Please sign in to comment.