o
    2h
                     @   sh   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z
 G dd deZej							dd
dZdS )z)A python interface for Grappler clusters.    N)step_stats_pb2)op_performance_data_pb2)device_properties_pb2)_pywrap_tf_clusterc                   @   sn   e Zd ZdZ				dddZdd Zdd	 Zed
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )ClusterzGrappler Clusters.TNc                 C   sF   d| _ | | _|du rt||| _ dS dd |D }t|| _ dS )a  Creates a Cluster.

    Args:
      allow_soft_placement: If True, TF will automatically fix illegal
        placements instead of erroring out if the placement isn't legal.
      disable_detailed_stats: If True, detailed statistics will not be
        available.
      disable_timeline: If True, the timeline information will not be reported.
      devices: A list of devices of type device_properties_pb2.NamedDevice.
        If None, a device list will be created based on the spec of
        the local machine.
    Nc                 S   s   g | ]}|  qS  )SerializeToString.0devicer   r   ]/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/grappler/cluster.py
<listcomp>4   s    z$Cluster.__init__.<locals>.<listcomp>)_tf_cluster_generate_timeline
tf_clusterTF_NewClusterTF_NewVirtualCluster)selfallow_soft_placementdisable_detailed_statsdisable_timelinedevicesdevices_serializedr   r   r   __init__   s   
zCluster.__init__c                 C   s$   | j d urt| j  d | _ d S d S N)r   r   TF_ShutdownClusterr   r   r   r   Shutdown7   s   

zCluster.Shutdownc                 C   s   |    d S r   )r   r   r   r   r   __del__<   s   zCluster.__del__c                 C   s   | j S r   )r   r   r   r   r   r   ?   s   zCluster.tf_clusterc                 C   s$   | j du rg S dd t| j D S )z-Returns a list of available hardware devices.Nc                 S      g | ]}t j|qS r   )r   NamedDevice
FromStringr	   r   r   r   r   G       z'Cluster.ListDevices.<locals>.<listcomp>)r   r   TF_ListDevicesr   r   r   r   ListDevicesC   s
   

zCluster.ListDevicesc                 C   s   t  S )zCReturns a list of all available operations (sorted alphabetically).)r   TF_ListAvailableOpsr   r   r   r   ListAvailableOpsJ   s   zCluster.ListAvailableOpsc                 C   s   t | j|jS r   )r   TF_GetSupportedDevicesr   tf_itemr   itemr   r   r   GetSupportedDevicesN   s   zCluster.GetSupportedDevicesc                 C   s   t | S r   )r   TF_EstimatePerformancer   )r   r   r   r   r   EstimatePerformanceQ   s   zCluster.EstimatePerformancec                 C   s:   t |j| j| j\}}}dd |D }||tj|fS )zReturns the cost of running the specified item.

    Args:
      item: The item for which to measure the costs.
    Returns: The triplet op_perfs, runtime, step_stats.
    c                 S   r   r   )r   OpPerformancer!   )r
   op_perf_bytesr   r   r   r   ^   r"   z(Cluster.MeasureCosts.<locals>.<listcomp>)r   TF_MeasureCostsr(   r   r   r   	StepStatsr!   )r   r*   op_perf_bytes_listrun_timestep_stats_bytesop_perfsr   r   r   MeasureCostsT   s   

zCluster.MeasureCostsc                 C   s   t |j| jS )zReturns a snapshot of the peak memory usage.

    Args:
      item: The item for which to measure the costs.
    Returns: A hashtable indexed by device name.
    )r   TF_DeterminePeakMemoryUsager(   r   r)   r   r   r   DeterminePeakMemoryUsagec   s   z Cluster.DeterminePeakMemoryUsageTTTN)__name__
__module____qualname____doc__r   r   r   propertyr   r$   r&   r+   r-   r6   r8   r   r   r   r   r      s"    

r   Tc                 c   s"    t | |||}|V  |  d S r   )r   r   )r   r   r   r   clusterr   r   r   	Provisionn   s   r@   r9   )r=   
contextlibtensorflow.core.frameworkr   tensorflow.core.grappler.costsr   tensorflow.core.protobufr   tensorflow.python.grapplerr   r   objectr   contextmanagerr@   r   r   r   r   <module>   s   U