nyanko7 commited on
Commit
8902214
1 Parent(s): ebb4814

chore: sync with upstream

Browse files
Files changed (1) hide show
  1. app.py +16 -25
app.py CHANGED
@@ -60,7 +60,7 @@ samplers_k_diffusion = [
60
  # ]
61
 
62
  start_time = time.time()
63
- timeout = 120
64
 
65
  scheduler = DDIMScheduler.from_pretrained(
66
  base_model,
@@ -151,6 +151,9 @@ def setup_model(name, lora_state=None, lora_scale=1.0):
151
  local_lora.to(local_unet.device, dtype=local_unet.dtype)
152
 
153
  pipe.setup_unet(local_unet)
 
 
 
154
  pipe.setup_text_encoder(clip_skip, local_te)
155
  return pipe
156
 
@@ -177,7 +180,7 @@ def make_token_names(embs):
177
  all_tokens.append(tokens)
178
  return all_tokens
179
 
180
- def setup_tokenizer(embs):
181
  reg_match = [re.compile(fr"(?:^|(?<=\s|,)){k}(?=,|\s|$)") for k in embs.keys()]
182
  clip_keywords = [' '.join(s) for s in make_token_names(embs)]
183
 
@@ -186,18 +189,13 @@ def setup_tokenizer(embs):
186
  prompt = m.sub(v, prompt)
187
  return prompt
188
 
 
 
 
 
 
 
189
 
190
- def restore_all():
191
- global te_base_weight, tokenizer
192
- tokenizer.prepare_for_tokenization = original_prepare_for_tokenization
193
-
194
- embeddings = text_encoder.get_input_embeddings()
195
- text_encoder.get_input_embeddings().weight.data = embeddings.weight.data[:te_base_weight_length]
196
- tokenizer = CLIPTokenizer.from_pretrained(
197
- base_model,
198
- subfolder="tokenizer",
199
- torch_dtype=torch.float16,
200
- )
201
 
202
  def convert_size(size_bytes):
203
  if size_bytes == 0:
@@ -230,20 +228,19 @@ def inference(
230
  lora_state=None,
231
  lora_scale=None,
232
  ):
233
- global pipe, unet, tokenizer, text_encoder
234
  if seed is None or seed == 0:
235
  seed = random.randint(0, 2147483647)
236
 
237
  start_time = time.time()
238
- restore_all()
239
  generator = torch.Generator("cuda").manual_seed(int(seed))
240
 
241
- setup_model(model, lora_state, lora_scale)
242
  sampler_name, sampler_opt = None, None
243
  for label, funcname, options in samplers_k_diffusion:
244
  if label == sampler:
245
  sampler_name, sampler_opt = funcname, options
246
 
 
247
  if embs is not None and len(embs) > 0:
248
  ti_embs = {}
249
  for name, file in embs.items():
@@ -255,13 +252,14 @@ def inference(
255
  ti_embs[name] = loaded_learned_embeds
256
 
257
  if len(ti_embs) > 0:
258
- tokens = setup_tokenizer(ti_embs)
259
  added_tokens = tokenizer.add_tokens(tokens)
260
  delta_weight = torch.cat([val for val in ti_embs.values()], dim=0)
261
 
262
  assert added_tokens == delta_weight.shape[0]
263
  text_encoder.resize_token_embeddings(len(tokenizer))
264
- text_encoder.get_input_embeddings().weight.data[-delta_weight.shape[0]:] = delta_weight
 
265
 
266
  config = {
267
  "negative_prompt": neg_prompt,
@@ -296,13 +294,6 @@ def inference(
296
  else:
297
  result = pipe.txt2img(prompt, width=width, height=height, **config)
298
 
299
- # restore
300
- if embs is not None and len(embs) > 0:
301
- restore_all()
302
-
303
- if model in [key[0] for key in alt_models]:
304
- local_unet.to("cpu")
305
-
306
  end_time = time.time()
307
  vram_free, vram_total = torch.cuda.mem_get_info()
308
  print(f"done: model={model}, res={width}x{height}, step={steps}, time={round(end_time-start_time, 2)}s, vram_alloc={convert_size(vram_total-vram_free)}/{convert_size(vram_total)}")
 
60
  # ]
61
 
62
  start_time = time.time()
63
+ timeout = 90
64
 
65
  scheduler = DDIMScheduler.from_pretrained(
66
  base_model,
 
151
  local_lora.to(local_unet.device, dtype=local_unet.dtype)
152
 
153
  pipe.setup_unet(local_unet)
154
+ pipe.tokenizer.prepare_for_tokenization = original_prepare_for_tokenization
155
+ pipe.tokenizer.added_tokens_encoder = {}
156
+ pipe.tokenizer.added_tokens_decoder = {}
157
  pipe.setup_text_encoder(clip_skip, local_te)
158
  return pipe
159
 
 
180
  all_tokens.append(tokens)
181
  return all_tokens
182
 
183
+ def setup_tokenizer(tokenizer, embs):
184
  reg_match = [re.compile(fr"(?:^|(?<=\s|,)){k}(?=,|\s|$)") for k in embs.keys()]
185
  clip_keywords = [' '.join(s) for s in make_token_names(embs)]
186
 
 
189
  prompt = m.sub(v, prompt)
190
  return prompt
191
 
192
+ def prepare_for_tokenization(self, text: str, is_split_into_words: bool = False, **kwargs):
193
+ text = parse_prompt(text)
194
+ r = original_prepare_for_tokenization(text, is_split_into_words, **kwargs)
195
+ return r
196
+ tokenizer.prepare_for_tokenization = prepare_for_tokenization.__get__(tokenizer, CLIPTokenizer)
197
+ return [t for sublist in make_token_names(embs) for t in sublist]
198
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
  def convert_size(size_bytes):
201
  if size_bytes == 0:
 
228
  lora_state=None,
229
  lora_scale=None,
230
  ):
 
231
  if seed is None or seed == 0:
232
  seed = random.randint(0, 2147483647)
233
 
234
  start_time = time.time()
235
+ pipe = setup_model(model, lora_state, lora_scale)
236
  generator = torch.Generator("cuda").manual_seed(int(seed))
237
 
 
238
  sampler_name, sampler_opt = None, None
239
  for label, funcname, options in samplers_k_diffusion:
240
  if label == sampler:
241
  sampler_name, sampler_opt = funcname, options
242
 
243
+ tokenizer, text_encoder = pipe.tokenizer, pipe.text_encoder
244
  if embs is not None and len(embs) > 0:
245
  ti_embs = {}
246
  for name, file in embs.items():
 
252
  ti_embs[name] = loaded_learned_embeds
253
 
254
  if len(ti_embs) > 0:
255
+ tokens = setup_tokenizer(tokenizer, ti_embs)
256
  added_tokens = tokenizer.add_tokens(tokens)
257
  delta_weight = torch.cat([val for val in ti_embs.values()], dim=0)
258
 
259
  assert added_tokens == delta_weight.shape[0]
260
  text_encoder.resize_token_embeddings(len(tokenizer))
261
+ token_embeds = text_encoder.get_input_embeddings().weight.data
262
+ token_embeds[-delta_weight.shape[0]:] = delta_weight
263
 
264
  config = {
265
  "negative_prompt": neg_prompt,
 
294
  else:
295
  result = pipe.txt2img(prompt, width=width, height=height, **config)
296
 
 
 
 
 
 
 
 
297
  end_time = time.time()
298
  vram_free, vram_total = torch.cuda.mem_get_info()
299
  print(f"done: model={model}, res={width}x{height}, step={steps}, time={round(end_time-start_time, 2)}s, vram_alloc={convert_size(vram_total-vram_free)}/{convert_size(vram_total)}")