-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdeployment.py
1324 lines (1168 loc) · 54.8 KB
/
deployment.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
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
#!/usr/bin/env python
# Copyright (c) 2024, 2025 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
import json
import shlex
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Union
from cachetools import TTLCache, cached
from oci.data_science.models import ModelDeploymentShapeSummary
from pydantic import ValidationError
from ads.aqua.app import AquaApp, logger
from ads.aqua.common.entities import (
AquaMultiModelRef,
ComputeShapeSummary,
ContainerPath,
)
from ads.aqua.common.enums import InferenceContainerTypeFamily, ModelFormat, Tags
from ads.aqua.common.errors import AquaRuntimeError, AquaValueError
from ads.aqua.common.utils import (
DEFINED_METADATA_TO_FILE_MAP,
build_params_string,
build_pydantic_error_message,
get_combined_params,
get_container_params_type,
get_model_by_reference_paths,
get_ocid_substring,
get_params_dict,
get_params_list,
get_resource_name,
get_restricted_params_by_container,
load_gpu_shapes_index,
validate_cmd_var,
)
from ads.aqua.config.container_config import AquaContainerConfig, Usage
from ads.aqua.constants import (
AQUA_MODEL_ARTIFACT_FILE,
AQUA_MODEL_TYPE_CUSTOM,
AQUA_MODEL_TYPE_MULTI,
AQUA_MODEL_TYPE_SERVICE,
AQUA_MULTI_MODEL_CONFIG,
MODEL_BY_REFERENCE_OSS_PATH_KEY,
MODEL_NAME_DELIMITER,
UNKNOWN_DICT,
)
from ads.aqua.data import AquaResourceIdentifier
from ads.aqua.finetuning.finetuning import FineTuneCustomMetadata
from ads.aqua.model import AquaModelApp
from ads.aqua.model.constants import AquaModelMetadataKeys, ModelCustomMetadataFields
from ads.aqua.modeldeployment.entities import (
AquaDeployment,
AquaDeploymentConfig,
AquaDeploymentDetail,
ConfigurationItem,
ConfigValidationError,
CreateModelDeploymentDetails,
ModelDeploymentConfigSummary,
)
from ads.aqua.modeldeployment.utils import MultiModelDeploymentConfigLoader
from ads.common.object_storage_details import ObjectStorageDetails
from ads.common.utils import UNKNOWN, get_log_links
from ads.config import (
AQUA_DEPLOYMENT_CONTAINER_CMD_VAR_METADATA_NAME,
AQUA_DEPLOYMENT_CONTAINER_METADATA_NAME,
AQUA_DEPLOYMENT_CONTAINER_URI_METADATA_NAME,
COMPARTMENT_OCID,
PROJECT_OCID,
)
from ads.model.datascience_model import DataScienceModel
from ads.model.deployment import (
ModelDeployment,
ModelDeploymentContainerRuntime,
ModelDeploymentInfrastructure,
ModelDeploymentMode,
)
from ads.model.model_metadata import ModelCustomMetadataItem
from ads.telemetry import telemetry
class AquaDeploymentApp(AquaApp):
"""Provides a suite of APIs to interact with Aqua model deployments within the Oracle
Cloud Infrastructure Data Science service, serving as an interface for deploying
machine learning models.
Methods
-------
create(model_id: str, instance_shape: str, display_name: str,...) -> AquaDeployment
Creates a model deployment for Aqua Model.
get(model_deployment_id: str) -> AquaDeployment:
Retrieves details of an Aqua model deployment by its unique identifier.
list(**kwargs) -> List[AquaModelSummary]:
Lists all Aqua deployments within a specified compartment and/or project.
get_deployment_config(self, model_id: str) -> AquaDeploymentConfig:
Gets the deployment config of given Aqua model.
get_multimodel_deployment_config(self, model_ids: List[str],...) -> ModelDeploymentConfigSummary:
Retrieves the deployment configuration for multiple Aqua models and calculates
the GPU allocations for all compatible shapes.
list_shapes(self, **kwargs) -> List[Dict]:
Lists the valid model deployment shapes.
Note:
Use `ads aqua deployment <method_name> --help` to get more details on the parameters available.
This class is designed to work within the Oracle Cloud Infrastructure
and requires proper configuration and authentication set up to interact
with OCI services.
"""
@telemetry(entry_point="plugin=deployment&action=create", name="aqua")
def create(
self,
create_deployment_details: Optional[CreateModelDeploymentDetails] = None,
**kwargs,
) -> "AquaDeployment":
"""
Creates a new Aqua model deployment.\n
For detailed information about CLI flags see: https://github.com/oracle-samples/oci-data-science-ai-samples/blob/main/ai-quick-actions/cli-tips.md#create-model-deployment
Args:
create_deployment_details : CreateModelDeploymentDetails, optional
An instance of CreateModelDeploymentDetails containing all required and optional
fields for creating a model deployment via Aqua.
kwargs:
instance_shape (str): The instance shape used for deployment.
display_name (str): The name of the model deployment.
compartment_id (Optional[str]): The compartment OCID.
project_id (Optional[str]): The project OCID.
description (Optional[str]): The description of the deployment.
model_id (Optional[str]): The model OCID to deploy.
models (Optional[List[AquaMultiModelRef]]): List of models for multimodel deployment.
instance_count (int): Number of instances used for deployment.
log_group_id (Optional[str]): OCI logging group ID for logs.
access_log_id (Optional[str]): OCID for access logs.
predict_log_id (Optional[str]): OCID for prediction logs.
bandwidth_mbps (Optional[int]): Bandwidth limit on the load balancer in Mbps.
web_concurrency (Optional[int]): Number of worker processes/threads for handling requests.
server_port (Optional[int]): Server port for the Docker container image.
health_check_port (Optional[int]): Health check port for the Docker container image.
env_var (Optional[Dict[str, str]]): Environment variables for deployment.
container_family (Optional[str]): Image family of the model deployment container runtime.
memory_in_gbs (Optional[float]): Memory (in GB) for the selected shape.
ocpus (Optional[float]): OCPU count for the selected shape.
model_file (Optional[str]): File used for model deployment.
private_endpoint_id (Optional[str]): Private endpoint ID for model deployment.
container_image_uri (Optional[str]): Image URI for model deployment container runtime.
cmd_var (Optional[List[str]]): Command variables for the container runtime.
freeform_tags (Optional[Dict]): Freeform tags for model deployment.
defined_tags (Optional[Dict]): Defined tags for model deployment.
Returns
-------
AquaDeployment
An Aqua deployment instance.
"""
# Build deployment details from kwargs if not explicitly provided.
if create_deployment_details is None:
try:
create_deployment_details = CreateModelDeploymentDetails(**kwargs)
except ValidationError as ex:
custom_errors = build_pydantic_error_message(ex)
raise AquaValueError(
f"Invalid parameters for creating a model deployment. Error details: {custom_errors}."
) from ex
if not (create_deployment_details.model_id or create_deployment_details.models):
raise AquaValueError(
"Invalid parameters for creating a model deployment. Either `model_id` or `models` must be provided."
)
# Set defaults for compartment and project if not provided.
compartment_id = create_deployment_details.compartment_id or COMPARTMENT_OCID
project_id = create_deployment_details.project_id or PROJECT_OCID
freeform_tags = create_deployment_details.freeform_tags
defined_tags = create_deployment_details.defined_tags
# validate instance shape availability in compartment
available_shapes = [
shape.name.lower()
for shape in self.list_shapes(
compartment_id=compartment_id
)
]
if create_deployment_details.instance_shape.lower() not in available_shapes:
raise AquaValueError(
f"Invalid Instance Shape. The selected shape '{create_deployment_details.instance_shape}' "
f"is not available in the {self.region} region. Please choose another shape to deploy the model."
)
# Get container config
container_config = self.get_container_config()
# Create an AquaModelApp instance once to perform the deployment creation.
model_app = AquaModelApp()
if create_deployment_details.model_id:
logger.debug(
f"Single model ({create_deployment_details.model_id}) provided. "
"Delegating to single model creation method."
)
aqua_model = model_app.create(
model_id=create_deployment_details.model_id,
compartment_id=compartment_id,
project_id=project_id,
freeform_tags=freeform_tags,
defined_tags=defined_tags,
)
return self._create(
aqua_model=aqua_model,
create_deployment_details=create_deployment_details,
container_config=container_config,
)
else:
model_ids = [model.model_id for model in create_deployment_details.models]
try:
model_config_summary = self.get_multimodel_deployment_config(
model_ids=model_ids, compartment_id=compartment_id
)
if not model_config_summary.gpu_allocation:
raise AquaValueError(model_config_summary.error_message)
create_deployment_details.validate_multimodel_deployment_feasibility(
models_config_summary=model_config_summary
)
except ConfigValidationError as err:
raise AquaValueError(f"{err}") from err
service_inference_containers = container_config.inference.values()
supported_container_families = [
container_config_item.family
for container_config_item in service_inference_containers
if any(
usage.upper() in container_config_item.usages
for usage in [Usage.MULTI_MODEL, Usage.OTHER]
)
]
if not supported_container_families:
raise AquaValueError(
"Currently, there are no containers that support multi-model deployment."
)
# Check if provided container family supports multi-model deployment
if (
create_deployment_details.container_family
and create_deployment_details.container_family
not in supported_container_families
):
raise AquaValueError(
f"Unsupported deployment container '{create_deployment_details.container_family}'. "
f"Only {supported_container_families} families are supported for multi-model deployments."
)
# Verify if it matches one of the registered containers and attempt to
# extract the container family from there.
# If the container is not recognized, we can only issue a warning that
# the provided container may not support multi-model deployment.
if create_deployment_details.container_image_uri:
selected_container_name = ContainerPath(
full_path=create_deployment_details.container_image_uri
).name
container_config_item = next(
(
container_config_item
for container_config_item in service_inference_containers
if ContainerPath(
full_path=f"{container_config_item.name}:{container_config_item.version}"
).name.upper()
== selected_container_name.upper()
),
None,
)
if (
container_config_item
and container_config_item.family not in supported_container_families
):
raise AquaValueError(
f"Unsupported deployment container '{create_deployment_details.container_image_uri}'. "
f"Only {supported_container_families} families are supported for multi-model deployments."
)
if not container_config_item:
logger.warning(
f"The provided container `{create_deployment_details.container_image_uri}` may not support multi-model deployment. "
f"Only the following container families are supported: {supported_container_families}."
)
logger.debug(
f"Multi models ({model_ids}) provided. Delegating to multi model creation method."
)
aqua_model = model_app.create_multi(
models=create_deployment_details.models,
compartment_id=compartment_id,
project_id=project_id,
freeform_tags=freeform_tags,
defined_tags=defined_tags,
)
return self._create_multi(
aqua_model=aqua_model,
model_config_summary=model_config_summary,
create_deployment_details=create_deployment_details,
container_config=container_config,
)
def _create(
self,
aqua_model: DataScienceModel,
create_deployment_details: CreateModelDeploymentDetails,
container_config: Dict,
) -> AquaDeployment:
"""Builds the configurations required by single model deployment and creates the deployment.
Parameters
----------
aqua_model : DataScienceModel
An instance of Aqua data science model.
create_deployment_details : CreateModelDeploymentDetails
An instance of CreateModelDeploymentDetails containing all required and optional
fields for creating a model deployment via Aqua.
container_config: Dict
Container config dictionary.
Returns
-------
AquaDeployment
An Aqua deployment instance.
"""
tags = {}
for tag in [
Tags.AQUA_SERVICE_MODEL_TAG,
Tags.AQUA_FINE_TUNED_MODEL_TAG,
Tags.AQUA_TAG,
]:
if tag in aqua_model.freeform_tags:
tags[tag] = aqua_model.freeform_tags[tag]
tags.update({Tags.AQUA_MODEL_NAME_TAG: aqua_model.display_name})
tags.update({Tags.TASK: aqua_model.freeform_tags.get(Tags.TASK, UNKNOWN)})
# Set up info to get deployment config
config_source_id = create_deployment_details.model_id
model_name = aqua_model.display_name
is_fine_tuned_model = Tags.AQUA_FINE_TUNED_MODEL_TAG in aqua_model.freeform_tags
if is_fine_tuned_model:
try:
config_source_id = aqua_model.custom_metadata_list.get(
FineTuneCustomMetadata.FINE_TUNE_SOURCE
).value
model_name = aqua_model.custom_metadata_list.get(
FineTuneCustomMetadata.FINE_TUNE_SOURCE_NAME
).value
except ValueError as err:
raise AquaValueError(
f"Either {FineTuneCustomMetadata.FINE_TUNE_SOURCE} or {FineTuneCustomMetadata.FINE_TUNE_SOURCE_NAME} is missing "
f"from custom metadata for the model {config_source_id}"
) from err
# set up env and cmd var
env_var = create_deployment_details.env_var or {}
cmd_var = create_deployment_details.cmd_var or []
try:
model_path_prefix = aqua_model.custom_metadata_list.get(
MODEL_BY_REFERENCE_OSS_PATH_KEY
).value.rstrip("/")
except ValueError as err:
raise AquaValueError(
f"{MODEL_BY_REFERENCE_OSS_PATH_KEY} key is not available in the custom metadata field."
) from err
if ObjectStorageDetails.is_oci_path(model_path_prefix):
os_path = ObjectStorageDetails.from_path(model_path_prefix)
model_path_prefix = os_path.filepath.rstrip("/")
env_var.update({"BASE_MODEL": f"{model_path_prefix}"})
if is_fine_tuned_model:
_, fine_tune_output_path = get_model_by_reference_paths(
aqua_model.model_file_description
)
if not fine_tune_output_path:
raise AquaValueError(
"Fine tuned output path is not available in the model artifact."
)
os_path = ObjectStorageDetails.from_path(fine_tune_output_path)
fine_tune_output_path = os_path.filepath.rstrip("/")
env_var.update({"FT_MODEL": f"{fine_tune_output_path}"})
container_type_key = self._get_container_type_key(
model=aqua_model,
container_family=create_deployment_details.container_family,
)
container_image_uri = (
create_deployment_details.container_image_uri
or self.get_container_image(container_type=container_type_key)
)
if not container_image_uri:
try:
container_image_uri = aqua_model.custom_metadata_list.get(
AQUA_DEPLOYMENT_CONTAINER_URI_METADATA_NAME
).value
except ValueError as err:
raise AquaValueError(
f"{AQUA_DEPLOYMENT_CONTAINER_URI_METADATA_NAME} key is not available in the custom metadata "
f"field. Either re-register the model with custom container URI, or set container_image_uri "
f"parameter when creating this deployment."
) from err
logger.info(
f"Aqua Image used for deploying {aqua_model.id} : {container_image_uri}"
)
try:
cmd_var_string = aqua_model.custom_metadata_list.get(
AQUA_DEPLOYMENT_CONTAINER_CMD_VAR_METADATA_NAME
).value
default_cmd_var = shlex.split(cmd_var_string)
if default_cmd_var:
cmd_var = validate_cmd_var(default_cmd_var, cmd_var)
logger.info(f"CMD used for deploying {aqua_model.id} :{cmd_var}")
except ValueError:
logger.debug(
f"CMD will be ignored for this deployment as {AQUA_DEPLOYMENT_CONTAINER_CMD_VAR_METADATA_NAME} "
f"key is not available in the custom metadata field for this model."
)
except Exception as e:
logger.error(
f"There was an issue processing CMD arguments. Error: {str(e)}"
)
model_formats_str = aqua_model.freeform_tags.get(
Tags.MODEL_FORMAT, ModelFormat.SAFETENSORS
).upper()
model_format = model_formats_str.split(",")
# Figure out a better way to handle this in future release
if (
ModelFormat.GGUF in model_format
and container_type_key.lower()
== InferenceContainerTypeFamily.AQUA_LLAMA_CPP_CONTAINER_FAMILY
):
model_file = create_deployment_details.model_file
if model_file is not None:
logger.info(
f"Overriding {model_file} as model_file for model {aqua_model.id}."
)
else:
try:
model_file = aqua_model.custom_metadata_list.get(
AQUA_MODEL_ARTIFACT_FILE
).value
except ValueError as err:
raise AquaValueError(
f"{AQUA_MODEL_ARTIFACT_FILE} key is not available in the custom metadata field "
f"for model {aqua_model.id}. Either register the model with a default model_file or pass "
f"as a parameter when creating a deployment."
) from err
env_var.update({"BASE_MODEL_FILE": f"{model_file}"})
tags.update({Tags.MODEL_ARTIFACT_FILE: model_file})
# Fetch the startup cli command for the container
# container_index.json will have "containerSpec" section which will provide the cli params for
# a given container family
container_config = self.get_container_config_item(container_type_key)
container_spec = container_config.spec if container_config else UNKNOWN
# these params cannot be overridden for Aqua deployments
params = container_spec.cli_param if container_spec else UNKNOWN
server_port = create_deployment_details.server_port or (
container_spec.server_port if container_spec else None
)
# Give precendece to the input parameter
health_check_port = create_deployment_details.health_check_port or (
container_spec.health_check_port if container_spec else None
)
deployment_config = self.get_deployment_config(model_id=config_source_id)
config_params = deployment_config.configuration.get(
create_deployment_details.instance_shape, ConfigurationItem()
).parameters.get(get_container_params_type(container_type_key), UNKNOWN)
# validate user provided params
user_params = env_var.get("PARAMS", UNKNOWN)
if user_params:
# todo: remove this check in the future version, logic to be moved to container_index
if (
container_type_key.lower()
== InferenceContainerTypeFamily.AQUA_LLAMA_CPP_CONTAINER_FAMILY
):
# AQUA_LLAMA_CPP_CONTAINER_FAMILY container uses uvicorn that required model/server params
# to be set as env vars
raise AquaValueError(
f"Currently, parameters cannot be overridden for the container: {container_image_uri}. Please proceed "
f"with deployment without parameter overrides."
)
restricted_params = self._find_restricted_params(
params, user_params, container_type_key
)
if restricted_params:
raise AquaValueError(
f"Parameters {restricted_params} are set by Aqua "
f"and cannot be overridden or are invalid."
)
deployment_params = get_combined_params(config_params, user_params)
params = f"{params} {deployment_params}".strip()
if params:
env_var.update({"PARAMS": params})
env_vars = container_spec.env_vars if container_spec else []
for env in env_vars:
if isinstance(env, dict):
env = {k: v for k, v in env.items() if v}
for key, _ in env.items():
if key not in env_var:
env_var.update(env)
logger.info(f"Env vars used for deploying {aqua_model.id} :{env_var}")
tags = {**tags, **(create_deployment_details.freeform_tags or {})}
model_type = (
AQUA_MODEL_TYPE_CUSTOM if is_fine_tuned_model else AQUA_MODEL_TYPE_SERVICE
)
return self._create_deployment(
create_deployment_details=create_deployment_details,
aqua_model_id=aqua_model.id,
model_name=model_name,
model_type=model_type,
container_image_uri=container_image_uri,
server_port=server_port,
health_check_port=health_check_port,
env_var=env_var,
tags=tags,
cmd_var=cmd_var,
)
def _create_multi(
self,
aqua_model: DataScienceModel,
model_config_summary: ModelDeploymentConfigSummary,
create_deployment_details: CreateModelDeploymentDetails,
container_config: AquaContainerConfig,
) -> AquaDeployment:
"""Builds the environment variables required by multi deployment container and creates the deployment.
Parameters
----------
model_config_summary : model_config_summary
Summary Model Deployment configuration for the group of models.
aqua_model : DataScienceModel
An instance of Aqua data science model.
create_deployment_details : CreateModelDeploymentDetails
An instance of CreateModelDeploymentDetails containing all required and optional
fields for creating a model deployment via Aqua.
container_config: Dict
Container config dictionary.
Returns
-------
AquaDeployment
An Aqua deployment instance.
"""
model_config = []
model_name_list = []
env_var = {**(create_deployment_details.env_var or UNKNOWN_DICT)}
container_type_key = self._get_container_type_key(
model=aqua_model,
container_family=create_deployment_details.container_family,
)
container_config = self.get_container_config_item(container_type_key)
container_spec = container_config.spec if container_config else UNKNOWN
container_params = container_spec.cli_param if container_spec else UNKNOWN
for model in create_deployment_details.models:
user_params = build_params_string(model.env_var)
if user_params:
restricted_params = self._find_restricted_params(
container_params, user_params, container_type_key
)
if restricted_params:
selected_model = model.model_name or model.model_id
raise AquaValueError(
f"Parameters {restricted_params} are set by Aqua "
f"and cannot be overridden or are invalid."
f"Select other parameters for model {selected_model}."
)
# replaces `--served-model-name`` with user's model name
container_params_dict = get_params_dict(container_params)
container_params_dict.update({"--served-model-name": model.model_name})
# replaces `--tensor-parallel-size` with model gpu count
container_params_dict.update({"--tensor-parallel-size": model.gpu_count})
params = build_params_string(container_params_dict)
deployment_config = model_config_summary.deployment_config.get(
model.model_id, AquaDeploymentConfig()
).configuration.get(
create_deployment_details.instance_shape, ConfigurationItem()
)
# finds the corresponding deployment parameters based on the gpu count
# and combines them with user's parameters. Existing deployment parameters
# will be overriden by user's parameters.
params_found = False
for item in deployment_config.multi_model_deployment:
if (
model.gpu_count
and item.gpu_count
and item.gpu_count == model.gpu_count
):
config_parameters = item.parameters.get(
get_container_params_type(container_type_key), UNKNOWN
)
params = f"{params} {get_combined_params(config_parameters, user_params)}".strip()
params_found = True
break
if not params_found and deployment_config.parameters:
config_parameters = deployment_config.parameters.get(
get_container_params_type(container_type_key), UNKNOWN
)
params = f"{params} {get_combined_params(config_parameters, user_params)}".strip()
params_found = True
# if no config parameters found, append user parameters directly.
if not params_found:
params = f"{params} {user_params}".strip()
artifact_path_prefix = model.artifact_location.rstrip("/")
if ObjectStorageDetails.is_oci_path(artifact_path_prefix):
os_path = ObjectStorageDetails.from_path(artifact_path_prefix)
artifact_path_prefix = os_path.filepath.rstrip("/")
model_config.append({"params": params, "model_path": artifact_path_prefix})
model_name_list.append(model.model_name)
env_var.update({AQUA_MULTI_MODEL_CONFIG: json.dumps({"models": model_config})})
env_vars = container_spec.env_vars if container_spec else []
for env in env_vars:
if isinstance(env, dict):
env = {k: v for k, v in env.items() if v}
for key, _ in env.items():
if key not in env_var:
env_var.update(env)
logger.info(f"Env vars used for deploying {aqua_model.id} : {env_var}.")
container_image_uri = (
create_deployment_details.container_image_uri
or self.get_container_image(container_type=container_type_key)
)
server_port = create_deployment_details.server_port or (
container_spec.server_port if container_spec else None
)
health_check_port = create_deployment_details.health_check_port or (
container_spec.health_check_port if container_spec else None
)
tags = {
Tags.AQUA_MODEL_ID_TAG: aqua_model.id,
Tags.MULTIMODEL_TYPE_TAG: "true",
Tags.AQUA_TAG: "active",
**(create_deployment_details.freeform_tags or UNKNOWN_DICT),
}
model_name = f"{MODEL_NAME_DELIMITER} ".join(model_name_list)
aqua_deployment = self._create_deployment(
create_deployment_details=create_deployment_details,
aqua_model_id=aqua_model.id,
model_name=model_name,
model_type=AQUA_MODEL_TYPE_MULTI,
container_image_uri=container_image_uri,
server_port=server_port,
health_check_port=health_check_port,
env_var=env_var,
tags=tags,
)
aqua_deployment.models = create_deployment_details.models
return aqua_deployment
def _create_deployment(
self,
create_deployment_details: CreateModelDeploymentDetails,
aqua_model_id: str,
model_name: str,
model_type: str,
container_image_uri: str,
server_port: str,
health_check_port: str,
env_var: dict,
tags: dict,
cmd_var: Optional[dict] = None,
):
"""Creates data science model deployment.
Parameters
----------
create_deployment_details : CreateModelDeploymentDetails
An instance of CreateModelDeploymentDetails containing all required and optional
fields for creating a model deployment via Aqua.
aqua_model_id: str
The id of the aqua model to be deployed.
model_name: str
The name of the aqua model to be deployed. If it's multi model deployment, it is a list of model names.
model_type: str
The type of aqua model to be deployed. Allowed values are: `custom`, `service` and `multi_model`.
container_image_uri: str
The container image uri to deploy the model.
server_port: str
The service port of the container image.
health_check_port: str
The health check port of the container image.
env_var: dict
The environment variables input for the deployment.
tags: dict
The tags input for the deployment.
cmd_var: dict, optional
The cmd arguments input for the deployment.
Returns
-------
AquaDeployment
An Aqua deployment instance.
"""
# Start model deployment
# configure model deployment infrastructure
infrastructure = (
ModelDeploymentInfrastructure()
.with_project_id(create_deployment_details.project_id or PROJECT_OCID)
.with_compartment_id(
create_deployment_details.compartment_id or COMPARTMENT_OCID
)
.with_shape_name(create_deployment_details.instance_shape)
.with_bandwidth_mbps(create_deployment_details.bandwidth_mbps)
.with_replica(create_deployment_details.instance_count)
.with_web_concurrency(create_deployment_details.web_concurrency)
.with_private_endpoint_id(create_deployment_details.private_endpoint_id)
.with_access_log(
log_group_id=create_deployment_details.log_group_id,
log_id=create_deployment_details.access_log_id,
)
.with_predict_log(
log_group_id=create_deployment_details.log_group_id,
log_id=create_deployment_details.predict_log_id,
)
)
if (
create_deployment_details.memory_in_gbs
and create_deployment_details.ocpus
and infrastructure.shape_name.endswith("Flex")
):
infrastructure.with_shape_config_details(
ocpus=create_deployment_details.ocpus,
memory_in_gbs=create_deployment_details.memory_in_gbs,
)
# configure model deployment runtime
container_runtime = (
ModelDeploymentContainerRuntime()
.with_image(container_image_uri)
.with_server_port(server_port)
.with_health_check_port(health_check_port)
.with_env(env_var)
.with_deployment_mode(ModelDeploymentMode.HTTPS)
.with_model_uri(aqua_model_id)
.with_region(self.region)
.with_overwrite_existing_artifact(True)
.with_remove_existing_artifact(True)
)
if cmd_var:
container_runtime.with_cmd(cmd_var)
# configure model deployment and deploy model on container runtime
deployment = (
ModelDeployment()
.with_display_name(create_deployment_details.display_name)
.with_description(create_deployment_details.description)
.with_freeform_tags(**tags)
.with_defined_tags(**(create_deployment_details.defined_tags or {}))
.with_infrastructure(infrastructure)
.with_runtime(container_runtime)
).deploy(wait_for_completion=False)
deployment_id = deployment.id
logger.info(
f"Aqua model deployment {deployment_id} created for model {aqua_model_id}."
)
# we arbitrarily choose last 8 characters of OCID to identify MD in telemetry
telemetry_kwargs = {"ocid": get_ocid_substring(deployment_id, key_len=8)}
# tracks unique deployments that were created in the user compartment
self.telemetry.record_event_async(
category=f"aqua/{model_type}/deployment",
action="create",
detail=model_name,
**telemetry_kwargs,
)
# tracks the shape used for deploying the custom or service models by name
self.telemetry.record_event_async(
category=f"aqua/{model_type}/deployment/create",
action="shape",
detail=create_deployment_details.instance_shape,
value=model_name,
)
return AquaDeployment.from_oci_model_deployment(
deployment.dsc_model_deployment, self.region
)
@staticmethod
def _get_container_type_key(model: DataScienceModel, container_family: str) -> str:
container_type_key = UNKNOWN
if container_family:
container_type_key = container_family
else:
try:
container_type_key = model.custom_metadata_list.get(
AQUA_DEPLOYMENT_CONTAINER_METADATA_NAME
).value
except ValueError as err:
raise AquaValueError(
f"{AQUA_DEPLOYMENT_CONTAINER_METADATA_NAME} key is not available in the custom metadata field "
f"for model {model.id}. For unverified Aqua models, {AQUA_DEPLOYMENT_CONTAINER_METADATA_NAME} should be"
f"set and value can be one of {', '.join(InferenceContainerTypeFamily.values())}."
) from err
return container_type_key
@telemetry(entry_point="plugin=deployment&action=list", name="aqua")
def list(self, **kwargs) -> List["AquaDeployment"]:
"""List Aqua model deployments in a given compartment and under certain project.
Parameters
----------
kwargs
Keyword arguments, such as compartment_id and project_id,
for `list_call_get_all_results <https://docs.oracle.com/en-us/iaas/tools/python/2.118.1/api/pagination.html#oci.pagination.list_call_get_all_results>`_
Returns
-------
List[AquaDeployment]:
The list of the Aqua model deployments.
"""
compartment_id = kwargs.pop("compartment_id", COMPARTMENT_OCID)
model_deployments = self.list_resource(
self.ds_client.list_model_deployments,
compartment_id=compartment_id,
**kwargs,
)
results = []
for model_deployment in model_deployments:
oci_aqua = (
(
Tags.AQUA_TAG in model_deployment.freeform_tags
or Tags.AQUA_TAG.lower() in model_deployment.freeform_tags
)
if model_deployment.freeform_tags
else False
)
if oci_aqua:
results.append(
AquaDeployment.from_oci_model_deployment(
model_deployment, self.region
)
)
# log telemetry if MD is in active or failed state
deployment_id = model_deployment.id
state = model_deployment.lifecycle_state.upper()
if state in ["ACTIVE", "FAILED"]:
# tracks unique deployments that were listed in the user compartment
# we arbitrarily choose last 8 characters of OCID to identify MD in telemetry
self.telemetry.record_event_async(
category="aqua/deployment",
action="list",
detail=get_ocid_substring(deployment_id, key_len=8),
value=state,
)
logger.info(
f"Fetched {len(results)} model deployments from compartment_id={compartment_id}."
)
# tracks number of times deployment listing was called
self.telemetry.record_event_async(category="aqua/deployment", action="list")
return results
@telemetry(entry_point="plugin=deployment&action=delete", name="aqua")
def delete(self, model_deployment_id: str):
logger.info(f"Deleting model deployment {model_deployment_id}.")
return self.ds_client.delete_model_deployment(
model_deployment_id=model_deployment_id
).data
@telemetry(entry_point="plugin=deployment&action=deactivate", name="aqua")
def deactivate(self, model_deployment_id: str):
logger.info(f"Deactivating model deployment {model_deployment_id}.")
return self.ds_client.deactivate_model_deployment(
model_deployment_id=model_deployment_id
).data
@telemetry(entry_point="plugin=deployment&action=activate", name="aqua")
def activate(self, model_deployment_id: str):
logger.info(f"Activating model deployment {model_deployment_id}.")
return self.ds_client.activate_model_deployment(
model_deployment_id=model_deployment_id
).data
@telemetry(entry_point="plugin=deployment&action=get", name="aqua")
def get(self, model_deployment_id: str, **kwargs) -> "AquaDeploymentDetail":
"""Gets the information of Aqua model deployment.
Parameters
----------
model_deployment_id: str
The OCID of the Aqua model deployment.
kwargs
Keyword arguments, for `get_model_deployment
<https://docs.oracle.com/en-us/iaas/tools/python/2.119.1/api/data_science/client/oci.data_science.DataScienceClient.html#oci.data_science.DataScienceClient.get_model_deployment>`_
Returns
-------
AquaDeploymentDetail:
The instance of the Aqua model deployment details.
"""
logger.info(f"Fetching model deployment details for {model_deployment_id}.")
model_deployment = self.ds_client.get_model_deployment(
model_deployment_id=model_deployment_id, **kwargs
).data
oci_aqua = (
(
Tags.AQUA_TAG in model_deployment.freeform_tags
or Tags.AQUA_TAG.lower() in model_deployment.freeform_tags
)
if model_deployment.freeform_tags
else False
)
if not oci_aqua:
raise AquaRuntimeError(
f"Target deployment {model_deployment_id} is not Aqua deployment as it does not contain "
f"{Tags.AQUA_TAG} tag."
)
log_id = ""
log_group_id = ""
log_name = ""
log_group_name = ""
logs = (
model_deployment.category_log_details.access
or model_deployment.category_log_details.predict
)
if logs:
log_id = logs.log_id
log_group_id = logs.log_group_id
if log_id:
log_name = get_resource_name(log_id)
if log_group_id:
log_group_name = get_resource_name(log_group_id)
log_group_url = get_log_links(region=self.region, log_group_id=log_group_id)
log_url = get_log_links(
region=self.region,
log_group_id=log_group_id,
log_id=log_id,
compartment_id=model_deployment.compartment_id,
source_id=model_deployment.id,
)
aqua_deployment = AquaDeployment.from_oci_model_deployment(
model_deployment, self.region
)
if Tags.MULTIMODEL_TYPE_TAG in model_deployment.freeform_tags:
aqua_model_id = model_deployment.freeform_tags.get(
Tags.AQUA_MODEL_ID_TAG, UNKNOWN
)
if not aqua_model_id:
raise AquaRuntimeError(