-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtest.py
2367 lines (2053 loc) · 77 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import docker
import json
import os
import re
import numpy as np
import subprocess
import zipfile
import sys
import warnings
import tempfile
import traceback
from pathlib import Path
import yaml
import sys
from dataclasses import dataclass
from enum import Enum
from typing import Callable
from typing import List, Any
# ruff: noqa
MISSING_PACKAGES = False
try:
from fuzzywuzzy import fuzz
from rich.console import Console
from rich.table import Table
from rich.text import Text
from scipy.stats import spearmanr
except ImportError:
MISSING_PACKAGES = True
# ruff: enable
from .. import ErsiliaBase, throw_ersilia_exception
from ..default import (
DOCKERFILE_FILE,
INSTALL_YAML_FILE,
METADATA_JSON_FILE,
METADATA_YAML_FILE,
PACK_METHOD_BENTOML,
PACK_METHOD_FASTAPI,
PREDEFINED_EXAMPLE_FILES,
RUN_FILE,
EOS_TMP,
GITHUB_ORG,
S3_BUCKET_URL_ZIP,
DOCKERHUB_ORG,
)
from ..hub.fetch.actions.template_resolver import TemplateResolver
from ..io.input import ExampleGenerator
from ..hub.content.card import ModelCard
from ..utils.download import GitHubDownloader, S3Downloader
from ..utils.conda import SimpleConda
from ..utils.exceptions_utils import test_exceptions as texc
from ..utils.docker import SimpleDocker
from ..utils.logging import make_temp_dir
from ..utils.spinner import show_loader
from ..utils.hdf5 import Hdf5DataLoader
from ..utils.terminal import run_command_check_output, yes_no_input
from .inspect import ModelInspector
from ..cli import echo
warnings.filterwarnings("ignore", message="Using slow pure-python SequenceMatcher.*")
class Options(Enum):
"""
Enum for different options.
"""
NUM_SAMPLES = 5
BASE = "base"
OUTPUT_CSV = "result.csv"
INPUT_CSV = "input.csv"
OUTPUT1_CSV = "output1.csv"
OUTPUT2_CSV = "output2.csv"
OUTPUT_FILES = [
"file.csv",
"file.h5",
"file.json",
]
INPUT_TYPES = ["str", "list", "csv"]
class Checks(Enum):
"""
Enum for different check types.
"""
MODEL_CONSISTENCY = "Check Consistency of Model Output"
IMAGE_SIZE = "Image Size Mb"
PREDEFINED_EXAMPLE = "Check Predefined Example Input"
ENV_SIZE = "Environment Size Mb"
DIR_SIZE = "Directory Size Mb"
# messages
SIZE_CACL_SUCCESS = "Size Successfully Calculated"
SIZE_CACL_FAILED = "Size Calculation Failed"
INCONSISTENCY = "Inconsistent Output Detected"
CONSISTENCY = "Model Output Was Consistent"
RUN_BASH = "RMSE-MEAN"
class TableType(Enum):
"""
Enum for different table types.
"""
MODEL_INFORMATION_CHECKS = "Model Metadata Checks"
MODEL_FILE_CHECKS = "Model File Checks"
MODEL_DIRECTORY_SIZES = "Model Directory Sizes"
MODEL_ENV_SIZES = "Model Environment Sizes"
RUNNER_CHECKUP_STATUS = "Runner Checkup Status"
FINAL_RUN_SUMMARY = "Test Run Summary"
DEPENDECY_CHECK = "Dependency Check"
COMPUTATIONAL_PERFORMANCE = "Computational Performance Summary"
SHALLOW_CHECK_SUMMARY = "Validation and Size Check Summary"
CONSISTENCY_BASH = "Consistency Summary Between Ersilia and Bash Execution Outputs"
MODEL_OUTPUT = "Model Output Content Validation Summary"
INSPECT_SUMMARY = "Inspect Summary"
@dataclass
class TableConfig:
"""
Configuration for a table.
"""
title: str
headers: List[str]
TABLE_CONFIGS = {
TableType.MODEL_INFORMATION_CHECKS: TableConfig(
title="Model Information Checks", headers=["Check", "Status"]
),
TableType.MODEL_FILE_CHECKS: TableConfig(
title="Model File Checks", headers=["Check", "Status"]
),
TableType.MODEL_DIRECTORY_SIZES: TableConfig(
title="Model Directory Sizes", headers=["Check", "Size"]
),
TableType.RUNNER_CHECKUP_STATUS: TableConfig(
title="Runner Checkup Status",
headers=["Runner", "Status"],
),
TableType.FINAL_RUN_SUMMARY: TableConfig(
title="Test Run Summary", headers=["Check", "Status"]
),
TableType.DEPENDECY_CHECK: TableConfig(
title="Dependency Check", headers=["Check", "Status"]
),
TableType.COMPUTATIONAL_PERFORMANCE: TableConfig(
title="Computational Performance Summary", headers=["Check", "Status"]
),
TableType.SHALLOW_CHECK_SUMMARY: TableConfig(
title="Validation and Size Check Results",
headers=["Check", "Details", "Status"],
),
TableType.MODEL_OUTPUT: TableConfig(
title="Model Output Content Validation Summary",
headers=["Check", "Detail", "Status"],
),
TableType.CONSISTENCY_BASH: TableConfig(
title="Consistency Summary Between Ersilia and Bash Execution Outputs",
headers=["Check", "Result", "Status"],
),
TableType.INSPECT_SUMMARY: TableConfig(
title="Inspect Summary", headers=["Check", "Status"]
),
}
class STATUS_CONFIGS(Enum):
"""
Enum for status configurations.
"""
PASSED = ("PASSED", "green", "✔")
FAILED = ("FAILED", "red", "✘")
WARNING = ("WARNING", "yellow", "⚠")
SUCCESS = ("SUCCESS", "green", "★")
NA = ("N/A", "dim", "~")
def __init__(self, label, color, icon):
self.label = label
self.color = color
self.icon = icon
def __str__(self):
return f"[{self.color}]{self.icon} {self.label}[/{self.color}]"
class CheckStrategy:
"""
Execuetd a strategy for checking inspect commands.
Parameters
----------
check_function : callable
The function to check.
success_key : str
The key for success.
details_key : str
The key for details.
"""
def __init__(self, check_function, success_key, details_key):
self.check_function = check_function
self.success_key = success_key
self.details_key = details_key
def execute(self):
"""
Execute the check strategy.
Returns
-------
dict
The results of the check.
"""
if self.check_function is None:
return {}
result = self.check_function()
if result is None:
return {}
return {
self.success_key: result.success,
self.details_key: result.details,
}
class InspectService(ErsiliaBase):
"""
Service for inspecting models and their configurations.
Parameters
----------
dir : str, optional
Directory where the model is located.
model : str, optional
Model identifier.
remote : bool, optional
Flag indicating whether the model is remote.
config_json : str, optional
Path to the configuration JSON file.
credentials_json : str, optional
Path to the credentials JSON file.
Examples
--------
.. code-block:: python
inspector = InspectService(
dir="/path/to/model", model="model_id"
)
results = inspector.run()
"""
def __init__(
self,
dir: str,
model: str,
remote: bool = False,
config_json: str = None,
credentials_json: str = None,
):
super().__init__(config_json, credentials_json)
self.dir = dir
self.model = model
self.remote = remote
self.resolver = TemplateResolver(model_id=model, repo_path=self.dir)
def run(self, check_keys: list = None) -> dict:
"""
Run the inspection checks on the specified model.
Parameters
----------
check_keys : list, optional
A list of check keys to execute. If None, all checks will be executed.
Returns
-------
dict
A dictionary containing the results of the inspection checks.
Raises
------
ValueError
If the model is not specified.
KeyError
If any of the specified keys do not exist.
"""
def _transform_key(value):
if value is True:
return str(STATUS_CONFIGS.PASSED)
elif value is False:
return str(STATUS_CONFIGS.FAILED)
return value
inspector = ModelInspector(self.model, self.dir)
checks = self._get_checks(inspector)
output = {}
if check_keys:
for key in check_keys:
try:
strategy = checks.get(key)
if strategy.check_function:
output.update(strategy.execute())
except KeyError:
raise KeyError(f"Check '{key}' does not exist.")
else:
for key, strategy in checks.all():
if strategy.check_function:
output.update(strategy.execute())
output = {
" ".join(word.capitalize() for word in k.split("_")): _transform_key(v)
for k, v in output.items()
}
output = [(key, value) for key, value in output.items()]
return output
def _get_checks(self, inspector: ModelInspector) -> dict:
def create_check(check_fn, key, details):
return lambda: CheckStrategy(check_fn, key, details)
dependency_check = (
"Dockerfile" if self.resolver.is_bentoml() else "Install_YAML"
)
checks = {
"is_github_url_available": create_check(
inspector.check_repo_exists if self.remote else lambda: None,
"is_github_url_available",
"is_github_url_available_details",
),
"complete_metadata": create_check(
inspector.check_complete_metadata if self.remote else lambda: None,
"complete_metadata",
"complete_metadata_details",
),
"complete_folder_structure": create_check(
inspector.check_complete_folder_structure,
"complete_folder_structure",
"complete_folder_structure_details",
),
"docker_check": create_check(
inspector.check_dependencies_are_valid,
f"{dependency_check}_check",
"check_details",
),
"computational_performance_tracking": create_check(
inspector.check_computational_performance,
"computational_performance_tracking",
"computational_performance_tracking_details",
),
"extra_files_check": create_check(
inspector.check_no_extra_files if self.remote else lambda: None,
"extra_files_check",
"extra_files_check_details",
),
}
class LazyChecks:
def __init__(self, checks):
self._checks = checks
self._loaded = {}
def get(self, key):
if key not in self._loaded:
if key not in self._checks:
raise KeyError(f"Check '{key}' does not exist.")
self._loaded[key] = self._checks[key]()
return self._loaded[key]
def all(self):
for key in self._checks.keys():
yield key, self.get(key)
return LazyChecks(checks)
class SetupService:
"""
Service for setting up the environment and fetching the model repository.
Parameters
----------
model_id : str
Identifier of the model.
dir : str
Directory where the model repository will be cloned.
from_github : bool
Flag indicating whether to fetch the repository from GitHub.
from_s3 : bool
Flag indicating whether to fetch the repository from S3.
logger : Any
Logger for logging messages.
"""
BASE_URL = "https://github.com/ersilia-os/"
def __init__(
self,
model_id: str,
dir: str,
from_github: bool,
from_s3: bool,
logger: Any,
):
self.model_id = model_id
self.dir = dir
self.logger = logger
self.from_github = from_github
self.from_s3 = from_s3
self.mc = ModelCard()
self.metadata = self.mc.get(model_id)
self.s3 = self.metadata.get("card", {}).get("S3") or self.metadata.get(
"metadata", {}
).get("S3")
self.repo_url = f"{self.BASE_URL}{self.model_id}"
self.overwrite = self._handle_overwrite()
self.github_down = GitHubDownloader(overwrite=self.overwrite)
self.s3_down = S3Downloader()
self.conda = SimpleConda()
def _handle_overwrite(self) -> bool:
if os.path.exists(self.dir):
self.logger.info(f"Directory {self.dir} already exists.")
return yes_no_input(
f"Directory {self.dir} already exists. Do you want to overwrite it? [Y/n]",
default_answer="n",
)
return False
def _download_s3(self):
if not self.overwrite and os.path.exists(self.dir):
self.logger.info("Skipping S3 download as user chose not to overwrite.")
return
tmp_file = os.path.join(make_temp_dir("ersilia-"), f"{self.model_id}.zip")
self.logger.info(f"Downloading model from S3 to temporary file: {tmp_file}")
self.s3_down.download_from_s3(
bucket_url=S3_BUCKET_URL_ZIP,
file_name=f"{self.model_id}.zip",
destination=tmp_file,
)
self.logger.info(f"Extracting model to: {self.dir}")
with zipfile.ZipFile(tmp_file, "r") as zip_ref:
zip_ref.extractall(EOS_TMP)
def _download_github(self):
try:
if not os.path.exists(EOS_TMP):
self.logger.info(f"Path does not exist. Creating: {EOS_TMP}")
os.makedirs(EOS_TMP, exist_ok=True)
except OSError as e:
self.logger.error(f"Failed to create directory {EOS_TMP}: {e}")
self.logger.info(f"Cloning repository from GitHub to: {EOS_TMP}")
self.github_down.clone(
org=GITHUB_ORG,
repo=self.model_id,
destination=self.dir,
)
def get_model(self):
if self.from_s3:
self._download_s3()
if self.from_github:
self._download_github()
@staticmethod
def run_command(
command: str, logger, capture_output: bool = False, shell: bool = True
) -> str:
"""
Run a shell command.
Parameters
----------
command : str
The command to run.
logger : logging.Logger
Logger for logging messages.
capture_output : bool, optional
Flag indicating whether to capture the command output.
shell : bool, optional
Flag indicating whether to run the command in the shell.
Returns
-------
str
The output of the command.
Raises
------
subprocess.CalledProcessError
If the command returns a non-zero exit code.
"""
try:
if capture_output:
result = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
shell=shell,
)
return result.stdout
else:
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=shell,
)
stdout_lines, stderr_lines = [], []
for line in iter(process.stdout.readline, ""):
if line.strip():
stdout_lines.append(line.strip())
logger.info(line.strip())
for line in iter(process.stderr.readline, ""):
if line.strip():
stderr_lines.append(line.strip())
logger.info(line.strip())
process.wait()
if process.returncode != 0:
raise subprocess.CalledProcessError(
returncode=process.returncode,
cmd=command,
output="\n".join(stdout_lines),
stderr="\n".join(stderr_lines),
)
return process.stdout
except subprocess.CalledProcessError as e:
logger.error(f"Error executing command: {e}")
if e.output:
logger.debug(f"Output: {e.output.strip()}")
if e.stderr:
logger.error(f"Error: {e.stderr.strip()}")
sys.exit(1)
except Exception as e:
logger.debug(f"Unexpected error: {e}")
sys.exit(1)
@staticmethod
def get_conda_env_location(model_id: str, logger) -> str:
"""
Get the location of the Conda environment for the model.
Parameters
----------
model_id : str
Identifier of the model.
logger : logging.Logger
Logger for logging messages.
Returns
-------
str
The location of the Conda environment.
Raises
------
subprocess.CalledProcessError
If the command to list Conda environments returns a non-zero exit code.
"""
try:
result = SetupService.run_command(
"conda env list", logger=logger, capture_output=True
)
for line in result.splitlines():
if line.startswith("#") or not line.strip():
continue
parts = line.split()
if parts[0] == model_id:
return parts[-1]
except subprocess.CalledProcessError as e:
print(f"Error running conda command: {e.stderr}")
except Exception as e:
print(f"Unexpected error: {e}")
return None
class IOService:
"""
Service for handling input/output operations related to model testing.
Parameters
----------
logger : logging.Logger
Logger for logging messages.
model_id : str
Identifier of the model.
dir : str
Directory where the model repository is located.
Examples
--------
.. code-block:: python
ios = IOService(
logger=logger,
model_id="model_id",
dir="/path/to/dir",
)
ios.read_information()
"""
RUN_FILE = f"model/framework/{RUN_FILE}"
BENTOML_FILES = [
DOCKERFILE_FILE,
METADATA_JSON_FILE,
RUN_FILE,
"src/service.py",
"pack.py",
"README.md",
"LICENSE",
]
ERSILIAPACK_FILES = [
INSTALL_YAML_FILE,
METADATA_YAML_FILE,
PREDEFINED_EXAMPLE_FILES[0],
PREDEFINED_EXAMPLE_FILES[1],
RUN_FILE,
"README.md",
"LICENSE",
]
def __init__(self, logger, model_id: str, dir: str):
self.logger = logger
self.model_id = model_id
self.dir = dir
self.model_size = 0
self.console = Console()
self.check_results = []
self.simple_docker = SimpleDocker()
self.resolver = TemplateResolver(model_id=model_id, repo_path=self.dir)
def _run_check(
self, check_function, data, check_name: str, additional_info=None
) -> bool:
try:
if additional_info is not None:
check_function(additional_info)
else:
check_function(data)
self.check_results.append((check_name, str(STATUS_CONFIGS.PASSED)))
return True
except Exception as e:
self.logger.error(f"Check '{check_name}' failed: {e}")
self.check_results.append((check_name, str(STATUS_CONFIGS.FAILED)))
return False
def _get_metadata(self):
path = METADATA_JSON_FILE if self.resolver.is_bentoml() else METADATA_YAML_FILE
path = os.path.join(self.dir, path)
with open(path, "r") as file:
if path.endswith(".json"):
data = json.load(file)
elif path.endswith((".yml", ".yaml")):
data = yaml.safe_load(file)
else:
raise ValueError(f"Unsupported file format: {path}")
return data
def collect_and_save_json(self, results, output_file):
"""
Helper function to collect JSON results and save them to a file.
"""
aggregated_json = {}
for result in results:
aggregated_json.update(result)
with open(output_file, "w") as f:
json.dump(aggregated_json, f, indent=4)
def _create_json_data(self, rows, key):
def sanitize_name(name):
return re.sub(r"[ \-./]", "_", str(name).lower())
def parse_status(status):
if isinstance(status, str):
status = re.sub(
r"[-+]?\d*\.\d+|\d+", lambda m: str(float(m.group())), status
)
if "[green]✔" in status:
return True
elif "[red]✘" in status:
return False
else:
return re.sub(r"\[.*?\]", "", status).strip()
return status
def parse_performance(status):
return {
f"pred_{match[0]}": float(match[1])
for match in re.findall(
r"(\d+) predictions executed in (\d+\.\d{2}) seconds. \n", status
)
}
key = re.sub(r" ", "_", key.lower())
json_data = {}
for row in rows:
check_name = sanitize_name(row[0])
check_status = row[-1]
if check_name == "computational_performance_tracking_details":
json_data[check_name] = parse_performance(check_status)
else:
json_data[check_name] = parse_status(check_status)
return {key: json_data}
def _generate_table(self, title, headers, rows, large_table=False, merge=False):
f_col_width = 30 if large_table else 30
l_col_width = 50 if large_table else 10
d_col_width = 30 if not large_table else 20
table = Table(
title=Text(title, style="bold light_green"),
border_style="light_green",
show_lines=True,
)
table.add_column(headers[0], justify="left", width=f_col_width, style="bold")
for header in headers[1:-1]:
table.add_column(header, justify="center", width=d_col_width, style="bold")
table.add_column(headers[-1], justify="right", width=l_col_width, style="bold")
prev_value = None
for row in rows:
first_col = str(row[0])
if merge and first_col == prev_value:
first_col = ""
else:
prev_value = first_col
styled_row = [
Text(first_col, style="bold"),
*[str(cell) for cell in row[1:-1]],
row[-1],
]
table.add_row(*styled_row)
json_data = self._create_json_data(rows, title)
self.console.print(table)
return json_data
@staticmethod
def get_model_type(model_id: str, repo_path: str) -> str:
"""
Get the type of the model based on the repository contents.
Parameters
----------
model_id : str
Identifier of the model.
repo_path : str
Path to the model repository.
Returns
-------
str
The type of the model (e.g., PACK_METHOD_BENTOML, PACK_METHOD_FASTAPI).
"""
resolver = TemplateResolver(model_id=model_id, repo_path=repo_path)
if resolver.is_bentoml():
return PACK_METHOD_BENTOML
elif resolver.is_fastapi():
return PACK_METHOD_FASTAPI
else:
return None
def get_file_requirements(self) -> List[str]:
"""
Get the list of required files for the model.
Returns
-------
List[str]
List of required files.
Raises
------
ValueError
If the model type is unsupported.
"""
type = IOService.get_model_type(model_id=self.model_id, repo_path=self.dir)
if type == PACK_METHOD_BENTOML:
return self.BENTOML_FILES
elif type == PACK_METHOD_FASTAPI:
return self.ERSILIAPACK_FILES
else:
raise ValueError(f"Unsupported model type: {type}")
def read_information(self) -> dict:
"""
Read the information file for the model.
Returns
-------
dict
The contents of the information file.
Raises
------
FileNotFoundError
If the information file does not exist.
"""
file = os.path.join(EOS_TMP, self.model_id, METADATA_JSON_FILE)
if not os.path.exists(file):
raise FileNotFoundError(
f"Information file does not exist for model {self.model_id}"
)
with open(file, "r") as f:
return json.load(f)
def get_conda_env_size(self):
"""
Get the size of the Conda environment for the model.
Returns
-------
int
The size of the Conda environment in megabytes.
Raises
------
Exception
If there is an error calculating the size.
"""
try:
loc = SetupService.get_conda_env_location(self.model_id, self.logger)
return self.calculate_directory_size(loc)
except Exception as e:
self.logger.error(
f"Error calculating size of Conda environment '{self.model_id}': {e}"
)
return 0
def calculate_directory_size(self, path: str) -> int:
"""
Calculate the size of a directory.
Parameters
----------
path : str
The path to the directory.
Returns
-------
int
The size of the directory.
"""
try:
size_output = SetupService.run_command(
["du", "-sm", path],
logger=self.logger,
capture_output=True,
shell=False,
)
size = float(size_output.split()[0])
return size
except Exception as e:
self.logger.error(f"Error calculating directory size for {path}: {e}")
return 0
def calculate_image_size(self, tag="latest"):
"""
Calculate the size of a Docker image.
Parameters
----------
tag : str, optional
The tag of the Docker image (default is 'latest').
Returns
-------
str
The size of the Docker image.
"""
image_name = f"{DOCKERHUB_ORG}/{self.model_id}:{tag}"
client = docker.from_env()
try:
image = client.images.get(image_name)
size_bytes = image.attrs["Size"]
size_mb = size_bytes / (1024**2)
return f"{size_mb:.2f} MB", Checks.SIZE_CACL_SUCCESS.value
except docker.errors.ImageNotFound:
return f"Image '{image_name}' not found.", Checks.SIZE_CACL_FAILED.value
except Exception as e:
return f"An error occurred: {e}", Checks.SIZE_CACL_FAILED.value
@throw_ersilia_exception()
def get_directories_sizes(self) -> str:
"""
Get the sizes of the model directory.
Returns
-------
str
A string of containing size of the model directory
"""
dir_size = self.calculate_directory_size(self.dir)
dir_size = f"{dir_size:.2f}"
return dir_size
@throw_ersilia_exception()
def get_env_sizes(self) -> str:
"""
Get the sizes of the Conda environment directory.
Returns
-------
str
A string of containing size of the model environment
"""
env_size = self.get_conda_env_size()
env_size = f"{env_size:.2f}"
return env_size
def _extract_size(self, data, key="validation_and_size_check_results"):
sizes, keys = {}, ("environment_size_mb", "image_size_mb")
validation_results = next((item.get(key) for item in data if key in item), None)
if validation_results:
if keys[0] in validation_results:
env_size = validation_results[keys[0]]
self.logger.info(f"Environment Size: {env_size}")
sizes["Environment Size"] = float(env_size)
if keys[1] in validation_results:
img_size = validation_results[keys[1]]
self.logger.info(f"Image Size: {img_size}")
sizes["Image Size"] = float(img_size)
return sizes
def _extract_execution_times(self, data, key="computational_performance_summary"):
self.logger.info("Performance Extraction is started")
performance = {
"Computational Performance 1": None,
"Computational Performance 10": None,
"Computational Performance 100": None,
}
summary = next((item.get(key) for item in data if key in item), None)
if summary:
preds = summary.get("computational_performance_tracking_details")
performance["Computational Performance 1"] = float(preds.get("pred_1"))
performance["Computational Performance 10"] = float(preds.get("pred_10"))
performance["Computational Performance 100"] = float(preds.get("pred_100"))
return performance
def update_metadata(self, json_data):
"""
Processes JSON/YAML metadata to extract size and performance info and then updates them.
Parameters
----------
json_data : dict
Report data from the command output.
Returns
-------
dict
Updated metadata containing computed performance and size information.
"""
sizes = self._extract_size(json_data)
exec_times = self._extract_execution_times(json_data)
metadata = self._get_metadata()
metadata.update(sizes)
metadata.update(exec_times)
self._save_file(
metadata,
)