o >g@sddlZddlmZmZddlZddejdefddZddZ dd ejd e d e d e de de de deeeffddZ ddZ GdddZ de iZdS)N)TupleCallablexmodecC|SN)rrrrC/home/comdoleger1/zen-flux-style-shape/comfy_extras/nodes_tomesd.py do_nothingsr cCsL|jddkrt|d|dkr|dn||ddSt|||S)Nr)shapetorchgather unsqueezesqueeze)inputdimindexrrr mps_gather_workaround srFmetricwhsxsyrno_randreturnc s|j\}dks|dks|dkrttfS|jjdkrtntjt ||||}} |r?tj|| d|jtj d} ntj |||| df|jd} tj|| |||jtj d} | j d| tj | | j d d| || ||dd||| |} |||ks| ||krtj|||jtj d} | | d ||d | |f<n| } | dd djdd } ~ ~ || | d d d d d f| d d d d d ffd d ||jd dd}|\} }| |d d}t| jd|jd d \}}|jd ddd}|dd d d f |dd d d f |dd dWd n 1s6wYddtjdtjf fdd }dtjdtjf f dd }||fS)a Partitions the tokens into src and dst and merges r tokens from src to dst. Dst tokens are partitioned by choosing one randomy in each (sx, sy) region. Args: - metric [B, N, C]: metric to use for similarity - w: image width in tokens - h: image height in tokens - sx: stride in the x dimension for dst, must divide w - sy: stride in the y dimension for dst, must divide h - r: number of tokens to remove (by merging) - no_rand: if true, disable randomness (use top left corner only) rr mpsdevicedtype)sizer )r!rrsrcNr rcsF|jd}|d|d}|d|d}||fS)Nr r rr)r expand)rCr%dst)BNa_idxb_idxrnum_dstrr splitMs z/bipartite_soft_matching_random2d..splitT)rkeepdim)r descending).N.r'meanrrcsz|\}}|j\}}}|d|||d}|d||d}|jd||||d}tj||gddS)Nr2r')reducer r&)r r(scatter_reducercat)rrr%r*nt1cunm)dst_idxrrr0src_idxunm_idxrr mergecs  z/bipartite_soft_matching_random2d..mergecs jd}|dd|ddf|d|dddf}}|j\}}}|d|d}tj||j|jd}|jd||d|jdjddd d|||d|jdjdddd||d|S)Nr .r2r'rr$)r r(rzerosr r!scatter_)rZunm_lenr;r*_r:r%out) r+r,r-r.r<rr/rr=r>rr unmergems . 44z1bipartite_soft_matching_random2d..unmerge)r4)r r r typerrrno_gradr@int64randintrA ones_liker!view transposereshapeargsortnormminmaxTensor)rrrrrrrrBZhsyZwsxZrand_idxZidx_buffer_viewZ idx_bufferabscoresZnode_maxnode_idxZedge_idxr?rDr) r+r,r-r.r<rr/rr0r=r>r bipartite_soft_matching_random2dsF  ( &7, rVcCs|\}}}}||}ttt||jd}d} d} d} || krPtt||} tt||} t|jd|}d}t|| | | | ||\}}||fSdd}||fS)Nr r#FcSrrr)yrrr szget_functions..)intmathceilsqrtr rV)rratiooriginal_shaperSr:Z original_hZ original_woriginal_tokens downsampleZstride_xZstride_yZmax_downsamplerrrrmunothingrrr get_functionss rdc@s,eZdZeddZdZdZdZddZdS) TomePatchModelcCsddddddddfd iS) NrequiredMODELFLOATg333333?gg?g{Gz?)defaultrOrPstep)modelr]r)srrr INPUT_TYPESszTomePatchModel.INPUT_TYPESrgpatchzmodel_patches/unetcsBd_fdd}fdd}|}|||||fS)Ncs$t||d\}_||||fS)Nr^)rdrb)qkv extra_optionsrar]selfrr tomesd_msz&TomePatchModel.patch..tomesd_mcs |Sr)rb)r8rs)rurr tomesd_us z&TomePatchModel.patch..tomesd_u)rbcloneset_model_attn1_patchset_model_attn1_output_patch)rurlr]rvrwrarrtr ros   zTomePatchModel.patchN) __name__ __module__ __qualname__ classmethodrn RETURN_TYPESFUNCTIONCATEGORYrorrrr res  rer)F)rtypingrrrZrQstrr rrYboolrVrdreNODE_CLASS_MAPPINGSrrrr s2   i