|
| 1 | +import pytest |
| 2 | + |
| 3 | +from together.resources.finetune import createFinetuneRequest |
| 4 | +from together.types.finetune import ( |
| 5 | + FinetuneTrainingLimits, |
| 6 | + FinetuneFullTrainingLimits, |
| 7 | + FinetuneLoraTrainingLimits, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +_MODEL_NAME = "meta-llama/Meta-Llama-3.1-8B-Instruct-Reference" |
| 12 | +_TRAINING_FILE = "file-7dbce5e9-7993-4520-9f3e-a7ece6c39d84" |
| 13 | +_VALIDATION_FILE = "file-7dbce5e9-7553-4520-9f3e-a7ece6c39d84" |
| 14 | +_FROM_CHECKPOINT = "ft-12345678-1234-1234-1234-1234567890ab" |
| 15 | +_MODEL_LIMITS = FinetuneTrainingLimits( |
| 16 | + max_num_epochs=20, |
| 17 | + max_learning_rate=1.0, |
| 18 | + min_learning_rate=1e-6, |
| 19 | + full_training=FinetuneFullTrainingLimits( |
| 20 | + max_batch_size=96, |
| 21 | + min_batch_size=8, |
| 22 | + ), |
| 23 | + lora_training=FinetuneLoraTrainingLimits( |
| 24 | + max_batch_size=128, |
| 25 | + min_batch_size=8, |
| 26 | + max_rank=64, |
| 27 | + target_modules=["q", "k", "v", "o", "mlp"], |
| 28 | + ), |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def test_simple_request(): |
| 33 | + request = createFinetuneRequest( |
| 34 | + model_limits=_MODEL_LIMITS, |
| 35 | + model=_MODEL_NAME, |
| 36 | + training_file=_TRAINING_FILE, |
| 37 | + ) |
| 38 | + |
| 39 | + assert request.model == _MODEL_NAME |
| 40 | + assert request.training_file == _TRAINING_FILE |
| 41 | + assert request.learning_rate > 0 |
| 42 | + assert request.n_epochs > 0 |
| 43 | + assert request.warmup_ratio == 0.0 |
| 44 | + assert request.training_type.type == "Full" |
| 45 | + assert request.batch_size == _MODEL_LIMITS.full_training.max_batch_size |
| 46 | + |
| 47 | + |
| 48 | +def test_validation_file(): |
| 49 | + request = createFinetuneRequest( |
| 50 | + model_limits=_MODEL_LIMITS, |
| 51 | + model=_MODEL_NAME, |
| 52 | + training_file=_TRAINING_FILE, |
| 53 | + validation_file=_VALIDATION_FILE, |
| 54 | + ) |
| 55 | + |
| 56 | + assert request.training_file == _TRAINING_FILE |
| 57 | + assert request.validation_file == _VALIDATION_FILE |
| 58 | + |
| 59 | + |
| 60 | +def test_no_training_file(): |
| 61 | + with pytest.raises( |
| 62 | + TypeError, match="missing 1 required positional argument: 'training_file'" |
| 63 | + ): |
| 64 | + _ = createFinetuneRequest( |
| 65 | + model_limits=_MODEL_LIMITS, |
| 66 | + model=_MODEL_NAME, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def test_lora_request(): |
| 71 | + request = createFinetuneRequest( |
| 72 | + model_limits=_MODEL_LIMITS, |
| 73 | + model=_MODEL_NAME, |
| 74 | + training_file=_TRAINING_FILE, |
| 75 | + lora=True, |
| 76 | + ) |
| 77 | + |
| 78 | + assert request.training_type.type == "Lora" |
| 79 | + assert request.training_type.lora_r == _MODEL_LIMITS.lora_training.max_rank |
| 80 | + assert request.training_type.lora_alpha == _MODEL_LIMITS.lora_training.max_rank * 2 |
| 81 | + assert request.training_type.lora_dropout == 0.0 |
| 82 | + assert request.training_type.lora_trainable_modules == "all-linear" |
| 83 | + assert request.batch_size == _MODEL_LIMITS.lora_training.max_batch_size |
| 84 | + |
| 85 | + |
| 86 | +def test_from_checkpoint_request(): |
| 87 | + request = createFinetuneRequest( |
| 88 | + model_limits=_MODEL_LIMITS, |
| 89 | + training_file=_TRAINING_FILE, |
| 90 | + from_checkpoint=_FROM_CHECKPOINT, |
| 91 | + ) |
| 92 | + |
| 93 | + assert request.model is None |
| 94 | + assert request.from_checkpoint == _FROM_CHECKPOINT |
| 95 | + |
| 96 | + |
| 97 | +def test_both_from_checkpoint_model_name(): |
| 98 | + with pytest.raises( |
| 99 | + ValueError, |
| 100 | + match="You must specify either a model or a checkpoint to start a job from, not both", |
| 101 | + ): |
| 102 | + _ = createFinetuneRequest( |
| 103 | + model_limits=_MODEL_LIMITS, |
| 104 | + model=_MODEL_NAME, |
| 105 | + training_file=_TRAINING_FILE, |
| 106 | + from_checkpoint=_FROM_CHECKPOINT, |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def test_no_from_checkpoint_no_model_name(): |
| 111 | + with pytest.raises( |
| 112 | + ValueError, match="You must specify either a model or a checkpoint" |
| 113 | + ): |
| 114 | + _ = createFinetuneRequest( |
| 115 | + model_limits=_MODEL_LIMITS, |
| 116 | + training_file=_TRAINING_FILE, |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def test_batch_size_limit(): |
| 121 | + with pytest.raises( |
| 122 | + ValueError, |
| 123 | + match="Requested batch size is higher that the maximum allowed value", |
| 124 | + ): |
| 125 | + _ = createFinetuneRequest( |
| 126 | + model_limits=_MODEL_LIMITS, |
| 127 | + model=_MODEL_NAME, |
| 128 | + training_file=_TRAINING_FILE, |
| 129 | + batch_size=128, |
| 130 | + ) |
| 131 | + |
| 132 | + with pytest.raises( |
| 133 | + ValueError, match="Requested batch size is lower that the minimum allowed value" |
| 134 | + ): |
| 135 | + _ = createFinetuneRequest( |
| 136 | + model_limits=_MODEL_LIMITS, |
| 137 | + model=_MODEL_NAME, |
| 138 | + training_file=_TRAINING_FILE, |
| 139 | + batch_size=1, |
| 140 | + ) |
| 141 | + |
| 142 | + with pytest.raises( |
| 143 | + ValueError, |
| 144 | + match="Requested batch size is higher that the maximum allowed value", |
| 145 | + ): |
| 146 | + _ = createFinetuneRequest( |
| 147 | + model_limits=_MODEL_LIMITS, |
| 148 | + model=_MODEL_NAME, |
| 149 | + training_file=_TRAINING_FILE, |
| 150 | + batch_size=256, |
| 151 | + lora=True, |
| 152 | + ) |
| 153 | + |
| 154 | + with pytest.raises( |
| 155 | + ValueError, match="Requested batch size is lower that the minimum allowed value" |
| 156 | + ): |
| 157 | + _ = createFinetuneRequest( |
| 158 | + model_limits=_MODEL_LIMITS, |
| 159 | + model=_MODEL_NAME, |
| 160 | + training_file=_TRAINING_FILE, |
| 161 | + batch_size=1, |
| 162 | + lora=True, |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +def test_non_lora_model(): |
| 167 | + with pytest.raises( |
| 168 | + ValueError, match="LoRA adapters are not supported for the selected model." |
| 169 | + ): |
| 170 | + _ = createFinetuneRequest( |
| 171 | + model_limits=FinetuneTrainingLimits( |
| 172 | + max_num_epochs=20, |
| 173 | + max_learning_rate=1.0, |
| 174 | + min_learning_rate=1e-6, |
| 175 | + full_training=FinetuneFullTrainingLimits( |
| 176 | + max_batch_size=96, |
| 177 | + min_batch_size=8, |
| 178 | + ), |
| 179 | + lora_training=None, |
| 180 | + ), |
| 181 | + model=_MODEL_NAME, |
| 182 | + training_file=_TRAINING_FILE, |
| 183 | + lora=True, |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +def test_non_full_model(): |
| 188 | + with pytest.raises( |
| 189 | + ValueError, match="Full training is not supported for the selected model." |
| 190 | + ): |
| 191 | + _ = createFinetuneRequest( |
| 192 | + model_limits=FinetuneTrainingLimits( |
| 193 | + max_num_epochs=20, |
| 194 | + max_learning_rate=1.0, |
| 195 | + min_learning_rate=1e-6, |
| 196 | + lora_training=FinetuneLoraTrainingLimits( |
| 197 | + max_batch_size=96, |
| 198 | + min_batch_size=8, |
| 199 | + max_rank=64, |
| 200 | + target_modules=["q", "k", "v", "o", "mlp"], |
| 201 | + ), |
| 202 | + full_training=None, |
| 203 | + ), |
| 204 | + model=_MODEL_NAME, |
| 205 | + training_file=_TRAINING_FILE, |
| 206 | + lora=False, |
| 207 | + ) |
| 208 | + |
| 209 | + |
| 210 | +@pytest.mark.parametrize("warmup_ratio", [-1.0, 2.0]) |
| 211 | +def test_bad_warmup(warmup_ratio): |
| 212 | + with pytest.raises(ValueError, match="Warmup ratio should be between 0 and 1"): |
| 213 | + _ = createFinetuneRequest( |
| 214 | + model_limits=_MODEL_LIMITS, |
| 215 | + model=_MODEL_NAME, |
| 216 | + training_file=_TRAINING_FILE, |
| 217 | + warmup_ratio=warmup_ratio, |
| 218 | + ) |
| 219 | + |
| 220 | + |
| 221 | +@pytest.mark.parametrize("min_lr_ratio", [-1.0, 2.0]) |
| 222 | +def test_bad_min_lr_ratio(min_lr_ratio): |
| 223 | + with pytest.raises( |
| 224 | + ValueError, match="Min learning rate ratio should be between 0 and 1" |
| 225 | + ): |
| 226 | + _ = createFinetuneRequest( |
| 227 | + model_limits=_MODEL_LIMITS, |
| 228 | + model=_MODEL_NAME, |
| 229 | + training_file=_TRAINING_FILE, |
| 230 | + min_lr_ratio=min_lr_ratio, |
| 231 | + ) |
| 232 | + |
| 233 | + |
| 234 | +@pytest.mark.parametrize("max_grad_norm", [-1.0, -0.01]) |
| 235 | +def test_bad_max_grad_norm(max_grad_norm): |
| 236 | + with pytest.raises(ValueError, match="Max gradient norm should be non-negative"): |
| 237 | + _ = createFinetuneRequest( |
| 238 | + model_limits=_MODEL_LIMITS, |
| 239 | + model=_MODEL_NAME, |
| 240 | + training_file=_TRAINING_FILE, |
| 241 | + max_grad_norm=max_grad_norm, |
| 242 | + ) |
| 243 | + |
| 244 | + |
| 245 | +@pytest.mark.parametrize("weight_decay", [-1.0, -0.01]) |
| 246 | +def test_bad_weight_decay(weight_decay): |
| 247 | + with pytest.raises(ValueError, match="Weight decay should be non-negative"): |
| 248 | + _ = createFinetuneRequest( |
| 249 | + model_limits=_MODEL_LIMITS, |
| 250 | + model=_MODEL_NAME, |
| 251 | + training_file=_TRAINING_FILE, |
| 252 | + weight_decay=weight_decay, |
| 253 | + ) |
| 254 | + |
| 255 | + |
| 256 | +def test_bad_training_method(): |
| 257 | + with pytest.raises(ValueError, match="training_method must be one of .*"): |
| 258 | + _ = createFinetuneRequest( |
| 259 | + model_limits=_MODEL_LIMITS, |
| 260 | + model=_MODEL_NAME, |
| 261 | + training_file=_TRAINING_FILE, |
| 262 | + training_method="NON_SFT", |
| 263 | + ) |
0 commit comments