cswry / OSEDiff

[NeurlPS2024] One-Step Effective Diffusion Network for Real-World Image Super-Resolution
Apache License 2.0
215 stars 12 forks source link

Reproduce the training process for multiple losses encounters "RuntimeError: Expected to mark a variable ready only once" #30

Closed Yangkai-Wei closed 2 months ago

Yangkai-Wei commented 3 months ago

This is a very solid job, I reproduce the training process based on the paper and previous issues.

Among them, I use the following method to implement multiple losses, including loss_data, vsd_loss and loss_diff. Then by controlling whether the gradient is trainable, loss_diff only updates unet_reg.

`
loss_data_mse = F.mse_loss(output_image, pixel_values) loss_data_lpips = lpips_loss(output_image, pixel_values).mean()

loss_data = loss_data_mse + 2*loss_data_lpips

w = (1 - alphas[timesteps]) w = w.view(bsz, 1, 1, 1) grad = w*(noise_pred_reg_lora.detach()- noise_pred_reg_frozen) grad = torch.nan_to_num(grad)

loss_vds = SpecifyGradient.apply(latents, grad)

loss_diff = F.mse_loss(noise_pred_reg_lora.float(), target.float(), reduction="mean")

loss_lora = loss_data + loss_vds

for param in unet_reg.parameters(): param.requires_grad = False

for param in unet_lora.parameters(): param.requires_grad = original_trainable_status['unet_lora'][param] for param in vae.parameters(): param.requires_grad = original_trainable_status['vae'][param]

accelerator.backward(loss_lora, retain_graph=True)

for param in unet_reg.parameters(): param.requires_grad = original_trainable_status['unet_reg'][param] for param in unet_lora.parameters(): param.requires_grad = False for param in vae.parameters(): param.requires_grad = False

accelerator.backward(loss_diff)

if accelerator.sync_gradients: params_to_clip = params_to_optimize accelerator.clip_gradnorm(params_to_clip, args.max_grad_norm)

if accelerator.sync_gradients: optimizer.step() lr_scheduler.step() optimizer.zero_grad(set_to_none=args.set_grads_to_none)

for param in unet_lora.parameters(): param.requires_grad = original_trainable_status['unet_lora'][param] for param in unet_reg.parameters(): param.requires_grad = original_trainable_status['unet_reg'][param] for param in vae.parameters(): param.requires_grad = original_trainable_status['vae'][param]

`

However, when training with accelerator’s DDP, I keep encountering the following error. Can anyone please guide me on how to solve it? Thank you very much.

RuntimeError: Expected to mark a variable ready only once. This error is caused by one of the following reasons: 1) Use of a module parameter outside the forward function. Please make sure model parameters are not shared across multiple concurrent forward-backward passes. or try to use _set_static_graph() as a workaround if this module graph does not change during training loop.2) Reused parameters in multiple reentrant backward passes. For example, if you use multiple checkpoint functions to wrap the same part of your model, it would result in the same set of parameters been used by different reentrant backward passes multiple times, and hence marking a variable ready multiple times. DDP does not support such use cases in default. You can try to use _set_static_graph() as a workaround if your module graph does not change over iterations.