U
    yhL                     @   sd   d dl mZ d dlmZ d dlmZ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)
TensorType)Varunifyc                 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ZtracedrZmgu r   Y/var/www/html/venv/lib/python3.8/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
   Z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   0   s    r   c                 C   s   t |tr$||  kr| | S |S nt |trpg }|jD ]*}||  krX|| |  q8|| q8tt|S t |trg }|D ]}|t| | q|S t |trg }|D ]}|t| | qt|S |S dS )z2
    Apply the most general unifier to a type
    N)	
isinstancer   keysr   __args__r   r   listsubstitute_solution_one_type)mappingtnew_typetypr   r   r   r   9   s,    




r   c                 C   sr   d}|rTd}|D ]@}|| }|| |  kr@|| }|| ||< ||| krd}qq| jD ]}t||j|_qZd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kZold_mapping_valZ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_equalityp   s    r*   N)Z/torch.fx.experimental.graph_gradual_typecheckerr   Ztorch.fx.tensor_typer   Z!torch.fx.experimental.unificationr   r   r   r   r   r   r   r	   r*   r   r   r   r   <module>   s   		#