o f/@s@ddlZddlZddlZddlmZddlZddlmZddl Z ddZ d:ddZ d;d d Z d dZ dddZd d!Zd"d#Zd$d%Zd&d'ZGd(d)d)ejZGd*d+d+ejZd,d-Zd.d/Z d0d1Z!Gd2d3d3ejZ"d:d4d5Z#d6d7Z$d8d9Z%dS)?N)repeatcCsHd|vr|dkr dS|dkrdStdt|ddi|dtS)Ntarget__is_first_stage____is_unconditional__z%Expected key `target` to instantiate.params)KeyErrorget_obj_from_strgetdict)configrrL/home/gustavo/Documentos/CRM/imagedream/ldm/modules/diffusionmodules/util.pyinstantiate_from_configs rFcCs<|dd\}}|rt|}t|ttj|dd|S)N.)package)rsplit importlib import_modulereloadgetattr)stringrmodulecls module_imprrr r s   r -C6?{Gz?Mb?cCs|dkrtj|d|d|tjdd}| S|dkr]tj|dtjd||}|d|tjd}t|d}||d}d|dd|dd}tj|dd d }| S|d krotj|||tjd}| S|d krtj|||tjdd}| St d |d)Nlinearg?)dtypecosinerr+?)a_mina_max sqrt_linearsqrtz schedule 'z ' unknown.) torchlinspacefloat64arangenppicospowclip ValueErrornumpy)schedule n_timestep linear_start linear_endcosine_sbetas timestepsalphasrrr make_beta_schedule&s:    r;cCst|tjs t|n|}d|}|d}|}|d}|d}||8}||||9}|d}|dd|dd}t|dd|g}d|}|S)Nrrr"r ) isinstancer(Tensortensorcumprodr'clonecat)r8r: alphas_baralphas_bar_sqrtalphas_bar_sqrt_0alphas_bar_sqrt_Trrr enforce_zero_terminal_snrHs   rFTcCs|dkr||}tttd||}n|dkr*tdt|d|dt}ntd|d|d}|r?t d ||S) Nuniformrquadg?r z/There is no ddim discretization method called ""rz%Selected timesteps for ddim sampler: ) r,asarraylistranger)r'astypeintNotImplementedErrorprint)ddim_discr_methodnum_ddim_timestepsnum_ddpm_timestepsverbosecddim_timesteps steps_outrrr make_ddim_timesteps]s rXcCs||}t|dg||dd}|td|d|d||}|r?td|d|td|d||||fS)Nrr"rz'Selected alphas for ddim sampler: a_t: z ; a_(t-1): z&For the chosen value of eta, which is zB, this results in the following sigma_t schedule for ddim sampler )r,rJtolistr'rP) alphacumsrVetarTr: alphas_prevsigmasrrr make_ddim_sampling_parametersts$ r^r#cCsPg}t|D]}||}|d|}|td|||||qt|S)a# Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of (1-beta) over time from t = [0,1]. :param num_diffusion_timesteps: the number of betas to produce. :param alpha_bar: a lambda that takes an argument t from 0 to 1 and produces the cumulative product of (1-beta) up to that part of the diffusion process. :param max_beta: the maximum beta to use; use values lower than 1 to prevent singularities. r)rLappendminr,array)num_diffusion_timesteps alpha_barmax_betar8it1t2rrr betas_for_alpha_bars   " rhcCs4|j^}}|d|}|j|gdt|dRS)Nr"rr)shapegatherreshapelen)atx_shapeb_outrrr extract_into_tensors  rtcCs4|rt|t|}tj|t|g|RS||S)a Evaluate a function without caching intermediate activations, allowing for reduced memory at the expense of extra compute in the backward pass. :param func: the function to evaluate. :param inputs: the argument sequence to pass to `func`. :param params: a sequence of parameters `func` depends on but does not explicitly take as arguments. :param flag: if False, disable gradient checkpointing. )tupleCheckpointFunctionapplyrm)funcinputsrflagargsrrr checkpoints r|c@s$eZdZeddZeddZdS)rvcGsd||_t|d||_t||d|_t|j|j}Wd|S1s+wY|SN) run_functionrK input_tensors input_paramsr(no_grad)ctxr~lengthr{output_tensorsrrr forwards  zCheckpointFunction.forwardcGsdd|jD|_tdd|jD}|j|}Wdn1s%wYtjj||j|j|dd}|`|`~d|S)NcSsg|] }|dqST)detachrequires_grad_.0xrrr sz/CheckpointFunction.backward..cSsg|]}||qSr)view_asrrrr rsT) allow_unused)NN)rr( enable_gradr~autogradgradr)r output_gradsshallow_copiesr input_gradsrrr backwards   zCheckpointFunction.backwardN)__name__ __module__ __qualname__ staticmethodrrrrrr rvs   rv'cCs|sV|d}tt| tjd|tjd|j|jd}|dddf|d}tj t |t |gdd}|drTtj |t |ddddfgdd}|St |d |d }|S) aX Create sinusoidal timestep embeddings. :param timesteps: a 1-D Tensor of N indices, one per batch element. These may be fractional. :param dim: the dimension of the output. :param max_period: controls the minimum frequency of the embeddings. :return: an [N x dim] Tensor of positional embeddings. r r)startendrdeviceNr"dimrzb -> b d)d)r(expmathlogr+float32torfloatrAr.sin zeros_liker)r9r max_period repeat_onlyhalffreqsr{ embeddingrrr timestep_embeddings(  rcCs|D]}|q|S)z< Zero out the parameters of a module and return it. ) parametersrzero_)rprrr zero_modules rcCs |D] }||q|S)z9 Scale the parameters of a module and return it. )rrmul_)rscalerrrr scale_modules rcCs|jttdt|jdS)z6 Take the mean over all non-batch dimensions. rr)meanrKrLrmrj)r>rrr mean_flatsrcCs td|S)z Make a standard normalization layer. :param channels: number of input channels. :return: an nn.Module for normalization. ) GroupNorm32)channelsrrr normalizations rc@seZdZddZdS)SiLUcCs|t|Sr})r(sigmoidselfrrrr rsz SiLU.forwardN)rrrrrrrr rs rcseZdZfddZZS)rcst||jSr})superrrtyperr __class__rr rszGroupNorm32.forward)rrrr __classcell__rrrr rsrcOV|dkr tj|i|S|dkrtj|i|S|dkr$tj|i|Std|)z4 Create a 1D, 2D, or 3D convolution module. rr unsupported dimensions: )nnConv1dConv2dConv3dr1dimsr{kwargsrrr conv_ndrcOstj|i|S)z! Create a linear module. )rLinear)r{rrrr r(srcOr)z8 Create a 1D, 2D, or 3D average pooling module. rr rr)r AvgPool1d AvgPool2d AvgPool3dr1rrrr avg_pool_nd/rrcs$eZdZfddZddZZS)HybridConditionercs"tt||_t||_dSr})r__init__rconcat_conditionercrossattn_conditioner)rc_concat_configc_crossattn_configrrr r=  zHybridConditioner.__init__cCs"||}||}|g|gdS)N)c_concat c_crossattn)rr)rrrrrr rBrzHybridConditioner.forward)rrrrrrrrrr r<s rcs,fdd}fdd}|r|S|S)Ncs<tjdgddRdjdgdtdRS)Nrrrri)r(randnrrmrrrjrr Is znoise_like..cstjdS)Nr)r(rrrrr rLsr)rjrr repeat_noisenoiserrr noise_likeHsrcCLt|tjtjtjfr"|jj|j_|jdur$|jj|j_dSdSdS)z3 Convert primitive modules to float16. N) r<rrrrweightdatarbiaslrrr convert_module_to_f16Q  rcCr)zP Convert primitive modules to float32, undoing convert_module_to_f16(). N) r<rrrrrrrrrrrr convert_module_to_f32Zrr)F)rrrr)r#)rF)&osrr(torch.nnrr2r,einopsrrrr r;rFrXr^rhrtr|rFunctionrvrrrrrModuler GroupNormrrrrrrrrrrrr sB     "