o
    iI                     @   sL   d dl mZ d dlZd dlmZ d dlm  mZ d dlZG dd dZ	dS )    use_name_ofN)contextmanagerc                   @   sP   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	e
dd ZdS )ArrayApiNamespaceStackc                 C   s2   t  | _tj| j| jd| _tj	j
d| _d S )N)opopsarray_api_compat)	threadinglocal_thread_localtypesSimpleNamespace_wrap_namedtensor_op_wrap_namedtensor_opsnamedtensortracer	signaturepythonimport_r   self r   ]/home/ubuntu/.local/lib/python3.10/site-packages/einx/_src/adapter/arrayapi/namespacestack.py__init__	   s   
zArrayApiNamespaceStack.__init__c                 C   s    |   }t|dksJ |d S )Nr   )
_get_stacklen)r   stackr   r   r   get_xp   s   zArrayApiNamespaceStack.get_xpc                    s   t   fdd}|S )Nc              
      sT    dd |D  z |d| i|W dd |D  S dd |D  w )Nc                 S      g | ]}|j qS r   value.0tr   r   r   
<listcomp>       zNArrayApiNamespaceStack._wrap_namedtensor_op.<locals>.inner.<locals>.<listcomp>outc                 S   r   r   r    r"   r   r   r   r%      r&   _enter_exit)r'   tensorskwargsr   r   r   r   inner   s   ,z:ArrayApiNamespaceStack._wrap_namedtensor_op.<locals>.innerr   )r   r   r.   r   r-   r   r      s   z+ArrayApiNamespaceStack._wrap_namedtensor_opc                    s    fdd|  D S )Nc                    s   i | ]
\}}|  |qS r   )r   )r#   namer   r   r   r   
<dictcomp>   s    z@ArrayApiNamespaceStack._wrap_namedtensor_ops.<locals>.<dictcomp>)items)r   r   r   r   r   r      s   z,ArrayApiNamespaceStack._wrap_namedtensor_opsc                 C   s   t | jds
g | j_| jjS )Nr   )hasattrr   r   r   r   r   r   r   !   s   z!ArrayApiNamespaceStack._get_stackc                 C   s"   | j j| }|  }|| d S N)r   array_namespacer   append)r   r+   xpr   r   r   r   r)   &   s   zArrayApiNamespaceStack._enterc                 C   s$   |   }t|dksJ |  d S )Nr   )r   r   pop)r   r+   r   r   r   r   r*   +   s   zArrayApiNamespaceStack._exitc              	   c   s0    |  | zd V  W | | d S | | w r3   r(   )r   r+   r   r   r   __call__0   s
   
zArrayApiNamespaceStack.__call__N)__name__
__module____qualname__r   r   r   r   r   r)   r*   r   r8   r   r   r   r   r      s    r   )
einx._src.util.functoolsr   r	   
contextlibr   einx._src.tracer_srcr   r   r   r   r   r   r   <module>   s    