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
10 changes: 8 additions & 2 deletions src/dstack/_internal/core/backends/base/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ def get_offers_by_requirements(
unallocated_resources: bool,
) -> List[InstanceOfferWithAvailability]:
"""
Returns backend offers with availability matching requirements.
Returns backend offers with availability for requirements.

The offers are matched against `requirements` with `OfferMatcher` afterwards, so this
method may use `requirements` only to narrow down or shape the offers, e.g. to query
the provider or to size offers.

See `Compute.get_offers()` for the `full_offers` argument description.
Set the class variable `full_offers_argument_has_effect` to `True` if the `full_offers`
Expand All @@ -388,7 +392,9 @@ def get_offers_by_requirements(
def get_offers(
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
) -> Iterator[InstanceOfferWithAvailability]:
return iter(self._get_offers_cached(requirements, full_offers, unallocated_resources))
offers = self._get_offers_cached(requirements, full_offers, unallocated_resources)
matcher = OfferMatcher(requirements)
return (o for o in offers if matcher.matches(o))

def _get_offers_cached_key(
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
Expand Down
7 changes: 7 additions & 0 deletions src/dstack/_internal/core/backends/base/offers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def get_catalog_offers(
catalog_item_filter: Optional[Callable[[gpuhunt.CatalogItem], bool]] = None,
) -> List[InstanceOffer]:
"""
Returns offers from the `gpuhunt` catalog for the given `backend`.

Note: historically, `gpuhunt` was responsible for filtering offers by `requirements`.
The same filtering is now also done in `dstack` by `OfferMatcher` to avoid converting offers to `gpuhunt` items.
Double filtering is not on a hot path (offers from `gpuhunt` are cached),
but we should eventually drop it to avoid keeping matching logic in sync.

Args:
configurable_disk_size: disk sizes the backend can provision. Used for catalog items
without a fixed disk size: the offer's disk is set to the smallest size within both
Expand Down
9 changes: 4 additions & 5 deletions src/dstack/_internal/core/backends/lium/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
from dstack._internal.core.backends.base.compute import (
Compute,
ComputeWithFilteredOffersCached,
ComputeWithAllOffersCached,
ComputeWithVolumeSupport,
generate_unique_instance_name_for_job,
generate_unique_volume_name,
Expand Down Expand Up @@ -78,21 +78,20 @@
}


class LiumCompute(ComputeWithFilteredOffersCached, ComputeWithVolumeSupport, Compute):
class LiumCompute(ComputeWithAllOffersCached, ComputeWithVolumeSupport, Compute):
def __init__(self, config: LiumConfig):
super().__init__()
self.config = config
self.api_client = LiumAPIClient(config.creds.api_key)
self._catalog = gpuhunt.Catalog(balance_resources=False, auto_reload=False)
self._catalog.add_provider(LiumProvider())

def get_offers_by_requirements(
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
def get_all_offers_with_availability(
self, unallocated_resources: bool
) -> List[InstanceOfferWithAvailability]:
offers = get_catalog_offers(
backend=BackendType.LIUM,
locations=self.config.regions or None,
requirements=requirements,
catalog=self._catalog,
)
return [
Expand Down
11 changes: 5 additions & 6 deletions src/dstack/_internal/core/backends/seeweb/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from dstack._internal.core.backends.base.backend import Compute
from dstack._internal.core.backends.base.compute import (
DEFAULT_PRIVATE_SUBNETS,
ComputeWithAllOffersCached,
ComputeWithCreateInstanceSupport,
ComputeWithFilteredOffersCached,
ComputeWithInstanceVolumesSupport,
ComputeWithMultinodeSupport,
ComputeWithPrivilegedSupport,
Expand All @@ -33,7 +33,7 @@
InstanceOfferWithAvailability,
)
from dstack._internal.core.models.placement import PlacementGroup
from dstack._internal.core.models.runs import JobProvisioningData, Requirements
from dstack._internal.core.models.runs import JobProvisioningData
from dstack._internal.utils.logging import get_logger

logger = get_logger(__name__)
Expand All @@ -56,7 +56,7 @@

class SeewebCompute(
ComputeWithCreateInstanceSupport,
ComputeWithFilteredOffersCached,
ComputeWithAllOffersCached,
ComputeWithPrivilegedSupport,
ComputeWithInstanceVolumesSupport,
ComputeWithMultinodeSupport,
Expand All @@ -74,13 +74,12 @@ def _make_catalog(self) -> gpuhunt.Catalog:
catalog.add_provider(SeewebProvider(token=self.config.creds.api_token))
return catalog

def get_offers_by_requirements(
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
def get_all_offers_with_availability(
self, unallocated_resources: bool
) -> List[InstanceOfferWithAvailability]:
offers = get_catalog_offers(
backend=BackendType.SEEWEB,
locations=self.config.regions or None,
requirements=requirements,
catalog=self._make_catalog(),
)
# A plan can be in the catalog but sold out: mark only currently-creatable
Expand Down
29 changes: 29 additions & 0 deletions src/tests/_internal/core/backends/base/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dstack._internal import settings
from dstack._internal.core.backends.base.compute import (
ComputeWithAllOffersCached,
ComputeWithFilteredOffersCached,
GoArchType,
generate_unique_backend_name,
generate_unique_gateway_instance_name,
Expand Down Expand Up @@ -89,6 +90,34 @@ def test_excludes_offers_whose_disk_does_not_fit(self):
assert [o.instance.name for o in offers] == ["configurable"]


class _ComputeWithFilteredOffers(ComputeWithFilteredOffersCached):
def __init__(self, offers: list[InstanceOfferWithAvailability]) -> None:
super().__init__()
self.offers = offers
self.calls = 0

def get_offers_by_requirements(
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
) -> list[InstanceOfferWithAvailability]:
self.calls += 1
return self.offers


class TestComputeWithFilteredOffersCachedGetOffers:
def test_matches_offers_against_requirements(self):
gpu_offer = get_instance_offer_with_availability(instance_type="gpu", gpu_count=1)
cpu_offer = get_instance_offer_with_availability(instance_type="cpu", gpu_count=0)
compute = _ComputeWithFilteredOffers([gpu_offer, cpu_offer])
requirements = Requirements(resources=ResourcesSpec.model_validate({"gpu": 1}))

first = list(compute.get_offers(requirements, False, False))
second = list(compute.get_offers(requirements, False, False))

assert [o.instance.name for o in first] == ["gpu"]
assert [o.instance.name for o in second] == ["gpu"]
assert compute.calls == 1


class TestGenerateUniqueInstanceName:
def test_generates_name(self):
configuration = get_instance_configuration(
Expand Down
24 changes: 19 additions & 5 deletions src/tests/_internal/core/backends/lium/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ def _item():

def _offer(compute):
with patch.object(compute._catalog, "query", return_value=[_item()]):
return compute.get_offers_by_requirements(
Requirements(resources=ResourcesSpec()),
full_offers=False,
unallocated_resources=False,
)[0]
return compute.get_all_offers_with_availability(unallocated_resources=False)[0]


def _run_job(compute, **overrides):
Expand Down Expand Up @@ -142,6 +138,24 @@ def _terminate(compute, data):
compute.terminate_instance(data.instance_id, data.region, data.backend_data)


class TestGetOffers:
def test_queries_catalog_once_and_matches_requirements(self, compute):
def get_offers(gpu: str):
requirements = Requirements(resources=ResourcesSpec.model_validate({"gpu": gpu}))
return list(compute.get_offers(requirements, False, False))

with patch.object(compute._catalog, "query", return_value=[_item()]) as query:
matching = get_offers("A6000")
not_matching = get_offers("H100")

assert [offer.instance.name for offer in matching] == ["executor-id"]
assert matching[0].availability == InstanceAvailability.AVAILABLE
assert matching[0].instance_runtime == InstanceRuntime.RUNNER
assert not_matching == []
query.assert_called_once()
assert query.call_args.kwargs["gpu_name"] is None


class TestRunJob:
def test_authorizes_cli_keys_for_root_ssh_forwarding(self, compute, tmp_path):
client_key = (
Expand Down
43 changes: 35 additions & 8 deletions src/tests/_internal/core/backends/seeweb/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ def test_get_offers_marks_only_creatable_as_available():
return_value={("ECS1GPU6", "it-fr2")},
),
):
offers = SeewebCompute(_config()).get_offers_by_requirements(
requirements=Requirements(resources=ResourcesSpec()),
full_offers=False,
unallocated_resources=False,
offers = SeewebCompute(_config()).get_all_offers_with_availability(
unallocated_resources=False
)

by_name = {offer.instance.name: offer for offer in offers}
Expand All @@ -152,15 +150,44 @@ def test_get_offers_filters_configured_regions():
return_value={("ECS1GPU7", "it-fr2"), ("ECS1GPU7", "it-mi2")},
),
):
offers = SeewebCompute(_config(["it-mi2"])).get_offers_by_requirements(
requirements=Requirements(resources=ResourcesSpec()),
full_offers=False,
unallocated_resources=False,
offers = SeewebCompute(_config(["it-mi2"])).get_all_offers_with_availability(
unallocated_resources=False
)

assert [offer.region for offer in offers] == ["it-mi2"]


def test_get_offers_fetches_offers_once_and_matches_requirements():
raws = [_raw("ECS1GPU6", "L4"), _raw("ECS1GPU11", "A30")]
with (
patch(
"dstack._internal.core.backends.seeweb.compute.SeewebProvider.get",
return_value=raws,
) as provider_get,
patch(
"dstack._internal.core.backends.seeweb.api_client.SeewebApiClient"
".get_available_plan_regions",
return_value={("ECS1GPU6", "it-fr2"), ("ECS1GPU11", "it-fr2")},
) as get_available_plan_regions,
):
compute = SeewebCompute(_config())
l4_offers = list(
compute.get_offers(
Requirements(resources=ResourcesSpec.model_validate({"gpu": "L4"})), False, False
)
)
a30_offers = list(
compute.get_offers(
Requirements(resources=ResourcesSpec.model_validate({"gpu": "A30"})), False, False
)
)

assert [offer.instance.name for offer in l4_offers] == ["ECS1GPU6"]
assert [offer.instance.name for offer in a30_offers] == ["ECS1GPU11"]
provider_get.assert_called_once()
get_available_plan_regions.assert_called_once()


def test_select_image_prefers_uefi_image():
availability = SeewebPlanAvailability(
regions=frozenset({"it-fr2"}),
Expand Down
Loading