o
    7?e                     @   sz   d Z ddl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 )zN
Required functions for optimized contractions of numpy arrays using pytorch.
    N   )convert_to_valid_einsum_chars)to_backend_cache_wrap)	transposeeinsum	tensordotto_torchbuild_expressionevaluate_constants4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZc                  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   Zis_availablehasattr_TORCH_HAS_TENSORDOT)r   device r   Z/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/opt_einsum/backends/torch.py_get_torch_and_device   s   
r   c                 C   s
   | j | S )z:Normal torch transpose is only valid for 2D matrices.
    )Zpermute)aaxesr   r   r   r      s   
r   c                 G   s   t | } t \}}|| |S )z9Variadic version of torch.einsum to match numpy api.
    )r   r   r   )ZequationZoperandsr   _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 )z6Simple translation of tensordot syntax to einsum.
    )dimsr      Nz	{},{}->{} )r   r   r   Z
ndimension
isinstanceintrangeiter_torch_symbols_basezipnextappendformatmapjoinr   )xyr   r   r   ZxndZyndZx_ixZy_ixZout_ixZavailable_ixZax1Zax2repeatiZleaveZ
einsum_strr   r   r   r   0   sB   






r   c                 C   s*   t  \}}t| tjr|| |S | S )N)r   r   npZndarrayZ
from_numpyto)arrayr   r   r   r   r   r   b   s   
r   c                    s    fdd}|S )z=Build 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>p       z<build_expression.<locals>.torch_contract.<locals>.<listcomp>r   )backendr   )Z	_contractr   typenumpyr   )ZarraysZtorch_arraysZ	torch_outexprr   r   torch_contracto   s
   z(build_expression.<locals>.torch_contractr   )r   r8   r9   r   r7   r   r	   l   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/   r0   r   r   r   r2      r3   z&evaluate_constants.<locals>.<listcomp>r   T)r4   r
   r   )Zconst_arraysr8   r   r   r   r
   {   s   r
   )r   )__doc__r6   r+   parserr   Zsharingr   __all__r   r   r    r   r   r   r   r   r	   r
   r   r   r   r   <module>   s     
2
	