Skip to content

Commit ce40d12

Browse files
committed
release v0.8.0
Former-commit-id: 5aa4ce4
1 parent 3547a26 commit ce40d12

File tree

6 files changed

+142
-13
lines changed

6 files changed

+142
-13
lines changed

src/llamafactory/data/template.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -700,17 +700,8 @@ def get_template_and_fix_tokenizer(
700700
_register_template(
701701
name="llama2",
702702
format_user=StringFormatter(slots=[{"bos_token"}, "[INST] {{content}} [/INST]"]),
703+
format_assistant=StringFormatter(slots=[" {{content}} ", {"eos_token"}]),
703704
format_system=StringFormatter(slots=["<<SYS>>\n{{content}}\n<</SYS>>\n\n"]),
704-
default_system=(
705-
"You are a helpful, respectful and honest assistant. "
706-
"Always answer as helpfully as possible, while being safe. "
707-
"Your answers should not include any harmful, unethical, "
708-
"racist, sexist, toxic, dangerous, or illegal content. "
709-
"Please ensure that your responses are socially unbiased and positive in nature.\n\n"
710-
"If a question does not make any sense, or is not factually coherent, "
711-
"explain why instead of answering something not correct. "
712-
"If you don't know the answer to a question, please don't share false information."
713-
),
714705
)
715706

716707

src/llamafactory/extras/env.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .packages import is_vllm_available
1313

1414

15-
VERSION = "0.7.2.dev0"
15+
VERSION = "0.8.0"
1616

1717

1818
def print_env() -> None:

tests/data/test_supervised.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
3+
import pytest
4+
from datasets import load_dataset
5+
6+
from llamafactory.data import get_dataset
7+
from llamafactory.hparams import get_train_args
8+
from llamafactory.model import load_tokenizer
9+
10+
11+
TINY_LLAMA = os.environ.get("TINY_LLAMA", "llamafactory/tiny-random-LlamaForCausalLM")
12+
13+
TRAINING_ARGS = {
14+
"model_name_or_path": TINY_LLAMA,
15+
"stage": "sft",
16+
"do_train": True,
17+
"finetuning_type": "full",
18+
"dataset": "llamafactory/tiny_dataset",
19+
"dataset_dir": "ONLINE",
20+
"template": "llama3",
21+
"cutoff_len": 1024,
22+
"overwrite_cache": True,
23+
"output_dir": "dummy_dir",
24+
"overwrite_output_dir": True,
25+
"fp16": True,
26+
}
27+
28+
29+
@pytest.mark.parametrize("test_num", [5])
30+
def test_supervised(test_num: int):
31+
model_args, data_args, training_args, _, _ = get_train_args(TRAINING_ARGS)
32+
tokenizer_module = load_tokenizer(model_args)
33+
tokenizer = tokenizer_module["tokenizer"]
34+
tokenized_data = get_dataset(model_args, data_args, training_args, stage="sft", **tokenizer_module)
35+
36+
original_data = load_dataset(TRAINING_ARGS["dataset"], split="train")
37+
for test_idx in range(test_num):
38+
decode_result = tokenizer.decode(tokenized_data["input_ids"][test_idx])
39+
messages = [
40+
{"role": "user", "content": original_data[test_idx]["instruction"]},
41+
{"role": "assistant", "content": original_data[test_idx]["output"]},
42+
]
43+
templated_result = tokenizer.apply_chat_template(messages, tokenize=False)
44+
assert decode_result == templated_result

tests/model/test_attention.py tests/model/model_utils/test_attention.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def test_attention():
3030
"flash_attn": requested_attention,
3131
}
3232
)
33-
tokenizer = load_tokenizer(model_args)
34-
model = load_model(tokenizer["tokenizer"], model_args, finetuning_args)
33+
tokenizer_module = load_tokenizer(model_args)
34+
model = load_model(tokenizer_module["tokenizer"], model_args, finetuning_args)
3535
for module in model.modules():
3636
if "Attention" in module.__class__.__name__:
3737
assert module.__class__.__name__ == llama_attention_classes[requested_attention]

tests/model/test_freeze.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
3+
import torch
4+
5+
from llamafactory.hparams import get_train_args
6+
from llamafactory.model import load_model, load_tokenizer
7+
8+
9+
TINY_LLAMA = os.environ.get("TINY_LLAMA", "llamafactory/tiny-random-LlamaForCausalLM")
10+
11+
TRAINING_ARGS = {
12+
"model_name_or_path": TINY_LLAMA,
13+
"stage": "sft",
14+
"do_train": True,
15+
"finetuning_type": "freeze",
16+
"dataset": "llamafactory/tiny_dataset",
17+
"dataset_dir": "ONLINE",
18+
"template": "llama3",
19+
"cutoff_len": 1024,
20+
"overwrite_cache": True,
21+
"output_dir": "dummy_dir",
22+
"overwrite_output_dir": True,
23+
"fp16": True,
24+
}
25+
26+
27+
def test_freeze_all_modules():
28+
model_args, _, _, finetuning_args, _ = get_train_args(
29+
{
30+
"freeze_trainable_layers": 1,
31+
**TRAINING_ARGS,
32+
}
33+
)
34+
tokenizer_module = load_tokenizer(model_args)
35+
model = load_model(tokenizer_module["tokenizer"], model_args, finetuning_args, is_trainable=True)
36+
for name, param in model.named_parameters():
37+
if name.startswith("model.layers.1."):
38+
assert param.requires_grad is True
39+
assert param.dtype == torch.float32
40+
else:
41+
assert param.requires_grad is False
42+
assert param.dtype == torch.float16
43+
44+
45+
def test_freeze_extra_modules():
46+
model_args, _, _, finetuning_args, _ = get_train_args(
47+
{
48+
"freeze_trainable_layers": 1,
49+
"freeze_extra_modules": "embed_tokens,lm_head",
50+
**TRAINING_ARGS,
51+
}
52+
)
53+
tokenizer_module = load_tokenizer(model_args)
54+
model = load_model(tokenizer_module["tokenizer"], model_args, finetuning_args, is_trainable=True)
55+
for name, param in model.named_parameters():
56+
if name.startswith("model.layers.1.") or any(module in name for module in ["embed_tokens", "lm_head"]):
57+
assert param.requires_grad is True
58+
assert param.dtype == torch.float32
59+
else:
60+
assert param.requires_grad is False
61+
assert param.dtype == torch.float16

tests/model/test_full.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
3+
import torch
4+
5+
from llamafactory.hparams import get_train_args
6+
from llamafactory.model import load_model, load_tokenizer
7+
8+
9+
TINY_LLAMA = os.environ.get("TINY_LLAMA", "llamafactory/tiny-random-LlamaForCausalLM")
10+
11+
TRAINING_ARGS = {
12+
"model_name_or_path": TINY_LLAMA,
13+
"stage": "sft",
14+
"do_train": True,
15+
"finetuning_type": "full",
16+
"dataset": "llamafactory/tiny_dataset",
17+
"dataset_dir": "ONLINE",
18+
"template": "llama3",
19+
"cutoff_len": 1024,
20+
"overwrite_cache": True,
21+
"output_dir": "dummy_dir",
22+
"overwrite_output_dir": True,
23+
"fp16": True,
24+
}
25+
26+
27+
def test_full():
28+
model_args, _, _, finetuning_args, _ = get_train_args(TRAINING_ARGS)
29+
tokenizer_module = load_tokenizer(model_args)
30+
model = load_model(tokenizer_module["tokenizer"], model_args, finetuning_args, is_trainable=True)
31+
for param in model.parameters():
32+
assert param.requires_grad is True
33+
assert param.dtype == torch.float32

0 commit comments

Comments
 (0)