@@ -48,29 +48,68 @@ class Resources(CoreModel):
4848 gpus : List [Gpu ]
4949 spot : bool
5050 disk : Disk = Disk (size_mib = 102400 ) # the default value (100GB) for backward compatibility
51+ # TODO: make description a computed field after migrating to pydanticV2
5152 description : str = ""
5253 cpu_arch : Optional [gpuhunt .CPUArchitecture ] = None
5354
54- def pretty_format (self , include_spot : bool = False ) -> str :
55+ @root_validator
56+ def _description (cls , values ) -> Dict :
57+ try :
58+ description = values ["description" ]
59+ if not description :
60+ cpus = values ["cpus" ]
61+ memory_mib = values ["memory_mib" ]
62+ gpus = values ["gpus" ]
63+ disk_size_mib = values ["disk" ].size_mib
64+ spot = values ["spot" ]
65+ cpu_arch = values ["cpu_arch" ]
66+ values ["description" ] = Resources ._pretty_format (
67+ cpus , cpu_arch , memory_mib , disk_size_mib , gpus , spot , include_spot = True
68+ )
69+ except KeyError :
70+ return values
71+ return values
72+
73+ @staticmethod
74+ def _pretty_format (
75+ cpus : int ,
76+ cpu_arch : Optional [gpuhunt .CPUArchitecture ],
77+ memory_mib : int ,
78+ disk_size_mib : int ,
79+ gpus : List [Gpu ],
80+ spot : bool ,
81+ include_spot : bool = False ,
82+ ) -> str :
5583 resources = {}
56- if self . cpus > 0 :
57- resources ["cpus" ] = self . cpus
58- resources ["cpu_arch" ] = self . cpu_arch
59- if self . memory_mib > 0 :
60- resources ["memory" ] = f"{ self . memory_mib / 1024 :.0f} GB"
61- if self . disk . size_mib > 0 :
62- resources ["disk_size" ] = f"{ self . disk . size_mib / 1024 :.0f} GB"
63- if self . gpus :
64- gpu = self . gpus [0 ]
84+ if cpus > 0 :
85+ resources ["cpus" ] = cpus
86+ resources ["cpu_arch" ] = cpu_arch
87+ if memory_mib > 0 :
88+ resources ["memory" ] = f"{ memory_mib / 1024 :.0f} GB"
89+ if disk_size_mib > 0 :
90+ resources ["disk_size" ] = f"{ disk_size_mib / 1024 :.0f} GB"
91+ if gpus :
92+ gpu = gpus [0 ]
6593 resources ["gpu_name" ] = gpu .name
66- resources ["gpu_count" ] = len (self . gpus )
94+ resources ["gpu_count" ] = len (gpus )
6795 if gpu .memory_mib > 0 :
6896 resources ["gpu_memory" ] = f"{ gpu .memory_mib / 1024 :.0f} GB"
6997 output = pretty_resources (** resources )
70- if include_spot and self . spot :
98+ if include_spot and spot :
7199 output += " (spot)"
72100 return output
73101
102+ def pretty_format (self , include_spot : bool = False ) -> str :
103+ return Resources ._pretty_format (
104+ self .cpus ,
105+ self .cpu_arch ,
106+ self .memory_mib ,
107+ self .disk .size_mib ,
108+ self .gpus ,
109+ self .spot ,
110+ include_spot ,
111+ )
112+
74113
75114class InstanceType (CoreModel ):
76115 name : str
0 commit comments