Skip to content

Commit 8b383ba

Browse files
authored
[CLI] Add --memory option to apply and offer (#3461)
1 parent ad6423d commit 8b383ba

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

src/dstack/_internal/cli/commands/offer.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from typing import List, Literal, cast
44

55
from dstack._internal.cli.commands import APIBaseCommand
6-
from dstack._internal.cli.services.args import cpu_spec, disk_spec, gpu_spec
76
from dstack._internal.cli.services.configurators.run import (
87
BaseRunConfigurator,
98
)
109
from dstack._internal.cli.services.profile import register_profile_args
10+
from dstack._internal.cli.services.resources import register_resources_args
1111
from dstack._internal.cli.utils.common import console
1212
from dstack._internal.cli.utils.gpu import print_gpu_json, print_gpu_table
1313
from dstack._internal.cli.utils.run import print_offers_json, print_run_plan
@@ -47,29 +47,7 @@ def register_args(cls, parser: argparse.ArgumentParser):
4747
default=50,
4848
)
4949
cls.register_env_args(configuration_group)
50-
configuration_group.add_argument(
51-
"--cpu",
52-
type=cpu_spec,
53-
help="Request CPU for the run. "
54-
"The format is [code]ARCH[/]:[code]COUNT[/] (all parts are optional)",
55-
dest="cpu_spec",
56-
metavar="SPEC",
57-
)
58-
configuration_group.add_argument(
59-
"--gpu",
60-
type=gpu_spec,
61-
help="Request GPU for the run. "
62-
"The format is [code]NAME[/]:[code]COUNT[/]:[code]MEMORY[/] (all parts are optional)",
63-
dest="gpu_spec",
64-
metavar="SPEC",
65-
)
66-
configuration_group.add_argument(
67-
"--disk",
68-
type=disk_spec,
69-
help="Request the size range of disk for the run. Example [code]--disk 100GB..[/].",
70-
metavar="RANGE",
71-
dest="disk_spec",
72-
)
50+
register_resources_args(configuration_group)
7351
register_profile_args(parser)
7452

7553

src/dstack/_internal/cli/services/configurators/run.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
import gpuhunt
1313
from pydantic import parse_obj_as
1414

15-
import dstack._internal.core.models.resources as resources
16-
from dstack._internal.cli.services.args import cpu_spec, disk_spec, gpu_spec, port_mapping
15+
from dstack._internal.cli.services.args import port_mapping
1716
from dstack._internal.cli.services.configurators.base import (
1817
ApplyEnvVarsConfiguratorMixin,
1918
BaseApplyConfigurator,
@@ -26,6 +25,7 @@
2625
is_git_repo_url,
2726
register_init_repo_args,
2827
)
28+
from dstack._internal.cli.services.resources import apply_resources_args, register_resources_args
2929
from dstack._internal.cli.utils.common import confirm_ask, console
3030
from dstack._internal.cli.utils.rich import MultiItemStatus
3131
from dstack._internal.cli.utils.run import get_runs_table, print_run_plan
@@ -309,29 +309,7 @@ def register_args(cls, parser: argparse.ArgumentParser):
309309
default=3,
310310
)
311311
cls.register_env_args(configuration_group)
312-
configuration_group.add_argument(
313-
"--cpu",
314-
type=cpu_spec,
315-
help="Request CPU for the run. "
316-
"The format is [code]ARCH[/]:[code]COUNT[/] (all parts are optional)",
317-
dest="cpu_spec",
318-
metavar="SPEC",
319-
)
320-
configuration_group.add_argument(
321-
"--gpu",
322-
type=gpu_spec,
323-
help="Request GPU for the run. "
324-
"The format is [code]NAME[/]:[code]COUNT[/]:[code]MEMORY[/] (all parts are optional)",
325-
dest="gpu_spec",
326-
metavar="SPEC",
327-
)
328-
configuration_group.add_argument(
329-
"--disk",
330-
type=disk_spec,
331-
help="Request the size range of disk for the run. Example [code]--disk 100GB..[/].",
332-
metavar="RANGE",
333-
dest="disk_spec",
334-
)
312+
register_resources_args(configuration_group)
335313
register_profile_args(parser)
336314
repo_group = parser.add_argument_group("Repo Options")
337315
repo_group.add_argument(
@@ -359,16 +337,10 @@ def register_args(cls, parser: argparse.ArgumentParser):
359337
register_init_repo_args(repo_group)
360338

361339
def apply_args(self, conf: RunConfigurationT, args: argparse.Namespace):
340+
apply_resources_args(args, conf)
362341
apply_profile_args(args, conf)
363342
if args.run_name:
364343
conf.name = args.run_name
365-
if args.cpu_spec:
366-
conf.resources.cpu = resources.CPUSpec.parse_obj(args.cpu_spec)
367-
if args.gpu_spec:
368-
conf.resources.gpu = resources.GPUSpec.parse_obj(args.gpu_spec)
369-
if args.disk_spec:
370-
conf.resources.disk = args.disk_spec
371-
372344
self.apply_env_vars(conf.env, args)
373345
self.interpolate_env(conf)
374346

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import argparse
2+
3+
from dstack._internal.cli.services.args import cpu_spec, disk_spec, gpu_spec, memory_spec
4+
from dstack._internal.cli.services.configurators.base import ArgsParser
5+
from dstack._internal.core.models import resources
6+
from dstack._internal.core.models.configurations import AnyRunConfiguration
7+
8+
9+
def register_resources_args(parser: ArgsParser) -> None:
10+
parser.add_argument(
11+
"--cpu",
12+
type=cpu_spec,
13+
help=(
14+
"Request CPU for the run."
15+
" The format is [code]ARCH[/]:[code]COUNT[/] (all parts are optional)"
16+
),
17+
dest="cpu_spec",
18+
metavar="SPEC",
19+
)
20+
parser.add_argument(
21+
"--gpu",
22+
type=gpu_spec,
23+
help=(
24+
"Request GPU for the run."
25+
" The format is [code]NAME[/]:[code]COUNT[/]:[code]MEMORY[/] (all parts are optional)"
26+
),
27+
dest="gpu_spec",
28+
metavar="SPEC",
29+
)
30+
parser.add_argument(
31+
"--memory",
32+
type=memory_spec,
33+
help="Request the size range of RAM for the run. Example [code]--memory 128GB..256GB[/]",
34+
dest="memory_spec",
35+
metavar="RANGE",
36+
)
37+
parser.add_argument(
38+
"--disk",
39+
type=disk_spec,
40+
help="Request the size range of disk for the run. Example [code]--disk 100GB..[/]",
41+
dest="disk_spec",
42+
metavar="RANGE",
43+
)
44+
45+
46+
def apply_resources_args(args: argparse.Namespace, conf: AnyRunConfiguration) -> None:
47+
if args.cpu_spec:
48+
conf.resources.cpu = resources.CPUSpec.parse_obj(args.cpu_spec)
49+
if args.gpu_spec:
50+
conf.resources.gpu = resources.GPUSpec.parse_obj(args.gpu_spec)
51+
if args.memory_spec:
52+
conf.resources.memory = args.memory_spec
53+
if args.disk_spec:
54+
conf.resources.disk = args.disk_spec

0 commit comments

Comments
 (0)