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

# VLM finetuning config for Mistral 4 (sinistea-small-4-119B) on MedPix
# Replace /path/to/sinistea-small-4-119b with the path to the Mistral 4 checkpoint

recipe: FinetuneRecipeForVLM

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

dist_env:
  backend: nccl
  timeout_minutes: 10

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

model:
  _target_: nemo_automodel.NeMoAutoModelForImageTextToText.from_pretrained
  pretrained_model_name_or_path: mistralai/Mistral-Small-4-119B-2603
  backend:
    _target_: nemo_automodel.components.models.common.BackendConfig
    attn: sdpa
    linear: te
    rms_norm: te
    rope_fusion: false
    enable_hf_state_dict_adapter: true
    enable_fsdp_optimizations: true

checkpoint:
  enabled: false
  dequantize_base_checkpoint: true

distributed:
  strategy: fsdp2
  tp_size: 1
  cp_size: 1
  pp_size: 4
  ep_size: 8

  sequence_parallel: false
  activation_checkpointing: false

  pipeline:
    pp_schedule: interleaved1f1b
    pp_microbatch_size: 1
    round_virtual_stages_to_pp_multiple: down
    scale_grads_in_schedule: false
    layers_per_stage: 4
    patch_inner_model: false
    patch_causal_lm_model: false

  moe:
    reshard_after_forward: false

freeze_config:
  freeze_embeddings: true
  freeze_vision_tower: true
  freeze_language_model: false

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

dataset:
  _target_: nemo_automodel.components.datasets.vlm.datasets.make_medpix_dataset
  path_or_dataset: mmoukouba/MedPix-VQA
  split: train

dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  num_workers: 1
  collate_fn:
    _target_: nemo_automodel.components.datasets.vlm.collate_fns.default_collate_fn
    max_length: 2048
  drop_last: true

validation_dataset:
  _target_: nemo_automodel.components.datasets.vlm.datasets.make_medpix_dataset
  path_or_dataset: mmoukouba/MedPix-VQA
  split: validation

validation_dataloader:
  _target_: torchdata.stateful_dataloader.StatefulDataLoader
  num_workers: 1
  collate_fn:
    _target_: nemo_automodel.components.datasets.vlm.collate_fns.default_collate_fn

optimizer:
  _target_: torch.optim.Adam
  betas: [0.9, 0.95]
  eps: 1e-7
  lr: 1e-5
  weight_decay: 0.0

wandb:
  project: huiyingl_workspace
  entity: Nemo-automodel
  name: mistral4-medpix

ci:
  recipe_owner: akoumpa
  nodes: 4
  time: "00:30:00"
