Server Fine-Tuning Quick Guide (LoRA, multi-GPU)

1) Activate the conda env
- source /mydata/<USER>/<USER>/conda/anaconda3/etc/profile.d/conda.sh
- conda create -n VLM-ft python=3.10 -y
- conda activate VLM-ft

Hugging Face authentication (needed for gated/private models like some Meta Llama repos)
- Option A (recommended; persists on this account):
  - python -m pip install -U huggingface_hub
  - hf auth login
  - hf auth whoami
- Option B (non-interactive; for batch jobs):
  - export HF_TOKEN=<your_token>
  - python -c "from huggingface_hub import whoami; import os; print(whoami(token=os.environ['HF_TOKEN']))"

2) (One-time) Install dependencies in VLM-ft
- python -m pip install -U pip
- # NOTE: Recent Transformers versions require torch>=2.6 when loading legacy *.bin weights
- # (security restriction around torch.load). The cu124 index currently tops out at torch==2.5.1.
- # Use the cu128 wheels to get torch>=2.6 on this server.
- python -m pip install --index-url https://download.pytorch.org/whl/cu128 -U torch torchvision
- python -m pip install -U transformers accelerate peft pillow einops timm sentencepiece

Sanity-check torch + CUDA
- python -c "import torch; print('torch', torch.__version__, 'cuda', torch.version.cuda, 'avail', torch.cuda.is_available())"

3) Verify required local files
- Datasets (used directly; no splitting):
  - /mydata/<USER>/vlm_conversations_train.json
  - /mydata/<USER>/vlm_conversations_val.json
- Finetune helper files (must already exist):
  - /mydata/<USER>/VLM_finetune/finetune/finetune.py
  - /mydata/<USER>/VLM_finetune/finetune/data_mix.py
  - /mydata/<USER>/VLM_finetune/finetune/ixc_utils.py
  - /mydata/<USER>/VLM_finetune/finetune/ds_config_zero2.json

4) Dry-run (no training) to confirm GPUs + paths
- python /mydata/<USER>/FT.py --no_launch

5) Launch multi-GPU LoRA fine-tuning (uses ALL visible GPUs)
- python /mydata/<USER>/FT.py \
    --output_dir /mydata/<USER>/outputs/lora_run_001

If you get CUDA OOM (common with --max_length 8192):
- python /mydata/<USER>/FT.py --cuda_visible_devices 2,3 \
  --max_length 4096 --img_size 336 --hd_num 4 \
  --output_dir /mydata/<USER>/outputs/lora_run_001
- (optional) add --use_deepspeed to offload optimizer/params to CPU (slower but uses less GPU memory)

Optional: choose GPUs
- CUDA_VISIBLE_DEVICES=2,3 python /mydata/<USER>/FT.py --output_dir /mydata/<USER>/outputs/lora_run_001
- or: python /mydata/<USER>/FT.py --cuda_visible_devices 2,3 --output_dir /mydata/<USER>/outputs/lora_run_001

Keep same params but split model across 2 GPUs (ZeRO-3)
- python /mydata/<USER>/FT.py --cuda_visible_devices 2,3 --use_deepspeed \
  --deepspeed_config /mydata/<USER>/VLM_finetune/finetune/ds_config_zero3.json \
  --output_dir /mydata/<USER>/outputs/lora_run_001

Keep same params but split across 2 GPUs using FSDP (no CUDA toolkit needed)
- python /mydata/<USER>/FT.py --cuda_visible_devices 2,3 \
  --use_fsdp --fsdp_policy "full_shard auto_wrap" \
  --fsdp_wrap_cls InternLM2DecoderLayer \
  --output_dir /mydata/<USER>/outputs/lora_run_001


Optional: save merged full model after training
- python /mydata/<USER>/FT.py \
    --output_dir /mydata/<USER>/outputs/lora_run_001 \
    --save_merged_model

Outputs
- LoRA adapters are saved inside --output_dir (look for adapter_config.json + adapter_model.safetensors).
- If --save_merged_model is used: merged model is saved to <output_dir>/merged_model.

Note
- If FT.py prints warnings about missing image paths, training can fail; ensure images exist at the paths stored in the JSON files.