L i:E dZddlZddlZddlmZddlmZmZmZerddl m Z GddejZ d eee d eeed eee fd Zd eee d eeeeeed ffd ee fdZd eee d eeeeeefd ee deee eeffdZGddZGddZdZdededee deeedfdeef dZy)zACollection of utils to be used by backbones and their components.N)Iterable) TYPE_CHECKINGOptionalUnion)PretrainedConfigceZdZdZdZy) BackboneTypetimm transformersN)__name__ __module__ __qualname__TIMM TRANSFORMERSg/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/utils/backbone_utils.pyr r s D!Lrr out_features out_indices stage_namesc  td|t|tfstdt|t fd|Drtdd|t |t t |k7rtd||Dcgc] }||vs| c}x}k7rtd|d||t|tstd t|tfd |D}t fd |Drtd d |t |t t |k7r d|}|||k7rd|dndz }t||tt|k7r;tt||dDcgc]\}}| } }}td| d||G|Dt |t |k7r td||Dcgc]}| c}k7r tdyyycc}wcc}}wcc}w)zW Verify that out_indices and out_features are valid for the given stage_names. Nz2Stage_names must be set for transformers backbonesz out_features must be a list got c3&K|]}|v ywNr).0featrs r z2verify_out_features_out_indices..,s@4t;&@sz.out_features must be a subset of stage_names: z got z2out_features must not contain any duplicates, got z@out_features must be in the same order as stage_names, expected z out_indices must be a list, got c3HK|]}|dkr|tzn|yw)rN)lenridxrs rrz2verify_out_features_out_indices..9s' cPS37s;'7!7!K cs"c3PK|]}|ttvs|ywr)rangerr s rrz2verify_out_features_out_indices..:s"Us#U3{CS=T2TsUs&&z2out_indices must be valid indices for stage_names z, got z1out_indices must not contain any duplicates, got z(equivalent to z))c |dS)Nrr)xs rz1verify_out_features_out_indices..As jklmjnr)keyz?out_indices must be in the same order as stage_names, expected zHout_features and out_indices should have the same length if both are setzQout_features and out_indices should correspond to the same stages if both are set) ValueError isinstancelisttypeanyrsettuplesortedzip) rrrr sorted_featspositive_indicesmsg_r!sorted_negatives ` rverify_out_features_out_indicesr7 s_ MNN,0?\@R?STU U @<@ @Mk]Z_`l_mno o | C $5 6 6QR^Q_`a a k,bdTUaMaT,bbL cRS_R``efrest +t,?[@Q?RST T cWb cc U.U UQR]Q^^depdqrs s  C,<(=$> >Ek]SC =MQ\=\_%5$6b9bd dCS/ ! uV,<%=> >17${#DK#;dop&K!{ '#O^$njuv N **rc>eZdZUdZeeed<dZeed<ddZ ddZ ddZ e dZ e jd eefd Z e d Zejd eeeeeffd Ze dZe dZdZ ddeedeedeefdZfdZxZS) BackboneMixinN backbone_typeThas_attentionsct|dd td|jjjDcgc]}|d c}|_|jjjDcgc]}|d c}|_t|jjj}|jjj}t|||j ||c|_ |_ ycc}wcc}w)zo Initialize the backbone model from timm The backbone must already be loaded to self._backbone _backboneNz=self._backbone must be set before calling _init_timm_backbonemodulenum_chsr@) getattrr)rI feature_infoinfor num_featuresr+r module_namer7 _out_features _out_indices)selfconfigstagerrs r_init_timm_backbonez!BackboneMixin._init_timm_backbones 4d + 3\] ] :>9T9T9Y9YZE(OZ;?>>;V;V;[;[\%U9-\4>>66BBC ~~22>>@  (%;DL\L\ 1=k-D-[\s C:1 C?ct|d}t|dd}t|dd}||_t|||\|_|_d|_y)Nrrrr@)rLrrCrQrRrO)rSrTrrrs r_init_transformers_backbonez)BackboneMixin._init_transformers_backbonesXfm4 v~t< fmT: &0Z%;K1 -D-!rc||_t|dd|_|jrtjntj |_|j tjk(r|j|y|j tj k(r|j|ytd|j d)z Method to initialize the backbone. This method is called by the constructor of the base class after the pretrained model weights have been loaded. use_timm_backboneFzbackbone_type z not supported.N) rTrLrZr rrrFrVrXr))rSrTs r_init_backbonezBackboneMixin._init_backbones  !(1De!L262H2H\..lNgNg   !2!2 2  $ $V ,   <#<#< <  , ,V 4~d.@.@-AQR Rrc|jSrrQrSs rrzBackboneMixin.out_features!!!rrcNt|d|j\|_|_yz Set the out_features attribute. This will also update the out_indices attribute to match the new out_features. Nr@rCrrQrRrSrs rrzBackboneMixin.out_features' 1[%4TEUEU1 -D-rc|jSrrRr^s rrzBackboneMixin.out_indices   rrcNtd||j\|_|_yz Set the out_indices attribute. This will also update the out_features attribute to match the new out_indices. Nr@rbrSrs rrzBackboneMixin.out_indices' 1[;DDTDT1 -D-rczt|jDcic]\}}||j|c}}Scc}}wr) enumeraterrO)rSirUs rout_feature_channelsz"BackboneMixin.out_feature_channelss8=FdFVFV :} r)r>N)NNN)r rrrFrr __annotations__rGboolrVrXr[propertyrsetterr+strrrr/introrrrryr __classcell__rs@rrErEs(,0M8L)0 NDJ0 !S""" c  !! uU3Zc-B'C  YY OO%04,0&* ]'tn]$D> ] d^ ]rrEceZdZdZedZej deefdZedZ e j de e e dfee ffdZ fd Z xZS) BackboneConfigMixinzv A Mixin to support handling the `out_features` and `out_indices` attributes for the backbone configurations. c|jSrr]r^s rrz BackboneConfigMixin.out_features r_rrcNt|d|j\|_|_yrarbrcs rrz BackboneConfigMixin.out_featuresrdrc|jSrrfr^s rrzBackboneConfigMixin.out_indicesrgrr.cNtd||j\|_|_yrirbrjs rrzBackboneConfigMixin.out_indicesrkrctt|}|jd|d<|jd|d<|Srrrs rrzBackboneConfigMixin.to_dict'rr)r rr__doc__rrrr+rrrr/rrrrs@rrrs"" c  !! uU38_d3i-G'H  rrcddlm}m}t|dd}t|dd}t|dd}t|dd}t|dd}|in|}|r | t d || | t d ||||j dd |i|S|r%| t d |j |f||d |}|S|r"| t d|j |fi|}|S| | t d||j |fi|}|j |}|S)a> Loads the backbone model from a config object. If the config is from the backbone model itself, then we return a backbone model with randomly initialized weights. If the config is from the parent model of the backbone model itself, then we load the pretrained backbone weights if specified. r) AutoBackbone AutoConfigbackbone_configNrZuse_pretrained_backbonebackbonebackbone_kwargs?You can't specify both `backbone_kwargs` and `backbone_config`.z>Cannot specify both config.backbone_config and config.backbonerTz8config.backbone must be set if use_timm_backbone is True)rZrz>config.backbone must be set if use_pretrained_backbone is TruezO(;TB%f.GN!&*d;f&7>O+3bO?6Z[["':'FKbKnYZZ#4#rsH $116"499" *r8C=)*r8@#8O*r^fgopsgt^u*rZ%49%%%S 5c? :;<%c%>+49%+%S 5: 567+c+ 49d3i  +@yyx''T5p\\!\sm\eD*<$<=> \ d^ \r