o
    پi                     @   sP   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 dej	j
ZdS )    N)OrderedDict)UnionOptionalListc                       sf   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dZdd Z  ZS )AttentionExtractz*attn.softmaxz
*attn_dropNevalfxforwardFmodelnamesmodemethod	hook_type	use_regexc                    s  t    |dv sJ |dkr| }n| }|dv sJ |dkrmddlm}m} |||dkr3dnd }	 p:| j |rNdd	  D fd
d	|	D }
n	 fdd	|	D }
|
satd  d|||
dd| _	d| _
nM|dv ssJ ddlm} dd	 | D } p| j |rdd	  D fdd	|D }
n	 fdd	|D }
|
std  d|| _	||
| |d| _
|
| _|| _|| _dS )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
        )trainr   r   )r   hookr   r   )get_graph_node_namesGraphExtractNet   c                 S      g | ]}t |qS  recompile.0rr   r   P/home/ubuntu/.local/lib/python3.10/site-packages/timm/utils/attention_extract.py
<listcomp>0       z-AttentionExtract.__init__.<locals>.<listcomp>c                    &   g | ] t  fd dD r qS )c                       g | ]}|  qS r   matchr   gr   r   r   1   r   8AttentionExtract.__init__.<locals>.<listcomp>.<listcomp>anyr   regexesr$   r   r   1      & c                    r    )c                       g | ]}t   |qS r   fnmatchr   nr$   r   r   r   3       r&   r'   r)   r   r$   r   r   3   r,   zNo node names found matching .T)return_dictN)r	   forward_pre)FeatureHooksc                 S   s   g | ]\}}|qS r   r   )r   r1   mr   r   r   r   >   s    c                 S   r   r   r   r   r   r   r   r   A   r   c                    r    )c                    r!   r   r"   r   r8   r   r   r   B   r   r&   r'   r)   r*   r9   r   r   B   r,   c                    r    )c                    r-   r   r.   r0   r9   r   r   r   D   r2   r&   r'   r)   r3   r9   r   r   D   r,   zNo module names found matching )default_hook_type)super__init__r   r   timm.models._features_fxr   r   default_node_namesRuntimeErrorr
   hookstimm.models._featuresr7   named_modulesdefault_module_namesr   r   r   )selfr
   r   r   r   r   r   r   r   
node_namesmatchedr7   module_names	__class__)r   r+   r   r<      sB   




zAttentionExtract.__init__c                 C   s6   | j d ur| | | j j|jd}|S | |}|S )N)device)r@   r
   
get_outputrJ   )rD   xoutputr   r   r   r	   O   s   


zAttentionExtract.forward)Nr   r   r	   F)__name__
__module____qualname__r>   rC   r   torchnnModuler   r   strboolr<   r	   __classcell__r   r   rH   r   r   	   s,    

Ar   )r/   r   collectionsr   typingr   r   r   rQ   rR   rS   r   r   r   r   r   <module>   s    