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

# LoRA fine-tune of LLaVA-OneVision-1.5-8B-Instruct on the RDR image-description dataset.
#
# To run this recipe:
#   automodel examples/vlm_finetune/llava_onevision/llava_ov_1_5_8b_lora.yaml --nproc-per-node 8

recipe: FinetuneRecipeForVLM

step_scheduler:
  global_batch_size: 8
  local_batch_size: 1
  ckpt_every_steps: 50
  val_every_steps: 50
  num_epochs: 3

dist_env:
  backend: nccl
  timeout_minutes: 10

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

model:
  _target_: nemo_automodel.NeMoAutoModelForImageTextToText.from_pretrained
  pretrained_model_name_or_path: lmms-lab/LLaVA-OneVision-1.5-8B-Instruct
  attn_implementation: flash_attention_2
  # The hub config.json carries an auto_map for the remote reference impl;
  # NeMo routes to its own custom class via config.architectures[0], but HF's
  # AutoConfig still demands trust_remote_code=True when auto_map is present.
  trust_remote_code: true

processor:
  _target_: transformers.AutoProcessor.from_pretrained
  pretrained_model_name_or_path: lmms-lab/LLaVA-OneVision-1.5-8B-Instruct
  trust_remote_code: true

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

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

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

dataset:
  _target_: nemo_automodel.components.datasets.vlm.datasets.make_rdr_dataset
  path_or_dataset: quintend/rdr-items
  split: train

dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  num_workers: 0
  pin_memory: true
  collate_fn:
    _target_: nemo_automodel.components.datasets.vlm.collate_fns.llava_onevision_collate_fn

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

peft:
  _target_: nemo_automodel.components._peft.lora.PeftConfig
  match_all_linear: false
  # Rice ViT uses a fused qkv layer (no *_proj suffixes) and GeLU MLP with
  # fc1/fc2, so these suffix patterns naturally target the Qwen3 text backbone
  # only; additionally keep the merger projector out with an explicit exclusion.
  target_modules:
    - "*q_proj"
    - "*k_proj"
    - "*v_proj"
    - "*o_proj"
    - "*gate_proj"
    - "*up_proj"
    - "*down_proj"
  dim: 16
  alpha: 32
  dropout: 0.05
  use_triton: true

freeze_config:
  freeze_vision_tower: true
  freeze_language_model: false

# Uncomment and configure for W&B logging
# wandb:
#   project:
#   entity:
#   name:
#   save_dir:

ci:
  recipe_owner: akoumpa
