o
    e«Ïi:  ã                   @   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   úq/var/www/addictedbytheproject.nl/epg/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 broadcasting.
    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ƒr3g }|jD ]}|| v r'| | | ¡ q| |¡ qtt|ƒƒS t |tƒrIg }|D ]
}| t| |ƒ¡ q<|S t |tƒrag }|D ]
}| t| |ƒ¡ qRt|ƒS |S )z2
    Apply the most general unifier to a type
    )Ú
isinstancer   r   Ú__args__r   r   ÚlistÚsubstitute_solution_one_type)ÚmappingÚtÚnew_typeÚtypr   r   r   r   ;   s,   




r   c                 C   sp   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)Ú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   
	#