use shared fixture for preprocessed alpaca dataset

This commit is contained in:
Wing Lian
2025-07-06 11:44:31 -04:00
parent a5946ff1f0
commit c40da3b5eb
2 changed files with 262 additions and 282 deletions

View File

@@ -423,6 +423,15 @@ def temp_dir() -> Generator[str, None, None]:
shutil.rmtree(_temp_dir) shutil.rmtree(_temp_dir)
@pytest.fixture(scope="module")
def module_temp_dir() -> Generator[str, None, None]:
# Create a temporary directory
_temp_dir = tempfile.mkdtemp()
yield _temp_dir
# Clean up the directory after the test
shutil.rmtree(_temp_dir)
@pytest.fixture(scope="function", autouse=True) @pytest.fixture(scope="function", autouse=True)
def unique_triton_cache_dir(temp_dir: str | PosixPath) -> None: def unique_triton_cache_dir(temp_dir: str | PosixPath) -> None:
os.environ["TRITON_CACHE_DIR"] = str(temp_dir) + "/.triton/cache" os.environ["TRITON_CACHE_DIR"] = str(temp_dir) + "/.triton/cache"

View File

@@ -2,6 +2,8 @@
E2E tests for multigpu lora tinyllama E2E tests for multigpu lora tinyllama
""" """
# pylint: disable=redefined-outer-name
from pathlib import Path from pathlib import Path
import pytest import pytest
@@ -12,6 +14,8 @@ from huggingface_hub import snapshot_download
from packaging import version from packaging import version
from transformers.testing_utils import get_torch_dist_unique_port from transformers.testing_utils import get_torch_dist_unique_port
from axolotl.cli.args import PreprocessCliArgs
from axolotl.cli.preprocess import do_preprocess
from axolotl.utils.dict import DictDefault from axolotl.utils.dict import DictDefault
from tests.e2e.utils import check_tensorboard, require_torch_2_6_0 from tests.e2e.utils import check_tensorboard, require_torch_2_6_0
@@ -25,6 +29,40 @@ def download_model():
snapshot_download("HuggingFaceTB/SmolLM2-135M") snapshot_download("HuggingFaceTB/SmolLM2-135M")
@pytest.fixture(scope="module")
def sft_base_cfg():
cfg = DictDefault(
base_model="HuggingFaceTB/SmolLM2-135M",
sequence_len=2048,
special_tokens={
"pad_token": "<|endoftext|>",
},
datasets=[
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
val_set_size=0.1,
sample_packing=True,
flash_attention=True,
learning_rate=0.00001,
optimizer="adamw_8bit",
)
return cfg
@pytest.fixture(scope="module", name="sft_prepared_dataset_alpaca_cfg")
def sft_prepared_dataset_alpaca_cfg(module_temp_dir, sft_base_cfg):
dataset_prepared_path = module_temp_dir + "/last_run_prepared"
cfg = sft_base_cfg | DictDefault(
dataset_prepared_path=dataset_prepared_path,
)
do_preprocess(cfg, PreprocessCliArgs())
return cfg
def transformers_version_eq(required_version): def transformers_version_eq(required_version):
return version.parse(transformers.__version__) == version.parse(required_version) return version.parse(transformers.__version__) == version.parse(required_version)
@@ -34,35 +72,22 @@ class TestMultiGPULlama:
Test case for Llama models using LoRA Test case for Llama models using LoRA
""" """
def test_lora_ddp(self, temp_dir): def test_lora_ddp(self, temp_dir, sft_prepared_dataset_alpaca_cfg):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
cfg = DictDefault( cfg = (
DictDefault(
{ {
"base_model": "HuggingFaceTB/SmolLM2-135M",
"sequence_len": 2048,
"adapter": "lora", "adapter": "lora",
"lora_r": 8, "lora_r": 8,
"lora_alpha": 16, "lora_alpha": 16,
"lora_dropout": 0.05, "lora_dropout": 0.05,
"lora_target_linear": True, "lora_target_linear": True,
"val_set_size": 0.01,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 1, "micro_batch_size": 1,
"gradient_accumulation_steps": 2, "gradient_accumulation_steps": 2,
# "gradient_checkpointing": True, # "gradient_checkpointing": True,
"output_dir": temp_dir, "output_dir": temp_dir,
"dataset_prepared_path": temp_dir + "/last_run_prepared",
"learning_rate": 0.00001, "learning_rate": 0.00001,
"optimizer": "adamw_8bit", "optimizer": "adamw_8bit",
"lr_scheduler": "cosine", "lr_scheduler": "cosine",
@@ -71,6 +96,8 @@ class TestMultiGPULlama:
"bf16": True, "bf16": True,
} }
) )
| sft_prepared_dataset_alpaca_cfg
)
# write cfg to yaml file # write cfg to yaml file
Path(temp_dir).mkdir(parents=True, exist_ok=True) Path(temp_dir).mkdir(parents=True, exist_ok=True)
@@ -97,13 +124,13 @@ class TestMultiGPULlama:
"gradient_accumulation_steps", "gradient_accumulation_steps",
[1, 2], [1, 2],
) )
def test_lora_ddp_packed(self, temp_dir, gradient_accumulation_steps): def test_lora_ddp_packed(
self, temp_dir, sft_prepared_dataset_alpaca_cfg, gradient_accumulation_steps
):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
cfg = DictDefault( cfg = (
DictDefault(
{ {
"base_model": "HuggingFaceTB/SmolLM2-135M",
"sequence_len": 2048,
"sample_packing": True,
"eval_sample_packing": False, "eval_sample_packing": False,
"pad_to_sequence_len": True, "pad_to_sequence_len": True,
"adapter": "lora", "adapter": "lora",
@@ -112,23 +139,12 @@ class TestMultiGPULlama:
"lora_dropout": 0.05, "lora_dropout": 0.05,
"lora_target_linear": True, "lora_target_linear": True,
"val_set_size": 0.05, "val_set_size": 0.05,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:20%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 1, "micro_batch_size": 1,
"gradient_accumulation_steps": gradient_accumulation_steps, "gradient_accumulation_steps": gradient_accumulation_steps,
# "gradient_checkpointing": True, # "gradient_checkpointing": True,
"output_dir": temp_dir, "output_dir": temp_dir,
"dataset_prepared_path": temp_dir + "/last_run_prepared",
"learning_rate": 0.00001, "learning_rate": 0.00001,
"optimizer": "adamw_8bit", "optimizer": "adamw_8bit",
"lr_scheduler": "cosine", "lr_scheduler": "cosine",
@@ -137,6 +153,8 @@ class TestMultiGPULlama:
"bf16": True, "bf16": True,
} }
) )
| sft_prepared_dataset_alpaca_cfg
)
# write cfg to yaml file # write cfg to yaml file
Path(temp_dir).mkdir(parents=True, exist_ok=True) Path(temp_dir).mkdir(parents=True, exist_ok=True)
@@ -392,25 +410,13 @@ class TestMultiGPULlama:
"fsdp_state_dict_type", "fsdp_state_dict_type",
["FULL_STATE_DICT", "SHARDED_STATE_DICT"], ["FULL_STATE_DICT", "SHARDED_STATE_DICT"],
) )
def test_fsdp_packed(self, temp_dir, fsdp_state_dict_type): def test_fsdp_packed(
self, temp_dir, sft_prepared_dataset_alpaca_cfg, fsdp_state_dict_type
):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
cfg = DictDefault( cfg = DictDefault(
{ {
"base_model": "HuggingFaceTB/SmolLM2-135M",
"sample_packing": True,
"pad_to_sequence_len": True, "pad_to_sequence_len": True,
"sequence_len": 1024,
"val_set_size": 0.05,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 2, "micro_batch_size": 2,
@@ -438,6 +444,7 @@ class TestMultiGPULlama:
}, },
"use_tensorboard": True, "use_tensorboard": True,
} }
| sft_prepared_dataset_alpaca_cfg
) )
# write cfg to yaml file # write cfg to yaml file
@@ -471,33 +478,23 @@ class TestMultiGPULlama:
[True, False], [True, False],
) )
def test_fsdp2_packed( def test_fsdp2_packed(
self, temp_dir, attention_backend, fsdp_reshard_after_forward self,
temp_dir,
sft_prepared_dataset_alpaca_cfg,
attention_backend,
fsdp_reshard_after_forward,
): ):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
cfg = DictDefault( cfg = (
DictDefault(
{ {
"base_model": "HuggingFaceTB/SmolLM2-135M",
"sample_packing": True,
"pad_to_sequence_len": True, "pad_to_sequence_len": True,
"sequence_len": 2048,
"val_set_size": 0.1,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 4, "micro_batch_size": 4,
"gradient_accumulation_steps": 2, "gradient_accumulation_steps": 2,
"gradient_checkpointing": True, "gradient_checkpointing": True,
"output_dir": temp_dir, "output_dir": temp_dir,
"dataset_prepared_path": temp_dir + "/last_run_prepared",
"learning_rate": 0.00001, "learning_rate": 0.00001,
"optimizer": "adamw_torch_8bit", "optimizer": "adamw_torch_8bit",
"lr_scheduler": "cosine", "lr_scheduler": "cosine",
@@ -517,6 +514,8 @@ class TestMultiGPULlama:
"use_tensorboard": True, "use_tensorboard": True,
} }
) )
| sft_prepared_dataset_alpaca_cfg
)
if attention_backend == "flash": if attention_backend == "flash":
cfg.flash_attention = True cfg.flash_attention = True
elif attention_backend == "flex": elif attention_backend == "flex":
@@ -543,9 +542,12 @@ class TestMultiGPULlama:
temp_dir + "/runs", "train/train_loss", 2.1, "Train Loss (%s) is too high" temp_dir + "/runs", "train/train_loss", 2.1, "Train Loss (%s) is too high"
) )
def test_fsdp_qlora_prequant_packed(self, temp_dir): def test_fsdp_qlora_prequant_packed(
self, temp_dir, sft_prepared_dataset_alpaca_cfg
):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
cfg = DictDefault( cfg = (
DictDefault(
{ {
"base_model": "axolotl-ai-co/SmolLM2-135M-bnb-nf4-bf16", "base_model": "axolotl-ai-co/SmolLM2-135M-bnb-nf4-bf16",
"adapter": "qlora", "adapter": "qlora",
@@ -559,28 +561,14 @@ class TestMultiGPULlama:
# "embed_tokens", # "embed_tokens",
# "lm_head", # "lm_head",
# ], # ],
"sample_packing": True,
"eval_sample_packing": False, "eval_sample_packing": False,
"pad_to_sequence_len": True, "pad_to_sequence_len": True,
"sequence_len": 1024,
"val_set_size": 0.01,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 2, "micro_batch_size": 2,
"gradient_accumulation_steps": 2, "gradient_accumulation_steps": 2,
# "gradient_checkpointing": True, # "gradient_checkpointing": True,
"output_dir": temp_dir, "output_dir": temp_dir,
"dataset_prepared_path": temp_dir + "/last_run_prepared",
"learning_rate": 0.00001, "learning_rate": 0.00001,
"optimizer": "adamw_torch_fused", "optimizer": "adamw_torch_fused",
"lr_scheduler": "cosine", "lr_scheduler": "cosine",
@@ -602,6 +590,8 @@ class TestMultiGPULlama:
"use_tensorboard": True, "use_tensorboard": True,
} }
) )
| sft_prepared_dataset_alpaca_cfg
)
# write cfg to yaml file # write cfg to yaml file
Path(temp_dir).mkdir(parents=True, exist_ok=True) Path(temp_dir).mkdir(parents=True, exist_ok=True)
@@ -641,7 +631,12 @@ class TestMultiGPULlama:
[True, False], [True, False],
) )
def test_ds_zero3_packed( def test_ds_zero3_packed(
self, temp_dir, gradient_accumulation_steps, deepspeed, qlora self,
temp_dir,
sft_prepared_dataset_alpaca_cfg,
gradient_accumulation_steps,
deepspeed,
qlora,
): ):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
if qlora: if qlora:
@@ -655,29 +650,15 @@ class TestMultiGPULlama:
} }
else: else:
adapter = {} adapter = {}
cfg = DictDefault( cfg = (
DictDefault(
{ {
"base_model": "HuggingFaceTB/SmolLM2-135M",
"sample_packing": True,
"pad_to_sequence_len": True, "pad_to_sequence_len": True,
"sequence_len": 1024,
"val_set_size": 0.05,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 1, "micro_batch_size": 1,
"gradient_accumulation_steps": gradient_accumulation_steps, "gradient_accumulation_steps": gradient_accumulation_steps,
"output_dir": temp_dir, "output_dir": temp_dir,
"dataset_prepared_path": temp_dir + "/last_run_prepared",
"learning_rate": 0.00001, "learning_rate": 0.00001,
"optimizer": "adamw_torch_fused", "optimizer": "adamw_torch_fused",
"lr_scheduler": "cosine", "lr_scheduler": "cosine",
@@ -687,6 +668,8 @@ class TestMultiGPULlama:
**adapter, **adapter,
} }
) )
| sft_prepared_dataset_alpaca_cfg
)
# write cfg to yaml file # write cfg to yaml file
Path(temp_dir).mkdir(parents=True, exist_ok=True) Path(temp_dir).mkdir(parents=True, exist_ok=True)
@@ -717,7 +700,13 @@ class TestMultiGPULlama:
"qlora", "qlora",
[True, False], [True, False],
) )
def test_ds_zero2_packed(self, temp_dir, gradient_accumulation_steps, qlora): def test_ds_zero2_packed(
self,
temp_dir,
sft_prepared_dataset_alpaca_cfg,
gradient_accumulation_steps,
qlora,
):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
if qlora: if qlora:
adapter = { adapter = {
@@ -730,29 +719,15 @@ class TestMultiGPULlama:
} }
else: else:
adapter = {} adapter = {}
cfg = DictDefault( cfg = (
DictDefault(
{ {
"base_model": "HuggingFaceTB/SmolLM2-135M",
"sample_packing": True,
"pad_to_sequence_len": True, "pad_to_sequence_len": True,
"sequence_len": 1024,
"val_set_size": 0.01,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 1, "micro_batch_size": 1,
"gradient_accumulation_steps": gradient_accumulation_steps, "gradient_accumulation_steps": gradient_accumulation_steps,
"output_dir": temp_dir, "output_dir": temp_dir,
"dataset_prepared_path": temp_dir + "/last_run_prepared",
"learning_rate": 0.00001, "learning_rate": 0.00001,
"optimizer": "adamw_torch_fused", "optimizer": "adamw_torch_fused",
"lr_scheduler": "cosine", "lr_scheduler": "cosine",
@@ -762,6 +737,8 @@ class TestMultiGPULlama:
**adapter, **adapter,
} }
) )
| sft_prepared_dataset_alpaca_cfg
)
# write cfg to yaml file # write cfg to yaml file
Path(temp_dir).mkdir(parents=True, exist_ok=True) Path(temp_dir).mkdir(parents=True, exist_ok=True)
@@ -792,7 +769,13 @@ class TestMultiGPULlama:
"qlora", "qlora",
[True, False], [True, False],
) )
def test_ds_zero1_packed(self, temp_dir, gradient_accumulation_steps, qlora): def test_ds_zero1_packed(
self,
temp_dir,
sft_prepared_dataset_alpaca_cfg,
gradient_accumulation_steps,
qlora,
):
# pylint: disable=duplicate-code # pylint: disable=duplicate-code
if qlora: if qlora:
adapter = { adapter = {
@@ -805,29 +788,15 @@ class TestMultiGPULlama:
} }
else: else:
adapter = {} adapter = {}
cfg = DictDefault( cfg = (
DictDefault(
{ {
"base_model": "HuggingFaceTB/SmolLM2-135M",
"sample_packing": True,
"pad_to_sequence_len": True, "pad_to_sequence_len": True,
"sequence_len": 1024,
"val_set_size": 0.01,
"special_tokens": {
"pad_token": "<|endoftext|>",
},
"datasets": [
{
"path": "tatsu-lab/alpaca",
"type": "alpaca",
"split": "train[:10%]",
},
],
"num_epochs": 1, "num_epochs": 1,
"max_steps": 2, "max_steps": 2,
"micro_batch_size": 1, "micro_batch_size": 1,
"gradient_accumulation_steps": gradient_accumulation_steps, "gradient_accumulation_steps": gradient_accumulation_steps,
"output_dir": temp_dir, "output_dir": temp_dir,
"dataset_prepared_path": temp_dir + "/last_run_prepared",
"learning_rate": 0.00001, "learning_rate": 0.00001,
"optimizer": "adamw_torch_fused", "optimizer": "adamw_torch_fused",
"lr_scheduler": "cosine", "lr_scheduler": "cosine",
@@ -837,6 +806,8 @@ class TestMultiGPULlama:
**adapter, **adapter,
} }
) )
| sft_prepared_dataset_alpaca_cfg
)
# write cfg to yaml file # write cfg to yaml file
Path(temp_dir).mkdir(parents=True, exist_ok=True) Path(temp_dir).mkdir(parents=True, exist_ok=True)