a
    ==ic%                     @   s   d dl Z d dlmZmZmZmZ d dlmZ d dlZd dl	m
Z
mZ d dlZd dlmZ d dlmZ ejejejejejejejejejejejejiZG dd dejjZdS )    N)CallableSequenceAnyDict)chain)GraphNode)
TensorMetac                   @   st   e Zd ZdZdd Zedd Zdd Zee	dd	d
dZ
edddZedddZdeeee edddZdS )PrimContexta  
    The prototype prim tracing context.

    Example usage:

    import torch._prims.utils as utils
    from torch._prims.context import PrimContext
    from torch._prims.executor import execute
    from torch.overrides import push_torch_function_mode

    a = torch.randn((2, 2))
    b = torch.randn((2, 2))

    with push_torch_function_mode(PrimContext):
      meta_a = ctx.placeholder(utils.TensorMeta(a))
      meta_b = ctx.placeholder(utils.TensorMeta(b))
      result = torch.add(meta_a, meta_b)
      ctx.output(result)

    exc_result = execute(ctx, a, b)

    Currently this only acquires a trace of prims, and
    it does not account for control flow. As such,
    execute must be called with tensors that have the
    same metadata (dtype, device, shape...) as
    the tensors used to trace the operations.

    The tracing context's FX graph can be acquired
    using its graph attribute.
    c                 C   s6   t  | _d| _d| _d| _ttj| _ttj	| _
d S )Nr   )r   graph_tensor_name_counterZ_dim_name_counterZ_shape_name_countertuplestringascii_lowercase
_lowercaseascii_uppercaseZ
_uppercase)self r   e/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/torch/_prims/context.py__init__;   s    zPrimContext.__init__c                 C   sB   d}| t |kr2|| t |  | }| t | } q||  | }|S )N )len)idxcharsnamer   r   r   _create_nameE   s    zPrimContext._create_namec                 C   s    | j }| j d | _ | || jS )N   )r   r   r   )r   r   r   r   r   _tensor_nameO   s    zPrimContext._tensor_nameN)tmnodereturnc                 C   s   |j d usJ d |j j|< d S N)r   Zusersr   r   r   r   r   r   	_add_userU   s    zPrimContext._add_user)ac                 C   s@   |   }| j|}t|tr<|jd ur0td||_||_|S )Nz0Attempting to reuse a TensorMeta in a new trace!)r   r   placeholder
isinstancer	   r   
ValueErrortname)r   r$   r   r   r   r   r   r%   Y   s    

zPrimContext.placeholder)r   c                 C   s*   t |tsJ | j|}| || d S r!   )r&   r	   r   outputr#   r"   r   r   r   r)   e   s    zPrimContext.outputr   )functypesargskwargsc           
      C   s   |du ri }t |dr|j|i |}t|ts4J |  }| jjd||||d}||_||_dd t	||
 D D ]}| || qv|S |tv rt| }	tjj| | jd |	|i |W  d   S 1 s0    Y  ||i |S )z
        Determines which function to call. The order of which
        function is called is determined by:

        - func's "meta" attribute, if it exists
        - if func is a torch operation, its corresponding reference
        - func
        Nmetacall_function)r   r,   r-   c                 s   s   | ]}t |tr|V  qd S r!   )r&   r	   ).0xr   r   r   	<genexpr>   s   z1PrimContext.__torch_function__.<locals>.<genexpr>)replace)hasattrr.   r&   r	   r   r   Zcreate_noder(   r   r   valuesr#   _torch_to_reference_maptorch	overridesZenable_torch_function_modeinner)
r   r*   r+   r,   r-   r)   Zoutput_namer   r1   fnr   r   r   __torch_function__l   s*    


,zPrimContext.__torch_function__)r   N)__name__
__module____qualname____doc__r   staticmethodr   r   r	   r   r#   r   r%   r)   r   r   r   r;   r   r   r   r   r
      s    

	  r
   )r   typingr   r   r   r   	itertoolsr   r7   Ztorch.fx.graphr   r   Ztorch.overridesZtorch._prims.utilsr	   Ztorch._refsZ_refsrefsaddmulgegtleltr6   r8   ZTorchFunctionModer
   r   r   r   r   <module>   s   