
    Opf                     z    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 Zd	 Zd
 ZddZed             Zd Z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                      t           5d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   devices     Y/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/opt_einsum/backends/torch.py_get_torch_and_devicer      sN      :2244?%v&uk::    c                      | j         | S )z:Normal torch transpose is only valid for 2D matrices.
    )permute)aaxess     r   r   r      s     19dr   c                 n    t          |           } t                      \  }}|                    | |          S )z9Variadic version of torch.einsum to match numpy api.
    )r   r   r   )equationoperandsr   _s       r   r   r   %   s4    
 -X66H$&&HE1<<(+++r   c                    t                      \  }}t          r|                    | ||          S |                                 }|                                }t	          |t
                    r"t          ||z
  |          t          |          f}t	          |d         t
                    r|d         f|d         f}t	          |d         t
                    r|d         |d         ff}dg|z  }dg|z  }g }	t          t                    }
t          | D ]\  }}t          |
          }|||<   |||<   t          |          D ]3}||         )t          |
          }|||<   |	                    |           4t          |          D ]3}||         )t          |
          }|||<   |	                    |           4 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    xndyndx_ixy_ixout_ixavailable_ixax1ax2repeatileave
einsum_strs                    r   r   r   0   s     %&&HE1 0q!$///
,,..C
,,..C $ 3S4Z%%uT{{2 $q'3 $Q{DG#$q'3 $Awa# 6C<D6C<DF +,,LJ  Sl##S	S		 3ZZ ! !7?&&EDGMM%   3ZZ ! !7?&&EDGMM%    $#S4v2F%G%GHJ*a###r   c                     t                      \  }}t          | t          j                  r(|                    |                               |          S | S )N)r   r&   npndarray
from_numpyto)arrayr   r   s      r   r	   r	   b   sL    )++ME6%$$ 2&&))&111Lr   c                     fd}|S )z=Build a torch function based on ``arrays`` and ``expr``.
    c                      d | D             }                     |d          }|j        j        dk    r|                                S |                                                                S )Nc                 ,    g | ]}t          |          S  r	   .0r1   s     r   
<listcomp>z<build_expression.<locals>.torch_contract.<locals>.<listcomp>p   s    444444r   r   )backendr   )	_contractr   typenumpyr   )arraystorch_arrays	torch_outexprs      r   torch_contractz(build_expression.<locals>.torch_contracto   sf    44V444NN<NAA	 E))??$$$}}$$&&&r   rH   )r    rT   rU   s    ` r   r
   r
   l   s$    ' ' ' ' ' r   c                 ,    d | D             }  || dddS )z]Convert constant arguments to torch, and perform any possible constant
    contractions.
    c                 ,    g | ]}t          |          S rH   rI   rJ   s     r   rL   z&evaluate_constants.<locals>.<listcomp>   s    666AHQKK666r   r   T)rM   r   rH   )const_arraysrT   s     r   r   r   {   s.     76666L4w4HHHHr   )r   )__doc__rP   r@   parserr   sharingr   __all__r   r   r*   r   r   r   r   r	   r
   r   rH   r   r   <module>r]      s         2 2 2 2 2 2 + + + + + +
d
d
d L 
 
 
  , , ,/$ /$ /$ /$d     I I I I Ir   