1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import pytest
from pathlib import Path
from test_base import TestBase, servicePreparer
from devtools_testutils.aio import recorded_by_proxy_async
from devtools_testutils import is_live_and_not_recording
@pytest.mark.skipif(
condition=(not is_live_and_not_recording()),
reason="Skipped because we cannot record network calls with AOAI client",
)
class TestFineTuningAsync(TestBase):
async def _create_sft_finetuning_job_async(
self, openai_client, train_file_id, validation_file_id, model_type="openai", training_type="Standard"
):
"""Helper method to create a supervised fine-tuning job asynchronously."""
return await openai_client.fine_tuning.jobs.create(
training_file=train_file_id,
validation_file=validation_file_id,
model=self.test_finetuning_params["sft"][model_type]["model_name"],
method={
"type": "supervised",
"supervised": {
"hyperparameters": {
"n_epochs": self.test_finetuning_params["n_epochs"],
"batch_size": self.test_finetuning_params["batch_size"],
"learning_rate_multiplier": self.test_finetuning_params["learning_rate_multiplier"],
}
},
},
extra_body={"trainingType": training_type},
)
async def _create_dpo_finetuning_job_async(self, openai_client, train_file_id, validation_file_id):
"""Helper method to create a DPO fine-tuning job asynchronously."""
return await openai_client.fine_tuning.jobs.create(
training_file=train_file_id,
validation_file=validation_file_id,
model=self.test_finetuning_params["dpo"]["openai"]["model_name"],
method={
"type": "dpo",
"dpo": {
"hyperparameters": {
"n_epochs": self.test_finetuning_params["n_epochs"],
"batch_size": self.test_finetuning_params["batch_size"],
"learning_rate_multiplier": self.test_finetuning_params["learning_rate_multiplier"],
}
},
},
extra_body={"trainingType": "Standard"},
)
async def _create_rft_finetuning_job_async(self, openai_client, train_file_id, validation_file_id):
"""Helper method to create an RFT fine-tuning job asynchronously."""
grader = {
"name": "Response Quality Grader",
"type": "score_model",
"model": "o3-mini",
"input": [
{
"role": "user",
"content": "Evaluate the model's response based on correctness and quality. Rate from 0 to 10.",
}
],
"range": [0.0, 10.0],
}
return await openai_client.fine_tuning.jobs.create(
training_file=train_file_id,
validation_file=validation_file_id,
model=self.test_finetuning_params["rft"]["openai"]["model_name"],
method={
"type": "reinforcement",
"reinforcement": {
"grader": grader,
"hyperparameters": {
"n_epochs": self.test_finetuning_params["n_epochs"],
"batch_size": self.test_finetuning_params["batch_size"],
"learning_rate_multiplier": self.test_finetuning_params["learning_rate_multiplier"],
"eval_interval": 5,
"eval_samples": 2,
"reasoning_effort": "medium",
},
},
},
extra_body={"trainingType": "Standard"},
)
async def _upload_test_files_async(self, openai_client, job_type="sft"):
"""Helper method to upload training and validation files for fine-tuning tests asynchronously."""
test_data_dir = Path(__file__).parent.parent / "test_data" / "finetuning"
training_file_path = test_data_dir / self.test_finetuning_params[job_type]["training_file_name"]
validation_file_path = test_data_dir / self.test_finetuning_params[job_type]["validation_file_name"]
with open(training_file_path, "rb") as f:
train_file = await openai_client.files.create(file=f, purpose="fine-tune")
train_processed_file = await openai_client.files.wait_for_processing(train_file.id)
assert train_processed_file is not None
assert train_processed_file.id is not None
TestBase.assert_equal_or_not_none(train_processed_file.status, "processed")
print(f"[test_finetuning_{job_type}_async] Uploaded training file: {train_processed_file.id}")
with open(validation_file_path, "rb") as f:
validation_file = await openai_client.files.create(file=f, purpose="fine-tune")
validation_processed_file = await openai_client.files.wait_for_processing(validation_file.id)
assert validation_processed_file is not None
assert validation_processed_file.id is not None
TestBase.assert_equal_or_not_none(validation_processed_file.status, "processed")
print(f"[test_finetuning_{job_type}_async] Uploaded validation file: {validation_processed_file.id}")
return train_processed_file, validation_processed_file
async def _cleanup_test_files_async(self, openai_client, train_file, validation_file, job_type):
"""Helper method to clean up uploaded files after testing asynchronously."""
await openai_client.files.delete(train_file.id)
print(f"[test_finetuning_{job_type}_async] Deleted training file: {train_file.id}")
await openai_client.files.delete(validation_file.id)
print(f"[test_finetuning_{job_type}_async] Deleted validation file: {validation_file.id}")
@servicePreparer()
@recorded_by_proxy_async
async def test_sft_finetuning_create_job_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "sft")
fine_tuning_job = await self._create_sft_finetuning_job_async(
openai_client, train_file.id, validation_file.id
)
print(f"[test_finetuning_sft_async] Created fine-tuning job: {fine_tuning_job.id}")
TestBase.validate_fine_tuning_job(fine_tuning_job)
TestBase.assert_equal_or_not_none(fine_tuning_job.training_file, train_file.id)
TestBase.assert_equal_or_not_none(fine_tuning_job.validation_file, validation_file.id)
assert fine_tuning_job.method is not None, "Method should not be None for SFT job"
TestBase.assert_equal_or_not_none(fine_tuning_job.method.type, "supervised")
print(f"[test_finetuning_sft_async] SFT method validation passed - type: {fine_tuning_job.method.type}")
await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_sft_async] Cancelled job: {fine_tuning_job.id}")
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "sft")
@servicePreparer()
@recorded_by_proxy_async
async def test_finetuning_retrieve_job_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "sft")
fine_tuning_job = await self._create_sft_finetuning_job_async(
openai_client, train_file.id, validation_file.id
)
print(f"[test_finetuning_sft_async] Created job: {fine_tuning_job.id}")
retrieved_job = await openai_client.fine_tuning.jobs.retrieve(fine_tuning_job.id)
print(f"[test_finetuning_sft_async] Retrieved job: {retrieved_job.id}")
TestBase.validate_fine_tuning_job(retrieved_job, expected_job_id=fine_tuning_job.id)
TestBase.assert_equal_or_not_none(retrieved_job.training_file, train_file.id)
TestBase.assert_equal_or_not_none(retrieved_job.validation_file, validation_file.id)
await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_sft_async] Cancelled job: {fine_tuning_job.id}")
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "sft")
@servicePreparer()
@recorded_by_proxy_async
async def test_finetuning_list_jobs_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "sft")
fine_tuning_job = await self._create_sft_finetuning_job_async(
openai_client, train_file.id, validation_file.id
)
print(f"[test_finetuning_sft_async] Created job: {fine_tuning_job.id}")
jobs_list_async = openai_client.fine_tuning.jobs.list()
jobs_list = []
async for job in jobs_list_async:
jobs_list.append(job)
print(f"[test_finetuning_sft_async] Listed {len(jobs_list)} jobs")
assert len(jobs_list) > 0
job_ids = [job.id for job in jobs_list]
assert fine_tuning_job.id in job_ids
await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_sft_async] Cancelled job: {fine_tuning_job.id}")
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "sft")
@servicePreparer()
@recorded_by_proxy_async
async def test_finetuning_cancel_job_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "sft")
fine_tuning_job = await self._create_sft_finetuning_job_async(
openai_client, train_file.id, validation_file.id
)
print(f"[test_finetuning_sft_async] Created job: {fine_tuning_job.id}")
cancelled_job = await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_sft_async] Cancelled job: {cancelled_job.id}")
TestBase.validate_fine_tuning_job(cancelled_job, expected_job_id=fine_tuning_job.id)
TestBase.assert_equal_or_not_none(cancelled_job.status, "cancelled")
retrieved_job = await openai_client.fine_tuning.jobs.retrieve(fine_tuning_job.id)
print(f"[test_finetuning_sft_async] Verified cancellation persisted for job: {retrieved_job.id}")
TestBase.validate_fine_tuning_job(
retrieved_job, expected_job_id=fine_tuning_job.id, expected_status="cancelled"
)
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "sft")
@servicePreparer()
@recorded_by_proxy_async
async def test_dpo_finetuning_create_job_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "dpo")
fine_tuning_job = await self._create_dpo_finetuning_job_async(
openai_client, train_file.id, validation_file.id
)
print(f"[test_finetuning_dpo_async] Created DPO fine-tuning job: {fine_tuning_job.id}")
print(fine_tuning_job)
TestBase.validate_fine_tuning_job(fine_tuning_job)
TestBase.assert_equal_or_not_none(fine_tuning_job.training_file, train_file.id)
TestBase.assert_equal_or_not_none(fine_tuning_job.validation_file, validation_file.id)
assert fine_tuning_job.method is not None, "Method should not be None for DPO job"
TestBase.assert_equal_or_not_none(fine_tuning_job.method.type, "dpo")
print(f"[test_finetuning_dpo_async] DPO method validation passed - type: {fine_tuning_job.method.type}")
await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_dpo_async] Cancelled job: {fine_tuning_job.id}")
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "dpo")
@servicePreparer()
@recorded_by_proxy_async
async def test_rft_finetuning_create_job_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "rft")
fine_tuning_job = await self._create_rft_finetuning_job_async(
openai_client, train_file.id, validation_file.id
)
print(f"[test_finetuning_rft_async] Created RFT fine-tuning job: {fine_tuning_job.id}")
TestBase.validate_fine_tuning_job(fine_tuning_job)
TestBase.assert_equal_or_not_none(fine_tuning_job.training_file, train_file.id)
TestBase.assert_equal_or_not_none(fine_tuning_job.validation_file, validation_file.id)
assert fine_tuning_job.method is not None, "Method should not be None for RFT job"
TestBase.assert_equal_or_not_none(fine_tuning_job.method.type, "reinforcement")
print(f"[test_finetuning_rft_async] RFT method validation passed - type: {fine_tuning_job.method.type}")
await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_rft_async] Cancelled job: {fine_tuning_job.id}")
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "rft")
@servicePreparer()
@recorded_by_proxy_async
async def test_finetuning_list_events_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "sft")
fine_tuning_job = await self._create_sft_finetuning_job_async(
openai_client, train_file.id, validation_file.id
)
print(f"[test_finetuning_sft_async] Created job: {fine_tuning_job.id}")
TestBase.validate_fine_tuning_job(fine_tuning_job)
TestBase.assert_equal_or_not_none(fine_tuning_job.training_file, train_file.id)
TestBase.assert_equal_or_not_none(fine_tuning_job.validation_file, validation_file.id)
await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_sft_async] Cancelled job: {fine_tuning_job.id}")
events_list_async = openai_client.fine_tuning.jobs.list_events(fine_tuning_job.id)
events_list = []
async for event in events_list_async:
events_list.append(event)
print(f"[test_finetuning_sft_async] Listed {len(events_list)} events for job: {fine_tuning_job.id}")
# Verify that events exist (at minimum, job creation event should be present)
assert len(events_list) > 0, "Fine-tuning job should have at least one event"
# Verify events have required attributes
for event in events_list:
assert event.id is not None, "Event should have an ID"
assert event.object is not None, "Event should have an object type"
assert event.created_at is not None, "Event should have a creation timestamp"
assert event.level is not None, "Event should have a level"
assert event.message is not None, "Event should have a message"
assert event.type is not None, "Event should have a type"
print(f"[test_finetuning_sft_async] Successfully validated {len(events_list)} events")
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "sft")
@servicePreparer()
@recorded_by_proxy_async
async def test_sft_finetuning_create_job_oss_model_async(self, **kwargs):
project_client = self.create_async_client(**kwargs)
openai_client = project_client.get_openai_client()
async with project_client:
train_file, validation_file = await self._upload_test_files_async(openai_client, "sft")
fine_tuning_job = await self._create_sft_finetuning_job_async(
openai_client, train_file.id, validation_file.id, "oss", "GlobalStandard"
)
print(f"[test_finetuning_sft_oss_async] Created fine-tuning job: {fine_tuning_job.id}")
TestBase.validate_fine_tuning_job(
fine_tuning_job, expected_model=self.test_finetuning_params["sft"]["oss"]["model_name"]
)
TestBase.validate_fine_tuning_job(fine_tuning_job)
TestBase.assert_equal_or_not_none(fine_tuning_job.training_file, train_file.id)
TestBase.assert_equal_or_not_none(fine_tuning_job.validation_file, validation_file.id)
assert fine_tuning_job.method is not None, "Method should not be None for SFT job"
TestBase.assert_equal_or_not_none(fine_tuning_job.method.type, "supervised")
print(f"[test_finetuning_sft_oss_async] SFT method validation passed - type: {fine_tuning_job.method.type}")
await openai_client.fine_tuning.jobs.cancel(fine_tuning_job.id)
print(f"[test_finetuning_sft_oss_async] Cancelled job: {fine_tuning_job.id}")
await self._cleanup_test_files_async(openai_client, train_file, validation_file, "sft_oss")
|