MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / encode_prompt

Function encode_prompt

diffusers/examples/controlnet/train_controlnet_sdxl.py:698–735  ·  view source on GitHub ↗
(prompt_batch, text_encoders, tokenizers, proportion_empty_prompts, is_train=True)

Source from the content-addressed store, hash-verified

696
697# Adapted from pipelines.StableDiffusionXLPipeline.encode_prompt
698def encode_prompt(prompt_batch, text_encoders, tokenizers, proportion_empty_prompts, is_train=True):
699 prompt_embeds_list = []
700
701 captions = []
702 for caption in prompt_batch:
703 if random.random() < proportion_empty_prompts:
704 captions.append("")
705 elif isinstance(caption, str):
706 captions.append(caption)
707 elif isinstance(caption, (list, np.ndarray)):
708 # take a random caption if there are multiple
709 captions.append(random.choice(caption) if is_train else caption[0])
710
711 with torch.no_grad():
712 for tokenizer, text_encoder in zip(tokenizers, text_encoders):
713 text_inputs = tokenizer(
714 captions,
715 padding="max_length",
716 max_length=tokenizer.model_max_length,
717 truncation=True,
718 return_tensors="pt",
719 )
720 text_input_ids = text_inputs.input_ids
721 prompt_embeds = text_encoder(
722 text_input_ids.to(text_encoder.device),
723 output_hidden_states=True,
724 )
725
726 # We are only ALWAYS interested in the pooled output of the final text encoder
727 pooled_prompt_embeds = prompt_embeds[0]
728 prompt_embeds = prompt_embeds.hidden_states[-2]
729 bs_embed, seq_len, _ = prompt_embeds.shape
730 prompt_embeds = prompt_embeds.view(bs_embed, seq_len, -1)
731 prompt_embeds_list.append(prompt_embeds)
732
733 prompt_embeds = torch.concat(prompt_embeds_list, dim=-1)
734 pooled_prompt_embeds = pooled_prompt_embeds.view(bs_embed, -1)
735 return prompt_embeds, pooled_prompt_embeds
736
737
738def prepare_train_dataset(dataset, accelerator):

Callers 1

compute_embeddingsFunction · 0.70

Calls 2

text_encoderFunction · 0.85
toMethod · 0.45

Tested by

no test coverage detected