@@ -336,24 +336,18 @@ def _update_params_for_recommendation_id(
336
336
337
337
# Validate recommendation id
338
338
if not re .match (r"[a-zA-Z0-9](-*[a-zA-Z0-9]){0,63}\/\w{8}$" , inference_recommendation_id ):
339
- raise ValueError ("Inference Recommendation id is not valid" )
339
+ raise ValueError ("inference_recommendation_id is not valid" )
340
340
job_or_model_name = inference_recommendation_id .split ("/" )[0 ]
341
341
342
342
sage_client = self .sagemaker_session .sagemaker_client
343
-
344
- # Desribe inference recommendation job and model details
345
- recommendation_res , model_res = self ._describe_recommendation_job_and_model (
346
- sage_client = sage_client ,
347
- job_or_model_name = job_or_model_name ,
348
- )
349
-
350
- # Search the recommendation from above describe results
343
+ # Get recommendation from right size job and model
351
344
(
352
345
right_size_recommendation ,
353
346
model_recommendation ,
354
- ) = self ._get_right_size_and_model_recommendation (
355
- recommendation_res = recommendation_res ,
356
- model_res = model_res ,
347
+ right_size_job_res ,
348
+ ) = self ._get_recommendation (
349
+ sage_client = sage_client ,
350
+ job_or_model_name = job_or_model_name ,
357
351
inference_recommendation_id = inference_recommendation_id ,
358
352
)
359
353
@@ -374,7 +368,7 @@ def _update_params_for_recommendation_id(
374
368
"since they are in recommendation, or specify both of them if you want"
375
369
"to override the recommendation."
376
370
)
377
- input_config = recommendation_res ["InputConfig" ]
371
+ input_config = right_size_job_res ["InputConfig" ]
378
372
model_config = right_size_recommendation ["ModelConfiguration" ]
379
373
envs = (
380
374
model_config ["EnvironmentParameters" ]
@@ -498,56 +492,76 @@ def _convert_to_stopping_conditions_json(
498
492
]
499
493
return stopping_conditions
500
494
501
- def _get_right_size_and_model_recommendation (
502
- self ,
503
- model_res = None ,
504
- recommendation_res = None ,
505
- inference_recommendation_id = None ,
506
- ):
507
- """Get recommendation from right size job or model"""
508
- right_size_recommendation , model_recommendation = None , None
509
- if recommendation_res :
510
- right_size_recommendation = self ._get_recommendation (
511
- recommendation_list = recommendation_res ["InferenceRecommendations" ],
512
- inference_recommendation_id = inference_recommendation_id ,
513
- )
514
- if model_res :
515
- model_recommendation = self ._get_recommendation (
516
- recommendation_list = model_res ["DeploymentRecommendation" ][
517
- "RealTimeInferenceRecommendations"
518
- ],
495
+ def _get_recommendation (self , sage_client , job_or_model_name , inference_recommendation_id ):
496
+ """Get recommendation from right size job and model"""
497
+ right_size_recommendation , model_recommendation , right_size_job_res = None , None , None
498
+ right_size_recommendation , right_size_job_res = self ._get_right_size_recommendation (
499
+ sage_client = sage_client ,
500
+ job_or_model_name = job_or_model_name ,
501
+ inference_recommendation_id = inference_recommendation_id ,
502
+ )
503
+ if right_size_recommendation is None :
504
+ model_recommendation = self ._get_model_recommendation (
505
+ sage_client = sage_client ,
506
+ job_or_model_name = job_or_model_name ,
519
507
inference_recommendation_id = inference_recommendation_id ,
520
508
)
521
- if right_size_recommendation is None and model_recommendation is None :
522
- raise ValueError ("Inference Recommendation id is not valid" )
509
+ if model_recommendation is None :
510
+ raise ValueError ("inference_recommendation_id is not valid" )
523
511
524
- return right_size_recommendation , model_recommendation
512
+ return right_size_recommendation , model_recommendation , right_size_job_res
525
513
526
- def _get_recommendation (self , recommendation_list , inference_recommendation_id ):
527
- """Get recommendation based on recommendation id"""
528
- return next (
529
- (
530
- rec
531
- for rec in recommendation_list
532
- if rec ["RecommendationId" ] == inference_recommendation_id
533
- ),
534
- None ,
535
- )
536
-
537
- def _describe_recommendation_job_and_model (self , sage_client , job_or_model_name ):
538
- """Describe inference recommendation job and model results"""
539
- recommendation_res , model_res = None , None
514
+ def _get_right_size_recommendation (
515
+ self ,
516
+ sage_client ,
517
+ job_or_model_name ,
518
+ inference_recommendation_id ,
519
+ ):
520
+ """Get recommendation from right size job"""
521
+ right_size_recommendation , right_size_job_res = None , None
540
522
try :
541
- recommendation_res = sage_client .describe_inference_recommendations_job (
523
+ right_size_job_res = sage_client .describe_inference_recommendations_job (
542
524
JobName = job_or_model_name
543
525
)
526
+ if right_size_job_res :
527
+ right_size_recommendation = self ._search_recommendation (
528
+ recommendation_list = right_size_job_res ["InferenceRecommendations" ],
529
+ inference_recommendation_id = inference_recommendation_id ,
530
+ )
544
531
except sage_client .exceptions .ResourceNotFound :
545
532
pass
533
+
534
+ return right_size_recommendation , right_size_job_res
535
+
536
+ def _get_model_recommendation (
537
+ self ,
538
+ sage_client ,
539
+ job_or_model_name ,
540
+ inference_recommendation_id ,
541
+ ):
542
+ """Get recommendation from model"""
543
+ model_recommendation = None
546
544
try :
547
545
model_res = sage_client .describe_model (ModelName = job_or_model_name )
546
+ if model_res :
547
+ model_recommendation = self ._search_recommendation (
548
+ recommendation_list = model_res ["DeploymentRecommendation" ][
549
+ "RealTimeInferenceRecommendations"
550
+ ],
551
+ inference_recommendation_id = inference_recommendation_id ,
552
+ )
548
553
except sage_client .exceptions .ResourceNotFound :
549
554
pass
550
- if recommendation_res is None and model_res is None :
551
- raise ValueError ("Inference Recommendation id is not valid" )
552
555
553
- return recommendation_res , model_res
556
+ return model_recommendation
557
+
558
+ def _search_recommendation (self , recommendation_list , inference_recommendation_id ):
559
+ """Search recommendation based on recommendation id"""
560
+ return next (
561
+ (
562
+ rec
563
+ for rec in recommendation_list
564
+ if rec ["RecommendationId" ] == inference_recommendation_id
565
+ ),
566
+ None ,
567
+ )
0 commit comments