|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# ============================================================================== |
| 17 | +# Qwen2-Audio 7B SFT (Supervised Fine-Tuning) Script |
| 18 | +# |
| 19 | +# Usage: |
| 20 | +# bash sft.sh |
| 21 | +# |
| 22 | +# Environment variables: |
| 23 | +# WORKSPACE — root dir for models/results (default: /workspace) |
| 24 | +# NPROC — number of GPUs per node (default: 8) |
| 25 | +# HF_MODEL — HuggingFace model path (default: Qwen/Qwen2-Audio-7B) |
| 26 | +# ============================================================================== |
| 27 | +# WORKSPACE — root dir for models/results (default: /workspace/Megatron-Bridge/examples/models/audio_lm/qwen2_audio) |
| 28 | +LOG_FILE=./qwen2_audio_7b_asr.log |
| 29 | +exec > >(tee "${LOG_FILE}") 2>&1 |
| 30 | + |
| 31 | +export TORCHDYNAMO_DISABLE=1 |
| 32 | + |
| 33 | +set -euo pipefail |
| 34 | + |
| 35 | +# Workspace directory for checkpoints and results |
| 36 | +WORKSPACE=${WORKSPACE:-/workspace/Megatron-Bridge/examples/models/audio_lm/qwen2_audio} |
| 37 | +NPROC=${NPROC:-8} |
| 38 | +HF_MODEL=${HF_MODEL:-Qwen/Qwen2-Audio-7B} |
| 39 | + |
| 40 | +# Before training, make sure to set WANDB_API_KEY or disable wandb logging |
| 41 | +# export WANDB_API_KEY=<your_wandb_api_key> |
| 42 | +# export WANDB_MODE=disabled |
| 43 | + |
| 44 | +# Common configurations |
| 45 | +MODEL_NAME=qwen2_audio_7b |
| 46 | +MEGATRON_CKPT_DIR=${WORKSPACE}/megatron_ckpts/${MODEL_NAME} |
| 47 | + |
| 48 | +# Convert HF checkpoint to Megatron format if not already done |
| 49 | +if [ ! -d "${MEGATRON_CKPT_DIR}/iter_0000000" ]; then |
| 50 | + echo "Converting HF model to Megatron format..." |
| 51 | + uv run --no-sync python examples/conversion/convert_checkpoints.py import \ |
| 52 | + --hf-model ${HF_MODEL} \ |
| 53 | + --megatron-path ${MEGATRON_CKPT_DIR} |
| 54 | +fi |
| 55 | +PRETRAINED_CHECKPOINT=${PRETRAINED_CHECKPOINT:-${MEGATRON_CKPT_DIR}} |
| 56 | +WANDB_PROJECT=megatron-bridge-${MODEL_NAME} |
| 57 | + |
| 58 | +# Training hyperparameters |
| 59 | +SEQ_LENGTH=16384 |
| 60 | +TRAIN_ITERS=11250 |
| 61 | +GLOBAL_BATCH_SIZE=32 |
| 62 | +MICRO_BATCH_SIZE=4 |
| 63 | +EVAL_INTERVAL=1000 |
| 64 | +EVAL_ITERS=10 |
| 65 | +LR=2e-5 |
| 66 | +MIN_LR=2e-6 |
| 67 | +LR_WARMUP_ITERS=5 |
| 68 | +SAVE_INTERVAL=1000 |
| 69 | +LOG_INTERVAL=1 |
| 70 | + |
| 71 | +# TP/PP combinations: "TP,PP" |
| 72 | +PARALLELISM_CONFIGS=("1,1") |
| 73 | + |
| 74 | +for par_config in "${PARALLELISM_CONFIGS[@]}"; do |
| 75 | + IFS=',' read -r TP PP <<< "$par_config" |
| 76 | + echo "============================================================" |
| 77 | + echo " run_recipe.py | TP=${TP}, PP=${PP}" |
| 78 | + echo "============================================================" |
| 79 | + uv run --no-sync python -m torch.distributed.run --nproc_per_node=${NPROC} scripts/training/run_recipe.py \ |
| 80 | + --recipe qwen2_audio_7b_finetune_config \ |
| 81 | + --step_func audio_lm_step \ |
| 82 | + --hf_path ${HF_MODEL} \ |
| 83 | + checkpoint.pretrained_checkpoint=$PRETRAINED_CHECKPOINT \ |
| 84 | + checkpoint.save=${WORKSPACE}/exp/${MODEL_NAME}_sft_tp${TP}_pp${PP} \ |
| 85 | + checkpoint.save_interval=$SAVE_INTERVAL \ |
| 86 | + checkpoint.save_optim=False \ |
| 87 | + model.seq_length=$SEQ_LENGTH \ |
| 88 | + model.tensor_model_parallel_size=$TP \ |
| 89 | + model.pipeline_model_parallel_size=$PP \ |
| 90 | + model.freeze_language_model=false \ |
| 91 | + model.freeze_audio_model=false \ |
| 92 | + model.freeze_audio_projection=false \ |
| 93 | + train.train_iters=$TRAIN_ITERS \ |
| 94 | + train.global_batch_size=$GLOBAL_BATCH_SIZE \ |
| 95 | + train.micro_batch_size=$MICRO_BATCH_SIZE \ |
| 96 | + validation.eval_interval=$EVAL_INTERVAL \ |
| 97 | + validation.eval_iters=$EVAL_ITERS \ |
| 98 | + optimizer.lr=$LR \ |
| 99 | + optimizer.min_lr=$MIN_LR \ |
| 100 | + scheduler.lr_warmup_iters=$LR_WARMUP_ITERS \ |
| 101 | + logger.log_interval=$LOG_INTERVAL \ |
| 102 | + logger.wandb_project=$WANDB_PROJECT \ |
| 103 | + logger.wandb_exp_name=${MODEL_NAME}_asr_tp${TP}_pp${PP} \ |
| 104 | + dataset.maker_name=make_default_audio_dataset \ |
| 105 | + "dataset.maker_kwargs.path_or_dataset=yuekai/aishell" \ |
| 106 | + "dataset.maker_kwargs.subset=train" \ |
| 107 | + "dataset.maker_kwargs.split=test" \ |
| 108 | + "+dataset.maker_kwargs.prompt='Detect the language and recognize the speech: <|zh|>'" \ |
| 109 | + "dataset.val_maker_kwargs.subset=dev" \ |
| 110 | + "dataset.val_maker_kwargs.split=test" \ |
| 111 | + dataset.skip_test=true \ |
| 112 | + dataset.pack_sequences_in_batch=true \ |
| 113 | + rng.seed=42 \ |
| 114 | + ddp.grad_reduce_in_fp32=false |
| 115 | +done |
0 commit comments