
    ^jj                        d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ g dZdej                  j                  d	dfd
Zdej                  j                  d	dfdZdee   d	eeedf   eedf   f   fdZdee   d	efdZdeeef   ded	efdZdej                  j2                  deeef   d	dfdZdej                  j2                  dej                  j2                  d	efdZy)    )AnyN)Refine)Equality)unifyVar)
TensorType)check_for_type_equality
convert_eqinfer_symbolic_types infer_symbolic_types_single_passsubstitute_all_typessubstitute_solution_one_typeunify_eqtracedreturnc                     t        |       }|j                          t        |j                        }t	        | j
                  |       y)z-
    Calls our symbolic inferencer once.
    N)r   refiner   constraintsr   graphr   rmgus      r/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/experimental/unify_refinements.pyr   r      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 broadcasting.
    N)r   r   r   r   r   r   symbolic_relationsr   s      r   r   r       sl     	vAHHJ
1==
!Cs+vAHHJ
1==
!Cs+r   
list_of_eq.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)r   r    r!   eqs       r   r
   r
   4   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   A   s    
 *%HCc?r   mappingtc                    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   dimsr   r"   listr   )r%   r&   new_typetyps       r   r   r   J   s     !S<1:H	Az	"66 	%Cg~-$		%
 %/**	At	 	HCOO8#FG	H	Au	 	HCOO8#FG	HX r   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   m   s     D
 	A%ajOqzW$!!*$W-
'!*,	  [[ ?-gqvv>?r   g1g2c                     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/   )r5   r6   r4   ms       r   r	   r	      s>     BHHbhh' 166QVV r   )typingr   torchtorch.fx/torch.fx.experimental.graph_gradual_typecheckerr   &torch.fx.experimental.refinement_typesr   !torch.fx.experimental.unificationr   r   torch.fx.tensor_typer   __all__fxGraphModuler   r   r*   r"   r
   r   dictobjectr   Graphr   boolr	    r   r   <module>rI      s      B ; 8 +,UXX-A-A ,d ,!5!5 $ (
"4> 
"eE#s(OU3PS8_4T.U 
"h C  $vv~*>  6  c  F? ?ffn9M ?RV ?*	 	EHHNN 	t 	r   