-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathai.py
More file actions
1302 lines (1157 loc) · 54.2 KB
/
ai.py
File metadata and controls
1302 lines (1157 loc) · 54.2 KB
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
594
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
from __future__ import annotations
import datetime
import enum
import json
import os
import re
import shutil
import tarfile
import time
import traceback
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
from urllib.parse import urlparse
import boto3 # type: ignore
import requests
from pydantic import ValidationError
from rich.prompt import Confirm
from superai import settings
from superai.log import logger
from superai.meta_ai.ai_helper import (
find_root_model,
get_user_model_class,
list_models,
upload_dir,
)
from superai.meta_ai.ai_template import AITemplate
from superai.meta_ai.config_parser import AIConfig, InstanceConfig
from superai.meta_ai.deployed_predictors import DeployedPredictor, PredictorFactory
from superai.meta_ai.dockerizer import ecr_full_name, push_image
from superai.meta_ai.environment_file import EnvironmentFileProcessor
from superai.meta_ai.exceptions import ModelDeploymentError, ModelNotFoundError
from superai.meta_ai.image_builder import (
AiImageBuilder,
BaseAIOrchestrator,
Orchestrator,
TrainingOrchestrator,
)
from superai.meta_ai.parameters import (
AiDeploymentParameters,
Config,
HyperParameterSpec,
ModelParameters,
TrainingParameters,
)
from superai.meta_ai.schema import (
SchemaParameters,
TaskBatchInput,
TaskInput,
TaskPredictionInstance,
TrainerOutput,
)
from superai.utils import retry
DEPLOYMENT_PARAMETERS_SUBKEY = (
"deployment_parameters" # Used to access deployment parameters in the training parameters
)
# Prefix path for the model directory on storage backend
MODEL_ARTIFACT_PREFIX_S3 = "meta_ai_models"
# extended path to model weights
MODEL_WEIGHT_INFIX_S3 = "saved_models"
if TYPE_CHECKING:
from superai.meta_ai import BaseModel
log = logger.get_logger(__name__)
class Stage(str, enum.Enum):
DEV = "DEV"
PROD = "PROD"
STAGING = "STAGING"
LATEST = "LATEST"
SANDBOX = "SANDBOX"
class AI:
def __init__(
self,
ai_template: AITemplate,
input_params: SchemaParameters,
output_params: SchemaParameters,
name,
configuration: Optional[Config] = None,
version: int = None,
root_id: Optional[str] = None,
description: Optional[str] = None,
weights_path: str = None,
overwrite=False,
**kwargs,
):
"""Creates an AI with custom inference logic and optional data dependencies as a superai artifact.
Args:
input_params: Schema definition of the AI object input.
output_params: Schema definition of the AI object output.
name: Name of the AI. If the name already exists, an exception will be raised.
version: AI integer version. If no version is specified the AI is set to version 1.
root_id: Id of the root AI. Establishes the lineage of AIs.
Is necessary when using `version` > 1.
description: Optional; A free text description. Allows the user to describe the AI's intention.
weights_path: Path to a file or directory containing model data. This is accessible in the
:func:`BaseModel.load_weights(weights_path) <superai.meta_ai.base.BaseModel.load_weights>
**kwargs: Arbitrary keyword arguments
"""
self.input_params = input_params
self.output_params = output_params
self.configuration = configuration
self.ai_template = ai_template
self.name = name
self.version = version
self.root_id = root_id
self.bucket_name = self.ai_template.bucket_name
self.description = description
self.weights_path = weights_path
self.code_path = self.ai_template.code_path
self.conda_env = self.ai_template.conda_env
self.requirements = self.ai_template.requirements
self.artifacts = self.ai_template.artifacts
self.parameters = self.ai_template.parameters
self.client = self.ai_template.client
self.overwrite = overwrite
self.stage: Optional[str] = None # stage is not set by default
if "loaded" not in kwargs:
# self.update_version_by_availability(kwargs.get("loaded", False))
self._location = self.save(overwrite=self.overwrite)
else:
assert kwargs.get("location") is not None, "Location cannot be None while loading"
self._location = kwargs.get("location")
self.model_class_name = ai_template.model_class
self.model_class_path = ai_template.model_class_path
self.environs: "EnvironmentFileProcessor" = ai_template.environs
self.is_weights_loaded = False
self.container = None
self._id = None
self.model_class: Optional[BaseModel] = None
# ID of deployment serving predictions
self.served_by: Optional[str] = None
def _init_model_class(self, load_weights=True, force_reload=False):
"""
Initializes the BaseModel class.
Args:
load_weights: bool
Will also load weights if True.
force_reload:
Will reload the model class even if it was already loaded before.
"""
if self.model_class is None or force_reload:
model_class_template = get_user_model_class(
model_name=self.model_class_name, save_location=self._location, path=self.model_class_path
)
self.model_class: BaseModel = model_class_template(
input_schema=self.ai_template.input_schema,
output_schema=self.ai_template.output_schema,
configuration=self.ai_template.configuration,
)
self.model_class.update_parameters(self.input_params, self.output_params)
if load_weights and (not self.is_weights_loaded or force_reload):
if self.weights_path is not None:
self.model_class.load_weights(self.weights_path)
self.is_weights_loaded = True
@property
def id(self) -> Optional[str]:
if not self._id:
model = self.client.get_model_by_name_version(self.name, self.version)
if model:
self._id = model[0]["id"]
return self._id
@property
def deployment_id(self) -> str:
if self.served_by:
return self.served_by
else:
# Legacy method to retrieve one deployment with matching model_id
# Could lead to inconsistencies if multiple deployments are present
deployment_list = self.client.list_deployments(self.id)
if deployment_list:
return deployment_list[0]["id"]
@property
def deployed(self) -> Optional[bool]:
if self.id and self.served_by:
deployment = self.client.get_deployment(self.served_by)
if deployment:
return True
return False
def __eq__(self, other):
if not isinstance(other, AI):
return False
compare_keys = [
"input_params",
"output_params",
"name",
"version",
"stage",
"description",
"artifacts",
"parameters",
]
comparisons = [self.__dict__[key] == other.__dict__[key] for key in compare_keys]
return all(comparisons)
def __str__(self):
return (
f"AI model : "
f"\n\tName: {self.name} "
f"\n\tVersion: {self.version}"
f"\n\tDescription: {self.description}"
f"\n\tStage: {self.stage}"
f"\n\tId: {self.id}"
f"\n\tDeployed: {self.deployed}"
)
def update_version_by_availability(self, loaded=False):
existing_models = self.ai_template.client.get_model_by_name(self.name)
if len(existing_models) and not loaded:
if self.version in [x["version"] for x in existing_models]:
latest_version: int = self.ai_template.client.get_latest_version_of_model_by_name(self.name)
self.version = latest_version + 1
log.info(
f"Found an existing model with name {self.name} in the database, "
f"incrementing version from the latest found version: {latest_version} -> {self.version}"
)
@classmethod
def load_by_name_version(cls, name, version: int = None, stage: str = None) -> "AI":
"""
Loads an AI by name. If the version or stage are specified that specific version will be loaded.
Args:
name: AI name.
version: A version number.
stage: The AI stage.
Returns:
AI object
"""
raise NotImplementedError("Method not supported")
@classmethod
def load(cls, path: str, weights_path: str = None) -> "AI":
"""Loads an AI from a local or S3 path. If an S3 path is passed, the AI model will be downloaded from S3
directly.
Args:
path
weights_path
if :param path is a valid path, the model will be loaded from the local path
if :param path is a valid S3 path, i.e., s3://bucket/prefix/some_model_path, the model will be downloaded
from S3. Manage S3 access using your AWS credentials.
if :param path is a valid model path i.e., prefix is model://some_name/version, or model://some_name/stage
then the database will be referenced to find the relevant model and loaded.
A path can be considered valid only if there is an `AISaveFile.json` and `ai_model` file present.
Returns:
AI class of the loaded model
"""
if path.startswith("s3://"):
return cls.load_from_s3(path, weights_path)
elif path.startswith("model://"):
# get s3 path from db and load using cls.load_from_s3(path)
name = path.split("model://")[-1].split("/")[0]
log.info(f"Searching models with name `{name}` in database...")
all_models: List[Dict] = list_models(name, raw=True, verbose=False)
if len(all_models):
match = re.search("(.*)/(.*)", path.split("model://" + name + "/")[-1])
if match is not None:
stage, version = match.groups()
if stage and version:
selected = list(
filter(
lambda x: x["stage"] == stage and x["version"] == version,
all_models,
)
)
if len(selected):
s3_path = selected[0]["modelSavePath"]
weights_path = selected[0]["weightsPath"]
return cls.load_from_s3(s3_path, weights_path)
else:
raise ModelNotFoundError(
f"No model found for the given stage {stage} and version {version}"
)
log.info("Returning the latest version in models")
selected = all_models.sort(key=lambda x: x["version"], reverse=True)[0]
s3_path = selected["modelSavePath"]
weights_path = selected["weightsPath"]
return cls.load_from_s3(s3_path, weights_path)
else:
# either stage or version is present
# check for version
ending = path.split(f"model://{name}/")[-1]
if cls.is_valid_version(ending):
s3_path = [entry for entry in all_models if int(entry["version"]) == int(ending)][0][
"modelSavePath"
]
else:
stage = ending
selected_models: List[dict] = [entry for entry in all_models if entry["stage"] == stage]
model_entry = selected_models.sort(key=lambda x: x["version"], reverse=True)[0]
s3_path = model_entry["modelSavePath"]
weights_path = model_entry["weightsPath"]
return cls.load_from_s3(s3_path, weights_path)
else:
raise ModelNotFoundError(f"No models found for the given name : {name}")
else:
if os.path.exists(path):
return cls.load_local(path, weights_path)
else:
raise ValueError("Invalid path, please ensure the path exists")
@staticmethod
def is_valid_version(version: str) -> bool:
try:
int(version)
return True
except ValueError:
return False
@classmethod
def load_from_s3(cls, path: str, weights_path: Optional[str] = None) -> "AI":
assert path.startswith("s3") and path.endswith(
"AISavedModel.tar.gz"
), "Invalid path provided, should start with s3 and end with AISavedModel.tar.gz"
log.info(f"Loading from '{path}' with weights in '{weights_path}'")
s3 = boto3.client("s3")
download_folder = os.path.join("/tmp", f"ai_contents_{int(time.time())}")
os.makedirs(download_folder, exist_ok=True)
log.info(f"Storing temporary files in {download_folder}")
parsed_url = urlparse(path, allow_fragments=False)
bucket_name = parsed_url.netloc
path_to_object = parsed_url.path if not parsed_url.path.startswith("/") else parsed_url.path[1:]
log.info(f"Downloading and unpacking AI object from bucket `{bucket_name}` and path `{path_to_object}`")
s3.download_file(bucket_name, path_to_object, os.path.join(download_folder, "AISavedModel.tar.gz"))
with tarfile.open(os.path.join(download_folder, "AISavedModel.tar.gz")) as tar:
def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members, numeric_owner)
safe_extract(tar, path=os.path.join(download_folder,"AISavedModel"))
return cls.load_local(
load_path=os.path.join(download_folder, "AISavedModel", "ai"),
weights_path=weights_path,
download_folder=download_folder,
)
@classmethod
def load_local(cls, load_path: str, weights_path: Optional[str] = None, **kwargs) -> "AI":
"""Loads AI model stored locally.
Args:
weights_path: Location of weights.
load_path: The location of the AISave or any other matching folder.
**kwargs: Arbitrary keyword arguments
"""
log.info(f"Attempting to load model from {load_path}...")
with open(os.path.join(load_path, "AISaveFile.json"), "r") as json_file:
details = json.load(json_file)
log.info("Verifying AISaveFile.json...")
input_params = details["input_params"]
output_params = details["output_params"]
configuration = details["configuration"]
name = details["name"]
version = details.get("version")
description = details.get("description")
ai_template = AITemplate.load_local(load_path)
if weights_path is not None:
if weights_path.startswith("s3"):
s3 = boto3.client("s3")
download_folder = kwargs.get("download_folder", os.path.join("/tmp", f"ai_contents_{int(time.time())}"))
os.makedirs(download_folder, exist_ok=True)
parsed_url = urlparse(weights_path, allow_fragments=False)
bucket_name = parsed_url.netloc
path_to_object = parsed_url.path if not parsed_url.path.startswith("/") else parsed_url.path[1:]
log.info(f"Downloading and unpacking weights from bucket '{bucket_name}' and path '{path_to_object}'")
s3.download_file(
bucket_name, path_to_object, os.path.join(download_folder, os.path.basename(weights_path))
)
if weights_path.endswith(".tar.gz"):
tar_weights = tarfile.open(os.path.join(download_folder, os.path.basename(weights_path)))
weights_folder_name = os.path.basename(weights_path).split(".tar.gz")[0]
weights_path = os.path.join(download_folder, weights_folder_name)
tar_weights.extractall(path=download_folder)
log.info(f"New weights path {weights_path}")
tar_weights.close()
shutil.rmtree(os.path.join(download_folder, os.path.basename(weights_path)))
elif not os.path.exists(weights_path):
raise ValueError(
f"Unexpected weights path provided {weights_path}. Please ensure its a s3 path or a local folder"
)
log.info(f"Loaded model from {load_path}")
return AI(
ai_template=ai_template,
input_params=input_params,
output_params=output_params,
name=name,
configuration=configuration,
version=version,
description=description,
weights_path=weights_path,
location=load_path,
loaded=True,
)
def _create_database_entry(self, **kwargs):
"""Adds an entry in the meta AI database.
Args:
name: Name of the model.
description: Description of model.
version: Version of model.
stage: Stage of model.
metadata: Metadata associated with model.
endpoint: Endpoint specified of the model.
input_schema: Input schema followed by model.
output_schema: Output schema followed by model.
model_save_path: Location in S3 where the AISaveModel has to be placed.
weights_path: Location of weights.
visibility: Visibility of model. Default visibility: PRIVATE.
root_id: Id of the root model. Establishes the lineage of the model.
deployment_parameters: Hardware and scaling parameters for the model.
**kwargs: Arbitrary keyword arguments
"""
log.info("Creating database entry...")
if not self.id:
self._id = self.client.add_model(**kwargs)
else:
# TODO: Add proper exception class
raise Exception("Model is already registered in the Database.")
return self.id
def transition_ai_version_stage(
self,
stage: str,
version: Optional[int] = None,
archive_existing: bool = True,
):
"""Transitions an AI version number to the specified stage. [Mutable]
Args:
version: Optinal; AI version number. Can be defaulted to object version.
stage: Transition AI version to new stage.
archive_existing: When transitioning an AI version to a particular stage, this flag dictates whether
all existing ai versions in that stage should be atomically moved to the “archived”
stage. This ensures that at-most-one ai version exists in the target stage. This
field is by default set to True.
Returns:
Updated AI version
"""
# TODO: Archive existing
sett_version = version if version is not None else self.version
assert sett_version is not None, "Cannot transition ai version with None"
try:
# FIXME: handle empty version or stage to not overwrite entry in DB
self.client.update_model(self.name, version=sett_version, stage=stage)
log.info(f"Transitioned Model {self.name}:{sett_version} to stage:{stage}")
self.stage = stage
self.save(overwrite=True)
self.push()
except Exception as e:
traceback.print_exc()
log.info(f"Could not update model due to reason: {e}. \nMake sure you have pushed the model")
return self
def update_weights_path(self, weights_path: str):
"""Updates model weight file. Running this operation will increase the AI version.
Args:
weights_path: Path to a file or directory containing model data.
"""
if self.version is not None:
self.version += 1
else:
self.version = 1
self.weights_path = weights_path
self._location = self.save()
# self.push()
log.info(
f"AI version updated to {self.version} after updating weights. New version created in {self._location}. "
f"Make sure to AI.push to update in database"
)
# TODO Do we return a new AI object?
def update_ai_class(self, model_class: str, model_class_path="."):
"""Updates the model_class. Running this operation will increase the AI version.
Args:
model_class: Name of a subclass of :class:`~BaseModel`.
model_class_path: Path to :param model_class
"""
if self.version is not None:
self.version += 1
else:
self.version = 1
self.model_class = None
self.model_class_name = model_class
self.model_class_path = model_class_path
self._location = self.save(overwrite=True)
# self.push()
log.info(
f"AI version updated to {self.version} after updating AI class. New version created in {self._location}. "
f"Make sure to AI.push to update in database."
)
def update(
self,
version: Optional[int] = None,
stage: Optional[str] = None,
weights_path: Optional[str] = None,
ai_class: Optional[str] = None,
ai_class_path: str = ".",
):
"""
Updates the AI.
Args:
version: New AI version number. If the version number already exists, this method will fail.
stage: New AI stage.
weights_path: New path to a file or directory containing model data.
ai_class: Name of a subclass of :class:`~BaseModel`.
ai_class_path: Path to :param ai_class_path
"""
models = self.client.get_model_by_name(self.name)
if version is None:
log.info(f"Version not specified, checking version: {self.version}")
version = self.version
if len(models) == 0:
raise Exception(
f"No models found with the name: {self.name}:{self.version}. "
f"Make sure you have 'AI.push'ed some models. "
)
elif version in [x["version"] for x in models]:
raise Exception(
f"Model name and version already exists. Try updating the version number. "
f"Hint: Try version {self.client.get_latest_version_of_model_by_name(self.name) + 1}"
)
else:
kwargs = {}
if stage is not None:
kwargs["stage"] = stage
if weights_path is not None:
kwargs["weights_path"] = weights_path
if kwargs != {}:
self.version = version
self.weights_path = weights_path
self._location = self.save()
log.info(f"Updated model {self.name}:{self.version}. " f"Make sure to AI.push to update the database")
if ai_class is not None:
self.model_class = None
self.model_class_name = ai_class
self.model_class_path = ai_class_path
self.save(overwrite=True)
log.info("AI.update complete!")
self.push()
def predict(self, inputs: Union[TaskInput, List[dict]]) -> List[TaskPredictionInstance]:
"""Predicts from model_class and ensures that predict method adheres to schema in ai_definition.
Args:
inputs
Returns:
List of TaskPredictions
Each TaskPredictionInstance corresponds to a single prediction instance.
Models can output multiple instances per input.
"""
self._init_model_class(load_weights=True)
if not isinstance(inputs, TaskInput):
try:
TaskInput.parse_obj(inputs)
except ValidationError as e:
log.warning(
f"AI input could not be parsed as TaskInput. This could be enforced in future versions! {e}"
)
output = self.model_class.predict(inputs)
result = TaskPredictionInstance.validate_prediction(output)
return result
def predict_batch(self, inputs: Union[List[List[dict]], TaskBatchInput]) -> List[List[TaskPredictionInstance]]:
"""Predicts a batch of inputs from model_class and ensures that predict method adheres to schema in ai_definition.
Args:
inputs
Returns:
Batch of lists of TaskPredictions
Each TaskPredictionInstance corresponds to a single prediction instance.
For each input in the batch we expect a list of prediction instances.
"""
self._init_model_class(load_weights=True)
if not isinstance(inputs, TaskBatchInput):
try:
TaskBatchInput.parse_obj(inputs)
except ValidationError as e:
log.warning(
f"AI input could not be parsed as TaskBatchInput. This could be enforced in future versions! {e}"
)
batch = self.model_class.predict_batch(inputs)
result = TaskPredictionInstance.validate_prediction_batch(batch)
return result
def save(self, path: str = ".AISave", overwrite: bool = False):
"""Saves the model locally.
Args:
path:
overwrite:
"""
save_path = os.path.join(path, self.name)
os.makedirs(save_path, exist_ok=True)
if self.version is None:
version = "0"
else:
version = str(self.version)
version_save_path = os.path.join(save_path, version)
if not os.path.exists(version_save_path):
os.makedirs(version_save_path)
elif overwrite:
log.info(f"Removing existing content from path: {version_save_path}")
shutil.rmtree(version_save_path)
os.makedirs(version_save_path)
self.ai_template.save(version_save_path)
# save file information
with open(os.path.join(version_save_path, "AISaveFile.json"), "w") as ai_file_writer:
content = {
"input_params": self.input_params.to_json,
"output_params": self.output_params.to_json,
"configuration": self.configuration,
"name": self.name,
"version": self.version,
"stage": self.stage,
"description": self.description,
"weights_path": self.weights_path,
}
json.dump(content, ai_file_writer, indent=1)
log.info(f"Saved model in {version_save_path}")
return version_save_path
@retry(Exception, tries=5, delay=0.5, backoff=1)
def _upload_tarfile(self, upload_info, path):
log.info(f"Uploading file at {path} to {upload_info}")
with open(path, "rb") as f:
files = {"file": (path, f)}
upload_response = requests.post(upload_info["url"], data=upload_info["fields"], files=files)
if upload_response.status_code == 204:
log.info("Upload complete successfully")
else:
raise Exception(
f"Could not upload file {upload_response.status_code}: {upload_response.reason}\n"
f"{upload_response.text} "
)
@staticmethod
def _compress_folder(path_to_tarfile: str, location: str):
"""Helper to compress a directory into a tarfile
Args:
path_to_tarfile: Path to file to be generated after compressing
location: Path to folder to be compressed
"""
assert path_to_tarfile.endswith(".tar.gz"), "Should be a valid tarfile path"
with tarfile.open(path_to_tarfile, "w:gz") as tar:
for ff in os.listdir(location):
tar.add(os.path.join(location, ff), ff)
# tar.list()
assert os.path.exists(path_to_tarfile)
def push(self, update_weights: bool = False, weights_path: Optional[str] = None, overwrite=False) -> str:
"""Pushes the saved model to S3, creates an entry and enters the S3 URI in the database.
Args:
update_weights: Update weights in s3 or not
weights_path: Path to weights in s3
overwrite: Overwrite existing entry
"""
if self.id:
if not overwrite:
log.warning("Model already exists in the DB and overwrite is not set.")
return self.id
else:
if settings.current_env == "prod":
confirmed = Confirm.ask(
"Do you [bold]really[/bold] want to push weights for a [red]production[/red] AI? This can negatively impact Data Programs relying on the existing AI."
)
if not confirmed:
log.warning("Aborting push")
raise ModelDeploymentError("Push aborted by User")
else:
if self.version > 1:
self.root_id = self.root_id or find_root_model(self.name, self.client)
if self.root_id is None:
raise ValueError(
"AIs with version > 1 must have a root_id. This should be the ID of the AI with version=1."
)
self._id = self._create_database_entry(
name=self.name,
version=self.version,
description=self.description,
metadata=self.artifacts,
input_schema=self.input_params.to_json,
output_schema=self.output_params.to_json,
root_id=self.root_id,
deployment_parameters=self.ai_template.deployment_parameters,
)
modelSavePath = self._upload_model_folder(self.id)
weights = self._upload_weights(self.id, update_weights, weights_path)
self.client.update_model(
self.id,
weights_path=weights,
model_save_path=modelSavePath,
deployment_parameters=self.ai_template.deployment_parameters,
)
return self.id
def _upload_model_folder(self, idx: str) -> str:
s3_client = boto3.client("s3")
path_to_tarfile = os.path.join(self._location, "AISavedModel.tar.gz")
log.info(f"Compressing AI folder at {self._location}")
self._compress_folder(path_to_tarfile, self._location)
object_name = os.path.join(MODEL_ARTIFACT_PREFIX_S3, idx, self.name, str(self.version), "AISavedModel.tar.gz")
with open(path_to_tarfile, "rb") as f:
s3_client.upload_fileobj(f, self.bucket_name, object_name)
modelSavePath = os.path.join("s3://", self.bucket_name, object_name)
log.info(f"Uploaded AI object to '{modelSavePath}'")
return modelSavePath
def _upload_training_data(self, local_directory: str, training_id: str) -> str:
training_data_path = os.path.join("training/data", training_id)
# TODO: replace s3 push with push via signed url or similar
local_directory_path = Path(local_directory)
upload_dir(local_directory_path, training_data_path, self.bucket_name, prefix="/")
log.info(f"Uploaded Training data to {training_data_path}.")
return training_data_path
def _upload_weights(self, idx: str, update_weights: bool, weights_path: Optional[str]) -> Optional[str]:
weights: Optional[str] = None
if self.weights_path is not None and update_weights:
if self.weights_path.startswith("s3"):
weights = self.weights_path
elif os.path.exists(self.weights_path):
if os.path.isdir(self.weights_path):
upload_object_name = os.path.join(MODEL_ARTIFACT_PREFIX_S3, MODEL_WEIGHT_INFIX_S3, idx)
else:
raise ValueError("weights_path must be a directory")
log.info("Uploading weights...")
upload_dir(self.weights_path, upload_object_name, self.bucket_name, prefix="/")
weights = f"s3://{os.path.join(self.bucket_name, upload_object_name)}"
log.info(f"Uploaded weights to '{weights}'")
else:
log.warning("Weights path provided were invalid, weights will not be uploaded")
else:
log.warning("No weights path given, weights will not be uploaded")
if weights_path is not None:
weights = weights_path
return weights
def deploy(
self,
orchestrator: Union[str, "Orchestrator"] = Orchestrator.LOCAL_DOCKER,
skip_build: bool = False,
properties: Optional[Union[dict, AiDeploymentParameters]] = None,
enable_cuda: bool = False,
enable_eia: bool = False,
cuda_devel: bool = False,
redeploy: bool = False,
build_all_layers: bool = False,
download_base: bool = False,
use_internal: bool = False,
) -> "DeployedPredictor":
"""
Deploys the model to the specified orchestrator.
Args:
orchestrator: Which orchestrator to be used to deploy.
skip_build: Skip building
enable_cuda: Create CUDA-Compatible image
enable_eia: Create Elastic Inference compatible image
cuda_devel: Create development CUDA image
properties: Optional variable to override hardware and scaling deployment parameters from the AI template.
redeploy: Allow un-deploying existing deployment and replacing it.
build_all_layers: Perform a fresh build of all layers
download_base: Always download the base image to get the latest version from ECR
use_internal: Use internal development base image. Only accessible for super.AI developers.
"""
if redeploy and settings.current_env == "prod":
confirmed = Confirm.ask(
"Do you [bold]really[/bold] want to redeploy a [red]production[/red] AI? "
"This can negatively impact Data Programs relying on the existing AI."
)
if not confirmed:
log.warning("Aborting deployment")
raise ModelDeploymentError("Deployment aborted by User")
if isinstance(orchestrator, str):
try:
orchestrator = Orchestrator[orchestrator]
except KeyError:
raise ValueError(
f"Unknown orchestrator: {orchestrator}. Try one of: {', '.join(Orchestrator.__members__.values())}"
)
deployment_parameters = self._merge_deployment_parameters(properties, enable_cuda)
# Build image and compile deployment properties
local_image_name, global_image_name = self.build(
orchestrator,
enable_eia=enable_eia,
skip_build=skip_build,
cuda_devel=cuda_devel,
use_internal=use_internal,
deployment_parameters=deployment_parameters,
build_all_layers=build_all_layers,
download_base=download_base,
)
if PredictorFactory.is_remote(orchestrator):
if not skip_build:
self.push_model(image_name=local_image_name)
predictor_obj: DeployedPredictor = PredictorFactory.get_predictor_obj(
orchestrator=orchestrator,
local_image_name=local_image_name,
deploy_properties=deployment_parameters,
weights_path=self.weights_path,
client=self.client,
ai=self,
)
predictor_obj.deploy(redeploy=redeploy)
return predictor_obj
def _merge_deployment_parameters(self, properties: Union[dict, AiDeploymentParameters], enable_cuda):
"""
Merges deployment parameters from the AI template and the user-provided deployment parameters.
Precedence is given to the user-provided deployment parameters.
Args:
properties: Optional variable to override hardware and scaling deployment parameters from the AI template.
enable_cuda: Legacy kwarg for backwards compatibility.
Returns:
"""
# Parse properties into DeploymentParameters object
properties = AiDeploymentParameters.parse_from_optional(properties)
if enable_cuda:
log.warning("enable_cuda is deprecated and will be removed in future versions. Use `properties` instead.")
properties.enable_cuda = enable_cuda
# Merge properties with deployment parameters from AI template with precedence to properties
template_deployment_parameters = self.ai_template.deployment_parameters.dict_for_db()
template_deployment_parameters.update(properties.dict_for_db())
deployment_parameters = AiDeploymentParameters.parse_obj(template_deployment_parameters)
return deployment_parameters
def build(
self,
orchestrator: BaseAIOrchestrator,
deployment_parameters: Optional[AiDeploymentParameters] = None,
enable_eia: bool = False,
skip_build=False,
cuda_devel=False,
use_internal=False,
build_all_layers=False,
download_base=False,
) -> Tuple[str, str]:
"""
Build the image and return the image name.
Args:
orchestrator:
deployment_parameters: Optional deployment parameters to override the default deployment parameters.
enable_eia:
skip_build: Skip building and return the image name which would be built.
cuda_devel:
use_internal:
Use the internal development base image
build_all_layers: Force a fresh build of all layers
download_base: Force redownload of the base image
Returns:
Tuple[str,str]: The local image name and the global image name.
"""
image_builder = AiImageBuilder(
orchestrator,
entrypoint_class=self.ai_template.model_class,
location=self._location,
name=self.name,
version=self.version,
environs=self.environs,
requirements=self.requirements,
conda_env=self.conda_env,
artifacts=self.artifacts,
deployment_parameters=deployment_parameters or self.ai_template.deployment_parameters,
)
if skip_build:
local_image_name = image_builder.full_image_name(self.name, self.version)
else:
local_image_name = image_builder.build_image(
cuda_devel=cuda_devel,
enable_eia=enable_eia,
skip_build=skip_build,
use_internal=use_internal,
build_all_layers=build_all_layers,
download_base=download_base,
)
global_image_name, *_ = ecr_full_name(image_name=self.name, version=self.version, model_id=self.id)
return local_image_name, global_image_name
def undeploy(self) -> Optional[bool]:
if self.id:
if self.deployed:
passed = self.client.undeploy(self.deployment_id)
if passed:
log.info("Endpoint deletion request succeeded.")
# TODO: Add wait for deployment status to be OFFLINE
return passed
else:
log.info("Deployment already offline.")
return True
else:
log.info("No ID found. Is the model registered in the Database?")
return None
@classmethod
def load_api(cls, model_path, orchestrator: Orchestrator) -> "DeployedPredictor.Type":
ai_object: Optional["AI"] = cls.load(model_path)
assert ai_object is not None, "Need an AI object associated with the class to load"
# TODO: If we pass a model_path like `model://...`, check if there is an endpoint entry, and check
# if endpoint is serving.
# if endpoint is not serving:
return ai_object.deploy(orchestrator)
def push_model(self, image_name: Optional[str] = None, version: Optional[str] = None) -> str:
"""Push model in ECR, involves tagging and pushing.
Note that your default AWS credentials will be used for this.
Args:
image_name: of local build image
version: Version tag
Returns:
New tag of container pushed to ECR
"""
if image_name is None:
image_name = self.name
if version is None:
if ":" in image_name:
version = image_name.split(":")[1]
image_name = image_name.split(":")[0]