@@ -85,26 +85,28 @@ def __init__(
85
85
)
86
86
87
87
if trained_betas is not None :
88
- self .betas = np . asarray (trained_betas )
88
+ self .betas = torch . from_numpy (trained_betas )
89
89
if beta_schedule == "linear" :
90
- self .betas = np .linspace (beta_start , beta_end , num_train_timesteps , dtype = np .float32 )
90
+ self .betas = torch .linspace (beta_start , beta_end , num_train_timesteps , dtype = torch .float32 )
91
91
elif beta_schedule == "scaled_linear" :
92
92
# this schedule is very specific to the latent diffusion model.
93
93
self .betas = (
94
- np .linspace (beta_start ** 0.5 , beta_end ** 0.5 , num_train_timesteps , dtype = np .float32 ) ** 2
94
+ torch .linspace (beta_start ** 0.5 , beta_end ** 0.5 , num_train_timesteps , dtype = torch .float32 ) ** 2
95
95
)
96
96
else :
97
97
raise NotImplementedError (f"{ beta_schedule } does is not implemented for { self .__class__ } " )
98
98
99
- self .alphas = np . array ( 1.0 - self .betas , dtype = np . float32 )
100
- self .alphas_cumprod = np .cumprod (self .alphas , axis = 0 )
99
+ self .alphas = 1.0 - self .betas
100
+ self .alphas_cumprod = torch .cumprod (self .alphas , dim = 0 )
101
101
102
- sigmas = ((1 - self .alphas_cumprod ) / self .alphas_cumprod ) ** 0.5
102
+ sigmas = np .array (((1 - self .alphas_cumprod ) / self .alphas_cumprod ) ** 0.5 )
103
+ sigmas = np .concatenate ([sigmas [::- 1 ], [0.0 ]]).astype (np .float32 )
103
104
self .sigmas = torch .from_numpy (sigmas )
104
105
105
106
# setable values
106
107
self .num_inference_steps = None
107
- self .timesteps = np .arange (0 , num_train_timesteps )[::- 1 ]
108
+ timesteps = np .linspace (0 , num_train_timesteps - 1 , num_train_timesteps , dtype = float )[::- 1 ].copy ()
109
+ self .timesteps = torch .from_numpy (timesteps )
108
110
self .derivatives = []
109
111
110
112
def get_lms_coefficient (self , order , t , current_order ):
@@ -138,16 +140,13 @@ def set_timesteps(self, num_inference_steps: int):
138
140
the number of diffusion steps used when generating samples with a pre-trained model.
139
141
"""
140
142
self .num_inference_steps = num_inference_steps
141
- timesteps = np .linspace (self .config .num_train_timesteps - 1 , 0 , num_inference_steps , dtype = float )
142
143
143
- low_idx = np .floor (timesteps ).astype (int )
144
- high_idx = np .ceil (timesteps ).astype (int )
145
- frac = np .mod (timesteps , 1.0 )
144
+ timesteps = np .linspace (0 , self .config .num_train_timesteps - 1 , num_inference_steps , dtype = float )[::- 1 ].copy ()
146
145
sigmas = np .array (((1 - self .alphas_cumprod ) / self .alphas_cumprod ) ** 0.5 )
147
- sigmas = ( 1 - frac ) * sigmas [ low_idx ] + frac * sigmas [ high_idx ]
146
+ sigmas = np . interp ( timesteps , np . arange ( 0 , len ( sigmas )), sigmas )
148
147
sigmas = np .concatenate ([sigmas , [0.0 ]]).astype (np .float32 )
149
148
self .sigmas = torch .from_numpy (sigmas )
150
- self .timesteps = timesteps
149
+ self .timesteps = torch . from_numpy ( timesteps )
151
150
152
151
self .derivatives = []
153
152
0 commit comments