# 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](https://huggingface.co/IFM/PAN2#reasoning-backbone) yang menulis prompt-nya tidak terintegrasi.

PAN 2 terintegrasi hanya sebagai blok [Modular Diffusers](../../modular_diffusers/overview). [PAN2Blocks](/docs/diffusers/v0.43.0/id/api/pipelines/pan2#diffusers.PAN2Blocks) mendukung dua alur kerja:

| Alur Kerja | Input |
|---|---|
| `t2v` | `prompt` |
| `i2v` | `prompt`, `image` |

```py
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):

```py
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](../../optimization/attention_backends) tersedia:

```py
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](https://github.com/ifm-ai/pan2-guardrail):

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

## Paralelisme konteks

[PAN2Transformer3DModel](/docs/diffusers/v0.43.0/id/api/models/pan2_transformer3d#diffusers.PAN2Transformer3DModel) mendukung [context parallelism](../../optimization/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.

```py
# 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]]

#### diffusers.PAN2ModularPipeline[[diffusers.PAN2ModularPipeline]]

```python
diffusers.PAN2ModularPipeline(blocks: diffusers.modular_pipelines.modular_pipeline.ModularPipelineBlocks | None = None, pretrained_model_name_or_path: str | os.PathLike | None = None, components_manager: diffusers.modular_pipelines.components_manager.ComponentsManager | None = None, collection: str | None = None, workflow: str | None = None, modular_config_dict: dict[str, typing.Any] | None = None, config_dict: dict[str, typing.Any] | None = None, **kwargs)
```

[Sumber](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/modular_pipelines/pan2/modular_pipeline.py#L42)

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

```py
import torch
from diffusers import ModularPipeline

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

#### check_safety_checker_loaded[[diffusers.PAN2ModularPipeline.check_safety_checker_loaded]]

```python
check_safety_checker_loaded()
```

[Sumber](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/modular_pipelines/pan2/modular_pipeline.py#L103)

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

#### disable_safety_checker[[diffusers.PAN2ModularPipeline.disable_safety_checker]]

```python
disable_safety_checker()
```

[Sumber](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/modular_pipelines/pan2/modular_pipeline.py#L118)

Matikan pemeriksaan prompt dan video, dengan risiko Anda sendiri.

#### enable_safety_checker[[diffusers.PAN2ModularPipeline.enable_safety_checker]]

```python
enable_safety_checker()
```

[Sumber](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/modular_pipelines/pan2/modular_pipeline.py#L97)

Periksa prompt dan video yang dihasilkan dengan guardrail PAN 2.

## PAN2Blocks[[diffusers.PAN2Blocks]]

#### diffusers.PAN2Blocks[[diffusers.PAN2Blocks]]

```python
diffusers.PAN2Blocks()
```

[Sumber](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/modular_pipelines/pan2/modular_blocks_pan2.py#L251)

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.

