a
    /Sic
  ã                   @   s`   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                   @   sf   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   sD   d| _ | | _|du r&t ||¡| _ n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/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/grappler/cluster.pyÚ
<listcomp>4   ó    z$Cluster.__init__.<locals>.<listcomp>)Ú_tf_clusterÚ_generate_timelineÚ
tf_clusterZTF_NewClusterZTF_NewVirtualCluster)ÚselfÚallow_soft_placementÚdisable_detailed_statsÚdisable_timelineÚdevicesZdevices_serializedr   r   r   Ú__init__   s    ÿzCluster.__init__c                 C   s    | j d urt | j ¡ d | _ d S ©N)r   r   Z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   s   g | ]}t j |¡‘qS r   )r   ZNamedDeviceÚ
FromStringr	   r   r   r   r   G   s   ÿz'Cluster.ListDevices.<locals>.<listcomp>)r   r   ZTF_ListDevicesr   r   r   r   ÚListDevicesC   s
    

ÿzCluster.ListDevicesc                 C   s   t  ¡ S )zCReturns a list of all available operations (sorted alphabetically).)r   ZTF_ListAvailableOpsr   r   r   r   ÚListAvailableOpsJ   s    zCluster.ListAvailableOpsc                 C   s   t  | j|j¡S r   )r   ZTF_GetSupportedDevicesr   Útf_item©r   Úitemr   r   r   ÚGetSupportedDevicesN   s    zCluster.GetSupportedDevicesc                 C   s   t  | ¡ ¡S r   )r   Z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 )z¬Returns 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   s   g | ]}t j |¡‘qS r   )r   ZOpPerformancer   )r
   Zop_perf_bytesr   r   r   r   ^   s   ÿz(Cluster.MeasureCosts.<locals>.<listcomp>)r   ZTF_MeasureCostsr   r   r   r   Ú	StepStatsr   )r   r!   Zop_perf_bytes_listZrun_timeZstep_stats_bytesZop_perfsr   r   r   ÚMeasureCostsT   s    ÿ
ÿ
ÿzCluster.MeasureCostsc                 C   s   t  |j| j¡S )z¢Returns 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   Z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#   r%   r&   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-   )TTTN)r*   Ú
contextlibÚtensorflow.core.frameworkr   Ztensorflow.core.grappler.costsr   Útensorflow.core.protobufr   Útensorflow.python.grapplerr   r   Úobjectr   Úcontextmanagerr-   r   r   r   r   Ú<module>   s   U    ý