Upload extensions_built_in/diffusion_models/omnigen2/src/pipelines/pipeline_utils.py with huggingface_hub
Browse files
extensions_built_in/diffusion_models/omnigen2/src/pipelines/pipeline_utils.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def get_pipeline_embeds(pipeline, prompt, negative_prompt, device):
|
| 5 |
+
""" Get pipeline embeds for prompts bigger than the maxlength of the pipe
|
| 6 |
+
:param pipeline:
|
| 7 |
+
:param prompt:
|
| 8 |
+
:param negative_prompt:
|
| 9 |
+
:param device:
|
| 10 |
+
:return:
|
| 11 |
+
"""
|
| 12 |
+
max_length = pipeline.tokenizer.model_max_length
|
| 13 |
+
|
| 14 |
+
# simple way to determine length of tokens
|
| 15 |
+
# count_prompt = len(prompt.split(" "))
|
| 16 |
+
# count_negative_prompt = len(negative_prompt.split(" "))
|
| 17 |
+
|
| 18 |
+
# create the tensor based on which prompt is longer
|
| 19 |
+
# if count_prompt >= count_negative_prompt:
|
| 20 |
+
input_ids = pipeline.tokenizer(prompt, return_tensors="pt", truncation=False, padding='longest').input_ids.to(device)
|
| 21 |
+
# input_ids = pipeline.tokenizer(prompt, padding="max_length",
|
| 22 |
+
# max_length=pipeline.tokenizer.model_max_length,
|
| 23 |
+
# truncation=True,
|
| 24 |
+
# return_tensors="pt",).input_ids.to(device)
|
| 25 |
+
shape_max_length = input_ids.shape[-1]
|
| 26 |
+
|
| 27 |
+
if negative_prompt is not None:
|
| 28 |
+
negative_ids = pipeline.tokenizer(negative_prompt, truncation=True, padding="max_length",
|
| 29 |
+
max_length=shape_max_length, return_tensors="pt").input_ids.to(device)
|
| 30 |
+
|
| 31 |
+
# else:
|
| 32 |
+
# negative_ids = pipeline.tokenizer(negative_prompt, return_tensors="pt", truncation=False).input_ids.to(device)
|
| 33 |
+
# shape_max_length = negative_ids.shape[-1]
|
| 34 |
+
# input_ids = pipeline.tokenizer(prompt, return_tensors="pt", truncation=False, padding="max_length",
|
| 35 |
+
# max_length=shape_max_length).input_ids.to(device)
|
| 36 |
+
|
| 37 |
+
concat_embeds = []
|
| 38 |
+
neg_embeds = []
|
| 39 |
+
for i in range(0, shape_max_length, max_length):
|
| 40 |
+
if hasattr(pipeline.text_encoder.config, "use_attention_mask") and pipeline.text_encoder.config.use_attention_mask:
|
| 41 |
+
attention_mask = input_ids[:, i: i + max_length].attention_mask.to(device)
|
| 42 |
+
else:
|
| 43 |
+
attention_mask = None
|
| 44 |
+
concat_embeds.append(pipeline.text_encoder(input_ids[:, i: i + max_length],
|
| 45 |
+
attention_mask=attention_mask)[0])
|
| 46 |
+
|
| 47 |
+
if negative_prompt is not None:
|
| 48 |
+
if hasattr(pipeline.text_encoder.config, "use_attention_mask") and pipeline.text_encoder.config.use_attention_mask:
|
| 49 |
+
attention_mask = negative_ids[:, i: i + max_length].attention_mask.to(device)
|
| 50 |
+
else:
|
| 51 |
+
attention_mask = None
|
| 52 |
+
neg_embeds.append(pipeline.text_encoder(negative_ids[:, i: i + max_length],
|
| 53 |
+
attention_mask=attention_mask)[0])
|
| 54 |
+
|
| 55 |
+
concat_embeds = torch.cat(concat_embeds, dim=1)
|
| 56 |
+
|
| 57 |
+
if negative_prompt is not None:
|
| 58 |
+
neg_embeds = torch.cat(neg_embeds, dim=1)
|
| 59 |
+
else:
|
| 60 |
+
neg_embeds = None
|
| 61 |
+
|
| 62 |
+
return concat_embeds, neg_embeds
|