
    i:                     V    d dl mZ d dlmZmZ d dlmZ d Zd Zd Z	d Z
d Zd	 Zd
 Zy)    )Refine)unifyVar)
TensorTypec                     t        |       }|j                          t        |j                        }t	        | j
                  |       y)z-
    Calls our symbolic inferencer once.
    N)r   refineunify_eqconstraintssubstitute_all_typesgraphtracedrmgus      a/var/www/html/engine/venv/lib/python3.12/site-packages/torch/fx/experimental/unify_refinements.py infer_symbolic_types_single_passr      s2     	vAHHJ
1==
!Cs+    c                 <   t        |       }|j                          t        |j                        }t	        | j
                  |       t        |       }|j                          t        |j                        }t	        | j
                  |       |j                          y)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   s      r   infer_symbolic_typesr      sl     	vAHHJ
1==
!Cs+vAHHJ
1==
!Cs+r   c                     g }g }| D ]8  }|j                  |j                         |j                  |j                         : 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   eqs       r   
convert_eqr   %   sR    
 C
C 

266

266 :uSz!!r   c                 6    t        |       \  }}t        ||      S )z@
    Apply unification to a set of
    equality constraints
    )r   r   )r   r   r   s      r   r	   r	   2   s    
 *%HCc?r   c                    t        |t              r|| v r| |   S |S t        |t              rQg }|j                  D ],  }|| v r|j	                  | |          |j	                  |       . t        t        |            S t        |t              r&g }|D ]  }|j	                  t        | |              |S t        |t
              r/g }|D ]  }|j	                  t        | |              t        |      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typs       r   r$   r$   ;   s     !S<1:H	Az	":: 	%Cg~-$		%
 %/**	At	 	HCOO8#FG	H	Au	 	HCOO8#FG	HX r   c                     d}|r0d}|D ]&  }||   }||   |v r||   }||   ||<   |||   k7  s%d}( |r0| j                   D ]  }t        ||j                        |_         y)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ns          r   r   r   ^   s     D
 	A%ajOqzW$!!*$W-
'!*,	  [[ ?-gqvv>?r   c                     t        | j                  |j                        D ]   \  }}|j                  |j                  k7  s  y y)zv
    A check equality to be used in fixed points.
    We do not use graph equality but instead type
    equality.
    FT)zipr*   r+   )g1g2r0   ms       r   check_for_type_equalityr6   s   s>     BHHbhh' 166QVV r   N)/torch.fx.experimental.graph_gradual_typecheckerr   !torch.fx.experimental.unificationr   r   torch.fx.tensor_typer   r   r   r   r	   r$   r   r6    r   r   <module>r;      s2    B 8 +,(
" F?*	r   