o
    Ni                     @   s~   d Z ddlmZ ddlmZ ddlmZ g dZdada	dZ
dd	 Zd
d Zdd ZdddZedd Zdd Zdd ZdS )zLRequired functions for optimized contractions of numpy arrays using pytorch.    )has_array_interface)convert_to_valid_einsum_chars)to_backend_cache_wrap)	transposeeinsum	tensordotto_torchbuild_expressionevaluate_constantsN4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZc                  C   s8   t d u rdd l} | j rdnd}| |fa t| dat S )Nr   cudacpur   )_TORCH_DEVICEtorchr   is_availablehasattr_TORCH_HAS_TENSORDOT)r   device r   M/home/ubuntu/.local/lib/python3.10/site-packages/opt_einsum/backends/torch.py_get_torch_and_device   s   
r   c                 C   s
   | j | S )z5Normal torch transpose is only valid for 2D matrices.)permute)aaxesr   r   r   r   $   s   
r   c                 O   s   t | } t \}}|| |S )z4Variadic version of torch.einsum to match numpy api.)r   r   r   )equationoperandskwargsr   _r   r   r   r   )   s   
r      c                 C   sd  t  \}}tr|j| ||dS |  }| }t|tr't|| |t|f}t|d tr7|d f|d f}t|d trG|d |d ff}dg| }dg| }g }	tt}
t	| D ]\}}t
|
}|||< |||< q[t|D ]}|| du rt
|
}|||< |	| qpt|D ]}|| du rt
|
}|||< |	| qdjtdj|||	f }t|| |S )z1Simple translation of tensordot syntax to einsum.)dimsr      Nz	{},{}->{} )r   r   r   
ndimension
isinstanceintrangeiter_torch_symbols_basezipnextappendformatmapjoinr   )xyr   r   r   xndyndx_ixy_ixout_ixavailable_ixax1ax2repeatileave
einsum_strr   r   r   r   3   sB   






r   c                 C   s&   t  \}}t| r|| |S | S )N)r   r   
from_numpyto)arrayr   r   r   r   r   r   d   s   
r   c                    s    fdd}|S )z8Build a torch function based on ``arrays`` and ``expr``.c                     s<   dd | D } j |dd}|jjdkr| S |  S )Nc                 S      g | ]}t |qS r   r   .0r.   r   r   r   
<listcomp>r       z<build_expression.<locals>.torch_contract.<locals>.<listcomp>r   )backendr   )	_contractr   typenumpyr   )arraystorch_arrays	torch_outexprr   r   torch_contractq   s
   z(build_expression.<locals>.torch_contractr   )r   rM   rN   r   rL   r   r	   n   s   	r	   c                 C   s   dd | D } || dddS )z]Convert constant arguments to torch, and perform any possible constant
    contractions.
    c                 S   r?   r   r@   rA   r   r   r   rC      rD   z&evaluate_constants.<locals>.<listcomp>r   T)rE   r
   r   )const_arraysrM   r   r   r   r
   }   s   r
   )r   )__doc__opt_einsum.helpersr   opt_einsum.parserr   opt_einsum.sharingr   __all__r   r   r'   r   r   r   r   r   r	   r
   r   r   r   r   <module>   s     	

1
	