# Copyright (c) 2025, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Fine-tune Llama-3.2-1B on SQuAD using SkyPilot for cloud execution.
#
# Usage:
#   automodel examples/llm_finetune/llama3_2/llama3_2_1b_squad_skypilot.yaml
#
# Prerequisites:
#   pip install "skypilot[gcp]"   # or [aws], [azure], etc.
#   sky check                     # verify cloud credentials
#
# Monitor:
#   sky status
#   sky logs <cluster_name>

# ---------------------------------------------------------------------------
# SkyPilot launcher config (removed before the job config reaches the VM)
# ---------------------------------------------------------------------------
skypilot:
  cloud: gcp                  # aws | gcp | azure | lambda | kubernetes
  accelerators: T4:1          # GPU type:count per node
  use_spot: true              # ~80 % cost reduction vs on-demand
  disk_size: 100              # GB
  num_nodes: 1
  region: us-central1         # optional; SkyPilot picks cheapest if omitted
  job_name: llama3_2_1b_squad

  # Credentials – use env-var placeholders so secrets are never stored in YAML.
  hf_token: ${HF_TOKEN}
  # wandb_key: ${WANDB_API_KEY}

  # Extra setup commands run on the VM after `pip install -e .`
  # setup: |
  #   pip install some-extra-dependency

# ---------------------------------------------------------------------------
# Training config (forwarded to the VM unchanged)
# ---------------------------------------------------------------------------
recipe:
  _target_: nemo_automodel.recipes.llm.train_ft.TrainFinetuneRecipeForNextTokenPrediction

step_scheduler:
  global_batch_size: 64
  local_batch_size: 8
  ckpt_every_steps: 1000
  val_every_steps: 10
  num_epochs: 1

dist_env:
  backend: nccl
  timeout_minutes: 1

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

model:
  _target_: nemo_automodel.NeMoAutoModelForCausalLM.from_pretrained
  pretrained_model_name_or_path: meta-llama/Llama-3.2-1B

compile:
  enabled: false
  mode: "default"
  fullgraph: false
  dynamic: true
  backend: null

clip_grad_norm:
  max_norm: 1.0

distributed:
  strategy: fsdp2
  dp_size: none
  tp_size: 1
  cp_size: 1

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

packed_sequence:
  packed_sequence_size: 0

dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  collate_fn:
    _target_: nemo_automodel.components.datasets.utils.default_collater
  shuffle: false

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

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

optimizer:
  _target_: torch.optim.Adam
  betas: [0.9, 0.999]
  eps: 1e-8
  lr: 1.0e-5
  weight_decay: 0

lr_scheduler:
  lr_decay_style: cosine
  min_lr: 1.0e-6

ci:
  recipe_owner: adil-a
