@@ -380,24 +380,18 @@ def _update_params_for_recommendation_id(
380
380
381
381
# Validate recommendation id
382
382
if not re .match (r"[a-zA-Z0-9](-*[a-zA-Z0-9]){0,63}\/\w{8}$" , inference_recommendation_id ):
383
- raise ValueError ("Inference Recommendation id is not valid" )
383
+ raise ValueError ("inference_recommendation_id is not valid" )
384
384
job_or_model_name = inference_recommendation_id .split ("/" )[0 ]
385
385
386
386
sage_client = self .sagemaker_session .sagemaker_client
387
-
388
- # Desribe inference recommendation job and model details
389
- recommendation_res , model_res = self ._describe_recommendation_job_and_model (
390
- sage_client = sage_client ,
391
- job_or_model_name = job_or_model_name ,
392
- )
393
-
394
- # Search the recommendation from above describe results
387
+ # Get recommendation from right size job and model
395
388
(
396
389
right_size_recommendation ,
397
390
model_recommendation ,
398
- ) = self ._get_right_size_and_model_recommendation (
399
- recommendation_res = recommendation_res ,
400
- model_res = model_res ,
391
+ right_size_job_res ,
392
+ ) = self ._get_recommendation (
393
+ sage_client = sage_client ,
394
+ job_or_model_name = job_or_model_name ,
401
395
inference_recommendation_id = inference_recommendation_id ,
402
396
)
403
397
@@ -418,7 +412,7 @@ def _update_params_for_recommendation_id(
418
412
"since they are in recommendation, or specify both of them if you want"
419
413
"to override the recommendation."
420
414
)
421
- input_config = recommendation_res ["InputConfig" ]
415
+ input_config = right_size_job_res ["InputConfig" ]
422
416
model_config = right_size_recommendation ["ModelConfiguration" ]
423
417
envs = (
424
418
model_config ["EnvironmentParameters" ]
@@ -540,56 +534,76 @@ def _convert_to_stopping_conditions_json(
540
534
]
541
535
return stopping_conditions
542
536
543
- def _get_right_size_and_model_recommendation (
544
- self ,
545
- model_res = None ,
546
- recommendation_res = None ,
547
- inference_recommendation_id = None ,
548
- ):
549
- """Get recommendation from right size job or model"""
550
- right_size_recommendation , model_recommendation = None , None
551
- if recommendation_res :
552
- right_size_recommendation = self ._get_recommendation (
553
- recommendation_list = recommendation_res ["InferenceRecommendations" ],
554
- inference_recommendation_id = inference_recommendation_id ,
555
- )
556
- if model_res :
557
- model_recommendation = self ._get_recommendation (
558
- recommendation_list = model_res ["DeploymentRecommendation" ][
559
- "RealTimeInferenceRecommendations"
560
- ],
537
+ def _get_recommendation (self , sage_client , job_or_model_name , inference_recommendation_id ):
538
+ """Get recommendation from right size job and model"""
539
+ right_size_recommendation , model_recommendation , right_size_job_res = None , None , None
540
+ right_size_recommendation , right_size_job_res = self ._get_right_size_recommendation (
541
+ sage_client = sage_client ,
542
+ job_or_model_name = job_or_model_name ,
543
+ inference_recommendation_id = inference_recommendation_id ,
544
+ )
545
+ if right_size_recommendation is None :
546
+ model_recommendation = self ._get_model_recommendation (
547
+ sage_client = sage_client ,
548
+ job_or_model_name = job_or_model_name ,
561
549
inference_recommendation_id = inference_recommendation_id ,
562
550
)
563
- if right_size_recommendation is None and model_recommendation is None :
564
- raise ValueError ("Inference Recommendation id is not valid" )
551
+ if model_recommendation is None :
552
+ raise ValueError ("inference_recommendation_id is not valid" )
565
553
566
- return right_size_recommendation , model_recommendation
554
+ return right_size_recommendation , model_recommendation , right_size_job_res
567
555
568
- def _get_recommendation (self , recommendation_list , inference_recommendation_id ):
569
- """Get recommendation based on recommendation id"""
570
- return next (
571
- (
572
- rec
573
- for rec in recommendation_list
574
- if rec ["RecommendationId" ] == inference_recommendation_id
575
- ),
576
- None ,
577
- )
578
-
579
- def _describe_recommendation_job_and_model (self , sage_client , job_or_model_name ):
580
- """Describe inference recommendation job and model results"""
581
- recommendation_res , model_res = None , None
556
+ def _get_right_size_recommendation (
557
+ self ,
558
+ sage_client ,
559
+ job_or_model_name ,
560
+ inference_recommendation_id ,
561
+ ):
562
+ """Get recommendation from right size job"""
563
+ right_size_recommendation , right_size_job_res = None , None
582
564
try :
583
- recommendation_res = sage_client .describe_inference_recommendations_job (
565
+ right_size_job_res = sage_client .describe_inference_recommendations_job (
584
566
JobName = job_or_model_name
585
567
)
568
+ if right_size_job_res :
569
+ right_size_recommendation = self ._search_recommendation (
570
+ recommendation_list = right_size_job_res ["InferenceRecommendations" ],
571
+ inference_recommendation_id = inference_recommendation_id ,
572
+ )
586
573
except sage_client .exceptions .ResourceNotFound :
587
574
pass
575
+
576
+ return right_size_recommendation , right_size_job_res
577
+
578
+ def _get_model_recommendation (
579
+ self ,
580
+ sage_client ,
581
+ job_or_model_name ,
582
+ inference_recommendation_id ,
583
+ ):
584
+ """Get recommendation from model"""
585
+ model_recommendation = None
588
586
try :
589
587
model_res = sage_client .describe_model (ModelName = job_or_model_name )
588
+ if model_res :
589
+ model_recommendation = self ._search_recommendation (
590
+ recommendation_list = model_res ["DeploymentRecommendation" ][
591
+ "RealTimeInferenceRecommendations"
592
+ ],
593
+ inference_recommendation_id = inference_recommendation_id ,
594
+ )
590
595
except sage_client .exceptions .ResourceNotFound :
591
596
pass
592
- if recommendation_res is None and model_res is None :
593
- raise ValueError ("Inference Recommendation id is not valid" )
594
597
595
- return recommendation_res , model_res
598
+ return model_recommendation
599
+
600
+ def _search_recommendation (self , recommendation_list , inference_recommendation_id ):
601
+ """Search recommendation based on recommendation id"""
602
+ return next (
603
+ (
604
+ rec
605
+ for rec in recommendation_list
606
+ if rec ["RecommendationId" ] == inference_recommendation_id
607
+ ),
608
+ None ,
609
+ )
0 commit comments