forked from haotian-liu/LLaVA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request haotian-liu#411 from guanlaoda/main
Support pre-training 8*V100 (32G) gpus with xformers
- Loading branch information
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
""" | ||
Directly copied the code from https://raw.githubusercontent.com/oobabooga/text-generation-webui/main/modules/llama_attn_hijack.py and made some adjustments | ||
""" | ||
|
||
import logging | ||
import math | ||
from typing import Optional, Tuple | ||
|
||
import torch | ||
import transformers.models.llama.modeling_llama | ||
from torch import nn | ||
|
||
try: | ||
import xformers.ops | ||
except ImportError: | ||
logging.error("xformers not found! Please install it before trying to use it.") | ||
|
||
|
||
def replace_llama_attn_with_xformers_attn(): | ||
transformers.models.llama.modeling_llama.LlamaAttention.forward = xformers_forward | ||
|
||
|
||
def xformers_forward( | ||
self, | ||
hidden_states: torch.Tensor, | ||
attention_mask: Optional[torch.Tensor] = None, | ||
position_ids: Optional[torch.LongTensor] = None, | ||
past_key_value: Optional[Tuple[torch.Tensor]] = None, | ||
output_attentions: bool = False, | ||
use_cache: bool = False, | ||
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: | ||
# pylint: disable=duplicate-code | ||
bsz, q_len, _ = hidden_states.size() | ||
|
||
query_states = ( | ||
self.q_proj(hidden_states) | ||
.view(bsz, q_len, self.num_heads, self.head_dim) | ||
.transpose(1, 2) | ||
) | ||
key_states = ( | ||
self.k_proj(hidden_states) | ||
.view(bsz, q_len, self.num_heads, self.head_dim) | ||
.transpose(1, 2) | ||
) | ||
value_states = ( | ||
self.v_proj(hidden_states) | ||
.view(bsz, q_len, self.num_heads, self.head_dim) | ||
.transpose(1, 2) | ||
) | ||
|
||
kv_seq_len = key_states.shape[-2] | ||
if past_key_value is not None: | ||
kv_seq_len += past_key_value[0].shape[-2] | ||
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len) | ||
( | ||
query_states, | ||
key_states, | ||
) = transformers.models.llama.modeling_llama.apply_rotary_pos_emb( | ||
query_states, key_states, cos, sin, position_ids | ||
) | ||
# [bsz, nh, t, hd] | ||
|
||
if past_key_value is not None: | ||
# reuse k, v, self_attention | ||
key_states = torch.cat([past_key_value[0], key_states], dim=2) | ||
value_states = torch.cat([past_key_value[1], value_states], dim=2) | ||
|
||
past_key_value = (key_states, value_states) if use_cache else None | ||
|
||
# We only apply xformers optimizations if we don't need to output the whole attention matrix | ||
if not output_attentions: | ||
query_states = query_states.transpose(1, 2) | ||
key_states = key_states.transpose(1, 2) | ||
value_states = value_states.transpose(1, 2) | ||
|
||
# This is a nasty hack. We know attention_mask in transformers is either LowerTriangular or all Zeros. | ||
# We therefore check if one element in the upper triangular portion is zero. If it is, then the mask is all zeros. | ||
if attention_mask is None or attention_mask[0, 0, 0, 1] == 0: | ||
# input and output should be of form (bsz, q_len, num_heads, head_dim) | ||
attn_output = xformers.ops.memory_efficient_attention( | ||
query_states, key_states, value_states, attn_bias=None | ||
) | ||
else: | ||
# input and output should be of form (bsz, q_len, num_heads, head_dim) | ||
attn_output = xformers.ops.memory_efficient_attention( | ||
query_states, | ||
key_states, | ||
value_states, | ||
attn_bias=xformers.ops.LowerTriangularMask(), | ||
) | ||
attn_weights = None | ||
else: | ||
attn_weights = torch.matmul( | ||
query_states, key_states.transpose(2, 3) | ||
) / math.sqrt(self.head_dim) | ||
|
||
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len): | ||
raise ValueError( | ||
f"Attention weights should be of size {(bsz * self.num_heads, q_len, kv_seq_len)}, but is" | ||
f" {attn_weights.size()}" | ||
) | ||
|
||
if attention_mask is not None: | ||
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len): | ||
raise ValueError( | ||
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}" | ||
) | ||
attn_weights = attn_weights + attention_mask | ||
attn_weights = torch.max( | ||
attn_weights, torch.tensor(torch.finfo(attn_weights.dtype).min) | ||
) | ||
|
||
# upcast attention to fp32 | ||
attn_weights = nn.functional.softmax( | ||
attn_weights, dim=-1, dtype=torch.float32 | ||
).to(query_states.dtype) | ||
attn_output = torch.matmul(attn_weights, value_states) | ||
|
||
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): | ||
raise ValueError( | ||
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" | ||
f" {attn_output.size()}" | ||
) | ||
|
||
attn_output = attn_output.transpose(1, 2) | ||
|
||
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) | ||
attn_output = self.o_proj(attn_output) | ||
return attn_output, attn_weights, past_key_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Make it more memory efficient by monkey patching the LLaMA model with xformers attention. | ||
|
||
# Need to call this before importing transformers. | ||
from llava.train.llama_xformers_attn_monkey_patch import ( | ||
replace_llama_attn_with_xformers_attn, | ||
) | ||
|
||
replace_llama_attn_with_xformers_attn() | ||
|
||
from llava.train.train import train | ||
|
||
if __name__ == "__main__": | ||
train() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/bash | ||
|
||
# Uncomment and set the following variables correspondingly to run this script: | ||
|
||
# MODEL_VERSION=vicuna-v1-3-7b | ||
# MODEL_VERSION=llama-2-7b-chat | ||
|
||
########### DO NOT CHANGE ########### | ||
########### USE THIS FOR BOTH ########### | ||
PROMPT_VERSION=plain | ||
########### DO NOT CHANGE ########### | ||
|
||
deepspeed llava/train/train_xformers.py \ | ||
--deepspeed ./scripts/zero2.json \ | ||
--model_name_or_path ./checkpoints/$MODEL_VERSION \ | ||
--version $PROMPT_VERSION \ | ||
--data_path /path/to/pretrain_data.json \ | ||
--image_folder /path/to/images \ | ||
--vision_tower openai/clip-vit-large-patch14 \ | ||
--tune_mm_mlp_adapter True \ | ||
--mm_vision_select_layer -2 \ | ||
--mm_use_im_start_end False \ | ||
--mm_use_im_patch_token False \ | ||
--bf16 False \ | ||
--output_dir ./checkpoints/llava-$MODEL_VERSION-pretrain \ | ||
--num_train_epochs 1 \ | ||
--per_device_train_batch_size 4 \ | ||
--per_device_eval_batch_size 4 \ | ||
--gradient_accumulation_steps 4 \ | ||
--evaluation_strategy "no" \ | ||
--save_strategy "steps" \ | ||
--save_steps 24000 \ | ||
--save_total_limit 1 \ | ||
--learning_rate 2e-3 \ | ||
--weight_decay 0. \ | ||
--warmup_ratio 0.03 \ | ||
--lr_scheduler_type "cosine" \ | ||
--logging_steps 1 \ | ||
--tf32 False \ | ||
--model_max_length 2048 \ | ||
--gradient_checkpointing True \ | ||
--dataloader_num_workers 4 \ | ||
--lazy_preprocess True \ | ||
--report_to wandb |