|
| 1 | +import shutil |
| 2 | +import unittest |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from tests.utils import execute_shell_command |
| 6 | + |
| 7 | +CACHE_DIR = Path(__file__).parent.parent.parent.joinpath("cache") |
| 8 | + |
| 9 | + |
| 10 | +def replace_in_script(script_path: Path, pattern: str, replacement: str): |
| 11 | + with open(script_path, "r") as f: |
| 12 | + script = f.readlines() |
| 13 | + script = [line.replace(pattern, replacement) for line in script] |
| 14 | + with open(script_path, "w") as f: |
| 15 | + for line in script: |
| 16 | + f.write(line) |
| 17 | + |
| 18 | + |
| 19 | +class TestTrainEagle3(unittest.TestCase): |
| 20 | + |
| 21 | + def setUp(self) -> None: |
| 22 | + # prepare data |
| 23 | + data_process = execute_shell_command( |
| 24 | + "python scripts/prepare_data.py --dataset sharegpt" |
| 25 | + ) |
| 26 | + data_process.wait() |
| 27 | + |
| 28 | + # modify the sccript to only train for 10 steps |
| 29 | + # add --max-num-steps 10 to the launch command |
| 30 | + script_path = Path(__file__).parent.parent.parent.joinpath( |
| 31 | + "examples", "run_llama3.1_8b_eagle3_online.sh" |
| 32 | + ) |
| 33 | + with open(script_path, "r") as f: |
| 34 | + script = f.readlines() |
| 35 | + |
| 36 | + # remove empty lines |
| 37 | + script = [line for line in script if line.strip()] |
| 38 | + script[-1] = script[-1].rstrip() + " --max-num-steps 10" |
| 39 | + |
| 40 | + # replace meta-llama/Llama-3.1-8B-Instruct with unsloth/Llama-3.2-1B-Instruct |
| 41 | + # so that we don't need HF token for gated repo |
| 42 | + script = [ |
| 43 | + line.replace( |
| 44 | + "meta-llama/Llama-3.1-8B-Instruct", "nreHieW/Llama-3.1-8B-Instruct" |
| 45 | + ) |
| 46 | + for line in script |
| 47 | + ] |
| 48 | + |
| 49 | + # write the script back to the file |
| 50 | + with open(script_path, "w") as f: |
| 51 | + for line in script: |
| 52 | + f.write(line) |
| 53 | + |
| 54 | + def test_online_train_eagle3_with_sglang_backend(self): |
| 55 | + # run training |
| 56 | + train_process = execute_shell_command( |
| 57 | + "bash examples/run_llama3.1_8b_eagle3_online.sh 2" |
| 58 | + ) |
| 59 | + train_process.wait() |
| 60 | + self.assertEqual(train_process.returncode, 0) |
| 61 | + |
| 62 | + def test_online_train_eagle3_with_hf_backend(self): |
| 63 | + # replace --target-model-backend sglang with --target-model-backend hf |
| 64 | + script_path = Path(__file__).parent.parent.parent.joinpath( |
| 65 | + "examples", "run_llama3.1_8b_eagle3_online.sh" |
| 66 | + ) |
| 67 | + replace_in_script( |
| 68 | + script_path, "--target-model-backend sglang", "--target-model-backend hf" |
| 69 | + ) |
| 70 | + |
| 71 | + # run training |
| 72 | + train_process = execute_shell_command( |
| 73 | + "bash examples/run_llama3.1_8b_eagle3_online.sh 2" |
| 74 | + ) |
| 75 | + train_process.wait() |
| 76 | + self.assertEqual(train_process.returncode, 0) |
| 77 | + |
| 78 | + def test_online_train_eagle3_with_custom_backend(self): |
| 79 | + # replace --target-model-backend sglang with --target-model-backend custom |
| 80 | + script_path = Path(__file__).parent.parent.parent.joinpath( |
| 81 | + "examples", "run_llama3.1_8b_eagle3_online.sh" |
| 82 | + ) |
| 83 | + replace_in_script( |
| 84 | + script_path, |
| 85 | + "--target-model-backend sglang", |
| 86 | + "--target-model-backend custom", |
| 87 | + ) |
| 88 | + |
| 89 | + # run training |
| 90 | + train_process = execute_shell_command( |
| 91 | + "bash examples/run_llama3.1_8b_eagle3_online.sh 2" |
| 92 | + ) |
| 93 | + train_process.wait() |
| 94 | + self.assertEqual(train_process.returncode, 0) |
| 95 | + |
| 96 | + def test_offline_train_eagle3(self): |
| 97 | + # remove the hidden states if they exist |
| 98 | + script_path = Path(__file__).parent.parent.parent.joinpath( |
| 99 | + "examples", "run_llama3.1_8b_eagle3_offline.sh" |
| 100 | + ) |
| 101 | + replace_in_script( |
| 102 | + script_path, |
| 103 | + "meta-llama/Llama-3.1-8B-Instruct", |
| 104 | + "nreHieW/Llama-3.1-8B-Instruct", |
| 105 | + ) |
| 106 | + replace_in_script( |
| 107 | + script_path, |
| 108 | + "--batch-size 32", |
| 109 | + "--batch-size 5", |
| 110 | + ) |
| 111 | + replace_in_script( |
| 112 | + script_path, |
| 113 | + "scripts/prepare_hidden_states.py", |
| 114 | + "scripts/prepare_hidden_states.py --num-samples 10", |
| 115 | + ) |
| 116 | + replace_in_script( |
| 117 | + script_path, |
| 118 | + "$ROOT_DIR/scripts/train_eagle3.py", |
| 119 | + "$ROOT_DIR/scripts/train_eagle3.py --max-num-steps 2", |
| 120 | + ) |
| 121 | + |
| 122 | + hidden_states_path = Path(__file__).parent.parent.parent.joinpath( |
| 123 | + "cache", "hidden_states", "sharegpt_train_Llama-3.1-8B-Instruct" |
| 124 | + ) |
| 125 | + if hidden_states_path.exists(): |
| 126 | + # delete the directory |
| 127 | + shutil.rmtree(hidden_states_path) |
| 128 | + |
| 129 | + training_process = execute_shell_command( |
| 130 | + "bash examples/run_llama3.1_8b_eagle3_offline.sh 2", |
| 131 | + ) |
| 132 | + training_process.wait() |
| 133 | + self.assertEqual(training_process.returncode, 0) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + unittest.main(verbosity=2) |
0 commit comments