Diffusers 文件
CogVideoX
並獲得增強的文件體驗
開始使用
CogVideoX
CogVideoX是一個大型擴散Transformer模型,擁有2B和5B引數,旨在從文字生成更長、更一致的影片。該模型使用3D因果變分自編碼器更高效地處理影片資料,透過減少序列長度(以及相關的訓練計算)並防止生成影片中的閃爍。一個帶有自適應LayerNorm的“專家”Transformer改進了文字與影片之間的對齊,而3D全注意力有助於準確捕捉生成影片中的運動和時間。
您可以在CogVideoX集合中找到所有原始的CogVideoX檢查點。
點選右側邊欄中的CogVideoX模型,檢視其他影片生成任務的更多示例。
以下示例演示瞭如何生成針對記憶體或推理速度進行最佳化的影片。
有關各種記憶體節省技術的更多詳細資訊,請參閱減少記憶體使用指南。
下方量化的CogVideoX 5B模型需要約16GB的VRAM。
import torch
from diffusers import CogVideoXPipeline, AutoModel
from diffusers.quantizers import PipelineQuantizationConfig
from diffusers.hooks import apply_group_offloading
from diffusers.utils import export_to_video
# quantize weights to int8 with torchao
pipeline_quant_config = PipelineQuantizationConfig(
quant_backend="torchao",
quant_kwargs={"quant_type": "int8wo"},
components_to_quantize=["transformer"]
)
# fp8 layerwise weight-casting
transformer = AutoModel.from_pretrained(
"THUDM/CogVideoX-5b",
subfolder="transformer",
torch_dtype=torch.bfloat16
)
transformer.enable_layerwise_casting(
storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16
)
pipeline = CogVideoXPipeline.from_pretrained(
"THUDM/CogVideoX-5b",
transformer=transformer,
quantization_config=pipeline_quant_config,
torch_dtype=torch.bfloat16
)
pipeline.to("cuda")
# model-offloading
pipeline.enable_model_cpu_offload()
prompt = """
A detailed wooden toy ship with intricately carved masts and sails is seen gliding smoothly over a plush, blue carpet that mimics the waves of the sea.
The ship's hull is painted a rich brown, with tiny windows. The carpet, soft and textured, provides a perfect backdrop, resembling an oceanic expanse.
Surrounding the ship are various other toys and children's items, hinting at a playful environment. The scene captures the innocence and imagination of childhood,
with the toy ship's journey symbolizing endless adventures in a whimsical, indoor setting.
"""
video = pipeline(
prompt=prompt,
guidance_scale=6,
num_inference_steps=50
).frames[0]
export_to_video(video, "output.mp4", fps=8)
注意事項
CogVideoX支援使用load_lora_weights()載入LoRA。
顯示示例程式碼
import torch from diffusers import CogVideoXPipeline from diffusers.hooks import apply_group_offloading from diffusers.utils import export_to_video pipeline = CogVideoXPipeline.from_pretrained( "THUDM/CogVideoX-5b", torch_dtype=torch.bfloat16 ) pipeline.to("cuda") # load LoRA weights pipeline.load_lora_weights("finetrainers/CogVideoX-1.5-crush-smol-v0", adapter_name="crush-lora") pipeline.set_adapters("crush-lora", 0.9) # model-offloading pipeline.enable_model_cpu_offload() prompt = """ PIKA_CRUSH A large metal cylinder is seen pressing down on a pile of Oreo cookies, flattening them as if they were under a hydraulic press. """ negative_prompt = "inconsistent motion, blurry motion, worse quality, degenerate outputs, deformed outputs" video = pipeline( prompt=prompt, negative_prompt=negative_prompt, num_frames=81, height=480, width=768, num_inference_steps=50 ).frames[0] export_to_video(video, "output.mp4", fps=16)
文字到影片 (T2V) 檢查點在1360x768解析度下效果最佳,因為它是在此解析度下預訓練的。
影像到影片 (I2V) 檢查點支援多種解析度。寬度可以在768到1360之間變化,但高度必須是758。高度和寬度都必須能被16整除。
T2V和I2V檢查點在81幀和161幀下效果最佳。建議以16fps匯出生成的影片。
請參考下表檢視啟用各種記憶體節省技術時的記憶體使用情況。
方法 記憶體使用(已啟用) 記憶體使用(已停用) enable_model_cpu_offload 19GB 33GB enable_sequential_cpu_offload <4GB ~33GB(推理速度非常慢) enable_tiling 11GB(啟用enable_model_cpu_offload) ---
CogVideoXPipeline
class diffusers.CogVideoXPipeline
< 源 >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLCogVideoX transformer: CogVideoXTransformer3DModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim_cogvideox.CogVideoXDDIMScheduler, diffusers.schedulers.scheduling_dpm_cogvideox.CogVideoXDPMScheduler] )
引數
- vae (AutoencoderKL) — 用於將影片編碼和解碼為潛在表示的變分自編碼器(VAE)模型。
- text_encoder (
T5EncoderModel
) — 凍結文字編碼器。CogVideoX使用T5;具體是t5-v1_1-xxl變體。 - tokenizer (
T5Tokenizer
) — T5Tokenizer類的分詞器。 - transformer (CogVideoXTransformer3DModel) — 一個文字條件
CogVideoXTransformer3DModel
,用於對編碼後的影片潛在表示進行去噪。 - scheduler (SchedulerMixin) — 與
transformer
結合使用,用於對編碼後的影片潛在表示進行去噪的排程器。
用於使用CogVideoX進行文字到影片生成的管道。
此模型繼承自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_frames: typing.Optional[int] = None num_inference_steps: int = 50 timesteps: typing.Optional[typing.List[int]] = None guidance_scale: float = 6 use_dynamic_cfg: bool = False num_videos_per_prompt: int = 1 eta: float = 0.0 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 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 = 226 ) → CogVideoXPipelineOutput 或 tuple
引數
- prompt (
str
或List[str]
, 可選) — 用於引導影像生成的提示或提示列表。如果未定義,則必須傳入prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可選) — 用於不引導影像生成的提示或提示列表。如果未定義,則必須傳入negative_prompt_embeds
。當不使用引導時(即,如果guidance_scale
小於1
時),則忽略。 - height (
int
, 可選, 預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的高度(畫素)。預設設定為480以獲得最佳效果。 - width (
int
, 可選, 預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的寬度(畫素)。預設設定為720以獲得最佳效果。 - num_frames (
int
, 預設為48
) — 生成的幀數。必須能被self.vae_scale_factor_temporal整除。生成的影片將包含1個額外幀,因為CogVideoX是基於 (num_seconds * fps + 1) 幀進行條件化的,其中num_seconds為6,fps為8。然而,由於影片可以以任何fps儲存,唯一需要滿足的條件是上述的可整除性。 - num_inference_steps (
int
, 可選, 預設為 50) — 去噪步數。更多去噪步數通常會帶來更高質量的影像,但推理速度會變慢。 - timesteps (
List[int]
, 可選) — 用於去噪過程的自定義時間步長,適用於在set_timesteps
方法中支援timesteps
引數的排程器。如果未定義,將使用傳入num_inference_steps
時的預設行為。必須按降序排列。 - guidance_scale (
float
, 可選, 預設為 7.0) — Classifier-Free Diffusion Guidance中定義的引導比例。guidance_scale
定義為Imagen Paper方程2中的w
。透過設定guidance_scale > 1
啟用引導比例。更高的引導比例會鼓勵生成與文字prompt
更緊密相關的影像,通常會以犧牲影像質量為代價。 - num_videos_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
輸入引數生成。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。在PIL:PIL.Image.Image
或np.array
之間選擇。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是純元組。 - attention_kwargs (
dict
, 可選) — 一個kwargs字典,如果指定,則作為self.processor
下定義的AttentionProcessor
的引數傳入diffusers.models.attention_processor。 - 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
, 預設為226
) — 編碼提示中的最大序列長度。必須與self.transformer.config.max_text_seq_length
保持一致,否則可能導致結果不佳。
返回
CogVideoXPipelineOutput 或 tuple
如果return_dict
為True,則返回CogVideoXPipelineOutput,否則返回tuple
。當返回元組時,第一個元素是生成的影像列表。
呼叫管道進行生成時呼叫的函式。
示例
>>> import torch
>>> from diffusers import CogVideoXPipeline
>>> from diffusers.utils import export_to_video
>>> # Models: "THUDM/CogVideoX-2b" or "THUDM/CogVideoX-5b"
>>> pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-2b", torch_dtype=torch.float16).to("cuda")
>>> prompt = (
... "A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. "
... "The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other "
... "pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, "
... "casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. "
... "The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical "
... "atmosphere of this unique musical performance."
... )
>>> video = pipe(prompt=prompt, guidance_scale=6, num_inference_steps=50).frames[0]
>>> export_to_video(video, "output.mp4", fps=8)
encode_prompt
< 源 >( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 226 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )
引數
- prompt (
str
或List[str]
, 可選) — 要編碼的提示。 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳入negative_prompt_embeds
。在不使用引導時(即,如果guidance_scale
小於1
時),將被忽略。 - do_classifier_free_guidance (
bool
, 可選, 預設為True
) — 是否使用分類器自由引導。 - num_videos_per_prompt (
int
, 可選, 預設為 1) — 每個提示應生成的影片數量。生成嵌入的 torch 裝置。 - prompt_embeds (
torch.Tensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預生成的負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds
將從negative_prompt
輸入引數生成。 - device — (
torch.device
, 可選): torch 裝置 - dtype — (
torch.dtype
, 可選): torch 資料型別
將提示編碼為文字編碼器隱藏狀態。
啟用融合的 QKV 投影。
如果啟用了 QKV 投影融合,則停用它。
CogVideoXImageToVideoPipeline
class diffusers.CogVideoXImageToVideoPipeline
< 來源 >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLCogVideoX transformer: CogVideoXTransformer3DModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim_cogvideox.CogVideoXDDIMScheduler, diffusers.schedulers.scheduling_dpm_cogvideox.CogVideoXDPMScheduler] )
引數
- vae (AutoencoderKL) — 變分自編碼器(VAE)模型,用於將影片編碼和解碼為潛在表示。
- text_encoder (
T5EncoderModel
) — 凍結文字編碼器。CogVideoX 使用 T5;特別是 t5-v1_1-xxl 變體。 - tokenizer (
T5Tokenizer
) —T5Tokenizer
類的分詞器。 - transformer (CogVideoXTransformer3DModel) — 用於對編碼影片潛在表示進行去噪的文字條件
CogVideoXTransformer3DModel
。 - scheduler (SchedulerMixin) — 與
transformer
結合使用的排程器,用於對編碼影片潛在表示進行去噪。
使用 CogVideoX 進行影像到影片生成的管道。
此模型繼承自DiffusionPipeline。請查閱超類文件,瞭解庫為所有管道實現通用方法(例如下載或儲存、在特定裝置上執行等)。
__call__
< 來源 >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] 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_frames: int = 49 num_inference_steps: int = 50 timesteps: typing.Optional[typing.List[int]] = None guidance_scale: float = 6 use_dynamic_cfg: bool = False num_videos_per_prompt: int = 1 eta: float = 0.0 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 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 = 226 ) → CogVideoXPipelineOutput 或 tuple
引數
- image (
PipelineImageInput
) — 用於調節生成的輸入影像。必須是影像、影像列表或torch.Tensor
。 - prompt (
str
或List[str]
, 可選) — 用於引導影像生成的提示。如果未定義,則必須傳入prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳入negative_prompt_embeds
。在不使用引導時(即,如果guidance_scale
小於1
時),將被忽略。 - height (
int
, 可選, 預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的畫素高度。為獲得最佳結果,預設設定為 480。 - width (
int
, 可選, 預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的畫素寬度。為獲得最佳結果,預設設定為 720。 - num_frames (
int
, 預設為48
) — 要生成的幀數。必須可被 self.vae_scale_factor_temporal 整除。生成的影片將包含 1 個額外幀,因為 CogVideoX 使用 (num_seconds * fps + 1) 幀進行條件設定,其中 num_seconds 為 6,fps 為 8。但是,由於影片可以以任何 fps 儲存,唯一需要滿足的條件是上述可整除性。 - num_inference_steps (
int
, 可選, 預設為 50) — 去噪步數。更多去噪步數通常會帶來更高質量的影像,但推理速度會變慢。 - timesteps (
List[int]
, 可選) — 用於去噪過程的自定義時間步長,適用於在其set_timesteps
方法中支援timesteps
引數的排程器。如果未定義,將使用傳入num_inference_steps
時的預設行為。必須按降序排列。 - guidance_scale (
float
, 可選, 預設為 7.0) — Classifier-Free Diffusion Guidance 中定義的引導比例。guidance_scale
定義為 Imagen Paper 方程 2 中的w
。透過設定guidance_scale > 1
啟用引導比例。更高的引導比例鼓勵生成與文字prompt
緊密相關的影像,通常以犧牲影像質量為代價。 - num_videos_per_prompt (
int
, 可選, 預設為 1) — 每個提示要生成的影片數量。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個或多個 torch generator(s),用於使生成具有確定性。 - latents (
torch.FloatTensor
, 可選) — 預生成的噪聲潛在表示,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同提示調整相同的生成。如果未提供,將使用提供的隨機generator
取樣生成一個潛在張量。 - prompt_embeds (
torch.FloatTensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.FloatTensor
, 可選) — 預生成的負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds
將從negative_prompt
輸入引數生成。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。在 PIL:PIL.Image.Image
或np.array
之間選擇。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通元組。 - attention_kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,將作為self.processor
中定義的AttentionProcessor
的引數傳遞到 diffusers.models.attention_processor。 - 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
, 預設為226
) — 編碼提示中的最大序列長度。必須與self.transformer.config.max_text_seq_length
保持一致,否則可能導致結果不佳。
返回
CogVideoXPipelineOutput 或 tuple
如果return_dict
為True,則返回CogVideoXPipelineOutput,否則返回tuple
。當返回元組時,第一個元素是生成的影像列表。
呼叫管道進行生成時呼叫的函式。
示例
>>> import torch
>>> from diffusers import CogVideoXImageToVideoPipeline
>>> from diffusers.utils import export_to_video, load_image
>>> pipe = CogVideoXImageToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b-I2V", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> prompt = "An astronaut hatching from an egg, on the surface of the moon, the darkness and depth of space realised in the background. High quality, ultrarealistic detail and breath-taking movie-like camera shot."
>>> image = load_image(
... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
... )
>>> video = pipe(image, prompt, use_dynamic_cfg=True)
>>> export_to_video(video.frames[0], "output.mp4", fps=8)
encode_prompt
< 來源 >( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 226 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )
引數
- prompt (
str
或List[str]
, 可選) — 要編碼的提示。 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳入negative_prompt_embeds
。在不使用引導時(即,如果guidance_scale
小於1
時),將被忽略。 - do_classifier_free_guidance (
bool
, 可選, 預設為True
) — 是否使用分類器自由引導。 - num_videos_per_prompt (
int
, 可選, 預設為 1) — 每個提示應生成的影片數量。生成嵌入的 torch 裝置。 - prompt_embeds (
torch.Tensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預生成的負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds
將從negative_prompt
輸入引數生成。 - device — (
torch.device
, 可選): torch 裝置 - dtype — (
torch.dtype
, 可選): torch 資料型別
將提示編碼為文字編碼器隱藏狀態。
啟用融合的 QKV 投影。
如果啟用了 QKV 投影融合,則停用它。
CogVideoXVideoToVideoPipeline
class diffusers.CogVideoXVideoToVideoPipeline
< 來源 >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLCogVideoX transformer: CogVideoXTransformer3DModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim_cogvideox.CogVideoXDDIMScheduler, diffusers.schedulers.scheduling_dpm_cogvideox.CogVideoXDPMScheduler] )
引數
- vae (AutoencoderKL) — 變分自編碼器(VAE)模型,用於將影片編碼和解碼為潛在表示。
- text_encoder (
T5EncoderModel
) — 凍結文字編碼器。CogVideoX 使用 T5;特別是 t5-v1_1-xxl 變體。 - tokenizer (
T5Tokenizer
) —T5Tokenizer
類的分詞器。 - transformer (CogVideoXTransformer3DModel) — 用於對編碼影片潛在表示進行去噪的文字條件
CogVideoXTransformer3DModel
。 - scheduler (SchedulerMixin) — 與
transformer
結合使用的排程器,用於對編碼影片潛在表示進行去噪。
使用 CogVideoX 進行影片到影片生成的管道。
此模型繼承自DiffusionPipeline。請查閱超類文件,瞭解庫為所有管道實現通用方法(例如下載或儲存、在特定裝置上執行等)。
__call__
< 來源 >( video: typing.List[PIL.Image.Image] = None 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 strength: float = 0.8 guidance_scale: float = 6 use_dynamic_cfg: bool = False num_videos_per_prompt: int = 1 eta: float = 0.0 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 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 = 226 ) → CogVideoXPipelineOutput or tuple
引數
- video (
List[PIL.Image.Image]
) — 用於條件生成輸入的影片。必須是影片的影像/幀列表。 - prompt (
str
或List[str]
,可選) — 用於引導影像生成的提示或提示列表。如果未定義,則必須傳入prompt_embeds
。 - negative_prompt (
str
或List[str]
,可選) — 用於不引導影像生成的提示或提示列表。如果未定義,則必須傳入negative_prompt_embeds
。當不使用指導時(即guidance_scale
小於1
時),該引數將被忽略。 - height (
int
,可選,預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的高度(畫素)。為了獲得最佳結果,預設設定為 480。 - width (
int
,可選,預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的寬度(畫素)。為了獲得最佳結果,預設設定為 720。 - num_inference_steps (
int
,可選,預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但推理速度會變慢。 - timesteps (
List[int]
,可選) — 用於去噪過程的自定義時間步長,適用於在其set_timesteps
方法中支援timesteps
引數的排程器。如果未定義,將使用傳入num_inference_steps
時的預設行為。必須按降序排列。 - strength (
float
,可選,預設為 0.8) — 強度越高,原始影片與生成影片之間的差異越大。 - guidance_scale (
float
,可選,預設為 7.0) — 如 Classifier-Free Diffusion Guidance 中所定義的指導比例。guidance_scale
被定義為 Imagen Paper 中公式 2 的w
。透過設定guidance_scale > 1
啟用指導比例。更高的指導比例會促使生成與文字prompt
更緊密相關的影像,但通常會犧牲影像質量。 - num_videos_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_embeds
將從negative_prompt
輸入引數生成。 - output_type (
str
,可選,預設為"pil"
) — 生成影像的輸出格式。在 PIL:PIL.Image.Image
或np.array
之間選擇。 - return_dict (
bool
,可選,預設為True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通元組。 - attention_kwargs (
dict
,可選) — 一個 kwargs 字典,如果指定,則會傳遞給 diffusers.models.attention_processor 中self.processor
下定義的AttentionProcessor
。 - 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
,預設為226
) — 編碼提示中的最大序列長度。必須與self.transformer.config.max_text_seq_length
保持一致,否則可能導致結果不佳。
返回
CogVideoXPipelineOutput 或 tuple
如果return_dict
為True,則返回CogVideoXPipelineOutput,否則返回tuple
。當返回元組時,第一個元素是生成的影像列表。
呼叫管道進行生成時呼叫的函式。
示例
>>> import torch
>>> from diffusers import CogVideoXDPMScheduler, CogVideoXVideoToVideoPipeline
>>> from diffusers.utils import export_to_video, load_video
>>> # Models: "THUDM/CogVideoX-2b" or "THUDM/CogVideoX-5b"
>>> pipe = CogVideoXVideoToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> pipe.scheduler = CogVideoXDPMScheduler.from_config(pipe.scheduler.config)
>>> input_video = load_video(
... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/hiker.mp4"
... )
>>> prompt = (
... "An astronaut stands triumphantly at the peak of a towering mountain. Panorama of rugged peaks and "
... "valleys. Very futuristic vibe and animated aesthetic. Highlights of purple and golden colors in "
... "the scene. The sky is looks like an animated/cartoonish dream of galaxies, nebulae, stars, planets, "
... "moons, but the remainder of the scene is mostly realistic."
... )
>>> video = pipe(
... video=input_video, prompt=prompt, strength=0.8, guidance_scale=6, num_inference_steps=50
... ).frames[0]
>>> export_to_video(video, "output.mp4", fps=8)
encode_prompt
< 原始檔 >( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 226 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )
引數
- prompt (
str
或List[str]
,可選) — 待編碼的提示 - negative_prompt (
str
或List[str]
,可選) — 用於不引導影像生成的提示或提示列表。如果未定義,則必須傳入negative_prompt_embeds
。當不使用指導時(即guidance_scale
小於1
時),該引數將被忽略。 - do_classifier_free_guidance (
bool
,可選,預設為True
) — 是否使用分類器自由指導。 - num_videos_per_prompt (
int
,可選,預設為 1) — 每個提示應生成的影片數量。用於放置結果嵌入的 torch 裝置。 - prompt_embeds (
torch.Tensor
,可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.Tensor
,可選) — 預先生成的負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds
將從negative_prompt
輸入引數生成。 - device — (
torch.device
,可選): torch 裝置 - dtype — (
torch.dtype
,可選): torch 資料型別
將提示編碼為文字編碼器隱藏狀態。
啟用融合的 QKV 投影。
如果啟用了 QKV 投影融合,則停用它。
CogVideoXFunControlPipeline
class diffusers.CogVideoXFunControlPipeline
< 原始檔 >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLCogVideoX transformer: CogVideoXTransformer3DModel scheduler: KarrasDiffusionSchedulers )
引數
- vae (AutoencoderKL) — 變分自編碼器 (VAE) 模型,用於將影片編碼和解碼為潛在表示。
- text_encoder (
T5EncoderModel
) — 凍結的文字編碼器。CogVideoX 使用 T5;具體來說是 t5-v1_1-xxl 變體。 - tokenizer (
T5Tokenizer
) — T5Tokenizer 類的分詞器。 - transformer (CogVideoXTransformer3DModel) — 一個文字條件
CogVideoXTransformer3DModel
,用於對編碼的影片潛在變數進行去噪。 - scheduler (SchedulerMixin) — 與
transformer
結合使用的排程器,用於對編碼的影片潛在變數進行去噪。
用於使用 CogVideoX Fun 進行受控文字到影片生成的管道。
此模型繼承自DiffusionPipeline。請查閱超類文件,瞭解庫為所有管道實現通用方法(例如下載或儲存、在特定裝置上執行等)。
__call__
< 原始檔 >( prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None control_video: typing.Optional[typing.List[PIL.Image.Image]] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 timesteps: typing.Optional[typing.List[int]] = None guidance_scale: float = 6 use_dynamic_cfg: bool = False num_videos_per_prompt: int = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None control_video_latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None 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 = 226 ) → CogVideoXPipelineOutput 或 tuple
引數
- prompt (
str
或List[str]
,可選) — 用於引導影像生成的提示或提示列表。如果未定義,則必須傳入prompt_embeds
。 - negative_prompt (
str
或List[str]
,可選) — 用於不引導影像生成的提示或提示列表。如果未定義,則必須傳入negative_prompt_embeds
。當不使用指導時(即guidance_scale
小於1
時),該引數將被忽略。 - control_video (
List[PIL.Image.Image]
) — 用於條件生成的控制影片。必須是影片的影像/幀列表。如果未提供,則必須提供control_video_latents
。 - height (
int
,可選,預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的高度(畫素)。為了獲得最佳結果,預設設定為 480。 - width (
int
,可選,預設為 self.transformer.config.sample_height * self.vae_scale_factor_spatial) — 生成影像的寬度(畫素)。為了獲得最佳結果,預設設定為 720。 - num_inference_steps (
int
,可選,預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但推理速度會變慢。 - timesteps (
List[int]
, 可選) — 用於去噪過程的自定義時間步,適用於其set_timesteps
方法支援timesteps
引數的排程器。如果未定義,將使用傳遞num_inference_steps
時的預設行為。必須按降序排列。 - guidance_scale (
float
, 可選, 預設為 6.0) — 如 Classifier-Free Diffusion Guidance 中定義的引導比例。guidance_scale
定義為 Imagen Paper 方程 2 中的w
。透過設定guidance_scale > 1
啟用引導比例。更高的引導比例鼓勵生成與文字prompt
緊密相關的影像,通常以犧牲較低影像質量為代價。 - num_videos_per_prompt (
int
, 可選, 預設為 1) — 每個提示要生成的影片數量。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個或多個 torch 生成器,用於使生成確定化。 - latents (
torch.Tensor
, 可選) — 預先生成的噪聲潛在變數,從高斯分佈中取樣,用作影片生成的輸入。可用於使用不同的提示調整相同的生成。如果未提供,將使用提供的隨機generator
取樣生成一個潛在變數張量。 - control_video_latents (
torch.Tensor
, 可選) — 預先生成的控制潛在變數,從高斯分佈中取樣,用作受控影片生成的輸入。如果未提供,則必須提供control_video
。 - prompt_embeds (
torch.Tensor
, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,負面提示嵌入將從negative_prompt
輸入引數生成。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。選擇 PIL:PIL.Image.Image
或np.array
。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通元組。 - attention_kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,將傳遞給 diffusers.models.attention_processor 中定義的self.processor
的AttentionProcessor
。 - 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
, 預設為226
) — 編碼提示中的最大序列長度。必須與self.transformer.config.max_text_seq_length
保持一致,否則可能導致結果不佳。
返回
CogVideoXPipelineOutput 或 tuple
如果return_dict
為True,則返回CogVideoXPipelineOutput,否則返回tuple
。當返回元組時,第一個元素是生成的影像列表。
呼叫管道進行生成時呼叫的函式。
示例
>>> import torch
>>> from diffusers import CogVideoXFunControlPipeline, DDIMScheduler
>>> from diffusers.utils import export_to_video, load_video
>>> pipe = CogVideoXFunControlPipeline.from_pretrained(
... "alibaba-pai/CogVideoX-Fun-V1.1-5b-Pose", torch_dtype=torch.bfloat16
... )
>>> pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
>>> pipe.to("cuda")
>>> control_video = load_video(
... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/hiker.mp4"
... )
>>> prompt = (
... "An astronaut stands triumphantly at the peak of a towering mountain. Panorama of rugged peaks and "
... "valleys. Very futuristic vibe and animated aesthetic. Highlights of purple and golden colors in "
... "the scene. The sky is looks like an animated/cartoonish dream of galaxies, nebulae, stars, planets, "
... "moons, but the remainder of the scene is mostly realistic."
... )
>>> video = pipe(prompt=prompt, control_video=control_video).frames[0]
>>> export_to_video(video, "output.mp4", fps=8)
encode_prompt
< 原始碼 >( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 226 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )
引數
- prompt (
str
或List[str]
, 可選) — 要編碼的提示 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示或多個提示。如果未定義,則必須傳遞negative_prompt_embeds
。當不使用引導時(即,如果guidance_scale
小於1
時),此引數將被忽略。 - do_classifier_free_guidance (
bool
, 可選, 預設為True
) — 是否使用無分類器引導。 - num_videos_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 資料型別
將提示編碼為文字編碼器隱藏狀態。
啟用融合的 QKV 投影。
如果啟用了 QKV 投影融合,則停用它。
CogVideoXPipelineOutput
class diffusers.pipelines.cogvideo.pipeline_output.CogVideoXPipelineOutput
< 原始碼 >( frames: Tensor )
CogVideo 管道的輸出類。