
    Pi#                        d dl Z d dlmc mZ d dlZd dlmZ d dlm	Z	m
Z
mZ d dlmZ e j                            ddd          Ze j                            dd          Ze j                            d	d          Ze j                            d
          Ze j                            dd          Zd Z e j        d           dd            Ze j        dd            Ze j        j         e j        d           	 	 	 ddede j        dede
e j        edf         dedej         de j        fd                        Z!ee!_        dS )     N)partial)CallableUnionAnyPartitionSpecPzjax.sharding)from_NamedShardingMeshjaxnumpynp)as_c                 l    | }|/t          |t          j        j        j                  rdS |j        }|/dS )NTF)
isinstanceeinxexprstage3Compositionparent)r   nodes     n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/experimental/op/shard.py_is_composedr      sB    D

dDI,899 	4{ 
 5    c                      d fd	S )Nc                 0     |  |          |          S N )expr_in	tensor_inexpr_outbackendcts       r   <lambda>z<lambda>.<locals>.<lambda>   s&    !!	)K K r   r   r   r$   r#   s   ``r   r%   r%      s$           r   )tracec                    dd l }| |fD ]}|                                D ]}t          |t          j        j        j                  rt          d          t          |t          j        j        j                  rw|}|j	        nt          |j	        t          j        j        j
                  r7t          |j	                  r#||j	        j        d         urt          d          |j	        }|j	        nьt          j                            || j        |          }|                    |g          \  }t#          j        | g|g|          \  \  } \  }t'          d | D                       }	|t(                              t,                                                                        t'          d |	D                                 }
t3          |
t'          d |	D                                 }nt          ||j        j                  rbt9          d	 |	D                       }t9          d
 |j        D                       }|                    |          st          d| d|           nlt(                              |                              t'          d |	D                                 }
t3          |
t'          d |	D                                 }t'          d | D                       }d |D             }t?          | }tA          ||          }t,          !                    ||          }t#          j"        | g|g|g|          \  }|| fS )Nr   zConcatenation not allowedzhIf device axes are used within a composition they must appear as the left-most member of the composition)r"   c              3      K   | ]N}t          |t          j        j        j                  &t          j        j                            |          J|V  Od S r   )r   r   r   r   Axis	is_marked.0axiss     r   	<genexpr>zshard_stage3.<locals>.<genexpr>:   sl        dDI,122 8<y7G7Q7QRV7W7W     r   c              3   $   K   | ]}|j         V  d S r   valuer-   as     r   r/   zshard_stage3.<locals>.<genexpr>B   s$      9W9Wa!'9W9W9W9W9W9Wr   c              3   $   K   | ]}|j         V  d S r   namer3   s     r   r/   zshard_stage3.<locals>.<genexpr>C   $      .K.K!qv.K.K.K.K.K.Kr   )
axis_namesc              3   $   K   | ]}|j         V  d S r   r6   r3   s     r   r/   zshard_stage3.<locals>.<genexpr>F   s$      77a16777777r   c              3   4   K   | ]}t          |          V  d S r   )strr3   s     r   r/   zshard_stage3.<locals>.<genexpr>G   s(      99AQ999999r   z9Marked axes must be subset of mesh axes. Got marked axes z and mesh axes c              3   $   K   | ]}|j         V  d S r   r1   r3   s     r   r/   zshard_stage3.<locals>.<genexpr>N   s$      /M/MA/M/M/M/M/M/Mr   c              3   $   K   | ]}|j         V  d S r   r6   r3   s     r   r/   zshard_stage3.<locals>.<genexpr>O   r8   r   c              3   b   K   | ]*}t          |t          j        j        j                  &|V  +d S r   )r   r   r   r   r*   r,   s     r   r/   zshard_stage3.<locals>.<genexpr>R   s9      UU$Zdi>N>S-T-TUUUUUUUr   c                 h    g | ]/}t           j        j                            |          r|j        nd 0S r   )r   r   r   r+   r7   r,   s     r   
<listcomp>z shard_stage3.<locals>.<listcomp>S   s7    ___RV49#3#=#=d#C#CMdii___r   )#r   allr   r   r   r   Concatenation
ValueErrorMarkerr   Listr   childrentracercall_factoryshapeall_to_tensorutilflattentupletnparraytjaxdevicesreshapetMeshshardingr   setr9   issubsettPtNamedSharding
device_put	unflatten)r   r    r!   meshr"   r   rootr   childmarked_axesrR   marked_names
mesh_namesaxespartition_specrU   s                   r   shard_stage3rd      st    JJJ(# ) )HHJJ 	) 	)D$	 0 >?? > !<===$	 0 788 )l."5<1A1FGG(66 ")>q)AAA(U   "LE l.	)$ ((GM7(SSI(()55LY  $|WI	{GTTTJW      K |))DLLNN++33E9W9W;9W9W9W4W4WXXW.K.K{.K.K.K)K)KLLL	D#,+	,	, M77;777779999999
$$Z00 	uLuuisuu  	 ))D//))%/M/M/M/M/M*M*MNNW.K.K{.K.K.K)K)KLLL UU'UUUUUD__Z^___N(N dN33H	844I >7)i[8*gVVVLYgr   Tc                    dd l }t          j        j                            |           \  } t          j        j                            |           t                    dk    rt          dt                               fd}|	  |g           }n/# t          j        j
        $ r}	t          j        j                            t          j        j                            d         d                             }
t          j                            |
t          |                                          g          }	  ||g          }n# t          j        j
        $ r |	w xY wY d }	~	n[d }	~	ww xY wt          ||j        j                  rt          j        j                            t          j        j        j                            d |j        D                                 }
t          j                            |
|j        j                  } ||g          }nt          |t.          t0          f          rt          j        j                            t          j        j                            d         d                             }
t          j                            |
t          |          g          } ||g          }|                                }||fS )Nr      z%Expected exactly one expression, got c                     t           j                            t           j                            d         d                   g| z   d                                 D             z             d         S )Nr   c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS ).N)depth1depth2)r   r   Equationr   asarraynewaxis)r-   kvs      r   rA   z(parse.<locals>.solve.<locals>.<listcomp>q   sY       Aq 	""1bjmmCO&DTZ^"__  r   )cse)r   r   solverk   items)eqsrp   op
parameterstensor_shapes    r   rq   zparse.<locals>.solvem   s    yY1a,778 &,,..     
 
  	r   c                 V    g | ]&}t           j        j                            |          'S r   )r   r   stage1	NamedAxis)r-   r7   s     r   rA   zparse.<locals>.<listcomp>   s8     ) ) )59	 **400) ) )r   )r   r   rt   rL   !_clean_description_and_parametersr   rx   parse_oplenrD   SolveExceptionr   
get_markedrk   rR   r   rU   r   rE   rF   mayber9   rJ   listrN   __deepcopy__)descriptionrv   rp   r\   jax_devicesru   r   rq   r   e	expr_meshmesh_eqr!   rt   s    ``  `       @r   parser   `   s   JJJ"glLLZ K 
		"	";	/	/B
2ww!||JRJJKKK	 	 	 	 	 	 	 	 |
	eBiiGGy' 	 	 		(44TY5E5P5PQSTUQVWXQY5Z5Z[[Ii((S5G5G4HIIG%	**9+    	 
D#,+	,	, #I$++I!'' ) )=A_) ) )  
 
	
 )$$Y0BCC%	""	D4-	(	( #I$001A1L1LRPQUSTX1V1VWW	)$$YT<<%	""##%%HHs1   B E83BE3EE3E**E33E8c                      d fd	S )Nc                 .     |  |          fd|i|S )Nr\   r   )r   tensorr\   r"   kwargsr#   r$   s        r   r%   z<lambda>.<locals>.<lambda>   s<    VWVWQQvYYW W%)W-3W W r   NNr   r&   s   ``r   r%   r%      s$           r   r   r   r\   r"   rp   ru   returnc                     |j         dk    rt          d          t          | t          j                            |          f||d|\  }}t          |||||          \  }}|S )a
  Shards a tensor over a mesh of devices.

    *This function is currently experimental and will likely change in future versions.*

    *This function is currently only supported for Jax: A sharding is created
    based on the given expression, and applied to the tensor using* ``jax.device_put``.

    The tensor is sharded across the marked axes in the input expression. The marked axes
    match the axis names and shape of the mesh:

    >>> x = jnp.ones((2, 4, 128))
    >>> x = einx.experimental.shard("[d1 d2] c")
    >>> x.sharding
    NamedSharding(mesh=Mesh('d1': 2, 'd2': 4), spec=PartitionSpec('d1', 'd2', None))

    Axis compositions can be used to apply the
    `sharding rules of Jax <https://jax.readthedocs.io/en/latest/notebooks/Distributed_arrays_and_automatic_parallelization.html>`_,
    where tensor axes are evenly divided by the number of shards:

    >>> x = jnp.ones((128, 640, 480, 3))
    >>> x = einx.experimental.shard("([batch] _) ...", x)
    >>> x.sharding
    NamedSharding(mesh=Mesh('batch': 8), spec=PartitionSpec('batch',))

    If possible, the sharding is created over all devices. ``_`` is a regular axis name,
    and its value is determined by :doc:`einx's expression solver </faq/solver>`.

    Optionally, an existing mesh can be passed:

    >>> from jax.sharding import Mesh
    >>> devices = np.asarray(jax.devices()).reshape(4, 2)
    >>> mesh = Mesh(devices, axis_names=("d1", "d2"))
    >>> x = jnp.ones((4, 1024, 1024))
    >>> x = einx.experimental.shard("a ([d2] b) ([d1] c)", x, mesh=mesh)
    >>> x.sharding
    NamedSharding(mesh=Mesh('d1': 4, 'd2': 2), spec=PartitionSpec(None, 'd2', 'd1'))

    The array is replicated over all mesh axes that are not part of the expression:

    >>> x = jnp.ones((1024, 1024))
    >>> x = einx.experimental.shard("a ([d1] b)", x, mesh=mesh)
    >>> x.sharding
    NamedSharding(mesh=Mesh('d1': 4, 'd2': 2), spec=PartitionSpec(None, 'd1',))

    Args:
        description: Description string in Einstein notation (see above).
        tensor: Input tensor or tensor factory matching the description string.
        mesh: Mesh or list of devices to shard the tensor over. If not given, a new mesh over all
            available devices will be created matching the axes in the given expression.
            Defaults to ``None``.
        cse: Whether to apply common subexpression elimination to the expressions. Defaults
            to True.
        graph: Whether to return the graph representation of the operation instead of
            computing the result. Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        The sharded tensor if ``graph=False``, otherwise the graph
        representation of the operation.
    r   z;einx.experimental.shard is currently only supported for Jax)r\   rp   )r\   r"   )r7   NotImplementedErrorr   r   rH   	get_shaperd   )r   r   r\   r"   rp   ru   r   r!   s           r   shardr      s    T |u!"_```T[**6229=3 JT GX $GVXDRYZZZFHMr   r   )TNN)Nr   T)"r   einx.op.utilrt   rL   r   r   	functoolsr   typingr   r   r   numpy.typingnptrH   import_rX   rY   rT   rQ   rO   r   jitrd   	lru_cacher   traceback_utilfilterr<   TensorBackendbool	ArrayLiker   r   r   r   <module>r      s:                       ' ' ' ' ' ' ' ' ' '      	[#^DD$$_N$KKF.99{5!!
k't,,   
   > > > >B 6 6 6 6r 	    .3J JJKJ J 4<d*+	J
 
J -J 
[J J J  JZ r   