Guest User

Untitled

a guest
Sep 1st, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. def get_noise_noisy_latents_and_timesteps(
  2. args, noise_scheduler, latents: torch.FloatTensor
  3. ):
  4. """
  5. Генерация фиксированного шума на основе содержимого latents.
  6. """
  7.  
  8. if args.fixed_noise:
  9. # Переводим в float32 перед numpy()
  10. latents_bytes = latents.detach().to(torch.float32).cpu().numpy().tobytes()
  11. hash_int = int(hashlib.sha256(latents_bytes).hexdigest(), 16) % (2**32)
  12.  
  13. g = torch.Generator(device=latents.device).manual_seed(hash_int)
  14. noise = torch.randn(latents.shape, generator=g, device=latents.device)
  15. else:
  16. noise = torch.randn_like(latents)
  17.  
  18. if args.noise_offset:
  19. if args.noise_offset_random_strength:
  20. noise_offset = torch.rand(1, device=latents.device) * args.noise_offset
  21. else:
  22. noise_offset = args.noise_offset
  23. noise = custom_train_functions.apply_noise_offset(latents, noise, noise_offset, args.adaptive_noise_scale)
  24. if args.multires_noise_iterations:
  25. noise = custom_train_functions.pyramid_noise_like(
  26. noise, latents.device, args.multires_noise_iterations, args.multires_noise_discount
  27. )
  28.  
  29. b_size = latents.shape[0]
  30. min_timestep = 0 if args.min_timestep is None else args.min_timestep
  31. max_timestep = noise_scheduler.config.num_train_timesteps if args.max_timestep is None else args.max_timestep
  32.  
  33. timesteps = get_timesteps(min_timestep, max_timestep, b_size, latents.device)
  34.  
  35. if args.ip_noise_gamma:
  36. if args.ip_noise_gamma_random_strength:
  37. strength = torch.rand(1, device=latents.device) * args.ip_noise_gamma
  38. else:
  39. strength = args.ip_noise_gamma
  40. noisy_latents = noise_scheduler.add_noise(latents, noise + strength * torch.randn_like(latents), timesteps)
  41. else:
  42. noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps)
  43.  
  44. return noise, noisy_latents, timesteps
Advertisement
Add Comment
Please, Sign In to add comment