
    Vh                     x   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 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y)    N)Iterable)AnyOptional)log_export_usagereport_exportabilitymodeltarget_submodulesargs.kwargsreturnc                   
 |xs i }g }i 
| j                         D ci c]  \  }}||
 c}}
fd}	 | j                         D ],  \  }}||v s|j                  |j                  |d             .  | |i | |D ]  }	|	j                           
S c c}}w # t        $ r"}t	        j
                  d|        Y d}~Ed}~ww xY w# |D ]  }	|	j                           w xY w)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                     ||f|    <   y N )modulemodule_argsmodule_kwargsresultssubmodule_to_namess      C/home/dcms/DCMS/lib/python3.12/site-packages/torch/_export/tools.pypre_forwardz4_generate_inputs_for_submodules.<locals>.pre_forward*   s    /:M.J"6*+    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   handlesnamemodr   ehr   r   s             @@r   _generate_inputs_for_submodulesr&      s   $ \rFGG5:5H5H5JK	c#t)KK,,. 	ID#((11+41P	
 	tv  	AHHJ	N' L  
STUSVW	
 	


  	AHHJ	s4   BB ,B 	C	'C?C C		C C%TFstrictpre_dispatchr#   r(   r)   c                   t        d       |xs i }| j                         D cg c]  \  }}|dk7  s| }}}t        | |||      t               i fd | d||       t               }j	                         D ]3  }	|	t        |	      j                  d      d   }
|j                  |
       5 t        j                  dt        |             |D ]  }t        j                  |        S c c}}w )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)event c                    t        |       v ry j                  t        |              ||K	 t        j                  j                  j                  | ||       d |<   t        j                  d|       y | j                         D ]3  \  }}|dk(  r|n| d| }j                  |d      \  }	}
 |||	|
       5 y # t        $ rC}t        |      j                  d      d   }t        j                  d||       ||<   Y d }~d }~ww xY w)	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_kwargsr)   reportr(   submod_inputstried_module_types
try_exports              r   rF   z(report_exportability.<locals>.try_exportb   s1    <--tF|,v1(##++!!- ,  '+{#5{C #113 	LLD&&1R&7d}AdV=TO)6):):*&K v]K	L 	!  ( GMM$/2	>Y '({#(s   A	C   	D	9DDz\nr   zFound %d export issues:)r   r   r&   setvaluesr8   r9   r1   r6   r:   len)r#   r
   r   r(   r)   r"   _all_submod_namesunique_issues	exceptionkeyissuerC   rD   rE   rF   s      ``       @@@@r   r   r   >   s    4 89\rF,/,=,=,?Nq42:NN3C9I4QWXM-/F# #J sBf%EM]]_ #	 y/''.q1Cc"#
 KK)3}+=> E Mo Os
   C=C=r   )loggingr   collections.abcr   typingr   r   r2   torch.exporttorch.export._tracetorch._utils_internalr   	getLogger__name__r6   __all__nnModulestrtupledictr&   boolr   r   r   r   r   <module>r_      s(     $      2 g!!
" (,	)88??)}) S/) T#s(^$	)
 
#uS#X
)^ (,U
 U	U
S/U T#s(^$U
 U U 
#x	"
"#Ur   