
# ------------------------------------------------------------------------------------------------
# WARNING: HARDWARE REQUIREMENTS
# ------------------------------------------------------------------------------------------------
# Ling-1T (BailingMoeV2: 1T total / ~50B activated, 80 layers, 256 experts,
# first_k_dense_replace=4).  Full SFT requires ~12 TB for params + grads +
# Adam state in fp32.
# Minimum:    256x H100 80GB (32 nodes) -- 47 GB/rank, comfortable.
# Lower bound: 128x H100 80GB (16 nodes) -- 94 GB/rank, will OOM without offload.
# Use ling_1t_lora_pp.yaml (32 GPUs) if full SFT is not feasible.
# ------------------------------------------------------------------------------------------------
#
# Topology: PP=4, EP=64; FSDP shards the rest.
# Uses dispatcher=hybridep (DeepEP times out at hidden_size=8192).
# Requires env vars (set in ci.env_vars):
#   NUM_OF_HYBRID_EP_RANKS_PER_NVLINK_DOMAIN=8
#   NUM_OF_STAGES_G2S_COMBINE_API=4   # multi-node ep=64 backward combine
#
# To run:
#   automodel examples/llm_finetune/ling/ling_1t_sft.yaml --nproc-per-node 8

recipe: TrainFinetuneRecipeForNextTokenPrediction

step_scheduler:
  global_batch_size: 512
  local_batch_size: 4
  ckpt_every_steps: 500
  val_every_steps: 500
  num_epochs: 2

dist_env:
  backend: nccl
  timeout_minutes: 30

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-1T
  torch_dtype: bfloat16
  backend:
    _target_: nemo_automodel.components.models.common.BackendConfig
    attn: sdpa
    linear: te
    rms_norm: torch_fp32
    experts: torch_mm
    dispatcher: hybridep
    fake_balanced_gate: false
    enable_hf_state_dict_adapter: true
    enable_fsdp_optimizations: true
    rope_fusion: false

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

distributed:
  strategy: fsdp2
  tp_size: 1
  cp_size: 1
  pp_size: 4
  ep_size: 64
  sequence_parallel: false
  activation_checkpointing: true

  pipeline:
    pp_schedule: interleaved1f1b
    pp_microbatch_size: 1
    round_virtual_stages_to_pp_multiple: down
    scale_grads_in_schedule: false
    layers_per_stage: 5  # 80 / pp=4 / virtual=4
    patch_inner_model: false
    patch_causal_lm_model: false

  moe:
    reshard_after_forward: false
    wrap_outer_model: false

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

dataset:
  _target_: nemo_automodel.components.datasets.llm.hellaswag.HellaSwag
  path_or_dataset: rowan/hellaswag
  split: train

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

validation_dataset:
  _target_: nemo_automodel.components.datasets.llm.hellaswag.HellaSwag
  path_or_dataset: rowan/hellaswag
  split: validation
  num_samples_limit: 128

validation_dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  collate_fn:
    _target_: nemo_automodel.components.datasets.utils.default_collater
    pad_seq_len_divisible: 160
  num_workers: 4

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

lr_scheduler:
  lr_decay_style: cosine
  min_lr: 1.0e-6

ci:
  recipe_owner: Hayden727
  nodes: 32
  time: "01:00:00"
  env_vars:
    NUM_OF_HYBRID_EP_RANKS_PER_NVLINK_DOMAIN: "8"
    NUM_OF_STAGES_G2S_COMBINE_API: "4"
