basujindal / stable-diffusion

Optimized Stable Diffusion modified to run on lower GPU VRAM
Other
3.14k stars 467 forks source link

TypeError: sample() got an unexpected keyword argument 'sampler' ...again #191

Closed Bakunawa0 closed 1 year ago

Bakunawa0 commented 1 year ago

92 contains a similar error, but is now closed because apparently updating the conda env solved it for them. Good for them, but doing that didn't work for me. Of course this happened several days and commits ago so I doubt it is relevant now. Reading through the code, I found the definition of sample at line 470:

def sample(self,
               S,
               conditioning,
               x0=None,
               shape = None,
               seed=1234, 
               callback=None,
               img_callback=None,
               quantize_x0=False,
               eta=0.,
               mask=None,
               sampler="plms",
               temperature=1.,
               noise_dropout=0.,
               score_corrector=None,
               corrector_kwargs=None,
               verbose=True,
               x_T=None,
               log_every_t=100,
               unconditional_guidance_scale=1.,
               unconditional_conditioning=None,
               ):

If we compare this to when it is called in optimized_txt2img.py, this error seems impossible:

samples_ddim = model.sample(
                    S=opt.ddim_steps,
                    conditioning=c,
                    seed=opt.seed,
                    shape=shape,
                    verbose=False,
                    unconditional_guidance_scale=opt.scale,
                    unconditional_conditioning=uc,
                    eta=opt.ddim_eta,
                    x_T=start_code,
                    sampler=opt.sampler,
                )

How is sampler unexpected? It is obviously defined. opt.sampler is also initialized to plms if you don't specify a sampler, so it can't be that either. What gives? Here's my output, even though it is basically the same as in #92 , just formatted in a more readable way.

$ python optimizedSD/optimized_txt2img.py --prompt "[REDACTED]" --H 448 --W 512 --n_samples 3 --n_iter 3 --ddim_steps 50
Global seed set to 303241
Loading model from models/ldm/stable-diffusion-v1/model.ckpt
Global Step: 470000
UNet: Running in eps-prediction mode
CondStage: Running in eps-prediction mode
FirstStage: Running in eps-prediction mode
making attention of type 'vanilla' with 512 in_channels
Working with z of shape (1, 4, 32, 32) = 4096 dimensions.
making attention of type 'vanilla' with 512 in_channels
Using prompt: [REDACTED]
data:   0%|                                                                             | 0/1 [00:03<?, ?it/s]
Sampling:   0%|                                                                         | 0/3 [00:03<?, ?it/s]
Traceback (most recent call last):
File "optimizedSD/optimized_txt2img.py", line 299, in <module>
samples_ddim = model.sample(
File "/home/jul/.conda/envs/ldm/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
TypeError: sample() got an unexpected keyword argument 'sampler'
Bakunawa0 commented 1 year ago

I removed the ldm environment then recreated it and that fixed the issue. Strange, but at least it's working again.