# Qwen 2.5 VL 3B full fine-tune training config.
#
# Requirements:
#   - Log into WandB (`wandb login`) or disable `enable_wandb`
#   - (optional) If you want to use flash attention, run `pip install -U flash-attn --no-build-isolation`
#
# Usage:
#   oumi train -c configs/recipes/vision/qwen2_5_vl_3b/sft/full/train.yaml
#
# See Also:
#   - Documentation: https://oumi.ai/docs/en/latest/user_guides/train/train.html
#   - Config class: oumi.core.configs.TrainingConfig
#   - Config source: https://github.com/oumi-ai/oumi/blob/main/src/oumi/core/configs/training_config.py
#   - Other training configs: configs/**/*train.yaml

model:
  model_name: "Qwen/Qwen2.5-VL-3B-Instruct"
  model_max_length: 16384
  torch_dtype_str: "bfloat16"
  attn_implementation: "sdpa"
  load_pretrained_weights: True
  trust_remote_code: True
  enable_liger_kernel: False
  chat_template: "qwen2-vl-instruct"

data:
  train:
    collator_name: "vision_language_with_padding"
    use_torchdata: True
    datasets:
      - dataset_name: "merve/vqav2-small"
        split: "validation"
        shuffle: True
        seed: 42
        transform_num_workers: "auto"
        dataset_kwargs:
          processor_name: "Qwen/Qwen2.5-VL-3B-Instruct"
          # limit: 4096 # Uncomment to limit dataset size!
          return_tensors: True
      # Or, to use another dataset for training:
      # - dataset_name: "HuggingFaceH4/llava-instruct-mix-vsft"
      #   split: "train"
      #   shuffle: True
      #   seed: 42
      #   transform_num_workers: "auto"
      #   dataset_kwargs:
      #     processor_name: "Qwen/Qwen2.5-VL-3B-Instruct"
      #     return_tensors: True
      # Note: If the images of the dataset are large they may cause OOM errors.
      # Consider resizing the images or using GPUs with more memory.

training:
  output_dir: "output/vlm_finetuned"
  trainer_type: "TRL_SFT"
  enable_gradient_checkpointing: True
  per_device_train_batch_size: 1 # Must be 1: the model generates variable-sized image features
  gradient_accumulation_steps: 1
  # If this is not passed, checkpoints may be saved which are suitable for resuming training but not for loading from HF
  save_final_model: True
  gradient_checkpointing_kwargs:
    # Reentrant docs: https://pytorch.org/docs/stable/checkpoint.html#torch.utils.checkpoint.checkpoint
    use_reentrant: False
  ddp_find_unused_parameters: False
  empty_device_cache_steps: 1
  compile: False

  optimizer: "adamw_torch_fused"
  learning_rate: 2e-5
  warmup_ratio: 0.03
  weight_decay: 0.01
  lr_scheduler_type: "cosine"

  logging_steps: 50
  save_steps: 0
  dataloader_num_workers: 64  # Match LIMO preprocessing workers
  dataloader_prefetch_factor: 32
  include_performance_metrics: True
  log_model_summary: False
  enable_wandb: True

fsdp:
  enable_fsdp: True
  sharding_strategy: "FULL_SHARD"  # Similar to DeepSpeed ZeRO-3
  forward_prefetch: True
  use_orig_params: True
  auto_wrap_policy: "TRANSFORMER_BASED_WRAP"
  transformer_layer_cls: "transformers.models.qwen2_5_vl.modeling_qwen2_5_vl.Qwen2_5_VLDecoderLayer"
