Ë
    ßGìjÄ  ã                  ó<  — d Z ddlmZ ddlmZ ddlZddl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 dd
l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 ddlmZ ddlmZmZm Z   ejB                  d«      Z"de"_#        dd„Z$e"jJ                  d„ «       Z&e"jN                  d„ «       Z(d„ Z)d„ Z*d„ Z+e)ejX                  e"<   e*ejZ                  e"<   e+ej\                  e"<    ej^                  e" ej`                  e&d¬«      «       	 	 d	 	 	 dd„Z1	 	 d 	 	 	 	 	 d!d„Z2y)"a  JAX primitives related to sparse operations.

This is experimental work to explore sparse support in JAX.

The primitives defined here are deliberately low-level: each primitive implements
a common sparse operation (sparse to dense, dense to sparse, sparse matrix/vector
product, sparse matrix/matrix product) for two common sparse representations
(CSR and COO).

These routines have reference implementations defined via XLA scatter/gather
operations that will work on any backend, although they are not particularly
performant. On GPU runtimes built against CUDA 11.0/ROCm 5.0 or newer, each operation is
computed efficiently via cusparse/hipsparse.

Further down are some examples of potential high-level wrappers for sparse objects.
(API should be considered unstable and subject to change).
é    )Úannotations)ÚpartialN)Ú	tree_util)Ú	JAXSparse)ÚBCOO)ÚBCSR)ÚCOO)ÚCSRÚCSC)Ú_coo_extract)Úmlir)Úcore)Údtypes)Úad)Úbatching)ÚArrayÚ	DTypeLikeÚShapeÚtodenseFc                ó\   — t        j                  | «      \  }}t        j                  |d|iŽS )zIConvert input to a dense matrix. If input is already dense, pass through.Útree)r   Útree_flattenÚ	todense_pÚbind)ÚarrÚbufsr   s      ú_C:\Users\Stabi\Downloads\virtual-tryon(1)\venv\Lib\site-packages\jax/experimental/sparse/api.pyr   r   =   s*   € ä×%Ñ% cÓ*�*€$ˆÜ	�‰˜Ð	) DÑ	)Ð)ó    c                ór   — t        j                  | |«      }t        |t        «      r|j	                  «       S |S ©N)r   Útree_unflattenÚ
isinstancer   r   ©r   r   r   s      r   Ú_todense_implr$   B   s.   € ä× Ñ   tÓ,€#Ü$ S¬)Ô4ˆ�‰‹Ð=¸#Ð=r   c                óø   — t        j                  | |«      }t        |t        j                  «      r|S t        j                  |j
                  |j                  t        j                  |j                  «      ¬«      S )N)Ú	weak_type)
r   r!   r"   r   ÚShapedArrayÚshapeÚdtyper   Úis_weakly_typedÚdatar#   s      r   Ú_todense_abstract_evalr,   G   sX   € ä× Ñ   tÓ,€#Ü�”T×%Ñ%Ô&Ø€JÜ	×	Ñ	˜#Ÿ)™) S§Y¡Y¼&×:PÑ:PÐQT×QYÑQYÓ:ZÔ	[Ð[r   c               óÞ   — t        |d   t        j                  «      rJ ‚t        d„ |dd  D «       «      sJ ‚t	        j
                  | d|iŽ}t	        j
                  |d   g| dd  ¢­d|iŽ}||fS )Nr   c              3  óP   K  — | ]  }t        |t        j                  «      –— Œ  y ­wr    )r"   r   ÚZero)Ú.0Úts     r   Ú	<genexpr>z_todense_jvp.<locals>.<genexpr>P   s   è ø€ Ð:©\¨ŒZ˜œ2Ÿ7™7×#©\ùs   ‚$&é   r   )r"   r   r/   Úallr   r   )ÚprimalsÚtangentsr   Úprimals_outÚtangents_outs        r   Ú_todense_jvpr9   N   st   € Ü˜ ™¤R§W¡WÔ-Ð-Ð	-Ü	Ñ:¨X°a°b©\Ó:Ô	:Ð:Ð	:Ü—‘ Ð3¨dÑ3€+Ü—‘ ¨¡ÐE¨g°a°b¨kÒEÀÑE€,Ø	�lÐ	"Ð"r   c               óä  — t        j                  |d   «      sJ ‚t        d„ |dd  D «       «      rJ ‚t        «       }t	        j
                  ||gt        |«      z  «      }ddlm}m	} ddl
m} ddlm} ||u r| fS t        ||«      r|\  }	}
 ||
| «      |
fS t        ||«      r|\  }	}
} ||
|| «      |
|fS t        |t        «      r|\  }	}}t!        ||| «      ||fS t#        dt%        |«      › �«      ‚)Nr   c              3  óF   K  — | ]  }t        j                  |«      –— Œ y ­wr    )r   Úis_undefined_primal)r0   Úbufs     r   r2   z%_todense_transpose.<locals>.<genexpr>W   s   è ø€ ÐA¹°”×'Ñ'¨×,¹ùs   ‚!r3   )r   r   )Ú_bcoo_extract)Úbcsr_extractztodense_transpose for )r   r<   ÚanyÚobjectr   r!   ÚlenÚjax.experimental.sparser   r   Újax.experimental.sparse.bcoor>   Újax.experimental.sparse.bcsrr?   r"   r	   r   ÚNotImplementedErrorÚtype)Úctr   r   ÚstandinÚobjr   r   r>   r?   Ú_ÚindicesÚindptrÚrowÚcols                 r   Ú_todense_transposerP   U   s  € Ü	×	Ñ	  Q¡Ô	(Ð(Ð	(ÜÑA¸¸Q¸R¹ÓAÔAÐAÐ	Aä‹H€'Ü× Ñ  ¨ y´3°t³9Ñ'<Ó=€#ß0Ý8Ý7ØˆG�^Øˆ5€LÜ�#�tÔØ�J€A€wÙ˜ "Ó% wÐ.Ð.Ü�#�tÔØÑ€A€w�Ù˜ ¨Ó,¨g°vÐ=Ð=Ü�#”sÔØ�K€A€sˆCÜ˜˜S "Ó% s¨CÐ/Ð/ä
Ð 6´t¸C³y°kÐBÓ
CÐCr   c               óV   —  t        j                  t        t        |¬«      |«      | Ž dfS )N)r   r   )ÚjaxÚvmapr   r$   )Úbatched_argsÚ
batch_dimsr   s      r   Ú_todense_batching_rulerV   l   s'   € Ø	@Œ�‰”'œ-¨dÔ3°ZÓ	@À,Ð	OÐQRÐ	RÐRr   )Úmultiple_resultsc           	     óÈ   — t         t        t        t        t        dœ}||vr(t        d|›dt        |j                  «       «      › �«      ‚||   } |j                  | f||dœ|¤ŽS )a~  Create an empty sparse array.

  Args:
    shape: sequence of integers giving the array shape.
    dtype: (optional) dtype of the array.
    index_dtype: (optional) dtype of the index arrays.
    format: string specifying the matrix format (e.g. ['bcoo']).
    **kwds: additional keywords passed to the format-specific _empty constructor.
  Returns:
    mat: empty sparse matrix.
  )ÚbcsrÚbcooÚcooÚcsrÚcsczsparse_format=z  not recognized; must be one of )r)   Úindex_dtype)	r   r   r	   r
   r   Ú
ValueErrorÚlistÚkeysÚ_empty)r(   r)   r^   Úsparse_formatÚkwdsÚformatsÚclss          r   Úemptyrg   v   sr   € ô ¤4´¼CÌÑL€'Ø˜'Ñ!Ü
�~ mÐ%6ð 7'Ü'+¨G¯L©L«NÓ';Ð&<ð>ó ?ð ?à�Ñ€#Ø	ˆ�‰�EÐ	H °KÑ	HÀ4Ñ	HÐHr   c           	     óJ  — t         t        t        t        dœ}|€| }t	        j
                  t        j                  | «      } t	        j
                  t        j                  |«      }t	        j
                  t        j                  |«      }||   } |j                  d|| |||dœ|¤ŽS )aÇ  Create 2D sparse identity matrix.

  Args:
    N: int. Number of rows in the output.
    M: int, optional. Number of columns in the output. If None, defaults to `N`.
    k: int, optional. Index of the diagonal: 0 (the default) refers to the main
       diagonal, a positive value refers to an upper diagonal, and a negative value
       to a lower diagonal.
    dtype: data-type, optional. Data-type of the returned array.
    index_dtype: (optional) dtype of the index arrays.
    format: string specifying the matrix format (e.g. ['bcoo']).
    **kwds: additional keywords passed to the format-specific _empty constructor.

  Returns:
    I: two-dimensional sparse matrix with ones along the k-th diagonal.
  )rZ   r[   r\   r]   )ÚMÚNÚkr)   r^   © )	r   r	   r
   r   r   Úconcrete_or_errorÚoperatorÚindexÚ_eye)	rj   ri   rk   r)   r^   rc   rd   re   rf   s	            r   Úeyerq   ‹   s‰   € ô$ ¤#¬c¼#Ñ>€'Ø€YØ	€AÜ
×ÑœXŸ^™^¨QÓ/€!Ü
×ÑœXŸ^™^¨QÓ/€!Ü
×ÑœXŸ^™^¨QÓ/€!à�Ñ€#Ø	ˆ�‰Ð	N�A˜˜a u¸+Ñ	NÈÑ	NÐNr   )r   zJAXSparse | ArrayÚreturnr   )NÚint32rZ   )
r(   r   r)   úDTypeLike | Noner^   r   rc   Ústrrr   r   )Nr   Nrs   rZ   )rj   Úintri   z
int | Nonerk   rv   r)   rt   r^   r   rc   ru   rr   r   )3Ú__doc__Ú
__future__r   Ú	functoolsr   rn   rR   r   Újax.experimental.sparse._baser   rD   r   rE   r   Újax.experimental.sparse.coor	   Újax.experimental.sparse.csrr
   r   Újax.experimental.sparse.utilr   Újax.interpretersr   Újax._srcr   r   Újax._src.interpretersr   r   Újax._src.typingr   r   r   Ú	Primitiver   rW   r   Údef_implr$   Údef_abstract_evalr,   r9   rP   rV   Úprimitive_jvpsÚprimitive_transposesÚprimitive_batchersÚregister_loweringÚ	lower_funrg   rq   rl   r   r   Ú<module>rŠ      sK  ðñõ$ #å Û ã 
Ý Ý 3Ý -Ý -Ý +ß 0Ý 5Ý !å Ý Ý $Ý *ß 3Ñ 3ð ˆD�N‰N˜9Ó%€	Ø"€	Ô ó*ð
 ×Ññ>ó ð>ð ×Ññ\ó ð\ò#òDò.Sð  ,€× Ñ �)Ñ Ø%7€× Ñ ˜	Ñ "Ø)?€× Ñ ˜IÑ &Ø €× Ñ �y . $§.¡.Ø Eô#+ô ,ð PWØ%ðIØðIØ2;óIð* MQØ?EðOØðOØ9<ðOØR[ôOr   