U vb@sZddlZddlZddlmZmZddZGdddeZGdd d eZ Gd d d Z dS) N)GaussianDiffusionMineGaussianDiffusioncCsNt|tr|drpt|tdd}td|D],}ttd|||kr2ttd||Sq2td|ddd|d D}|t|}|t|}d}g}t |D]\}}|||krdnd} | |krtd | d ||dkrd} n| d|d} d } g} t|D] } | |t | | | 7} q|| 7}|| 7}qt|S) aT Create a list of timesteps to use from an original diffusion process, given the number of timesteps we want to take from equally-sized portions of the original process. For example, if there's 300 timesteps and the section counts are [10,15,20] then the first 100 timesteps are strided to be 10 timesteps, the second 100 are strided to be 15 timesteps, and the final 100 are strided to be 20. If the stride is a string starting with "ddim", then the fixed striding from the DDIM paper is used, and only one section is allowed. :param num_timesteps: the number of diffusion steps in the original process to divide up. :param section_counts: either a list of numbers, or a string containing comma-separated numbers, indicating the step count per section. As a special case, use "ddimN" where N is a number of steps to use the striding from the DDIM paper. :return: a set of diffusion steps from the original process to use. ZddimNrrzcannot create exactly z steps with an integer stridecSsg|] }t|qS)int).0xrr2/home/zsyue/code/python/GradDiff/models/respace.py &sz#space_timesteps..,zcannot divide section of z steps into g) isinstancestr startswithrlenrangeset ValueErrorsplit enumerateappendround) num_timestepsZsection_countsZ desired_countiZsize_perextra start_idxZ all_stepsZ section_countsizeZ frac_stridecur_idxZ taken_steps_rrr space_timestepss>        rcsHeZdZdZfddZfddZfddZdd Zd d ZZ S) SpacedDiffusionMine# A diffusion process which can skip steps in a base diffusion process. :param use_timesteps: a collection (sequence or set) of timesteps from the original diffusion process to retain. :param kwargs: the kwargs to create the base diffusion process. c st||_g|_t|d|_tf|}d}g}t|jD]4\}}||jkr:|d|||}|j|q:t ||d<t j f|dSNbetasg?r) r use_timesteps timestep_maproriginal_num_stepsrralphas_cumprodrnparraysuper__init__selfr#kwargsbase_diffusionZlast_alpha_cumprodZ new_betasrZ alpha_cumprod __class__rr r*Hs   zSpacedDiffusionMine.__init__cstj||f||SNr)p_mean_variance _wrap_modelr,modelargsr-r/rr r3Xsz#SpacedDiffusionMine.p_mean_variancecstj||f||Sr1r)training_lossesr4r5r/rr r9[sz#SpacedDiffusionMine.training_lossescCs"t|tr|St||j|j|jSr1r _WrappedModelr$rescale_timestepsr%r,r6rrr r4^s zSpacedDiffusionMine._wrap_modelcCs|Sr1rr,trrr _scale_timestepsesz$SpacedDiffusionMine._scale_timesteps __name__ __module__ __qualname____doc__r*r3r9r4r@ __classcell__rrr/r r?s    rcsHeZdZdZfddZfddZfddZdd Zd d ZZ S) SpacedDiffusionr c st||_g|_t|d|_tf|}d}g}t|jD]4\}}||jkr:|d|||}|j|q:t ||d<t j f|dSr!) rr#r$rr%rrr&rr'r(r)r*r+r/rr r*rs   zSpacedDiffusion.__init__cstj||f||Sr1r2r5r/rr r3szSpacedDiffusion.p_mean_variancecstj||f||Sr1r8r5r/rr r9szSpacedDiffusion.training_lossescCs"t|tr|St||j|j|jSr1r:r=rrr r4s zSpacedDiffusion._wrap_modelcCs|Sr1rr>rrr r@sz SpacedDiffusion._scale_timestepsrArrr/r rGis    rGc@seZdZddZddZdS)r;cCs||_||_||_||_dSr1)r6r$r<r%)r,r6r$r<r%rrr r*sz_WrappedModel.__init__cKsFtj|j|j|jd}||}|jr6|d|j}|j||f|S)N)devicedtypeg@@) thtensorr$rHrIr<floatr%r6)r,rtsr-Z map_tensorZnew_tsrrr __call__s z_WrappedModel.__call__N)rBrCrDr*rNrrrr r;sr;) numpyr'torchrJgaussian_diffusionrrrrrGr;rrrr s 8*+