o
    Zh                     @   s  d dl Z d dlZd dlmZ d dlmZmZ d dlZd dlZd dl	Zd dl
mZ e 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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dZdS )    N)Iterable)AnyOptional)log_export_usagereport_exportabilitymodeltarget_submodulesargs.kwargsreturnc           
   
      s   |pi }g }i  dd |   D  fdd}zGz |   D ]\}}||v r0||j|dd q| |i | W n tyS } ztd|  W Y d}~nd}~ww W |D ]}	|	  qW S |D ]}	|	  qbw )	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                 S   s   i | ]\}}||qS  r   ).0namemodr   r   B/var/www/auris/lib/python3.10/site-packages/torch/_export/tools.py
<dictcomp>(   s    z3_generate_inputs_for_submodules.<locals>.<dictcomp>c                    s   ||f |  < d S Nr   )moduleZmodule_argsZmodule_kwargsresultsZsubmodule_to_namesr   r   pre_forward*   s   z4_generate_inputs_for_submodules.<locals>.pre_forwardT)Zwith_kwargszDFailed to generate submodule inputs because of the following error:
N)named_modulesappendZregister_forward_pre_hook	Exceptionwarningswarnremove)
r   r   r	   r
   Zhandlesr   r   r   ehr   r   r   _generate_inputs_for_submodules   s4   

r   TFstrictpre_dispatchr   r!   r"   c          
         s   t dd |pi }dd |  D }t| |||t i  fdd| d|| t } D ]}|durKt|d	d
 }|| q7t	dt
| |D ]}	t	|	 qV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                 S   s   g | ]
\}}|d kr|qS ) r   )r   r   _r   r   r   
<listcomp>\   s    z(report_exportability.<locals>.<listcomp>c              
      s   t | v rd S t |  |d us|d urWztjjj| || d d |< td| W d S  tyV } zt	|
dd }td|| ||< W Y d }~nd }~ww |  D ] \}}|dkre|n| d| }|d\}	}
|||	|
 q[d S )	Nr    zSuccessfully exported `%s`
r   z(Failed exporting `%s` with exception: %sr$   .)NN)typeaddtorchZexport_traceZ_exportloginfor   reprsplitwarningZnamed_childrenget)r   module_namer	   r
   r   Z	short_msgr   ZsubmodZsub_module_nameZsubmod_argsZsubmod_kwargsr"   reportr!   Zsubmod_inputsZtried_module_types
try_exportr   r   r6   b   s<   z(report_exportability.<locals>.try_exportr$   Nz\nr   zFound %d export issues:)r   r   r   setvaluesr/   r0   r*   r-   r1   len)
r   r	   r
   r!   r"   Zall_submod_namesZunique_issues	exceptionkeyissuer   r4   r   r   >   s$   
%
r   )loggingr   collections.abcr   typingr   r   r+   Ztorch.exportZtorch.export._traceZtorch._utils_internalr   	getLogger__name__r-   __all__nnModulestrtupledictr   boolr   r   r   r   r   r   <module>   sN   


/
