Diffusers 文档
CogView4
并获得增强的文档体验
开始使用
CogView4
此管道由zRzRzRzRzRzRzR贡献。原始代码库可在此处找到。原始权重可在hf.co/THUDM找到。
CogView4Pipeline
class diffusers.CogView4Pipeline
< 来源 >( tokenizer: AutoTokenizer text_encoder: GlmModel vae: AutoencoderKL transformer: CogView4Transformer2DModel scheduler: FlowMatchEulerDiscreteScheduler )
参数
- vae (AutoencoderKL) — 变分自动编码器 (VAE) 模型,用于将图像编码和解码为潜在表示。
- text_encoder (
GLMModel
) — 冻结的文本编码器。CogView4 使用 glm-4-9b-hf。 - tokenizer (
PreTrainedTokenizer
) — PreTrainedTokenizer 类别的分词器。 - transformer (CogView4Transformer2DModel) — 一个文本条件
CogView4Transformer2DModel
,用于对编码图像潜在表示进行去噪。 - scheduler (SchedulerMixin) — 与
transformer
结合使用以对编码图像潜在表示进行去噪的调度器。
使用 CogView4 进行文本到图像生成的管道。
该模型继承自DiffusionPipeline。请查阅超类文档,了解库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等)。
__call__
< 来源 >( prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 timesteps: typing.Optional[typing.List[int]] = None sigmas: typing.Optional[typing.List[float]] = None guidance_scale: float = 5.0 num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.FloatTensor] = None prompt_embeds: typing.Optional[torch.FloatTensor] = None negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None original_size: typing.Optional[typing.Tuple[int, int]] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) output_type: str = 'pil' return_dict: bool = True attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 1024 ) → ~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示或提示列表。如果未定义,则必须传递prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可选) — 用于不引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时(即,如果guidance_scale
小于1
时),将被忽略。 - height (
int
, 可选, 默认为 self.transformer.config.sample_size * self.vae_scale_factor) — 生成图像的高度(像素)。如果未提供,则设置为 1024。 - width (
int
, 可选, 默认为 self.transformer.config.sample_size * self.vae_scale_factor) — 生成图像的宽度(像素)。如果未提供,则设置为 1024。 - num_inference_steps (
int
, 可选, 默认为50
) — 去噪步骤数。更多的去噪步骤通常会带来更高质量的图像,但推理速度会变慢。 - timesteps (
List[int]
, 可选) — 用于去噪过程的自定义时间步长,适用于支持set_timesteps
方法中timesteps
参数的调度器。如果未定义,将使用传递num_inference_steps
时的默认行为。必须按降序排列。 - sigmas (
List[float]
, 可选) — 用于去噪过程的自定义 sigmas,适用于支持set_timesteps
方法中sigmas
参数的调度器。如果未定义,将使用传递num_inference_steps
时的默认行为。 - guidance_scale (
float
, 可选, 默认为5.0
) — Classifier-Free Diffusion Guidance 中定义的引导比例。guidance_scale
定义为 Imagen Paper 方程 2 中的w
。通过设置guidance_scale > 1
启用引导比例。较高的引导比例鼓励生成与文本prompt
紧密相关的图像,通常以牺牲较低图像质量为代价。 - num_images_per_prompt (
int
, 可选, 默认为1
) — 每个提示生成的图像数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch 生成器,用于使生成具有确定性。 - latents (
torch.FloatTensor
, 可选) — 预生成的噪声潜在量,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示调整相同生成。如果未提供,将使用提供的随机generator
采样生成一个潜在量张量。 - prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,文本嵌入将从prompt
输入参数生成。 - negative_prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,负面提示嵌入将从negative_prompt
输入参数生成。 - original_size (
Tuple[int]
, 可选, 默认为 (1024, 1024)) — 如果original_size
与target_size
不同,图像将显示为缩小或放大。如果未指定,original_size
默认为(height, width)
。SDXL 微调的一部分,如 https://huggingface.ac.cn/papers/2307.01952 第 2.2 节所述。 - crops_coords_top_left (
Tuple[int]
, 可选, 默认为 (0, 0)) —crops_coords_top_left
可用于生成一个看起来像是从crops_coords_top_left
位置向下“裁剪”的图像。通常通过将crops_coords_top_left
设置为 (0, 0) 来获得有利的、居中的图像。SDXL 微调的一部分,如 https://huggingface.ac.cn/papers/2307.01952 第 2.2 节所述。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通元组。 - attention_kwargs (
dict
, optional) — 一个可选的 kwargs 字典,如果指定,将作为参数传递给self.processor
中定义的AttentionProcessor
,详见 diffusers.models.attention_processor。 - callback_on_step_end (
Callable
, optional) — 在推理过程中,每个去噪步骤结束时调用的函数。该函数通过以下参数调用: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
, optional) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您只能包含管道类._callback_tensor_inputs
属性中列出的变量。 - max_sequence_length (
int
, 默认为224
) — 编码提示中的最大序列长度。可以设置为其他值,但可能会导致较差的结果。
返回
~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
或 tuple
如果 return_dict
为 True,则返回 ~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
,否则返回 tuple
。返回元组时,第一个元素是生成的图像列表。
调用管道进行生成时调用的函数。
示例
>>> import torch
>>> from diffusers import CogView4Pipeline
>>> pipe = CogView4Pipeline.from_pretrained("THUDM/CogView4-6B", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> prompt = "A photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt).images[0]
>>> image.save("output.png")
encode_prompt
< source >( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_images_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None max_sequence_length: int = 1024 )
参数
- prompt (
str
或List[str]
, 可选) — 待编码的提示。 - negative_prompt (
str
或List[str]
, 可选) — 不用于指导图像生成的提示。如果未定义,则必须传递negative_prompt_embeds
。当不使用指导时(即,如果guidance_scale
小于1
),则忽略此参数。 - do_classifier_free_guidance (
bool
, 可选, 默认为True
) — 是否使用无分类器指导。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示应生成的图像数量。用于放置结果嵌入的 torch 设备。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,负提示嵌入将从negative_prompt
输入参数生成。 - device — (
torch.device
, 可选): torch 设备。 - dtype — (
torch.dtype
, 可选): torch 数据类型。 - max_sequence_length (
int
, 默认为1024
) — 编码提示中的最大序列长度。可以设置为其他值,但可能会导致较差的结果。
将提示编码为文本编码器隐藏状态。
CogView4PipelineOutput
class diffusers.pipelines.cogview4.pipeline_output.CogView4PipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
CogView3 管道的输出类。