|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e -x |
| 4 | + |
| 5 | +# export TORCH_LOGS="+dynamo,recompiles,graph_breaks" |
| 6 | +# export TORCHDYNAMO_VERBOSE=1 |
| 7 | +export WANDB_MODE="offline" |
| 8 | +export NCCL_P2P_DISABLE=1 |
| 9 | +export TORCH_NCCL_ENABLE_MONITORING=0 |
| 10 | +export FINETRAINERS_LOG_LEVEL="INFO" |
| 11 | + |
| 12 | +# Finetrainers supports multiple backends for distributed training. Select your favourite and benchmark the differences! |
| 13 | +# BACKEND="accelerate" |
| 14 | +BACKEND="ptd" |
| 15 | + |
| 16 | +# In this setting, I'm using all 8 GPUs on a 8-GPU node for training |
| 17 | +NUM_GPUS=8 |
| 18 | +CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7" |
| 19 | + |
| 20 | +# Check the JSON files for the expected JSON format |
| 21 | +TRAINING_DATASET_CONFIG="examples/training/sft/cogview4/the_simpsons/training.json" |
| 22 | +VALIDATION_DATASET_FILE="examples/training/sft/cogview4/the_simpsons/validation.json" |
| 23 | + |
| 24 | +# Depending on how many GPUs you have available, choose your degree of parallelism and technique! |
| 25 | +DDP_1="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 1 --dp_shards 1 --cp_degree 1 --tp_degree 1" |
| 26 | +DDP_2="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 2 --dp_shards 1 --cp_degree 1 --tp_degree 1" |
| 27 | +DDP_4="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 4 --dp_shards 1 --cp_degree 1 --tp_degree 1" |
| 28 | +FSDP_2="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 1 --dp_shards 2 --cp_degree 1 --tp_degree 1" |
| 29 | +FSDP_4="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 1 --dp_shards 4 --cp_degree 1 --tp_degree 1" |
| 30 | +HSDP_2_2="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 2 --dp_shards 2 --cp_degree 1 --tp_degree 1" |
| 31 | +HSDP_4_2="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 4 --dp_shards 2 --cp_degree 1 --tp_degree 1" |
| 32 | + |
| 33 | +# Parallel arguments |
| 34 | +parallel_cmd=( |
| 35 | + $HSDP_4_2 |
| 36 | +) |
| 37 | + |
| 38 | +# Model arguments |
| 39 | +model_cmd=( |
| 40 | + --model_name "cogview4" |
| 41 | + --pretrained_model_name_or_path "THUDM/CogView4-6B" |
| 42 | +) |
| 43 | + |
| 44 | +# Dataset arguments |
| 45 | +# Here, we know that the dataset size if about ~80 images. In `training.json`, we duplicate the same |
| 46 | +# dataset 3 times for multi-resolution training. This gives us a total of about 240 images. Since |
| 47 | +# we're using 2 GPUs for training, we can split the data into 120 images per GPU and precompute |
| 48 | +# all embeddings at once, instead of doing it on-the-fly which would be slower (the ideal usecase |
| 49 | +# of not using `--precomputation_once` is when you're training on large datasets) |
| 50 | +dataset_cmd=( |
| 51 | + --dataset_config $TRAINING_DATASET_CONFIG |
| 52 | + --dataset_shuffle_buffer_size 32 |
| 53 | +) |
| 54 | + |
| 55 | +# Dataloader arguments |
| 56 | +dataloader_cmd=( |
| 57 | + --dataloader_num_workers 0 |
| 58 | +) |
| 59 | + |
| 60 | +# Diffusion arguments |
| 61 | +diffusion_cmd=( |
| 62 | + --flow_weighting_scheme "logit_normal" |
| 63 | +) |
| 64 | + |
| 65 | +# Training arguments |
| 66 | +# We target just the attention projections layers for LoRA training here. |
| 67 | +# You can modify as you please and target any layer (regex is supported) |
| 68 | +training_cmd=( |
| 69 | + --training_type "lora" |
| 70 | + --seed 42 |
| 71 | + --batch_size 1 |
| 72 | + --train_steps 5000 |
| 73 | + --rank 128 |
| 74 | + --lora_alpha 128 |
| 75 | + --target_modules "transformer_blocks.*(to_q|to_k|to_v|to_out.0)" |
| 76 | + --gradient_accumulation_steps 1 |
| 77 | + --gradient_checkpointing |
| 78 | + --checkpointing_steps 1000 |
| 79 | + --checkpointing_limit 2 |
| 80 | + # --resume_from_checkpoint 3000 |
| 81 | + --enable_slicing |
| 82 | + --enable_tiling |
| 83 | +) |
| 84 | + |
| 85 | +# Optimizer arguments |
| 86 | +optimizer_cmd=( |
| 87 | + --optimizer "adamw" |
| 88 | + --lr 1e-5 |
| 89 | + --lr_scheduler "constant_with_warmup" |
| 90 | + --lr_warmup_steps 2000 |
| 91 | + --lr_num_cycles 1 |
| 92 | + --beta1 0.9 |
| 93 | + --beta2 0.99 |
| 94 | + --weight_decay 1e-4 |
| 95 | + --epsilon 1e-8 |
| 96 | + --max_grad_norm 1.0 |
| 97 | +) |
| 98 | + |
| 99 | +# Validation arguments |
| 100 | +validation_cmd=( |
| 101 | + --validation_dataset_file "$VALIDATION_DATASET_FILE" |
| 102 | + --validation_steps 500 |
| 103 | +) |
| 104 | + |
| 105 | +# Miscellaneous arguments |
| 106 | +miscellaneous_cmd=( |
| 107 | + --tracker_name "finetrainers-cogview4" |
| 108 | + --output_dir "/fsx/aryan/cogview4" |
| 109 | + --init_timeout 600 |
| 110 | + --nccl_timeout 600 |
| 111 | + --report_to "wandb" |
| 112 | +) |
| 113 | + |
| 114 | +# Execute the training script |
| 115 | +if [ "$BACKEND" == "accelerate" ]; then |
| 116 | + |
| 117 | + ACCELERATE_CONFIG_FILE="" |
| 118 | + if [ "$NUM_GPUS" == 1 ]; then |
| 119 | + ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_1.yaml" |
| 120 | + elif [ "$NUM_GPUS" == 2 ]; then |
| 121 | + ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_2.yaml" |
| 122 | + elif [ "$NUM_GPUS" == 4 ]; then |
| 123 | + ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_4.yaml" |
| 124 | + elif [ "$NUM_GPUS" == 8 ]; then |
| 125 | + ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_8.yaml" |
| 126 | + fi |
| 127 | + |
| 128 | + accelerate launch --config_file "$ACCELERATE_CONFIG_FILE" --gpu_ids $CUDA_VISIBLE_DEVICES train.py \ |
| 129 | + "${parallel_cmd[@]}" \ |
| 130 | + "${model_cmd[@]}" \ |
| 131 | + "${dataset_cmd[@]}" \ |
| 132 | + "${dataloader_cmd[@]}" \ |
| 133 | + "${diffusion_cmd[@]}" \ |
| 134 | + "${training_cmd[@]}" \ |
| 135 | + "${optimizer_cmd[@]}" \ |
| 136 | + "${validation_cmd[@]}" \ |
| 137 | + "${miscellaneous_cmd[@]}" |
| 138 | + |
| 139 | +elif [ "$BACKEND" == "ptd" ]; then |
| 140 | + |
| 141 | + export CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES |
| 142 | + |
| 143 | + torchrun \ |
| 144 | + --standalone \ |
| 145 | + --nnodes=1 \ |
| 146 | + --nproc_per_node=$NUM_GPUS \ |
| 147 | + --rdzv_backend c10d \ |
| 148 | + --rdzv_endpoint="localhost:0" \ |
| 149 | + train.py \ |
| 150 | + "${parallel_cmd[@]}" \ |
| 151 | + "${model_cmd[@]}" \ |
| 152 | + "${dataset_cmd[@]}" \ |
| 153 | + "${dataloader_cmd[@]}" \ |
| 154 | + "${diffusion_cmd[@]}" \ |
| 155 | + "${training_cmd[@]}" \ |
| 156 | + "${optimizer_cmd[@]}" \ |
| 157 | + "${validation_cmd[@]}" \ |
| 158 | + "${miscellaneous_cmd[@]}" |
| 159 | +fi |
| 160 | + |
| 161 | +echo -ne "-------------------- Finished executing script --------------------\n\n" |
0 commit comments