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

# PyTorch torchtune

> WandBLogger metric logger로 LLM fine-tuning 실험을 추적하려면 PyTorch torchtune에서 W&B 로깅을 사용하세요.

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>
    Colab에서 사용해 보기
  </a>;

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

[torchtune](https://meta-pytorch.org/torchtune/stable/index.html)은 대규모 언어 모델(LLM)의 작성, 파인튜닝, 실험 과정을 간소화하도록 설계된 PyTorch 기반 라이브러리입니다. 또한 torchtune은 [W\&B 로깅](https://meta-pytorch.org/torchtune/stable/deep_dives/wandb_logging.html)을 기본으로 지원하여 트레이닝 과정의 추적과 시각화를 강화합니다.

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541/w-lBKSCruauC3-2f/images/integrations/torchtune_dashboard.png?fit=max&auto=format&n=w-lBKSCruauC3-2f&q=85&s=b2ff48f4797a1d2c55b760c210c95408" alt="TorchTune 트레이닝 대시보드" width="1942" height="1286" data-path="images/integrations/torchtune_dashboard.png" />
</Frame>

[torchtune을 사용한 Mistral 7B 파인튜닝](https://wandb.ai/capecape/torchtune-mistral/reports/torchtune-The-new-PyTorch-LLM-fine-tuning-library---Vmlldzo3NTUwNjM0)에 관한 W\&B 블로그 게시물을 확인하세요.

<div id="wb-logging-at-your-fingertips">
  ## 간편하게 사용하는 W\&B 로깅
</div>

<Tabs>
  <Tab title="명령줄">
    실행 시 명령줄 인수를 재정의하세요:

    ```bash theme={null}
    tune run lora_finetune_single_device --config llama3/8B_lora_single_device \
      metric_logger._component_=torchtune.utils.metric_logging.WandBLogger \
      metric_logger.project="llama3_lora" \
      log_every_n_steps=5
    ```
  </Tab>

  <Tab title="레시피">
    레시피 설정에서 W\&B 로깅을 활성화하세요:

    ```yaml theme={null}
    # llama3/8B_lora_single_device.yaml 내부
    metric_logger:
      _component_: torchtune.utils.metric_logging.WandBLogger
      project: llama3_lora
    log_every_n_steps: 5
    ```
  </Tab>
</Tabs>

<div id="use-the-wb-metric-logger">
  ## W\&B metric logger 사용
</div>

`metric_logger` 섹션을 수정해 레시피의 설정 파일에서 W\&B 로깅을 활성화합니다. `_component_`를 `torchtune.utils.metric_logging.WandBLogger` 클래스로 변경합니다. 로깅 동작을 사용자 지정하려면 `project` 이름과 `log_every_n_steps`도 전달할 수 있습니다.

또한 [wandb.init()](/ko/models/ref/python/functions/init) 방법에 전달하듯이 다른 `kwargs`도 전달할 수 있습니다. 예를 들어 팀에서 작업하는 경우 `entity` 인수를 `WandBLogger` 클래스에 전달해 팀 이름을 지정할 수 있습니다.

<Tabs>
  <Tab title="레시피">
    ```yaml theme={null}
    # llama3/8B_lora_single_device.yaml 내부
    metric_logger:
      _component_: torchtune.utils.metric_logging.WandBLogger
      project: llama3_lora
      entity: my_project
      job_type: lora_finetune_single_device
      group: my_awesome_experiments
    log_every_n_steps: 5
    ```
  </Tab>

  <Tab title="명령줄">
    ```shell theme={null}
    tune run lora_finetune_single_device --config llama3/8B_lora_single_device \
      metric_logger._component_=torchtune.utils.metric_logging.WandBLogger \
      metric_logger.project="llama3_lora" \
      metric_logger.entity="my_project" \
      metric_logger.job_type="lora_finetune_single_device" \
      metric_logger.group="my_awesome_experiments" \
      log_every_n_steps=5
    ```
  </Tab>
</Tabs>

<div id="what-is-logged">
  ## 무엇이 로깅되나요?
</div>

로깅된 메트릭은 W\&B 대시보드에서 확인할 수 있습니다. 기본적으로 W\&B는 설정 파일의 모든 하이퍼파라미터와 launch override를 로깅합니다.

W\&B는 **Overview** 탭에 최종적으로 해석된 설정을 캡처합니다. W\&B는 [Files 탭](https://wandb.ai/capecape/torchtune/runs/joyknwwa/files)에도 설정을 YAML 형식으로 저장합니다.

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541/w-lBKSCruauC3-2f/images/integrations/torchtune_config.png?fit=max&auto=format&n=w-lBKSCruauC3-2f&q=85&s=80ee1d6653d94ebea2cef75ced3bcd41" alt="TorchTune 설정" width="1806" height="1362" data-path="images/integrations/torchtune_config.png" />
</Frame>

<div id="logged-metrics">
  ### 로깅된 메트릭
</div>

각 레시피마다 자체 트레이닝 루프가 있습니다. 기본적으로 로깅되는 메트릭은 각 레시피 문서에서 확인할 수 있으며, 다음이 포함됩니다:

| Metric              | 설명                                                                                                      |
| ------------------- | ------------------------------------------------------------------------------------------------------- |
| `loss`              | 모델의 손실                                                                                                  |
| `lr`                | 학습률                                                                                                     |
| `tokens_per_second` | 모델의 초당 토큰 수                                                                                             |
| `grad_norm`         | 모델의 그라디언트 노름                                                                                            |
| `global_step`       | 트레이닝 루프의 현재 step에 해당합니다. 그라디언트 누적이 반영되므로, 그라디언트는 누적되고 모델은 `gradient_accumulation_steps`마다 한 번씩 업데이트됩니다. |

<Note>
  `global_step`은 트레이닝 step 수와 동일하지 않습니다. 트레이닝 루프의 현재 step을 나타내며, 그라디언트 누적이 반영됩니다. 즉, 옵티마이저 step이 수행될 때마다 `global_step`이 1씩 증가합니다. 예를 들어 dataloader에 batch가 10개 있고 gradient accumulation steps가 2이며 3 에포크 동안 실행하면, 옵티마이저는 총 15번 step을 수행합니다. 이 경우 `global_step`은 1부터 15까지의 값을 가집니다.
</Note>

torchtune의 간결한 설계 덕분에 커스텀 메트릭을 쉽게 추가하거나 기존 메트릭을 수정할 수 있습니다. 해당 [레시피 파일](https://github.com/meta-pytorch/torchtune/tree/main/recipes)만 수정하면 됩니다. 예를 들어 `current_epoch`를 전체 에포크 수 대비 백분율로 계산해 로깅하려면 다음과 같이 할 수 있습니다:

```python theme={null}
# 레시피 파일의 `train.py` 함수 내부
self._metric_logger.log_dict(
    {"current_epoch": self.epochs * self.global_step / self._steps_per_epoch},
    step=self.global_step,
)
```

<Note>
  이 라이브러리는 매우 빠르게 발전하고 있으므로 현재 메트릭은 변경될 수 있습니다. 맞춤형 메트릭을 추가하려면 레시피를 수정하고 해당 `self._metric_logger.*` 함수를 호출해야 합니다.
</Note>

<div id="save-and-load-checkpoints">
  ## 체크포인트 저장 및 불러오기
</div>

torchtune 라이브러리는 다양한 [체크포인트 형식](https://meta-pytorch.org/torchtune/stable/deep_dives/checkpointer.html)을 지원합니다. 사용 중인 모델의 출처에 따라 적절한 [checkpointer 클래스](https://meta-pytorch.org/torchtune/stable/deep_dives/checkpointer.html)로 전환해야 합니다.

모델 체크포인트를 [W\&B Artifacts](/ko/models/artifacts/)에 저장하려면, 가장 간단한 방법은 해당 레시피 내부의 `save_checkpoint` 함수를 재정의하는 것입니다.

다음은 모델 체크포인트를 W\&B Artifacts에 저장하도록 `save_checkpoint` 함수를 재정의하는 예시입니다.

```python theme={null}
def save_checkpoint(self, epoch: int) -> None:
    ...
    ## checkpoint를 W&B에 저장합니다
    ## Checkpointer Class에 따라 파일 이름이 달라집니다
    ## full_finetune 케이스의 예시입니다
    checkpoint_file = Path.joinpath(
        self._checkpointer._output_dir, f"torchtune_model_{epoch}"
    ).with_suffix(".pt")
    wandb_artifact = wandb.Artifact(
        name=f"torchtune_model_{epoch}",
        type="model",
        # 모델 checkpoint에 대한 설명
        description="Model checkpoint",
        # dict 형태로 원하는 메타데이터를 추가할 수 있습니다
        metadata={
            utils.SEED_KEY: self.seed,
            utils.EPOCHS_KEY: self.epochs_run,
            utils.TOTAL_EPOCHS_KEY: self.total_epochs,
            utils.MAX_STEPS_KEY: self.max_steps_per_epoch,
        },
    )
    wandb_artifact.add_file(checkpoint_file)
    wandb.log_artifact(wandb_artifact)
```
