o
    bi
                     @   s>   d Z ddlZddlmZ dddZdd Zdd	 Zd
d ZdS )z!!!DO NOT USE!!!

Distribution related class for Tensorflow backend.

This is just a prototype and we might want to unify it
with other backends in the future.
    N)dtensorc                 C   s~   | r|   nd} tjj| d}g }g }|D ]}|j dkr$|| q|| q| du r8t|dkr6|n|}dd |D S )a  Return all the available devices based on the device type.

    Note that this should return the global devices in a distributed setting.

    Args:
        device_type: string of `"cpu"`, `"gpu"` or `"tpu"`. Default to `gpu` or
        `tpu` if available when device_type is not provided. Otherwise will
        return the `cpu` devices.

    Return:
        List of devices that are available for distribute computation.
    N)device_typecpur   c                 S   s,   g | ]}|j   d |jd d  qS ):)r   lowernamesplit).0device r   a/home/ubuntu/.local/lib/python3.10/site-packages/keras/src/backend/tensorflow/distribution_lib.py
<listcomp>*   s    z list_devices.<locals>.<listcomp>)uppertfconfiglist_logical_devicesr   r   appendlen)r   
tf_devicescpu_devicesother_devicesr   r   r   r   list_devices   s   r   c                 C   s   d S Nr   )valuetensor_layoutr   r   r   distribute_value0   s   r   c                 C   s&   t t| j| j}tj|| j dS )zConvert the DeviceMesh to Tensorflow backend specific Mesh.

    Args:
        device_mesh: DeviceMesh instance to convert.

    Returns:
        A `tf.dtensor.Mesh` instance.
    )	mesh_dimslocal_devices)listzip
axis_namesshaper   create_distributed_meshdevicesflatten)device_meshr   r   r   r   _to_backend_mesh5   s   	
r'   c                 C   s8   | j du r	tddd | jD }| j j}tj||dS )zConvert the TensorLayout to Tensorflow backend specific Sharding.

    Args:
        tensor_layout: TensorLayout instance to convert.

    Returns:
        A `tf.dtensor.Layout` instance.
    NzDCannot create sharding when device mesh is not set for TensorLayout.c                 S   s   g | ]	}|r|nt jqS r   )r   	UNSHARDED)r
   axisr   r   r   r   S   s    z&_to_backend_layout.<locals>.<listcomp>)sharding_specsmesh)r&   
ValueErroraxesbackend_meshr   Layout)r   r*   dtensor_meshr   r   r   _to_backend_layoutD   s   
	r1   r   )	__doc__
tensorflowr   tensorflow.experimentalr   r   r   r'   r1   r   r   r   r   <module>   s    
#