o
    i                     @   s@   d dl m  mZ d dlZd dlZd dlmZ G dd dZdS )    Nuse_name_ofc                   @   sD   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	dS )TorchDeviceStackc                 C   s"   t  | _tj| j| jd| _d S )N)opops)	threadinglocal_thread_localtypesSimpleNamespace_wrap_namedtensor_op_wrap_namedtensor_opsnamedtensorself r   W/home/ubuntu/.local/lib/python3.10/site-packages/einx/_src/adapter/torch/devicestack.py__init__   s   
zTorchDeviceStack.__init__c                 C   s    |   }t|dksJ |d S )Nr   )
_get_stacklen)r   stackr   r   r   
get_device   s   zTorchDeviceStack.get_devicec                    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>       zHTorchDeviceStack._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   ,z4TorchDeviceStack._wrap_namedtensor_op.<locals>.innerr   )r   r   r'   r   r&   r   r      s   z%TorchDeviceStack._wrap_namedtensor_opc                    s    fdd|  D S )Nc                    s   i | ]
\}}|  |qS r   )r   )r   namer   r   r   r   
<dictcomp>   s    z:TorchDeviceStack._wrap_namedtensor_ops.<locals>.<dictcomp>)items)r   r   r   r   r   r      s   z&TorchDeviceStack._wrap_namedtensor_opsc                 C   s   t | jds
g | j_| jjS )Nr   )hasattrr	   r   r   r   r   r   r      s   zTorchDeviceStack._get_stackc                 C   sH   d }|D ]}t |tjjjrtjj|d} nq|  }|| d S )Ndevice)	
isinstancetracer	signature	classicalTensorpythongetattrr   append)r   r$   r,   tensorr   r   r   r   r"   $   s   zTorchDeviceStack._enterc                 C   s$   |   }t|dksJ |  d S )Nr   )r   r   pop)r   r$   r   r   r   r   r#   /   s   zTorchDeviceStack._exitN)
__name__
__module____qualname__r   r   r   r   r   r"   r#   r   r   r   r   r      s    r   )einx._src.tracer_srcr.   r   r
   einx._src.util.functoolsr   r   r   r   r   r   <module>   s
    