(
text_encoders,
tokenizers,
prompt: str,
max_sequence_length,
device=None,
num_images_per_prompt: int = 1,
)
| 840 | |
| 841 | # Copied from dreambooth sd3 example |
| 842 | def encode_prompt( |
| 843 | text_encoders, |
| 844 | tokenizers, |
| 845 | prompt: str, |
| 846 | max_sequence_length, |
| 847 | device=None, |
| 848 | num_images_per_prompt: int = 1, |
| 849 | ): |
| 850 | prompt = [prompt] if isinstance(prompt, str) else prompt |
| 851 | |
| 852 | clip_tokenizers = tokenizers[:2] |
| 853 | clip_text_encoders = text_encoders[:2] |
| 854 | |
| 855 | clip_prompt_embeds_list = [] |
| 856 | clip_pooled_prompt_embeds_list = [] |
| 857 | for tokenizer, text_encoder in zip(clip_tokenizers, clip_text_encoders): |
| 858 | prompt_embeds, pooled_prompt_embeds = _encode_prompt_with_clip( |
| 859 | text_encoder=text_encoder, |
| 860 | tokenizer=tokenizer, |
| 861 | prompt=prompt, |
| 862 | device=device if device is not None else text_encoder.device, |
| 863 | num_images_per_prompt=num_images_per_prompt, |
| 864 | ) |
| 865 | clip_prompt_embeds_list.append(prompt_embeds) |
| 866 | clip_pooled_prompt_embeds_list.append(pooled_prompt_embeds) |
| 867 | |
| 868 | clip_prompt_embeds = torch.cat(clip_prompt_embeds_list, dim=-1) |
| 869 | pooled_prompt_embeds = torch.cat(clip_pooled_prompt_embeds_list, dim=-1) |
| 870 | |
| 871 | t5_prompt_embed = _encode_prompt_with_t5( |
| 872 | text_encoders[-1], |
| 873 | tokenizers[-1], |
| 874 | max_sequence_length, |
| 875 | prompt=prompt, |
| 876 | num_images_per_prompt=num_images_per_prompt, |
| 877 | device=device if device is not None else text_encoders[-1].device, |
| 878 | ) |
| 879 | |
| 880 | clip_prompt_embeds = torch.nn.functional.pad( |
| 881 | clip_prompt_embeds, (0, t5_prompt_embed.shape[-1] - clip_prompt_embeds.shape[-1]) |
| 882 | ) |
| 883 | prompt_embeds = torch.cat([clip_prompt_embeds, t5_prompt_embed], dim=-2) |
| 884 | |
| 885 | return prompt_embeds, pooled_prompt_embeds |
| 886 | |
| 887 | |
| 888 | def main(args): |
no test coverage detected