11import shutil
2- from typing import List
2+ from typing import List , Literal
33
44from rich .table import Table
55
6+ from dstack ._internal .cli .models .offer import OfferCommandOutput
67from dstack ._internal .cli .utils .common import console
78from dstack ._internal .core .models .profiles import SpotPolicy
89from dstack ._internal .core .models .runs import Requirements , RunSpec , get_policy_map
910from dstack ._internal .server .schemas .gpus import GpuGroup
1011
1112
12- def print_gpu_json (gpus , run_spec , group_by_cli , api_project ):
13+ def print_gpu_json (
14+ gpus : List [GpuGroup ],
15+ run_spec : RunSpec ,
16+ group_by : List [Literal ["gpu" , "backend" , "region" , "count" ]],
17+ project : str ,
18+ ):
1319 """Print GPU information in JSON format."""
1420 req = Requirements (
1521 resources = run_spec .configuration .resources ,
@@ -18,49 +24,14 @@ def print_gpu_json(gpus, run_spec, group_by_cli, api_project):
1824 reservation = run_spec .configuration .reservation ,
1925 )
2026
21- if req .spot is None :
22- spot_policy = "auto"
23- elif req .spot :
24- spot_policy = "spot"
25- else :
26- spot_policy = "on-demand"
27-
28- output = {
29- "project" : api_project ,
30- "user" : "admin" , # TODO: Get actual user name
31- "resources" : req .resources .dict (),
32- "spot_policy" : spot_policy ,
33- "max_price" : req .max_price ,
34- "reservation" : run_spec .configuration .reservation ,
35- "group_by" : group_by_cli ,
36- "gpus" : [],
37- }
38-
39- for gpu_group in gpus :
40- gpu_data = {
41- "name" : gpu_group .name ,
42- "memory_mib" : gpu_group .memory_mib ,
43- "vendor" : gpu_group .vendor .value ,
44- "availability" : [av .value for av in gpu_group .availability ],
45- "spot" : gpu_group .spot ,
46- "count" : {"min" : gpu_group .count .min , "max" : gpu_group .count .max },
47- "price" : {"min" : gpu_group .price .min , "max" : gpu_group .price .max },
48- }
49-
50- if gpu_group .backend :
51- gpu_data ["backend" ] = gpu_group .backend .value
52- if gpu_group .backends :
53- gpu_data ["backends" ] = [b .value for b in gpu_group .backends ]
54- if gpu_group .region :
55- gpu_data ["region" ] = gpu_group .region
56- if gpu_group .regions :
57- gpu_data ["regions" ] = gpu_group .regions
58-
59- output ["gpus" ].append (gpu_data )
60-
61- import json
27+ output = OfferCommandOutput (
28+ project = project ,
29+ requirements = req ,
30+ group_by = group_by ,
31+ gpus = gpus ,
32+ )
6233
63- print (json . dumps ( output , indent = 2 ))
34+ print (output . json ( ))
6435
6536
6637def print_gpu_table (gpus : List [GpuGroup ], run_spec : RunSpec , group_by : List [str ], project : str ):
0 commit comments