
    ukit              
         d Z ddlmZ ddlZddlmZ ddlZddlZddlm	Z	m
Z
m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	Z ej,                  d       G d d             Z G d dej0                        Z ej,                  d       G d de             Z ej,                  d       G d de             Z ej,                  d       G d de             Z ej,                  d       G d d             Z ej,                  d       G d d             Z ej,                  d       G d d             Z ej,                  d       G d d             Z eez  ez  ez  ez  e z  Z!	 	 	 	 	 	 d;d Z"	 	 	 	 	 	 d<d!Z#	 	 	 	 	 	 d=d"Z$	 	 	 	 	 	 d>d#Z% ej,                  d       G d$ d%             Z& e'ejP                  ejR                  fejR                  ejP                  fejT                  ejV                  fejV                  ejT                  fejX                  ejP                  fejZ                  ejX                  fejZ                  ejP                  fg      Z. ej,                  d       G d& d'             Z/ ej,                  d       G d( d)             Z0 ej,                  d       G d* d+             Z1 ej,                  d       G d, d-             Z2e&e/z  e1z  e0z  e2z  Z3	 	 	 	 	 	 d?d.Z4ej,                   G d/ d0             Z5e G d1 d2             Z6	 	 	 	 d@d3Z7dAd4Z8	 	 	 	 dBd5Z9	 	 	 	 dCd6Z:	 	 	 	 dDd7Z;dEd8Z<	 	 	 	 dFd9Z=	 	 	 	 dBd:Z>y)Gz1Defines expressions and constraints over layouts.    )annotationsN)Sequence)Anyassert_neverfinal   )fragmented_array)launch_context)layouts)inference_utils)tcgen05T)frozenc                  "    e Zd ZU dZded<   d Zy)VariablezMA variable is an abstract identifier.

  `key` is supposed to be hashable.
  VariableKeykeyc                "    d| j                    dS )NzV()r   selfs    b/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/experimental/mosaic/gpu/constraints.py__str__zVariable.__str__/   s    z    N__name__
__module____qualname____doc____annotations__r    r   r   r   r   '   s     r   r   c                      e Zd ZdZy)ConstantzA constant is a known layout.N)r   r   r   r   r!   r   r   r#   r#   3   s    %r   r#   c                  "    e Zd ZU dZded<   d Zy)RegisterLayoutzWraps a known register layout.fa.FragmentedLayoutvaluec                "    d| j                    dS NzC(r   r'   r   s    r   r   zRegisterLayout.__str__=       

|1r   Nr   r!   r   r   r%   r%   7   s    &	r   r%   c                  "    e Zd ZU dZded<   d Zy)
TMEMLayoutzWraps a known TMEM layout.tcgen05.TMEMLayoutr'   c                "    d| j                    dS r)   r*   r   s    r   r   zTMEMLayout.__str__G   r+   r   Nr   r!   r   r   r-   r-   A   s    "	r   r-   c                  "    e Zd ZU dZded<   d Zy)
SMEMTilingzWraps a known SMEM Tile Transform.

  If an SMEM reference may, in principle, have transforms but should not be
  tiled, then `value` is `None`.
  lc.TileTransform | Noner'   c                "    d| j                    dS r)   r*   r   s    r   r   zSMEMTiling.__str__U   r+   r   Nr   r!   r   r   r1   r1   K   s     
! r   r1   c                  (    e Zd ZU ded<   ded<   d Zy)Reduce
Expression
expressiontuple[int, ...]axesc                <    d| j                    d| j                   dS )NzReduce([z], r   )r9   r7   r   s    r   r   zReduce.__str__^   s     dii[DOO#4A66r   Nr   r   r   r    r   r!   r   r   r5   r5   Y   s    7r   r5   c                  ,    e Zd ZU ded<   ded<   ded<   y)BroadcastInDimr6   r7   r8   r9   shapeNr   r   r   r    r!   r   r   r=   r=   b   s    	r   r=   c                  ,    e Zd ZU ded<   ded<   ded<   y)Reshaper6   r7   r8   source_shapetarget_shapeNr?   r!   r   r   rA   rA   i   s    r   rA   c                      e Zd ZU ded<   d Zy)	Transposer6   r7   c                "    d| j                    dS )NzT(r   r7   r   s    r   r   zTranspose.__str__t   s     ""r   Nr;   r!   r   r   rE   rE   p   s    #r   rE   c                    d fd}t         j                  |      }|xt        d x\    t               S  t        d xy\  }|t        j
                  d x>\  } ||      s
t               S t        t	        j
                   j                              S  	 t        | j                   j                        S  	 t        | j                   j                        S )Nc                n    t        j                  | d      D ]  \  }}j                  |   |k7  s y y)NTstrictF)zipr9   r>   )r>   axiss	broadcasts      r   _check_shape_broadcastz;reduce_broadcast_expression.<locals>._check_shape_broadcast   s<    y~~uT: a		!	# r   r!   r*   r>   )r7   r9   r>   )r>   r8   returnbool)	reduce_expressionr7   Unsatisfiabler%   faWGSplatFragLayoutr>   r=   r9   )rO   assignmentsrP   reduced_exprlayoutr>   s   `     r   reduce_broadcast_expressionr[      s     #9#7#7E,	_ 
		%!!.'. ?" 4 49?? KL
L / %>>OO  
& 
!	ioo r   c                   t        | j                  |      }|xt        d x\    t               S  t        d x\  }|xt        j
                  d xd\  } t        j                  |      t        j                  | j                        k(  sJ t        t	        j
                  | j                              S  xt        j                  d xf\  }} t        j                  |      t        j                  | j                        k(  sJ t        t	        j                  | j                  |            S  xt        j                  d x\   }|j                  }t        | j                        t        |      k  rt        j                  | |      S t        || j                   t        |       d  d      D ]&  \  }}	|	|z  dk7  st        j                  | |      c S  t        |      d	z
  }
| j                   |
 d  }| j                  |
 d  }|d   }||k7  s| j                  t        |          |z  dk7  rt        j                  | |      S t        |      S   y  	 t        j                  | |      S )
Nr!   r*   rQ   )r>   vec_sizerG   TrJ   r   r   )rT   r7   rU   r%   rV   rW   mathprodrC   WGStridedFragLayoutTiledLayoutbase_tile_shapelendataclassesreplacerL   rB   )reshaperX   rY   rZ   r>   r]   tiled_layout
tile_shapetsrN   num_minor_tiled_dimssource_minor_tiled_dimstarget_minor_tiled_dimsmajor_tiled_dims                 r   reduce_reshape_expressionrn      sE    #7#5#5{C,	_ 
		%.R!!.5!TYYw/C/C%DD
DD"")=)=>  /
 DR##CC5!TYYw/C/C%DD
DD$$,,x  D .R^^-#33*%%&Z8&&w<HH
 :w';';S_<L<M'NW[\ Keb!2v{ ((\JJK "%Z1!4
$+$8$8:N9N9O$P
!$+$8$8:N9N9O$P
!&qM/%)@@""C
O#34F!K&&w<HH
  -
-7 . 
&T 
  \BBr   c                X   t        | j                  |      }|xt        d x\    t               S  t        d xe\  }|t        d       S |j                  }t        |      dk7  rt        dt        |             t        t        j                  |d d d               S  	 t        |      S )Nr!   r*      z#Only 2D tilings are supported, got rG   )
rT   r7   rU   r1   tilingrc   NotImplementedErrorlcTileTransformrE   )	transposerX   rY   tile_transformrr   s        r   reduce_transpose_expressionrx      s     #9#7#7E,	_ 
		)		$$$f	V	!1#f+?
 	
 (("677 
* 
,//r   c                   | xt         d x\    | S  xt        d x\    |j                  | |       S  xt        d xy\  } } t	        | |      }|xt
        d x\    t               S  t        d x7\  }t        |t        j                        rt        |j                  |            S  	 t        ||      S  xt        d x\    t        | |      S  xt        d x\    t        | |      S  t        d x\   t!        | |      S  	 t#        |        y)zWReduces an expression as much as is possible given a set of known variable assignments.r!   N)r7   r9   r*   )r#   r   getr5   rT   rU   r%   
isinstancerV   ra   reducer=   r[   rA   rn   rE   rx   r   )exprrX   r9   rY   rZ   s        r   rT   rT      s     		k 
	__T4(( 
	+	+	+&t[9l]_
  )Z-Od 34
4 *<d;
; 
, 
	({;; 
	&t[99 
	({;; 
	
4r   c                  4    e Zd ZU dZded<   ded<   ddZd Zy)	Equalsz&States that `lhs` and `rhs` are equal.r6   lhsrhsc                    | j                   | j                  k(  ryt        | j                   t              rt        | j                  t              ryy )NTF)r   r   r{   r#   r   s    r   holdszEquals.holds  s6    xx488$((H%*TXXx*Hr   c                <    d| j                    d| j                   dS )NzEquals(z == r   r   r   r   s    r   r   zEquals.__str__  s    TXXJd488*A..r   NrR   zbool | Noner   r   r   r   r    r   r   r!   r   r   r   r     s    .///r   r   c                  4    e Zd ZU dZded<   ded<   ddZd Zy)	Relayouta  States that `source` must be relayout-able to `target`.

  Relayout-ability here is not defined as a fundamental property of layouts, but
  rather a reflection of our implementation. For instance, when evaluating this
  constraint, we will return `False` systematically if a relayout exists but we
  do not ever plan to support it.

  Modeling this constraint this way is helpful, in order to allow pruning
  inefficient solutions when attempting to solve a constraint system.
  r6   sourcetargetc                ~   | j                   }| j                  }||k(  ryt        |t              rt        |t              sy|j                  |j                  }}||fx  rR dk(  rM\  xt
        j                  d x5\   cxt
        j                  d x\   }} |j                  |j                  k(  S    x  rI dk(  rD\  t
        j                  d x.\   t
        j                  d x\    t        j                  ||      S     r< dk(  r7\  t
        j                  d x!\   t
        j                  d x\   ||ft        v S   	 y)zkReturns whether the relayout constraint holds.

    Returns `None` if the constraint can't be checked.
    TNrp   r!   F)r   r   r{   r%   r'   rV   rW   r`   r>   ra   layouts_libsplat_is_compatible_with_tiled_SUPPORTED_TILED_RELAYOUTS)r   r   r   source_layouttarget_layoutsplatstrideds          r   r   zRelayout.holds2  s   
 [[F[[F fn-Z6 #)<<=M

&OO*2!*,OB,B,B,DO{{gmm++ P332!2>>#3399=
 	
 4 .-2>>R^^-}-1KKK .r   c                <    d| j                    d| j                   dS )Nz	Relayout(     ⟶ r   r   r   r   s    r   r   zRelayout.__str__P  s    t{{m6$++a88r   Nr   r   r!   r   r   r   r   "  s    	 
<9r   r   c                  v    e Zd ZU dZded<   ded<   ded<   	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 dd	Zdd
Zd Z	y)IsTransferablezYStates that `source` layout must be transferable across memory spaces to `target` layout.r6   r   r   r8   r>   c                d   t        | j                        dk(  sJ | j                  d   }t        j                  |      }|t        j
                  f|t        j                  ft        j                  ||      t        j                  ft        j                  ||      t        j                  |      fgS )z:Returns the list of supported TMEM <-> Register transfers.rp   r   )rc   r>   r   tmem_default_layoutrV   TCGEN05_LAYOUTTMEM_NATIVE_LAYOUTtmem_half_lane_layoutWGMMA_LAYOUTtmem_m64_collective_layoutfa_m64_collective_layout)r   packingcolumnsr   s       r   supported_tmem_transfersz'IsTransferable.supported_tmem_transfers]  s     tzz?ajjmG!55g>	b//0	b334		&	&w	8"//J..w@,,W5	
	 r   c                D    |j                   }||f| j                  |      v S N)vector_lengthr   )r   tmem_layout
reg_layoutr   s       r   _is_valid_tmem_transferz&IsTransferable._is_valid_tmem_transfern  s*     ''G$(E(Eg(NNNr   c                p    t        j                  |      r|d uxr t        |j                        dk(  S |d u S )Nrp   )r   is_mma_layoutrc   rr   )r   smem_layoutr   s      r   _is_valid_smem_transferz&IsTransferable._is_valid_smem_transfert  s=     $$Z0$E[-?-?)@A)EE$r   c                   | j                   | j                  k7  sJ d       | j                   | j                  fx  r4 dk(  r/\  t        d x#\  ct        d x\  }} | j	                  ||      S   x  r4 dk(  r/\  t        d x#\  ct        d x\  }} | j	                  ||      S   x  r4 dk(  r/\  t
        d x#\  ct        d x\  }} | j                  ||      S   x  r4 dk(  r/\  t        d x#\  ct
        d x\  }} | j                  ||      S     rm dk(  rh\  t        d x\\   t        d xR\   t        | j                         j                  }t        | j                        j                  }t        d| d|         	 y)zbReturns whether the constraint holds.

    Returns `None` if the constraint can't be checked.
    zJIsTransferable constraints within the same memory space are not supported.rp   r*   Nr!   zUnsupported transfer: z -> )r   r   r-   r%   r   r1   r   r#   typer   rs   )r   srcdstsource_typetarget_types        r   r   zIsTransferable.holds  sL    ;;$++% 	%
 ++t{{
";;: C.";;++C55 <;;>$j&;;++C55 <;;: C.";;++C55 <;;>$j&;;++C55 <!!8:xz4;;'004;;'00!$[Mk]C
 	
 " r   c                <    d| j                    d| j                   dS )NzIsTransferable(r   r   r   r   s    r   r   zIsTransferable.__str__  s    T[[M}A>>r   N)r   intrR   z4list[tuple[tcgen05.TMEMLayout, fa.FragmentedLayout]])r   r.   r   r&   rR   rS   )r   r2   r   r&   rR   rS   r   )
r   r   r   r   r    r   r   r   r   r   r!   r   r   r   r   T  sx    a

	;"O+O9LOO* & 	:?r   r   c                  4    e Zd ZU dZded<   ded<   d	dZd Zy)
	NotOfTypez0States that `expr` is not an instance of `type`.r6   r}   ztype[fa.FragmentedLayout]r   c                    t        | j                  t              syt        | j                  t              syt        | j                  j                  | j
                         S )zjWhether the distinctiveness constraint holds.

    Returns `None` if the constraint can't be checked.
    NT)r{   r}   r#   r%   r'   r   r   s    r   r   zNotOfType.holds  sB    
 dii*dii0$))//499555r   c                N    d| j                    d| j                  j                   S )Nztype(u   ) ≠ )r}   r   r   r   s    r   r   zNotOfType.__str__  s$    499+VDII$6$6#788r   Nr   r   r!   r   r   r   r     s    8!!	69r   r   c                  4    e Zd ZU dZded<   ded<   d	dZd Zy)
Dividesa  States that the `expr` tiling is a divisor of `tiling_multiple`.

  That is to say that, for each tiled dimension in `expr`, the dimension must
  divide its corresponding dimension in `tiling_multiple` starting from the
  tail.

  If `tiling_multiple` contains more dimensions than `expr`, then
  the extra dimensions in `tiling_multiple` are ignored for the purposes of the
  check.

  `expr` is not allowed to contain more dimensions than `tiling_multiple`, and
  this constraint therefore also constrains the rank of `expr`.
  r6   r}   r8   tiling_multiplec                   | j                   xt        d x\   y xt        d x\  t        j                  d x\  } |}nP xt        d x(\  xt
        j                  d x\   } |j                  }n  t        dx\  }|j                  }n 	 y t        |      t        | j                        kD  ryt        t        |      t        | j                              D ]  \  }}||z  s y y)Nr*   T)rr   r!   F)r}   r1   rt   ru   r%   rV   ra   rb   r-   rc   r   rL   reversed)r   trr   rZ   r'   sizemultiples          r   r   zDivides.holds  s    
))!:!D! " 8:7B,,67 8;>; : 0;'' <&& 
6{S--.. hv.9M9M0NO h	D r   c                :    | j                    d| j                   dS )Nz % z == 0)r   r}   r   s    r   r   zDivides.__str__  s     ""#3tyyk77r   Nr   r   r!   r   r   r   r     s     	""08r   r   c                r   | xt         d x]\  }} t        ||      }t        |t              r
t               S t        ||      }t        |t              r
t               S t        ||      S  xt        d xS\  }} t        ||      }t        ||      }	t        |t              st        |	t              r
t               S t	        ||	      S  xt
        d x7\  }
} t        |
|      }t        |t              r
t               S t        ||      S  xt        d xU\  }}} t        ||      }t        ||      }	t        |t              st        |	t              r
t               S t        ||	|      S  xt        d x7\  }
} t        |
|      }t        |t              r
t               S t        ||      S  }t        |       y)zReduces a constraint.r   Nr   r}   r   r   r   r>   r}   r   )	r   rT   r{   rU   r   r   r   r   r   )
constraintrX   r   r   lhs_redrhs_redr   r   
source_red
target_redr}   r   expr_redr>   r   nevers                   r   reduce_constraintr     s   
 		!	!	!!#{3g	G]	+!#{3g	G]	+GW%% 
" 
0	/	/$V[9j$V[9j	J	.*
m3 j*-- 
0 
)	(	("45h	Hm	,x&&	 
)
 
C	B	B$V[9j$V[9j	J	.*Z2WJ
E:: 
C 
=	<	<"45h	Hm	,X//	 
=
 
5r   c                      e Zd ZU dZ ej
                  e      Zded<    ej
                  e	      Z
ded<   ddZ	 	 	 	 ddZd	 Zy
)ConstraintSystema.  A constraint system contains a set of constraints and assignments.

  Assignments assign constant values to variables in the system (bound
  variables). Constraints describe relationships between variables that must be
  upheld, and can be used to determine assignments for unknown (free) variables.
  )default_factorydict[Variable, Constant]rX   Sequence[Constraint]constraintsc                   	
 t               
g 	d	
 fd j                  D ]  }|xt        d x\  }}  |        |       $ xt        d x\  }}  |        |       E xt        d x\  }  |       ] xt
        d x\  }}   |        |        xt        d x\  }  |        }t        |        	S )z1Returns the list of free variables in the system.c                   | xt         d x:\    | vr2| j                  vr#j                  |        j                  |        y y y  xt        d x\    y  xt
        d x\  }  |       y  xt        d x\  }  |       y  xt        d x\  }  |       y  t        d x\  } |       y  	 t        |        y )Nr!   rG   )
r   rX   addappendr#   r5   r=   rA   rE   r   )r}   eextract_variablesfree_variablesseen_variablesr   s     r   r   z4ConstraintSystem.unknowns.<locals>.extract_variables'  s    XZ'D8H8H,Ht$!!$' -I'  XZ
 !V!
A
 ")^)
A
 *"W"
A
 #$
A
 %
t
r   r   r   )r}   r   )r}   r6   rR   None)setr   r   r   r   r   r   r   )r   r   r   r   r   r   r}   r   r   r   r   s   `       @@@r   unknownszConstraintSystem.unknowns#  s    $'EN%'N $ && 
%V%%
C
 
C
  & 4X33
F
#
F
# 4 "Y!
D
! "B^BB
F
#
F
# C  W
D
!  
u
!" r   c                   | j                   j                         D ]2  \  }}||j                   v s||j                   |   k7  s(t               c S  t        | j                   |j                   z  g | j                  |j                        S )NrX   r   )rX   itemsrU   r   r   )r   othervariable
assignments       r   __and__zConstraintSystem.__and__L  s     !% 0 0 6 6 8 *	U&&	&:9J9J89T+T $$u'8'88;d&&;):):; r   c                    d}|dz  }| j                   j                         D ]  \  }}|d| d| dz  } |dz  }| j                  D ]  }|d| dz  } |S )NzConstraintSystem
z  assignments:
z    u    ⟵ 
z  constraints:
)rX   r   r   )r   rr   constantr   s        r   r   zConstraintSystem.__str__W  s    A	A $ 0 0 6 6 8 0
HT*U8*B//a0	A&& !
T*R  a!Hr   N)rR   zlist[Variable])r   r   rR    ConstraintSystem | Unsatisfiable)r   r   r   r   rd   fielddictrX   r    listr   r   r   r   r!   r   r   r   r     s`     +<+*;*;++'  '8k&7&7&M+#M'R	#	'	r   r   c                      e Zd ZddZy)rU   c                    | S r   r!   )r   r   s     r   r   zUnsatisfiable.__and__e  s    Kr   N)r   r   rR   rU   )r   r   r   r   r!   r   r   rU   rU   b  s    r   rU   c                    t               }| D ]U  }|t        d xH\  xt        d x:\   ct        j                  k(  r'}t        |t              sJ |j                  |       S   W |S )z)Returns a all vars distinct from a splat.r   r!   )r   r   r   rV   rW   r{   r   )r   varsr   vars       r   non_splat_variablesr   i  s`     $ j
G+(*+"2F2FG#x((( H
 
+r   c                v    t        |       }|sydd}| D ]"  }|t        d x\  }}||v s ||      s y 	 $ y)a   Returns whether the constraints imply a non-splat to splat relayout.

  Such relayouts are impossible and this helps shortcut the search.

  If this function returns False, this doesn't necessarily mean that there are
  no non-splat to splat relayouts, just that this is not known yet.
  Fc                n    t        | t              xr$ t        | j                  t        j                        S r   )r{   r%   r'   rV   rW   )r   s    r   is_constant_splatz>_has_relayout_of_non_splat_to_splat.<locals>.is_constant_splat  s,    a( Z	%%. r   r   T)rR   rS   )r   r   )r   	non_splatr   r   r   r   s         r   #_has_relayout_of_non_splat_to_splatr   v  sX     "+.)	
   j
1Y#4V#< 2  
r   c                j   t        | j                        }g }t        |      dkD  }|r~d}| j                  D ]j  }|t        d x\\  }}t	        |t
              s"||v s'||vs,d}|j                  |       |j                  t        |t        j                               i 	 l |r~| t        |      z  S )a  Adds transitive NotOfType constraints for all non-splat variables.

  Given `n` variables `l0`, ... `l{n-1}`, and a set of relayouts
  `{ Relayout(l{i}, l{i+1}) : 0 <= i < n }`, if we also know that
  `l{0}` is not splat, then we can automatically deduce that none of
  `l0`, ..., `l{n-1}` are splat either.

  This helps us quickly conclude that a system is unsatisfiable in cases where
  a non-splat variable is transitively relaid out into a splat layout.
  r   Fr   Tr   )r   r   rc   r   r{   r   r   r   r   rV   rW   r   )constraint_systemr   new_constraintsnew_non_splat_foundr   r   r   s          r   saturate_distinct_from_splatr     s     ""3"?"?@)&(/I*'33 
3*I%	)"&MM&!""9VR5I5I#JK 4 
 	 
-/J	JJr   c                N   i dfddfd}t               }| j                  D ]|  }|t        d xo\  xt        d xa\   cxt        d xT\   }}t	        |t              sJ t	        |t              sJ |j                  |       |j                  |        |||       z   ~ i }t        |t              D ]+  } |      }|j                  |g       j                  |       - i }	|j                         D ]"  }
|
D ]  }|
D cg c]
  }||k7  s	| c}|	|<    $ |	S c c}w )zComputes all transitively equal variables in a constraint system.

  The output dictionary maps each variable that appears in constraints in the
  constraint system to all the variables it is transitively equal to.
  c                L    | vr| | <   |    | k7  r |          | <   |    S r   r!   )vfindparents    r   r  z-compute_transitively_equal_vars.<locals>.find  s9    fQiayA~vay/fQi!9r   c                <     |       } |      }||k7  r||<   y y r   r!   )v1v2root1root2r  r  s       r   unionz.compute_transitively_equal_vars.<locals>.union  s*    HEHE~fUm r   r   r!   r   )r  r   rR   r   )r  r   r  r   )r   r   r   r   r{   r   sortedstr
setdefaultr   values)systemr
  all_varsr   r   r   
componentsr  root
equal_varscomponent_varsr   r  r  s               @@r   compute_transitively_equal_varsr    s1    &(&  E(&& j
?'hj'->XZ?#x(((#x(((SSc3 @ 02*($ .a7D$#**1-. 02*"))+ Hn H*8GEQJuGjmHH 
 Hs   
D"D"c                L   t        |       }g }| j                  D ]g  }|j                  |       |t        d xH\  }}t	        |t
              s3|j                  |g       D ]  }|j                  t        ||              f 	 i t        |      }t        j                  | |      S )zESaturates Divides constraints between all transitively equal vars.
  r   r   )
r  r   r   r   r{   r   rz   merge_divides_constraintsrd   re   )r  r  r   r   r}   r   	equal_vars          r   +saturate_divides_constraints_for_equal_varsr    s    
 /v6*&(/&& j:&
>dH%%>>$3 Hi""79o#FGH ?  .o>/			V	AAr   c                $   g }i }| D ]  }|t         d x\  xt        d x\   }}t        |t              sJ |j                  |      x}|||<   Ht	        t        |      t        |            }g }|dkD  rCt        || d || d d      D ]*  \  }	}
|j                  t        j                  |	|
             , t        |      ||<      	 |j                  |        |j                         D ]   \  }}|j                  t        ||             " |S )z.Merges Divides constraints that can be merged.r   Nr!   r   TrJ   )r   r   r{   rz   minrc   rL   r   r^   gcdtupler   )r   resultvar_to_tiling_multiplesr   r  r   previous_tiling_multiplemin_lennew_tiling_multiplexyr}   s               r   r  r    s1   &>@ "j
I
I!X&&&(?(C(CA(FF$O'6
!!
$

 c/*C0H,IJ Q;/7()46NPWxy6Ybfg 7da&&txx1~67%*+>%?" J j!%"&  7<<> 2dO
MM'$012	-r   c                  	 | j                   	g }d}d	fd}| j                  D ]  }t        |	      xt        d x\    t               c S  xt        d x8\  xt
        d x*\   cxt        d x\   }}  |||      st               c S d}i   xt        d x8\  xt        d x*\   cxt
        d x\   }}  |||      st               c S d}   }t        |t              sJ |j                         x |j                  |       |||k7  z  }xdu r t               c S du sd} t        |      }|t        |      t        |      k7  z  }|}t        |      r
t               S |rt        	| j                   z  |      S y)	a6  Performs one reduction step over each constraint in a constraint system.

  Returns:
    - Unsatisfiable(): if the constraint system is unsatisfiable.
    - A new constraint system if any constraint was reduced.
    - None: if the constraint system is not known unsatisfiable, but hasn't been
      reduced.
  Fc                *    | v r	|    |k7  ry|| <   y)NFTr!   )r   cstrX   s     r   
try_assignz'_reduce_system_once.<locals>.try_assign'  s'    
kk#.#5Kr   r!   Nr   Tr   )r   r   r'  r#   rR   rS   )rX   r   r   rU   r   r   r#   r{   
Constraintr   r   r  rc   r   r   )
r   r   changedr(  r   r   r'  new_constraintr   rX   s
            @r   _reduce_system_oncer,    s    "--+"$+' &11 j
J
4=? ?6?'hj'->XZ?#s#
  @ @6?'hj'->XZ?#s#
  @ .*555""$~.~33G ?"G+. .k:/	S!S%555'+ )5?"3"?"??  
r   c                    	 t        |       x 	 | S xt        d x\    t               S  xxt        d x\   } |} n  }t        |       I)zReduces a constraint system until it can no longer be reduced.

  Returns:
    - Unsatisfiable(): if the constraint system is unsatisfiable.
    - The maximally reduced constraint system otherwise.
  r!   )r,  rU   r   r   )r   
new_systemr   s      r   r|   r|   U  s[     	
/
0 
 =? ++& ,U 	r   )rO   r=   rX   r   rR   Expression | Unsatisfiable)rf   rA   rX   r   rR   r/  )rv   rE   rX   r   rR   r/  )r}   r6   rX   r   rR   r/  )r   r)  rX   r   rR   zConstraint | Unsatisfiable)r   r   rR   zset[Variable])r   r   rR   rS   )r   r   rR   r   )r  r   rR   zdict[Variable, list[Variable]])r  r   rR   r   )r   r   rR   zlist[Constraint])r   r   rR   z'ConstraintSystem | Unsatisfiable | None)?r   
__future__r   abccollections.abcr   rd   r^   typingr   r   r    r	   rV   r
   rt   r   r   r   r   r   	dataclassr   ABCr#   r%   r-   r1   r5   r=   rA   rE   r6   r[   rn   rx   rT   r   	frozensetr   WGMMA_TRANSPOSED_LAYOUTr   TCGEN05_TRANSPOSED_LAYOUTWGMMA_LAYOUT_UPCAST_2XWGMMA_LAYOUT_UPCAST_4Xr   r   r   r   r   r)  r   r   rU   r   r   r   r  r  r  r,  r|   r!   r   r   <module>r<     s   8 # 
 $   + + $ " $    d#  $&sww & d#X  $ d#  $ d#
 
 $
 d#7 7 $7 d#  $ d#    $  d## # $#   	
  ,D>2C2C#;2C2Cj00'?00(
#;6 d#/ / $/ '__b0011445!!2#4#450 9 9:0
( 
  d#.9 .9 $.9b d#L? L? $L?^ d#9 9 $9* d#*8 *8 $*8Z h*^;gE
'')A''T I I IX   
%

6 K' K% KF00#0fBBB*8:':,:z'%r   