> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-serverless-sft-revamp.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# MosaicML Composer

> 신경망 학습을 위한 최첨단 알고리즘

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/mosaicml/MosaicML_Composer_and_wandb.ipynb" />

[Composer](https://github.com/mosaicml/composer) 는 신경망 트레이닝을 더 효과적이고 빠르며 저렴하게 수행할 수 있도록 돕는 라이브러리입니다. 이 라이브러리에는 신경망 트레이닝을 가속화하고 일반화 성능을 개선하기 위한 여러 최신 메소드가 포함되어 있으며, 다양한 최적화 기법을 쉽게 조합(composing)할 수 있는 선택적인 [Trainer](https://docs.mosaicml.com/projects/composer/en/stable/trainer/using_the_trainer.html) API를 제공합니다.

W\&B는 ML 실험 로그를 기록하기 위한 경량 래퍼를 제공합니다. 하지만 사용자가 직접 이 둘을 결합할 필요는 없습니다. W\&B는 [WandBLogger](https://docs.mosaicml.com/projects/composer/en/stable/trainer/file_uploading.html#weights-biases-artifacts)를 통해 Composer 라이브러리에 직접 통합되어 있습니다.

## W\&B 로그 기록 시작하기

```python theme={null}
from composer import Trainer
from composer.loggers import WandBLogger

# WandBLogger를 사용하여 Trainer 초기화
trainer = Trainer(..., logger=WandBLogger())
```

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541-serverless-sft-revamp/IRi-6mj09rKBO-jh/images/integrations/n6P7K4M.gif?s=49fad2c62162c86f41a07089f0360941" alt="Interactive dashboards" width="1920" height="1080" data-path="images/integrations/n6P7K4M.gif" />
</Frame>

## Composer의 `WandBLogger` 사용하기

Composer 라이브러리는 `Trainer` 내에서 [WandBLogger](https://docs.mosaicml.com/projects/composer/en/stable/trainer/file_uploading.html#weights-biases-artifacts) 클래스를 사용하여 W\&B에 메트릭을 로그합니다. 로거를 인스턴스화하고 `Trainer`에 전달하기만 하면 될 정도로 간단합니다.

```python theme={null}
wandb_logger = WandBLogger(project="gpt-5", log_artifacts=True)
trainer = Trainer(logger=wandb_logger)
```

## 로거 인수(Logger arguments)

아래는 `WandbLogger`의 파라미터입니다. 전체 리스트와 설명은 [Composer 문서](https://docs.mosaicml.com/projects/composer/en/stable/api_reference/generated/composer.loggers.WandBLogger.html)를 참조하세요.

| 파라미터             | 설명                                                                                                                                                                                                                                                          |
| :--------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `project`        | W\&B Projects 이름 (str, 선택 사항)                                                                                                                                                                                                                               |
| `group`          | W\&B 그룹 이름 (str, 선택 사항)                                                                                                                                                                                                                                     |
| `name`           | W\&B Runs 이름. 지정하지 않으면 State.run\_name이 사용됨 (str, 선택 사항)                                                                                                                                                                                                    |
| `entity`         | 사용자 이름 또는 W\&B Teams 이름과 같은 W\&B Entities 이름 (str, 선택 사항)                                                                                                                                                                                                   |
| `tags`           | W\&B 태그 (List\[str], 선택 사항)                                                                                                                                                                                                                                 |
| `log_artifacts`  | 체크포인트를 wandb에 로그할지 여부, 기본값: `false` (bool, 선택 사항)                                                                                                                                                                                                           |
| `rank_zero_only` | rank-zero 프로세스에서만 로그를 기록할지 여부. Artifacts를 로그할 때는 모든 rank에서 로그를 기록하는 것이 강력히 권장됩니다. rank 1 이상인 프로세스의 Artifacts는 저장되지 않으며, 이로 인해 관련 정보가 누락될 수 있습니다. 예를 들어 Deepspeed ZeRO를 사용할 때 모든 rank의 Artifacts가 없으면 체크포인트에서 복구하는 것이 불가능할 수 있습니다. 기본값: `True` (bool, 선택 사항) |
| `init_kwargs`    | wandb `config` 등 `wandb.init()`에 전달할 파라미터. `wandb.init()`에서 허용하는 파라미터는 [`wandb.init()` 파라미터](/models/ref/python/functions/init)를 참조하세요.                                                                                                                     |

전형적인 사용 예시는 다음과 같습니다:

```python theme={null}
init_kwargs = {"notes":"이 실험에서 더 높은 학습률을 테스트함", 
               "config":{"arch":"Llama",
                         "use_mixed_precision":True
                         }
               }

wandb_logger = WandBLogger(log_artifacts=True, init_kwargs=init_kwargs)
```

## 예측 샘플 로그 기록하기

[Composer의 Callbacks](https://docs.mosaicml.com/projects/composer/en/stable/trainer/callbacks.html) 시스템을 사용하면 `WandBLogger`를 통해 W\&B에 로그를 기록하는 시점을 제어할 수 있습니다. 다음 예시에서는 검증 이미지 샘플과 예측값(prediction)을 로그로 기록합니다:

```python theme={null}
import wandb
from composer import Callback, State, Logger

class LogPredictions(Callback):
    def __init__(self, num_samples=100, seed=1234):
        super().__init__()
        self.num_samples = num_samples
        self.data = []
        
    def eval_batch_end(self, state: State, logger: Logger):
        """배치당 예측값을 계산하고 self.data에 저장합니다"""
        
        if state.timer.epoch == state.max_duration: # 마지막 검증 에포크에서
            if len(self.data) < self.num_samples:
                n = self.num_samples
                x, y = state.batch_pair
                outputs = state.outputs.argmax(-1)
                # 이미지, 그라운드 트루스, 예측값을 리스트로 구성
                data = [[wandb.Image(x_i), y_i, y_pred] for x_i, y_i, y_pred in list(zip(x[:n], y[:n], outputs[:n]))]
                self.data += data
            
    def eval_end(self, state: State, logger: Logger):
        with wandb.init() as run:
            "wandb.Table을 생성하고 로그를 기록합니다"
            columns = ['image', 'ground truth', 'prediction']
            table = wandb.Table(columns=columns, data=self.data[:self.num_samples])
            run.log({'sample_table':table}, step=int(state.timer.batch))         
...

trainer = Trainer(
    ...
    loggers=[WandBLogger()],
    callbacks=[LogPredictions()]
)
```
