
    )                     x   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ej                   ej                  ej                  j                         G d dej                                      Zej                   ej                  ej                  j                          G d	 d
ej                                      Zy)zFGenerates optimized Kubernetes manifests for GKE Inference Quickstart.    )util)base)
exceptions)log)filesc                   ,    e Zd ZdZed        Zd Zd Zy)Create~  Generate ready-to-deploy Kubernetes manifests with compute, load balancing, and autoscaling capabilities.

  To get supported model, model servers, and model server versions, run `gcloud
  alpha container ai profiles model-and-server-combinations list`. To get
  supported accelerators with their performance metrics, run `gcloud alpha
  container ai profiles accelerators list`.
  c                    | j                  ddd       | j                  ddd       | j                  dd	       | j                  d
t        d       | j                  dt        d       | j                  ddd       | j                  dd	       | j                  dg ddd       | j                  dd	       | j                  dd	       y )N--modelT
The model.requiredhelp--model-serverThe model server.--model-server-versionPThe model server version. If not specified, this defaults to the latest version.r   --target-ntpot-milliseconds  The maximum normalized time per output token (NTPOT) in milliseconds. NTPOT is measured as the request_latency / output_tokens. If this is set, the manifests will include Horizontal Pod Autoscaler (HPA) resources which automatically adjust the model server replica count in response to changes in model server load to keep p50 NTPOT below the specified threshold. If the provided target-ntpot-milliseconds is too low to achieve, the HPA manifest will not be generated. typer   z--target-ttft-millisecondszIf specified, results will only show accelerators that can meet the latency target and will show their throughput performances at the target ttft target to achieve, the HPA manifest will not be generated. --accelerator-typeThe accelerator type.--namespaceIThe namespace to deploy the manifests in. Default namespace is 'default'.--outputmanifestcommentsallr"   &The output to display. Default is all.choicesdefaultr   --output-pathIThe path to save the output to. If not specified, output to the terminal.--model-bucket-urizThe Google Cloud Storage bucket URI to load the model from. This URI must point to the directory containing the model's config file (config.json) and model weights. If unspecified, defaults to loading the model from Hugging Face.add_argumentintparsers    5lib/surface/container/ai/profiles/manifests/create.pyArgszCreate.Args#   s0   
  
    
     %D	   $	  	 $  
    /5	      4      c                    t        j                  t        j                  j                        }t        j
                  t        j                  j                        }	 |j                  |j                  |j                  |j                        }|j                         }|j                  r|j                  |_        |j                  r|j                  |_        |j                         }|j                   r|j                   |_        |j%                  ||j&                  |j(                        }|j                  |j                  ||_        |j"                  ||_        |j.                  j1                  |      }|S # t2        j4                  $ rF}	t7        j8                  d|	        t6        j:                  j=                  d|	        g cY d }	~	S d }	~	ww xY w)N)modelmodelServermodelServerVersion)modelServerInfoacceleratorTypekubernetesNamespaceAn error has occurred: )r   GetClientInstancer   ReleaseTrackGAGetMessagesModuleModelServerInfor3   model_servermodel_server_versionPerformanceRequirementstarget_ntpot_millisecondstargetNtpotMillisecondstarget_ttft_millisecondstargetTtftMillisecondsStorageConfigmodel_bucket_urimodelBucketUri GenerateOptimizedManifestRequestaccelerator_type	namespaceperformanceRequirementsstorageConfigoptimizedManifestGenerater   Errorr   errorstatusPrint)
selfargsclientmessagesmodel_server_infoperformance_requirementsstorage_configrequestresponsees
             r/   Runz
Create.Runq   s   ##D$5$5$8$89F%%d&7&7&:&:;H%"22

''!66 3 
 "*!A!A!C		'	'** 	!8 
	&	&)) 	!7  --/n			(,(=(=%99+//"nn : g #
:
:
F%<<H*B'		&	&	2 .))227;ho 	ii)!-.	jj045is   DE< <G;G
GGc                    |s t         j                  j                  d       y d}|j                  dk7  r#|j                  D ]  }||j
                  dz   z  } |j                  r1dj                  |j                  D cg c]  }d| 	 c}      }||z  }|j                  rh	 t        j                  |j                  |      5 }|j                  |       d d d        t         j                  j                  d|j                          y t         j                  j                  |       y c c}w # 1 sw Y   [xY w# t        j                  $ r"}t        j                  d|        Y d }~y d }~ww xY w)	NNo manifests generated. r!   
---

# Output saved to /An error occurred while saving output to file: )r   outrS   outputkubernetesManifestscontentr!   joinoutput_pathr   
FileWriterwriter   rP   rQ   	rT   rU   	resourcesoutput_contentr    linecomment_stringfr]   s	            r/   DisplayzCreate.Display   s9   	ggmm-.N{{j 33((**Y66 4 yy):L:L!M:L$Btf+:L!MNn&nId..?1
''.
! @()9)9(:;< 
ggmmN# "N
 @?  I		CA3GHHIs6   <D(! D9 D-4D9 -D62D9 9E.E))E.N__name__
__module____qualname____doc__staticmethodr0   r^   ru    r1   r/   r	   r	      s)     K KZ)V$r1   r	   c                   ,    e Zd ZdZed        Zd Zd Zy)CreateAlphar
   c                 t   | j                  ddd       | j                  ddd       | j                  dd	       | j                  d
t        d       | j                  ddd       | j                  dd	       | j                  dg ddd       | j                  dd	       | j                  ddd       y )Nr   Tr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r#   r$   r'   r(   r)   zQGCS bucket URI to pull model from. If not specified, default to the model hoster.)hiddenr   r*   r-   s    r/   r0   zCreateAlpha.Args   s   
  
    
     %D	   $  
    /5	      $	  r1   c           	      f   t        j                  t        j                  j                        }t        j
                  t        j                  j                        }	 |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                        }|j                  j                  |      }|S # t         j"                  $ rF}t%        j&                  d|        t$        j(                  j+                  d|        g cY d }~S d }~ww xY w)N)!modelAndModelServerInfo_modelName'modelAndModelServerInfo_modelServerName*modelAndModelServerInfo_modelServerVersionrC   r7   r8   storageConfig_modelBucketUrir9   )r   r:   r   r;   ALPHAr=   &GkerecommenderOptimizedManifestRequestr3   r?   r@   rB   rJ   rK   rG   v1alpha1OptimizedManifestr   rP   r   rQ   rR   rS   )rT   rU   rV   rW   r[   r\   r]   s          r/   r^   zCreateAlpha.Run  s    ##D$5$5$;$;<F%%d&7&7&=&=>H??,0JJ262C2C595N5N"&"@"@//"nn'+'<'< @ g 227;ho 	ii)!-.	jj045is   A:C D0*;D+%D0+D0c                 <   |s t         j                  j                  d       y d}|j                  dk(  s|j                  dk(  r#|j                  D ]  }||j
                  dz   z  } |j                  dk(  s|j                  dk(  r=|j                  r1dj                  |j                  D cg c]  }d| 	 c}      }||z  }|j                  rh	 t        j                  |j                  |      5 }|j                  |       d d d        t         j                  j                  d	|j                          y t         j                  j                  |       y c c}w # 1 sw Y   [xY w# t        j                  $ r"}t        j                  d
|        Y d }~y d }~ww xY w)Nr`   ra   r    r"   rb   r!   rc   rd   re   rf   )r   rg   rS   rh   k8sManifestsrj   r!   rk   rl   r   rm   rn   r   rP   rQ   ro   s	            r/   ru   zCreateAlpha.Display  s[   	ggmm-.N{{j DKK5$8,,((**Y66 - {{j DKK5$8			I<N<N#O<NDbK<N#OP.(Id..?1
''.
! @()9)9(:;< 
ggmmN# $P
 @?  I		CA3GHHIs6   )E E& .E 4E& E#E& &F9FFNrv   r|   r1   r/   r~   r~      s(     @ @D*$r1   r~   N)rz   %googlecloudsdk.api_lib.ai.recommenderr   googlecloudsdk.callioper   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   DefaultUniverseOnlyReleaseTracksr;   r<   CreateCommandr	   r   r~   r|   r1   r/   <module>r      s    M 6 ( * # * D%%(()X$T X$ * X$v D%%++,x$$$$ x$ - x$r1   