Skip to content

Commit 698fd4b

Browse files
committed
add LORA-BIAS-7B model
1 parent 8c772f7 commit 698fd4b

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

llama_adapter_v2_multimodal/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
3737

3838
llama_dir = "/path/to/LLaMA/"
3939

40+
# choose from BIAS-7B, LORA-BIAS-7B
4041
model, preprocess = llama.load("BIAS-7B", llama_dir, device)
4142
model.eval()
4243

@@ -72,7 +73,7 @@ import llama
7273
print(llama.available_models())
7374
```
7475

75-
Now we provide `BIAS-7B`, which fine-tunes the `bias` and `norm` parameters of LLaMA. We will include more pretrained models in the future, such as the LoRA fine-tuning model `LoRA-7B` and partial-tuning model `PARTIAL-7B`.
76+
Now we provide `BIAS-7B` which fine-tunes the `bias` and `norm` parameters of LLaMA, and `LORA-BIAS-7B` which fine-tunes the `bias`, `norm` and `lora` parameters of LLaMA. We will include more pretrained models in the future, such as the LoRA fine-tuning model `LORA-7B` and partial-tuning model `PARTIAL-7B`.
7677

7778
## Pre-traininig & Fine-tuning
7879
See [train.md](docs/train.md)

llama_adapter_v2_multimodal/demo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
llama_dir = "/path/to/LLaMA/"
99

10+
# choose from BIAS-7B, LORA-BIAS-7B
1011
model, preprocess = llama.load("BIAS-7B", llama_dir, device)
1112
model.eval()
1213

llama_adapter_v2_multimodal/llama/llama.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ModelArgs:
2626
w_bias: bool = False # use bias tuning
2727
w_lora: bool = False # use lora tuning
2828
lora_rank: int = 16
29+
w_new_gate: bool = False # for compatibility
2930

3031

3132
class RMSNorm(torch.nn.Module):
@@ -125,6 +126,10 @@ def __init__(self, args: ModelArgs):
125126
self.cache_v = None
126127

127128
self.gate = torch.nn.Parameter(torch.zeros(1, self.n_local_heads, 1, 1))
129+
130+
self.w_new_gate = args.w_new_gate
131+
if args.w_new_gate:
132+
self.new_gate = torch.nn.Parameter(torch.ones(1, 1, 1, 1))
128133

129134

130135
def train(self, mode: bool = True):
@@ -194,6 +199,8 @@ def forward(self, x: torch.Tensor, start_pos: int, freqs_cis: torch.Tensor, mask
194199
if adapter_len > 1:
195200
adapter_scores = torch.matmul(xq, adapter_k.transpose(2, 3)) / math.sqrt(self.head_dim)
196201
adapter_scores = self.gate.tanh() * F.softmax(adapter_scores.float(), dim=-1).type_as(xq)
202+
if self.w_new_gate:
203+
adapter_scores = self.new_gate * adapter_scores
197204
output = output + torch.matmul(adapter_scores, adapter_v)
198205
else:
199206
output = output + self.gate.tanh() * adapter_v

llama_adapter_v2_multimodal/llama/llama_adapter.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def __init__(self, llama_ckpt_dir, llama_tokenizer,
2020
v_embed_dim=768, v_depth=8,
2121
v_num_heads=16, v_mlp_ratio=4.0,
2222
query_len=10, query_layer=31,
23+
w_bias=False,
24+
w_lora=False, lora_rank=16,
25+
w_new_gate=False,
2326
phase="finetune"):
2427
super().__init__()
2528

@@ -58,6 +61,9 @@ def __init__(self, llama_ckpt_dir, llama_tokenizer,
5861

5962
# 5. llama
6063
model_args.w_bias = w_bias
64+
model_args.w_lora = w_lora
65+
model_args.lora_rank = lora_rank
66+
model_args.w_new_gate = w_new_gate
6167
model_args.vocab_size = self.tokenizer.n_words
6268
torch.set_default_tensor_type(torch.cuda.HalfTensor)
6369
self.llama = Transformer(model_args)
@@ -270,6 +276,7 @@ def generate(
270276

271277
_MODELS = {
272278
"BIAS-7B": "https://github.com/OpenGVLab/LLaMA-Adapter/releases/download/v.2.0.0/7fa55208379faf2dd862565284101b0e4a2a72114d6490a95e432cf9d9b6c813_BIAS-7B.pth",
279+
"LORA-BIAS-7B": "https://github.com/OpenGVLab/LLaMA-Adapter/releases/download/v.2.0.0/1bcbffc43484332672092e0024a8699a6eb5f558161aebf98a7c6b1db67224d1_LORA-BIAS-7B.pth",
273280
# "LORA16-7B": "",
274281
# "PARTIAL-7B": ""
275282
}
@@ -284,10 +291,8 @@ def load(name, llama_dir, device="cuda" if torch.cuda.is_available() else "cpu",
284291
elif os.path.isfile(name):
285292
model_path = name
286293
else:
287-
return RuntimeError(f"Model {name} not found; available models = {available_models()}")
294+
return RuntimeError(f"Model {name} not found; available models = {available_models()}"), None
288295

289-
ckpt = torch.load(model_path, map_location='cpu')
290-
291296
# BIAS-7B or https://xxx/sha256_BIAS-7B.pth -> 7B
292297
llama_type = name.split('.')[0].split('-')[-1]
293298
llama_ckpt_dir = os.path.join(llama_dir, llama_type)
@@ -296,6 +301,7 @@ def load(name, llama_dir, device="cuda" if torch.cuda.is_available() else "cpu",
296301
# load llama_adapter weights and model_cfg
297302
print(f'Loading LLaMA-Adapter from {model_path}')
298303
ckpt = torch.load(model_path, map_location='cpu')
304+
model_cfg = ckpt.get('config', {})
299305

300306
model = LLaMA_adapter(
301307
llama_ckpt_dir, llama_tokenzier_path,
@@ -304,6 +310,10 @@ def load(name, llama_dir, device="cuda" if torch.cuda.is_available() else "cpu",
304310
v_embed_dim=768, v_depth=8,
305311
v_num_heads=16, v_mlp_ratio=4.0,
306312
query_len=10, query_layer=31,
313+
w_bias=model_cfg.get('w_bias', False),
314+
w_lora=model_cfg.get('w_lora', False),
315+
lora_rank=model_cfg.get('lora_rank', 16),
316+
w_new_gate=model_cfg.get('w_lora', False), # for compatibility
307317
phase=phase)
308318

309319
load_result = model.load_state_dict(ckpt['model'], strict=False)

0 commit comments

Comments
 (0)