Diffusers documentation

PriorTransformer

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

PriorTransformer

Prior Transformer 最初由 Ramesh 等人在 Hierarchical Text-Conditional Image Generation with CLIP Latents 中提出。它用于根据 CLIP 文本嵌入预测 CLIP 图像嵌入;图像嵌入通过去噪扩散过程进行预测。

该论文的摘要如下:

CLIP 等对比模型已被证明能够学习捕获图像语义和风格的鲁棒表示。为了将这些表示用于图像生成,我们提出了一种两阶段模型:一个先验模型,根据文本描述生成 CLIP 图像嵌入;以及一个解码器,根据图像嵌入生成图像。我们表明,显式生成图像表示可以在几乎不损失照片真实感和描述相似度的情况下提高图像多样性。基于图像表示进行条件生成的解码器还可以生成图像的变体,在保留其语义和风格的同时,改变图像表示中不存在的非关键细节。此外,CLIP 的联合嵌入空间支持以零样本方式进行语言引导的图像操作。我们使用扩散模型作为解码器,并分别使用自回归模型和扩散模型作为先验模型进行实验,发现后者在计算上更高效,且能生成更高质量的样本。

PriorTransformer

类 diffusers.PriorTransformer

< >

( num_attention_heads: int = 32attention_head_dim: int = 64num_layers: int = 20embedding_dim: int = 768num_embeddings = 77additional_embeddings = 4dropout: float = 0.0time_embed_act_fn: str = 'silu'norm_in_type: str | None = Noneembedding_proj_norm_type: str | None = Noneencoder_hid_proj_type: str | None = 'linear'added_emb_type: str | None = 'prd'time_embed_dim: int | None = Noneembedding_proj_dim: int | None = Noneclip_embed_dim: int | None = None )

参数

  • num_attention_heads (int,可选,默认值为 32) — 多头注意力机制中使用的注意力头数量。
  • attention_head_dim (int,可选,默认为 64) — 每个注意力头中的通道数。
  • num_layers (int,可选,默认为 20) — 要使用的 Transformer 块层数。
  • embedding_dim (int,可选,默认值为 768) — 模型输入 hidden_states 的维度
  • num_embeddings (int,可选,默认值为 77) — 模型输入 hidden_states 的嵌入数量
  • additional_embeddings (int,可选,默认为 4) — 附加到投影后的 hidden_states 末尾的额外 token 数量。实际使用的 hidden_states 长度为 num_embeddings + additional_embeddings。
  • dropout (float,可选,默认值为 0.0) — 要使用的 dropout 概率。
  • time_embed_act_fn (str,可选,默认为 ‘silu’) — 用于创建时间步嵌入的激活函数。
  • norm_in_type (str,可选,默认为 None) — 在将隐藏状态传入 Transformer 块之前应用的归一化层。如果不需要归一化,请将其设置为 None。
  • embedding_proj_norm_type (str,可选,默认为 None) — 应用于输入 proj_embedding 的归一化层。如果不需要归一化,请将其设置为 None。
  • encoder_hid_proj_type (str, 可选,默认为 linear) — 应用于输入 encoder_hidden_states 的投影层。如果 encoder_hidden_states 为 None,请将其设置为 None。
  • added_emb_type (str,可选,默认为 prd) — 用于条件化模型的附加嵌入。 可选值为 prd 或 None。如果选择 prd,则会在前面添加一个标记,表示文本嵌入与图像嵌入之间的(量化)点积,如 unclip 论文https://huggingface.co/papers/2204.06125中所述。如果为 None,则不会在前面添加任何附加嵌入。
  • time_embed_dim (int, *optional*, defaults to None) -- The dimension of timestep embeddings. If None, will be set to num_attention_heads * attention_head_dim`
  • embedding_proj_dim (int,可选,默认为 None) — proj_embedding 的维度。如果为 None,则设置为 embedding_dim。
  • clip_embed_dim (int,可选,默认为 None) — 输出的维度。如果为 None,则设置为 embedding_dim。

A Prior Transformer 模型。

forward

< >

( hidden_statestimestep: typing.Union[torch.Tensor, float, int]proj_embedding: Tensorencoder_hidden_states: typing.Optional[torch.Tensor] = Noneattention_mask: typing.Optional[torch.BoolTensor] = Nonereturn_dict: bool = True ) → PriorTransformerOutput or tuple

参数

  • hidden_states(形状为 (batch_size, embedding_dim) 的 torch.Tensor)— 当前预测的图像嵌入。
  • timestep (torch.LongTensor) — 当前去噪步骤。
  • proj_embedding(形状为 (batch_size, embedding_dim) 的 torch.Tensor)— 去噪过程所依赖的投影嵌入向量。
  • encoder_hidden_states (torch.Tensor,形状为 (batch_size, num_embeddings, embedding_dim)) — 去噪过程所依赖的文本嵌入的隐藏状态。
  • attention_mask(形状为 (batch_size, num_embeddings) 的 torch.BoolTensor)— 文本嵌入的文本掩码。
  • return_dict (bool,可选,默认为 True) — 是否返回 PriorTransformerOutput 而不是普通的元组。

返回值

PriorTransformerOutput 或 tuple

If return_dict is True, a PriorTransformerOutput is returned, otherwise a tuple is returned where the first element is the sample tensor.

PriorTransformer 的前向方法。

set_default_attn_processor

< >

( )

禁用自定义注意力处理器并设置默认的注意力实现。

PriorTransformerOutput

类 diffusers.models.transformers.prior_transformer.PriorTransformerOutput

< >

( predicted_image_embedding: Tensor )

参数

  • predicted_image_embedding (torch.Tensor,形状为 (batch_size, embedding_dim)) — 基于 CLIP 文本嵌入输入条件预测得到的 CLIP 图像嵌入。

PriorTransformer 的输出。

Update on GitHub