
# ------------------------------------------------------------------------------------------------
# Ling-mini-2.0 (BailingMoeV2: 16B total / 1.43B activated) LoRA SFT on SQuAD.
# ------------------------------------------------------------------------------------------------
# Hardware: tested on 4x H100 80GB with FSDP2; LoRA + activation checkpointing
# keeps per-rank memory under 25 GB.  For more / fewer GPUs, only adjust
# --nproc-per-node; the YAML's per-rank fields stay the same.
# ------------------------------------------------------------------------------------------------
#
# To run:
#   automodel examples/llm_finetune/ling/ling_mini_2_0_squad.yaml --nproc-per-node 4
#
# Architecture notes for BailingMoeV2 (shared by Ling-mini / -flash / -1T):
#   - GQA attention (16 / 4 heads on mini) with per-head QK-RMSNorm.
#   - Half-RoPE: partial_rotary_factor=0.5 rotates only first 64 / 128 dims.
#   - Sigmoid routing + per-expert bias (aux-loss-free), grouped topk
#     (n_group=8, topk_group=4), 1 shared expert.
#   - first_k_dense_replace=1: layer 0 uses a dense MLP, layers 1-19 are MoE.

recipe: TrainFinetuneRecipeForNextTokenPrediction

step_scheduler:
  global_batch_size: 32
  local_batch_size: 4
  ckpt_every_steps: 200
  val_every_steps: 100
  num_epochs: 1
  max_steps: 500

dist_env:
  backend: nccl
  timeout_minutes: 20

rng:
  _target_: nemo_automodel.components.training.rng.StatefulRNG
  seed: 42
  ranked: true

model:
  _target_: nemo_automodel.NeMoAutoModelForCausalLM.from_pretrained
  pretrained_model_name_or_path: inclusionAI/Ling-mini-2.0
  torch_dtype: bfloat16
  backend:
    _target_: nemo_automodel.components.models.common.BackendConfig
    attn: sdpa
    linear: torch
    rms_norm: torch_fp32
    experts: torch_mm
    dispatcher: torch
    enable_hf_state_dict_adapter: true

peft:
  _target_: nemo_automodel.components._peft.lora.PeftConfig
  target_modules: '*_proj'
  dim: 16
  alpha: 32
  dropout: 0.0

checkpoint:
  enabled: true
  checkpoint_dir: checkpoints/ling_mini_2_0_lora
  model_save_format: safetensors
  save_consolidated: true

distributed:
  strategy: fsdp2
  tp_size: 1
  cp_size: 1
  pp_size: 1
  # ep_size must match --nproc-per-node (the framework's HF state-dict adapter
  # for grouped MoE experts requires an `ep` mesh dimension to split sharded
  # expert tensors back into per-expert HF tensors).  Override from the CLI
  # if your GPU count differs from the recommended 4.
  ep_size: 4
  sequence_parallel: false
  activation_checkpointing: true

loss_fn:
  _target_: nemo_automodel.components.loss.masked_ce.MaskedCrossEntropy

dataset:
  _target_: nemo_automodel.components.datasets.llm.squad.make_squad_dataset
  dataset_name: rajpurkar/squad
  split: train
  seq_length: 2048

dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  collate_fn: nemo_automodel.components.datasets.utils.default_collater
  shuffle: true
  num_workers: 2

validation_dataset:
  _target_: nemo_automodel.components.datasets.llm.squad.make_squad_dataset
  dataset_name: rajpurkar/squad
  split: validation
  seq_length: 2048
  limit_dataset_samples: 128

validation_dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  collate_fn: nemo_automodel.components.datasets.utils.default_collater
  num_workers: 2

optimizer:
  _target_: torch.optim.AdamW
  lr: 1.0e-4
  weight_decay: 0.01
  betas: [0.9, 0.95]
  eps: 1e-8

lr_scheduler:
  lr_decay_style: cosine
  min_lr: 1.0e-6
