multimodalart / majesty-diffusion

Majesty Diffusion by @Dango233(@Dango233max) and @apolinario (@multimodalart)
276 stars 45 forks source link

Seeding the system #9

Open joetm opened 2 years ago

joetm commented 2 years ago

Hi, great work on this system!

I am looking for a way to make the image generation reproducible. Other notebooks/scripts have a seed value. I added some code to Latent Majesty Diffusion (v1.3) to seed torch:

seed = 1234567
torch.manual_seed(seed)

I also added this snippet to seed pseudo-random number generators in pytorch, numpy, python.random:

from pytorch_lightning import seed_everything
seed_everything(seed, workers=True)

But the images are still different in each run.

Any idea what else I need to change to make the image generation reproducible? That is, given the same seed, I want the system to produce the same image.

apolinario commented 2 years ago

I've been trying to do the same! I'd love to be able to optionally make the process deterministic

JDunn3 commented 2 years ago

Perhaps this?

Disabling the benchmarking feature with torch.backends.cudnn.benchmark = False causes cuDNN to deterministically select an algorithm, possibly at the cost of reduced performance.

https://pytorch.org/docs/stable/notes/randomness.html

joetm commented 2 years ago

This is all the code I added in an attempt to make the system (and its upscaler) deterministic.

from pytorch_lightning import seed_everything
seed = 1040790415
def seed_torch(seed = 1040790415):
    os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    random.seed(seed)
    np.random.seed(seed) 
    seed_everything(seed, workers=True)
    torch.backends.cudnn.benchmark = False  
    torch.backends.cudnn.deterministic = True
    torch.use_deterministic_algorithms(True, warn_only=True)
seed_torch()

If you set warn_only to False, it will show you the name of the nondeterministic algorithm. But there could be more.

In the end, I gave up and used another system entirely.