
    קg                        d dl Z d dlZd dlmZmZmZmZmZ d dlZd dl	Zd dl
Zd dlmZ  e j        e          ZdgZ	 ddej        j        dee         deedf         d	eeeef                  d
eeeeef         f         f
dZ	 dddddej        j        deedf         d	eeeef                  deded
eeee         f         fdZdS )    N)AnyDictIterableOptionalTuple)log_export_usagereport_exportabilitymodeltarget_submodulesargs.kwargsreturnc                   
 |pi }g }i 
d |                                  D             
fd}	 |                                  D ]3\  }}||v r*|                    |                    |d                     4 | |i | n.# t          $ r!}t	          j        d|            Y d}~nd}~ww xY w|D ]}	|	                                 n# |D ]}	|	                                 w xY w
S )a~  
    Generate inputs for targeting submdoules in the given model. Note that if two submodules refer to the same obj, this
    function doesn't work.

    Args:
        model: root model.
        inputs: inputs to the root model.
        target_submodules: submodules that we want to generate inputs for.

    Returns:
        A dict that maps from submodule name to its inputs.
    c                     i | ]\  }}||	S  r   ).0namemods      O/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_export/tools.py
<dictcomp>z3_generate_inputs_for_submodules.<locals>.<dictcomp>'   s    KKK	c#tKKK    c                 "    ||f|          <   d S Nr   )modulemodule_argsmodule_kwargsresultssubmodule_to_namess      r   pre_forwardz4_generate_inputs_for_submodules.<locals>.pre_forward)   s    /:M.J"6*+++r   T)with_kwargszDFailed to generate submodule inputs because of the following error:
N)named_modulesappendregister_forward_pre_hook	Exceptionwarningswarnremove)r
   r   r   r   handlesr   r   r   ehr   r   s             @@r   _generate_inputs_for_submodulesr+      s|   $ \rFGGKKU5H5H5J5JKKKK K K K K K,,.. 	 	ID#(((11+41PP   	tv 
 
 
WTUWW	
 	
 	
 	
 	
 	
 	
 	


  	 	AHHJJJJ	 	 	AHHJJJJ	Ns0   AB  C	 
B,B'"C	 'B,,C	 	C$TFstrictpre_dispatchr   r-   r.   c                @  
 t          d           |pi }d |                                 D             }t          | |||          t                      i 

fd | d||           t                      }
                                D ]A}|=t          |                              d          d         }|                    |           Bt          	                    d	t          |                     |D ]}	t          	                    |	           
S )
a  
    Report exportability issues for a module in one-shot.

    Args:
        mod: root module.
        args: args to the root module.
        kwargs: kwargs to the root module.
    Returns:
        A dict that maps from submodule name to the exception that was raised when trying to export it.
        `None` means the module is exportable without issue.
    Sample output:
        {
            '': UnsupportedOperatorException(func=<OpOverload(op='testlib.op_missing_meta', overload='default')>),
            'submod_1': UnsupportedOperatorException(func=<OpOverload(op='testlib.op_missing_meta', overload='default')>),
            'submod_2': None
        }
    zexport.report_exportability)eventc                 $    g | ]\  }}|d k    |S ) r   )r   r   _s      r   
<listcomp>z(report_exportability.<locals>.<listcomp>[   s!    NNNq42:::::r   c                 r   t          |           v rd S                     t          |                      ||	 t          j        j                            | ||           d |<   t                              d|           d S # t          $ rS}t          |          
                    d          d         }t                              d||           ||<   Y d }~nd }~ww xY w|                                 D ];\  }}|dk    r|n| d| }                    |d          \  }	}
 |||	|
           <d S )	Nr,   zSuccessfully exported `%s`
r   z(Failed exporting `%s` with exception: %sr2   .)NN)typeaddtorchexport_trace_exportloginfor$   reprsplitwarningnamed_childrenget)r   module_namer   r   r)   	short_msgr   submodsub_module_namesubmod_argssubmod_kwargsr.   reportr-   submod_inputstried_module_types
try_exports              r   rN   z(report_exportability.<locals>.try_exporta   s    <<---FtF||,,,v1(#++!!- ,    '+{#5{CCC ( ( ( GGMM$//2	>Y   '({######( #1133 	L 	LLD&&1R&7&7dd=T=Td=T=TO)6):):* *&K Jv]KKKKs   A	B 
C$A	CC$r2   Nz\nr   zFound %d export issues:)r   r!   r+   setvaluesr@   rA   r9   r>   rB   len)r   r   r   r-   r.   all_submod_namesunique_issues	exceptionkeyissuerK   rL   rM   rN   s      ``     @@@@r   r	   r	   =   sS   4 89999\rFNNC,=,=,?,?NNN3C9I4QWXXM-/F# # # # # # # # # #J JsBf%%%EEM]]__ # #	 y//''..q1Cc"""KK)3}+=+=>>>  EMr   r   )loggingr%   typingr   r   r   r   r   r:   torch.exporttorch.export._tracetorch._utils_internalr   	getLogger__name__r>   __all__nnModulestrr+   boolr$   r	   r   r   r   <module>rc      s     7 7 7 7 7 7 7 7 7 7 7 7 7 7          2 2 2 2 2 2 g!!!
" (,	) )8?)}) S/) T#s(^$	)
 
#uS#X
) ) ) )^ (,U
 U U U	U
S/U T#s(^$U
 U U 
#x	"
"#U U U U U Ur   