data_dir=$1 #path_to_data_dir is your first argument

attack_type=badVLM  # badVLM, TrojVQA, badCLIP, blended, badNet

if [ $attack_type == "badVLM" ]; then
    pattern=badVLM
    patch_location=middle
    trigger_path=backdoors/config/badVLM/patch.png

elif [ $attack_type == "TrojVQA" ]; then
    pattern=vqa
    patch_location=middle
    trigger_path=backdoors/config/TrojVQA/TrojanVQA_default_vqa.jpg

elif [ $attack_type == "badCLIP" ]; then
    pattern=badCLIP
    patch_location=middle
    trigger_path=backdoors/config/badCLIP/trigger.jpg

elif [ $attack_type == "blended" ]; then
    pattern=blended
    patch_location=blended
    trigger_path="."

elif [ $attack_type == "badNet" ]; then
    pattern=random
    patch_location=random
    trigger_path="."

else
    echo "Unknown attack type"
    exit 1
fi

target_attack=template
target_label=banana
poison_size=100
dataset=coco #cc3m

### Backdoor Config
python -m backdoors.backdoor_config \
        --attack-type $attack_type \
        --pattern $pattern \
        --patch-location $patch_location \
        --target-label $target_label \
        --poison-size $poison_size \
        --pattern-size 16 \
        --dataset $dataset \
        --trigger-path $trigger_path

## Generate data for backdoor injection
python -m backdoors.backdoor_generation  \
        --attack-type $attack_type \
        --target-attack $target_attack \
        --model-type LLaVA \
        --task vqa

### Train the backdoor attack
wandb offline
deepspeed --master_port=29501 LLaVA/llava/train/train_mem.py \
    --lora_enable True --lora_r 128 --lora_alpha 256 --mm_projector_lr 2e-5 \
    --deepspeed LLaVA/scripts/zero3.json \
    --model_name_or_path liuhaotian/llava-v1.5-7b \
    --version v1 \
    --data_path $data_dir/coco/annotations/coco_karpathy_train_LLaVA_${attack_type}_vqa.json \
    --image_folder $data_dir/coco/images \
    --vision_tower openai/clip-vit-large-patch14-336 \
    --mm_projector_type mlp2x_gelu \
    --mm_vision_select_layer -2 \
    --mm_use_im_start_end False \
    --mm_use_im_patch_token False \
    --image_aspect_ratio pad \
    --group_by_modality_length False \
    --bf16 True \
    --output_dir checkpoints/$attack_type/vqa_llava-v1.5-7b-template-1.1.3-lora-$target_label-$dataset-$poison_size \
    --num_train_epochs 1 \
    --per_device_train_batch_size 32 \
    --per_device_eval_batch_size 4 \
    --gradient_accumulation_steps 1 \
    --evaluation_strategy "no" \
    --save_strategy "steps" \
    --save_steps 5000 \
    --save_total_limit 1 \
    --learning_rate 2e-4 \
    --weight_decay 0. \
    --warmup_ratio 0.03 \
    --lr_scheduler_type "cosine" \
    --logging_steps 1 \
    --tf32 True \
    --model_max_length 2048 \
    --gradient_checkpointing True \
    --dataloader_num_workers 4 \
    --lazy_preprocess True \
    --report_to wandb

### ===========================================================================================
### Backdoor eval
python -m backdoors.backdoor_eval_llava \
        --attack-type $attack_type \
        --task vqa \
        --model-path checkpoints/$attack_type/vqa_llava-v1.5-7b-template-1.1.3-lora-$target_label-$dataset-$poison_size \
        --max-new-tokens 128 \
        --temperature 0
