a
    ij                     @  sD  d Z ddlmZ ddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZ ddlZejrbddlZeZejZe ddd	d
Ze ddddZddddZdd ZddddZdddddZdddddZdddddZdddd d!Zd/dd#ddd$d%d&Zdddd'd(Zd)ddd*d+Zdd,d-d.Z dS )0a  Compat utils between TF/Torch/Numpy/Jax.

Currently, each numpy API has slightly different behavior. Those functions
ensure compatibility so that the code works seamlessly between all APIs.

In the future, those functions could be deleted and replaced by the
official numpy API.
    )annotationsN)AnyOptional)numpy_utils)Array
FloatArrayzdict[torch_.dtype, _NpDType])returnc                  C  sb   t j} | jtj| jtj| jtj| jtj| jtj| j	tj	| j
tj
| jtj| jtj| jtj| jtjiS )z&Returns mapping torch -> numpy dtypes.)lazytorchboolnpZbool_Zuint8Zint8Zint16Zint32Zint64Zfloat16Zfloat32Zfloat64Z	complex64Z
complex128)r
    r   N/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/enp/compat.py_torch_to_np_dtypes-   s    r   zdict[np.dtype, torch_.dtype]c                   C  s   t dd t  D S )z&Returns mapping numpy -> torch dtypes.c                 s  s    | ]\}}t ||fV  qd S N)r   dtype).0tnr   r   r   	<genexpr>D       z&_np_to_torch_dtypes.<locals>.<genexpr>)dictr   itemsr   r   r   r   _np_to_torch_dtypesA   s    r   znp.dtypec                 C  s
   t  |  S )z2Returns the numpy dtype for the given torch dtype.)r   r   r   r   r   dtype_torch_to_npG   s    r   c                 C  s   t  t|  S )z2Returns the torch dtype for the given numpy dtype.)r   r   r   r   r   r   r   dtype_np_to_torchL   s    r   r   c                 C  s,   t jr|t ju rt| |jS t| |jS dS )z`isinstance(x, xnp.Array)`.N)r	   Z	has_torchr
   
isinstanceZTensorZndarrayxxnpr   r   r   is_array_xnpQ   s    r!   zArray['*d'])r   r   c                 C  s"   t | r| |S | |S dS )z`x.astype(dtype)`.N)r	   is_torchtypeastype)r   r   r   r   r   r$   Y   s    

r$   c                C  s4   t | }t | r"|j| |dS |j| |dS dS )z `xnp.expand_dims(x, axis=axis)`.axisN)r	   get_xnpr"   Z	unsqueezeexpand_dimsr   r&   r    r   r   r   r(   a   s    

r(   zlist[Array['*d']]c                C  s<   t | d }t | d r*|j| |dS |j| |dS dS )z `xnp.concatenate(x, axis=axis)`.r   r%   N)r	   r'   r"   concatZconcatenater)   r   r   r   r*   j   s    r*   zFloatArray['*d']c                 C  s   t | rt j| S |  S )z$`x.round()` for jnp, tnp, np, otrch.)r	   is_tftnparoundroundr   r   r   r   r.   v   s    
r.   FzOptional[int])r   r&   keepdimsr   c                 C  s8   t | rt jj| ||dS t | }|jj| ||dS )z4Like `np.linalg.norm` but auto-support jnp, tnp, np.)r&   r0   )r	   r+   tfnormr'   linalg)r   r&   r0   r    r   r   r   r2   }   s    

r2   c                 C  s   t | j| S )z3Like `np.linalg.inv` but auto-support jnp, tnp, np.)
_tf_or_xnpr3   invr/   r   r   r   r5      s    r5   zFloatArray['*d m m']c                 C  s   t | j| S )z3Like `np.linalg.det` but auto-support jnp, tnp, np.)r4   r3   detr/   r   r   r   r6      s    r6   r/   c                 C  s(   t | }t jr |t ju r t jS |S d S r   )r	   r'   Zhas_tfr,   r1   r   r   r   r   r4      s    
r4   )NF)!__doc__
__future__r   	functoolstypingr   r   Z	etils.enpr   Zetils.enp.typingr   r   numpyr   TYPE_CHECKINGr
   Ztorch_Z_NpDTyper	   	lru_cacher   r   r   r   r!   r$   r(   r*   r.   r2   r5   r6   r4   r   r   r   r   <module>   s8   			  