
    kh                        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 	 ddlmZ ddlmZ d	Z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,	 ddlm-Z- e,j]                  e-       dee
j^                     fdZ0dee
j^                     fdZ1d Z2eeehZ3defdZ4defdZ5d Z6de
j^                  deee7   ee7   f   fdZde
j^                  deee7e7f   ee7   f   fdZ G d d e
j^                        Z8 G d! d"e
j^                        Z9y# e$ r d
ZY w xY w# e$ r Y w xY w)#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                 0    t         j                  |        | 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+   s    T/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/models/_features_fx.pyr"   r"   >   s     fM    c                     | t         v S N)r-   r/   s    r0   r#   r#   F   s    ]""r1   c                       t        t              S r3   )listr-    r1   r0   r$   r$   J   s    r1   funcc                 0    t         j                  |        | S )zF
    Decorator for functions which ought not to be traced through
    )_autowrap_functionsr.   r7   s    r0   r%   r%   V   s     D!Kr1   c                     | t         v S r3   )r9   r:   s    r0   r&   r&   ^   s    &&&r1   c                       t        t              S r3   )r5   r9   r6   r1   r0   r'   r'   b   s    #$$r1   modelreturnc                 V    t        | t        t              t        t              d      S )Nleaf_modulesautowrap_functionstracer_kwargs)_get_graph_node_namesr5   r-   r9   )r=   s    r0   r   r   f   s&     '+M':RVWjRkl r1   return_nodesc                 r    t         sJ d       t        | |t        t              t        t              d      S )NKPlease update to PyTorch 1.10+, torchvision 0.11+ for FX feature extractionr@   rC   )has_fx_feature_extraction_create_feature_extractorr5   r-   r9   )r=   rF   s     r0   r   r   m   s7    $s&ss$$|'+M':RVWjRkl r1   c                        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Zd	 Z xZS )r(   zX A FX Graph based feature extractor that works with the model feature_info metadata
    return_dictr=   out_indices.out_map
output_fmtc                    t         |           t        sJ d       t        ||      | _        |t        |      t        |      k(  sJ t        |      | _        t        | j                  |      }t        ||      | _
        || _        y )NrH   )super__init__rI   r   feature_infolenr   rO   r   r   graph_modulerL   )selfr=   rM   rN   rO   rL   rF   	__class__s          r0   rR   zFeatureGraphNet.__init__z   s     	(w*ww(-e[Aw<3{#3333 ,)$*;*;WE4ULI&r1   c                 r    | j                  |      }| j                  r|S t        |j                               S r3   )rU   rL   r5   valuesrV   xouts      r0   forwardzFeatureGraphNet.forward   s1    "JCJJL!!r1   )NNCHWF)__name__
__module____qualname____doc__torchjitFinalbool__annotations__r
   Moduler   intr   r   strrR   r]   __classcell__rW   s   @r0   r(   r(   u   sp    && '+$ %'99' sCx' d^	'
 ' '$"r1   r(   c            	            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Zdeeej                      ej                   f   fdZ x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
    rL   r=   rF   squeeze_outc                 `    t         |           || _        t        ||      | _        || _        y r3   )rQ   rR   rn   r   rU   rL   )rV   r=   rF   rn   rL   rW   s        r0   rR   zGraphExtractNet.__init__   s/     	&4ULI&r1   r>   c                     | j                  |      }| j                  r|S t        |j                               }| j                  rt        |      dk(  r|d   S |S )Nr   r   )rU   rL   r5   rY   rn   rT   rZ   s      r0   r]   zGraphExtractNet.forward   sQ    "J3::< ))c#h!ms1vDDr1   )TF)r_   r`   ra   rb   rc   rd   re   rf   rg   r
   rh   r   r   rj   r   rR   Tensorr]   rk   rl   s   @r0   r)   r)      s     && !% %
'99
'  S#XS	 9:
' 	
'
 
'EE$u||"4ell"BC Er1   r)   ):rb   typingr   r   r   r   r   r   r	   rc   r
   	_featuresr   r   %torchvision.models.feature_extractionr   rJ   r   rE   rI   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.   rh   r"   r#   r$   r9   r%   r&   r'   rj   r(   r)   r6   r1   r0   <module>r{      s   F E E   <&kc $
 [ Z [ [ < @  e 2M=	&j!
DO #d299o #  H 'h '%		 eDItCy4H.I BII U4S>SWX[S\C\=] "bii "<!Ebii !EG  & %&T  		s#   D> E >E	E	EE