o
    ii#                     @   s   d dl Z dd ZeG dd deZeG dd deZeG dd	 d	eZeG d
d deZeG dd deZeG dd deZeG dd deZ	eG dd deZ
eG dd deZdS )    Nc                 C   s
   d| _ | S )Nzeinx.errors)
__module__)obj r   M/home/ubuntu/.local/lib/python3.10/site-packages/einx/_src/frontend/errors.pyexport   s   r   c                   @   s   e Zd ZdS )	EinxErrorN)__name__r   __qualname__r   r   r   r   r   	   s    r   c                   @      e Zd ZdZdddZdS )SyntaxErrora5  
    This error occurs if einx operations encounter invalid syntax in an expression.

    Example:
        >>> x = np.random.rand(3, 4)
        >>> einx.id("a (b -> b a", x)
        einx.errors.SyntaxError: Found an opening parenthesis that is not closed:
        Expression: "a (b"
                    ^
    Nc                    s   t  ts	tdddlm} | }t |tr|g| _n|d u r%g | _nt|| _t fdd| jD s8J d|v rF|	d|
| j}t| | d S )Nzexpression must be a stringr   )ExpressionIndicatorc                 3   s$    | ]}|d ko|t  k V  qdS r   N)len.0p
expressionr   r   	<genexpr>)   s   " z'SyntaxError.__init__.<locals>.<genexpr>%EXPR%)
isinstancestr
ValueErroreinx._src.namedtensorr   intposlistallreplacecreater   __init__)selfr   messager   r   	indicatorr   r   r   r       s   



zSyntaxError.__init__Nr   r   r	   __doc__r    r   r   r   r   r      s    r   c                   @   r
   )	RankErrora  
    This error occurs if the ranks (i.e. number of dimensions) of all expressions cannot
    be determined under the given input tensor shapes and additional constraints.

    Example:
        >>> x = np.random.rand(3, 4)
        >>> einx.id("a b c -> a c b", x)
        einx.errors.RankError: The number of tensor dimensions and axes in the expression does not match.
        The operation was called with the following arguments:
        - Positional argument #1: Tensor with shape (3, 4)
    Nc                    (  |d u rg }t |tr|g| _n|d u rg | _nt|| _t fdd| jD s+J g }|D ]/}t |tr<|| q/t|dksDJ |d d ur^|d d ur^||d  d|d   q/d|v rn|d j	
| j}t|dkr|d7 }|D ]	}|d	| 7 }qz|d
   7 }t| | d S )Nc                 3   &    | ]}|d ko|t  jk V  qdS r   r   r   r   
invocationr   r   r   I      $ z%RankError.__init__.<locals>.<genexpr>   r       = r   z>
The following equation(s) were determined for the expression:
    
r   r   r   r   r   r   appendr   r   r#   r   to_call_signature_stringr   r    r!   r,   r"   r   constraintsconstraints2
constraintr   r+   r   r    ?   0   



zRankError.__init__NNr%   r   r   r   r   r'   1   s    r'   c                   @   r
   )AxisSizeErrora  
    This error occurs if the size of all axes in an einx expression cannot be determined
    under the given input tensor shapes and additional constraints.

    Example:
        >>> x = np.random.rand(3, 4)
        >>> y = np.random.rand(4, 4)
        >>> einx.add("a b, a c -> a b c", x, y)
        einx.errors.AxisSizeError: Failed to determine the size of all axes in the expression under the given constraints.
        Expression: "a b, a c -> a b c"

        The operation was called with the following arguments:
        - Positional argument #1: Tensor with shape (3, 4)
        - Positional argument #2: Tensor with shape (4, 4)
    Nc                    r(   )Nc                 3   r)   r   r*   r   r+   r   r   r   {   r-   z)AxisSizeError.__init__.<locals>.<genexpr>r.   r   r/   r0   r   zT
The expression, tensor shapes and contraints resulted in the following equation(s):r1   r2   r3   r6   r   r+   r   r    q   r:   zAxisSizeError.__init__r;   r%   r   r   r   r   r<   _   s    r<   c                   @   r
   )SemanticErroraS  
    This error occurs if the expression requirements of a particular einx operation are not met.

    Example:
        >>> x = np.random.rand(3, 4)
        >>> y = np.random.rand(4, 5)
        >>> einx.dot("a [b], [c] d -> a d", x, y)
        einx.errors.SemanticError: All contracted axes must appear in exactly two input expressions.
        Expression: "a [b], [c] d -> a d"
                        ^    ^
        The operation was called with the following arguments:
        - Positional argument #1: Tensor with shape (3, 4)
        - Positional argument #2: Tensor with shape (4, 5)
    Nc                    s   t |tsJ  d urHt |tr|g| _n|d u rg | _nt|| _t fdd| jD s0J d|v r?|d j| j}|d 	  7 }nd | _t
| | d S )Nc                 3   r)   r   r*   r   r+   r   r   r      r-   z)SemanticError.__init__.<locals>.<genexpr>r   r2   )r   r   r   r   r   r   r   r#   r   r5   r   r    )r!   r"   r,   r   r   r+   r   r       s   


zSemanticError.__init__r;   r%   r   r   r   r   r=      s    r=   c                   @   r
   )OperationNotSupportedErrora2  
    This error occurs if an einx operation is not supported by the selected backend.

    Example:
        >>> x = np.random.rand(3, 4)
        >>> einx.min("a [b]", x, backend="numpy.einsum")
        einx.errors.OperationNotSupportedError: min operation is not supported by the numpy.einsum backend.
    Nc                 C   s   |d u rd}t | | d S )Nz0This operation is not supported by this backend.)r   r    )r!   r"   r   r   r   r       s   z#OperationNotSupportedError.__init__r$   r%   r   r   r   r   r>      s    	r>   c                   @      e Zd ZdZdS )ImportBackendErrora  
    This error occurs if a requested backend failed to be imported or initialized.

    Example:
        >>> sys.modules["jax"] = types.SimpleNamespace()  # Create some invalid jax module
        >>> x = np.random.rand(3, 4)
        >>> einx.min("a [b]", x, backend="jax")
        einx.errors.ImportBackendError: Failed to import backend "jax" for module "jax" due to the following error: ...
    Nr   r   r	   r&   r   r   r   r   r@          
r@   c                   @   r?   )BackendResolutionErroraU  
    This error occurs if the backend with which to execute an einx operation could not be determined.

    Example:
        >>> x = jnp.zeros((10, 11))
        >>> y = torch.zeros(10, 11)
        >>> einx.add("a b, a b", x, y)
        einx.errors.BackendResolutionError: Failed to determine which backend to use for this operation: ...
    NrA   r   r   r   r   rC      rB   rC   c                   @   s   e Zd ZdZedd ZdS )CallOperationErrora  
    This error occurs if the Python function that einx compiles for an einx operation fails to execute.

    Example:
        >>> op = lambda x: None  # Create some invalid operation
        >>> einop = einx.jax.adapt_with_vmap(op)
        >>> x = np.random.rand(3, 4)
        >>> einop("a [b] -> a", x)
        einx.errors.CallOperationError: The function that was compiled for this operation failed to execute.
        ...
        The error was: AssertionError: Expected 1st return value of the adapted function to be a tensor
    c                 C   s   d}|d urE|d7 }t | dD ]\}}||dd| d7 }q| jd urEt| j}|rEdd |D }t|dkrE|d	|d
  d7 }|d7 }tt| drX|t| j 7 }tt	| d
krg|d|  7 }t
|S )NzDThe function that was compiled for this operation failed to execute.z# The following code was generated:
r/   4z: r2   c                 S   s   g | ]
}|j d kr|jqS )z<string>)filenamelineno)r   framer   r   r   
<listcomp>   s    z-CallOperationError.create.<locals>.<listcomp>zThe error occurred at line r   z.
z
The error was: r   )	enumerate
splitlines__traceback__	traceback
extract_tbr   hasattrtyper   r   rD   )ecoder"   ilinetblinenosr   r   r   r      s"   
zCallOperationError.createN)r   r   r	   r&   staticmethodr   r   r   r   r   rD      s    rD   )rM   r   	Exceptionr   r   r'   r<   r=   r>   r@   rC   rD   r   r   r   r   <module>   s(    "-1%