
    ^j|+                     ~   d dl Z d dlmZmZ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 d dlmZmZmZ d dlmZmZ eddddej(                  d	ej*                  d
ej,                  fd       Zej0                  efd       Zeddddej,                  de	d
e	fd       Zeddddeez  e	z  ej8                  z  dej,                  dej,                  dej                  j:                  d
df
d       Zeddddee	z  dee	z  dee	z  dee	z  dej,                  dej,                  dej                  j:                  d
dfd       Zeddddeeef   deeef   d
efd       Z eddddeez  d
efd       Z!eddddeez  d
e	fd       Z"ej0                  dej(                  deej(                     dejF                  d
dfd        Z$edddd!       Z%eddddeded
ej8                  fd"       Z&edddd#ej8                  d
eeef   fd$       Z'ej0                  d.de	d%ee	   d
e	fd&       Z(edddde)e	z  d'ej,                  d
e	fd(       Z*edddde)e	z  d'ej,                  d
e	fd)       Z+e	 	 d/dej,                  d*ej,                  d
dfd+       Z,edddd	ej*                  d,ej(                  d
ej(                  fd-       Z-y)0    N)OptionalTupleUnion)Float32Int32
const_expr)arith)llvmnvvmvector)Tdsl_user_oplocipxcoordreturnc                b    | j                   t        j                  || j                  ||      z   S )Nr   )iteratorcutecrd2idxlayout)r   r   r   r   s       d/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_vendor/quack/utils.pyelem_pointerr      s$    ::UAHH#"EEE    c                     t        t        | t        j                              r2 |t        j                  | t        j
                  d            d         S | S )N   r   )r   
isinstancer   Pointermake_tensormake_layout)r   dtypes     r   load_scalar_or_pointerr$      sB    *Q-.T%%a)9)9!)<=a@AAr   smem_ptrpeer_cta_rank_in_clusterc          
          | j                  ||      j                         }t        t        j                  t        j                         ||j                         gdddd            S )zMMap the given smem pointer to the address at another CTA rank in the cluster.r   z$mapa.shared::cluster.u32 $0, $1, $2;z=r,r,rFhas_side_effectsis_align_stack)tointir_valuer   r
   
inline_asmr   i32)r%   r&   r   r   smem_ptr_i32s        r   set_block_rankr0      s_    
 >>cb>1::<LEEG3<<>?2" 	
	 	r   valmbar_ptrc                .   t        ||||      j                         }t        ||||      j                         }t        t        | t                    rt        |       } t        | t
        t        t        j                  f      sJ d       t
        dt        dt        j                  dit        |          }t
        dt        dt        j                  dit        |          }	t        j                  d || j                  ||      |gd	| d
d|	 ddd       y )Nr   z$val must be Float32, Int32, or Int64f32s32s64frlz6st.async.shared::cluster.mbarrier::complete_tx::bytes.z [$0], $1, [$2];zr,z,rTFr(   )r0   r,   r   r   floatr   r   cutlassInt64typer
   r-   )
r1   r%   r2   r&   r   r   remote_smem_ptr_i32remote_mbar_ptr_i32suffix
constraints
             r   store_shared_remoterB   .   s    )*hj  )*hj  *S%()clcGUGMM:;c=cc;ueUGMM5A$s)LF3sGMM3?S	JJOO	cllsrl:<OP
@HXY
ZLr   val0val1val2val3c                f   t        ||||      j                         }	t        ||||      j                         }
t        | t        t        f      sJ d       t        | t              rt        nt        }t        dt        di|   }t        dt        di|   }t        j                  d |	|
 ||       j                  ||       ||      j                  ||       ||      j                  ||       ||      j                  ||      gd| d| d	| d
| d| d| dd| d| d| d| dd       y )Nr   zval must be Float32, or Int32r4   r5   r7   r8   z{
	.reg .v4 .z abcd;
	mov.z abcd.x, $2;
	mov.z abcd.y, $3;
	mov.z abcd.z, $4;
	mov.zG abcd.w, $5;
	st.async.shared::cluster.mbarrier::complete_tx::bytes.v4.z [$0], abcd, [$1];
	}
zr,r,,TFr(   )r0   r,   r   r   r   r
   r-   )rC   rD   rE   rF   r%   r2   r&   r   r   r>   r?   r#   r@   rA   s                 r   store_shared_remote_x4rI   M   sw    )*hj  )*hj  dWe,-N/NN-!$0GeEueU+E2F3s+E2JOO$K  SR 0$K  SR 0$K  SR 0$K  SR 0	
	H h h h h DDJ8 L	 zl!J<qAj\B)r   abc          
         t        j                  t         j                  j                        dk(  rit	        t        j                  t        j                         t	        |       j                  ||      t	        |      j                  ||      ||            S t	        t        j                  t	        |       j                  ||      t	        |      j                  ||      ||            S )N   r   )
r;   r   CUDA_VERSIONmajorr   r   fminr   r4   r,   )rJ   rK   r   r   s       r   rP   rP   |   s    '..445;II
###3
###3
 	
 		AJCB/AJCB/		
 r   c          
          t        t        j                  t        j                         t        |       j                  ||      gdddd            S )Nr   zsqrt.approx.f32 $0, $1;z=f,fFr(   )r   r
   r-   r   r4   r,   rJ   r   r   s      r   sqrtrS      sJ    EEGQZ  SR 01%" 	
	 	r   c          
          t        t        j                  t        j                         t        |       j                  ||      gdddd            S )Nr   zcvt.rpi.ftz.s32.f32 $0, $1;z=r,fFr(   )r   r
   r-   r   r.   r   r,   rR   s      r   ceilrU      sJ    EEGQZ  SR 01)" 	
	 	r   tXsXtXpX
fill_valuec           	         t        j                  | d         }|j                  |       t        j                  | j
                  d   d         D ]|  }t        j                  | j
                  d         D ]U  }t        |du      r(||d|f   rt        j                  || d|fd|f          8t        j                  || d|fd|f          W ~ y)zFill out-of-bounds values in shared memory tensor.

    Args:
        tXsX: Shared memory tensor to fill
        tXpX: Predicate tensor indicating valid elements
        fill_value: Value to fill OOB locations with
    ))Nr   Nr   r   r      N)r   make_rmem_tensor_likefillr;   range_constexprshaper   autovec_copy)rV   rW   rX   	tXrX_fillrest_vrest_ks         r   fill_oobrc      s     **40B+CDINN:))$**Q-*:; Q--djjm< 	QF$d*+FAv-.%%itVndF6R1ST!!)T4.$2N-OP	QQr   c                   ddl m} t        |      }| j                  }|j                  j                  |g|      }t        j                  |||      }t        |      D ]_  \  }	}
t        j                   | |
      j                  ||      |t        j                  t        j                         |	||      ||      }a |S )zBuild an MLIR vector <N x elem_type> from N scalar DSL values.

    Example: make_vector(cutlass.Uint32, v0, v1) -> <2 x i32> MLIR vector
    r   )irr   )positionr   r   )cutlass._mlirre   len	mlir_type
VectorTypegetr
   
mlir_undef	enumerater   insertelementr,   _arithconstantr   r.   )	elem_typer   r   valuesre   nmlir_tyvec_tyvecivs              r   make_vectorry      s     !FA!!G]]sG,F
//&cb
1C&! 
1""aL!!cb!1__QUUWaSR@

 Jr   c          	         t        j                  t        j                   dt        j                               | j	                         |j	                         f||      }t        j
                  t        j                   dt        j                               |      }t        j                  t        j                  |g dg||            }|S )NrZ   r   r   r   dynamic_positionstatic_positionr   r   )
r   from_elementsr   r4   r,   bitcasti64r;   r<   extract)rJ   rK   r   r   	vec_f32x2	vec_i64x1ress          r   f32x2_to_i64r      s    $$	AEEGqzz|QZZ\:I qxx15573Y?I
--y2sPSXZ[C Jr   cc          	         t        j                  t        j                   dt        j                               | j	                         f||      }t        j
                  t        j                   dt        j                               |      }t        t        j                  |g dg||            }t        t        j                  |g dg||            }||fS )Nr   r   rZ   r   r{   )	r   r~   r   r   r,   r   r4   r   r   )r   r   r   r   r   res0res1s          r   i64_to_f32x2r      s    $$QXXa%9AJJL?PSXZ[Iqxx15573Y?Iy2sPSXZ[D y2sPSXZ[D :r   lanec                 \   t        |d u       rt        j                  j                         }t	        j
                  t        t        j                  t        j                  j                                    D ]4  }d|z  }t        j                  j                  | |d      }||k\  s0| |z  } 6 | S )Nr   r   )offsetmask_and_clamp)r   r   archlane_idxr;   r]   intmathlog2	WARP_SIZEshuffle_sync_up)r1   r   rw   r   partial_sums        r   warp_prefix_sumr      s    $$,yy!!#$$S4993F3F)G%HI aii//FST/U6>;C Jr   gmem_ptrc                   ddl m} |j                  dk(  rs|j                  dk(  rdt	        j
                  t        j                         t        j                  j                  |j                  t        |       j                               S t	        j
                  t        j                  j                  |j                  t        |       j                               S Nr   )rN   rM   	   )r   opptrrJ   )r   r   rJ   )r;   rN   rO   minorr   	atomicrmwr   r.   AtomicOpKindINCllvm_ptrr   r,   rJ   r   r   r   rN   s        r   atomic_inc_i32r         $ RL$6$6!$;~~D--11x7H7HERSHL]L]L_
 	

 ~~  $$(*;*;uQx?P?P?R
 	
r   c                   ddl m} |j                  dk(  rs|j                  dk(  rdt	        j
                  t        j                         t        j                  j                  |j                  t        |       j                               S t	        j
                  t        j                  j                  |j                  t        |       j                               S r   )r;   rN   rO   r   r   r   r   r.   r   ADDr   r   r,   r   s        r   atomic_add_i32r     r   r   clc_response_ptrc                 f    | j                   }|j                   }t        j                  ||||       y)aN  
    The clusterlaunchcontrol.try_cancel instruction requests atomically cancelling the launch
    of a cluster that has not started running yet. It asynchronously writes an opaque response
    to shared memory indicating whether the operation succeeded or failed. On success, the
    opaque response contains the ctaid of the first CTA of the canceled cluster.

    :param mbar_ptr: A pointer to the mbarrier address in SMEM
    :type mbar_ptr:  Pointer
    :param clc_response_ptr: A pointer to the cluster launch control response address in SMEM
    :type clc_response_ptr:  Pointer
    r   N)r   r   clusterlaunchcontrol_try_cancel)r2   r   r   r   mbar_llvm_ptrclc_response_llvm_ptrs         r   issue_clc_query_nomulticastr   &  s5    $ %%M,55((	r   tensorc                @   t        |j                  t        j                        sJ t        j                  |j
                  t        ||       j                         |j                  |j                  j                        }t        j                  ||j                        S )N)assumed_align)r   r   r   r    make_ptrelement_typer   r+   memspace	alignmentr!   r   )r   r   r   r   new_ptrs        r   domain_offset_alignedr   B  ss     foot||444mmVU#))+oo//	G GV]]33r   )N)NN).r   typingr   r   r   r;   cutlass.cuter   r   r   r   cutlass._mlir.dialectsr	   ro   r
   r   r   cutlass.cutlass_dslr   r   TensorCoordr    r   jitr$   r0   r:   r<   IntrB   rI   rP   rS   rU   Numericrc   ry   r   r   r   r   r   r   r   r    r   r   <module>r      s)    ) )   . . 2 5 5 . ;?D FDKK F

 FT\\ F F $+  
 DHTll6;
 "  		5	 7==	0ll ll #kkoo	 
 <  	+
E/+
E/+ E/+ E/	+
 ll+ ll+ #kkoo+ 
+ +\ DHT E%.! eE7N&; V]  * $(T 
EGO 
g 
 
 $(T 
EGO 
e 
 
 Q4;; Qht{{&; Q QZ^ Q 
Q0 (,  , 04 G  '--   *.4 	GMM 	E'7BR<S 	 	 	 	huo 	 	 
	 BF4 
cEk 
T\\ 
TY 
 
  BF4 
cEk 
T\\ 
TY 
 
   		llll
 
 6 37D4::4#{{4	[[4 4r   