
    9j7                        U d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlm	Z	 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 d d	lmZmZmZmZmZmZmZ d d
lmZ  ej>                  e       Z!d Z"d Z#dejH                  jJ                  ejL                  jN                  z  dee
   de(ejR                  e*d   e
ejV                  dz  f   fdZ,d Z-d Z.d Z/dej`                  jb                  fdZ2d Z3d>de4de4de4fdZ5de4de4fdZ6d Z7ejp                  d ejr                  dejt                  dejv                  dejx                  d ejz                  d!ej|                  d"ej~                  d#ej                  d$ej                  d%ej                  d&ej                  d'ej                  d(ej                  d)ej                  d*iZGeGj                         D  ci c]  \  } }|| 
 c}} ZId+ ZJe j                  e j                  ej                  ej                  ej                  ej                  ej                  eJeRej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  d,ZYeZe4ed-e
f   f   e[d.<   d/ Z\d0 Z]d1ej                  jR                  de(eZej                  j                  e_e4   f   eZe4e4f   f   fd2Z`d3eZe4e4f   d4ej                  j                  de4fd5Zbd4ej                  j                  fd6Zc G d7 d8      Zd G d9 d:ed      Zee	d;        Zf G d< d=      Zgyc c}} w )?    N)CallableSequence)contextmanager)Any)_C)'replace_quantized_ops_with_standard_ops)_tree_map_with_pathDim)ExportedProgram)ConstantArgumentCustomObjArgument	InputKind	InputSpec
OutputKind
OutputSpecTensorArgument)subgraph_rewriterc                 $   g }t        | j                         |      D ]p  \  }}dt        |j                               v r=t        j
                  j                  |      \  }}|j                  t        |             ^|j                  |d u       r |S )NPackedParams)	zipinputsstrtypetorchjit_flattenappendlen)method_graphargs_paramsparam_count_listinput_arg_params_in_vars_s          W/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_export/converter.py_get_param_count_listr'   "   s    "<#6#6#8+F =S//++K8JGQ##CL1##Kt$;<=     c                    t         j                  j                  |       j                         }t        j                         }t        j
                  d       t         j                  j                  | |ddd      \  }}}t        j
                  |       |t         j                  j                  |       j                         k7  rt        d      ||fS )NFT)strict_force_outplace_return_inputs_stateszXstate_dict changed after running the tracer; something weird is happening in your model!)r   r   _unique_state_dictkeysis_autocast_cache_enabledset_autocast_cache_enabled_get_trace_graphRuntimeError)modelargsorig_state_dict_keysprev_autocast_cache_enabledtrace_graph	torch_out_inputs_statess          r&   _trace_and_get_graph_from_modelr:   .   s     !9977>CCE
 #("A"A"C	$$U+-2YY-G-G" .H .*KN 
$$%@Auyy;;EBGGII:
 	

 	!!r(   r3   r4   returnz	_C.IValuec                 
   t        | t        j                  j                  t        j                  j                  f      rt        t        j                  j                  t        |            d         }d }t        | t        j                  j                        r	 | j                  j                  }t        j                  |       t        j                  t        j                  t        j                  | j                         d      }t        j"                  |      \  }}|j%                  d      j                  }	t        |      t        |      z   }
t'        |	|
      }t        j                  j                  |
      \  }}t        j(                  |	t        |      |dd      }||||fS g }| j                  }t        j                  |       t'        ||      }t        j(                  |||dd      }|||d fS t+        | |      \  }}t        j,                  |       t        j                  j/                  |       }t1        |j3                               }t1        |j5                               }t7        |      t7        |      z
  }t1        |j9                               }t;        |      D ]"  \  }}||k\  s|j=                  |||z
            $ t        j                  |       |||d fS # t        $ r}t        d      |d }~ww xY w)Nr   z('forward' method must be a script methodT)preserveParametersforwardF)
isinstancer   r   ScriptFunctionScriptModuletupler   r>   graphAttributeErrorr2   r   $_jit_pass_onnx_function_substitution_freeze_moduletypingcast_c_jit_onnx_list_model_parameters_get_methodr'   "_propagate_and_assign_input_shapesr:   _jit_pass_onnx_lintr-   listvaluesr   r   r.   	enumeratesetDebugName)r3   r4   flattened_argsr8   rC   efreezed_modulemoduleparamsr   r    r!   r$   r%   
state_dictgraph_inputsuser_input_numparam_namesiinps                       r&   _create_jit_graphr]   J   s    %%))22EII4J4JKLuyy11%+>qAB	eUYY334V++ 33E:..BOOUXX64N  ??ONFF!--i8>>L+f5K4\;O++K8JGQ99eGn.>uE &)V33 
//60=55>#3UE
 fi--6udCE95!--e4J*##%&F'L&Z8Nz()KL) >3[^);<=> ++E2&)T))K " V"#MNTUUVs   K( (	L1K==Lc                     | |z   S N )abs     r&   list_addrc   |   s    q5Lr(   c                     | |gz   S r_   r`   )	containerelements     r&   list_appendrg      s    y  r(   c                 4    |d| }||d } | g |||i |S )z
    subgraph: GraphModule from sub-block.
    iter_idx: The index of interaction.
    len_loop_local_arguments: The number of loop local arguments in args.
    Nr`   )subgraphiter_idxlen_loop_local_argumentsr4   kwargsloop_local_argsglobal_argss          r&   execute_subgraph_from_prim_loopro      s?     445O
 /01KG[G(G_GGGr(   gmc                 >    d }d }t        j                  | ||       y )Nc                    t         j                  j                  j                  j	                  | |      }t         j                  j                  j                  |      }t         j                  j                  j                  j                  ||d      }t         j                  j                  j                  j                  |      }|S )Ntrunc)rounding_mode)
r   opsatensym_sizeintscalar_tensordivScalar_modeIntTensor)imdimscalesym_size_intry   div_scalar_mode
int_tensors          r&   patternz.inplace_optimize_sym_size_div.<locals>.pattern   s    yy~~..222s;		44\B))..,,885 9 
 YY^^''..?
r(   c                 t    t         j                  j                  j                  j	                  | |      }||z  S r_   )r   ru   rv   rw   rx   )r~   r   r   r   s       r&   replacementz2inplace_optimize_sym_size_div.<locals>.replacement   s-    yy~~..222s;u$$r(   )r   replace_pattern)rp   r   r   s      r&   inplace_optimize_sym_size_divr      s     % %%b';?r(   c                 ^    t        |       dk(  rt        d      | d   j                         ryy)Nr   zEmpty argument name for codegenFT)r   r2   isdigitnames    r&   is_valid_for_codegenr      s-    
4yA~<==Awr(   r   prefixc                 N    | j                  dd      } t        |       r| S | d|  S )N.r%   )replacer   )r   r   s     r&   normalize_namer      s0    <<S!DD!XQtfr(   c                 L    | j                  d      }ddj                  |      z   S )zprim::If -> convert_prim_If::convert_r%   )splitjoin)r   	name_lists     r&   ir_name_to_func_namer      s$    

4 I+++r(   c                 J    |r| j                  |      S | j                  |      S r_   )get_attrplaceholder)fx_graphr   is_top_level_graphs      r&   #get_node_as_placeholder_or_get_attrr      s'      &&%%r(                           	   
               c                 X    | j                   }|t        vrt        d|       t        |   S )z
    prim::dtype has the signature "Tensor a) -> int", where it gets the dtype of
    the tensor and returns the integer corresponding to this dtype based on the
    enum in ScalarType.h
    zUnsupported dtype )dtype_TORCH_DTYPE_TO_ENUMr2   )tensorr   s     r&   get_dtype_as_intr      s4     LLE((/w788&&r(   )z	prim::maxz	prim::minzprim::TupleIndexzaten::__is__zaten::__isnot__zaten::__not__zaten::__contains__zprim::dtypez	aten::lenzaten::numelz
aten::sizezaten::storage_offsetzaten::stride.kind_to_standard_operatorsc                     | j                         j                         | j                         j                         }}| j                  d      }|||fS Nr   )input	debugNameoutputs)nodeirv_parent_nameirv_name	attr_names       r&   &get_ir_value_parent_name_and_attr_namer      sC     $

 6 6 8$++-:Q:Q:SXOvI_i//r(   c                 ~    g }| |v r|j                  ||           ||    } | |v rdj                  t        |            S Nr   )r   r   reversed)irref_mapname_mapr   s       r&   construct_fqnr     sH    I
-"&R[ - 88HY'((r(   rC   c                 ^    i i i fdfd |         |        fS )a  
    Perform two passes to get a mapping of blocks to a set of FQNs of its lifted attributes.
    When a graph has control flow, the graph will be divided into multiple blocks. We want to convert
    each block to a graph which will be passed into torch.cond. A restriction for torch.cond is that model
    parameters/buffers are expected to be lifted as inputs to the subgraphs. Before converting the model,
    we will run this pass which will:
        1. Figure out which params/buffers are used within blocks through tracing the GetAttr calls.
        2. Process the graph bottom up to find the lifted attributes of each block by taking the union
        of the attributes used in the current block, and the lifted attributes of all its child blocks.

    Returns:
        A mapping of blocks to a set of FQNs of its lifted attributes, and a
        mapping of node names to the FQNs of its lifted attributes.
    c                     | j                         D ]K  }|j                         dk(  rt        |      \  }}}||<   ||<   |j                         D ]
  } |        M y)zI
        First DFS path to construct reference map and name map.
        prim::GetAttrN)nodeskindr   blocks)	entryr   r   r   r   block_dfs_get_attr_dependencynode_to_attr_namenode_to_parent_maps	         r&   r   z;get_block_to_lifted_attrs.<locals>._dfs_get_attr_dependency*  ss     KKM 
	0Dyy{o-
 ;4@	#/>"8,.7!(+ 0(/0
	0r(   c                    t               }| j                         D ]  }|j                         D ]  }|j                   |            } |j	                         dk(  sC|j                         j                         }|t        j                               vs}|j                  t        |              t        | t        j                  j                        s|| <   |S )z|
        Walk the graph in a bottom-up fashion to build the expected to be
        lifted arguments for each block.
        r   )setr   r   unionr   r   r   rO   addr   r?   r   r   Graph)	r   	argumentsr   r   r   _map_blocks_to_lifted_attrsblocks_to_lifted_attrsr   r   s	        r&   r   z>get_block_to_lifted_attrs.<locals>._map_blocks_to_lifted_attrs:  s    
 "e	KKM 	D P%OO,G,NO	P yy{o-;;=224 3'9'@'@'B#CCMM%h0BDUV	 %0,5"5)r(   r`   )rC   r   r   r   r   r   s    @@@@@r&   get_block_to_lifted_attrsr   	  sD    & >@ *, )+0 2 U#&!#444r(   name_to_attribute_fqnr   c                 d    dt         f fd}|j                         dk(  r(t        |j                               j	                         }nA|j                         dk(  r|j                         j	                         }nt        d| d      |j                  d      } ||      }|r	| d| }|S |}|S )Nr   c                 4    | v r|    S t        d|  d      Nz
Attribute 
 not found)
ValueError)r   r   s    r&   r   z0get_attribute_fqn_from_ts_node.<locals>.get_attr\  s+    (((..z$z:;;r(   prim::SetAttrr   z7Unexpected node kind when getting attribute fqn. node:  r   )r   r   nextr   r   r   r2   r   )r   r   r   
input_namer   root_attr_nameattr_fqns   `      r&   get_attribute_fqn_from_ts_noder   Y  s    <s < yy{o%$++-(224
		'ZZ\++-
EdV1M
 	
 vIj)N2@.!9+.HO GPHOr(   c                    | j                         }|dk(  rt        d|        t        j                  j	                  |      }t        |j                        j                  d      \  }}|j                  }	 t        t        j                  |      }t        ||      }|rt        ||      }|S |j                  }	 |S # t        $ r3}	t        d| j                          d| j                                |	d }	~	ww xY w)N(no schema)zgot empty schema for r   zUnable to find operator z with schema )schemaAssertionErrorr   r   parse_schemar   r   r   overload_namegetattrru   default	Exceptionr2   r   )
r   
schema_strr   nsop_nameoverrideop_overload_modop_overload_packetop_overloadrS   s
             r&   get_op_overloadr   r  s    J]"4TF;<<&+hh&;&;J&GFfkk"((.KB##H
!%))R0$_g>!"4h?K  -44K   &tyy{m=P
	s   74B< -B< <	C8.C33C8c                   x	   e Zd Zdej                  j
                  ej                  j                  z  deeej                  f   deeej                  f   deej                  j                  e
e   f   deeef   deeef   deeef   fdZd	 Zd
ej                  j                  dee   fdZd Zd ZdefdZd
ej                  j                  fdZdej                  j*                  fdZd Zdej0                  j2                  fdZd Zd
ej                  j                  fdZd
ej                  j                  fdZd
ej                  j                  fdZd
ej                  j                  fdZd
ej                  j                  fdZ d
ej                  j                  fdZ!d
ej                  j                  fdZ"d
ej                  j                  fdZ#d
ej                  j                  fdZ$d
ej                  j                  fd Z%d
ej                  j                  fd!Z&d
ej                  j                  fd"Z'd
ej                  j                  fd#Z(d
ej                  j                  fd$Z)d
ej                  j                  fd%Z*d
ej                  j                  fd&Z+d
ej                  j                  fd'Z,d
ej                  j                  fd(Z-d
ej                  j                  fd)Z.d
ej                  j                  fd*Z/d
ej                  j                  fd+Z0d
ej                  j                  fd,Z1d
ej                  j                  fd-Z2d
ej                  j                  fd.Z3d/ Z4d
ej                  j                  fd0Z5d1ej                  j                  fd2Z6d
ej                  j                  fd3Z7d
ej                  j                  fd4Z8d
ej                  j                  fd5Z9d
ej                  j                  fd6Z:d
ej                  j                  fd7Z;d
ej                  j                  fd8Z<d
ej                  j                  fd9Z=d
ej                  j                  fd:Z>d
ej                  j                  fd;Z?d
ej                  j                  fd<Z@d= ZAy>)?TS2FXGraphConverterts_graphname_to_paramname_to_bufferr   name_to_non_tensor_attributename_to_constantr   c                 ^    | _         | _        | _        t        j                  j                          _        g  _        g  _        i  _	        | _
        | _        i  _        | _        i  _        | _        t         D ]  }t#        |      }	t%         |	 fd        t'                _        y )Nc                 &    j                  |       S r_   )_convert_standard_operators)r   selfs    r&   <lambda>z.TS2FXGraphConverter.__init__.<locals>.<lambda>  s    T==dC r(   )r   r   r   r   fxr   r   input_specsoutput_specsname_to_noder   r   !name_to_non_tensor_attribute_noder   	subgraphsr   r   r   setattrr   #name_update_from_subblock_to_parent)
r  r   r   r   r   r   r   r   khandler_func_names
   `         r&   __init__zTS2FXGraphConverter.__init__  s     !*,(-(8,..0
  	
 1A 6K" BD.
 =Y):< '=# , 	A 4Q 7 !C		 >AU0r(   c                     || j                   v xsG || j                  v xs7 || j                  v xr' t        | j                  |   t        j
                        S r_   )r   r   r   r?   r   ScriptObject)r  fqns     r&   _is_get_attr_nodez%TS2FXGraphConverter._is_get_attr_node  s^    4&&& d((( t,,, Ot44S95;M;MN	
r(   r   r   c           
         g g }}|j                         D ]  }t        || j                  | j                  | j                  i | j
                  | j                        }|D ]7  }t        |      }|j                  j                  |      }	|	|j                  |<   9 |j                         }
| j                  |
      }|j                  | j                  j                  |             |j                  |        ||fS r_   )r   r   r   r   r   r   r   r   r   r   r  convertadd_subgraphr   r   )r  r   r   subgraph_nodessubgraph_convertersr   subgraph_converter	block_argnormalized_block_arg_nameplaceholder_noderi   subgraph_names               r&   _convert_block_to_subgraphz.TS2FXGraphConverter._convert_block_to_subgraph  s   .0"+[[] 	;E!4""##++%%**" ' N	,:9,E)#5#>#>#J#J-$  >N"//	:N *113H --h7M!!$--"8"8"GH&&'9:+	;, 222r(   c                    t               }|j                         D ]  }|j                         D ]  }|j                         D ][  }|j	                         | j
                  v s |j	                         | j                  vs=|j                  |j	                                ] |j                  | j                  |            }  |S )aW  
        Identify inputs from the innermost sub-block. This is needed
        for nested sub-blocks when the input is hidden in the nested sub-block.
        E.g., example IR of input is hidden in the nested sub-block.
        Graph[x.1]
        %1 = ...
            Block[]
                Block[x.1]
                    %2 = x.1 ...
        )
r   r   r   r   r   r  r   r   r   _identify_inputs_as_arguments)r  r   r   r   
block_nodeblock_node_ins         r&   r  z1TS2FXGraphConverter._identify_inputs_as_arguments  s     "e	\\^ 
	E#kkm 	
%/%6%6%8 AM%//1T5F5FF)335T=W=WW!m&=&=&?@A &OO66zB		
	 r(   c                 ^    t        | j                  t        j                  j                        S r_   )r?   r   r   r   r   )r  s    r&   r   z&TS2FXGraphConverter.is_top_level_graph  s    $--88r(   r;   c                 T    dt        | j                         }|| j                  |<   |S )N	subgraph_)r   r	  )r  ri   r   s      r&   r  z TS2FXGraphConverter.add_subgraph  s+    3t~~./0'tr(   c                    g }i }t        |j                         |j                        D ]P  \  }}|j                  r| j	                  |      ||j
                  <   1|j                  | j	                  |             R t        |      |fS r_   )r   r   r   
kwarg_onlyget_fx_value_by_ir_valuer   r   rB   )r  r   r   r4   rl   r   
schema_args          r&   get_args_kwargsz#TS2FXGraphConverter.get_args_kwargs  s    !$T[[]F4D4D!E 	BE:$$*.*G*G*Nz'D99%@A		B T{F""r(   valuec                    |j                         }|| j                  v r| j                  |   }|S || j                  v rQt        | j                  |   t        j
                        r| j                  j                  |      S | j                  |   S || j                  v r| j                  | j                  |         S t        d| d      )NzInput r   )r   r  r   r?   r   r  r   r   r   get_fx_value_by_fqnr   )r  r*  
value_name
input_nodes       r&   r'  z,TS2FXGraphConverter.get_fx_value_by_ir_value   s    __&
*****:6J4000$//
;U=O=OP}}--j99((444555++D,F,Fz,RSSvj\<==r(   c                    || j                   v r| j                   |   }|S || j                  v r| j                  |   }|S || j                  v r| j                  |   }|S || j                  v r| j                  |   }|S t	        d| d      r   )r  r   r  r   r   )r  r   fx_nodes      r&   r,  z'TS2FXGraphConverter.get_fx_value_by_fqn/  s    4$$$''-G  T***++D1G  T;;;<<TBG
 	 T66677=G  z$z:;;r(   c                    | j                          | j                  j                         D ]  }| j                  |        | j	                          t
        j                  j                  i | j                  | j                  | j                  | j                  | j                  | j                        }t        |       |j                  j!                          |S r_   )convert_graph_inputsr   r   convert_nodeconvert_graph_outputsr   r  GraphModuler	  r   r   r   r   r   r   rC   lint)r  r   rp   s      r&   r  zTS2FXGraphConverter.convert<  s    !!#MM'') 	$Dd#	$ 	""$ XX!!..$$ %% 33	
 '' MM	
 	&b)
	r(   c           
      :   | j                   j                         D ]}  }|j                         }|| j                  v rqt	        |      }| j
                  j                  t        t        j                  t        |      |             t        | j                  || j                               }n|| j                  v rrt	        |      }| j
                  j                  t        t        j                  t        |      |d             t        | j                  || j                               }n\|| j                   v rt#        | j                   |   t$        j&                        s$t)        dt+        | j                   |                t	        |      }| j
                  j                  t        t        j,                  t/        ||      |d             t        | j                  || j                               }nt#        |j+                         t$        j0                        rt	        |d	      }| j
                  j                  t        t        j2                  t        |      |             | j                  j5                  |      }|| j6                  |<    y )
Nr   argtargetT)r9  r:  
persistentz0Input conversion only handles ScriptObject, got )r   	class_fqnFr   )r   )r   r   r   r   r   r  r   r   r   	PARAMETERr   r   r   r   r   BUFFERr   r?   r   r  r   r   
CUSTOM_OBJr   	ClassType
USER_INPUTr   r  )r  graph_inputr   normalized_namer0  s        r&   r2  z(TS2FXGraphConverter.convert_graph_inputsV  s;   ==//1 =	.K((*Dt)))"0"6  ''!++*@# >MM4)@)@)B ,,,"0"6  ''!((*@##'	 >MM4)@)@)B ...!$"7"7"=u?Q?QR(J4PTPePefjPkKlJmn  #1"6  ''!,,-!0O  $#(	 >MM4)@)@)B K,,.@"0g"F  ''!,,*@# --33OD&-Dd#{=	.r(   c                    d }|j                         D cg c]  }| j                  |       }}| j                  j                  |t	        |            }|| j
                  |j                         j                         <   y c c}w )Nc                 ^    | j                  t        j                        j                         S )N)r   )tor   floatitem)ts    r&   to_float_tensorz?TS2FXGraphConverter.convert_aten_Float.<locals>.to_float_tensor  s     44ekk4*//11r(   )r   r'  r   call_functionrB   r  r   r   )r  r   rJ  r\   inp_listr0  s         r&   convert_aten_Floatz&TS2FXGraphConverter.convert_aten_Float  st    	2 CG++-P3D11#6PP----(O
 8?$++-1134 Qs   Bc                    | j                  |t        j                  j                  j                  j
                  j                        \  }}|D ]  }|dk(  s	t        ||         ||<    t        d |D              rt        j                  nt        j                  j                  fd}|j                         j                         }| j                  j                  |||      }|| j                  |<   y)z9aten::tensor creates a constant tensor ad-hoc --> GetAttrrequires_gradc              3   <   K   | ]  }t        |t                y wr_   )r?   rx   ).0ra   s     r&   	<genexpr>z:TS2FXGraphConverter.convert_aten_tensor.<locals>.<genexpr>  s     4!:a%4s   c                  D    d|v r|d   t         |d      |d<    | i |S )Nr   )_TORCH_ENUM_TO_DTYPE)r4   rl   	to_tensors     r&   r:  z7TS2FXGraphConverter.convert_aten_tensor.<locals>.target  s6    & VG_%@"6vg"Gwd-f--r(   N)r)  r   ru   rv   r   r   _schemaboolall_refsr   r   r   rK  r  )	r  r   r4   rl   r  r:  output_namer0  rU  s	           @r&   convert_aten_tensorz'TS2FXGraphConverter.convert_aten_tensor  s    ++D%))..2G2G2O2O2W2WXf 	,AO# Oq		, 4t44 LL## 		. kkm--/----fdFC)0+&r(   c                     t        j                  dd       t         fd|j                         D              } j                  j                  t        |      }| j                  |j                         j                         <   | j                  |j                  d      j                         <    j                         sL|d   j                  dk(  r9 j                  j                  |j                  d      j                                y y y )NzConverting aten::append.t, which is a inplace mutation of the list. This makes the converter non-functional: the result depends on the order of the append nodes being converter!r   
stacklevelc              3   @   K   | ]  }j                  |        y wr_   r'  )rQ  r\   r  s     r&   rR  z:TS2FXGraphConverter.convert_aten_append.<locals>.<genexpr>  s     QCT2237Q   r   r   )warningswarnrB   r   r   rK  rg   r  r   r   inputsAtr   opr  r   )r  r   r4   r0  s   `   r&   convert_aten_appendz'TS2FXGraphConverter.convert_aten_append  s     	|	
 Q4;;=QQ----k4@7>$++-1134 ;B$--*4467 &&(T!WZZ=-H4488q9I9S9S9UV .I(r(   c                 ,   |j                         j                         }d }|j                  d      r|j                  d      }|dk(  r|j	                  d      }n|dk(  r|j                  d      }n|dk(  r|j                  d      }n~|dk(  rCd| }| j                  j                  |      }|| j                  |<   ||j                  d      }}n6|dk(  r|j                  d      }nt        d|j                  d             d }|| j                  |<   y )	Nr*  r[   fr   rI  lifted_tensor_ivalzUnsupported constant type: )r   r   hasAttributekindOfr[   rh  r   r   r   r  rI  rj  r   r   )r  r   r   r*  constant_kind
alias_namer0  s          r&   convert_prim_Constantz)TS2FXGraphConverter.convert_prim_Constant  s   {{}&&(W% KK0M#w#%w#%w#%$TF+  --00<*1!!$'($&&/e&(		'* #>t{{7?S>T!UVVE&+d#r(   c                 $   |j                         D cg c]  }| j                  |       }}| j                  j                  |j	                  d      t        |            }|| j                  |j                         j                         <   y c c}w r   )	r   r'  r   call_methodr   rB   r  r   r   )r  r   r\   rL  r0  s        r&   convert_prim_CallMethodz+TS2FXGraphConverter.convert_prim_CallMethod  ss    BF++-P3D11#6PP--++FF6N(O
 8?$++-1134 Qs   Bc                 F   |j                         j                         }|j                  t        j                  j
                  j                               r>|j                         }|j                         j                         }|| j                  |<   y t        d| d      )NzUnsupported JitType (z) when get device)r   r   isSubtypeOfr   r   
TensorTypegetdevicer   r   r   r   )r  r   
input_typerw  rZ  s        r&   convert_prim_devicez'TS2FXGraphConverter.convert_prim_device  s    ZZ\&&(
!!%(("5"5"9"9";<&&(F++-113K17D!!+.4ZL@QRSSr(   c                    t        | j                  |      }|j                         j                         }|| j                  |<   | j	                         r| j                  |      r)| j                  j                  |      | j                  |<   y || j                  vr| j                  |   | j                  |<   | j                  |   | j                  |<   y | j                  |      r| j                  |   | j                  |<   y y r_   )r   r   r   r   r   r  r   r   r  r  r   )r  r   r   rZ  s       r&   convert_prim_GetAttrz(TS2FXGraphConverter.convert_prim_GetAttr  s    1$2L2LdSkkm--/2:"";/""$%%h/ 261G1G1Q!!+.4#I#II99(C ::8D 261W1W2!!+. %%h/151B1B81L!!+. 0r(   c                 n   t        | j                  |      }t        |j                               d   }| j	                  |      }| j                  |      rR| j                  j                  |      }| j                  j                  t        j                  j                  ||f       y || j                  |<   y Nr   )r   r   rB   r   r'  r  r   r   rK  r   r}   copy_r  )r  r   r   
attr_valuets_graph_tensor_inputfx_attr_nodes         r&   convert_prim_SetAttrz(TS2FXGraphConverter.convert_prim_SetAttr  s    1$2L2LdS4;;=)!,
 $ = =j I!!(+==11(;LMM''""\3H$I @UD228<r(   c                    t        |      }| j                  ||j                        \  }}| j                  j	                  |||      }|j                         dk(  r.|j                         j                         }|| j                  |<   y t        |j                               D ]P  \  }}|j                         }| j                  j	                  t        j                  ||f      }	|	| j                  |<   R y r}  )r   r)  rV  r   rK  outputsSizer   r   r  rP   outputsoperatorgetitem)
r  r   r:  r4   rl   r0  rZ  r[   outpnext_fx_nodes
             r&   convert_call_function_opz,TS2FXGraphConverter.convert_call_function_op)  s     &++D&..Af----fdFC
 "++-113K-4Dk*$T\\^4 >4"nn.#}}::$$wl  2>!!+.>r(   c                 &    | j                  |       y r_   _convert_prim_iteratorr  r   s     r&   convert_prim_TupleConstructz/TS2FXGraphConverter.convert_prim_TupleConstruct>      ##D)r(   c                 &    | j                  |       y r_   r  r  s     r&   convert_prim_ListConstructz.TS2FXGraphConverter.convert_prim_ListConstructA  r  r(   c                     |j                         D cg c]  }| j                  |       }}|j                         j                         }|| j                  |<   y c c}w r_   )r   r'  r   r   r  )r  r   r\   output_listrZ  s        r&   r  z*TS2FXGraphConverter._convert_prim_iteratorD  sP    EI[[]Sct44S9SSkkm--/)4+& Ts   Ac                 T   i }d\  }}t        |j                               D ]I  \  }}|dz  dk(  r| j                  |      } | j                  |      }||t        d      |||<   d\  }}K ||t        d      |j	                         j                         }|| j                  |<   y )N)NNr   r   z*DictConstruct has an empty key value pair.zGDictConstruct has an odd number of elements (violating our assumption).)rP   r   r'  r   r   r   r  )r  r   output_dictr  vr[   r\   rZ  s           r&   convert_prim_DictConstructz.TS2FXGraphConverter.convert_prim_DictConstructJ  s    1. 
	"FAs 1uz11#611#69	()UVV!"A!1
	" =AM Y  kkm--/)4+&r(   c                 &    | j                  |       y r_   _convert_prim_unpack_iteratorr  s     r&   convert_prim_ListUnpackz+TS2FXGraphConverter.convert_prim_ListUnpacka      **40r(   c                 &    | j                  |       y r_   r  r  s     r&   convert_prim_TupleUnpackz,TS2FXGraphConverter.convert_prim_TupleUnpackd  r  r(   c                    t        |j                               D ]o  \  }}|j                         }| j                  |j	                               }| j
                  j                  t        j                  ||f      }|| j                  |<   q y r_   )
rP   r  r   r'  r   r   rK  r  r  r  )r  r   r[   r  	outp_namer\   r0  s          r&   r  z1TS2FXGraphConverter._convert_prim_unpack_iteratorg  sr     0 	3GAt(I//

=Cmm11(2B2BS!HMG+2Di(		3r(   c                     t         j                  j                  j                  j                  }t         fd|j                         D              } j                  j                  ||dt         j                  i      } j                  j                  t         j                  j                  j                  j                  |f      }|j                         j                         }| j                  |<   y )Nc              3   @   K   | ]  }j                  |        y wr_   r`  rQ  r   r  s     r&   rR  z7TS2FXGraphConverter.convert_aten_Int.<locals>.<genexpr>r       UeT2259Ura  r   )r   ru   rv   _to_copyr   rB   r   r   rK  int32_local_scalar_denser   r   r  )r  r   r:  r4   to_copy_noder0  rZ  s   `      r&   convert_aten_Intz$TS2FXGraphConverter.convert_aten_Into  s    ((00Ut{{}UU}}2264'5;;AWX----IINN..66
 kkm--/)0+&r(   c                 F    t         j                  j                  j                  }t	         fd|j                         D              } j                  j                  ||dt         j                  i      }|j                         j                         }| j                  |<   y )Nc              3   @   K   | ]  }j                  |        y wr_   r`  r  s     r&   rR  z?TS2FXGraphConverter.convert_prim_NumToTensor.<locals>.<genexpr>  r  ra  r   )r   ru   rv   ry   rB   r   r   rK  longr   r   r  r  r   r:  r4   r0  rZ  s   `     r&   convert_prim_NumToTensorz,TS2FXGraphConverter.convert_prim_NumToTensor  ss     --Ut{{}UU----fdWejj<QRkkm--/)0+&r(   c                 ^    |j                         j                         }d| j                  |<   y )N )r   r   r   r  r   rZ  s      r&   convert_prim_CreateObjectz-TS2FXGraphConverter.convert_prim_CreateObject  s&    kkm--/24"";/r(   c                 2   t         j                  j                  j                  j                  }| j                  ||j                        \  }}| j                  j                  |||      }|j                         j                         }|| j                  |<   y r_   )r   ru   rv   convolutionr   r)  rV  r   rK  r   r   r  )r  r   r:  r4   rl   r0  rZ  s          r&   convert_aten__convolutionz-TS2FXGraphConverter.convert_aten__convolution  ss     ++33++D&..Af----fdFCkkm--/)0+&r(   c                    t        |      }|j                  }| j                  ||      \  }}|j                  dk(  r|d   j                  }|| j
                  v rt        | j
                  |   t        j                        r| j
                  |   }|j                         dk(  rt        |      }| j
                  |   j                         |d<   | j                  j                  t        j                  j                  j                   j"                  t%        |      |      }	|j'                         j)                         }
|	| j*                  |
<   y | j-                  |       y )NTensor_moder   )r   rV  r)  r   r   r   r?   r   r}   numelrN   rH  r   rK  ru   rv   rz   r{   rB   r   r   r  r  )r  r   r:  r   r4   rl   	arg1_nametensor_constantupdated_argsr0  rZ  s              r&   convert_aten_divz$TS2FXGraphConverter.convert_aten_div  s'    &++D&9f =0QID111j%%i0%,,7 #'"7"7	"B"((*a/#':L&*&;&;I&F&K&K&MLO"mm99		**66l+G #'++-"9"9";K5<D%%k2%%d+r(   c                     t         fd|j                         D              \  }} j                  j                  t        j
                  ||f      }|j                         j                         }| j                  |<   y )Nc              3   @   K   | ]  }j                  |        y wr_   r`  r  s     r&   rR  z?TS2FXGraphConverter.convert_aten___getitem__.<locals>.<genexpr>  s!      '
5:D))%0'
ra  )	rB   r   r   rK  r  r  r   r   r  )r  r   input_containerindexr0  rZ  s   `     r&   convert_aten___getitem__z,TS2FXGraphConverter.convert_aten___getitem__  sq    !& '
>Bkkm'
 "
 ----6
 kkm--/)0+&r(   c                 2   t        |      }| j                  ||j                        \  }}|t        j                  j
                  j                  j                  u s1|t        j                  j
                  j                  j                  u rq|j                         j                         D cg c]  }|j                   }}|D cg c]   }|j                         dk7  rt        |      " }}t        d |D              }	|	rt        |      dk  rt        dt        |             t!        |      }
d|
d<   | j"                  j%                  t        j                  j
                  j                  j                  t'        |
            }| j"                  j%                  t        j                  j
                  j(                  j*                  |f      }|j                         j-                         }|| j.                  |<   y | j1                  |       y c c}w c c}w )Nr   c              3   H   K   | ]  }|j                   j                    y wr_   )rV  
is_mutable)rQ  r:  s     r&   rR  z6TS2FXGraphConverter.convert_aten_to.<locals>.<genexpr>  s      %.4))%s    "r   zexpected at least 4 args, got Tr   )r   r)  rV  r   ru   rv   rF  r   
prim_dtyper   usesuserr   anyr   r   rN   r   rK  rB   cloner   r   r  r  )r  r   r:  r4   _kwargsuse
user_nodes	user_nodeuser_targetshas_mutable_targetnew_argsr0  
clone_noderZ  s                 r&   convert_aten_toz#TS2FXGraphConverter.convert_aten_to  s    &,,T6>>Bg UYY^^&&,,,%))..:K:K:V:V0V.2kkm.@.@.BCs#((CJC ",##%6  	*L 
 "% %8D% " "t9q=(+I#d))UVV:"--55IINN%%++U8_
 "]]88IINN((007*
 #kkm5571;!!+.%%d+7 Ds   ,H%Hc                    |j                         dk(  rt        |j                  d      j                         t        j
                        rft        |j                  d      j                         t        j
                        r/t        j                  j                  j                  j                  }nt        d|       t        |      }|t        j                  j                  j                  j                  u rk| j                  ||j                        \  }}|j                         j                         }| j                   j#                  t$        |      | j&                  |<   y | j)                  |       y )Nr   r   r   z$unable to determined the target for )r   r?   rd  r   r   ListTyperu   rv   r   rI  r2   r   r)  rV  r   r   r   rK  rc   r  r  )r  r   r:  r4   r  rZ  s         r&   convert_aten_addz$TS2FXGraphConverter.convert_aten_add  s   ;;=M)$--*//15>>Bza %%'H ++--"%I$#PQQ$T*FUYY^^''))) !00v~~FMD'++-113K-1]]-H-HSW-XDk*))$/r(   c                 @   t        |j                               }|d   j                         | j                  vrt	        d      t        |j                               }t        |j                               j                         }|j                         D ]  }|j                         dk(  r,|j                         j                         |k(  rt	        d      |j                         dk\  sV|j                         D ]   }|j                         |k(  st	        d        y )Nr   zKprim::Loop currently cannot run with dynamic value of number of iterations.r   z@prim::Loop currently cannot run with dynamic value of condition.r   )rN   r   r   r   r2   r   r   r  r   r  r   )r  r   r   subblockcondition_output_namer  s         r&   _check_prim_loop_supportz,TS2FXGraphConverter._check_prim_loop_support  s   dkkm$ !9 (=(==] 
 & $X%5%5%7 8 B B DNN$ 	D  "a'KKM++-1FF"V  !Q& LLN D~~'+@@*^ 	r(   c           	         t        |j                               }| j                  |       | j                  |d         }|dd  D cg c]  }|j	                          }}| j                  |      }|j                         D ]   }|j                  | j                  |         }" t        |      }| j                  ||      \  }}	t        |      dk7  rt        dt        |             |	d   }
| j                         s*| j                  j                  |
j                        | _        ||z   D cg c]  }| j                  |       }}t        |      D ]i  }| j                   j#                  t$        |d   |t        |      g|i       }|j'                         dk\  rt)        |j+                               D ]c  \  }}|j	                         }| j                   j#                  t,        j.                  ||dz   f      | j0                  |<   | j0                  |   ||<   e t)        |
j                        D ]  \  }}| j                   j#                  t,        j.                  |||j'                         z   dz   f      | j0                  |<   |j3                  |      }| j0                  |   |||j'                         z   |z   <    l y c c}w c c}w )Nr   r   r   zexpected 1 subgraph node, got )rN   r   r  r'  r   r  r   r   r   r  r   r   r   r  r,  ranger   rK  ro   r  rP   r  r  r  r  r  )r  r   r   num_iterationsr\   loop_local_argumentsglobal_argumentsr   r  r  r  r   fx_block_argsrj   	loop_noder[   r  rZ  global_argument_indexs                      r&   convert_prim_Loopz%TS2FXGraphConverter.convert_prim_Loop  s   dkkm$%%d+66vayA <B!":FCFF==dC [[] 	E/55++E2 	
   01.2.M.M"/
++ ~!# #A#nBUAV!WXX03&&(88>>&JJ 4 -/??
 $$T*
 
 n- )	H33/ #1%,- #	 
I !Q&(8 	FGAt"&.."2K59]]5P5P ((%E6D%%k2 (,'8'8'EM!$	F %"FF 4 +/--*E*E$$!D,,..2+!!$' )9(>(>t(D%%%d+ a$"2"2"447LLM9)	=  G4
s   K.Kif_nodec                     |j                         D ]5  }|j                         D ]   }|j                         dk(  st        d       7 y )Nr   zDuring converting prim::If to torch.cond, found prim::SetAttr op which is not supported yet. Please file an issue if you come across this error.)r   r   r   r2   )r  r  r   r   s       r&   _check_set_attr_in_if_blockz/TS2FXGraphConverter._check_set_attr_in_if_blockm  sM    ^^% 	E 99;/1&- 	r(   c                 $   | j                  |       t        |j                               }t        |      dk7  rt	        dt        |             | j                  |d         }| j                  |      }|j                         D ]   }|j                  | j                  |         }" t        |      }| j                  ||      \  }}t        |      dk7  rt	        dt        |             |D cg c]  }| j                  |       }	}||d   |d   t        |	      f}
| j                  j                  t        j                   |
i       }|j#                         dk(  r.|j%                         j'                         }|| j(                  |<   y |j#                         dkD  rmt+        |j-                               D ]P  \  }}|j'                         }| j                  j                  t.        j0                  ||f      }|| j(                  |<   R y y c c}w )Nr   z#expected 1 input for prim::If, got r   r   zexpected 2 subgraph nodes, got )r  rN   r   r   r   r'  r  r   r   r   r  r,  rB   r   rK  r   condr  r   r   r  rP   r  r  r  )r  r   r   	predicater   r   r  r%   r   r  r4   	cond_noderZ  r[   r   r  s                   r&   convert_prim_Ifz#TS2FXGraphConverter.convert_prim_Ifw  s   ((.dkkm$v;! #Fs6{m!TUU11&)<	 66t<	 [[] 	LE!(C(CE(JKI	L O	 ;;D)L~!# 1#n2E1FG  ENND11$7NN 11- 	
 MM//

D"E	 "++-113K-6Dk*!#&t||~6 9	6$..0--55h6F6FTUW18!!+.9 $ Os   1Hc                 &    | j                  |       y r_   )_convert_as_noopr  s     r&   convert_aten_Boolz%TS2FXGraphConverter.convert_aten_Bool  s    d#r(   c                      y r_   r`   r  s     r&   convert_prim_Enterz&TS2FXGraphConverter.convert_prim_Enter  s    
 	r(   c                      y r_   r`   r  s     r&   convert_prim_Exitz%TS2FXGraphConverter.convert_prim_Exit  s    r(   c                     t        |      }|j                  }| j                  ||      \  }}|j                         j	                         }|d   | j
                  |<   y )Nr   )r   rV  r)  r   r   r  )r  r   r:  r   r4   r  rZ  s          r&   r  z$TS2FXGraphConverter._convert_as_noop  sU     !&,,T6:gkkm--/)-a+&r(   c                      t         j                  j                  j                  }t	         fd|j                         D              } j                  j                  ||       y )Nc              3   @   K   | ]  }j                  |        y wr_   r`  r  s     r&   rR  zMTS2FXGraphConverter.convert_profiler__record_function_exit.<locals>.<genexpr>  r  ra  )r   ru   profiler_record_function_exitrB   r   r   rK  )r  r   r:  r4   s   `   r&   &convert_profiler__record_function_exitz:TS2FXGraphConverter.convert_profiler__record_function_exit  sD     ##99Ut{{}UU##FD1r(   c                     d}| j                  t        |j                                     f}| j                  j	                  ||      }|j                         j                         }|| j                  |<   y )Ntolist)r'  r   r   r   rq  r   r   r  r  s         r&   convert_prim_tolistz'TS2FXGraphConverter.convert_prim_tolist  sa     --d4;;=.ABD--++FD9kkm--/)0+&r(   c                     |j                         j                         }t        j                         | j                  |<   y r_   )r   r   r   r}   r   r  s      r&   convert_prim_Uninitializedz.TS2FXGraphConverter.convert_prim_Uninitialized  s.    
 kkm--/-2\\^k*r(   c                 
    t         |j                            }t         fd|j                         D              } j                  j                  ||      }|j                         j                         }| j                  |<   y )Nc              3   @   K   | ]  }j                  |        y wr_   r`  r  s     r&   rR  zBTS2FXGraphConverter._convert_standard_operators.<locals>.<genexpr>  r  ra  )	r   r   rB   r   r   rK  r   r   r  r  s   `     r&   r  z/TS2FXGraphConverter._convert_standard_operators  sc    +DIIK8Ut{{}UU----fd;kkm--/)0+&r(   c                 T   |j                         }t        |      }t        | || j                        }dj	                  t        |      j                  d      d d       }t        j                  d|j                  |       	  ||       y # t        $ r}t        d|       |d }~ww xY w)Nr  
r   z[%s] converts [%s]zTS2EPConverter failed for node )r   r   r   r  r   r   r   logdebug__name__r   r2   )r  r   	node_kindr  handler_funcnode_strrS   s          r&   r3  z TS2FXGraphConverter.convert_node  s    IIK	
 1;t%68U8UV
 773t9??40!45		&(=(=xH	U 	U!@LMSTT	Us   B
 
	B'B""B'c                    g }| j                   j                         D cg c]  }|j                          c}t        | j                        z   }|D ]g  }|| j
                  v r| j
                  |   }| j                         s^t        |t        j                  j                        r:|j                  dk(  r+| j                  j                  t        j                  |f      }|j                  |       | j                   j                  t#        t$        j&                  t)        |      |             || j*                  v rm|j                  | j*                  |          | j                   j                  t#        t$        j&                  t-        || j*                  |         |             [t/        d| d       t1        |      dk(  r| j                  j3                  g        y t1        |      dk(  r| j                  j3                  |d          y t1        |      dkD  r| j                  j3                  |       y | j                  j3                  |       y c c}w )	Nr   r   r8  )r   r*  zOutput r   r   r   )r   r  r   rN   r  r  r   r?   r   r  Nodere  r   rK  r  r   r  r   r   USER_OUTPUTr   r   r   r   r   r   )r  r4   r  outp_name_listrZ  r0  s         r&   r4  z)TS2FXGraphConverter.convert_graph_outputs  s   7;}}7L7L7NOt$..*ORV44S
 
 * 	DKd///++K8 //1"7EHHMM:

m3"mm99%++zRGG$!!(("..*<*  5 55D11+>?!!(("..,!,D4I4I+4V  + !7;-z!BCC?	DB t9>MM  $Y!^MM  Q Y]MM  
 MM  &c Ps   IN)Br  
__module____qualname__r   r   r   Blockdictr   r}   r   r   r  r  r	  rN   r  r  r   r  r)  Valuer'  r,  r  r5  r  r2  rM  r[  rf  ro  rr  ry  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r3  r4  r`   r(   r&   r   r     s(   DC((..588>>1DC C-.DC S%,,./	DC
 !%UXX^^SX%= >DC '+38nDC sCx.DC  $CH~DCL
3uxx}} 3c 3429 
	#EHHMM 	#>ehhnn >-- 4>.@	?uxx}} 	?1 18W W.,%((-- ,8?EHHMM ?T TM M8
U 
U>UXX]] >** **uxx}} *5588== 55uxx}} 5.1EHHMM 11UXX]] 13%((-- 31UXX]] 1 1UXX]] 15ehhmm 5	1ehhmm 	1,UXX]] ,B1UXX]] 1%,EHHMM %,N0UXX]] 0(6Nehhmm N`588== *9EHHMM *9X$ehhmm $uxx}} ehhmm 	1UXX]] 	12588== 21 1<uxx}} <1 1U U&3'r(   r   c                   N    e Zd ZdZ G d de      Zdej                  j                  ej                  j                  z  dee
ej                  f   dee
ej                  f   deej                  j                  ee
   f   dee
ef   d	ee
ef   d
ee
e
f   f fdZd Z fdZ xZS )ExplainTS2FXGraphConverterz
    Run TS2FXGraphConverter in an explain mode. It collects all failed operators conversions
    and provide that information to users. In order to collect all failed conversions, it
    also mocks some internal attributes (e.g., name_to_node).
    c                   .     e Zd Z fdZ fdZd Z xZS )$ExplainTS2FXGraphConverter._DictMockc                 2    t         |   |       || _        y r_   )superr  
mock_value)r  	dict_datar  	__class__s      r&   r  z-ExplainTS2FXGraphConverter._DictMock.__init__-  s    GY'(DOr(   c                 X    t         |   |      s| j                  S t         |   |      S r_   )r  __contains__r  __getitem__)r  keyr  s     r&   r  z0ExplainTS2FXGraphConverter._DictMock.__getitem__1  s,     7',&7&s++r(   c                      yNTr`   )r  r  s     r&   r  z1ExplainTS2FXGraphConverter._DictMock.__contains__8  s    r(   )r  r  r  r  r  r  __classcell__r  s   @r&   	_DictMockr  ,  s    	)	,	r(   r"  r   r   r   r   r   r   r   c                     t         |   |||||||       g | _        t        j	                  | j
                  t        j                  j                  d ddd di             | _        y )NmockrK  c                       y r_   r`   r`   r(   r&   r  z5ExplainTS2FXGraphConverter.__init__.<locals>.<lambda>Z  s    r(   r`   )	r  r  unsupported_node_listr  r"  r  r   r  r	  )	r  r   r   r   r   r   r   r   r  s	           r&   r  z#ExplainTS2FXGraphConverter.__init__;  sq     	"(!	
 ;=" 7@@HHMM
r(   c                     | j                          | j                  j                         D ]  }| j                  |        | j	                          y r_   )r2  r   r   r3  r4  r  s     r&   explainz"ExplainTS2FXGraphConverter.explain`  sD    !!#MM'') 	$Dd#	$""$r(   c                 z    	 t         |   |       y # t        $ r | j                  j	                  |       Y y w xY wr_   )r  r3  r   r&  r   )r  r   r  s     r&   r3  z'ExplainTS2FXGraphConverter.convert_nodef  s8    	4G & 	4&&--d3	4s    $::)r  r  r  __doc__r  r"  r   r   r   r  r   r}   r   r   r  r(  r3  r   r!  s   @r&   r  r  %  s    D #
((..588>>1#
 C-.#
 S%,,./	#

 !%UXX^^SX%= >#
 '+38n#
 sCx.#
  $CH~#
J%4 4r(   r  c              #   b   K   | j                   }d| _         	 d  || _         y # || _         w xY wwr  )disabled)r  r,  s     r&   disable_loggingr-  m  s,     ||HCL xs   /# /	,/c            	          e Zd Z	 ddej                  j
                  ej                  j                  z  deedf   de	e
ef   dz  fdZdefdZ ee      dd	       Zd
ej"                  j$                  de	e
ef   fdZd Zy)TS2EPConverterNts_modelsample_args.sample_kwargsc                    || _         t        ||      \  | _        | _        }}|| _        || _        i | _        i | _        t        | j                   t        j                  j                        s#t        | j                   j                               ng }t        | j                   t        j                  j                        sc| j                   j                         j                         D ]8  \  }t!        fd|D              r| j                  |<   *| j                  |<   : i | _        i | _        | j'                          y )Nc              3   v   K   | ]0  }j                   |j                   k(  r|k(  j                          2 y wr_   )shaperX  )rQ  paramr   s     r&   rR  z*TS2EPConverter.__init__.<locals>.<genexpr>  s6      ||u{{2 u_))+s   69)r0  r]   r   rV   r1  r2  r   r   r?   r   r   r@   rN   
parametersrW   itemsr  name_to_non_tensor_attributesr   lift_get_attr)r  r0  r1  r2  r%   
param_listr  r   s          @r&   r  zTS2EPConverter.__init__y  s    !+<X{+S(t{Aq&*6879 dmmUXX-D-DE ))+, 	
 $--)@)@A!]]557==? 	4	6 !+ 
 -3D&&q)-3D''*	4 >@*02r(   r;   c           	      :   t         j                  d       t         j                  d| j                         t        | j                        \  }}t	        | j                  | j
                  | j                  || j                  | j                  |      }|j                         }t        |       t         j                  d|j                  d             | j                  ||j                        }t         j                  d|       t        | j                  t        j                   j"                        si| j                  j%                         j'                         D ]>  \  }}||j$                  vst)        j*                  d| dd	
       ||j$                  |<   @ |S )Nz
TS2EPConverter logging starts from here.

INFO: (TORCH_LOGS="export" <cmd>)
    * Log TorchScript IR.

DEBUG: (TORCH_LOGS="+export" <cmd>), additionally
    * Log conversion IR by IR in a format of [<conversion handler name>] converts [<IR>].
        zTorchScript graph

%s
zGraphModule: %sF)print_outputz%szManually populate zN into state_dict ExportedProgram, but it is never used by the ExportedProgram.r   r]  )r  infor   r   r   r   r   r9  r   r  r   print_readableretrace_as_exported_programr?   r0  r   r   r@   rW   r8  rb  rc  )r  r   r   graph_converterrp   epr  r   s           r&   r  zTS2EPConverter.convert  sb   
	
 	,dmm<8QMM9
5 5 .MM"..!!!
 $$& 	03"B$5$55$5$IJ--,,
 	r
 $--)@)@A!]]557==? .	6BMM)MM,QC/}~#$ (.BMM!$. 	r(   c           	         t        | j                        \  }}t        | j                  | j                  | j                  || j
                  | j                  |      }|j                          t        |j                        dkD  rid}t        |j                        D ]N  \  }}dj                  t        |      j                  d      d d       }|d| d|j                          d| d	z  }P nd
}|rt        |       |S )Nr   z2Unsupported nodes are found in the following list:r  r  r   z

    z. z []zSuccess!)r   r   r  r   r   r9  r   r(  r   r&  rP   r   r   r   r   print)	r  r=  r   r   rA  explain_strr[   nr  s	            r&   r(  zTS2EPConverter.explain  s    8QMM9
5 5 5MM"..!!!
 	!4459NK!/"G"GH G1773q6<<#5bq#9:!Bqvvxj8*AFFG %K+r(   rp   r   c                 `   t        d | j                        }t        j                  j                  j                  || j                  |dd      }|j                  j                  |j                         D ci c]2  \  }}t        |t        j                  t        j                  f      r||4 c}}       |D ]  }|j                  j                  |d          |j                  j                  D ]  }|j                   t"        j$                  k(  s!|j&                  |v s0t        ||j&                     t        j                        s$t)        t+        ||j&                            d      t"        j,                  |_        d |_         |j1                         j3                  |       |S c c}}w )Nc                 ~    t        |t        j                        r"t        j                  g|j                         z  S d S r_   )r?   r   r}   r
   AUTOr   )pathxs     r&   r  z<TS2EPConverter.retrace_as_exported_program.<locals>.<lambda>  s0    (21ell(C
QUUW$ IM r(   FT)dynamic_shapesr*   pre_dispatchz& has been erroneously marked as buffer)r	   r1  r   export_trace_export
_constantsupdater8  r?   r}   r  rW   popgraph_signaturer  r   r   r>  r:  r   r   CONSTANT_TENSORr;  verifiercheck)r  rp   r   rM  rB  r  r  specs           r&   r@  z*TS2EPConverter.retrace_as_exported_program  s   
 - 	
 \\  (() ) 
 	 -224Aqa%,,0B0B!CD 1	
 " 	'AMMa&	' &&22 	'DyyI,,,@P1P!"24;;"?N( 0 =>??ef  &55	"&	' 	B	)s   87F*
c                      i dt         f fddt        j                  j                  ffd fd  j                         y )Nr  c                 h    | j                  d      }j                  }|D ]  }t        ||      } |S r   )r   r0  r   )r  r   r  rG  r  s       r&   r   z.TS2EPConverter.lift_get_attr.<locals>.get_attr0  s8    99S>DA "AqM"Hr(   r   c                     | j                  d      }| j                         j                         }|   }|r	| d| }|S |}|S )Nr   r   )r   r   r   )r   r   r   r   r   r   s        r&   get_fqnz-TS2EPConverter.lift_get_attr.<locals>.get_fqn7  sT    vI//1J2:>N:H.)9+6HO OXHOr(   c                 d   | j                         D ]  }|j                         dk(  r#|j                         j                         }d	|<   |j                         dk(  r |      } |      }|j                         j                         }|	|<   t	        |t
        j                        r|
j                  vrW|
j                  |<   nGt	        |t
        j                        r|
j                  vr|
j                  |<   n|
j                  |<   |j                         D ]
  } |         y )Nzprim::CreateObjectr  r   )r   r   r   r   r?   r   r}   r   r  r   r9  r   )r   r   rZ  r   r*  r  _dfs_get_attrr   r]  r   r  s         r&   r_  z3TS2EPConverter.lift_get_attr.<locals>._dfs_get_attr>  s    ,99;"66"&++-"9"9";K9;)+699;/1&t}H$X.E"&++-"9"9";K9A)+6!%6#4+>+>><AD//9#E5+=+=>#4+@+@@>CD11(;GL::8D $ ,H!(+,),r(   )r   r   r   r	  r   )r  r_  r   r]  r   s   `@@@@r&   r:  zTS2EPConverter.lift_get_attr  sC    " 13	# 		%((-- 		, 	,0 	dmm$r(   r_   )T)r  r  r  r   r   rA   r@   rB   r   r  r   r  r   r  r-  r  r(  r  r5  r@  r:  r`   r(   r&   r/  r/  w  s     04	"))((599+C+CC" 38_" CH~,	"H3 3j S 4.HH  . sCx..`9%r(   r/  )rename)hbuiltinsloggingr  rG   rb  collections.abcr   r   
contextlibr   r   r   torch.export._tracer   Atorch._export.passes.replace_quantized_ops_with_standard_ops_passr   torch.export.dynamic_shapesr	   r
   torch.export.exported_programr   torch.export.graph_signaturer   r   r   r   r   r   r   torch.fxr   	getLoggerr  r  r'   r:   nnModuler   r@   rB   r   rN   rA   r]   rc   rg   ro   r  r5  r   r   r   r   r   r   uint8int8int16r  int64float16float32float64	complex32	complex64
complex128rW  qint8quint8bfloat16r   r8  rT  r   maxminr  is_is_notnot_containsr   ru   rv   	sym_numelrw   sym_storage_offset
sym_strider   r  __annotations__r   r   r  r   r   r	  r   r   r   r  r-  r/  )r  r*  s   00r&   <module>r     sZ        . %     A 9   ' g!	"8/*88??UYY555/*=Ec]/*
5;;[)30B0BT0IIJ/*d!H(@ehh&:&: @" c  ,s ,s ,& 
KK	JJ	KK	KK	KK	MM1	MM1	MM1	OOQ	OOQ	b	JJ	KK	LL"	NNB $ 6J5O5O5QRzsEs
R 	'   ((LL]]"++# 99>>++))..))!IINN==IINN--= DhsCx&8!89 &0)M588>>M5
4C()4S>9:M5`S>162%((-- .Y' Y'xE4!4 E4P    _% _%C% Ss   M8