|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import requests |
| 6 | +from requests import Response |
| 7 | + |
| 8 | +from gpuhunt._internal.constraints import KNOWN_AMD_GPUS |
| 9 | +from gpuhunt._internal.models import AcceleratorVendor, QueryFilter, RawCatalogItem |
| 10 | +from gpuhunt.providers import AbstractProvider |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +API_URL = "https://admin.hotaisle.app/api" |
| 15 | + |
| 16 | + |
| 17 | +class HotAisleProvider(AbstractProvider): |
| 18 | + NAME = "hotaisle" |
| 19 | + |
| 20 | + def __init__(self, api_key: Optional[str] = None, team_handle: Optional[str] = None): |
| 21 | + """Hotaisle requries an API key and team handle to access the API.""" |
| 22 | + self.api_key = api_key or os.getenv("HOTAISLE_API_KEY") |
| 23 | + self.team_handle = team_handle or os.getenv("HOTAISLE_TEAM_HANDLE") |
| 24 | + |
| 25 | + if not self.api_key: |
| 26 | + raise ValueError("Set the HOTAISLE_API_KEY environment variable.") |
| 27 | + if not self.team_handle: |
| 28 | + raise ValueError("Set the HOTAISLE_TEAM_HANDLE environment variable.") |
| 29 | + |
| 30 | + def get( |
| 31 | + self, query_filter: Optional[QueryFilter] = None, balance_resources: bool = True |
| 32 | + ) -> list[RawCatalogItem]: |
| 33 | + offers = self.fetch_offers() |
| 34 | + return sorted(offers, key=lambda i: i.price) |
| 35 | + |
| 36 | + def fetch_offers(self) -> list[RawCatalogItem]: |
| 37 | + """Fetch available virtual machines from HotAisle API. |
| 38 | + See API documentation(https://admin.hotaisle.app/api/docs) |
| 39 | + for details.""" |
| 40 | + url = f"/teams/{self.team_handle}/virtual_machines/available/" |
| 41 | + response = self._make_request("GET", url) |
| 42 | + return convert_response_to_raw_catalog_items(response) |
| 43 | + |
| 44 | + def _make_request(self, method: str, url: str) -> Response: |
| 45 | + full_url = f"{API_URL}{url}" |
| 46 | + headers = { |
| 47 | + "accept": "application/json", |
| 48 | + "Authorization": f"Token {self.api_key}", |
| 49 | + } |
| 50 | + |
| 51 | + response = requests.request(method=method, url=full_url, headers=headers, timeout=30) |
| 52 | + response.raise_for_status() |
| 53 | + return response |
| 54 | + |
| 55 | + |
| 56 | +def get_gpu_memory(gpu_name: str) -> Optional[float]: |
| 57 | + for gpu in KNOWN_AMD_GPUS: |
| 58 | + if gpu.name.upper() == gpu_name.upper(): |
| 59 | + return float(gpu.memory) |
| 60 | + logger.warning(f"Unknown AMD GPU {gpu_name}") |
| 61 | + return None |
| 62 | + |
| 63 | + |
| 64 | +def convert_response_to_raw_catalog_items(response: Response) -> list[RawCatalogItem]: |
| 65 | + data = response.json() |
| 66 | + offers = [] |
| 67 | + for item in data: |
| 68 | + price_in_cents = item["OnDemandPrice"] |
| 69 | + price = float(price_in_cents) / 100 |
| 70 | + specs = item["Specs"] |
| 71 | + cpu_cores = specs["cpu_cores"] |
| 72 | + ram_capacity_bytes = specs["ram_capacity"] |
| 73 | + memory_gb = ram_capacity_bytes / (1024**3) |
| 74 | + disk_capacity_bytes = specs["disk_capacity"] |
| 75 | + disk_gb = disk_capacity_bytes / (1024**3) |
| 76 | + cpus = specs["cpus"] |
| 77 | + cpu_model = cpus["model"] |
| 78 | + gpus = specs["gpus"] |
| 79 | + gpu = gpus[0] |
| 80 | + gpu_count = gpu["count"] |
| 81 | + gpu_name = gpu["model"] |
| 82 | + gpu_vendor = AcceleratorVendor.AMD.value # All GPUs are AMD with HotAisle. |
| 83 | + gpu_memory = get_gpu_memory(gpu_name) |
| 84 | + |
| 85 | + # Create instance name: cpu_model-cores-ram-gpucount-gpu |
| 86 | + instance_name = f"{gpu_count}x {gpu_name} {cpu_cores}x {cpu_model}" |
| 87 | + |
| 88 | + offer = RawCatalogItem( |
| 89 | + instance_name=instance_name, |
| 90 | + location="us-michigan-1", # Hardcoded for now, as HotAisle only has one location. |
| 91 | + price=price, |
| 92 | + cpu=cpu_cores, |
| 93 | + memory=memory_gb, |
| 94 | + gpu_count=gpu_count, |
| 95 | + gpu_name=gpu_name, |
| 96 | + gpu_memory=gpu_memory, |
| 97 | + gpu_vendor=gpu_vendor, |
| 98 | + spot=False, |
| 99 | + disk_size=disk_gb, |
| 100 | + ) |
| 101 | + offers.append(offer) |
| 102 | + |
| 103 | + return offers |
0 commit comments