
    RЦi                     z    S SK r S SKrS SKJr  S SKJrJrJr  S SKr " S S\R                  R                  5      rg)    N)OrderedDict)UnionOptionalListc                      ^  \ rS rSrS/rS/r     SS\\R                  R                     S\
\\      S\S\S\S	\4U 4S
 jjjrS rSrU =r$ )AttentionExtract	   z*attn.softmaxz
*attn_dropmodelnamesmodemethod	hook_type	use_regexc                   > [         TU ]  5         US;   d   eUS:X  a  UR                  5       nOUR                  5       nUS;   d   eUS:X  Ga  SSKJnJn  U" U5      US:X  a  SOS   n	U=(       d    U R                  nU(       al  U V
s/ s H  n
[        R                  " U
5      PM     nn
U	 VV
s/ s H6  n[        U V
s/ s H  oR                  U5      PM     sn
5      (       d  M4  UPM8     nnn
OKU	 VVs/ s H<  n[        U Vs/ s H  n[        R                  " X5      PM     sn5      (       d  M:  UPM>     nnnU(       d  [        SU S	35      eU" XS
S9U l        SU l        GO=US;   d   eSSKJn  UR%                  5        VVs/ s H  u  nnUPM
     nnnU=(       d    U R&                  nU(       al  U V
s/ s H  n
[        R                  " U
5      PM     nn
U VV
s/ s H6  n[        U V
s/ s H  oR                  U5      PM     sn
5      (       d  M4  UPM8     nnn
OLU VVs/ s H=  n[        U Vs/ s H  n[        R                  " UU5      PM     sn5      (       d  M;  UPM?     nnnU(       d  [        SU S	35      eXl        U" XR%                  5       US9U l        Xl        X0l        X@l        gs  sn
f s  sn
f s  sn
nf s  snf s  snnf s  snnf s  sn
f s  sn
f s  sn
nf s  snf s  snnf )a  Extract attention maps (or other activations) from a model by name.

Args:
    model: Instantiated model to extract from.
    names: List of concrete or wildcard names to extract. Names are nodes for fx and modules for hooks.
    mode: 'train' or 'eval' model mode.
    method: 'fx' or 'hook' extraction method.
    hook_type: 'forward' or 'forward_pre' hooks used.
    use_regex: Use regex instead of fnmatch
)trainevalr   )fxhookr   r   )get_graph_node_namesGraphExtractNet   zNo node names found matching .T)return_dictN)forwardforward_pre)FeatureHookszNo module names found matching )default_hook_type)super__init__r   r   timm.models._features_fxr   r   default_node_namesrecompileanymatchfnmatchRuntimeErrorr
   hookstimm.models._featuresr   named_modulesdefault_module_namesr   r   r   )selfr
   r   r   r   r   r   r   r   
node_namesrregexesgmatchednr   mmodule_names	__class__s                     [/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/utils/attention_extract.pyr   AttentionExtract.__init__   sm   & 	((((7?KKMEJJLE'''T>V-e4$'/QqQJ4T44E278%Q2::a=%8&0WjCW8UWW8U4V1jW&0`jCX]8^X]ST9NX]8^4_1j`"%B5'#KLL(TJDJDJ  :::::*/*=*=*?@*?$!QA*?L@6T66E278%Q2::a=%8&2Ylcw:Ww!771:w:W6X1lY&2blcZ_:`Z_UV7??1a;PZ_:`6a1lb"%DUG1#MNNJ%g/B/B/DXabDJ
	; 98UW8^` A 9:WY:`bs   	 J70K?J<
K'K6K K%
K3KK: K!K"0K

K"K"'K-6!K(
K-%K-<KKK"(K-c                     U R                   b6  U R                  U5        U R                   R                  UR                  S9nU$ U R                  U5      nU$ )N)device)r(   r
   
get_outputr9   )r,   xoutputs      r6   r   AttentionExtract.forwardO   sM    ::!JJqMZZ**!((*;F  ZZ]F    )r(   r   r   r
   r   )Nr   r   r   F)__name__
__module____qualname____firstlineno__r!   r+   r   torchnnModuler   r   strboolr   r   __static_attributes____classcell__)r5   s   @r6   r   r   	   s    )*(>
 *.&#?)? DI&? 	?
 ? ? ? ?B r>   r   )r&   r"   collectionsr   typingr   r   r   rC   rD   rE   r    r>   r6   <module>rM      s-     	 # ( ( Luxx Lr>   