Make sure to check out the Schedulers guide to learn how to explore the tradeoff between scheduler speed and quality, and see the reuse components across pipelines section to learn how to efficiently load the same components into multiple pipelines.
This pipeline was contributed by zRzRzRzRzRzRzR. The original codebase can be found here. The original weights can be found under hf.co/THUDM.
( tokenizer: AutoTokenizer text_encoder: GlmModel vae: AutoencoderKL transformer: CogView4Transformer2DModel scheduler: FlowMatchEulerDiscreteScheduler )
Parameters
GLMModel
) —
Frozen text-encoder. CogView4 uses glm-4-9b-hf. PreTrainedTokenizer
) —
Tokenizer of class
PreTrainedTokenizer. CogView4Transformer2DModel
to denoise the encoded image latents. transformer
to denoise the encoded image latents. Pipeline for text-to-image generation using CogView4.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
( 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
or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). int
, optional, defaults to self.transformer.config.sample_size * self.vae_scale_factor) —
The height in pixels of the generated image. If not provided, it is set to 1024. int
, optional, defaults to self.transformer.config.sample_size * self.vae_scale_factor) —
The width in pixels of the generated image. If not provided it is set to 1024. int
, optional, defaults to 50
) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional, defaults to 5.0
) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. int
, optional, defaults to 1
) —
The number of images to generate per prompt. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.FloatTensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. torch.FloatTensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. Tuple[int]
, optional, defaults to (1024, 1024)) —
If original_size
is not the same as target_size
the image will appear to be down- or upsampled.
original_size
defaults to (height, width)
if not specified. Part of SDXL’s micro-conditioning as
explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (0, 0)) —
crops_coords_top_left
can be used to generate an image that appears to be “cropped” from the position
crops_coords_top_left
downwards. Favorable, well-centered images are usually achieved by setting
crops_coords_top_left
to (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
instead
of a plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined under
self.processor
in
diffusers.models.attention_processor. Callable
, optional) —
A function that calls at the end of each denoising steps during the inference. The function is called
with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a list of all tensors as specified by
callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. int
, defaults to 224
) —
Maximum sequence length in encoded prompt. Can be set to other values but may lead to poorer results. Returns
~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
or tuple
~pipelines.cogview4.pipeline_CogView4.CogView4PipelineOutput
if return_dict
is True, otherwise a
tuple
. When returning a tuple, the first element is a list with the generated images.
Function invoked when calling the pipeline for generation.
Examples:
>>> 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")
( 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 )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). bool
, optional, defaults to True
) —
Whether to use classifier free guidance or not. int
, optional, defaults to 1) —
Number of images that should be generated per prompt. torch device to place the resulting embeddings on torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.device
, optional):
torch device torch.dtype
, optional):
torch dtype int
, defaults to 1024
) —
Maximum sequence length in encoded prompt. Can be set to other values but may lead to poorer results. Encodes the prompt into text encoder hidden states.
( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
Output class for CogView3 pipelines.