> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Hugging Face Diffusers

> Diffusers와 함께 Weights & Biases autolog를 사용하여 프롬프트, 생성된 미디어, 설정, 파이프라인 아키텍처를 추적하세요.

export const ColabLink = ({url}) => <a href={url} target="_blank" rel="noopener noreferrer" className="colab-link">
    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path d="M14.25.18l.9.2.73.26.59.3.45.32.34.34.25.34.16.33.1.3.04.26.02.2-.01.13V8.5l-.05.63-.13.55-.21.46-.26.38-.3.31-.33.25-.35.19-.35.14-.33.1-.3.07-.26.04-.21.02H8.77l-.69.05-.59.14-.5.22-.41.27-.33.32-.27.35-.2.36-.15.37-.1.35-.07.32-.04.27-.02.21v3.06H3.17l-.21-.03-.28-.07-.32-.12-.35-.18-.36-.26-.36-.36-.35-.46-.32-.59-.28-.73-.21-.88-.14-1.05-.05-1.23.06-1.22.16-1.04.24-.87.32-.71.36-.57.4-.44.42-.33.42-.24.4-.16.36-.1.32-.05.24-.01h.16l.06.01h8.16v-.83H6.18l-.01-2.75-.02-.37.05-.34.11-.31.17-.28.25-.26.31-.23.38-.2.44-.18.51-.15.58-.12.64-.1.71-.06.77-.04.84-.02 1.27.05zm-6.3 1.98l-.23.33-.08.41.08.41.23.34.33.22.41.09.41-.09.33-.22.23-.34.08-.41-.08-.41-.23-.33-.33-.22-.41-.09-.41.09zm13.09 3.95l.28.06.32.12.35.18.36.27.36.35.35.47.32.59.28.73.21.88.14 1.04.05 1.23-.06 1.23-.16 1.04-.24.86-.32.71-.36.57-.4.45-.42.33-.42.24-.4.16-.36.09-.32.05-.24.02-.16-.01h-8.22v.82h5.84l.01 2.76.02.36-.05.34-.11.31-.17.29-.25.25-.31.24-.38.2-.44.17-.51.15-.58.13-.64.09-.71.07-.77.04-.84.01-1.27-.04-1.07-.14-.9-.2-.73-.25-.59-.3-.45-.33-.34-.34-.25-.34-.16-.33-.1-.3-.04-.25-.02-.2.01-.13v-5.34l.05-.64.13-.54.21-.46.26-.38.3-.32.33-.24.35-.2.35-.14.33-.1.3-.06.26-.04.21-.02.13-.01h5.84l.69-.05.59-.14.5-.21.41-.28.33-.32.27-.35.2-.36.15-.36.1-.35.07-.32.04-.28.02-.21V6.07h2.09l.14.01.21.03zm-6.47 14.25l-.23.33-.08.41.08.41.23.33.33.23.41.08.41-.08.33-.23.23-.33.08-.41-.08-.41-.23-.33-.33-.23-.41-.08-.41.08z" />
    </svg>
    Try in Colab
  </a>;

<ColabLink url="https://colab.research.google.com/github/wandb/examples/blob/master/colabs/diffusers/lcm-diffusers.ipynb" />

[Hugging Face Diffusers](https://huggingface.co/docs/diffusers/index)는 이미지, 오디오, 분자의 3D 구조를 생성하기 위한 사전 트레이닝된 확산 모델 라이브러리입니다. Weights & Biases 인테그레이션은 대화형 중앙 집중식 대시보드에 실험 추적, 미디어 시각화, 파이프라인 아키텍처 추적, 설정 관리 기능을 추가합니다.

<h2 id="log-experiments-in-two-lines">
  두 줄로 실험 로깅하기
</h2>

실험에 연결된 모든 프롬프트, 네거티브 프롬프트, 생성된 미디어 및 설정을 로깅하려면 다음 두 줄의 코드를 추가하세요:

```python theme={"system"}
# autolog 함수를 임포트하세요
from wandb.integration.diffusers import autolog

# 파이프라인을 호출하기 전에 autolog를 호출하세요
autolog(init=dict(project="diffusers_logging"))
```

<Frame caption="W&B가 사용자의 실험 결과를 로깅하는 방식의 예시입니다.">
  <img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/_media/diffusers-autolog-4.gif?s=ff4cf1aebe661ffb46f0f9b8646ab7f3" alt="실험 결과 로깅" width="800" height="419" data-path="products/wandb/_media/diffusers-autolog-4.gif" />
</Frame>

<h2 id="get-started">
  시작하기
</h2>

1. `diffusers`, `transformers`, `accelerate`, `wandb`를 설치하세요.

   * 명령줄:

     ```bash theme={"system"}
     pip install --upgrade diffusers transformers accelerate wandb
     ```

   * 노트북:

     ```bash theme={"system"}
     !pip install --upgrade diffusers transformers accelerate wandb
     ```

2. [`wandb.init()`](/ko/products/wandb/ref/python/functions/init)에 필요한 매개변수 딕셔너리를 받는 `init` 매개변수로 `autolog()`를 호출하세요. `autolog()`는 W\&B run을 초기화하고 [지원되는 모든 파이프라인 호출](https://github.com/wandb/wandb/blob/main/wandb/integration/diffusers/autologger.py#L12-L72)의 입력과 출력을 자동으로 추적합니다:

   * 각 파이프라인 호출은 작업 공간의 개별 [table](/ko/products/wandb/tables)에 추적되며, 파이프라인 호출에 연결된 설정은 해당 run의 설정에 있는 워크플로 목록에 추가됩니다.
   * 프롬프트, 네거티브 프롬프트, 생성된 미디어는 [`wandb.Table`](/ko/products/wandb/tables)에 로깅됩니다.
   * 시드와 파이프라인 아키텍처를 비롯하여 실험에 연결된 다른 모든 설정은 run의 설정 섹션에 저장됩니다.
   * 각 파이프라인 호출에서 생성된 미디어는 run의 [미디어 패널](/ko/products/wandb/track/log/media)에도 로깅됩니다.

   <Note>
     [지원되는 파이프라인 호출 목록](https://github.com/wandb/wandb/blob/main/wandb/integration/diffusers/autologger.py#L12-L72)을 확인하세요. 이 인테그레이션의 새 기능을 요청하거나 버그를 신고하려면 [Weights & Biases GitHub 이슈 페이지](https://github.com/wandb/wandb/issues)에서 이슈를 등록하세요.
   </Note>

<h2 id="examples">
  예시
</h2>

다음 예시에서는 일반적인 확산 워크플로에서 `autolog`를 사용하는 방법을 보여 줍니다. 이를 참고하여 자신의 파이프라인에 맞게 활용하세요.

<h3 id="autolog-example">
  autolog 예시
</h3>

다음은 `autolog`의 전체 과정 예시입니다:

<Tabs>
  <Tab title="스크립트">
    ```python theme={"system"}
    import torch
    from diffusers import DiffusionPipeline

    # autolog 함수를 임포트합니다
    from wandb.integration.diffusers import autolog

    # 파이프라인을 호출하기 전에 autolog를 호출합니다
    autolog(init=dict(project="diffusers_logging"))

    # 확산 파이프라인을 초기화합니다
    pipeline = DiffusionPipeline.from_pretrained(
        "stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16
    ).to("cuda")

    # 프롬프트, 네거티브 프롬프트, 시드를 정의합니다.
    prompt = ["a photograph of an astronaut riding a horse", "a photograph of a dragon"]
    negative_prompt = ["ugly, deformed", "ugly, deformed"]
    generator = torch.Generator(device="cpu").manual_seed(10)

    # 파이프라인을 호출하여 이미지를 생성합니다
    images = pipeline(
        prompt,
        negative_prompt=negative_prompt,
        num_images_per_prompt=2,
        generator=generator,
    )
    ```
  </Tab>

  <Tab title="노트북">
    ```python theme={"system"}
    import torch
    from diffusers import DiffusionPipeline

    import wandb

    # autolog 함수를 임포트합니다
    from wandb.integration.diffusers import autolog

    run = wandb.init()

    # 파이프라인을 호출하기 전에 autolog를 호출합니다
    autolog(init=dict(project="diffusers_logging"))

    # 확산 파이프라인을 초기화합니다
    pipeline = DiffusionPipeline.from_pretrained(
        "stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16
    ).to("cuda")

    # 프롬프트, 네거티브 프롬프트, 시드를 정의합니다.
    prompt = ["a photograph of an astronaut riding a horse", "a photograph of a dragon"]
    negative_prompt = ["ugly, deformed", "ugly, deformed"]
    generator = torch.Generator(device="cpu").manual_seed(10)

    # 파이프라인을 호출하여 이미지를 생성합니다
    images = pipeline(
        prompt,
        negative_prompt=negative_prompt,
        num_images_per_prompt=2,
        generator=generator,
    )

    # 실험을 종료합니다
    run.finish()
    ```
  </Tab>
</Tabs>

다음 이미지는 Weights & Biases에 로깅되는 내용을 보여줍니다:

* 단일 실험의 결과:

  <Frame>
    <img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/_media/diffusers-autolog-2.gif?s=21c13de7b55777b3ea951887983e4828" alt="실험 결과 로깅" width="900" height="508" data-path="products/wandb/_media/diffusers-autolog-2.gif" />
  </Frame>

* 여러 실험의 결과:

  <Frame>
    <img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/_media/diffusers-autolog-1.gif?s=5b3e18828704632fe6fa4d88de8b5b41" alt="실험 결과 로깅" width="888" height="448" data-path="products/wandb/_media/diffusers-autolog-1.gif" />
  </Frame>

* 실험의 설정:

  <Frame>
    <img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/_media/diffusers-autolog-3.gif?s=1eb5be9619698ee9365b1e325641e1cb" alt="실험 설정 로깅" width="600" height="683" data-path="products/wandb/_media/diffusers-autolog-3.gif" />
  </Frame>

<Note>
  IPython 노트북 환경에서 코드를 실행할 때는 파이프라인을 호출한 후 [`wandb.Run.finish()`](/ko/products/wandb/ref/python/functions/finish)을 명시적으로 호출해야 합니다. Python 스크립트를 실행할 때는 필요하지 않습니다.
</Note>

<h3 id="track-multi-pipeline-workflows">
  멀티 파이프라인 워크플로 추적하기
</h3>

다음 예시는 일반적인 [Stable Diffusion XL + Refiner](https://huggingface.co/docs/diffusers/using-diffusers/sdxl#base-to-refiner-model) 워크플로에서 `autolog`를 사용하는 방법을 보여 줍니다. 이 워크플로에서는 [`StableDiffusionXLPipeline`](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/stable_diffusion_xl)이 생성한 latent를 refiner가 정제합니다.

<Tabs>
  <Tab title="스크립트">
    ```python theme={"system"}
    import torch
    from diffusers import StableDiffusionXLImg2ImgPipeline, StableDiffusionXLPipeline
    from wandb.integration.diffusers import autolog

    # SDXL 기본 파이프라인을 초기화합니다
    base_pipeline = StableDiffusionXLPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        torch_dtype=torch.float16,
        variant="fp16",
        use_safetensors=True,
    )
    base_pipeline.enable_model_cpu_offload()

    # SDXL 리파이너 파이프라인을 초기화합니다
    refiner_pipeline = StableDiffusionXLImg2ImgPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-refiner-1.0",
        text_encoder_2=base_pipeline.text_encoder_2,
        vae=base_pipeline.vae,
        torch_dtype=torch.float16,
        use_safetensors=True,
        variant="fp16",
    )
    refiner_pipeline.enable_model_cpu_offload()

    prompt = "a photo of an astronaut riding a horse on mars"
    negative_prompt = "static, frame, painting, illustration, sd character, low quality, low resolution, greyscale, monochrome, nose, cropped, lowres, jpeg artifacts, deformed iris, deformed pupils, bad eyes, semi-realistic worst quality, bad lips, deformed mouth, deformed face, deformed fingers, deformed toes standing still, posing"

    # 무작위성을 제어하여 실험을 재현할 수 있도록 합니다.
    # 시드는 WandB에 자동으로 로깅됩니다.
    seed = 42
    generator_base = torch.Generator(device="cuda").manual_seed(seed)
    generator_refiner = torch.Generator(device="cuda").manual_seed(seed)

    # Diffusers용 WandB Autolog를 호출합니다. 이를 통해 프롬프트,
    # 생성된 이미지, 파이프라인 아키텍처 및 모든 관련 실험 설정이
    # W&B에 자동으로 로깅되어 이미지 생성 실험을
    # 쉽게 재현하고 공유하고 분석할 수 있습니다.
    autolog(init=dict(project="sdxl"))

    # 기본 파이프라인을 호출하여 잠재 표현을 생성합니다
    image = base_pipeline(
        prompt=prompt,
        negative_prompt=negative_prompt,
        output_type="latent",
        generator=generator_base,
    ).images[0]

    # 리파이너 파이프라인을 호출하여 정제된 이미지를 생성합니다
    image = refiner_pipeline(
        prompt=prompt,
        negative_prompt=negative_prompt,
        image=image[None, :],
        generator=generator_refiner,
    ).images[0]
    ```
  </Tab>

  <Tab title="노트북">
    ```python theme={"system"}
    import torch
    from diffusers import StableDiffusionXLImg2ImgPipeline, StableDiffusionXLPipeline

    import wandb
    from wandb.integration.diffusers import autolog

    run = wandb.init()

    # SDXL 기본 파이프라인을 초기화합니다
    base_pipeline = StableDiffusionXLPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        torch_dtype=torch.float16,
        variant="fp16",
        use_safetensors=True,
    )
    base_pipeline.enable_model_cpu_offload()

    # SDXL 리파이너 파이프라인을 초기화합니다
    refiner_pipeline = StableDiffusionXLImg2ImgPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-refiner-1.0",
        text_encoder_2=base_pipeline.text_encoder_2,
        vae=base_pipeline.vae,
        torch_dtype=torch.float16,
        use_safetensors=True,
        variant="fp16",
    )
    refiner_pipeline.enable_model_cpu_offload()

    prompt = "a photo of an astronaut riding a horse on mars"
    negative_prompt = "static, frame, painting, illustration, sd character, low quality, low resolution, greyscale, monochrome, nose, cropped, lowres, jpeg artifacts, deformed iris, deformed pupils, bad eyes, semi-realistic worst quality, bad lips, deformed mouth, deformed face, deformed fingers, deformed toes standing still, posing"

    # 무작위성을 제어하여 실험을 재현할 수 있도록 합니다.
    # 시드는 WandB에 자동으로 로깅됩니다.
    seed = 42
    generator_base = torch.Generator(device="cuda").manual_seed(seed)
    generator_refiner = torch.Generator(device="cuda").manual_seed(seed)

    # Diffusers용 WandB Autolog를 호출합니다. 이를 통해 프롬프트,
    # 생성된 이미지, 파이프라인 아키텍처 및 모든 관련 실험 설정이
    # W&B에 자동으로 로깅되어 이미지 생성 실험을
    # 쉽게 재현하고 공유하고 분석할 수 있습니다.
    autolog(init=dict(project="sdxl"))

    # 기본 파이프라인을 호출하여 잠재 표현을 생성합니다
    image = base_pipeline(
        prompt=prompt,
        negative_prompt=negative_prompt,
        output_type="latent",
        generator=generator_base,
    ).images[0]

    # 리파이너 파이프라인을 호출하여 정제된 이미지를 생성합니다
    image = refiner_pipeline(
        prompt=prompt,
        negative_prompt=negative_prompt,
        image=image[None, :],
        generator=generator_refiner,
    ).images[0]

    # 실험을 종료합니다
    run.finish()
    ```
  </Tab>
</Tabs>

다음 이미지는 Stable Diffusion XL + Refiner 실험의 예시입니다.

<Frame>
  <img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/_media/diffusers-autolog-6.gif?s=a4f2130cde06f6b6ca7f7654c338105d" alt="Stable Diffusion XL 실험 추적" width="800" height="418" data-path="products/wandb/_media/diffusers-autolog-6.gif" />
</Frame>

<h2 id="more-resources">
  추가 자료
</h2>

* [Stable Diffusion 프롬프트 엔지니어링 가이드](https://forge.coreweave.com/wandb/geekyrakshit/diffusers-prompt-engineering/reports/A-Guide-to-Prompt-Engineering-for-Stable-Diffusion--Vmlldzo1NzY4NzQ3)
* [PIXART-α: A Diffusion Transformer 모델 for Text-to-Image Generation](https://forge.coreweave.com/wandb/geekyrakshit/pixart-alpha/reports/PIXART-A-Diffusion-Transformer-Model-for-Text-to-Image-Generation--Vmlldzo2MTE1NzM3)
