|
| 1 | +import shlex |
| 2 | +import subprocess |
| 3 | +import tempfile |
| 4 | +from threading import Thread |
| 5 | +from typing import List, Optional |
| 6 | + |
| 7 | +import gpuhunt |
| 8 | +from gpuhunt.providers.hotaisle import HotAisleProvider |
| 9 | + |
| 10 | +from dstack._internal.core.backends.base.compute import ( |
| 11 | + Compute, |
| 12 | + ComputeWithCreateInstanceSupport, |
| 13 | + generate_unique_instance_name, |
| 14 | + get_shim_commands, |
| 15 | +) |
| 16 | +from dstack._internal.core.backends.base.offers import get_catalog_offers |
| 17 | +from dstack._internal.core.backends.hotaisle.api_client import HotaisleAPIClient |
| 18 | +from dstack._internal.core.backends.hotaisle.models import HotaisleConfig |
| 19 | +from dstack._internal.core.models.backends.base import BackendType |
| 20 | +from dstack._internal.core.models.instances import ( |
| 21 | + InstanceAvailability, |
| 22 | + InstanceConfiguration, |
| 23 | + InstanceOfferWithAvailability, |
| 24 | +) |
| 25 | +from dstack._internal.core.models.placement import PlacementGroup |
| 26 | +from dstack._internal.core.models.runs import JobProvisioningData, Requirements |
| 27 | +from dstack._internal.utils.logging import get_logger |
| 28 | + |
| 29 | +logger = get_logger(__name__) |
| 30 | + |
| 31 | +MAX_INSTANCE_NAME_LEN = 60 |
| 32 | + |
| 33 | + |
| 34 | +class HotaisleCompute( |
| 35 | + ComputeWithCreateInstanceSupport, |
| 36 | + Compute, |
| 37 | +): |
| 38 | + def __init__(self, config: HotaisleConfig): |
| 39 | + super().__init__() |
| 40 | + self.config = config |
| 41 | + self.api_client = HotaisleAPIClient(config.creds.api_key, config.team_handle) |
| 42 | + self.catalog = gpuhunt.Catalog(balance_resources=False, auto_reload=False) |
| 43 | + self.catalog.add_provider( |
| 44 | + HotAisleProvider(api_key=config.creds.api_key, team_handle=config.team_handle) |
| 45 | + ) |
| 46 | + |
| 47 | + def get_offers( |
| 48 | + self, requirements: Optional[Requirements] = None |
| 49 | + ) -> List[InstanceOfferWithAvailability]: |
| 50 | + offers = get_catalog_offers( |
| 51 | + backend=BackendType.HOTAISLE, |
| 52 | + locations=self.config.regions or None, |
| 53 | + requirements=requirements, |
| 54 | + catalog=self.catalog, |
| 55 | + ) |
| 56 | + offers = [ |
| 57 | + InstanceOfferWithAvailability( |
| 58 | + **offer.dict(), availability=InstanceAvailability.AVAILABLE |
| 59 | + ) |
| 60 | + for offer in offers |
| 61 | + ] |
| 62 | + return offers |
| 63 | + |
| 64 | + def get_payload_from_offer(self, instance_type) -> dict: |
| 65 | + # Only two instance types are available in Hotaisle with CPUs: 8-core and 13-core. Other fields are |
| 66 | + # not configurable. |
| 67 | + cpu_cores = instance_type.resources.cpus |
| 68 | + if cpu_cores == 8: |
| 69 | + cpu_model = "Xeon Platinum 8462Y+" |
| 70 | + frequency = 2800000000 |
| 71 | + else: # cpu_cores == 13 |
| 72 | + cpu_model = "Xeon Platinum 8470" |
| 73 | + frequency = 2000000000 |
| 74 | + |
| 75 | + return { |
| 76 | + "cpu_cores": cpu_cores, |
| 77 | + "cpus": { |
| 78 | + "count": 1, |
| 79 | + "manufacturer": "Intel", |
| 80 | + "model": cpu_model, |
| 81 | + "cores": cpu_cores, |
| 82 | + "frequency": frequency, |
| 83 | + }, |
| 84 | + "disk_capacity": 13194139533312, |
| 85 | + "ram_capacity": 240518168576, |
| 86 | + "gpus": [ |
| 87 | + { |
| 88 | + "count": len(instance_type.resources.gpus), |
| 89 | + "manufacturer": "AMD", |
| 90 | + "model": "MI300X", |
| 91 | + } |
| 92 | + ], |
| 93 | + } |
| 94 | + |
| 95 | + def create_instance( |
| 96 | + self, |
| 97 | + instance_offer: InstanceOfferWithAvailability, |
| 98 | + instance_config: InstanceConfiguration, |
| 99 | + placement_group: Optional[PlacementGroup], |
| 100 | + ) -> JobProvisioningData: |
| 101 | + instance_name = generate_unique_instance_name( |
| 102 | + instance_config, max_length=MAX_INSTANCE_NAME_LEN |
| 103 | + ) |
| 104 | + project_ssh_key = instance_config.ssh_keys[0] |
| 105 | + self.api_client.upload_ssh_key(project_ssh_key.public) |
| 106 | + vm_payload = self.get_payload_from_offer(instance_offer.instance) |
| 107 | + vm_data = self.api_client.create_virtual_machine(vm_payload, instance_name) |
| 108 | + return JobProvisioningData( |
| 109 | + backend=instance_offer.backend, |
| 110 | + instance_type=instance_offer.instance, |
| 111 | + instance_id=vm_data["name"], |
| 112 | + hostname=None, |
| 113 | + internal_ip=None, |
| 114 | + region=instance_offer.region, |
| 115 | + price=instance_offer.price, |
| 116 | + username="hotaisle", |
| 117 | + ssh_port=22, |
| 118 | + dockerized=True, |
| 119 | + ssh_proxy=None, |
| 120 | + backend_data=vm_data["ip_address"], |
| 121 | + ) |
| 122 | + |
| 123 | + def update_provisioning_data( |
| 124 | + self, |
| 125 | + provisioning_data: JobProvisioningData, |
| 126 | + project_ssh_public_key: str, |
| 127 | + project_ssh_private_key: str, |
| 128 | + ): |
| 129 | + vm_state = self.api_client.get_vm_state(provisioning_data.instance_id) |
| 130 | + if vm_state == "running": |
| 131 | + if provisioning_data.hostname is None and provisioning_data.backend_data: |
| 132 | + provisioning_data.hostname = provisioning_data.backend_data |
| 133 | + commands = get_shim_commands( |
| 134 | + authorized_keys=[project_ssh_public_key], |
| 135 | + arch=provisioning_data.instance_type.resources.cpu_arch, |
| 136 | + ) |
| 137 | + launch_command = "sudo sh -c " + shlex.quote(" && ".join(commands)) |
| 138 | + thread = Thread( |
| 139 | + target=_start_runner, |
| 140 | + kwargs={ |
| 141 | + "hostname": provisioning_data.hostname, |
| 142 | + "project_ssh_private_key": project_ssh_private_key, |
| 143 | + "launch_command": launch_command, |
| 144 | + }, |
| 145 | + daemon=True, |
| 146 | + ) |
| 147 | + thread.start() |
| 148 | + |
| 149 | + def terminate_instance( |
| 150 | + self, instance_id: str, region: str, backend_data: Optional[str] = None |
| 151 | + ): |
| 152 | + vm_name = instance_id |
| 153 | + self.api_client.terminate_virtual_machine(vm_name) |
| 154 | + |
| 155 | + |
| 156 | +def _start_runner( |
| 157 | + hostname: str, |
| 158 | + project_ssh_private_key: str, |
| 159 | + launch_command: str, |
| 160 | +): |
| 161 | + _setup_instance( |
| 162 | + hostname=hostname, |
| 163 | + ssh_private_key=project_ssh_private_key, |
| 164 | + ) |
| 165 | + _launch_runner( |
| 166 | + hostname=hostname, |
| 167 | + ssh_private_key=project_ssh_private_key, |
| 168 | + launch_command=launch_command, |
| 169 | + ) |
| 170 | + |
| 171 | + |
| 172 | +def _setup_instance( |
| 173 | + hostname: str, |
| 174 | + ssh_private_key: str, |
| 175 | +): |
| 176 | + setup_commands = ("sudo apt-get update",) |
| 177 | + _run_ssh_command( |
| 178 | + hostname=hostname, ssh_private_key=ssh_private_key, command=" && ".join(setup_commands) |
| 179 | + ) |
| 180 | + |
| 181 | + |
| 182 | +def _launch_runner( |
| 183 | + hostname: str, |
| 184 | + ssh_private_key: str, |
| 185 | + launch_command: str, |
| 186 | +): |
| 187 | + _run_ssh_command( |
| 188 | + hostname=hostname, |
| 189 | + ssh_private_key=ssh_private_key, |
| 190 | + command=launch_command, |
| 191 | + ) |
| 192 | + |
| 193 | + |
| 194 | +def _run_ssh_command(hostname: str, ssh_private_key: str, command: str): |
| 195 | + with tempfile.NamedTemporaryFile("w+", 0o600) as f: |
| 196 | + f.write(ssh_private_key) |
| 197 | + f.flush() |
| 198 | + subprocess.run( |
| 199 | + [ |
| 200 | + "ssh", |
| 201 | + "-F", |
| 202 | + "none", |
| 203 | + "-o", |
| 204 | + "StrictHostKeyChecking=no", |
| 205 | + "-i", |
| 206 | + f.name, |
| 207 | + f"hotaisle@{hostname}", |
| 208 | + command, |
| 209 | + ], |
| 210 | + stdout=subprocess.DEVNULL, |
| 211 | + stderr=subprocess.DEVNULL, |
| 212 | + ) |
0 commit comments