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 是一个基础世界模型,用于模拟场景在基于时间的动作和相机运动计划下如何演化。其 29B 生成式模拟器是一个双流多模态扩散 Transformer,已在此处集成;而负责编写其提示词的推理主干则未集成。

PAN 2 仅以 Modular Diffusers 块的形式集成。PAN2Blocks 支持两种工作流:

工作流输入
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 在 guider 上进行配置(默认值为 guidance_scale=2.5):

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

每次调用都接收单个提示词,且该提示词在编码时不进行填充。因此,Transformer 无需使用注意力掩码,并且所有 注意力后端 均可用:

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

安全检查器

PAN 2 会在生成前对提示词以及生成后的每个视频运行护栏检查,被拦截的提示词或视频会引发 ValueError。这些检查默认开启,load_components() 会加载护栏,这需要 PAN 2 guardrail 包:

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

上下文并行

PAN2Transformer3DModel 支持 context parallelism。文本和视频的 token 数量通常不能被设备数量整除(文本 token 数量取决于提示词),因此推理时请使用 ulysses_anything 或 ring_anything。例如用于训练时,普通的 ulysses_degree / ring_degree 要求两种 token 数量都能被 context parallel size 整除。

# 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

类 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 )

用于 PAN 2 的 ModularPipeline:文本生成视频,以及从首帧图像生成视频。

import torch
from diffusers import ModularPipeline

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

check_safety_checker_loaded

< >

( )

如果检查已启用但未加载 PAN 2 护栏,则引发异常。

disable_safety_checker

< >

( )

关闭提示词和视频检查,风险自负。

enable_safety_checker

< >

( )

使用 PAN 2 护栏检查提示词和生成的视频。

PAN2Blocks

类 diffusers.PAN2Blocks

< >

( )

用于文本生成视频和图像生成视频的模块化流水线组件,基于 PAN 2。

支持的工作流:

  • t2v:需要 prompt
  • i2v:需要 prompt、image

组件:

  • text_encoder (Qwen3_5TextModel)
  • 分词器 (Qwen2Tokenizer)
  • safety_checker (PAN2SafetyChecker)
  • vae (AutoencoderKLWan)
  • video_processor(VideoProcessor)
  • 调度器(FlowMatchEulerDiscreteScheduler)
  • transformer(PAN2Transformer3DModel)
  • 引导器(ClassifierFreeGuidance)

配置:

  • enable_safety_checker(默认值:True)

输入: prompt (str): 用于引导生成的提示词,一个字符串。 negative_prompt (str,可选): 用于不引导生成的提示词,一个字符串。当需要引导但未传入(或传入空值)时,将使用 PAN 2 的默认负面提示词,其时间范围设置为片段的时长。 max_sequence_length (int,可选,默认为 1536): 提示词编码的最大序列长度。 num_frames (int,可选,默认为 313): 要生成的视频帧数,形式为 4 * n + 1。 fps (float,可选,默认为 24.0): 视频的帧率,用于在默认负面提示词中设置其时长。 image (Image,可选): 第一帧,一张图像。 height (int,可选): 生成视频的高度(像素),默认为 704。 width (int,可选): 生成视频的宽度(像素),默认为 1248。 num_videos_per_prompt (int,可选,默认为 1): 每个提示词生成的视频数量。 image_latents (Tensor,可选): 归一化的首帧潜在变量。可以从 vae_encoder 步骤生成。 num_inference_steps (int): 去噪步骤数。 sigmas (list,可选): 用于去噪过程的自定义 sigmas。 latents (Tensor): 预生成的含噪潜在变量,用作视频生成的输入。 generator (Generator,可选): 用于确定性生成的 Torch 生成器。 attention_kwargs (dict,可选): 注意力处理器的额外 kwargs。 output_type (str,可选,默认为 np): 输出格式:‘pil’、‘np’、‘pt’。

输出: videos (list): 生成的视频。

Update on GitHub