o ) iÈã@sdddlmZddlZddlmZddlmZmZddl Z ddl m Z dgZ dd„Z dddd„ZdS)é)Ú annotationsN)Úrepeat)ÚListÚUnionÚbuild_sincos_position_embeddingcs‡fdd„}|S)Ncs.t|tjjƒrt|tƒst|ƒStt|ˆƒƒS)N)Ú isinstanceÚ collectionsÚabcÚIterableÚstrÚtupler)Úx©Ún©úg/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/blocks/pos_embed_utils.pyÚparsesz_ntuple..parser)rrrrrÚ_ntuples réçˆÃ@Ú grid_sizeúUnion[int, List[int]]Ú embed_dimÚintÚ spatial_dimsÚ temperatureÚfloatÚreturnútorch.nn.Parameterc CsD|dkr{tdƒ}||ƒ}|\}}tj|tjd}tj|tjd} t || ¡\}} |ddkr2tdƒ‚|d} tj| tjd| } d|| } t d| ¡| g¡} t d|  ¡| g¡} tjt  | ¡t  | ¡t  | ¡t  | ¡gdd d d d …d d …f}n|d krtd ƒ}||ƒ}|\}}}tj|tjd}tj|tjd} tj|tjd}t || |¡\}} }|d dkr¹td ƒ‚|d } tj| tjd| } d|| } t d| ¡| g¡} t d|  ¡| g¡} t d| ¡| g¡}tjt  | ¡t  | ¡t  | ¡t  | ¡t  |¡t  |¡gdd d d d …d d …f}nt dƒ‚t   |¡}d|_|S)a Builds a sin-cos position embedding based on the given grid size, embed dimension, spatial dimensions, and temperature. Reference: https://github.com/cvlab-stonybrook/SelfMedMAE/blob/68d191dfcc1c7d0145db93a6a570362de29e3b30/lib/models/mae3d.py Args: grid_size (List[int]): The size of the grid in each spatial dimension. embed_dim (int): The dimension of the embedding. spatial_dims (int): The number of spatial dimensions (2 for 2D, 3 for 3D). temperature (float): The temperature for the sin-cos position embedding. Returns: pos_embed (nn.Parameter): The sin-cos position embedding as a fixed parameter. é)ÚdtypeérzHEmbed dimension must be divisible by 4 for 2D sin-cos position embeddinggð?zm,d->mdé)ÚdimNrézHEmbed dimension must be divisible by 6 for 3D sin-cos position embeddingz6Spatial Dimension Size {spatial_dims} Not Implemented!F)rÚtorchÚarangeÚfloat32ÚmeshgridÚAssertionErrorÚeinsumÚflattenÚcatÚsinÚcosÚNotImplementedErrorÚnnÚ ParameterÚ requires_grad)rrrrZ to_2tupleZ grid_size_tÚhÚwZgrid_hZgrid_wZpos_dimÚomegaÚout_hÚout_wZpos_embZ to_3tupleÚdZgrid_dÚout_dZ pos_embedrrrr#s^  B    ú÷ ö )rr) rrrrrrrrrr)Ú __future__rÚcollections.abcrÚ itertoolsrÚtypingrrr%Útorch.nnr0Ú__all__rrrrrrÚs    ÿ