
    sgL                     V    d dl mZ d dlmZ d dlmZmZ d Zd Zd Z	d Z
d Zd	 Zd
 Zy)    )Refine)
TensorType)Varunifyc                     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      Z/var/www/html/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	   0   s    
 *%HCc?r   c                     t        |t              r|| j                         v r| |   S |S t        |t              r_g }|j                  D ]:  }|| j                         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   keysr   __args__r   r   listsubstitute_solution_one_type)mappingtnew_typetyps       r   r%   r%   9   s    !S1:H	Az	":: 	%Cglln$-$		%
 %/**	At	 	HCOO8#FG	H	Au	 	HCOO8#FG	HX r   c                     d}|r>d}|D ]4  }||   }||   |j                         v r||   }||   ||<   |||   k7  s3d}6 |r>| 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)r"   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g2r1   ms       r   check_for_type_equalityr7   p   s>     BHHbhh' 166QVV r   N)/torch.fx.experimental.graph_gradual_typecheckerr   torch.fx.tensor_typer   !torch.fx.experimental.unificationr   r   r   r   r   r	   r%   r   r7    r   r   <module>r<      s2    B + 8,&
" F?(	r   