Ë
    ÍGìjl*  ã                  ó  — 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)ÚUnionNz‡The 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© ó    úVC:\Users\Stabi\Downloads\virtual-tryon(1)\venv\Lib\site-packages\jaxlib/hlo_helpers.pyÚ<lambda>r   4   s   € ¤"§.¡.×"4Ñ"4´R·Z±Z·^±^Ó5EÔ"Fr   c                 óx   — t         j                  j                  t         j                  j                  «       «      S r   )r   r   r   ÚF64Typer   r   r   r   r   5   s   € ¤2§>¡>×#5Ñ#5´b·j±j·n±nÓ6FÔ#Gr   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   € Ü	"¤2§8¡8¨E£?Ñ	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   è ø€ Ð	1©& QŒj˜œCÓ Ô
 ©&ùs   ‚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Í   € ð !#€,Ø"$€-Üñ )á'ó)ó )Ðó *�o€vˆuÙØ×Ñœ<¨Ó/Ô0Ø×ÑÜ
×Ñ×ÑáóÙ�ô ˜Q¤Ô$‰Q¬"¯-©-×*HÑ*HÓ*JÑJØñàó	õð *ð Ù-ˆ-ð
9ð 9Ø37ð
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   ø€ ÜˆAƒw”#�~Ü”r—x‘x  ¬2¯8©8Ô4Ó5Ð5à	
�‰�8Ò	Ü�K‰K˜ !Ó$ˆÜ�[‰[˜ Ó"Ð"r   r   r   r   )r"   r   r>   r<   r=   Úlenr?   Úconcatenater   ÚIntegerAttrr   ÚIntegerTypeÚget_signless)ÚsizesrD   ÚszÚdsrB   rC   s       @@r   r/   r/   \   s±   ù€ Ü
  ¤r§x¡xÓ
0€%Ü# B¬¯©Ó1€(õ#ñ $)Ó)¡5˜R‰�RÕ 5€"Ð)Ù	Ü”R—X‘X˜b¤"§(¡(Ó+Ó,Ð,Ü
ˆ2ƒw�!‚|Øˆa‰5€Lä�?‰?Ø
ŒB�N‰N×Ñœ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   € Ü	�A”r—z‘zÔ	"Ð"Ð	"Ü	�‰Ü×Ñ×Ñ˜qÔ'7¸¿¹Ó'@ÐÓAó
Cð Cr   c                ó^   — t        t        j                  | t        j                  ¬«      «      S r:   )r<   r   r=   Úuint8rR   s    r   Úhlo_u8rV   t   ó   € Ü	”2—8‘8˜A¤R§X¡XÔ.Ó	/Ð/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Ô)Œ�‹Ð0¨qÐ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”�^ÜˆAƒw”#�~Ü��A‹YÐÜ�‹
€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”�^ÜˆAƒw”#�~Ø�‰U€lÜ�‹
€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ä
ÐCÄcÈ.ÓFYÑYÓ
ZÐZÜÜ—}‘}×(Ñ(Ð)9Ó:Ü—k‘k—o‘o oÓ6Ø(Ü—.‘.×$Ñ$Ü
�.‰.×
%Ñ
% bÓ
)¨;ó8äŸ,™,×*Ñ*Ù6IÓ
JÑ6I¨dŒ2×Ñ×#Ñ# DÕ)Ð6IÑ
Jóô	€*ð Ð'Ü+-¯<©<×+;Ñ+;ð %;×$@Ñ$@Ó$BÒ$HÀbÐ$Hô	=ñ %IÑ
ˆ)�Zô 
×Ñ× Ñ ô 03°<Ó/@À1Ò/D 
™|È"Ø!Ø "ð !õ ð %Iò	=ó 	,€JÐ'Ñ(ð Ð!Ø×ÑÐ&Ô'àÐô /1×.EÑ.E×.IÑ.IÜ
�
‰
”4œœc (›m¬S°«]¼SÀÓ=OÑ-OÓPÓQÜŸ™ô	#ó/$€JÐ*Ñ+ð Ð"Ü�Ó!¤S¨£]Ò2ÐO°_ÀhÐ4OÓOÐ2Ü˜_Ó-°°¼¸]Ó9KÑ0KÑK€oÜ�H‹~¤ ]Ó 3Ñ3€HàÐ Ü$&§L¡L×$4Ñ$4ñ />ó6ñ />¨ô 	×Ñ×#Ñ#Ü�M‰Mœ"Ÿ*™* Q¬b¯h©hÔ7Ó8Ü—‘×!Ñ!Ó#ð 	$õ 	%à.=ñ6ó %€JÐ Ñ!ð
 ÐØÐ%Ð%Ð%Üˆ~Ó¤# lÓ"3Ò3ð &Ø˜ð6&ó &Ð3ä#%§<¡<×#3Ñ#3ñ /=ó5ñ /=¨ô 	×Ñ×#Ñ#Ü�M‰Mœ"Ÿ*™* Q¬b¯h©hÔ7Ó8Ü—‘×!Ñ!Ó#ð 	$õ 	%à.<ñ5ó $€JÐÑ ô ×Ñ×%Ñ%¨lÀXØ1;ð &ó =€"ä�¤Ô%ÜŸ+™+Ÿ/™/¨.Ó9ÐØ5H€B‡L�L×ÑÐ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àôð €"‡(�(ˆ2�8‰8Ó‘g˜bŸn™n×9Ñ9¸1Ó=Ø
€"‡(�(ˆ2�7‰7Ó‘W˜RŸ^™^×8Ñ8¸!Ó<Ø
€"‡(�(ˆ2�8‰8Ó‘g˜bŸn™n×9Ñ9¸2Ó>Ø
€"‡(�(ˆ2�8‰8Ó‘g˜bŸn™n×9Ñ9¸2Ó>Ø
€"‡(�(ˆ2�8‰8Ó‘g˜bŸn™n×9Ñ9¸2Ó>Ø
€"‡(�(ˆ2�8‰8Ó‘g˜bŸn™n×9Ñ9¸1Ó=Ø
€"‡(�(ˆ2�9‰9Ó‘w˜rŸ~™~×:Ñ:¸BÓ?Ø
€"‡(�(ˆ2�9‰9Ó‘w˜rŸ~™~×:Ñ:¸BÓ?Ø
€"‡(�(ˆ2�9‰9Ó‘w˜rŸ~™~×:Ñ:¸BÓ?Ø
€"‡(�(ˆ2�:‰:Ó˜Ÿ
™
Ÿ™Ø
€"‡(�(ˆ2�:‰:Ó˜Ÿ
™
Ÿ™Ø
€"‡(�(ˆ2�:‰:Ó˜Ÿ
™
Ÿ™Ø
€"‡(�(ˆ2�<‰<ÓÑFØ
€"‡(�(ˆ2�=‰=ÓÑGðEÐ ÐAó ó 6óAð �c˜2Ÿ8™8�mÑ$€Ø�h˜}Ñ-¨r¯w©wÐ6Ñ7€ð9Ø-ð9à0ó9ó(Dó&Có
0ó0ó1ó<óóð" =?Ø!Ø/3Ø)+ØØ48Ø6:Ø59Ø7;ñnØðnð $ðnð !ð	nð
 :ðnð ðnð -ðnð 'ðnð ðnð 2ðnð 4ðnð 3ðnð 5ðnð ônr   