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] | |
| > إذا كنت مهتمًا بمعرفة المزيد حول كيفية استخدام نموذج معين، فيرجى الرجوع إلى بطاقة نموذج واجهة برمجة التطبيقات الخاصة به. | |
| ## معاملات خط الأنابيب | |
| توجد عدة معلمات يجب ضبطها في خط الأنابيب تؤثر على جودة توليد الفيديو أو سرعته. يُعدّ التجريب مع قيم مختلفة للمعلمات أمراً مهماً لاكتشاف التوازن المناسب بين الجودة والسرعة. | |
| ### num_frames | |
| الإطار هو صورة ثابتة تُعرض ضمن تسلسل من إطارات أخرى لإحداث حركة أو فيديو. تحكّم في عدد الإطارات المولّدة في الثانية الواحدة باستخدام `num_frames`. يؤدي زيادة `num_frames` إلى تحسين سلاسة الحركة المُدرَكة والاتساق البصري، مما يجعله مهماً بشكل خاص للفيديوهات ذات المحتوى الديناميكي. كما أن قيمة أعلى لـ`num_frames` تزيد من مدة الفيديو. | |
| تتطلب بعض نماذج الفيديو قيمًا أكثر تحديدًا لـ `num_frames` أثناء الاستدلال. على سبيل المثال، يوصي [HunyuanVideoPipeline](/docs/diffusers/v0.43.0/ar/api/pipelines/hunyuan_video#diffusers.HunyuanVideoPipeline) بحساب `num_frames` باستخدام `(4 * num_frames) +1`. تحقق دائمًا من بطاقة نموذج واجهة برمجة التطبيقات الخاصة بالخطوط الأنابيب لمعرفة ما إذا كانت هناك قيمة موصى بها. | |
| ```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 | |
| يتحكم معامل التوجيه أو "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/ar/api/pipelines/hunyuan_video#diffusers.HunyuanVideoPipeline) و [WanPipeline](/docs/diffusers/v0.43.0/ar/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/ar/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:
- 21.4 kB
- Xet hash:
- 63c0bb614e84e717d583cecef86be450ab5b4cfed69f98378ffb6f92f42effd8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.