o
    پi                     @   s  d Z ddlmZmZmZmZmZmZmZ ddl	Z	ddl	m
Z
 ddlmZmZ zddlmZ ddlmZ d	ZW n eyA   d
ZY nw ddlmZmZmZmZmZ ddlmZmZmZ ddlmZ ddl m!Z!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* g dZ+eee!eee"ee$e%e&e'e(e)e*hZ,zddlm-Z- e,.e- W n	 ey   Y nw dee
j/ fddZ0dee
j/ fddZ1dd Z2eeehZ3defddZ4defddZ5dd Z6d e
j/d!eee7 ee7 f fd"d#Zd e
j/d$eee7e7f ee7 f fd%d&ZG d'd( d(e
j/Z8G d)d* d*e
j/Z9dS )+zm PyTorch FX Based Feature Extraction Helpers
Using https://pytorch.org/vision/stable/feature_extraction.html
    )CallableDictListOptionalUnionTupleTypeN)nn   )_get_feature_info_get_return_layers)create_feature_extractor)get_graph_node_namesTF)
Conv2dSameScaledStdConv2dSame
CondConv2dStdConv2dSameFormat)resample_abs_pos_embedresample_abs_pos_embed_nhwcmaybe_add_mask)BilinearAttnTransform)MaxPool2dSameAvgPool2dSame)BatchNormAct2dSyncBatchNormActFrozenBatchNormAct2dGroupNormActGroupNorm1ActLayerNormActLayerNormAct2d)
register_notrace_moduleis_notrace_moduleget_notrace_modulesregister_notrace_functionis_notrace_functionget_notrace_functionsr   r   FeatureGraphNetGraphExtractNet)
InplaceAbnmodulec                 C      t |  | S )zq
    Any module not under timm.models.layers should get this decorator if we don't want to trace through it.
    )_leaf_modulesaddr*    r/   L/home/ubuntu/.local/lib/python3.10/site-packages/timm/models/_features_fx.pyr!   >      
r!   c                 C      | t v S N)r,   r.   r/   r/   r0   r"   F      r"   c                   C      t tS r3   )listr,   r/   r/   r/   r0   r#   J   r4   r#   funcc                 C   r+   )zF
    Decorator for functions which ought not to be traced through
    )_autowrap_functionsr-   r7   r/   r/   r0   r$   V   r1   r$   c                 C   r2   r3   )r8   r9   r/   r/   r0   r%   ^   r4   r%   c                   C   r5   r3   )r6   r8   r/   r/   r/   r0   r&   b   r4   r&   modelreturnc                 C   s   t | ttttddS )Nleaf_modulesautowrap_functionstracer_kwargs)_get_graph_node_namesr6   r,   r8   )r:   r/   r/   r0   r   f   s   r   return_nodesc                 C   s(   t sJ dt| |ttttddS )NKPlease update to PyTorch 1.10+, torchvision 0.11+ for FX feature extractionr<   r?   )has_fx_feature_extraction_create_feature_extractorr6   r,   r8   )r:   rB   r/   r/   r0   r   m   s
   r   c                       sf   e Zd ZU dZejje ed< 			dde	j
deedf d	ee d
edef
 fddZdd Z  ZS )r'   zX A FX Graph based feature extractor that works with the model feature_info metadata
    return_dictNNCHWFr:   out_indices.out_map
output_fmtc                    sj   t    tsJ dt||| _|d urt|t|ksJ t|| _t| j|}t	||| _
|| _d S )NrC   )super__init__rD   r   feature_infolenr   rJ   r   r   graph_modulerF   )selfr:   rH   rI   rJ   rF   rB   	__class__r/   r0   rL   z   s   


zFeatureGraphNet.__init__c                 C   s    |  |}| jr
|S t| S r3   )rO   rF   r6   valuesrP   xoutr/   r/   r0   forward   s   
zFeatureGraphNet.forward)NrG   F)__name__
__module____qualname____doc__torchjitFinalbool__annotations__r	   Moduler   intr   r   strrL   rW   __classcell__r/   r/   rQ   r0   r'   u   s$   
 
r'   c                	       s~   e Zd ZU dZejje ed< 		dde	j
deeeef ee f dedef fdd	Zd
eeej ejf fddZ  ZS )r(   a   A standalone feature extraction wrapper that maps dict -> list or single tensor
    NOTE:
      * one can use feature_extractor directly if dictionary output is desired
      * unlike FeatureGraphNet, this is intended to be used standalone and not with model feature_info
      metadata for builtin feature extraction mode
      * create_feature_extractor can be used directly if dictionary output is acceptable

    Args:
        model: model to extract features from
        return_nodes: node names to return features from (dict or list)
        squeeze_out: if only one output, and output in list format, flatten to single tensor
        return_dict: return as dictionary from extractor with node names as keys, ignores squeeze_out arg
    rF   TFr:   rB   squeeze_outc                    s&   t    || _t||| _|| _d S r3   )rK   rL   re   r   rO   rF   )rP   r:   rB   re   rF   rQ   r/   r0   rL      s   

zGraphExtractNet.__init__r;   c                 C   s>   |  |}| jr
|S t| }| jrt|dkr|d S |S )Nr
   r   )rO   rF   r6   rS   re   rN   rT   r/   r/   r0   rW      s
   
zGraphExtractNet.forward)TF)rX   rY   rZ   r[   r\   r]   r^   r_   r`   r	   ra   r   r   rc   r   rL   TensorrW   rd   r/   r/   rQ   r0   r(      s   
 &r(   ):r[   typingr   r   r   r   r   r   r   r\   r	   	_featuresr   r   %torchvision.models.feature_extractionr   rE   r   rA   rD   ImportErrortimm.layersr   r   r   r   r   r   r   r   timm.layers.non_local_attnr   timm.layers.pool2d_samer   r   timm.layers.norm_actr   r   r   r   r   r   r    __all__r,   r)   r-   ra   r!   r"   r#   r8   r$   r%   r&   rc   r'   r(   r/   r/   r/   r0   <module>   sb    $$

$(