
    SpfF(                    2   U d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mc mZ ddlmc mc mZ ddlZ ej        ej                   eej        j        d           ej        ej                   eej        j        d           ej        ej                   eej        j        d	           ej        ej                   eej        j        d
           ej        ej                   eej        j        d           ej        ej                   eej        j        d           ej        ej                   eej        j        d	           ej        ej                   eej        j        d
           ej        ej                   eej        j        d           ej        ej                   ej!        j"         ej        ej#                  ej$        j"         ej        ej%                  ej&        j"         ej        ej'                  d  ej        ej(                  d iZ)de*d<   dHdZ+dIdZ,e	e-ej.        f         Z/e0ee/         ej1        f         Z2dJdZ3dKdZ4dLd Z5dMd"Z6dMd#Z7dNd%Z8dOd'Z9dPd)Z:dPd*Z;d+d,dd-d.ddddd/	dQdGZ<dS )RzFA small library of helpers for use in jaxlib to build MLIR operations.    )annotations)CallableSequence)partial)UnionN             @   c                 x    t           j                            t           j                                                  S N)irComplexTypegetF32Type     R/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jaxlib/hlo_helpers.py<lambda>r   )   s"    "."4"4RZ^^5E5E"F"F r   c                 x    t           j                            t           j                                                  S r   )r   r   r   F64Typer   r   r   r   r   *   s"    2>#5#5bjnn6F6F#G#G r   z%dict[np.dtype, Callable[[], ir.Type]]_dtype_to_ir_type_factoryreturnir.Typec                N    t          t          j        |                                S r   )r   npdtyper   s    r   dtype_to_ir_typer    ,   s    	"28E??	3	5	55r   shapeSequence[int]c                \    t           j                            | t          |                    S r   )r   RankedTensorTyper   r    )r!   r   s     r   shape_dtype_to_ir_typer%   0   s$    			 	 (8(?(?	@	@@r   shape_type_pairsSequence[ShapeTypePair]+tuple[list[ir.Type], list[ir.Value] | None]c                   g }g }t          d | D                       }| D ]f\  }}|r"|                    t          |                     |                    t          j                            d |D             |                     g||r|nd fS )Nc              3  N   K   | ] \  }}t          d  |D                       V  !dS )c              3  B   K   | ]}t          |t                     V  d S r   )
isinstanceint.0ds     r   	<genexpr>z7mk_result_types_and_shapes.<locals>.<genexpr>.<genexpr>C   s/      	1	1QjC  
 	1	1	1	1	1	1r   N)any)r/   rshape_s      r   r1   z-mk_result_types_and_shapes.<locals>.<genexpr>B   sQ       ) )
&! 
	1	1&	1	1	111) ) ) ) ) )r   c                x    g | ]7}t          |t                    r|nt          j                                        8S r   )r,   r-   r   
ShapedTypeget_dynamic_sizer.   s     r   
<listcomp>z.mk_result_types_and_shapes.<locals>.<listcomp>J   sI        Q$$JQQ"-*H*H*J*J   r   )r2   appendshape_tensorr   r$   r   )r&   result_typesresult_shapeshas_dynamic_shapesr3   rtypes         r   mk_result_types_and_shapesr?   =   s     !#,"$- ) )') ) ) ) ) *  ovu 1<//000
   	 	   
 -
7--4
9 9r   sizesSequence[int | ir.Value]ir.Valuec                   t          dt          j                  t          dt          j                  fdfd| D             }|s,t          t          j        g t          j                            S t          |          dk    r|d         S t          j        |t          j	        
                    t          j                            d          d                    S )N)r   r   c                    t          |           t          u r.t          t          j        | gt          j                            S | j         k    rt          j        |           } t          j        |           S Nr   )	typer-   	hlo_constr   arrayint32hloconvertreshape)r0   i32_typeint1ds    r   dim_to_i32x1z"shape_tensor.<locals>.dim_to_i32x1T   sc    Aww#~~rx28444555	
8		K!$$["""r   c                &    g | ]} |          S r   r   )r/   szrO   s     r   r8   z shape_tensor.<locals>.<listcomp>[   s#    )))RR)))r   r   r   r   )r%   r   rI   rG   rH   lenrJ   concatenater   IntegerAttrr   IntegerTypeget_signless)r@   dsrO   rM   rN   s     @@@r   r:   r:   Q   s    
 rx
0
0%#B11(# # # # # # *)))5)))"	 DRXb"(++,,,
2ww!||a5L?
BNr~::2>>BBD D Dr   x
np.ndarrayc                    t          | t          j                  sJ t          j        t
          j                            | t          | j	                                      S )NrF   )
r,   r   ndarrayrJ   constantr   DenseElementsAttrr   r    r   rX   s    r   rG   rG   d   sU    	Arz	"	""""	q'7'@'@AA
C 
C Cr   r-   c                \    t          t          j        | t          j                            S rE   )rG   r   rH   uint8r_   s    r   hlo_u8rb   i   !    	28ARX...	/	//r   c                \    t          t          j        | t          j                            S rE   )rG   r   rH   rI   r_   s    r   hlo_s32re   k   rc   r   DimensionSizec                N    t          | t                    rt          |           n| S r   )r,   r-   re   r_   s    r   ensure_hlo_s32rh   n   s!    !!S))	0q0r   ir.DenseI64ArrayAttrc                z    t           j                            t          j        | t          j                            S r   )r   DenseI64ArrayAttrr   r   asarrayint64)xss    r   dense_int_arrayro   q   s'    			!	!"*R":":	;	;;r   yc                   t          |           t          u r5t          |          t          u rt          | |          S t          |           } t          |          t          u rt          |          }t	          j        | |          S r   )rF   r-   minre   rJ   minimumrX   rp   s     r   hlo_minru   t   se    	!WW^^Aww#~~AYY

A	!WW^^

A	Q		r   c                    t          |           t          u r*t          |          t          u r| |z   S t          |           } t          |          t          u rt          |          }t          j        | |          S r   )rF   r-   re   rJ   addrt   s     r   hlo_addrx   ~   s^    	!WW^^Aww#~~Ul

A	!WW^^

A	Ar    Fr      )	backend_confighas_side_effectr<   called_computationsapi_versionoperand_output_aliasesoperand_layoutsresult_layoutsextra_attributescall_target_namestrr;   Sequence[ir.Type]operandsSequence[ir.Value]r{   %str | bytes | dict[str, ir.Attribute]r|   boolr<   Sequence[ir.Value] | Noner}   Sequence[str]r~   r   dict[int, int] | Noner   Sequence[Sequence[int]] | Noner   r   dict[str, ir.Attribute] | Noneir.Operationc               t   t          |          }| t          j                            d          }nt	          |t
          t          f          r t          j                            |          }nVt	          |t                    r"t          j                            d          }d}nt          dt          |          z             t          t          j                            |           t          j	                            |          |t          j
                            t          j                            d          |          t          j                            d |D                                 }|Bt          j                            fd|                                pd	D                       |d
<   ||                    |           |t          j                            t#          j        t          t'          t)          |          t)          |          t)          |          z                       t"          j                            |d<   |	Ot)          |	          t)          |          k    sJ |	|f            t          |	          dgt)          |          z  z   }	t          |          t          |          z   }|	,t          j                            d |	D                       |d<   |
\|
J t)          |
          t)                    k    sJ |
f            t          j                            d |
D                       |d<   t,          j                            ||          }t	          |t                    r.t          j                            |          }||j        j        d<   |S )a  Helper function for building an hlo.CustomCall.

  Args:
    call_target_name: the name of the custom call target
    result_types: the MLIR types of the results of the custom call
    operands: the MLIR IR values that are arguments to the custom call
    backend_config: an opaque string passed to the custom call kernel
    has_side_effect: if True, marks the custom call as effectful
    result_shapes: tensors that represent the result shapes, to be used when
      the results have dynamic shapes. If not-None, its length must match the
      number of the results.
    called_computations: the list of function names called by the custom call.
    api_version: the ABI contract version of the custom call
    operand_output_aliases: a dict mapping operand numbers to outputs they alias
    operand_layouts: a sequence of layouts (dimension orders) for each operand
    result_layouts: a sequence of layouts (dimension orders) for each result
    extra_attributes: additional IR attributes to apply to the custom_call.
  Nry   r   z,custom_call backend_config unexpected type: r   c                L    g | ]!}t           j                            |          "S r   )r   FlatSymbolRefAttrr   )r/   names     r   r8   zcustom_call.<locals>.<listcomp>   s)    
J
J
Jd2##D))
J
J
Jr   )r   r|   r{   r~   r}   c                    g | ]=\  }}t           j                            t                    d k    r|gng |g           >S )r   )output_tuple_indicesoperand_indexoperand_tuple_indices)rJ   OutputOperandAliasr   rR   )r/   	input_idx
output_idxr;   s      r   r8   zcustom_call.<locals>.<listcomp>   sj     	= 	= 	=  )Z 
   03</@/@1/D/D
||"! " !  	= 	= 	=r   r   output_operand_aliasesr   indices_of_shape_operands)r   c           
         g | ]o}t           j                            t          j        t          j        |t          j                             t           j                                                  pS r   r[   r   DenseIntElementsAttrr   r   
atleast_1drl   rm   	IndexTyper/   ls     r   r8   zcustom_call.<locals>.<listcomp>   sq     6 6 6 *+ 	##M"*Qbh77788!!## 	$ 	% 	%6 6 6r   r   c           
         g | ]o}t           j                            t          j        t          j        |t          j                             t           j                                                  pS r   r   r   s     r   r8   zcustom_call.<locals>.<listcomp>   sq     5 5 5 *+ 	##M"*Qbh77788!!## 	$ 	% 	%5 5 5r   r   )resultsr   
attributeszmhlo.backend_config)listr   
StringAttrr   r,   r   bytesdict
ValueErrorBoolAttrrT   rU   rV   	ArrayAttritemsupdater   r   rl   rangerR   rm   rJ   CustomCallOpbuild_genericDictAttr	operationr   )r   r;   r   r{   r|   r<   r}   r~   r   r   r   r   backend_config_attrr   ops    `             r   custom_callr      s   B (^^(-++B//.3,// [-++N;;.$'' [ -++B//KK
Cc.FYFYY
Z
ZZ}(()9::kooo66(.$$
.
%
%b
)
);8 8,**
J
J6I
J
J
J 	 	 	* '+-<+;+; 	= 	= 	= 	= %;$@$@$B$B$Hb	= 	= 	= 	, 	,J'( !&''' /1.E.I.I

4c(mmS]]S=O=O-OPPQQ	# 	# 	#/$ /$J*+ "!!S]]222_h4O222_--]9K9K0KKoH~~] 3 33H $&L$4$4 6 6 />6 6 6 % %J !
 %%%~#l"3"33336&333#%<#3#3 5 5 /=5 5 5 $ $J  %%lX1; & = ="%% I+//.995HBL12	)r   )r   r   )r!   r"   r   r   )r&   r'   r   r(   )r@   rA   r   rB   )rX   rY   r   rB   )rX   r-   )rX   rf   )r   ri   )rX   rf   rp   rf   r   rf   )r   r   r;   r   r   r   r{   r   r|   r   r<   r   r}   r   r~   r-   r   r   r   r   r   r   r   r   r   r   )=__doc__
__future__r   collections.abcr   r   	functoolsr   typingr   jaxlib.mlir.irmlirr   jaxlib.mlir.dialects.stablehlodialects	stablehlorJ   numpyr   r   bool_rU   rV   int8int16rI   rm   ra   get_unsigneduint16uint32uint64float16F16Typer   float32r   float64r   	complex64
complex128r   __annotations__r    r%   r-   Valuerf   tupleTypeShapeTypePairr?   r:   rG   rb   re   rh   ro   ru   rx   r   r   r   r   <module>r      s   M L L " " " " " " . . . . . . . .                      , , , , , , , , , , , ,     "(28ggbn91==
"(27WWR^8!<<
"(28ggbn92>>
"(28ggbn92>>
"(28ggbn92>>
"(28ggbn91==
"(29wwr~:B??
"(29wwr~:B??
"(29wwr~:B??
"(2:

"(2:

"(2:

"(2<FF
"(2=GGE      6 6 6 6A A A A c28m$h}-rw679 9 9 9(D D D D&C C C C
0 0 0 00 0 0 01 1 1 1< < < <      " =?!/3)+486:597;n n n n n n n nr   