콘텐츠로 이동

Cosmos-Predict2.5(비디오 생성)

개요

Cosmos-Predict2.5는 미래 상태를 시뮬레이션하고 예측하여 비디오를 생성하는 데 특화된 Cosmos world foundation model(WFM) 중 하나입니다. Cosmos-Predict1에서 Text2World, Image2World, Video2World를 하나의 flow 기반 모델로 통합하고, Physical AI 비전 언어 모델(VLM)인 Cosmos-Reason1을 텍스트 인코더로 사용하도록 개선되었습니다. 따라서 한 번 컴파일된 파이프라인으로 텍스트, 이미지, 비디오 입력의 세 가지 생성 모드를 모두 사용할 수 있습니다. 이 튜토리얼에서는 RBLN Optimum(optimum-rbln)을 사용하여 Cosmos-Predict2.5 파이프라인의 각 모듈을 여러 개의 RBLN NPU용으로 컴파일하고, 입력 텍스트와 이미지를 이용하여 비디오를 생성(Image2World)하는 방법을 보여줍니다.

Info

리벨리온 확장형 설계(Rebellions Scalable Design, RSD)는 ATOM™+ (RBLN-CA22)와 ATOM™-Max(RBLN-CA25)에서 사용 가능합니다. 현재 사용 중인 RBLN NPU 종류는 rbln-stat 명령어로 확인할 수 있습니다.

환경 설정 및 설치 확인

시작하기 전에 시스템 환경이 올바르게 구성되어 있고 필요한 패키지가 모두 설치되어 있는지 확인합니다. 필요한 항목은 다음과 같습니다:

Note

  • RBLN Compiler를 사용하려면 RBLN 포털 계정이 필요합니다.
  • 위 명령은 Debian 계열 Linux(예: Ubuntu)에서 pip로 패키지를 설치하는 일반적인 절차를 전제로 합니다. OS나 환경이 다른 경우에는 설치 가이드에서 지원되는 설치 조합과 적용 가능한 명령을 확인할 수 있습니다.

Note

nvidia/Cosmos-Predict2.5-2B 모델은 접근이 제한되어 있습니다(gated model). HuggingFace에서 NVIDIA Open Model License에 동의하여 접근 권한을 부여받은 후, 아래와 같이 hf auth(huggingface-cli) 명령어를 사용하여 로그인할 수 있습니다.

$ hf auth login

    _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
    _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
    _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
    _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
    _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

    To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens.
Enter your token (input will not be visible):

RBLN Optimum(optimum-rbln) 라이브러리를 사용하여 실행하기

이 단계에서는 여러 개의 RBLN NPU에서 실행되도록 Cosmos-Predict2.5 모델을 컴파일합니다.

다중 NPU 대상 모델 컴파일

먼저 optimum-rbln 라이브러리에서 제공하는 RBLNCosmos2_5_PredictBasePipeline 클래스를 사용하여 Cosmos-Predict2.5 파이프라인의 모델을 컴파일합니다. 모델을 컴파일하려면 export 인수를 True로 설정합니다. 이 클래스의 from_pretrained() 메서드는 HuggingFace 허브에서 Cosmos-Predict2.5 모델을 다운로드하고 RBLN Compiler를 사용하여 컴파일합니다. 이때 HuggingFace 허브에서 nvidia/Cosmos-Predict2.5-2B 모델의 diffusers 형식 가중치는 별도의 revision으로 제공되므로, revision="diffusers/base/post-trained"로 지정합니다. 컴파일 후에는 save_pretrained() 메서드를 사용하여 모델 아티팩트(artifacts)를 디스크에 저장합니다. 이 과정은 컴파일된 모델을 포함하는 디렉터리(예: rbln-cosmos-predict2.5)를 생성합니다.

모델을 컴파일하기 전, 출력 비디오의 크기를 명시해야 합니다. Cosmos-Predict2.5는 704x1280 픽셀 해상도, 93 프레임(16 FPS 기준 약 5초)의 비디오를 생성합니다. 입력으로 주어지는 이미지 또는 비디오는 추론 시 VAE 인코더를 통해 처리되므로, 한 번 컴파일된 파이프라인이 Text2World, Image2World, Video2World 세 가지 모드를 모두 지원합니다.

또한 파이프라인의 일부 모듈은 추론 시 여러 개의 NPU가 필요하므로, 컴파일 시에 NPU 수를 명시해야 합니다. 컴파일 예제 코드는 다음과 같습니다.

import torch
from optimum.rbln import RBLNCosmos2_5_PredictBasePipeline

# Define the HuggingFace model ID
model_id = "nvidia/Cosmos-Predict2.5-2B"

# Compile Cosmos core models (transformer, text_encoder, vae, safety_checker)
pipe = RBLNCosmos2_5_PredictBasePipeline.from_pretrained(
    model_id,
    revision="diffusers/base/post-trained",
    torch_dtype=torch.bfloat16,
    export=True,
    rbln_config={
        "height": 704,
        "width": 1280,
        "num_frames": 93,
        "max_seq_len": 512,
        "create_runtimes": False,
        "text_encoder": {
            "num_devices": 8,
        },
        "transformer": {
            "num_devices": 8,
        },
    },
)

pipe.save_pretrained("rbln-cosmos-predict2.5")

컴파일된 RBLN 모델 불러오기

비디오를 생성하려면, 먼저 이전 단계에서 저장한 컴파일된 모델을 불러와야 합니다. RBLNCosmos2_5_PredictBasePipeline 클래스의 from_pretrained() 메서드로 모델을 불러옵니다. 이번에는 export 인수를 False로 설정하여 로컬 경로에서 사전 컴파일된 모델을 불러오도록 지정합니다.

Cosmos-Predict2.5 파이프라인은 여러 개의 모듈로 구성되어 있고, 이 중 일부는 디바이스 메모리를 많이 차지합니다. 따라서 이러한 모델을 동일한 디바이스에 모두 불러오면 디바이스 메모리 부족(out of memory) 오류가 발생합니다. 같은 수의 NPU를 사용하더라도 어떤 모듈을 어느 디바이스에 배치하는지에 따라 로드의 성공 여부가 달라지므로, rbln_config에서 device id를 유의해서 설정해야 합니다. 아래 예제 코드에서는 모든 서브모듈이 transformer가 사용하는 8개의 NPU 위에 함께 올라갑니다. text_encoder는 8개를 모두 공유하고, VAE encoder/decoder와 safety_checker는 앞쪽 3개를 나누어 사용하므로 전체 파이프라인이 8개의 NPU 풀에 들어갑니다.

from optimum.rbln import RBLNCosmos2_5_PredictBasePipeline

pipe = RBLNCosmos2_5_PredictBasePipeline.from_pretrained(
    model_id="rbln-cosmos-predict2.5",
    export=False,
    rbln_config={
        "text_encoder": {
            "device": [0, 1, 2, 3, 4, 5, 6, 7],
        },
        "transformer": {
            "device": [0, 1, 2, 3, 4, 5, 6, 7],
        },
        "vae": {
            "device_map": {"encoder": 0, "decoder": 1},
        },
        "safety_checker": {
            "qwen3guard": {"device": 2},
            "face_blur_filter": {"device": 2},
        },
    },
)

비디오 생성

컴파일된 파이프라인을 불러온 뒤 다음과 같이 비디오를 생성합니다. 입력 이미지 및 프롬프트와 함께 파이프라인을 호출하면, 입력 이미지를 첫 프레임으로 하는 비디오가 생성됩니다(Image2World). 네거티브 프롬프트는 파이프라인 내부에 정의된 기본값을 사용하며, negative_prompt 인수로 원하는 프롬프트를 직접 전달할 수도 있습니다.

Note

동일한 파이프라인으로 image와 video를 모두 생략하면 Text2World로, video 인수에 입력 비디오를 전달하면 Video2World로 동작합니다.

NVIDIA Open Model License

NVIDIA Open Model License 정책에 따라 safety_checker 서브모듈이 입력 프롬프트와 생성된 비디오에 대한 가드레일 검사를 파이프라인 내부에서 자동으로 적용합니다. safety_checker를 제거하거나 비활성화해서는 안 됩니다. 용도에 맞는 동등한 수준의 가드레일로 대체하지 않는 한, 가드레일을 우회하거나 그 효력을 떨어뜨리면 라이선스에 따른 권리가 자동으로 종료됩니다.

import torch
from diffusers.utils import export_to_video, load_image

# Conditioning image: the first frame the generated world video continues from.
image = load_image(
    "https://media.githubusercontent.com/media/nvidia-cosmos/cosmos-predict2.5/refs/heads/main/assets/base/bus_terminal.jpg"
)
prompt = "A nighttime city bus terminal gradually shifts from stillness to subtle movement. At first, multiple double-decker buses are parked under the glow of overhead lights, with a central bus labeled '87D' facing forward and stationary. As the video progresses, the bus in the middle moves ahead slowly, its headlights brightening the surrounding area and casting reflections onto adjacent vehicles. The motion creates space in the lineup, signaling activity within the otherwise quiet station. It then comes to a smooth stop, resuming its position in line. Overhead signage in Chinese characters remains illuminated, enhancing the vibrant, urban night scene."

# Image2World: condition on a single image and generate a 93-frame world video.
video = pipe(
    image=image,
    prompt=prompt,
    num_frames=93,
    generator=torch.Generator().manual_seed(1),
).frames[0]
export_to_video(video, "generated_video.mp4", fps=16)

예시 출력:

전체 예제 코드는 RBLN Model Zoo GitHub 저장소에서 확인할 수 있습니다.

참고