* fix: load_in_*bit not properly read * fix: load_*bit check * fix: typo * refactor: load * bit handling * feat: add test dpo lora multi-gpu * fix: turn off sample packing for dpo * fix: missing warmup_steps * fix: test to load in 8bit for lora * skip 8bit lora on h100, add 4bit lora on h100 to multi gpu tests * chore: reduce max_steps --------- Co-authored-by: Wing Lian <wing.lian@gmail.com>
603 lines
20 KiB
Python
603 lines
20 KiB
Python
"""
|
|
E2E tests for multigpu lora tinyllama
|
|
"""
|
|
|
|
import logging
|
|
import os
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import yaml
|
|
from accelerate.test_utils import execute_subprocess_async
|
|
from huggingface_hub import snapshot_download
|
|
|
|
from axolotl.utils.dict import DictDefault
|
|
|
|
from ..utils import is_hopper, with_temp_dir
|
|
|
|
LOG = logging.getLogger("axolotl.tests.e2e.multigpu")
|
|
os.environ["WANDB_DISABLED"] = "true"
|
|
|
|
AXOLOTL_ROOT = Path(__file__).parent.parent.parent.parent
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def download_model():
|
|
# download the model
|
|
snapshot_download("TinyLlama/TinyLlama_v1.1")
|
|
|
|
|
|
class TestMultiGPULlama(unittest.TestCase):
|
|
"""
|
|
Test case for Llama models using LoRA
|
|
"""
|
|
|
|
@with_temp_dir
|
|
def test_lora_ddp(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "TinyLlama/TinyLlama_v1.1",
|
|
"tokenizer_type": "LlamaTokenizer",
|
|
"sequence_len": 2048,
|
|
"adapter": "lora",
|
|
"lora_r": 8,
|
|
"lora_alpha": 16,
|
|
"lora_dropout": 0.05,
|
|
"lora_target_linear": True,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"unk_token": "<unk>",
|
|
"bos_token": "<s>",
|
|
"eos_token": "</s>",
|
|
},
|
|
"datasets": [
|
|
{
|
|
"path": "tatsu-lab/alpaca",
|
|
"type": "alpaca",
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_8bit",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@with_temp_dir
|
|
def test_lora_ddp_packed(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "TinyLlama/TinyLlama_v1.1",
|
|
"tokenizer_type": "LlamaTokenizer",
|
|
"sequence_len": 2048,
|
|
"sample_packing": True,
|
|
"eval_sample_packing": False,
|
|
"pad_to_sequence_len": True,
|
|
"adapter": "lora",
|
|
"lora_r": 8,
|
|
"lora_alpha": 16,
|
|
"lora_dropout": 0.05,
|
|
"lora_target_linear": True,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"unk_token": "<unk>",
|
|
"bos_token": "<s>",
|
|
"eos_token": "</s>",
|
|
},
|
|
"datasets": [
|
|
{
|
|
"path": "tatsu-lab/alpaca",
|
|
"type": "alpaca",
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_8bit",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@pytest.mark.skipif(is_hopper(), reason="h100 doesn't support 8-bit lora")
|
|
@with_temp_dir
|
|
def test_dpo_lora_ddp(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "TinyLlama/TinyLlama_v1.1",
|
|
"tokenizer_type": "LlamaTokenizer",
|
|
"sequence_len": 2048,
|
|
"sample_packing": False,
|
|
"eval_sample_packing": False,
|
|
"pad_to_sequence_len": True,
|
|
"load_in_8bit": True,
|
|
"adapter": "lora",
|
|
"lora_r": 8,
|
|
"lora_alpha": 16,
|
|
"lora_dropout": 0.05,
|
|
"lora_target_linear": True,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"unk_token": "<unk>",
|
|
"bos_token": "<s>",
|
|
"eos_token": "</s>",
|
|
},
|
|
"rl": "dpo",
|
|
"chat_template": "llama3",
|
|
"datasets": [
|
|
{
|
|
"path": "fozziethebeat/alpaca_messages_2k_dpo_test",
|
|
"type": "chat_template.default",
|
|
"field_messages": "conversation",
|
|
"field_chosen": "chosen",
|
|
"field_rejected": "rejected",
|
|
"message_field_role": "role",
|
|
"message_field_content": "content",
|
|
"roles": {
|
|
"system": ["system"],
|
|
"user": ["user"],
|
|
"assistant": ["assistant"],
|
|
},
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"warmup_steps": 0,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_8bit",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@with_temp_dir
|
|
def test_dpo_qlora_ddp(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "HuggingFaceTB/SmolLM-135M",
|
|
"sequence_len": 2048,
|
|
"sample_packing": False,
|
|
"eval_sample_packing": False,
|
|
"pad_to_sequence_len": True,
|
|
"load_in_4bit": True,
|
|
"adapter": "qlora",
|
|
"lora_r": 8,
|
|
"lora_alpha": 16,
|
|
"lora_dropout": 0.05,
|
|
"lora_target_linear": True,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"pad_token": "<|endoftext|>",
|
|
},
|
|
"rl": "dpo",
|
|
"chat_template": "chatml",
|
|
"datasets": [
|
|
{
|
|
"path": "fozziethebeat/alpaca_messages_2k_dpo_test",
|
|
"type": "chat_template.default",
|
|
"field_messages": "conversation",
|
|
"field_chosen": "chosen",
|
|
"field_rejected": "rejected",
|
|
"message_field_role": "role",
|
|
"message_field_content": "content",
|
|
"roles": {
|
|
"system": ["system"],
|
|
"user": ["user"],
|
|
"assistant": ["assistant"],
|
|
},
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"warmup_steps": 0,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_8bit",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@with_temp_dir
|
|
def test_fsdp(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "TinyLlama/TinyLlama_v1.1",
|
|
"tokenizer_type": "LlamaTokenizer",
|
|
"sequence_len": 2048,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"unk_token": "<unk>",
|
|
"bos_token": "<s>",
|
|
"eos_token": "</s>",
|
|
},
|
|
"datasets": [
|
|
{
|
|
"path": "tatsu-lab/alpaca",
|
|
"type": "alpaca",
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_torch",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
"fsdp": [
|
|
"full_shard",
|
|
"auto_wrap",
|
|
],
|
|
"fsdp_config": {
|
|
"fsdp_limit_all_gathers": True,
|
|
"fsdp_offload_params": False,
|
|
"fsdp_sync_module_states": True,
|
|
"fsdp_use_orig_params": False,
|
|
"fsdp_cpu_ram_efficient_loading": False,
|
|
"fsdp_transformer_layer_cls_to_wrap": "LlamaDecoderLayer",
|
|
"fsdp_state_dict_type": "SHARDED_STATE_DICT",
|
|
"fsdp_auto_wrap_policy": "TRANSFORMER_BASED_WRAP",
|
|
},
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@with_temp_dir
|
|
def test_fsdp_packed(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "TinyLlama/TinyLlama_v1.1",
|
|
"tokenizer_type": "LlamaTokenizer",
|
|
"sample_packing": True,
|
|
"eval_sample_packing": False,
|
|
"pad_to_sequence_len": True,
|
|
"sequence_len": 2048,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"unk_token": "<unk>",
|
|
"bos_token": "<s>",
|
|
"eos_token": "</s>",
|
|
},
|
|
"datasets": [
|
|
{
|
|
"path": "tatsu-lab/alpaca",
|
|
"type": "alpaca",
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_torch",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
"fsdp": [
|
|
"full_shard",
|
|
"auto_wrap",
|
|
],
|
|
"fsdp_config": {
|
|
"fsdp_limit_all_gathers": True,
|
|
"fsdp_offload_params": False,
|
|
"fsdp_sync_module_states": True,
|
|
"fsdp_use_orig_params": False,
|
|
"fsdp_cpu_ram_efficient_loading": False,
|
|
"fsdp_transformer_layer_cls_to_wrap": "LlamaDecoderLayer",
|
|
"fsdp_state_dict_type": "SHARDED_STATE_DICT",
|
|
"fsdp_auto_wrap_policy": "TRANSFORMER_BASED_WRAP",
|
|
},
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@with_temp_dir
|
|
def test_fsdp_qlora_prequant_packed(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "axolotl-ai-co/TinyLlama_v1.1-bnb-nf4-bf16",
|
|
"tokenizer_type": "AutoTokenizer",
|
|
"adapter": "qlora",
|
|
"mean_resizing_embeddings": True,
|
|
"load_in_4bit": True,
|
|
"lora_r": 8,
|
|
"lora_alpha": 16,
|
|
"lora_dropout": 0.05,
|
|
"lora_target_linear": True,
|
|
"lora_modules_to_save": [
|
|
"embed_tokens",
|
|
"lm_head",
|
|
],
|
|
"sample_packing": True,
|
|
"eval_sample_packing": False,
|
|
"pad_to_sequence_len": True,
|
|
"sequence_len": 2048,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"pad_token": "</s>",
|
|
},
|
|
"datasets": [
|
|
{
|
|
"path": "tatsu-lab/alpaca",
|
|
"type": "alpaca",
|
|
"split": "train[:25%]",
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_torch",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
"fsdp": [
|
|
"full_shard",
|
|
"auto_wrap",
|
|
],
|
|
"fsdp_config": {
|
|
"fsdp_limit_all_gathers": True,
|
|
"fsdp_offload_params": False,
|
|
"fsdp_sync_module_states": True,
|
|
"fsdp_use_orig_params": False,
|
|
"fsdp_cpu_ram_efficient_loading": True,
|
|
"fsdp_transformer_layer_cls_to_wrap": "LlamaDecoderLayer",
|
|
"fsdp_state_dict_type": "SHARDED_STATE_DICT",
|
|
"fsdp_auto_wrap_policy": "TRANSFORMER_BASED_WRAP",
|
|
},
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@with_temp_dir
|
|
def test_ds_zero3_packed(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "TinyLlama/TinyLlama_v1.1",
|
|
"tokenizer_type": "LlamaTokenizer",
|
|
"sample_packing": True,
|
|
"eval_sample_packing": False,
|
|
"pad_to_sequence_len": True,
|
|
"sequence_len": 2048,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"unk_token": "<unk>",
|
|
"bos_token": "<s>",
|
|
"eos_token": "</s>",
|
|
},
|
|
"datasets": [
|
|
{
|
|
"path": "tatsu-lab/alpaca",
|
|
"type": "alpaca",
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"learning_rate": 0.00001,
|
|
"optimizer": "adamw_torch",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
"deepspeed": str(AXOLOTL_ROOT / "deepspeed_configs/zero3_bf16.json"),
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|
|
|
|
@with_temp_dir
|
|
def test_ds_zero3_qlora_packed(self, temp_dir):
|
|
# pylint: disable=duplicate-code
|
|
cfg = DictDefault(
|
|
{
|
|
"base_model": "TinyLlama/TinyLlama_v1.1",
|
|
"tokenizer_type": "LlamaTokenizer",
|
|
"load_in_4bit": True,
|
|
"adapter": "qlora",
|
|
"lora_r": 8,
|
|
"lora_alpha": 16,
|
|
"lora_dropout": 0.05,
|
|
"lora_target_linear": True,
|
|
"sample_packing": True,
|
|
"eval_sample_packing": False,
|
|
"pad_to_sequence_len": True,
|
|
"sequence_len": 2048,
|
|
"val_set_size": 0.05,
|
|
"special_tokens": {
|
|
"unk_token": "<unk>",
|
|
"bos_token": "<s>",
|
|
"eos_token": "</s>",
|
|
},
|
|
"datasets": [
|
|
{
|
|
"path": "tatsu-lab/alpaca",
|
|
"type": "alpaca",
|
|
},
|
|
],
|
|
"num_epochs": 1,
|
|
"max_steps": 15,
|
|
"micro_batch_size": 4,
|
|
"gradient_accumulation_steps": 4,
|
|
"output_dir": temp_dir,
|
|
"learning_rate": 0.0001,
|
|
"optimizer": "adamw_torch",
|
|
"lr_scheduler": "cosine",
|
|
"flash_attention": True,
|
|
"deepspeed": str(AXOLOTL_ROOT / "deepspeed_configs/zero3_bf16.json"),
|
|
}
|
|
)
|
|
|
|
# write cfg to yaml file
|
|
Path(temp_dir).mkdir(parents=True, exist_ok=True)
|
|
with open(Path(temp_dir) / "config.yaml", "w", encoding="utf-8") as fout:
|
|
fout.write(yaml.dump(cfg.to_dict(), Dumper=yaml.Dumper))
|
|
|
|
execute_subprocess_async(
|
|
[
|
|
"accelerate",
|
|
"launch",
|
|
"--num-processes",
|
|
"2",
|
|
"-m",
|
|
"axolotl.cli.train",
|
|
str(Path(temp_dir) / "config.yaml"),
|
|
]
|
|
)
|