
    kh                     z    d dl Z d dlZd dlmZ d dlmZmZmZ d dlZ G d dej                  j                        Zy)    N)OrderedDict)UnionOptionalListc                        e Zd ZdgZdgZ	 	 	 	 	 ddeej                  j                     de	e
e      dedededef fd	Zd
 Z xZS )AttentionExtractz*attn.softmaxz
*attn_dropmodelnamesmodemethod	hook_type	use_regexc                 :   t         |           |dv sJ |dk(  r|j                         }n|j                         }|dv sJ |dk(  rddlm}m}  ||      |dk(  rdnd   }	|xs | j                  }|r]|D 
cg c]  }
t        j                  |
       }}
|	D 
cg c]-  }t        |D 
cg c]  }
|
j                  |       c}
      s,|/ }}}
n?|	D cg c]2  }t        |D cg c]  }t        j                  ||       c}      s1|4 }}}|st        d| d	       |||d
      | _        d| _        n|dv sJ ddlm} |j%                         D cg c]  \  }}|	 }}}|xs | j&                  }|r]|D 
cg c]  }
t        j                  |
       }}
|D 
cg c]-  }t        |D 
cg c]  }
|
j                  |       c}
      s,|/ }}}
n?|D cg c]2  }t        |D cg c]  }t        j                  ||       c}      s1|4 }}}|st        d| d	      || _         |||j%                         |      | _        || _        || _        || _        yc c}
w c c}
w c c}
}w c c}w c c}}w c c}}w c c}
w c c}
w c c}
}w c c}w c c}}w )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                     X/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/utils/attention_extract.pyr   zAttentionExtract.__init__   s^   & 	((((7?KKMEJJLE'''T>V-e4$'/QqQJ4T44E278Q2::a=88&0WCW8U8U4V1WW&0`CX]8^STA9N8^4_1``"%B5'#KLL(TJDJDJ  :::::*/*=*=*?@$!QA@L@6T66E278Q2::a=88&2Ycw:W!1771::W6X1YY&2bcZ_:`UV7??1a;P:`6a1bb"%DUG1#MNNDJ%gu/B/B/DXabDJ
	; 98UW8^` A 9:WY:`bs   7I!I+(I& I+I+I6#I1 I6I6I<=J J.JJJJ)JJJ&I+1I6JJc                     | j                   9| j                  |       | j                   j                  |j                        }|S | j                  |      }|S )N)device)r'   r	   
get_outputr7   )r+   xoutputs      r5   r   zAttentionExtract.forwardO   sM    ::!JJqMZZ**!((*;F  ZZ]F    )Nr   r   r   F)__name__
__module____qualname__r    r*   r   torchnnModuler   r   strboolr   r   __classcell__)r4   s   @r5   r   r   	   s    )*(>
 *.&#?)? DI&? 	?
 ? ? ?Br;   r   )r%   r!   collectionsr   typingr   r   r   r?   r@   rA   r    r;   r5   <module>rH      s-     	 # ( ( Luxx Lr;   