|
| 1 | +import os |
| 2 | +import time |
| 3 | +from typing import Any, Dict, List |
| 4 | + |
| 5 | +import pytest |
| 6 | +import requests |
| 7 | + |
| 8 | +import together |
| 9 | +from together.utils import parse_timestamp |
| 10 | + |
| 11 | + |
| 12 | +MODEL = "togethercomputer/llama-2-7b" |
| 13 | +N_EPOCHS = 1 |
| 14 | +N_CHECKPOINTS = 1 |
| 15 | +BATCH_SIZE = 32 |
| 16 | +LEARNING_RATE = 0.00001 |
| 17 | +SUFFIX = "pytest" |
| 18 | + |
| 19 | +CANCEL_TIMEOUT = 60 |
| 20 | + |
| 21 | +FT_STATUSES = [ |
| 22 | + "pending", |
| 23 | + "queued", |
| 24 | + "running", |
| 25 | + "cancel_requested", |
| 26 | + "cancelled", |
| 27 | + "error", |
| 28 | + "completed", |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +def list_models() -> List[Any]: |
| 33 | + model_list = together.Models.list() |
| 34 | + model: Dict[str, Any] |
| 35 | + |
| 36 | + finetunable_models = [] |
| 37 | + for model in model_list: |
| 38 | + if model.get("finetuning_supported"): |
| 39 | + finetunable_models.append(model.get("name")) |
| 40 | + return finetunable_models |
| 41 | + |
| 42 | + |
| 43 | +# Download, save, and upload dataset |
| 44 | +def upload_file( |
| 45 | + url: str = "https://huggingface.co/datasets/laion/OIG/resolve/main/unified_joke_explanations.jsonl", |
| 46 | + save_path: str = "unified_joke_explanations.jsonl", |
| 47 | +) -> str: |
| 48 | + download_response = requests.get(url) |
| 49 | + |
| 50 | + assert download_response.status_code == 200 |
| 51 | + |
| 52 | + with open(save_path, "wb") as file: |
| 53 | + file.write(download_response.content) |
| 54 | + |
| 55 | + response = together.Files.upload(save_path) |
| 56 | + os.remove(save_path) |
| 57 | + |
| 58 | + assert isinstance(response, dict) |
| 59 | + file_id = str(response["id"]) |
| 60 | + return file_id |
| 61 | + |
| 62 | + |
| 63 | +def create_ft( |
| 64 | + model: str, |
| 65 | + n_epochs: int, |
| 66 | + n_checkpoints: int, |
| 67 | + batch_size: int, |
| 68 | + learning_rate: float, |
| 69 | + suffix: str, |
| 70 | + file_id: str, |
| 71 | +) -> Dict[Any, Any]: |
| 72 | + response = together.Finetune.create( |
| 73 | + training_file=file_id, |
| 74 | + model=model, |
| 75 | + n_epochs=n_epochs, |
| 76 | + n_checkpoints=n_checkpoints, |
| 77 | + batch_size=batch_size, |
| 78 | + learning_rate=learning_rate, |
| 79 | + suffix=suffix, |
| 80 | + ) |
| 81 | + return response |
| 82 | + |
| 83 | + |
| 84 | +def test_create() -> None: |
| 85 | + file_id = upload_file() |
| 86 | + response = create_ft( |
| 87 | + MODEL, N_EPOCHS, N_CHECKPOINTS, BATCH_SIZE, LEARNING_RATE, SUFFIX, file_id |
| 88 | + ) |
| 89 | + |
| 90 | + assert isinstance(response, dict) |
| 91 | + assert response["training_file"] == file_id |
| 92 | + assert response["model"] == MODEL |
| 93 | + assert SUFFIX in str(response["model_output_name"]) |
| 94 | + |
| 95 | + |
| 96 | +def test_list() -> None: |
| 97 | + response = together.Finetune.list() |
| 98 | + assert isinstance(response, dict) |
| 99 | + assert isinstance(response["data"], list) |
| 100 | + |
| 101 | + |
| 102 | +def test_retrieve() -> None: |
| 103 | + ft_list = together.Finetune.list()["data"] |
| 104 | + ft_list.sort(key=lambda x: parse_timestamp(x["created_at"])) |
| 105 | + ft_id = ft_list[-1]["id"] |
| 106 | + response = together.Finetune.retrieve(ft_id) |
| 107 | + |
| 108 | + assert isinstance(response, dict) |
| 109 | + assert str(response["training_file"]).startswith("file-") |
| 110 | + assert str(response["id"]).startswith("ft-") |
| 111 | + |
| 112 | + |
| 113 | +def test_list_events() -> None: |
| 114 | + ft_list = together.Finetune.list()["data"] |
| 115 | + ft_list.sort(key=lambda x: parse_timestamp(x["created_at"])) |
| 116 | + ft_id = ft_list[-1]["id"] |
| 117 | + response = together.Finetune.list_events(ft_id) |
| 118 | + |
| 119 | + assert isinstance(response, dict) |
| 120 | + assert isinstance(response["data"], list) |
| 121 | + |
| 122 | + |
| 123 | +def test_status() -> None: |
| 124 | + ft_list = together.Finetune.list()["data"] |
| 125 | + ft_list.sort(key=lambda x: parse_timestamp(x["created_at"])) |
| 126 | + ft_id = ft_list[-1]["id"] |
| 127 | + response = together.Finetune.get_job_status(ft_id) |
| 128 | + |
| 129 | + assert isinstance(response, str) |
| 130 | + assert response in FT_STATUSES |
| 131 | + |
| 132 | + |
| 133 | +def test_download() -> None: |
| 134 | + ft_list = together.Finetune.list()["data"] |
| 135 | + ft_list.sort(key=lambda x: parse_timestamp(x["created_at"])) |
| 136 | + ft_list.reverse() |
| 137 | + |
| 138 | + ft_id = None |
| 139 | + for item in ft_list: |
| 140 | + id = item["id"] |
| 141 | + if together.Finetune.get_job_status(id) == "completed": |
| 142 | + ft_id = id |
| 143 | + break |
| 144 | + |
| 145 | + if ft_id is None: |
| 146 | + # no models available to download |
| 147 | + assert False |
| 148 | + |
| 149 | + output_file = together.Finetune.download(ft_id) |
| 150 | + |
| 151 | + assert os.path.exists(output_file) |
| 152 | + assert os.path.getsize(output_file) > 0 |
| 153 | + |
| 154 | + os.remove(output_file) |
| 155 | + |
| 156 | + |
| 157 | +def test_cancel() -> None: |
| 158 | + cancelled = False |
| 159 | + file_id = upload_file() |
| 160 | + response, file_id = create_ft( |
| 161 | + MODEL, N_EPOCHS, N_CHECKPOINTS, BATCH_SIZE, LEARNING_RATE, SUFFIX, file_id |
| 162 | + ) |
| 163 | + ft_id = response["id"] |
| 164 | + response = together.Finetune.cancel(ft_id) |
| 165 | + |
| 166 | + # loop to check if job was cancelled |
| 167 | + start = time.time() |
| 168 | + while time.time() - start < CANCEL_TIMEOUT: |
| 169 | + status = together.Finetune.get_job_status(ft_id) |
| 170 | + if status == "cancel_requested": |
| 171 | + cancelled = True |
| 172 | + break |
| 173 | + time.sleep(1) |
| 174 | + |
| 175 | + assert cancelled |
| 176 | + |
| 177 | + # delete file after cancelling |
| 178 | + together.Files.delete(file_id) |
| 179 | + |
| 180 | + |
| 181 | +def test_checkpoints() -> None: |
| 182 | + ft_list = together.Finetune.list()["data"] |
| 183 | + ft_list.sort(key=lambda x: parse_timestamp(x["created_at"])) |
| 184 | + ft_list.reverse() |
| 185 | + |
| 186 | + ft_id = None |
| 187 | + for item in ft_list: |
| 188 | + id = item["id"] |
| 189 | + if together.Finetune.get_job_status(id) == "completed": |
| 190 | + ft_id = id |
| 191 | + break |
| 192 | + |
| 193 | + if ft_id is None: |
| 194 | + # no models available to download |
| 195 | + assert False |
| 196 | + |
| 197 | + response = together.Finetune.get_checkpoints(ft_id) |
| 198 | + |
| 199 | + assert isinstance(response, list) |
| 200 | + |
| 201 | + |
| 202 | +if __name__ == "__main__": |
| 203 | + assert ( |
| 204 | + together.api_key |
| 205 | + ), "No API key found, please run `export TOGETHER_API_KEY=<API_KEY>`" |
| 206 | + pytest.main([__file__]) |
0 commit comments