Code Monkey home page Code Monkey logo

swiftbrush's People

Contributors

thuanz123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

swiftbrush's Issues

Paper code reproduction

Hi @thuanz123, thanks for your great work in one-step diffusion model. We tried to reproduce your work, but there are currently issues with the results. Could you please help review the code?

unet is loaded from pretrained sd1.5. 

tea_unet = copy.deepcopy(unet)
tea_unet.eval()
tea_unet.requires_grad_(False)

fake_unet = copy.deepcopy(unet)
fake_unet.train()
fake_unet.requires_grad_(True)

Omit the definitions of optimizer, dataloader, lr_scheduler .....

noise = torch.randn_like(latents)
bsz = noise.size(0)

with accelerator.accumulate(unet):

     noisy_latents = noise
     timesteps = torch.ones([bsz]).long().to(device) * (ddim_scheduler.num_train_timesteps - 1)
    
    pred_cond = unet(
        sample=noisy_latents,
        timestep=timesteps,
        encoder_hidden_states=text_positive_embeddings,
        attention_mask=None,
        added_cond_kwargs=added_cond_kwargs,
        ).sample

    predict = ddim_scheduler.convert_output(
        model_output=pred_cond,
        model_output_type=args.unet_prediction,
        sample=noisy_latents, 
        timesteps=timesteps,
        )
    predict_x0 = predict.pred_original_sample 
    predict_noise = predict.pred_epsilon

    ###################################
    # update fake score function    #
    ###################################
    # denoising Loss
    fake_timesteps = torch.randint(0, ddim_scheduler.num_train_timesteps, (bsz,), device=latents.device)
    fake_noise = torch.randn_like(predict_x0)

    fake_noisy_latents = ddim_scheduler.add_noise(predict_x0.detach(), fake_noise, fake_timesteps)

    fake_pred_cond = fake_unet(
        sample=fake_noisy_latents,
        timestep=fake_timesteps,
        encoder_hidden_states=text_positive_embeddings,
        attention_mask=None,
        added_cond_kwargs=added_cond_kwargs,
        ).sample
    fake_loss = F.mse_loss(fake_pred_cond.float(), fake_noise.float(), reduction="none")
    fake_snr = compute_snr(ddim_scheduler, fake_timesteps)
    fake_loss = (fake_loss.mean([1,2,3]) * fake_snr).mean()

    if (step + 1) % args.gradient_accumulation_steps == 0:  # debug check
        assert optimizer.gradient_state.sync_gradients is True
        assert optimizer_D.gradient_state.sync_gradients is True
    else:
        assert optimizer.gradient_state.sync_gradients is False
        assert optimizer_D.gradient_state.sync_gradients is False
    if accelerator.sync_gradients:
        accelerator.backward(fake_loss) 
        accelerator.clip_grad_norm_(fake_unet.parameters(), args.max_grad_norm)
    else:
        with fake_unet.no_sync():
            accelerator.backward(fake_loss)
            accelerator.clip_grad_norm_(fake_unet.parameters(), args.max_grad_norm)

    optimizer_D.step()
    optimizer_D.zero_grad()
    
    # distributionMatchingLoss
    dm_timesteps = torch.randint(int(ddim_scheduler.num_train_timesteps * 0.02), 
        int(ddim_scheduler.num_train_timesteps * 0.98), (bsz,), device=latents.device)
    dm_noise = torch.randn_like(predict_x0)
    dm_noisy_latents = ddim_scheduler.add_noise(predict_x0, dm_noise, timesteps)
    
    with torch.no_grad():
        pred_cond_real = tea_unet(
            sample=dm_noisy_latents,
            timestep=dm_timesteps,
            encoder_hidden_states=text_positive_embeddings,
            attention_mask=None,
            added_cond_kwargs=added_cond_kwargs,
            ).sample
        pred_uncond_real = tea_unet(
            sample=dm_noisy_latents,
            timestep=dm_timesteps,
            encoder_hidden_states=text_negative_embeddings,
            attention_mask=None,
            added_cond_kwargs=added_uncond_kwargs,
            ).sample
        
        pred_epsilon_real = classifier_free_guidance(
            pred_cond_real, pred_uncond_real, guidance_weight=args.validation_guidance_weight)

        pred_cond_fake = fake_unet(
            sample=dm_noisy_latents,
            timestep=dm_timesteps,
            encoder_hidden_states=text_positive_embeddings,
            attention_mask=None,
            added_cond_kwargs=added_cond_kwargs,
            ).sample
        pred_uncond_fake = fake_unet(
            sample=dm_noisy_latents,
            timestep=dm_timesteps,
            encoder_hidden_states=text_negative_embeddings,
            attention_mask=None,
            added_cond_kwargs=added_uncond_kwargs,
            ).sample
        pred_epsilon_fake = classifier_free_guidance(
            pred_cond_fake, pred_uncond_fake, guidance_weight=args.validation_guidance_weight)

    snr = compute_snr(ddim_scheduler, dm_timesteps)
    grad = (pred_epsilon_real - pred_epsilon_fake) * snr.reshape((bsz, 1, 1, 1))
    dm_loss = F.mse_loss(predict_x0, (predict_x0 - grad).detach()).mean()

    loss = dm_loss
    
    if accelerator.sync_gradients:
        accelerator.backward(loss)
        accelerator.clip_grad_norm_(unet.parameters(), args.max_grad_norm)
    else:
        with unet.no_sync():
            accelerator.backward(loss)
            accelerator.clip_grad_norm_(unet.parameters(), args.max_grad_norm)

    optimizer.step()
    optimizer.zero_grad()

I'm glad to hear that you're looking forward to my response!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.