o
    ‡ZŽhêû  ã                   @   sü  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 d dlmZ d dlm	Z	m
Z
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  e!¡Z"dd„ Z#dd„ Z$deej%j&ej'j(f dee	 de)ej*e+d e	e
ej, f fdd„Z-dd„ Z.dd„ Z/dd„ Z0dej1j2fdd„Z3dd„ Z4dQd!e5d"e5de5fd#d$„Z6d!e5de5fd%d&„Z7d'd(„ Z8ej9d ej:d)ej;d*ej<d+ej=d,ej>d-ej?d.ej@d/ejAd0ejBd1ejCd2ejDd3ejEd4ejFd5ejGd6iZHd7d8„ eH I¡ D ƒZJd9d:„ ZKe jLe jMejNejOejPejQejReKeSejTjUjVejTjUjWejTjUjXejTjUjYd;œZZd<d=„ Z[d>d?„ Z\d@ejj*de)e]ejj^e_e5 f e]e5e5f f fdAdB„Z`dCe]e5e5f dDejjade5fdEdF„ZbdDejjafdGdH„ZcG dIdJ„ dJƒZdG dKdL„ dLedƒZeedMdN„ ƒZfG dOdP„ dPƒZgdS )Ré    N)ÚSequence)Úcontextmanager)ÚAnyÚOptionalÚUnion)Ú_C)Ú'replace_quantized_ops_with_standard_ops)Ú_tree_map_with_pathÚDim)ÚExportedProgram)ÚConstantArgumentÚCustomObjArgumentÚ	InputKindÚ	InputSpecÚ
OutputKindÚ
OutputSpecÚTensorArgument)Úsubgraph_rewriterc                 C   s^   g }t |  ¡ |ƒD ]#\}}dt| ¡ ƒv r%tj |¡\}}| t|ƒ¡ q	| |d u¡ q	|S )NZPackedParams)	ÚzipÚinputsÚstrÚtypeÚtorchÚjitÚ_flattenÚappendÚlen)Úmethod_graphÚargs_paramsÚparam_count_listZinput_Zarg_params_Úin_varsÚ_© r"   úF/var/www/auris/lib/python3.10/site-packages/torch/_export/converter.pyÚ_get_param_count_list"   s   r$   c                 C   sl   t j | ¡ ¡ }t  ¡ }t  d¡ t jj| |dddd\}}}t  |¡ |t j | ¡ ¡ kr2tdƒ‚||fS )NFT)ÚstrictZ_force_outplaceZ_return_inputs_stateszXstate_dict changed after running the tracer; something weird is happening in your model!)r   r   Ú_unique_state_dictÚkeysZis_autocast_cache_enabledZset_autocast_cache_enabledZ_get_trace_graphÚRuntimeError)ÚmodelÚargsZorig_state_dict_keysZprev_autocast_cache_enabledZtrace_graphÚ	torch_outZ_inputs_statesr"   r"   r#   Ú_trace_and_get_graph_from_model.   s    
û
ÿr,   r)   r*   Úreturnz	_C.IValuec              
   C   sÐ  t | tjjtjjfƒr›ttj t|ƒ¡d ƒ}d }t | tjjƒr}z| jj}W n t	y7 } zt
dƒ|‚d }~ww t |¡ tjt tj| j¡dd}t |¡\}}| d¡j}	t|ƒt|ƒ }
t|	|
ƒ}tj |
¡\}}t |	t|ƒ|dd¡}||||fS g }| j}t |¡ t||ƒ}t |||dd¡}|||d fS t| |ƒ\}}t |¡ tj | ¡}t| ¡ ƒ}t| ¡ ƒ}t|ƒt|ƒ }t| ¡ ƒ}t|ƒD ]\}}||krÜ| |||  ¡ qËt |¡ |||d fS )Nr   z('forward' method must be a script methodT)ZpreserveParametersÚforwardF)Ú
isinstancer   r   ÚScriptFunctionÚScriptModuleÚtupler   r.   ÚgraphÚAttributeErrorr(   r   Z$_jit_pass_onnx_function_substitutionZ_freeze_moduleÚtypingÚcastZ_cZ_jit_onnx_list_model_parametersZ_get_methodr$   Z"_propagate_and_assign_input_shapesr,   Z_jit_pass_onnx_lintr&   ÚlistÚvaluesr   r   r'   Ú	enumerateZsetDebugName)r)   r*   Zflattened_argsr+   r3   ÚeZfreezed_moduleÚmoduleÚparamsr   r   r   r    r!   Ú
state_dictZgraph_inputsZuser_input_numZparam_namesÚiÚinpr"   r"   r#   Ú_create_jit_graphJ   sX   
€ÿ
ÿ
ÿ


ÿ
€
r@   c                 C   s   | | S ©Nr"   )ÚaÚbr"   r"   r#   Úlist_add|   s   rD   c                 C   s
   | |g S rA   r"   )Ú	containerÚelementr"   r"   r#   Úlist_append€   s   
rG   c                 O   s4   |d|… }||d… }| g |¢|‘|¢R i |¤ŽS )z 
    subgraph: GraphModule from sub-block.
    iter_idx: The index of interation.
    len_loop_local_arguments: The number of loop local arguments in args.
    Nr"   )ÚsubgraphÚiter_idxZlen_loop_local_argumentsr*   ÚkwargsZloop_local_argsZglobal_argsr"   r"   r#   Úexecute_subgraph_from_prim_loop„   s   rK   Úgmc                 C   s"   dd„ }dd„ }t  | ||¡ d S )Nc                 S   sJ   t jjj | |¡}t jj |¡}t jjjj||dd}t jjj 	|¡}|S )NÚtrunc)Zrounding_mode)
r   ÚopsÚatenÚsym_sizeÚintÚscalar_tensorÚdivÚScalar_modeZIntÚTensor)ÚimÚdimÚscaleÚsym_size_intrR   Zdiv_scalar_modeZ
int_tensorr"   r"   r#   Úpattern™   s   
ÿz.inplace_optimize_sym_size_div.<locals>.patternc                 S   s   t jjj | |¡}|| S rA   )r   rN   rO   rP   rQ   )rV   rW   rX   rY   r"   r"   r#   Úreplacement¢   s   z2inplace_optimize_sym_size_div.<locals>.replacement)r   Zreplace_pattern)rL   rZ   r[   r"   r"   r#   Úinplace_optimize_sym_size_div˜   s   	r\   c                 C   s(   t | ƒdkr
tdƒ‚| d  ¡ rdS dS )Nr   zEmpty argument name for codegenFT)r   r(   Úisdigit©Únamer"   r"   r#   Úis_valid_for_codegen©   s
   r`   Úrenamer_   Úprefixc                 C   s&   |   dd¡} t| ƒr| S |› d| › S )NÚ.r!   )Úreplacer`   )r_   rb   r"   r"   r#   Únormalize_name±   s   re   c                 C   s   |   d¡}dd |¡ S )zprim::If -> convert_prim_Ifú::Zconvert_r!   )ÚsplitÚjoin)r_   Ú	name_listr"   r"   r#   Úir_name_to_func_name¸   s   
rj   c                 C   s   |r|   |¡S |  |¡S rA   )Úget_attrÚplaceholder)Úfx_graphr_   Úis_top_level_graphr"   r"   r#   Ú#get_node_as_placeholder_or_get_attr¾   s   

ro   é   é   é   é   é   é   é   é   é	   é
   é   é   é   é   c                 C   s   i | ]\}}||“qS r"   r"   )Ú.0ÚkeyÚvaluer"   r"   r#   Ú
<dictcomp>Ö   ó    r   c                 C   s$   | 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_ENUMr(   )Útensorrƒ   r"   r"   r#   Úget_dtype_as_intÙ   s   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::stridec                 C   s.   |   ¡  ¡ |  ¡  ¡ }}|  d¡}|||fS )Nr_   )ÚinputÚ	debugNameÚoutputÚs)ÚnodeÚirv_parent_nameÚirv_nameÚ	attr_namer"   r"   r#   Ú&get_ir_value_parent_name_and_attr_nameû   s   

r   c                 C   s8   g }| |v r|  ||  ¡ ||  } | |v sd t|ƒ¡S ©Nrc   )r   rh   Úreversed)ZirZref_mapZname_mapri   r"   r"   r#   Úconstruct_fqn  s   þr’   r3   c                    sF   i ‰i ‰i ‰‡ ‡‡fdd„‰ ‡‡‡‡fd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                    sR   |   ¡ D ]"}| ¡ dkrt|ƒ\}}}|ˆ|< |ˆ|< | ¡ D ]}ˆ |ƒ qqdS )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_mapr"   r#   r™   *  s   ü
ÿ÷z;get_block_to_lifted_attrs.<locals>._dfs_get_attr_dependencyc                    s„   t ƒ }|  ¡ D ]-}| ¡ D ]	}| ˆ |ƒ¡}q| ¡ dkr4| ¡  ¡ }|t ˆ ¡ ƒvr4| t	|ˆˆƒ¡ q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ˆ   r8   Úaddr’   r/   r   r   ÚGraph)r—   Ú	argumentsr‹   r˜   r   )Ú_map_blocks_to_lifted_attrsÚblocks_to_lifted_attrsrš   r›   r"   r#   r¡   :  s   
ÿ€z>get_block_to_lifted_attrs.<locals>._map_blocks_to_lifted_attrsr"   )r3   r"   )r™   r¡   r¢   rš   r›   r#   Úget_block_to_lifted_attrs	  s   r£   Úname_to_attribute_fqnr‹   c                    sŠ   dt f‡ fdd„}| ¡ dkrt| ¡ ƒ ¡ }n| ¡ dkr%| ¡  ¡ }ntd|› dƒ‚| d¡}||ƒ}|rA|› d|› }|S |}|S )	Nr_   c                    s    | ˆ v rˆ |  S t d| › dƒ‚©Nz
Attribute ú
 not found)Ú
ValueErrorr^   ©r¤   r"   r#   rk   \  s   z0get_attribute_fqn_from_ts_node.<locals>.get_attrúprim::SetAttrr“   z7Unexpected node kind when getting attribute fqn. node: ú rc   )r   r•   Únextr   rˆ   r‡   r(   rŠ   )r¤   r‹   rk   Ú
input_namerŽ   Úroot_attr_nameÚattr_fqnr"   r¨   r#   Úget_attribute_fqn_from_ts_nodeY  s   
ÿ
þr¯   c           
   
   C   s²   |   ¡ }|dksJ d| › ƒ‚tj |¡}t|jƒ d¡\}}|j}zttj	|ƒ}t||ƒ}|r8t||ƒ}W |S |j
}W |S  tyX }	 ztd|  ¡ › d|   ¡ › ƒ|	‚d }	~	ww )Nú(no schema)zgot empty schema for rf   zUnable to find operator z with schema )Úschemar   r   Zparse_schemar   r_   rg   Úoverload_nameÚgetattrrN   ÚdefaultÚ	Exceptionr(   r•   )
r‹   Z
schema_strr±   ÚnsZop_nameÚoverrideZop_overload_modZop_overload_packetZop_overloadr:   r"   r"   r#   Úget_op_overloadr  s*   
úûÿþ€ÿr¸   c                   @   sœ  e Zd Zdeejjejjf 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d	„Zd
d„ Zdejjdee	 fdd„Zdd„ Zdd„ Zde	fdd„Zdejjfdd„Zdejjfdd„Zdd„ Zdejjfdd„Zd d!„ Zdejjfd"d#„Zdejjfd$d%„Zdejjfd&d'„Zdejjfd(d)„Z dejjfd*d+„Z!dejjfd,d-„Z"dejjfd.d/„Z#dejjfd0d1„Z$dejjfd2d3„Z%dejjfd4d5„Z&dejjfd6d7„Z'dejjfd8d9„Z(dejjfd:d;„Z)dejjfd<d=„Z*dejjfd>d?„Z+dejjfd@dA„Z,dejjfdBdC„Z-dejjfdDdE„Z.dejjfdFdG„Z/dejjfdHdI„Z0dejjfdJdK„Z1dejjfdLdM„Z2dejjfdNdO„Z3dejjfdPdQ„Z4dRdS„ Z5dejjfdTdU„Z6dVejjfdWdX„Z7dejjfdYdZ„Z8dejjfd[d\„Z9dejjfd]d^„Z:dejjfd_d`„Z;dejjfdadb„Z<dejjfdcdd„Z=dejjfdedf„Z>dejjfdgdh„Z?dejjfdidj„Z@dejjfdkdl„ZAdmdn„ ZBdoS )pÚTS2FXGraphConverterÚts_graphÚname_to_paramÚname_to_bufferr¢   Úname_to_non_tensor_attributeÚname_to_constantr¤   c           
         sŠ   |ˆ _ |ˆ _|ˆ _tj ¡ ˆ _g ˆ _g ˆ _i ˆ _	|ˆ _
|ˆ _i ˆ _|ˆ _i ˆ _|ˆ _t ¡ D ]}t|ƒ}	tˆ |	‡ fdd„ƒ q.tƒ ˆ _d S )Nc                    s
   ˆ   | ¡S rA   )Ú_convert_standard_operators)r‹   ©Úselfr"   r#   Ú<lambda>Å  s   
 z.TS2FXGraphConverter.__init__.<locals>.<lambda>)rº   r»   r¼   r   ÚfxrŸ   rm   Úinput_specsÚoutput_specsÚname_to_noder¾   r¤   Ú!name_to_non_tensor_attribute_noder½   Ú	subgraphsr¢   Úkind_to_standard_operatorsr'   rj   Úsetattrrœ   Ú#name_update_from_subblock_to_parent)
rÁ   rº   r»   r¼   r¢   r½   r¾   r¤   ÚkÚhandler_func_namer"   rÀ   r#   Ú__init__‰  s,   
þ

ýzTS2FXGraphConverter.__init__c                 C   s0   || j v p|| jv p|| jv ot| j| tjƒS rA   )r¼   r»   r¾   r/   r   ÚScriptObject)rÁ   Úfqnr"   r"   r#   Ú_is_get_attr_nodeÏ  s   
ÿ
ûz%TS2FXGraphConverter._is_get_attr_noder‹   r    c              	   C   s”   g g }}|  ¡ D ]<}t|| j| j| ji | j| jƒ}|D ]}t|ƒ}|j 	|¡}	|	|j
|< q| ¡ }
|  |
¡}| | j |¡¡ | |¡ q	||fS rA   )r–   r¹   r»   r¼   r¢   r¾   r¤   re   rm   rl   rÆ   ÚconvertÚadd_subgraphr   rk   )rÁ   r‹   r    Úsubgraph_nodesÚsubgraph_convertersr˜   Úsubgraph_converterZ	block_argZnormalized_block_arg_nameZplaceholder_noderH   Zsubgraph_namer"   r"   r#   Ú_convert_block_to_subgraphÙ  s,   
ù
ÿ
z.TS2FXGraphConverter._convert_block_to_subgraphc                 C   sn   t ƒ }| ¡ D ]-}| ¡ D ]&}| ¡ D ]}| ¡ | jv r*| ¡ | jvr*| | ¡ ¡ q| |  	|¡¡}qq|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˜   Z
block_nodeZblock_node_inr"   r"   r#   rØ   ó  s   €ÿù
z1TS2FXGraphConverter._identify_inputs_as_argumentsc                 C   s   t | jtjjƒS rA   )r/   rº   r   r   rŸ   rÀ   r"   r"   r#   rn     s   z&TS2FXGraphConverter.is_top_level_graphr-   c                 C   s   dt | jƒ› }|| j|< |S )NZ	subgraph_)r   rÈ   )rÁ   rH   r_   r"   r"   r#   rÓ     s   
z TS2FXGraphConverter.add_subgraphc                 C   sV   g }i }t | ¡ |jƒD ]\}}|jr|  |¡||j< q| |  |¡¡ qt|ƒ|fS rA   )r   r   r    Z
kwarg_onlyÚget_fx_value_by_ir_valuer_   r   r2   )rÁ   r‹   r±   r*   rJ   r‡   Z
schema_argr"   r"   r#   Úget_args_kwargs  s   z#TS2FXGraphConverter.get_args_kwargsr€   c                 C   s|   |  ¡ }|| jv r| j| }|S || jv r)t| j| tjƒr$| j |¡S | j| S || jv r6|  	| j| ¡S t
d|› dƒ‚)NzInput r¦   )rˆ   rÆ   r¾   r/   r   rÏ   rm   rk   r¤   Úget_fx_value_by_fqnr§   )rÁ   r€   Z
value_nameZ
input_noder"   r"   r#   rÙ     s   




z,TS2FXGraphConverter.get_fx_value_by_ir_valuec                 C   sp   || j v r| j | }|S || jv r| j| }|S || jv r$| j| }|S || jv r0| j| }|S td|› dƒ‚r¥   )rÆ   r¾   rÇ   r½   r§   )rÁ   r_   Úfx_noder"   r"   r#   rÛ   .  s   

	
ø

ú

ü
ÿz'TS2FXGraphConverter.get_fx_value_by_fqnc                 C   sn   |   ¡  | j ¡ D ]}|  |¡ q	|  ¡  tj i | j¥| j	¥| j
¥| j¥| j¥| j¡}t|ƒ |j ¡  |S rA   )Úconvert_graph_inputsrº   r”   Úconvert_nodeÚconvert_graph_outputsr   rÃ   ÚGraphModulerÈ   r»   r¼   r½   r¾   rm   r\   r3   Zlint)rÁ   r‹   rL   r"   r"   r#   rÒ   ;  s*   ÿþýüûø
zTS2FXGraphConverter.convertc              	   C   sZ  | j  ¡ D ]¥}| ¡ }|| jv r-t|ƒ}| j ttj	t
|d|d¡ t| j||  ¡ ƒ}nx|| jv rPt|ƒ}| j ttjt
|d|dd¡ t| j||  ¡ ƒ}nU|| jv rt| j| tjƒsbJ dƒ‚t|ƒ}| j ttjt||d|dd¡ t| j||  ¡ ƒ}n$t| ¡ tjƒrŠqt|dd	}| j ttjt
|d|d¡ | j |¡}|| j|< qd S )
Nr^   ©ÚargÚtargetT)râ   rã   Ú
persistentz*Input conversion only handles ScriptObject)r_   Z	class_fqnFr‡   )rb   )rº   r   rˆ   r»   re   rÄ   r   r   r   Z	PARAMETERr   ro   rm   rn   r¼   ÚBUFFERr¾   r/   r   rÏ   Z
CUSTOM_OBJr   r   Z	ClassTypeZ
USER_INPUTrl   rÆ   )rÁ   Zgraph_inputr_   Znormalized_namerÜ   r"   r"   r#   rÝ   U  sx   
ýÿÿ
üÿÿ
ÿþÿúÿ
ÿýÿÄz(TS2FXGraphConverter.convert_graph_inputsc                    sF   dd„ }‡ fdd„|  ¡ D ƒ}ˆ j |t|ƒ¡}|ˆ j| ¡  ¡ < d S )Nc                 S   s   | j tjd ¡ S )N)rƒ   )Útor   ÚfloatÚitem)Útr"   r"   r#   Úto_float_tensor•  s   z?TS2FXGraphConverter.convert_aten_Float.<locals>.to_float_tensorc                    ó   g | ]}ˆ   |¡‘qS r"   ©rÙ   ©r~   r?   rÀ   r"   r#   Ú
<listcomp>˜  ó    
ÿz:TS2FXGraphConverter.convert_aten_Float.<locals>.<listcomp>)r   rm   Úcall_functionr2   rÆ   r‰   rˆ   )rÁ   r‹   rê   Úinp_listrÜ   r"   rÀ   r#   Úconvert_aten_Float”  s   
ÿþz&TS2FXGraphConverter.convert_aten_Floatc                    s’   |   |tjjjjj¡\}}|D ]}|dkrt|| ƒ||< qtdd„ |D ƒƒr*tjntj	j‰ ‡ fdd„}| 
¡  ¡ }| j |||¡}|| j|< dS )z9aten::tensor creates a constant tensor ad-hoc --> GetAttrZrequires_gradc                 s   s    | ]}t |tƒV  qd S rA   )r/   rQ   )r~   rB   r"   r"   r#   Ú	<genexpr>«  ó   € z:TS2FXGraphConverter.convert_aten_tensor.<locals>.<genexpr>c                     s2   d|v r|d d urt |d  |d< ˆ | i |¤ŽS )Nrƒ   )Ú_TORCH_ENUM_TO_DTYPE)r*   rJ   ©Z	to_tensorr"   r#   rã   ¯  s   z7TS2FXGraphConverter.convert_aten_tensor.<locals>.targetN)rÚ   r   rN   rO   r…   r´   Ú_schemaÚboolÚallZ_refsr‰   rˆ   rm   rð   rÆ   )rÁ   r‹   r*   rJ   rÌ   rã   Úoutput_namerÜ   r"   rö   r#   Úconvert_aten_tensor¡  s   €ÿý
z'TS2FXGraphConverter.convert_aten_tensorc                    s   t  d¡ t‡ fdd„| ¡ D ƒƒ}ˆ j t|¡}|ˆ j| ¡  	¡ < |ˆ j| 
d¡ 	¡ < ˆ  ¡ sD|d jdkrFˆ j | 
d¡ 	¡ ¡ d S d S d S )Nz±Converting 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!c                 3   ó    | ]}ˆ   |¡V  qd S rA   rì   rí   rÀ   r"   r#   ró   È  rô   z:TS2FXGraphConverter.convert_aten_append.<locals>.<genexpr>r   rl   )ÚwarningsÚwarnr2   r   rm   rð   rG   rÆ   r‰   rˆ   ÚinputsAtrn   ÚoprË   rž   )rÁ   r‹   r*   rÜ   r"   rÀ   r#   Úconvert_aten_append½  s   ÿÿz'TS2FXGraphConverter.convert_aten_appendc                 C   sÔ   |  ¡  ¡ }d }| d¡ra| d¡}|dkr| d¡}nG|dkr&| d¡}n=|dkr0| d¡}n3|dkrMd|› }| j |¡}|| j	|< || 
d¡}}n|dkrW| d¡}ntd| d¡› ƒ‚d }|| j|< d S )	Nr€   r>   ÚfrŠ   ré   Zlifted_tensor_ÚivalzUnsupported constant type: )r‰   rˆ   ZhasAttributeZkindOfr>   r  rŠ   rm   rk   rÆ   ré   r  r§   r¾   )rÁ   r‹   r_   r€   Zconstant_kindZ
alias_namerÜ   r"   r"   r#   Úconvert_prim_ConstantÓ  s*   

ÿ
z)TS2FXGraphConverter.convert_prim_Constantc                    sD   ‡ fdd„|  ¡ D ƒ}ˆ j | d¡t|ƒ¡}|ˆ j| ¡  ¡ < d S )Nc                    rë   r"   rì   rí   rÀ   r"   r#   rî   ð  rï   z?TS2FXGraphConverter.convert_prim_CallMethod.<locals>.<listcomp>r_   )r   rm   Úcall_methodrŠ   r2   rÆ   r‰   rˆ   )rÁ   r‹   rñ   rÜ   r"   rÀ   r#   Úconvert_prim_CallMethodï  s   
ÿþz+TS2FXGraphConverter.convert_prim_CallMethodc                 C   sP   |  ¡  ¡ }| tjj ¡ ¡r | ¡ }| ¡  	¡ }|| j
|< d S td|› dƒ‚)NzUnsupported JitType (z) when get device)r‡   r   ZisSubtypeOfr   r   Z
TensorTypeÚgetÚdevicer‰   rˆ   r¾   r§   )rÁ   r‹   Z
input_typer  rú   r"   r"   r#   Úconvert_prim_deviceù  s   z'TS2FXGraphConverter.convert_prim_devicec                 C   sš   t | j|ƒ}| ¡  ¡ }|| j|< |  ¡ r<|  |¡r%| j |¡| j|< d S || j	vr2| j
| | j	|< | j	| | j|< d S |  |¡rK| j| | j|< d S d S rA   )r¯   r¤   r‰   rˆ   rn   rÑ   rm   rk   rÆ   rÇ   r½   )rÁ   r‹   r®   rú   r"   r"   r#   Úconvert_prim_GetAttr  s"   


þÿÿ
ÿz(TS2FXGraphConverter.convert_prim_GetAttrc                 C   sd   t | j|ƒ}t| ¡ ƒd }|  |¡}|  |¡r+| j |¡}| j t	j
j||f¡ d S || j|< d S ©Nrp   )r¯   r¤   r2   r   rÙ   rÑ   rm   rk   rð   r   rU   Zcopy_rÇ   )rÁ   r‹   r®   Z
attr_valueZts_graph_tensor_inputZfx_attr_noder"   r"   r#   Úconvert_prim_SetAttr  s   

ÿz(TS2FXGraphConverter.convert_prim_SetAttrc           
      C   s   t |ƒ}|  ||j¡\}}| j |||¡}| ¡ dkr(| ¡  ¡ }|| j|< d S t	| 
¡ ƒD ]\}}| ¡ }| j tj||f¡}	|	| j|< q.d S r  )r¸   rÚ   r÷   rm   rð   ÚoutputsSizer‰   rˆ   rÆ   r9   ÚoutputsÚoperatorÚgetitem)
rÁ   r‹   rã   r*   rJ   rÜ   rú   r>   ÚoutpZnext_fx_noder"   r"   r#   Úconvert_call_function_op*  s   
ÿûz,TS2FXGraphConverter.convert_call_function_opc                 C   ó   |   |¡ d S rA   ©Ú_convert_prim_iterator©rÁ   r‹   r"   r"   r#   Úconvert_prim_TupleConstruct?  ó   z/TS2FXGraphConverter.convert_prim_TupleConstructc                 C   r  rA   r  r  r"   r"   r#   Úconvert_prim_ListConstructB  r  z.TS2FXGraphConverter.convert_prim_ListConstructc                    s0   ‡ fdd„|  ¡ D ƒ}| ¡  ¡ }|ˆ j|< d S )Nc                    rë   r"   rì   rí   rÀ   r"   r#   rî   F  r‚   z>TS2FXGraphConverter._convert_prim_iterator.<locals>.<listcomp>)r   r‰   rˆ   rÆ   )rÁ   r‹   Zoutput_listrú   r"   rÀ   r#   r  E  s   z*TS2FXGraphConverter._convert_prim_iteratorc                 C   sž   i }d\}}t | ¡ ƒD ])\}}|d dkr|  |¡}q|  |¡}|d ur)|d us-J dƒ‚|||< d\}}q|d u r>|d u sBJ dƒ‚| ¡  ¡ }|| j|< d S )N)NNrq   r   z*DictConstruct has an empty key value pair.zGDictConstruct has an odd number of elements (violating our assumption).)r9   r   rÙ   r‰   rˆ   rÆ   )rÁ   r‹   Zoutput_dictrÌ   Úvr>   r?   rú   r"   r"   r#   Úconvert_prim_DictConstructK  s    
ÿ
ÿz.TS2FXGraphConverter.convert_prim_DictConstructc                 C   r  rA   ©Ú_convert_prim_unpack_iteratorr  r"   r"   r#   Úconvert_prim_ListUnpackb  r  z+TS2FXGraphConverter.convert_prim_ListUnpackc                 C   r  rA   r  r  r"   r"   r#   Úconvert_prim_TupleUnpacke  r  z,TS2FXGraphConverter.convert_prim_TupleUnpackc                 C   sN   t | ¡ ƒD ]\}}| ¡ }|  | ¡ ¡}| j tj||f¡}|| j	|< qd S rA   )
r9   r  rˆ   rÙ   r‡   rm   rð   r  r  rÆ   )rÁ   r‹   r>   r  Z	outp_namer?   rÜ   r"   r"   r#   r  h  s   üz1TS2FXGraphConverter._convert_prim_unpack_iteratorc                    sn   t jjjj}t‡ fdd„| ¡ D ƒƒ}ˆ j ||dt j	i¡}ˆ j t jjj
j|f¡}| ¡  ¡ }|ˆ j|< d S )Nc                 3   rü   rA   rì   ©r~   r‡   rÀ   r"   r#   ró   s  rô   z7TS2FXGraphConverter.convert_aten_Int.<locals>.<genexpr>rƒ   )r   rN   rO   Z_to_copyr´   r2   r   rm   rð   Úint32Z_local_scalar_denser‰   rˆ   rÆ   )rÁ   r‹   rã   r*   Zto_copy_noderÜ   rú   r"   rÀ   r#   Úconvert_aten_Intp  s   ÿz$TS2FXGraphConverter.convert_aten_Intc                    sT   t jjj}t‡ fdd„| ¡ D ƒƒ}ˆ j ||dt ji¡}| 	¡  
¡ }|ˆ j|< d S )Nc                 3   rü   rA   rì   r   rÀ   r"   r#   ró   ˆ  rô   z?TS2FXGraphConverter.convert_prim_NumToTensor.<locals>.<genexpr>rƒ   )r   rN   rO   rR   r2   r   rm   rð   Úlongr‰   rˆ   rÆ   ©rÁ   r‹   rã   r*   rÜ   rú   r"   rÀ   r#   Úconvert_prim_NumToTensor€  s
   
z,TS2FXGraphConverter.convert_prim_NumToTensorc                 C   s   |  ¡  ¡ }d| j|< d S )NÚ )r‰   rˆ   r¤   ©rÁ   r‹   rú   r"   r"   r#   Úconvert_prim_CreateObjectŽ  s   z-TS2FXGraphConverter.convert_prim_CreateObjectc                 C   sH   t jjjj}|  ||j¡\}}| j |||¡}| 	¡  
¡ }|| j|< d S rA   )r   rN   rO   Zconvolutionr´   rÚ   r÷   rm   rð   r‰   rˆ   rÆ   )rÁ   r‹   rã   r*   rJ   rÜ   rú   r"   r"   r#   Úconvert_aten__convolution’  s
   z-TS2FXGraphConverter.convert_aten__convolutionc                 C   sÂ   t |ƒ}|j}|  ||¡\}}|jdkrZ|d j}|| jv rZt| j| tjƒrZ| j| }| 	¡ dkrZt
|ƒ}| j|  ¡ |d< | j tjjjjt|ƒ|¡}	| ¡  ¡ }
|	| j|
< d S |  |¡ d S )NZTensor_moderp   )r¸   r÷   rÚ   r²   r_   r¾   r/   r   rU   Znumelr7   rè   rm   rð   rN   rO   rS   rT   r2   r‰   rˆ   rÆ   r  )rÁ   r‹   rã   r±   r*   rJ   Z	arg1_nameZtensor_constantZupdated_argsrÜ   rú   r"   r"   r#   Úconvert_aten_div  s*   

ÿ

ý	
z$TS2FXGraphConverter.convert_aten_divc                    sL   t ‡ fdd„| ¡ D ƒƒ\}}ˆ j tj||f¡}| ¡  ¡ }|ˆ j|< d S )Nc                 3   rü   rA   rì   r   rÀ   r"   r#   ró   ¿  s   € 

ÿz?TS2FXGraphConverter.convert_aten___getitem__.<locals>.<genexpr>)	r2   r   rm   rð   r  r  r‰   rˆ   rÆ   )rÁ   r‹   Zinput_containerÚindexrÜ   rú   r"   rÀ   r#   Úconvert_aten___getitem__¾  s   ÿ
ÿz,TS2FXGraphConverter.convert_aten___getitem__c                 C   sî   t |ƒ}|  ||j¡\}}|tjjjjks|tjjjjkrpdd„ | 	¡  
¡ D ƒ}dd„ |D ƒ}tdd„ |D ƒƒ}|rpt|ƒdksBJ ‚t|ƒ}d|d< | j tjjjjt|ƒ¡}	| j tjjjj|	f¡}
| 	¡  ¡ }|
| j|< d S |  |¡ d S )	Nc                 S   s   g | ]}|j ‘qS r"   )Úuser)r~   Zuser"   r"   r#   rî   Ò  s    z7TS2FXGraphConverter.convert_aten_to.<locals>.<listcomp>c                 S   s    g | ]}|  ¡ d krt|ƒ‘qS )r°   )r±   r¸   )r~   Z	user_noder"   r"   r#   rî   Ó  s
    þc                 s   s    | ]}|j jV  qd S rA   )r÷   Z
is_mutable)r~   rã   r"   r"   r#   ró   Ø  s   € 
ÿz6TS2FXGraphConverter.convert_aten_to.<locals>.<genexpr>rs   Trr   )r¸   rÚ   r÷   r   rN   rO   ræ   rƒ   Z
prim_dtyper‰   ZusesÚanyr   r7   rm   rð   r2   Úcloner´   rˆ   rÆ   r  )rÁ   r‹   rã   r*   Ú_kwargsZ
user_nodesZuser_targetsZhas_mutable_targetÚnew_argsrÜ   Z
clone_noderú   r"   r"   r#   Úconvert_aten_toÈ  s0    þÿÿÿ
z#TS2FXGraphConverter.convert_aten_toc                 C   s°   |  ¡ dkr*t| d¡ ¡ tjƒr#t| d¡ ¡ tjƒr#tjjjj	}nt
d|› ƒ‚t|ƒ}|tjjjj	krQ|  ||j¡\}}| ¡  ¡ }| j t|¡| j|< d S |  |¡ d S )Nr°   r   rp   z#unable to determind the target for )r±   r/   rÿ   r   r   ZListTyperN   rO   rž   ré   r(   r¸   rÚ   r÷   r‰   rˆ   rm   rð   rD   rÆ   r  )rÁ   r‹   rã   r*   r0  rú   r"   r"   r#   Úconvert_aten_addî  s   ÿz$TS2FXGraphConverter.convert_aten_addc                 C   s¦   t | ¡ ƒ}|d  ¡ | jvrtdƒ‚t| ¡ ƒ}t| ¡ ƒ ¡ }| ¡ D ]+}| 	¡ dkr9| 
¡  ¡ |kr9tdƒ‚| 	¡ dkrP| ¡ D ]}| ¡ |krOtdƒ‚qCq%d S )Nr   zKprim::Loop currently cannot run with dynamic value of number of iterations.rp   z@prim::Loop currently cannot run with dynamic value of condition.rq   )r7   r   rˆ   r¾   r(   r«   r–   r  r”   r  r‰   )rÁ   r‹   r   ÚsubblockZcondition_output_namer  r"   r"   r#   Ú_check_prim_loop_support  s,   ÿÿÿÿ€öz,TS2FXGraphConverter._check_prim_loop_supportc              	      sª  t | ¡ ƒ}ˆ  |¡ ˆ  |d ¡}dd„ |dd … D ƒ}ˆ  |¡}| ¡ D ]
}| ˆ j| ¡}q&t |ƒ}ˆ  ||¡\}}t	|ƒdksEJ ‚|d }	ˆ  
¡ sUˆ j |	j¡ˆ _‡ fdd„|| D ƒ}
t|ƒD ]n}ˆ j t|d |t	|ƒg|
¢R i ¡}| ¡ dkr£t| ¡ ƒD ]\}}| ¡ }ˆ j tj||d f¡ˆ j|< ˆ j| |
|< q„t|	jƒD ])\}}ˆ j tj||| ¡  d f¡ˆ j|< | |¡}ˆ j| |
|| ¡  | < q¨qdd S )Nr   c                 S   ó   g | ]}|  ¡ ‘qS r"   ©rˆ   rí   r"   r"   r#   rî   $  ó    z9TS2FXGraphConverter.convert_prim_Loop.<locals>.<listcomp>rq   rp   c                    rë   r"   ©rÛ   ©r~   r_   rÀ   r"   r#   rî   =  s    ÿÿ)r7   r   r5  rÙ   rØ   r–   r   r¢   r×   r   rn   rË   Úrangerm   rð   rK   r  r9   r  rˆ   r  r  rÆ   r+  )rÁ   r‹   r   Znum_iterationsZloop_local_argumentsZglobal_argumentsr˜   rÔ   rÕ   rÖ   Úfx_block_argsrI   Z	loop_noder>   r  rú   r_   Zglobal_argument_indexr"   rÀ   r#   Úconvert_prim_Loop  sx   

ÿÿÿÿ
þýü÷þ
þÿþ
þ
þÿõåz%TS2FXGraphConverter.convert_prim_LoopÚif_nodec                 C   s4   |  ¡ D ]}| ¡ D ]}| ¡ dkrtdƒ‚q
qd S )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•   r(   )rÁ   r>  r˜   r‹   r"   r"   r#   Ú_check_set_attr_in_if_blockk  s   ÿÿÿÿz/TS2FXGraphConverter._check_set_attr_in_if_blockc                    s6  ˆ   |¡ t| ¡ ƒ}t|ƒdksJ ‚ˆ  |d ¡}ˆ  |¡}| ¡ D ]
}| ˆ j| ¡}q#t|ƒ}ˆ  	||¡\}}t|ƒdksBJ ‚‡ fdd„|D ƒ}||d |d t
|ƒf}	ˆ j tj|	i ¡}
| ¡ dkrs| ¡  ¡ }|
ˆ j|< d S | ¡ dkr—t| ¡ ƒD ]\}}| ¡ }ˆ j tj|
|f¡}|ˆ j|< qd S d S )Nrp   r   rq   c                    rë   r"   r9  r:  rÀ   r"   r#   rî   ˆ  r‚   z7TS2FXGraphConverter.convert_prim_If.<locals>.<listcomp>)r?  r7   r   r   rÙ   rØ   r–   r   r¢   r×   r2   rm   rð   r   Zcondr  r‰   rˆ   rÆ   r9   r  r  r  )rÁ   r‹   r   Ú	predicater    r˜   rÔ   r!   r<  r*   Z	cond_noderú   r>   r‰   r  r"   rÀ   r#   Úconvert_prim_Ifu  s6   

üüz#TS2FXGraphConverter.convert_prim_Ifc                 C   r  rA   )Ú_convert_as_noopr  r"   r"   r#   Úconvert_aten_Bool  r  z%TS2FXGraphConverter.convert_aten_Boolc                 C   ó   d S rA   r"   r  r"   r"   r#   Úconvert_prim_Enter   s   z&TS2FXGraphConverter.convert_prim_Enterc                 C   rD  rA   r"   r  r"   r"   r#   Úconvert_prim_Exit§  s   z%TS2FXGraphConverter.convert_prim_Exitc                 C   s<   t |ƒ}|j}|  ||¡\}}| ¡  ¡ }|d | j|< d S )Nr   )r¸   r÷   rÚ   r‰   rˆ   rÆ   )rÁ   r‹   rã   r±   r*   r0  rú   r"   r"   r#   rB  «  s
   z$TS2FXGraphConverter._convert_as_noopc                    s6   t jjj}t‡ fdd„| ¡ D ƒƒ}ˆ j ||¡ d S )Nc                 3   rü   rA   rì   r   rÀ   r"   r#   ró   »  rô   zMTS2FXGraphConverter.convert_profiler__record_function_exit.<locals>.<genexpr>)r   rN   ZprofilerZ_record_function_exitr2   r   rm   rð   )rÁ   r‹   rã   r*   r"   rÀ   r#   Ú&convert_profiler__record_function_exit¶  s   
z:TS2FXGraphConverter.convert_profiler__record_function_exitc                 C   s@   d}|   t| ¡ ƒ¡f}| j ||¡}| ¡  ¡ }|| j|< d S )NÚtolist)rÙ   r«   r   rm   r  r‰   rˆ   rÆ   r$  r"   r"   r#   Úconvert_prim_tolist¾  s
   z'TS2FXGraphConverter.convert_prim_tolistc                 C   s   |  ¡  ¡ }t ¡ | j|< d S rA   )r‰   rˆ   r   rU   r¾   r'  r"   r"   r#   Úconvert_prim_UninitializedÇ  s   z.TS2FXGraphConverter.convert_prim_Uninitializedc                    sN   t | ¡  }t‡ fdd„| ¡ D ƒƒ}ˆ j ||¡}| ¡  ¡ }|ˆ j|< d S )Nc                 3   rü   rA   rì   r   rÀ   r"   r#   ró   Ñ  rô   zBTS2FXGraphConverter._convert_standard_operators.<locals>.<genexpr>)	rÉ   r•   r2   r   rm   rð   r‰   rˆ   rÆ   r$  r"   rÀ   r#   r¿   Ï  s
   z/TS2FXGraphConverter._convert_standard_operatorsc              
   C   s‚   |  ¡ }t|ƒ}t| || jƒ}d t|ƒ d¡d d… ¡}t d|j	|¡ z||ƒ W d S  t
y@ } ztd|› ƒ|‚d }~ww )Nr&  Ú
rp   z[%s] converts [%s]zTS2EPConverter failed for node )r•   rj   r³   r  rh   r   rg   ÚlogÚdebugÚ__name__rµ   r(   )rÁ   r‹   Z	node_kindrÍ   Zhandler_funcÚnode_strr:   r"   r"   r#   rÞ   Ö  s   €ÿz TS2FXGraphConverter.convert_nodec              	   C   sT  g }dd„ | j  ¡ D ƒt| jƒ }|D ]b}|| jv rM| j| }|  ¡ s8t|tjj	ƒr8|j
dkr8| j tj|f¡}| |¡ | j ttjt|d|d¡ q|| jv rn| | j| ¡ | j ttjt|| j| d|d¡ qtd|› dƒ‚t|ƒd	kr„| j g ¡ d S t|ƒd
kr”| j |d	 ¡ d S t|ƒd
kr¢| j |¡ d S | j |¡ d S )Nc                 S   r6  r"   r7  )r~   r  r"   r"   r#   rî   ë  r8  z=TS2FXGraphConverter.convert_graph_outputs.<locals>.<listcomp>rl   r^   rá   )r_   r€   zOutput r¦   r   rp   )rº   r  r7   rË   rÆ   rn   r/   r   rÃ   ÚNoder   rm   rð   r/  r   rÅ   r   r   ZUSER_OUTPUTr   r¾   r   r§   r   r‰   )rÁ   r*   Zoutp_name_listrú   rÜ   r"   r"   r#   rß   é  sX   ÿ

ÿþ

ýÿ

ÿûÿ
ÿÿz)TS2FXGraphConverter.convert_graph_outputsN)CrN  Ú
__module__Ú__qualname__r   r   r   rŸ   ÚBlockÚdictr   rU   rœ   r   rÎ   rÑ   rP  r7   r×   rØ   rn   rÓ   rÚ   ÚValuerÙ   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,  r2  r3  r5  r=  r?  rA  rC  rE  rF  rB  rG  rI  rJ  r¿   rÞ   rß   r"   r"   r"   r#   r¹   ˆ  s€    þýüû
ú
ù

øF
?
	!
&N
(	r¹   c                       s®   e Zd ZdZG dd„ deƒZdeejj	ejj
f 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d„Zdd„ Z‡ fdd„Z‡  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                       s0   e Zd Z‡ fdd„Z‡ fdd„Zdd„ Z‡  ZS )z$ExplainTS2FXGraphConverter._DictMockc                    s   t ƒ  |¡ || _d S rA   )ÚsuperrÎ   Ú
mock_value)rÁ   Z	dict_datarX  ©Ú	__class__r"   r#   rÎ   '  s   
z-ExplainTS2FXGraphConverter._DictMock.__init__c                    s   t ƒ  |¡s	| jS t ƒ  |¡S rA   )rW  Ú__contains__rX  Ú__getitem__©rÁ   r   rY  r"   r#   r\  +  s   z0ExplainTS2FXGraphConverter._DictMock.__getitem__c                 C   s   dS ©NTr"   r]  r"   r"   r#   r[  2  s   z1ExplainTS2FXGraphConverter._DictMock.__contains__)rN  rQ  rR  rÎ   r\  r[  Ú__classcell__r"   r"   rY  r#   Ú	_DictMock&  s    r`  rº   r»   r¼   r¢   r½   r¾   r¤   c                    sH   t ƒ  |||||||¡ g | _t | jtj d dddd„ di ¡¡| _d S )NZmockrð   c                   S   rD  rA   r"   r"   r"   r"   r#   rÂ   T  s    z5ExplainTS2FXGraphConverter.__init__.<locals>.<lambda>r"   )	rW  rÎ   Úunsupported_node_listrV  r`  rÆ   r   rÃ   rP  )rÁ   rº   r»   r¼   r¢   r½   r¾   r¤   rY  r"   r#   rÎ   5  s*   
ùú
ýz#ExplainTS2FXGraphConverter.__init__c                 C   s.   |   ¡  | j ¡ D ]}|  |¡ q	|  ¡  d S rA   )rÝ   rº   r”   rÞ   rß   r  r"   r"   r#   ÚexplainZ  s   z"ExplainTS2FXGraphConverter.explainc                    s4   z	t ƒ  |¡ W d S  ty   | j |¡ Y d S w rA   )rW  rÞ   rµ   ra  r   r  rY  r"   r#   rÞ   `  s
   ÿz'ExplainTS2FXGraphConverter.convert_node)rN  rQ  rR  Ú__doc__rT  r`  r   r   r   rŸ   rS  r   rU   rœ   r   rÎ   rb  rÞ   r_  r"   r"   rY  r#   rV    s(    þýüû
ú
ù
ø%rV  c                 c   s*    | j }d| _ z	d V  W || _ d S || _ w r^  )Údisabled)rL  rd  r"   r"   r#   Údisable_loggingg  s   €re  c                
   @   s   e Zd Z	ddeejjejjf dee	df de
eee	f  fdd„Zdefd	d
„Zeeƒddd„ƒZdejjdeee	f fdd„Zdd„ ZdS )ÚTS2EPConverterNÚts_modelÚsample_args.Úsample_kwargsc                    sÂ   || _ t||ƒ\| _| _}}|| _|| _i | _i | _t| j t	j
jƒs)t| j  ¡ ƒng }t| j t	j
jƒsU| j  ¡  ¡ D ]\}‰ t‡ fdd„|D ƒƒrOˆ | j|< q:ˆ | j|< q:i | _i | _|  ¡  d S )Nc                 3   s(    | ]}ˆ j |j krˆ |k ¡ V  qd S rA   )Úshaperù   )r~   Úparam©r…   r"   r#   ró   ‰  s   € 
þýz*TS2EPConverter.__init__.<locals>.<genexpr>)rg  r@   rº   r<   rh  ri  r»   r¼   r/   r   r   r0   r7   Ú
parametersr=   Úitemsr.  Úname_to_non_tensor_attributesr¾   Úlift_get_attr)rÁ   rg  rh  ri  r!   Z
param_listrÌ   r"   rl  r#   rÎ   s  s(   ÿýþzTS2EPConverter.__init__r-   c                 C   sÔ   t  d¡ t  d| j¡ t| jƒ\}}t| j| j| j|| j| j|ƒ}| 	¡ }t
|ƒ t  d|jdd¡ |  ||j¡}t  d|¡ t| jtjjƒsh| j ¡  ¡ D ]\}}||jvrgt d|› d¡ ||j|< qP|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.)rL  Úinforº   r£   r¹   r»   r¼   ro  r¾   rÒ   r   Zprint_readableÚretrace_as_exported_programr/   rg  r   r   r0   r=   rn  rý   rþ   )rÁ   r¢   r¤   Úgraph_converterrL   ÚeprÌ   r…   r"   r"   r#   rÒ   —  sB   ÿÿù	þ

ÿ
€zTS2EPConverter.convertTc           	   	   C   s¬   t | jƒ\}}t| j| j| j|| j| j|ƒ}| ¡  t|j	ƒdkrLd}t
|j	ƒD ]"\}}d t|ƒ d¡d d… ¡}|d|› d| ¡ › d|› d	7 }q(nd
}|rTt|ƒ |S )Nr   z2Unsupported nodes are found in the following list:r&  rK  rp   z

    z. z [ú]zSuccess!)r£   rº   rV  r»   r¼   ro  r¾   rb  r   ra  r9   rh   r   rg   r•   Úprint)	rÁ   rq  r¢   r¤   rt  Zexplain_strr>   ÚnrO  r"   r"   r#   rb  Ë  s.   ÿù	"þzTS2EPConverter.explainrL   r¾   c                 C   sÄ   t dd„ | jƒ}tjjj|| j|ddd}|j dd„ | ¡ D ƒ¡ |D ]	}|j	 
|d ¡ q$|jjD ]&}|jtjkrX|j|v rXt||j tjƒsTJ t||j ƒ› dƒ‚tj|_q2| ¡  |¡ |S )	Nc                 S   s    t |tjƒrtjg| ¡  S d S rA   )r/   r   rU   r
   ZAUTOrW   )ÚpathÚxr"   r"   r#   rÂ   ì  s    z<TS2EPConverter.retrace_as_exported_program.<locals>.<lambda>FT)Údynamic_shapesr%   Zpre_dispatchc                 S   s(   i | ]\}}t |tjtjfƒr||“qS r"   )r/   r   rU   rÏ   )r~   rÌ   r  r"   r"   r#   r      s    ýÿz>TS2EPConverter.retrace_as_exported_program.<locals>.<dictcomp>z& has been erroneously marked as buffer)r	   rh  r   ZexportÚ_traceZ_exportÚ
_constantsÚupdatern  r=   ÚpopZgraph_signaturerÄ   r•   r   rå   rã   r/   rU   r   ZCONSTANT_TENSORZverifierÚcheck)rÁ   rL   r¾   r{  ru  rÌ   Úspecr"   r"   r#   rs  æ  s:   üûþÿÿþ€z*TS2EPConverter.retrace_as_exported_programc                    sN   i ‰dt f‡fdd„‰dtjjf‡fdd„‰‡ ‡‡‡‡fdd„‰ ˆ ˆjƒ d S )	NrÐ   c                    s(   |   d¡}ˆ j}|D ]}t||ƒ}q
|S r   )rg   rg  r³   )rÐ   r_   r  rx  rÀ   r"   r#   rk   '  s
   
z.TS2EPConverter.lift_get_attr.<locals>.get_attrr‹   c                    s<   |   d¡}|  ¡  ¡ }ˆ | }|r|› d|› }|S |}|S )Nr_   rc   )rŠ   r‡   rˆ   )r‹   rŽ   r¬   r­   r®   r¨   r"   r#   Úget_fqn.  s   
ÿz-TS2EPConverter.lift_get_attr.<locals>.get_fqnc                    sÆ   |   ¡ D ]\}| ¡ dkr| ¡  ¡ }dˆ|< | ¡ dkrUˆ|ƒ}ˆ|ƒ}| ¡  ¡ }|ˆ|< t|tjƒr?|ˆjvr>|ˆj|< nt|tjƒrP|ˆj	vrO|ˆj	|< n|ˆj
|< | ¡ D ]}ˆ |ƒ qYqd S )Nzprim::CreateObjectr&  r“   )r”   r•   r‰   rˆ   r/   r   rU   r¼   rÏ   r¾   ro  r–   )r˜   r‹   rú   r®   r€   r4  ©Ú_dfs_get_attrrk   r‚  r¤   rÁ   r"   r#   r„  5  s,   

€

€

ÿìz3TS2EPConverter.lift_get_attr.<locals>._dfs_get_attr)r   r   r   rP  rº   rÀ   r"   rƒ  r#   rp    s
   zTS2EPConverter.lift_get_attrrA   )T)rN  rQ  rR  r   r   r   r1   r0   r2   r   r   rT  r   rÎ   r   rÒ   re  rL  rb  rÃ   rà   rs  rp  r"   r"   r"   r#   rf  q  s$    üþ
ý
ü$4þ

ý.rf  )ra   )hÚbuiltinsÚloggingr  r5   rý   Úcollections.abcr   Ú
contextlibr   r   r   r   r   Ztorch.export._tracer   ZAtorch._export.passes.replace_quantized_ops_with_standard_ops_passr   Ztorch.export.dynamic_shapesr	   r
   Ztorch.export.exported_programr   Ztorch.export.graph_signaturer   r   r   r   r   r   r   Ztorch.fxr   Ú	getLoggerrN  rL  r$   r,   ÚnnÚModuler   r0   r2   rŸ   r7   r1   r@   rD   rG   rK   rÃ   rà   r\   r`   r   re   rj   ro   Zuint8Zint8Zint16r!  Zint64Zfloat16Zfloat32Zfloat64Z	complex32Z	complex64Z
complex128rø   Zqint8Zquint8Zbfloat16r„   rn  rõ   r†   ÚmaxÚminr  Úis_Úis_notÚnot_Úcontainsr   rN   rO   Z	sym_numelrP   Zsym_storage_offsetZ
sym_striderÉ   r   r’   rT  rS  rœ   r£   rP  r¯   r¸   r¹   rV  re  rf  r"   r"   r"   r#   Ú<module>   sº   $	
ÿÿ
þ2ññÿ"
þP
ÿÿ
þ       H
	