# 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 のカーネルを統合しています。

```py
# Instantiating Nemotron V3 Nano with Expert Parallelism, FSDP2, and TransformerEngine + DeepEP kernels.
import os

import torch
import torch.distributed as dist

from nemo_automodel import NeMoAutoModelForCausalLM
from nemo_automodel.recipes._dist_utils import create_distributed_setup_from_config

dist.init_process_group(backend="nccl")
torch.cuda.set_device(int(os.environ.get("LOCAL_RANK", 0)))
torch.manual_seed(1111)

dist_setup = create_distributed_setup_from_config(
    {
        "strategy": "fsdp2",
        "ep_size": 8,
    },
)
model = NeMoAutoModelForCausalLM.from_pretrained(
    "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16",
    dtype=torch.bfloat16,
    distributed_setup=dist_setup,
)
print(model)
dist.destroy_process_group()
```
以下のコマンドを使用して、`torchrun`でスクリプトを起動します。

```bash
torchrun --nproc-per-node=8 /path/to/script
```

## 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_finetuning)ガイドを参照し、NeMoを使用してファインチューニングを行う方法を学習してください。

