Buckets:
| # Генерация видео | |
| Модели генерации видео расширяют возможности генерации изображений (которую можно рассматривать как видео из одного кадра), добавляя обработку данных, связанных с пространством и временем. Обеспечение согласованности и выравнивания всех этих данных — текста, пространства и времени — от кадра к кадру представляет собой серьезную задачу при генерации длинных видео высокого разрешения. | |
| Современные модели для работы с видео решают эту задачу с помощью архитектуры диффузионного трансформера (DiT). Это снижает вычислительные затраты и позволяет более эффективно масштабироваться на более крупные и качественные данные изображений и видео. | |
| Ниже вы можете посмотреть, на что способны некоторые из этих видео-моделей. | |
| ```py | |
| # pip install ftfy | |
| import torch | |
| import numpy as np | |
| from diffusers import AutoModel, WanPipeline | |
| from diffusers.hooks.group_offloading import apply_group_offloading | |
| from diffusers.utils import export_to_video, load_image | |
| from transformers import UMT5EncoderModel | |
| text_encoder = UMT5EncoderModel.from_pretrained("Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="text_encoder", dtype=torch.bfloat16) | |
| vae = AutoModel.from_pretrained("Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="vae", dtype=torch.float32) | |
| transformer = AutoModel.from_pretrained("Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="transformer", dtype=torch.bfloat16) | |
| # group-offloading | |
| onload_device = torch.device("cuda") | |
| offload_device = torch.device("cpu") | |
| apply_group_offloading(text_encoder, | |
| onload_device=onload_device, | |
| offload_device=offload_device, | |
| offload_type="block_level", | |
| num_blocks_per_group=4 | |
| ) | |
| transformer.enable_group_offload( | |
| onload_device=onload_device, | |
| offload_device=offload_device, | |
| offload_type="leaf_level", | |
| use_stream=True | |
| ) | |
| pipeline = WanPipeline.from_pretrained( | |
| "Wan-AI/Wan2.1-T2V-14B-Diffusers", | |
| vae=vae, | |
| transformer=transformer, | |
| text_encoder=text_encoder, | |
| dtype=torch.bfloat16 | |
| ) | |
| pipeline.to("cuda") # or "mps", "xpu", "cpu" | |
| prompt = """ | |
| The camera rushes from far to near in a low-angle shot, | |
| revealing a white ferret on a log. It plays, leaps into the water, and emerges, as the camera zooms in | |
| for a close-up. Water splashes berry bushes nearby, while moss, snow, and leaves blanket the ground. | |
| Birch trees and a light blue sky frame the scene, with ferns in the foreground. Side lighting casts dynamic | |
| shadows and warm highlights. Medium composition, front view, low angle, with depth of field. | |
| """ | |
| negative_prompt = """ | |
| Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, | |
| low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, | |
| misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards | |
| """ | |
| output = pipeline( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| num_frames=81, | |
| guidance_scale=5.0, | |
| ).frames[0] | |
| export_to_video(output, "output.mp4", fps=16) | |
| ``` | |
| ```py | |
| import torch | |
| from diffusers importAutoModel, HunyuanVideoPipeline | |
| from diffusers.quantizers import PipelineQuantizationConfig | |
| from diffusers.utils import export_to_video | |
| # quantize weights to int4 with bitsandbytes | |
| pipeline_quant_config = PipelineQuantizationConfig( | |
| quant_backend="bitsandbytes_4bit", | |
| quant_kwargs={ | |
| "load_in_4bit": True, | |
| "bnb_4bit_quant_type": "nf4", | |
| "bnb_4bit_compute_dtype": torch.bfloat16 | |
| }, | |
| components_to_quantize="transformer" | |
| ) | |
| pipeline = HunyuanVideoPipeline.from_pretrained( | |
| "hunyuanvideo-community/HunyuanVideo", | |
| quantization_config=pipeline_quant_config, | |
| dtype=torch.bfloat16, | |
| ) | |
| # model-offloading and tiling | |
| pipeline.enable_model_cpu_offload() | |
| pipeline.vae.enable_tiling() | |
| prompt = "A fluffy teddy bear sits on a bed of soft pillows surrounded by children's toys." | |
| video = pipeline(prompt=prompt, num_frames=61, num_inference_steps=30).frames[0] | |
| export_to_video(video, "output.mp4", fps=15) | |
| ``` | |
| ```py | |
| import torch | |
| from diffusers import LTXPipeline, AutoModel | |
| from diffusers.hooks import apply_group_offloading | |
| from diffusers.utils import export_to_video | |
| # fp8 layerwise weight-casting | |
| transformer = AutoModel.from_pretrained( | |
| "Lightricks/LTX-Video", | |
| subfolder="transformer", | |
| dtype=torch.bfloat16 | |
| ) | |
| transformer.enable_layerwise_casting( | |
| storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16 | |
| ) | |
| pipeline = LTXPipeline.from_pretrained("Lightricks/LTX-Video", transformer=transformer, dtype=torch.bfloat16) | |
| # group-offloading | |
| onload_device = torch.device("cuda") | |
| offload_device = torch.device("cpu") | |
| pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type="leaf_level", use_stream=True) | |
| apply_group_offloading(pipeline.text_encoder, onload_device=onload_device, offload_type="block_level", num_blocks_per_group=2) | |
| apply_group_offloading(pipeline.vae, onload_device=onload_device, offload_type="leaf_level") | |
| prompt = """ | |
| A woman with long brown hair and light skin smiles at another woman with long blonde hair. The woman with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek. The camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be real-life footage | |
| """ | |
| negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted" | |
| video = pipeline( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| width=768, | |
| height=512, | |
| num_frames=161, | |
| decode_timestep=0.03, | |
| decode_noise_scale=0.025, | |
| num_inference_steps=50, | |
| ).frames[0] | |
| export_to_video(video, "output.mp4", fps=24) | |
| ``` | |
| В этом руководстве рассматриваются основы генерации видео, такие как параметры, которые необходимо настроить, и способы снижения потребления памяти. | |
| > [!TIP] | |
| > Если вы хотите узнать больше о том, как использовать конкретную модель, обратитесь к её карточке модели в API конвейера. | |
| ## Параметры конвейера | |
| В пайплайне есть несколько параметров, влияющих на качество или скорость генерации видео. Эксперименты с различными значениями параметров важны для поиска оптимального баланса между качеством и скоростью. | |
| ### num_frames | |
| Кадр — это статичное изображение, которое воспроизводится в последовательности с другими кадрами для создания движения или видео. Количество кадров, генерируемых в секунду, настраивается с помощью `num_frames`. Увеличение `num_frames` повышает плавность воспринимаемого движения и визуальную согласованность, что особенно важно для видео с динамичным содержанием. Более высокое значение `num_frames` также увеличивает длительность видео. | |
| Некоторые видеомодели требуют более конкретных значений `num_frames` для инференса. Например, [HunyuanVideoPipeline](/docs/diffusers/v0.43.0/ru/api/pipelines/hunyuan_video#diffusers.HunyuanVideoPipeline) рекомендует вычислять `num_frames` по формуле `(4 * num_frames) +1`. Всегда проверяйте карточку модели пайплайна в API, чтобы узнать, есть ли рекомендуемое значение. | |
| ```py | |
| import torch | |
| from diffusers import LTXPipeline | |
| from diffusers.utils import export_to_video | |
| pipeline = LTXPipeline.from_pretrained( | |
| "Lightricks/LTX-Video", dtype=torch.bfloat16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| prompt = """ | |
| A woman with long brown hair and light skin smiles at another woman with long blonde hair. The woman | |
| with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek. The | |
| camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and | |
| natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be | |
| real-life footage | |
| """ | |
| video = pipeline( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| width=768, | |
| height=512, | |
| num_frames=161, | |
| decode_timestep=0.03, | |
| decode_noise_scale=0.025, | |
| num_inference_steps=50, | |
| ).frames[0] | |
| export_to_video(video, "output.mp4", fps=24) | |
| ``` | |
| ### guidance_scale | |
| Параметр Guidance scale или «cfg» управляет тем, насколько сгенерированные кадры соответствуют входным условиям (тексту, изображению или обоим). Увеличение `guidance_scale` приводит к созданию кадров, которые точнее повторяют входные условия и содержат более мелкие детали, но повышает риск появления артефактов и снижает разнообразие результата. Более низкие значения `guidance_scale` способствуют более свободному следованию промпту и увеличению разнообразия вывода, однако детали могут быть менее проработанными. Если значение слишком низкое, модель может полностью игнорировать промпт и генерировать случайный шум. | |
| ```py | |
| import torch | |
| from diffusers import CogVideoXPipeline, CogVideoXTransformer3DModel | |
| from diffusers.utils import export_to_video | |
| pipeline = CogVideoXPipeline.from_pretrained( | |
| "THUDM/CogVideoX-2b", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| 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) | |
| ``` | |
| ### negative_prompt | |
| Отрицательный промпт полезен для исключения элементов, которые не должны присутствовать в сгенерированном видео. Его часто используют для улучшения качества и соответствия сгенерированного видео, отталкивая модель от нежелательных элементов, таких как «размытое, искажённое, уродливое». Это позволяет создавать более чистые и сфокусированные видео. | |
| ```py | |
| # pip install ftfy | |
| import torch | |
| from diffusers import WanPipeline | |
| from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler | |
| from diffusers.utils import export_to_video | |
| vae = AutoencoderKLWan.from_pretrained( | |
| "Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="vae", dtype=torch.float32 | |
| ) | |
| pipeline = WanPipeline.from_pretrained( | |
| "Wan-AI/Wan2.1-T2V-14B-Diffusers", vae=vae, dtype=torch.bfloat16 | |
| ) | |
| pipeline.scheduler = UniPCMultistepScheduler.from_config( | |
| pipeline.scheduler.config, flow_shift=5.0 | |
| ) | |
| pipeline.to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_lora_weights("benjamin-paine/steamboat-willie-14b", adapter_name="steamboat-willie") | |
| pipeline.set_adapters("steamboat-willie") | |
| pipeline.enable_model_cpu_offload() | |
| # use "steamboat willie style" to trigger the LoRA | |
| prompt = """ | |
| steamboat willie style, golden era animation, The camera rushes from far to near in a low-angle shot, | |
| revealing a white ferret on a log. It plays, leaps into the water, and emerges, as the camera zooms in | |
| for a close-up. Water splashes berry bushes nearby, while moss, snow, and leaves blanket the ground. | |
| Birch trees and a light blue sky frame the scene, with ferns in the foreground. Side lighting casts | |
| dynamic shadows and warm highlights. Medium composition, front view, low angle, with depth of field. | |
| """ | |
| output = pipeline( | |
| prompt=prompt, | |
| num_frames=81, | |
| guidance_scale=5.0, | |
| ).frames[0] | |
| export_to_video(output, "output.mp4", fps=16) | |
| ``` | |
| ## Сокращение использования памяти | |
| Современные видеомодели, такие как [HunyuanVideoPipeline](/docs/diffusers/v0.43.0/ru/api/pipelines/hunyuan_video#diffusers.HunyuanVideoPipeline) и [WanPipeline](/docs/diffusers/v0.43.0/ru/api/pipelines/wan#diffusers.WanPipeline), содержащие более 10 млрд параметров, требуют значительного объёма памяти, который часто превышает доступную память на потребительском оборудовании. Diffusers предлагает несколько техник для снижения требований к памяти при работе с такими крупными моделями. | |
| > [!TIP] | |
| > См. руководство [Сокращение использования памяти](../optimization/memory) для получения подробной информации о других техниках экономии памяти. | |
| Одной из таких техник является [group-offloading](../optimization/memory#group-offloading), которая выгружает группы внутренних слоёв модели (например, `torch.nn.Sequential`) на CPU, когда они не используются. Эти слои загружаются только тогда, когда они нужны для вычислений, чтобы избежать хранения **всех** компонентов модели на GPU. Для модели с 14 млрд параметров, такой как [WanPipeline](/docs/diffusers/v0.43.0/ru/api/pipelines/wan#diffusers.WanPipeline), group-offloading может снизить требуемый объём памяти до ~13 ГБ VRAM. | |
| ```py | |
| # pip install ftfy | |
| import torch | |
| import numpy as np | |
| from diffusers import AutoModel, WanPipeline | |
| from diffusers.hooks.group_offloading import apply_group_offloading | |
| from diffusers.utils import export_to_video, load_image | |
| from transformers import UMT5EncoderModel | |
| text_encoder = UMT5EncoderModel.from_pretrained("Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="text_encoder", dtype=torch.bfloat16) | |
| vae = AutoModel.from_pretrained("Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="vae", dtype=torch.float32) | |
| transformer = AutoModel.from_pretrained("Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="transformer", dtype=torch.bfloat16) | |
| # group-offloading | |
| onload_device = torch.device("cuda") | |
| offload_device = torch.device("cpu") | |
| apply_group_offloading(text_encoder, | |
| onload_device=onload_device, | |
| offload_device=offload_device, | |
| offload_type="block_level", | |
| num_blocks_per_group=4 | |
| ) | |
| transformer.enable_group_offload( | |
| onload_device=onload_device, | |
| offload_device=offload_device, | |
| offload_type="leaf_level", | |
| use_stream=True | |
| ) | |
| pipeline = WanPipeline.from_pretrained( | |
| "Wan-AI/Wan2.1-T2V-14B-Diffusers", | |
| vae=vae, | |
| transformer=transformer, | |
| text_encoder=text_encoder, | |
| dtype=torch.bfloat16 | |
| ) | |
| pipeline.to("cuda") # or "mps", "xpu", "cpu" | |
| prompt = """ | |
| The camera rushes from far to near in a low-angle shot, | |
| revealing a white ferret on a log. It plays, leaps into the water, and emerges, as the camera zooms in | |
| for a close-up. Water splashes berry bushes nearby, while moss, snow, and leaves blanket the ground. | |
| Birch trees and a light blue sky frame the scene, with ferns in the foreground. Side lighting casts dynamic | |
| shadows and warm highlights. Medium composition, front view, low angle, with depth of field. | |
| """ | |
| negative_prompt = """ | |
| Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, | |
| low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, | |
| misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards | |
| """ | |
| output = pipeline( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| num_frames=81, | |
| guidance_scale=5.0, | |
| ).frames[0] | |
| export_to_video(output, "output.mp4", fps=16) | |
| ``` | |
| Ещё один способ снизить потребление памяти — квантование модели, при котором веса модели хранятся в формате с пониженной точностью. Однако квантование может повлиять на качество видео в зависимости от конкретной видео-модели. Подробнее о поддерживаемых бэкендах квантования см. в разделе [Обзор](../quantization/overview). | |
| Пример ниже использует [bitsandbytes](../quantization/bitsandbytes) для квантования модели. | |
| ```py | |
| # pip install ftfy | |
| import torch | |
| from diffusers import WanPipeline | |
| from diffusers import AutoModel, WanPipeline | |
| from diffusers.quantizers import PipelineQuantizationConfig | |
| from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler | |
| from transformers import UMT5EncoderModel | |
| from diffusers.utils import export_to_video | |
| # quantize transformer and text encoder weights with bitsandbytes | |
| pipeline_quant_config = PipelineQuantizationConfig( | |
| quant_backend="bitsandbytes_4bit", | |
| quant_kwargs={"load_in_4bit": True}, | |
| components_to_quantize=["transformer", "text_encoder"] | |
| ) | |
| vae = AutoModel.from_pretrained( | |
| "Wan-AI/Wan2.1-T2V-14B-Diffusers", subfolder="vae", dtype=torch.float32 | |
| ) | |
| pipeline = WanPipeline.from_pretrained( | |
| "Wan-AI/Wan2.1-T2V-14B-Diffusers", vae=vae, quantization_config=pipeline_quant_config, dtype=torch.bfloat16 | |
| ) | |
| pipeline.scheduler = UniPCMultistepScheduler.from_config( | |
| pipeline.scheduler.config, flow_shift=5.0 | |
| ) | |
| pipeline.to("cuda") # or "mps", "xpu", "cpu" | |
| pipeline.load_lora_weights("benjamin-paine/steamboat-willie-14b", adapter_name="steamboat-willie") | |
| pipeline.set_adapters("steamboat-willie") | |
| pipeline.enable_model_cpu_offload() | |
| # use "steamboat willie style" to trigger the LoRA | |
| prompt = """ | |
| steamboat willie style, golden era animation, The camera rushes from far to near in a low-angle shot, | |
| revealing a white ferret on a log. It plays, leaps into the water, and emerges, as the camera zooms in | |
| for a close-up. Water splashes berry bushes nearby, while moss, snow, and leaves blanket the ground. | |
| Birch trees and a light blue sky frame the scene, with ferns in the foreground. Side lighting casts | |
| dynamic shadows and warm highlights. Medium composition, front view, low angle, with depth of field. | |
| """ | |
| output = pipeline( | |
| prompt=prompt, | |
| num_frames=81, | |
| guidance_scale=5.0, | |
| ).frames[0] | |
| export_to_video(output, "output.mp4", fps=16) | |
| ``` | |
| ## Скорость инференса | |
| [torch.compile](https://docs.pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) может ускорить инференс за счёт использования оптимизированных ядер. Компиляция занимает больше времени при первом запуске, но после компиляции выполнение становится значительно быстрее. Лучше всего скомпилировать пайплайн один раз, а затем использовать его многократно без каких-либо изменений. Любое изменение, например размера изображения, приводит к повторной компиляции. | |
| Пример ниже компилирует трансформер в конвейере и использует режим `"max-autotune"` для максимальной производительности. | |
| ```py | |
| import torch | |
| from diffusers import CogVideoXPipeline, CogVideoXTransformer3DModel | |
| from diffusers.utils import export_to_video | |
| pipeline = CogVideoXPipeline.from_pretrained( | |
| "THUDM/CogVideoX-2b", | |
| dtype=torch.float16 | |
| ).to("cuda") # or "mps", "xpu", "cpu" | |
| # torch.compile | |
| pipeline.transformer.to(memory_format=torch.channels_last) | |
| pipeline.transformer = torch.compile( | |
| pipeline.transformer, mode="max-autotune", fullgraph=True | |
| ) | |
| 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) | |
| ``` | |
Xet Storage Details
- Size:
- 23 kB
- Xet hash:
- b5802d011337d606beb41d5b00dca4b49caea91de7c6d83ad073e2e54a901a17
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.