change uniform sampler to prediction network, add option to jit unet for faster sds_loss

This commit is contained in:
terrance committed 2023-02-03 01:42:05 +00:00
1 parent 9e4bc773ad
commit 8ba8942779
4 files changed
+248 -47

No files matched your search

+31 -3
View File
@@ -17,9 +17,11 @@
# Modified from https://github.com/ashawkey/stable-dreamfusion/blob/main/nerf/sd.py
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import List, Optional, Tuple, Union
import appdirs
import mediapy
import numpy as np
import torch
@@ -49,6 +51,11 @@ SD_SOURCE = "runwayml/stable-diffusion-v1-5"
CLIP_SOURCE = "openai/clip-vit-large-patch14"
@dataclass
class UNet2DConditionOutput:
sample: torch.FloatTensor
class StableDiffusion(nn.Module):
"""Stable Diffusion implementation
@@ -81,13 +88,34 @@ class StableDiffusion(nn.Module):
pipe.enable_attention_slicing()
# use jitted unet
unet_traced_filename = Path(appdirs.user_data_dir("nerfstudio")) / "unet_traced.pt"
if unet_traced_filename.exists():
CONSOLE.print("Loading traced UNet.")
unet_traced = torch.jit.load("unet_traced.pt")
class TracedUNet(torch.nn.Module):
"""Jitted version of UNet"""
def __init__(self):
super().__init__()
self.in_channels = pipe.unet.in_channels
self.device = pipe.unet.device
def forward(self, latent_model_input, t, encoder_hidden_states):
sample = unet_traced(latent_model_input, t, encoder_hidden_states)[0]
return UNet2DConditionOutput(sample=sample)
self.unet = TracedUNet()
del pipe.unet
else:
self.unet = pipe.unet
self.unet.to(memory_format=torch.channels_last)
self.tokenizer = pipe.tokenizer
self.unet = pipe.unet
self.text_encoder = pipe.text_encoder
self.auto_encoder = pipe.vae
self.unet.to(memory_format=torch.channels_last)
CONSOLE.print("Stable Diffusion loaded!")
def get_text_embeds(