a
    Sic                     @   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 r4d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   U/var/www/html/django/DPS/env/lib/python3.9/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.
    )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   )equationoperandsr   _r   r   r   r   %   s    
r   c                 C   sh  t  \}}tr|j| ||dS |  }| }t|trNt|| |t|f}t|d trn|d f|d f}t|d tr|d |d ff}dg| }dg| }g }	tt}
t	| D ] \}}t
|
}|||< |||< qt|D ]*}|| du rt
|
}|||< |	| qt|D ].}|| du rt
|
}|||< |	| qdjtdj|||	f }t|| |S )z6Simple 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   ZxndZyndZx_ixZy_ixZout_ixZavailable_ixZax1Zax2repeatileave
einsum_strr   r   r   r   0   s>    




r   c                 C   s*   t  \}}t| tjr&|| |S | S )N)r   r!   npndarray
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r0| S |  S )Nc                 S   s   g | ]}t |qS r   r   .0r,   r   r   r   
<listcomp>p       z<build_expression.<locals>.torch_contract.<locals>.<listcomp>r   )backendr   )	_contractr   typenumpyr   )arraysZtorch_arraysZ	torch_outexprr   r   torch_contracto   s
    z(build_expression.<locals>.torch_contractr   )r   rB   rC   r   rA   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   s   g | ]}t |qS r   r7   r8   r   r   r   r:      r;   z&evaluate_constants.<locals>.<listcomp>r   T)r<   r
   r   )const_arraysrB   r   r   r   r
   {   s    r
   )r   )__doc__r?   r2   parserr   sharingr   __all__r   r   r%   r   r   r   r   r   r	   r
   r   r   r   r   <module>   s   
2
	