Skip to content

Commit d79b426

Browse files
authored
Match offers of all backends with OfferMatcher (#4540)
* Switch Lium and Seeweb to ComputeWithAllOffersCached * Match offers of ComputeWithFilteredOffersCached backends with OfferMatcher * Comment about double-filtering
1 parent 6d47c00 commit d79b426

7 files changed

Lines changed: 107 additions & 26 deletions

File tree

‎src/dstack/_internal/core/backends/base/compute.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,11 @@ def get_offers_by_requirements(
373373
unallocated_resources: bool,
374374
) -> List[InstanceOfferWithAvailability]:
375375
"""
376-
Returns backend offers with availability matching requirements.
376+
Returns backend offers with availability for requirements.
377+
378+
The offers are matched against `requirements` with `OfferMatcher` afterwards, so this
379+
method may use `requirements` only to narrow down or shape the offers, e.g. to query
380+
the provider or to size offers.
377381
378382
See `Compute.get_offers()` for the `full_offers` argument description.
379383
Set the class variable `full_offers_argument_has_effect` to `True` if the `full_offers`
@@ -388,7 +392,9 @@ def get_offers_by_requirements(
388392
def get_offers(
389393
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
390394
) -> Iterator[InstanceOfferWithAvailability]:
391-
return iter(self._get_offers_cached(requirements, full_offers, unallocated_resources))
395+
offers = self._get_offers_cached(requirements, full_offers, unallocated_resources)
396+
matcher = OfferMatcher(requirements)
397+
return (o for o in offers if matcher.matches(o))
392398

393399
def _get_offers_cached_key(
394400
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool

‎src/dstack/_internal/core/backends/base/offers.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def get_catalog_offers(
5353
catalog_item_filter: Optional[Callable[[gpuhunt.CatalogItem], bool]] = None,
5454
) -> List[InstanceOffer]:
5555
"""
56+
Returns offers from the `gpuhunt` catalog for the given `backend`.
57+
58+
Note: historically, `gpuhunt` was responsible for filtering offers by `requirements`.
59+
The same filtering is now also done in `dstack` by `OfferMatcher` to avoid converting offers to `gpuhunt` items.
60+
Double filtering is not on a hot path (offers from `gpuhunt` are cached),
61+
but we should eventually drop it to avoid keeping matching logic in sync.
62+
5663
Args:
5764
configurable_disk_size: disk sizes the backend can provision. Used for catalog items
5865
without a fixed disk size: the offer's disk is set to the smallest size within both

‎src/dstack/_internal/core/backends/lium/compute.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from dstack._internal.core.backends.base.compute import (
1616
Compute,
17-
ComputeWithFilteredOffersCached,
17+
ComputeWithAllOffersCached,
1818
ComputeWithVolumeSupport,
1919
generate_unique_instance_name_for_job,
2020
generate_unique_volume_name,
@@ -78,21 +78,20 @@
7878
}
7979

8080

81-
class LiumCompute(ComputeWithFilteredOffersCached, ComputeWithVolumeSupport, Compute):
81+
class LiumCompute(ComputeWithAllOffersCached, ComputeWithVolumeSupport, Compute):
8282
def __init__(self, config: LiumConfig):
8383
super().__init__()
8484
self.config = config
8585
self.api_client = LiumAPIClient(config.creds.api_key)
8686
self._catalog = gpuhunt.Catalog(balance_resources=False, auto_reload=False)
8787
self._catalog.add_provider(LiumProvider())
8888

89-
def get_offers_by_requirements(
90-
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
89+
def get_all_offers_with_availability(
90+
self, unallocated_resources: bool
9191
) -> List[InstanceOfferWithAvailability]:
9292
offers = get_catalog_offers(
9393
backend=BackendType.LIUM,
9494
locations=self.config.regions or None,
95-
requirements=requirements,
9695
catalog=self._catalog,
9796
)
9897
return [

‎src/dstack/_internal/core/backends/seeweb/compute.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from dstack._internal.core.backends.base.backend import Compute
88
from dstack._internal.core.backends.base.compute import (
99
DEFAULT_PRIVATE_SUBNETS,
10+
ComputeWithAllOffersCached,
1011
ComputeWithCreateInstanceSupport,
11-
ComputeWithFilteredOffersCached,
1212
ComputeWithInstanceVolumesSupport,
1313
ComputeWithMultinodeSupport,
1414
ComputeWithPrivilegedSupport,
@@ -33,7 +33,7 @@
3333
InstanceOfferWithAvailability,
3434
)
3535
from dstack._internal.core.models.placement import PlacementGroup
36-
from dstack._internal.core.models.runs import JobProvisioningData, Requirements
36+
from dstack._internal.core.models.runs import JobProvisioningData
3737
from dstack._internal.utils.logging import get_logger
3838

3939
logger = get_logger(__name__)
@@ -56,7 +56,7 @@
5656

5757
class SeewebCompute(
5858
ComputeWithCreateInstanceSupport,
59-
ComputeWithFilteredOffersCached,
59+
ComputeWithAllOffersCached,
6060
ComputeWithPrivilegedSupport,
6161
ComputeWithInstanceVolumesSupport,
6262
ComputeWithMultinodeSupport,
@@ -74,13 +74,12 @@ def _make_catalog(self) -> gpuhunt.Catalog:
7474
catalog.add_provider(SeewebProvider(token=self.config.creds.api_token))
7575
return catalog
7676

77-
def get_offers_by_requirements(
78-
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
77+
def get_all_offers_with_availability(
78+
self, unallocated_resources: bool
7979
) -> List[InstanceOfferWithAvailability]:
8080
offers = get_catalog_offers(
8181
backend=BackendType.SEEWEB,
8282
locations=self.config.regions or None,
83-
requirements=requirements,
8483
catalog=self._make_catalog(),
8584
)
8685
# A plan can be in the catalog but sold out: mark only currently-creatable

‎src/tests/_internal/core/backends/base/test_compute.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from dstack._internal import settings
88
from dstack._internal.core.backends.base.compute import (
99
ComputeWithAllOffersCached,
10+
ComputeWithFilteredOffersCached,
1011
GoArchType,
1112
generate_unique_backend_name,
1213
generate_unique_gateway_instance_name,
@@ -89,6 +90,34 @@ def test_excludes_offers_whose_disk_does_not_fit(self):
8990
assert [o.instance.name for o in offers] == ["configurable"]
9091

9192

93+
class _ComputeWithFilteredOffers(ComputeWithFilteredOffersCached):
94+
def __init__(self, offers: list[InstanceOfferWithAvailability]) -> None:
95+
super().__init__()
96+
self.offers = offers
97+
self.calls = 0
98+
99+
def get_offers_by_requirements(
100+
self, requirements: Requirements, full_offers: bool, unallocated_resources: bool
101+
) -> list[InstanceOfferWithAvailability]:
102+
self.calls += 1
103+
return self.offers
104+
105+
106+
class TestComputeWithFilteredOffersCachedGetOffers:
107+
def test_matches_offers_against_requirements(self):
108+
gpu_offer = get_instance_offer_with_availability(instance_type="gpu", gpu_count=1)
109+
cpu_offer = get_instance_offer_with_availability(instance_type="cpu", gpu_count=0)
110+
compute = _ComputeWithFilteredOffers([gpu_offer, cpu_offer])
111+
requirements = Requirements(resources=ResourcesSpec.model_validate({"gpu": 1}))
112+
113+
first = list(compute.get_offers(requirements, False, False))
114+
second = list(compute.get_offers(requirements, False, False))
115+
116+
assert [o.instance.name for o in first] == ["gpu"]
117+
assert [o.instance.name for o in second] == ["gpu"]
118+
assert compute.calls == 1
119+
120+
92121
class TestGenerateUniqueInstanceName:
93122
def test_generates_name(self):
94123
configuration = get_instance_configuration(

‎src/tests/_internal/core/backends/lium/test_compute.py‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ def _item():
9797

9898
def _offer(compute):
9999
with patch.object(compute._catalog, "query", return_value=[_item()]):
100-
return compute.get_offers_by_requirements(
101-
Requirements(resources=ResourcesSpec()),
102-
full_offers=False,
103-
unallocated_resources=False,
104-
)[0]
100+
return compute.get_all_offers_with_availability(unallocated_resources=False)[0]
105101

106102

107103
def _run_job(compute, **overrides):
@@ -142,6 +138,24 @@ def _terminate(compute, data):
142138
compute.terminate_instance(data.instance_id, data.region, data.backend_data)
143139

144140

141+
class TestGetOffers:
142+
def test_queries_catalog_once_and_matches_requirements(self, compute):
143+
def get_offers(gpu: str):
144+
requirements = Requirements(resources=ResourcesSpec.model_validate({"gpu": gpu}))
145+
return list(compute.get_offers(requirements, False, False))
146+
147+
with patch.object(compute._catalog, "query", return_value=[_item()]) as query:
148+
matching = get_offers("A6000")
149+
not_matching = get_offers("H100")
150+
151+
assert [offer.instance.name for offer in matching] == ["executor-id"]
152+
assert matching[0].availability == InstanceAvailability.AVAILABLE
153+
assert matching[0].instance_runtime == InstanceRuntime.RUNNER
154+
assert not_matching == []
155+
query.assert_called_once()
156+
assert query.call_args.kwargs["gpu_name"] is None
157+
158+
145159
class TestRunJob:
146160
def test_authorizes_cli_keys_for_root_ssh_forwarding(self, compute, tmp_path):
147161
client_key = (

‎src/tests/_internal/core/backends/seeweb/test_compute.py‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,8 @@ def test_get_offers_marks_only_creatable_as_available():
124124
return_value={("ECS1GPU6", "it-fr2")},
125125
),
126126
):
127-
offers = SeewebCompute(_config()).get_offers_by_requirements(
128-
requirements=Requirements(resources=ResourcesSpec()),
129-
full_offers=False,
130-
unallocated_resources=False,
127+
offers = SeewebCompute(_config()).get_all_offers_with_availability(
128+
unallocated_resources=False
131129
)
132130

133131
by_name = {offer.instance.name: offer for offer in offers}
@@ -152,15 +150,44 @@ def test_get_offers_filters_configured_regions():
152150
return_value={("ECS1GPU7", "it-fr2"), ("ECS1GPU7", "it-mi2")},
153151
),
154152
):
155-
offers = SeewebCompute(_config(["it-mi2"])).get_offers_by_requirements(
156-
requirements=Requirements(resources=ResourcesSpec()),
157-
full_offers=False,
158-
unallocated_resources=False,
153+
offers = SeewebCompute(_config(["it-mi2"])).get_all_offers_with_availability(
154+
unallocated_resources=False
159155
)
160156

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

163159

160+
def test_get_offers_fetches_offers_once_and_matches_requirements():
161+
raws = [_raw("ECS1GPU6", "L4"), _raw("ECS1GPU11", "A30")]
162+
with (
163+
patch(
164+
"dstack._internal.core.backends.seeweb.compute.SeewebProvider.get",
165+
return_value=raws,
166+
) as provider_get,
167+
patch(
168+
"dstack._internal.core.backends.seeweb.api_client.SeewebApiClient"
169+
".get_available_plan_regions",
170+
return_value={("ECS1GPU6", "it-fr2"), ("ECS1GPU11", "it-fr2")},
171+
) as get_available_plan_regions,
172+
):
173+
compute = SeewebCompute(_config())
174+
l4_offers = list(
175+
compute.get_offers(
176+
Requirements(resources=ResourcesSpec.model_validate({"gpu": "L4"})), False, False
177+
)
178+
)
179+
a30_offers = list(
180+
compute.get_offers(
181+
Requirements(resources=ResourcesSpec.model_validate({"gpu": "A30"})), False, False
182+
)
183+
)
184+
185+
assert [offer.instance.name for offer in l4_offers] == ["ECS1GPU6"]
186+
assert [offer.instance.name for offer in a30_offers] == ["ECS1GPU11"]
187+
provider_get.assert_called_once()
188+
get_available_plan_regions.assert_called_once()
189+
190+
164191
def test_select_image_prefers_uefi_image():
165192
availability = SeewebPlanAvailability(
166193
regions=frozenset({"it-fr2"}),

0 commit comments

Comments
 (0)