
    Ng                     n    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dS )    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_dropNevalfxforwardFmodelnamesmodemethod	hook_type	use_regexc                 @   t                                                       |dv sJ |dk    r|                                }n|                                }|dv sJ |dk    rddlm}m}  ||          |dk    rdnd         }	p| j        |rd D             fd	|	D             }
nfd
|	D             }
|
st          d d           |||
d          | _	        d| _
        n|dv sJ ddlm} d |                                D             }p| j        |rd D             fd|D             }
nfd|D             }
|
st          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                 6    g | ]}t          j        |          S  recompile.0rs     X/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/utils/attention_extract.py
<listcomp>z-AttentionExtract.__init__.<locals>.<listcomp>0        888Q2:a==888    c                 L    g | ]t          fd D                        S )c                 :    g | ]}|                               S r   match)r   r   gs     r    r!   z8AttentionExtract.__init__.<locals>.<listcomp>.<listcomp>1   s#    8U8U8U8U8U8Ur#   any)r   r(   regexess    @r    r!   z-AttentionExtract.__init__.<locals>.<listcomp>1   s<    WWWC8U8U8U8UW8U8U8U4V4VW1WWWr#   c                 L    g | ]t          fd D                        S )c                 :    g | ]}t          j         |          S r   fnmatch)r   nr(   s     r    r!   z8AttentionExtract.__init__.<locals>.<listcomp>.<listcomp>3   s&    8^8^8^STA9N9N8^8^8^r#   r)   )r   r(   r   s    @r    r!   z-AttentionExtract.__init__.<locals>.<listcomp>3   s=    ```C8^8^8^8^X]8^8^8^4_4_`1```r#   zNo node names found matching .T)return_dictN)r   forward_pre)FeatureHooksc                     g | ]\  }}|S r   r   r   r0   ms      r    r!   z-AttentionExtract.__init__.<locals>.<listcomp>>   s    @@@$!QA@@@r#   c                 6    g | ]}t          j        |          S r   r   r   s     r    r!   z-AttentionExtract.__init__.<locals>.<listcomp>A   r"   r#   c                 L    g | ]t          fd D                        S )c                 :    g | ]}|                               S r   r&   )r   r   r7   s     r    r!   z8AttentionExtract.__init__.<locals>.<listcomp>.<listcomp>B   s#    :W:W:W!1771:::W:W:Wr#   r)   )r   r7   r+   s    @r    r!   z-AttentionExtract.__init__.<locals>.<listcomp>B   s<    YYYc:W:W:W:Ww:W:W:W6X6XY1YYYr#   c                 L    g | ]t          fd D                        S )c                 :    g | ]}t          j         |          S r   r.   r6   s     r    r!   z8AttentionExtract.__init__.<locals>.<listcomp>.<listcomp>D   s&    :`:`:`UV7?1a;P;P:`:`:`r#   r)   )r   r7   r   s    @r    r!   z-AttentionExtract.__init__.<locals>.<listcomp>D   s=    bbbc:`:`:`:`Z_:`:`:`6a6ab1bbbr#   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._featuresr4   named_modulesdefault_module_namesr   r   r   )selfr   r   r   r   r   r   r   r   
node_namesmatchedr4   module_namesr+   	__class__s     `          @r    r?   zAttentionExtract.__init__   sR   & 	(((((7??KKMMEEJJLLE''''T>>VVVVVVVV--e44$'//QQqQJ4T4E a88%888WWWWjWWW````j``` M"#K5#K#K#KLLL(TJJJDJDJJ  :::::::::::@@%*=*=*?*?@@@L6T6E c88%888YYYYlYYYbbbblbbb O"#MU#M#M#MNNNDJ%gu/B/B/D/DXabbbDJ
	r#   c                     | j         6|                     |           | j                             |j                  }n|                     |          }|S )N)device)rC   r   
get_outputrM   )rG   xoutputs      r    r   zAttentionExtract.forwardO   sI    :!JJqMMMZ**!(*;;FFZZ]]Fr#   )Nr	   r
   r   F)__name__
__module____qualname__rA   rF   r   torchnnModuler   r   strboolr?   r   __classcell__)rK   s   @r    r   r   	   s        )*(>
 *.&#? ?)? DI&? 	?
 ? ? ? ? ? ? ? ?B      r#   r   )r/   r   collectionsr   typingr   r   r   rT   rU   rV   r   r   r#   r    <module>r\      s     				 # # # # # # ( ( ( ( ( ( ( ( ( ( L L L L Lux L L L L Lr#   