
    wvhl*                       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
Z
ddlmc mZ ddlmc mc mZ ddlZ e
j(                  de        ej,                  ej.                         eej0                  j2                  d       ej,                  ej4                         eej0                  j2                  d	       ej,                  ej6                         eej0                  j2                  d
       ej,                  ej8                         eej0                  j2                  d       ej,                  ej:                         eej0                  j2                  d       ej,                  ej<                         eej0                  j>                  d	       ej,                  ej@                         eej0                  j>                  d
       ej,                  ejB                         eej0                  j>                  d       ej,                  ejD                         eej0                  j>                  d       ej,                  ejF                        ejH                  jJ                   ej,                  ejL                        ejN                  jJ                   ej,                  ejP                        ejR                  jJ                   ej,                  ejT                        d  ej,                  ejV                        d iZ,de-d<   d"dZ.d#dZ/e	e0ejb                  f   Z2e3ee2   ejh                  f   Z5	 	 	 	 d$dZ6d%dZ7d&dZ8d'dZ9d'dZ:d(dZ;d)dZ<d*dZ=d*dZ>dddddddddd 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+d!Z?y),zFA small library of helpers for use in jaxlib to build MLIR operations.    )annotations)CallableSequence)partial)UnionNzThe jaxlib.hlo_helpers submodule is deprecated. Instead, use jax.ffi if possible or, for lower-level operations, jax.interpreters.mlir.             @   c                 x    t         j                  j                  t         j                  j                               S N)irComplexTypegetF32Type     M/opt/face_recognition/venv/lib/python3.12/site-packages/jaxlib/hlo_helpers.py<lambda>r   4   s     ".."4"4RZZ^^5E"F r   c                 x    t         j                  j                  t         j                  j                               S r   )r   r   r   F64Typer   r   r   r   r   5   s     2>>#5#5bjjnn6F#G r   z%dict[np.dtype, Callable[[], ir.Type]]_dtype_to_ir_type_factoryc                B    t        t        j                  |                 S r   )r   npdtyper   s    r   dtype_to_ir_typer   7   s    	"288E?	3	55r   c                T    t         j                  j                  | t        |            S r   )r   RankedTensorTyper   r   )shaper   s     r   shape_dtype_to_ir_typer"   ;   s!    				 	 (8(?	@@r   c                l   g }g }t        d | D              }| D ]  \  }}|r|j                  t        |             |j                  t        j                  j                  |D cg c]2  }t        |t              r|nt        j                  j                         4 c}|              ||r|fS d fS c c}w )Nc              3  F   K   | ]  \  }}t        d  |D                yw)c              3  >   K   | ]  }t        |t                 y wr   )
isinstanceint).0ds     r   	<genexpr>z7mk_result_types_and_shapes.<locals>.<genexpr>.<genexpr>N   s     	1QjC 
 	1s   N)any)r(   rshape_s      r   r*   z-mk_result_types_and_shapes.<locals>.<genexpr>M   s'      )
&! 
	1&	11)s   !)
r+   appendshape_tensorr   r    r   r&   r'   
ShapedTypeget_dynamic_size)shape_type_pairsresult_typesresult_shapeshas_dynamic_shapesr,   rtyper)   s          r   mk_result_types_and_shapesr7   H   s     !#,"$- )') ) * ovu</0
 Q$Q"--*H*H*JJ 	 --
9 937
9 9s   #7B1c                   t        dt        j                        t        dt        j                        fd}| D cg c]
  } ||       }}|s-t        t        j                  g t        j                              S t        |      dk(  r|d   S t        j                  |t        j                  j                  t        j                  j                  d      d            S c c}w )N)r   r   c                    t        |       t        u r/t        t        j                  | gt        j
                              S | j                   k7  rt        j                  |       } t        j                  |       S Nr   )	typer'   	hlo_constr   arrayint32hloconvertreshape)r)   i32_typeint1ds    r   dim_to_i32x1z"shape_tensor.<locals>.dim_to_i32x1_   sW    Aw#~rxx288455	
8	KK!$[[""r   r   r   r   )r"   r   r>   r<   r=   lenr?   concatenater   IntegerAttrr   IntegerTypeget_signless)sizesrD   szdsrB   rC   s       @@r   r/   r/   \   s    
 rxx
0%#B1(# $))RR)")	RXXb"((+,,
2w!|a5L??
BNNr~~::2>BD D *s   C%c                    t        | t        j                        sJ t        j                  t
        j                  j                  | t        | j                                    S )Nr;   )
r&   r   ndarrayr?   constantr   DenseElementsAttrr   r   r   xs    r   r<   r<   o   sL    	Arzz	""	"	q'7'@A
C Cr   c                ^    t        t        j                  | t        j                              S r:   )r<   r   r=   uint8rR   s    r   hlo_u8rV   t       	288ARXX.	//r   c                ^    t        t        j                  | t        j                              S r:   )r<   r   r=   r>   rR   s    r   hlo_s32rY   v   rW   r   c                <    t        | t              rt        |       S | S r   )r&   r'   rY   rR   s    r   ensure_hlo_s32r[   y   s    !!S)0q0r   c                    t         j                  j                  t        j                  | t        j
                              S r   )r   DenseI64ArrayAttrr   r   asarrayint64)xss    r   dense_int_arrayra   |   s(    				!	!"**R":	;;r   c                    t        |       t        u r(t        |      t        u rt        | |      S t        |       } t        |      t        u rt        |      }t	        j
                  | |      S r   )r;   r'   minrY   r?   minimumrS   ys     r   hlo_minrg      sR    	!W^Aw#~AY
A	!W^
A	Q	r   c                    t        |       t        u r!t        |      t        u r| |z   S t        |       } t        |      t        u rt        |      }t        j                  | |      S r   )r;   r'   rY   r?   addre   s     r   hlo_addrj      sN    	!W^Aw#~Ul
A	!W^
A	Ar    Fr      )	backend_confighas_side_effectr4   called_computationsapi_versionoperand_output_aliasesoperand_layoutsresult_layoutsextra_attributesc               |	   t        |      }| t        j                  j                  d      }nt	        |t
        t        f      r t        j                  j                  |      }nIt	        |t              r"t        j                  j                  d      }d}nt        dt        |      z         t        t        j                  j                  |       t        j                  j                  |      |t        j                  j                  t        j                  j                  d      |      t        j                  j                  |D cg c]!  }t        j                  j                  |      # c}            }|xt        j                  j                  |j                         xs dD cg c]8  \  }}t         j"                  j                  t%        |      dkD  r|gng |g       : c}}      |d<   ||j'                  |       |t        j(                  j                  t+        j,                  t        t/        t%        |      t%        |      t%        |      z               t*        j0                  	            |d
<   |	;t%        |	      t%        |      k(  s	J |	|f       t        |	      dgt%        |      z  z   }	t        |      t        |      z   }|	t        j                  j                  |	D cg c]u  }t        j(                  j                  t+        j2                  t+        j,                  |t*        j0                  	            t        j4                  j                               w c}      |d<   |
|
J t%        |
      t%        |      k(  s	J |
|f       t        j                  j                  |
D cg c]u  }t        j(                  j                  t+        j2                  t+        j,                  |t*        j0                  	            t        j4                  j                               w c}      |d<   t         j6                  j9                  |||      }t	        |t              r8t        j:                  j                  |      }||j<                  j>                  d<   |S c c}w c c}}w c c}w c c}w )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.
  rk   r   z,custom_call backend_config unexpected type: r   )call_target_namern   rm   rp   ro   r   )output_tuple_indicesoperand_indexoperand_tuple_indicesoutput_operand_aliasesr   indices_of_shape_operands)r   rN   rr   rs   )resultsoperands
attributeszmhlo.backend_config) listr   
StringAttrr   r&   strbytesdict
ValueErrorBoolAttrrG   rH   rI   	ArrayAttrFlatSymbolRefAttritemsr?   OutputOperandAliasrE   updateDenseIntElementsAttrr   r^   ranger_   
atleast_1d	IndexTypeCustomCallOpbuild_genericDictAttr	operationr~   )rv   r3   r}   rm   rn   r4   ro   rp   rq   rr   rs   rt   backend_config_attrnamer~   	input_idx
output_idxlops                      r   custom_callr      s   B (^(--++B/.3,/--++N;.$' --++B/K
Cc.FYY
ZZ}}(()9:kkooo6(..$$
..
%
%b
);8,,**6I
Jd2##D)
J	* '+-<<+;+; %;$@$@$B$Hb	=  )Z 
   03</@1/D
|"! " ! 	= 	,J'( !&' /1.E.E.I.I


4c(mS]S=O-OPQ	#/$J*+ "!S]2O_h4OO2_-]9K0KKoH~] 33H $&LL$4$4 />6 *+ 	##MM"**Qbhh78!!# 	$ 	%6 %J !
 %%%~#l"33 &6& &3#%<<#3#3 /=5 *+ 	##MM"**Qbhh78!!# 	$ 	%5 $J  %%lX1; & ="%++//.95HBLL12	)g K	=665s   &R),=R.
2A:R49A:R9)returnir.Type)r!   zSequence[int]r   r   )r2   zSequence[ShapeTypePair]r   z+tuple[list[ir.Type], list[ir.Value] | None])rJ   zSequence[int | ir.Value]r   ir.Value)rS   z
np.ndarrayr   r   )rS   r'   )rS   DimensionSize)r   zir.DenseI64ArrayAttr)rS   r   rf   r   r   r   )rv   r   r3   zSequence[ir.Type]r}   zSequence[ir.Value]rm   z%str | bytes | dict[str, ir.Attribute]rn   boolr4   zSequence[ir.Value] | Nonero   zSequence[str]rp   r'   rq   zdict[int, int] | Nonerr   Sequence[Sequence[int]] | Noners   r   rt   zdict[str, ir.Attribute] | Noner   zir.Operation)@__doc__
__future__r   collections.abcr   r   	functoolsr   typingr   warningsjaxlib.mlir.irmlirr   jaxlib.mlir.dialects.stablehlodialects	stablehlor?   numpyr   warnDeprecationWarningr   bool_rH   rI   int8int16r>   r_   rU   get_unsigneduint16uint32uint64float16F16Typer   float32r   float64r   	complex64
complex128r   __annotations__r   r"   r'   Valuer   tupleTypeShapeTypePairr7   r/   r<   rV   rY   r[   ra   rg   rj   r   r   r   r   <module>r      sF   M " .      , ,  F "((288gbnn991=
"((277WR^^88!<
"((288gbnn992>
"((288gbnn992>
"((288gbnn992>
"((288gbnn991=
"((299wr~~::B?
"((299wr~~::B?
"((299wr~~::B?
"((2::


"((2::


"((2::


"((2<<F
"((2==GE A  6A c288m$h}-rww679-909(D&C
001<" =?!/3)+486:597;nn $n !	n
 :n n -n 'n n 2n 4n 3n 5n nr   