o
    iO                     @   sd   d dl mZ d dlmZmZ d dlmZ dd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd ZdS )    )Refine)unifyVar)
TensorTypec                 C   s*   t | }|  t|j}t| j| dS )z-
    Calls our symbolic inferencer once.
    N)r   refineunify_eqconstraintssubstitute_all_typesgraphtracedrmgu r   d/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/torch/fx/experimental/unify_refinements.py infer_symbolic_types_single_pass   s   
r   c                 C   sX   t | }|  t|j}t| j| t | }|  t|j}t| j| |  dS )z
    Calls our symbolic inferencer twice.
    This is useful when one pass is not enough
    to infer all the information such as the case
    for braodcasting.
    N)r   r   r   r   r	   r
   symbolic_relationsr   r   r   r   infer_symbolic_types   s   

r   c                 C   s:   g }g }| D ]}| |j | |j qt|t|fS )za
    Convert equality constraints in the right format
    to be used by unification library.
    )appendlhsrhstuple)
list_of_eqr   r   eqr   r   r   
convert_eq%   s   r   c                 C   s   t | \}}t||S )z@
    Apply unification to a set of
    equality constraints
    )r   r   )r   r   r   r   r   r   r   2   s   
r   c                 C   s   t |tr||  v r| | S |S t |tr7g }|jD ]}||  v r+|| |  q|| qtt|S t |trMg }|D ]
}|t| | q@|S t |treg }|D ]
}|t| | qVt|S |S )z2
    Apply the most general unifier to a type
    )	
isinstancer   keysr   __args__r   r   listsubstitute_solution_one_type)mappingtnew_typetypr   r   r   r   ;   s,   




r   c                 C   st   d}|r+d}|D ] }|| }|| |  v r || }|| ||< ||| kr(d}q|s| jD ]	}t||j|_q.dS )z
    Apply the most general unifier to all types in a graph
    till reaching a fixed point. If the input and output graph
    are the same, we converge.
    TFN)r   nodesr   type)r
   r    flagkold_mapping_valnew_keynr   r   r   r	   ^   s   

r	   c                 C   s.   t | j|jD ]\}}|j|jkr dS qdS )zv
    A check equality to be used in fixed points.
    We do not use graph equality but instead type
    equality.
    FT)zipr$   r%   )g1g2r*   mr   r   r   check_for_type_equalitys   s
   r/   N)/torch.fx.experimental.graph_gradual_typecheckerr   !torch.fx.experimental.unificationr   r   torch.fx.tensor_typer   r   r   r   r   r   r	   r/   r   r   r   r   <module>   s   
	#