Spaces:
Sleeping
Sleeping
genevera
commited on
Commit
·
28d5bd6
1
Parent(s):
de80bf4
make results deterministic
Browse files
app.py
CHANGED
@@ -35,34 +35,35 @@ class AudioTokenWrapper(torch.nn.Module):
|
|
35 |
):
|
36 |
|
37 |
super().__init__()
|
|
|
38 |
# Load scheduler and models
|
39 |
-
self.ddpm = DDPMScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
40 |
-
self.ddim = DDIMScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
41 |
-
self.pndm = PNDMScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
42 |
-
self.lms = LMSDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
43 |
-
self.euler_anc = EulerAncestralDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
44 |
-
self.euler = EulerDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
45 |
-
self.dpm = DPMSolverMultistepScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
46 |
-
self.dpms = DPMSolverSinglestepScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
47 |
-
self.deis = DEISMultistepScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
48 |
-
self.unipc = UniPCMultistepScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
49 |
-
self.heun = HeunDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
50 |
-
self.kdpm2_anc = KDPM2AncestralDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
51 |
-
self.kdpm2 = KDPM2DiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
|
52 |
|
53 |
|
54 |
|
55 |
self.tokenizer = CLIPTokenizer.from_pretrained(
|
56 |
-
repo_id, subfolder="tokenizer"
|
57 |
)
|
58 |
self.text_encoder = CLIPTextModel.from_pretrained(
|
59 |
-
repo_id, subfolder="text_encoder", revision=None
|
60 |
)
|
61 |
self.unet = UNet2DConditionModel.from_pretrained(
|
62 |
-
repo_id, subfolder="unet", revision=None
|
63 |
)
|
64 |
self.vae = AutoencoderKL.from_pretrained(
|
65 |
-
repo_id, subfolder="vae", revision=None
|
66 |
)
|
67 |
|
68 |
checkpoint = torch.load(
|
@@ -172,17 +173,18 @@ def greet(audio, steps=25, scheduler="ddpm"):
|
|
172 |
audio_values = torch.unsqueeze(torch.tensor(audio), dim=0).to(device).to(dtype=weight_dtype)
|
173 |
if audio_values.ndim == 1:
|
174 |
audio_values = torch.unsqueeze(audio_values, dim=0)
|
|
|
|
|
175 |
with torch.no_grad():
|
176 |
-
torch.cuda.empty_cache()
|
177 |
aud_features = model.aud_encoder.extract_features(audio_values)[1]
|
178 |
audio_token = model.embedder(aud_features)
|
|
|
179 |
|
180 |
-
token_embeds = model.text_encoder.get_input_embeddings().weight.data
|
181 |
token_embeds[model.placeholder_token_id] = audio_token.clone()
|
182 |
g_gpu = torch.Generator(device='cuda')
|
183 |
-
g_gpu.manual_seed(
|
184 |
pipeline = StableDiffusionPipeline.from_pretrained(
|
185 |
-
|
186 |
tokenizer=model.tokenizer,
|
187 |
text_encoder=model.text_encoder,
|
188 |
vae=model.vae,
|
@@ -190,7 +192,8 @@ def greet(audio, steps=25, scheduler="ddpm"):
|
|
190 |
scheduler=use_sched,
|
191 |
safety_checker=None,
|
192 |
).to(device)
|
193 |
-
pipeline.
|
|
|
194 |
# pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
|
195 |
# pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
|
196 |
print(f"taking {steps} steps using the {scheduler} scheduler")
|
|
|
35 |
):
|
36 |
|
37 |
super().__init__()
|
38 |
+
self.repo_id = repo_id
|
39 |
# Load scheduler and models
|
40 |
+
self.ddpm = DDPMScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
41 |
+
self.ddim = DDIMScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
42 |
+
self.pndm = PNDMScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
43 |
+
self.lms = LMSDiscreteScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
44 |
+
self.euler_anc = EulerAncestralDiscreteScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
45 |
+
self.euler = EulerDiscreteScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
46 |
+
self.dpm = DPMSolverMultistepScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
47 |
+
self.dpms = DPMSolverSinglestepScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
48 |
+
self.deis = DEISMultistepScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
49 |
+
self.unipc = UniPCMultistepScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
50 |
+
self.heun = HeunDiscreteScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
51 |
+
self.kdpm2_anc = KDPM2AncestralDiscreteScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
52 |
+
self.kdpm2 = KDPM2DiscreteScheduler.from_pretrained(self.repo_id, subfolder="scheduler")
|
53 |
|
54 |
|
55 |
|
56 |
self.tokenizer = CLIPTokenizer.from_pretrained(
|
57 |
+
self.repo_id, subfolder="tokenizer"
|
58 |
)
|
59 |
self.text_encoder = CLIPTextModel.from_pretrained(
|
60 |
+
self.repo_id, subfolder="text_encoder", revision=None
|
61 |
)
|
62 |
self.unet = UNet2DConditionModel.from_pretrained(
|
63 |
+
self.repo_id, subfolder="unet", revision=None
|
64 |
)
|
65 |
self.vae = AutoencoderKL.from_pretrained(
|
66 |
+
self.repo_id, subfolder="vae", revision=None
|
67 |
)
|
68 |
|
69 |
checkpoint = torch.load(
|
|
|
173 |
audio_values = torch.unsqueeze(torch.tensor(audio), dim=0).to(device).to(dtype=weight_dtype)
|
174 |
if audio_values.ndim == 1:
|
175 |
audio_values = torch.unsqueeze(audio_values, dim=0)
|
176 |
+
|
177 |
+
# i dont know why but this seems mandatory for deterministic results
|
178 |
with torch.no_grad():
|
|
|
179 |
aud_features = model.aud_encoder.extract_features(audio_values)[1]
|
180 |
audio_token = model.embedder(aud_features)
|
181 |
+
token_embeds = model.text_encoder.get_input_embeddings().weight.data
|
182 |
|
|
|
183 |
token_embeds[model.placeholder_token_id] = audio_token.clone()
|
184 |
g_gpu = torch.Generator(device='cuda')
|
185 |
+
g_gpu.manual_seed(23229249375547) # no reason this can't be input by the user!
|
186 |
pipeline = StableDiffusionPipeline.from_pretrained(
|
187 |
+
pretrained_model_name_or_path=model.repo_id,
|
188 |
tokenizer=model.tokenizer,
|
189 |
text_encoder=model.text_encoder,
|
190 |
vae=model.vae,
|
|
|
192 |
scheduler=use_sched,
|
193 |
safety_checker=None,
|
194 |
).to(device)
|
195 |
+
pipeline.enable_xformers_memory_efficient_attention()
|
196 |
+
|
197 |
# pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
|
198 |
# pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
|
199 |
print(f"taking {steps} steps using the {scheduler} scheduler")
|