Diffusers 文档
SanaPipeline
并获得增强的文档体验
开始使用
SanaPipeline
SANA:基于线性扩散 Transformer 的高效高分辨率图像合成,由 NVIDIA 和麻省理工 HAN 实验室的 Enze Xie、Junsong Chen、Junyu Chen、Han Cai、Haotian Tang、Yujun Lin、Zhekai Zhang、Muyang Li、Ligeng Zhu、Yao Lu、Song Han 共同完成。
论文摘要如下:
我们引入了 Sana,一个文本到图像框架,能够高效生成高达 4096×4096 分辨率的图像。Sana 能够以极快的速度合成高分辨率、高质量的图像,并且具有强大的文本-图像对齐能力,可在笔记本电脑 GPU 上部署。核心设计包括:(1) 深度压缩自编码器:与传统仅压缩图像 8 倍的自编码器不同,我们训练了一个可将图像压缩 32 倍的自编码器,有效减少了潜在令牌的数量。(2) 线性 DiT:我们用线性注意力替换了 DiT 中的所有香草注意力,这在不牺牲质量的情况下在更高分辨率下更高效。(3) 仅解码器文本编码器:我们用现代仅解码器小型 LLM 替换了 T5 作为文本编码器,并设计了带有上下文学习的复杂人工指令,以增强图像-文本对齐。(4) 高效训练和采样:我们提出了 Flow-DPM-Solver 以减少采样步数,并采用高效的字幕标注和选择来加速收敛。因此,Sana-0.6B 与现代巨型扩散模型(例如 Flux-12B)相比非常有竞争力,其模型大小小 20 倍,吞吐量快 100 倍以上。此外,Sana-0.6B 可以在 16GB 笔记本电脑 GPU 上部署,生成 1024×1024 分辨率图像所需时间不到 1 秒。Sana 使得低成本内容创作成为可能。代码和模型将公开发布。
此管道由 lawrence-cj 和 chenjy2003 贡献。原始代码库可在 此处 找到。原始权重可在 hf.co/Efficient-Large-Model 下找到。
可用模型
模型 | 推荐数据类型 |
---|---|
Efficient-Large-Model/Sana_1600M_1024px_BF16_diffusers | torch.bfloat16 |
Efficient-Large-Model/Sana_1600M_1024px_diffusers | torch.float16 |
Efficient-Large-Model/Sana_1600M_1024px_MultiLing_diffusers | torch.float16 |
Efficient-Large-Model/Sana_1600M_512px_diffusers | torch.float16 |
Efficient-Large-Model/Sana_1600M_512px_MultiLing_diffusers | torch.float16 |
Efficient-Large-Model/Sana_600M_1024px_diffusers | torch.float16 |
Efficient-Large-Model/Sana_600M_512px_diffusers | torch.float16 |
更多信息请参考此集合。
注意:推荐的数据类型是针对 Transformer 权重的。文本编码器和 VAE 权重必须保持为 `torch.bfloat16` 或 `torch.float32` 才能使模型正常工作。请参考下面的推理示例,了解如何使用推荐的数据类型加载模型。
请务必为下载的模型检查点传递 `variant` 参数,以减少磁盘空间占用。对于推荐数据类型为 `torch.float16` 的模型,请将其设置为 `"fp16"`;对于推荐数据类型为 `torch.bfloat16` 的模型,请将其设置为 `"bf16"`。默认情况下,会下载 `torch.float32` 权重,这会占用两倍的磁盘存储空间。此外,`torch.float32` 权重可以通过指定 `torch_dtype` 参数进行即时下转换。请参阅文档了解更多信息。
量化
量化有助于通过以较低精度数据类型存储模型权重来减少大型模型的内存需求。但是,量化对视频质量的影响可能因视频模型而异。
有关支持的量化后端以及如何选择适合您用例的量化后端,请参阅量化概述。以下示例演示了如何使用 bitsandbytes 加载量化的 SanaPipeline 进行推理。
import torch
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, SanaTransformer2DModel, SanaPipeline
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, AutoModel
quant_config = BitsAndBytesConfig(load_in_8bit=True)
text_encoder_8bit = AutoModel.from_pretrained(
"Efficient-Large-Model/Sana_1600M_1024px_diffusers",
subfolder="text_encoder",
quantization_config=quant_config,
torch_dtype=torch.float16,
)
quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = SanaTransformer2DModel.from_pretrained(
"Efficient-Large-Model/Sana_1600M_1024px_diffusers",
subfolder="transformer",
quantization_config=quant_config,
torch_dtype=torch.float16,
)
pipeline = SanaPipeline.from_pretrained(
"Efficient-Large-Model/Sana_1600M_1024px_diffusers",
text_encoder=text_encoder_8bit,
transformer=transformer_8bit,
torch_dtype=torch.float16,
device_map="balanced",
)
prompt = "a tiny astronaut hatching from an egg on the moon"
image = pipeline(prompt).images[0]
image.save("sana.png")
< 来源 >
class diffusers.SanaPipeline
< source >( tokenizer: typing.Union[transformers.models.gemma.tokenization_gemma.GemmaTokenizer, transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast] text_encoder: Gemma2PreTrainedModel vae: AutoencoderDC transformer: SanaTransformer2DModel scheduler: DPMSolverMultistepScheduler )
使用 Sana 进行文本到图像生成的管道。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = None negative_prompt: str = '' num_inference_steps: int = 20 timesteps: typing.List[int] = None sigmas: typing.List[float] = None guidance_scale: float = 4.5 num_images_per_prompt: typing.Optional[int] = 1 height: int = 1024 width: int = 1024 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True clean_caption: bool = False use_resolution_binning: bool = True attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 300 complex_human_instruction: typing.List[str] = ["Given a user prompt, generate an 'Enhanced prompt' that provides detailed visual descriptions suitable for image generation. Evaluate the level of detail in the user prompt:", '- If the prompt is simple, focus on adding specifics about colors, shapes, sizes, textures, and spatial relationships to create vivid and concrete scenes.', '- If the prompt is already detailed, refine and enhance the existing details slightly without overcomplicating.', 'Here are examples of how to transform or refine prompts:', '- User Prompt: A cat sleeping -> Enhanced: A small, fluffy white cat curled up in a round shape, sleeping peacefully on a warm sunny windowsill, surrounded by pots of blooming red flowers.', '- User Prompt: A busy city street -> Enhanced: A bustling city street scene at dusk, featuring glowing street lamps, a diverse crowd of people in colorful clothing, and a double-decker bus passing by towering glass skyscrapers.', 'Please generate only the enhanced description for the prompt below and avoid including any additional commentary or evaluations:', 'User Prompt: '] ) → SanaPipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示词。如果未定义,则必须传递prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示词。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时(即,如果guidance_scale
小于1
则忽略)。 - num_inference_steps (
int
, 可选, 默认为 20) — 去噪步数。更多去噪步数通常会产生更高质量的图像,但推理速度会变慢。 - timesteps (
List[int]
, 可选) — 自定义时间步长,用于支持timesteps
参数的调度器中的去噪过程。如果未定义,将使用传递num_inference_steps
时的默认行为。必须按降序排列。 - sigmas (
List[float]
, 可选) — 自定义 sigma 值,用于支持sigmas
参数的调度器中的去噪过程。如果未定义,将使用传递num_inference_steps
时的默认行为。 - guidance_scale (
float
, 可选, 默认为 4.5) — 如 Classifier-Free Diffusion Guidance 中定义的引导比例。guidance_scale
定义为 Imagen Paper 中方程 2 的w
。通过设置guidance_scale > 1
启用引导比例。更高的引导比例鼓励生成与文本prompt
紧密相关的图像,通常以牺牲图像质量为代价。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示词生成的图像数量。 - height (
int
, 可选, 默认为 self.unet.config.sample_size) — 生成图像的高度(像素)。 - width (
int
, 可选, 默认为 self.unet.config.sample_size) — 生成图像的宽度(像素)。 - eta (
float
, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η):https://huggingface.co/papers/2010.02502。仅适用于 schedulers.DDIMScheduler,对其他调度器将被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch 生成器,用于使生成过程具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的噪声潜在变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词调整相同的生成。如果未提供,将使用提供的随机generator
进行采样生成潜在张量。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词加权。如果未提供,将从prompt
输入参数生成文本嵌入。 - prompt_attention_mask (
torch.Tensor
, 可选) — 预生成的文本嵌入注意力掩码。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。对于 PixArt-Sigma,此负面提示词应为空字符串。如果未提供,将从negative_prompt
输入参数生成负面提示词嵌入。 - negative_prompt_attention_mask (
torch.Tensor
, 可选) — 预生成的负面文本嵌入注意力掩码。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion.IFPipelineOutput
而不是普通元组。 - attention_kwargs — 一个 kwargs 字典,如果指定,则会传递给 diffusers.models.attention_processor 中
self.processor
下定义的AttentionProcessor
。 - clean_caption (
bool
, 可选, 默认为True
) — 是否在创建嵌入之前清理字幕。需要安装beautifulsoup4
和ftfy
。如果未安装这些依赖项,嵌入将从原始提示生成。 - use_resolution_binning (
bool
默认为True
) — 如果设置为True
,则首先使用ASPECT_RATIO_1024_BIN
将请求的高度和宽度映射到最接近的分辨率。将生成的潜在变量解码为图像后,它们将被调整回请求的分辨率。对于生成非方形图像非常有用。 - callback_on_step_end (
Callable
, 可选) — 推理过程中在每个去噪步骤结束时调用的函数。该函数以以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您只能包含管道类的._callback_tensor_inputs
属性中列出的变量。 - max_sequence_length (
int
,默认为300
) — 与prompt
一起使用的最大序列长度。 - complex_human_instruction (
List[str]
, 可选) — 复杂人类注意力的说明:https://github.com/NVlabs/Sana/blob/main/configs/sana_app_config/Sana_1600M_app.yaml#L55。
返回
SanaPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 SanaPipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表
调用管道进行生成时调用的函数。
示例
>>> import torch
>>> from diffusers import SanaPipeline
>>> pipe = SanaPipeline.from_pretrained(
... "Efficient-Large-Model/Sana_1600M_1024px_BF16_diffusers", torch_dtype=torch.float32
... )
>>> pipe.to("cuda")
>>> pipe.text_encoder.to(torch.bfloat16)
>>> pipe.transformer = pipe.transformer.to(torch.bfloat16)
>>> image = pipe(prompt='a cyberpunk cat with a neon sign that says "Sana"')[0]
>>> image[0].save("output.png")
禁用切片 VAE 解码。如果之前启用了 enable_vae_slicing
,此方法将返回一步计算解码。
禁用平铺 VAE 解码。如果之前启用了 enable_vae_tiling
,此方法将恢复一步计算解码。
启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。
启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。
encode_prompt
< source >( prompt: typing.Union[str, typing.List[str]] do_classifier_free_guidance: bool = True negative_prompt: str = '' num_images_per_prompt: int = 1 device: typing.Optional[torch.device] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None clean_caption: bool = False max_sequence_length: int = 300 complex_human_instruction: typing.Optional[typing.List[str]] = None lora_scale: typing.Optional[float] = None )
参数
- prompt (
str
或List[str]
, 可选) — 要编码的提示。 - negative_prompt (
str
或List[str]
, 可选) — 不引导图像生成的提示。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时(即,如果guidance_scale
小于1
则忽略)。对于 PixArt-Alpha,这应该为 ""。 - do_classifier_free_guidance (
bool
, 可选, 默认为True
) — 是否使用分类器自由引导。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示应生成的图像数量。 - device — (
torch.device
, 可选): 用于放置结果嵌入的 torch 设备。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,则将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负文本嵌入。对于 Sana,它应该是 "" 字符串的嵌入。 - clean_caption (
bool
, 默认为False
) — 如果为True
,函数将在编码前预处理并清理提供的字幕。 - max_sequence_length (
int
, 默认为 300) — 用于提示的最大序列长度。 - complex_human_instruction (
list[str]
, 默认为complex_human_instruction
) — 如果complex_human_instruction
不为空,函数将使用复杂的 Human 指令作为提示。
将提示编码为文本编码器隐藏状态。
SanaPAGPipeline
class diffusers.SanaPAGPipeline
< source >( tokenizer: typing.Union[transformers.models.gemma.tokenization_gemma.GemmaTokenizer, transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast] text_encoder: Gemma2PreTrainedModel vae: AutoencoderDC transformer: SanaTransformer2DModel scheduler: FlowMatchEulerDiscreteScheduler pag_applied_layers: typing.Union[str, typing.List[str]] = 'transformer_blocks.0' )
用于使用 Sana 进行文本到图像生成的管道。此管道支持使用 扰动注意力引导 (PAG)。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = None negative_prompt: str = '' num_inference_steps: int = 20 timesteps: typing.List[int] = None sigmas: typing.List[float] = None guidance_scale: float = 4.5 num_images_per_prompt: typing.Optional[int] = 1 height: int = 1024 width: int = 1024 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True clean_caption: bool = False use_resolution_binning: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 300 complex_human_instruction: typing.List[str] = ["Given a user prompt, generate an 'Enhanced prompt' that provides detailed visual descriptions suitable for image generation. Evaluate the level of detail in the user prompt:", '- If the prompt is simple, focus on adding specifics about colors, shapes, sizes, textures, and spatial relationships to create vivid and concrete scenes.', '- If the prompt is already detailed, refine and enhance the existing details slightly without overcomplicating.', 'Here are examples of how to transform or refine prompts:', '- User Prompt: A cat sleeping -> Enhanced: A small, fluffy white cat curled up in a round shape, sleeping peacefully on a warm sunny windowsill, surrounded by pots of blooming red flowers.', '- User Prompt: A busy city street -> Enhanced: A bustling city street scene at dusk, featuring glowing street lamps, a diverse crowd of people in colorful clothing, and a double-decker bus passing by towering glass skyscrapers.', 'Please generate only the enhanced description for the prompt below and avoid including any additional commentary or evaluations:', 'User Prompt: '] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示。如果未定义,则必须传递prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可选) — 不引导图像生成的提示。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时(即,如果guidance_scale
小于1
则忽略)。 - num_inference_steps (
int
, 可选, 默认为 20) — 去噪步骤的数量。更多的去噪步骤通常会导致更高质量的图像,但推理速度会变慢。 - timesteps (
List[int]
, 可选) — 自定义时间步长,用于去噪过程,适用于其set_timesteps
方法支持timesteps
参数的调度器。如果未定义,将使用传递num_inference_steps
时的默认行为。必须按降序排列。 - sigmas (
List[float]
, 可选) — 自定义 sigma,用于去噪过程,适用于其set_timesteps
方法支持sigmas
参数的调度器。如果未定义,将使用传递num_inference_steps
时的默认行为。 - guidance_scale (
float
, 可选, 默认为 4.5) — 无分类器扩散引导 中定义的引导比例。guidance_scale
定义为 Imagen 论文 中公式 2 的w
。通过设置guidance_scale > 1
来启用引导比例。更高的引导比例鼓励生成与文本prompt
紧密相关的图像,通常以牺牲较低图像质量为代价。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示要生成的图像数量。 - height (
int
, 可选, 默认为 self.unet.config.sample_size) — 生成图像的高度(像素)。 - width (
int
, 可选, 默认为 self.unet.config.sample_size) — 生成图像的宽度(像素)。 - eta (
float
, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η):https://huggingface.co/papers/2010.02502。仅适用于 schedulers.DDIMScheduler,对其他调度器将忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch generator(s),用于使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的带噪声的潜在变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,则将使用提供的随机generator
采样生成潜在张量。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,则将从prompt
输入参数生成文本嵌入。 - prompt_attention_mask (
torch.Tensor
, 可选) — 文本嵌入的预生成注意力掩码。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负文本嵌入。对于 PixArt-Sigma,此负提示应为 ""。如果未提供,negative_prompt_embeds
将从negative_prompt
输入参数生成。 - negative_prompt_attention_mask (
torch.Tensor
, 可选) — 负文本嵌入的预生成注意力掩码。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。选择 PIL:PIL.Image.Image
或np.array
。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion.IFPipelineOutput
而不是普通元组。 - clean_caption (
bool
, 可选, 默认为True
) — 在创建嵌入之前是否清理字幕。需要安装beautifulsoup4
和ftfy
。如果未安装依赖项,将从原始提示创建嵌入。 - use_resolution_binning (
bool
默认为True
) — 如果设置为True
,请求的高度和宽度将首先使用ASPECT_RATIO_1024_BIN
映射到最接近的分辨率。生成的潜在变量被解码为图像后,它们将被调整回请求的分辨率。这对于生成非方形图像很有用。 - callback_on_step_end (
Callable
, 可选) — 推理过程中在每个去噪步骤结束时调用的函数。该函数以以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您只能包含管道类的._callback_tensor_inputs
属性中列出的变量。 - max_sequence_length (
int
,默认为 300) — 与prompt
一起使用的最大序列长度。 - complex_human_instruction (
List[str]
, 可选) — 复杂人类注意力的说明:https://github.com/NVlabs/Sana/blob/main/configs/sana_app_config/Sana_1600M_app.yaml#L55。 - pag_scale (
float
, 可选, 默认为 3.0) — 扰动注意力引导的比例因子。如果设置为 0.0,则不使用扰动注意力引导。 - pag_adaptive_scale (
float
, 可选, 默认为 0.0) — 扰动注意力引导的自适应比例因子。如果设置为 0.0,则使用pag_scale
。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表
调用管道进行生成时调用的函数。
示例
>>> import torch
>>> from diffusers import SanaPAGPipeline
>>> pipe = SanaPAGPipeline.from_pretrained(
... "Efficient-Large-Model/Sana_1600M_1024px_BF16_diffusers",
... pag_applied_layers=["transformer_blocks.8"],
... torch_dtype=torch.float32,
... )
>>> pipe.to("cuda")
>>> pipe.text_encoder.to(torch.bfloat16)
>>> pipe.transformer = pipe.transformer.to(torch.bfloat16)
>>> image = pipe(prompt='a cyberpunk cat with a neon sign that says "Sana"')[0]
>>> image[0].save("output.png")
禁用切片 VAE 解码。如果之前启用了 enable_vae_slicing
,此方法将返回一步计算解码。
禁用平铺 VAE 解码。如果之前启用了 enable_vae_tiling
,此方法将恢复一步计算解码。
启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。
启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。
encode_prompt
< source >( prompt: typing.Union[str, typing.List[str]] do_classifier_free_guidance: bool = True negative_prompt: str = '' num_images_per_prompt: int = 1 device: typing.Optional[torch.device] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None clean_caption: bool = False max_sequence_length: int = 300 complex_human_instruction: typing.Optional[typing.List[str]] = None )
参数
- prompt (
str
或List[str]
, 可选) — 要编码的提示词 - negative_prompt (
str
或List[str]
, 可选) — 不引导图像生成的提示词。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时(即,如果guidance_scale
小于1
),则忽略此参数。对于 PixArt-Alpha,此参数应为 ""。 - do_classifier_free_guidance (
bool
, 可选, 默认为True
) — 是否使用无分类器引导 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示词应生成的图像数量 - device — (
torch.device
, 可选): 将生成的嵌入放置在哪个 torch 设备上 - prompt_embeds (
torch.Tensor
, 可选) — 预先生成的文本嵌入。可用于轻松调整文本输入,例如提示词加权。如果未提供,将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预先生成的负面文本嵌入。对于 Sana,它应该是“ ”字符串的嵌入。 - clean_caption (
bool
, 默认为False
) — 如果为True
,函数将在编码前预处理和清理提供的标题。 - max_sequence_length (
int
, 默认为 300) — 用于提示词的最大序列长度。 - complex_human_instruction (
list[str]
, 默认为complex_human_instruction
) — 如果complex_human_instruction
不为空,函数将使用复杂的“人类指令”作为提示词。
将提示编码为文本编码器隐藏状态。
SanaPipelineOutput
class diffusers.pipelines.sana.pipeline_output.SanaPipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
Sana 管道的输出类。