a
    d                     @   s  d Z ddlmZmZmZmZmZ ddlZddlmZ ddl	m
Z
 zddlmZ dZW n eyj   d	ZY n0 dd
lmZmZmZmZ ddlmZ ddlmZmZ g dZeeeeeeehZzddlmZ ee W n ey   Y n0 eej dddZeej dddZ dd Z!e" Z#edddZ$edddZ%dd Z&ejeee'e'f ee' f dddZG d d! d!ejZ(G d"d# d#ejZ)dS )$zm PyTorch FX Based Feature Extraction Helpers
Using https://pytorch.org/vision/stable/feature_extraction.html
    )CallableListDictUnionTypeN)nn   )_get_feature_info)create_feature_extractorTF)
Conv2dSameScaledStdConv2dSame
CondConv2dStdConv2dSame)BilinearAttnTransform)MaxPool2dSameAvgPool2dSame)	register_notrace_moduleis_notrace_moduleget_notrace_modulesregister_notrace_functionis_notrace_functionget_notrace_functionsr
   FeatureGraphNetGraphExtractNet)
InplaceAbnmodulec                 C   s   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addr    r   a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/timm/models/_features_fx.pyr   +   s    
r   c                 C   s   | t v S N)r   r   r   r   r    r   3   s    r   c                   C   s   t tS r!   )listr   r   r   r   r    r   7   s    r   funcc                 C   s   t |  | S )zF
    Decorator for functions which ought not to be traced through
    )_autowrap_functionsr   r#   r   r   r    r   ?   s    
r   c                 C   s   | t v S r!   )r%   r#   r   r   r    r   G   s    r   c                   C   s   t tS r!   )r"   r%   r   r   r   r    r   K   s    r   model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 extraction)Zleaf_modulesZautowrap_functions)Ztracer_kwargs)has_fx_feature_extraction_create_feature_extractorr"   r   r%   r&   r   r   r    r
   O   s
    r
   c                       s*   e Zd ZdZd 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
    Nc                    sh   t    tsJ dt| | _d ur>tt ks>J  fddt| jD }t||| _d S )Nr)   c                    s6   i | ].\}}| v r|d  dur*| n|d  qS )r   Nr   ).0iinfoout_indicesout_mapr   r    
<dictcomp>`   s   z,FeatureGraphNet.__init__.<locals>.<dictcomp>)	super__init__r*   r	   Zfeature_infolen	enumerater
   graph_module)selfr'   r0   r1   r(   	__class__r/   r    r4   Z   s    
zFeatureGraphNet.__init__c                 C   s   t | | S r!   )r"   r7   values)r8   xr   r   r    forwarde   s    zFeatureGraphNet.forward)N)__name__
__module____qualname____doc__r4   r=   __classcell__r   r   r9   r    r   W   s   r   c                       s\   e Zd ZdZd	eeeef ee f ed fddZ	eee
j e
jf d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
    T)r(   squeeze_outc                    s    t    || _t||| _d S r!   )r3   r4   rC   r
   r7   )r8   r'   r(   rC   r9   r   r    r4   v   s    
zGraphExtractNet.__init__)returnc                 C   s0   t | | }| jr,t|dkr,|d S |S )Nr   r   )r"   r7   r;   rC   r5   )r8   r<   outr   r   r    r=   {   s    zGraphExtractNet.forward)T)r>   r?   r@   rA   r   r   strr   boolr4   torchZTensorr=   rB   r   r   r9   r    r   i   s   *r   )*rA   typingr   r   r   r   r   rH   r   Z	_featuresr	   Z%torchvision.models.feature_extractionr
   r+   r*   ImportErrorZtimm.layersr   r   r   r   Ztimm.layers.non_local_attnr   Ztimm.layers.pool2d_samer   r   __all__r   r   r   Moduler   r   r   setr%   r   r   r   rF   r   r   r   r   r   r    <module>   s@   

&