# 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.

# This example demonstrates FP8 quantization for improved training performance on H100+ GPUs.

step_scheduler:
  global_batch_size: 8
  local_batch_size: 1
  ckpt_every_steps: 50
  val_every_steps: 50  # will run every x number of gradient steps
  max_steps: 100

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: zai-org/glm-4-9b-chat-hf
  torch_dtype: bf16
  trust_remote_code: true

checkpoint:
  enabled: true
  checkpoint_dir: checkpoints_fp8/
  model_save_format: torch_save
  save_consolidated: false 

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

  sequence_parallel: false

compile:
  enabled: true
  mode: "default"  # Options: "default", "reduce-overhead", "max-autotune"
  fullgraph: false
  dynamic: false  # Set to false for better performance with fixed shapes
  backend: null  # Use default backend (inductor)

fp8:
  enabled: true
  recipe_name: tensorwise  # Options: tensorwise, rowwise, rowwise_with_gw_hp
  enable_fsdp_float8_all_gather: true
  precompute_float8_dynamic_scale_for_fsdp: true
  force_recompute_fp8_weight_in_bwd: true
  filter_fqns: ["lm_head"]
  emulate: false  # Set to true for testing on older GPUs without native FP8 support

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

packed_sequence:
  # Set packed_sequence_size > 0 to run with packed sequences
  packed_sequence_size: 8192

dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  collate_fn:
    _target_: nemo_automodel.components.datasets.utils.default_collater
    pad_seq_len_divisible: 16 # fp8 requires divisible by 16
  shuffle: false

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

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

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

# Uncomment and configure for W&B logging
# wandb:
#   project: <your_wandb_project>
#   entity: <your_wandb_entity>
#   name: <your_wandb_exp_name>
#   save_dir: <your_wandb_save_dir>
