Diffusers documentation

PAN 2

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.43.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

PAN 2

PAN2 adalah model dunia dasar yang mensimulasikan bagaimana sebuah adegan berevolusi di bawah rencana aksi dan gerakan kamera yang berlandaskan waktu. Simulator generatif 29B-nya adalah transformer difusi multimodal dual-stream, yang terintegrasi di sini; backbone penalaran yang menulis prompt-nya tidak terintegrasi.

PAN 2 terintegrasi hanya sebagai blok Modular Diffusers. PAN2Blocks mendukung dua alur kerja:

Alur KerjaInput
t2vprompt
i2vprompt, image
import torch
from diffusers import ModularPipeline
from diffusers.utils import export_to_video, load_image

pipe = ModularPipeline.from_pretrained("IFM/PAN2")
pipe.load_components(dtype=torch.bfloat16)
pipe.to("cuda")

prompt = "A red fox trots through fresh snow in a pine forest at dawn."

video = pipe(prompt=prompt, generator=torch.Generator().manual_seed(0), output="videos")[0]
export_to_video(video, "pan2_t2v.mp4", fps=24)

image = load_image("path/to/first_frame.png")
video = pipe(prompt=prompt, image=image, generator=torch.Generator().manual_seed(0), output="videos")[0]
export_to_video(video, "pan2_i2v.mp4", fps=24)

Classifier-free guidance dikonfigurasi pada guider (guidance_scale=2.5 secara default):

pipe.update_components(guider=pipe.guider.new(guidance_scale=5.0))

Setiap panggilan menerima satu prompt, yang dikodekan tanpa padding. Oleh karena itu, transformer tidak pernah memerlukan attention mask, dan setiap attention backend tersedia:

pipe.transformer.set_attention_backend("flash_hub")  # FlashAttention-2

Pemeriksa keamanan

PAN 2 menjalankan guardrails pada prompt sebelum generasi dan pada setiap video yang dihasilkan setelahnya, dan prompt atau video yang diblokir akan memicu ValueError. Pemeriksaan ini aktif secara default, dan load_components() memuat guardrails, yang memerlukan paket PAN 2 guardrail:

pip install git+https://github.com/ifm-ai/pan2-guardrail

Paralelisme konteks

PAN2Transformer3DModel mendukung context parallelism. Jumlah token teks dan video biasanya tidak habis dibagi oleh jumlah perangkat (jumlah token teks bergantung pada prompt), sehingga gunakan ulysses_anything atau ring_anything untuk inferensi. ulysses_degree / ring_degree biasa, misalnya untuk pelatihan, membutuhkan kedua jumlah token tersebut habis dibagi oleh ukuran context parallel.

# torchrun --nproc_per_node=2 pan2_context_parallel.py
import torch
import torch.distributed as dist
from diffusers import ContextParallelConfig, ModularPipeline

dist.init_process_group("nccl")
rank = dist.get_rank()
torch.cuda.set_device(rank)

pipe = ModularPipeline.from_pretrained("IFM/PAN2")
pipe.load_components(dtype=torch.bfloat16)
pipe.to(f"cuda:{rank}")
pipe.transformer.enable_parallelism(config=ContextParallelConfig(ulysses_degree=2, ulysses_anything=True))

video = pipe(prompt="A red fox trots through fresh snow.", generator=torch.Generator().manual_seed(0), output="videos")[0]
dist.destroy_process_group()

PAN2ModularPipeline

kelas diffusers.PAN2ModularPipeline

< >

( blocks: diffusers.modular_pipelines.modular_pipeline.ModularPipelineBlocks | None = Nonepretrained_model_name_or_path: str | os.PathLike | None = Nonecomponents_manager: diffusers.modular_pipelines.components_manager.ComponentsManager | None = Nonecollection: str | None = Noneworkflow: str | None = Nonemodular_config_dict: dict[str, typing.Any] | None = Noneconfig_dict: dict[str, typing.Any] | None = None**kwargs )

A ModularPipeline untuk PAN 2: text-to-video, dan image-to-video dari frame pertama.

import torch
from diffusers import ModularPipeline

pipe = ModularPipeline.from_pretrained("IFM/PAN2")
pipe.load_components(dtype=torch.bfloat16)

check_safety_checker_loaded

< >

( )

Membuat error jika pemeriksaan diaktifkan tetapi guardrail PAN 2 tidak dimuat.

disable_safety_checker

< >

( )

Matikan pemeriksaan prompt dan video, dengan risiko Anda sendiri.

enable_safety_checker

< >

( )

Periksa prompt dan video yang dihasilkan dengan guardrail PAN 2.

PAN2Blocks

kelas diffusers.PAN2Blocks

< >

( )

Blok pipeline modular untuk generasi teks-ke-video dan gambar-ke-video dengan PAN 2.

Alur kerja yang didukung:

  • t2v: membutuhkan prompt
  • i2v: membutuhkan prompt, image

Komponen: text_encoder (Qwen3_5TextModel) tokenizer (Qwen2Tokenizer) safety_checker (PAN2SafetyChecker) vae (AutoencoderKLWan) video_processor (VideoProcessor) scheduler (FlowMatchEulerDiscreteScheduler) transformer (PAN2Transformer3DModel) guider (ClassifierFreeGuidance)

Konfigurasi: enable_safety_checker (default: True)

Inputs: prompt (str): The prompt to guide generation, a single string. negative_prompt (str, optional): The prompt not to guide generation, a single string. When guidance needs one and none (or an empty one) is passed, PAN 2’s default negative prompt is used, its time ranges set to the clip’s duration. max_sequence_length (int, optional, defaults to 1536): Maximum sequence length for prompt encoding. num_frames (int, optional, defaults to 313): Number of video frames to generate, of the form 4 * n + 1. fps (float, optional, defaults to 24.0): The frame rate of the video, which sets its duration in the default negative prompt. image (Image, optional): The first frame, a single image. height (int, optional): The height in pixels of the generated video, 704 by default. width (int, optional): The width in pixels of the generated video, 1248 by default. num_videos_per_prompt (int, optional, defaults to 1): The number of videos to generate per prompt. image_latents (Tensor, optional): The normalized first-frame latents. Can be generated from the vae_encoder step. num_inference_steps (int): The number of denoising steps. sigmas (list, optional): Custom sigmas for the denoising process. latents (Tensor): Pre-generated noisy latents to use as inputs for video generation. generator (Generator, optional): Torch generator for deterministic generation. attention_kwargs (dict, optional): Additional kwargs for attention processors. output_type (str, optional, defaults to np): Output format: ‘pil’, ‘np’, ‘pt’.

Output: video (list): Video yang dihasilkan.

Update on GitHub