@@ -246,36 +246,15 @@ public override DenseTensor<float> Step(DenseTensor<float> modelOutput, int time
246246 /// <returns></returns>
247247 public override DenseTensor < float > AddNoise ( DenseTensor < float > originalSamples , DenseTensor < float > noise , IReadOnlyList < int > timesteps )
248248 {
249- //TODO: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L456
250-
251- // Make sure alphas_cumprod and timestep have the same device and dtype as originalSamples
252- var alphasCumprod = new DenseTensor < float > ( _alphasCumulativeProducts . ToArray ( ) , new int [ ] { _alphasCumulativeProducts . Count } ) ; // Convert to DenseTensor
253-
254- var sqrtAlphaProd = new DenseTensor < float > ( originalSamples . Dimensions ) ;
255- var sqrtOneMinusAlphaProd = new DenseTensor < float > ( originalSamples . Dimensions ) ;
256-
257- for ( int i = 0 ; i < timesteps . Count ; i ++ )
258- {
259- int timestep = timesteps [ i ] ;
260- float alphaProd = alphasCumprod [ timestep ] ; // Assuming alphasCumprod is a 2D tensor
261- float sqrtAlpha = ( float ) Math . Sqrt ( alphaProd ) ;
262- float sqrtOneMinusAlpha = ( float ) Math . Sqrt ( 1.0f - alphaProd ) ;
263-
264- sqrtAlphaProd . SetValue ( i , sqrtAlpha ) ;
265- sqrtOneMinusAlphaProd . SetValue ( i , sqrtOneMinusAlpha ) ;
266- }
267-
268- // Reshape sqrtAlphaProd and sqrtOneMinusAlphaProd to match the shape of originalSamples
269- sqrtAlphaProd = sqrtAlphaProd . Reshape ( originalSamples . Dimensions ) . ToDenseTensor ( ) ;
270- sqrtOneMinusAlphaProd = sqrtOneMinusAlphaProd . Reshape ( originalSamples . Dimensions ) . ToDenseTensor ( ) ;
271-
272- // Compute noisy samples
273- var noisySamples = new DenseTensor < float > ( originalSamples . Dimensions ) ;
274- for ( int i = 0 ; i < originalSamples . Length ; i ++ )
275- {
276- noisySamples . SetValue ( i , ( noise . GetValue ( i ) * sqrtOneMinusAlphaProd . GetValue ( i ) ) + ( originalSamples . GetValue ( i ) * sqrtAlphaProd . GetValue ( i ) ) ) ;
277- }
278- return noisySamples ;
249+ // Ref: https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L456
250+ int timestep = timesteps [ 0 ] ;
251+ float alphaProd = _alphasCumulativeProducts [ timestep ] ;
252+ float sqrtAlpha = ( float ) Math . Sqrt ( alphaProd ) ;
253+ float sqrtOneMinusAlpha = ( float ) Math . Sqrt ( 1.0f - alphaProd ) ;
254+
255+ return noise
256+ . MultipleTensorByFloat ( sqrtOneMinusAlpha )
257+ . AddTensors ( originalSamples . MultipleTensorByFloat ( sqrtAlpha ) ) ;
279258 }
280259
281260 /// <summary>
0 commit comments