o
    "i*                     @   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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)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}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   Q/home/ubuntu/SoloSpeech/.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:
N)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   s4   

r'   TFstrictpre_dispatchr   r)   r*   c          
         s   t dd |pi }dd |  D }t| |||i  fdd| d|| t } D ]}|durGt|d	d
 }|| q3t	dt
| |D ]}	t	|	 qR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 us|d urHzt jjj| || d d |< td| W d S  tyG } zt|dd }t	d|| ||< W Y d }~nd }~ww | 
 D ] \}}|dkrV|n| d| }|d\}	}
|||	|
 qLd S )	Nr(   zSuccessfully exported `%s`
r   z(Failed exporting `%s` with exception: %sr,   .)NN)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
try_exportr   r   rE   _   s6   z(report_exportability.<locals>.try_exportr,   Nz\nr   zFound %d export issues:)r   r   r'   setvaluesr7   r8   addr5   r9   len)
r   r   r   r)   r*   all_submod_namesunique_issues	exceptionkeyissuer   rB   r   r   <   s"   
!
r   )loggingr!   typingr   r   r   r   r   r1   torch.exporttorch.export._tracetorch._utils_internalr   	getLogger__name__r5   __all__nnModulestrr'   boolr    r   r   r   r   r   <module>   sL   


/
