o
    ei                     @   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}zIz |   D ]\}}||v r0||j|dd q| |i | W n tyU } ztjd| dd	 W Y d
}~nd
}~ww W |D ]}	|	  qY S |D ]}	|	  qdw )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   ]/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/_export/tools.py
<dictcomp>(   s    z3_generate_inputs_for_submodules.<locals>.<dictcomp>c                    s   ||f |  < d S Nr   )modulemodule_argsmodule_kwargsresultssubmodule_to_namesr   r   pre_forward*   s   z4_generate_inputs_for_submodules.<locals>.pre_forwardT)with_kwargszDFailed to generate submodule inputs because of the following error:
   )
stacklevelN)named_modulesappendregister_forward_pre_hook	Exceptionwarningswarnremove)
r   r   r	   r
   handlesr   r   r   ehr   r   r   _generate_inputs_for_submodules   s6   

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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_kwargsr*   reportr)   submod_inputstried_module_types
try_exportr   r   rH   c   s<   z(report_exportability.<locals>.try_exportr,   Nz\nr   zFound %d export issues:)r   r   r'   setvaluesr9   r:   r2   r7   r;   len)
r   r	   r
   r)   r*   all_submod_namesunique_issues	exceptionkeyissuer   rD   r   r   ?   s$   
%
r   )loggingr!   collections.abcr   typingr   r   r3   torch.exporttorch.export._tracetorch._utils_internalr   	getLogger__name__r7   __all__nnModulestrtupledictr'   boolr    r   r   r   r   r   <module>   sN   


0
