U
    zh*                     @   s   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 eZdgZdejjee eedf eeeef  eeeeef f dddZdd	d
dejjeedf eeeef  eeeeee f dddZdS )    N)AnyDictIterableOptionalTuple)log_export_usagereport_exportability.)modeltarget_submodulesargskwargsreturnc           
   
      s   |pi }g }i  dd |   D  fdd}ztz<|   D ]$\}}||kr<||j|dd q<| || W n2 tk
r }	 ztd|	  W 5 d}	~	X Y nX W 5 |D ]}|  qX  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                 S   s   i | ]\}}||qS  r   ).0namemodr   r   E/var/www/html/venv/lib/python3.8/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removeappendZregister_forward_pre_hook	Exceptionwarningswarn)
r	   r
   r   r   Zhandlesr   hr   r   er   r   r   _generate_inputs_for_submodules   s&    r    TFstrictpre_dispatch)r   r   r   r"   r#   r   c          
         s   t dd |pi }dd |  D }t| |||i  fdd| d|| t } D ](}|dk	rft|d	d
 }|| qft	dt
| |D ]}	t	|	 q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>Z   s      z(report_exportability.<locals>.<listcomp>c              
      s   |d k	s|d k	rz2t jjj| || d d |< td| W d S  tk
r } z,t|dd }t	d|| ||< W 5 d }~X Y nX | 
 D ]@\}}|dkr|n| d| }|d\}	}
|||	|
 qd S )	Nr!   zSuccessfully exported `%s`
r   z(Failed exporting `%s` with exception: %sr%   .)NN)torchZexportZ_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_inputs
try_exportr   r   r4   _   s8       z(report_exportability.<locals>.try_exportr%   Nz\nr   zFound %d export issues:)r   r   r    setvaluesr-   r.   addr+   r/   len)
r   r   r   r"   r#   Zall_submod_namesZunique_issues	exceptionkeyissuer   r2   r   r   <   s     
!)N)N)loggingr   typingr   r   r   r   r   r*   Ztorch.exportZtorch.export._traceZtorch._utils_internalr   	getLogger__name__r+   __all__nnModulestrr    boolr   r   r   r   r   r   <module>   s8   
 
/ 
