# NeMo Automodel

[NeMo Automodel](https://github.com/NVIDIA-NeMo/Automodel) は、NVIDIA によるオープンソースの PyTorch DTensor ネイティブな学習ライブラリです。[LLM](https://docs.nvidia.com/nemo/automodel/latest/model-coverage/large-language-models/overview) および [VLM](https://docs.nvidia.com/nemo/automodel/latest/model-coverage/vision-language-models/overview) 的大規模・小規模な事前学習とファインチューニングをサポートし、FSDP2、テンソル並列、パイプライン並列、エキスパート並列、コンテキスト並列などの並列化戦略を備えています。これにより、研究環境および本番環境での高速な実験が可能です。高スループットを実現するため、DeepEP および TransformerEngine のカーネルを統合しています。

YAML設定ファイルでトレーニング実行を定義します（完全な[設定ファイル](https://github.com/NVIDIA-NeMo/Automodel/blob/106e5162ef5b5d7269a3ba1ef62bfa8941b26cc6/examples/llm_finetune/nemotron/nemotron_nano_v3_hellaswag_peft.yaml)を参照）。

```yaml
# Instantiate a Nemotron V3 Nano model
model:
  _target_: nemo_automodel.NeMoAutoModelForCausalLM.from_pretrained
  pretrained_model_name_or_path: nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16

# Run SFT on HellaSwag
dataset:
  _target_: nemo_automodel.components.datasets.llm.hellaswag.HellaSwag
  path_or_dataset: rowan/hellaswag
  split: train

# Train PEFT adapters
peft:
  _target_: nemo_automodel.components._peft.lora.PeftConfig
  exclude_modules: ["*.out_proj"]  # mamba layers use custom kernels that take in the out_proj.weight directly, thus lora doesn't work here.
  dim: 8
  alpha: 32
  use_triton: True

# Use EP + FSDP2 for training
distributed:
  strategy: fsdp2
  dp_size: none
  tp_size: 1
  cp_size: 1
  ep_size: 4

# ... other parameters
```

以下のコマンドを使用して、`torchrun`でトレーニングを開始します。

```bash
torchrun --nproc-per-node=4 examples/llm_finetune/finetune.py -c /path/to/yaml
```

## Transformers の統合

- Transformers でサポートされている LLM や VLM は、すべて NeMo Automodel 経由でもインスタンス化できます。[対応モデルの全リスト](https://docs.nvidia.com/nemo/automodel/latest/model-coverage/overview)を参照してください。
- Hugging Face のモデルを基盤とし、[AutoModel.from_pretrained()](/docs/transformers/main/ja/model_doc/auto#transformers.AutoModel.from_pretrained) を使用して構築されています。動的な高性能レイヤーの差し替えや、Expert Parallelism (EP) といったより洗練された並列化方式に対応しています。
- [AutoConfig.from_pretrained()](/docs/transformers/main/ja/model_doc/auto#transformers.AutoConfig.from_pretrained)内のarchitectureフィールドを検出し、Nemotron Nano V3などのカスタム実装を自動的に読み込みます。
- TransformersのAPIに忠実に従っており、そのまま置き換え可能な互換性を備えています。

## リソース

- [NeMo Automodel](https://github.com/NVIDIA-NeMo/Automodel)
- [NeMo Transformers API](https://docs.nvidia.com/nemo/automodel/latest/get-started/hf-compatibility)
- NeMo AutomodelのdenseモデルおよびMixture-of-Expert (MoE)の[ベンチマーク](https://docs.nvidia.com/nemo/automodel/latest/performance/performance-summary)
- NeMoの[事前学習](./nemo_automodel_pretraining)ガイドを参照して、NeMoを用いた事前学習の使用方法を学びましょう。

