forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dpo_trainer_integration.py
More file actions
102 lines (82 loc) · 3.81 KB
/
Copy pathtest_dpo_trainer_integration.py
File metadata and controls
102 lines (82 loc) · 3.81 KB
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Integration tests for DPO trainer"""
from __future__ import absolute_import
import time
import random
import boto3
from sagemaker.core.helper.session_helper import Session
from sagemaker.train.dpo_trainer import DPOTrainer
from sagemaker.train.common import TrainingType
import pytest
def test_dpo_trainer_lora_complete_workflow(sagemaker_session):
"""Test complete DPO training workflow with LORA."""
unique_id = f"{int(time.time())}-{random.randint(1000, 9999)}"
# Create DPOTrainer instance with comprehensive configuration
trainer = DPOTrainer(
model="meta-textgeneration-llama-3-2-1b-instruct",
training_type=TrainingType.LORA,
model_package_group="sdk-test-finetuned-models",
training_dataset="s3://mc-flows-sdk-testing/input_data/dpo/preference_dataset_train_256.jsonl",
s3_output_path="s3://mc-flows-sdk-testing/output/",
accept_eula=True,
base_job_name=f"dpo-lora-integ-{unique_id}",
)
# Customize hyperparameters for quick training
trainer.hyperparameters.max_epochs = 1
# Create training job
training_job = trainer.train(wait=False)
# Manual wait loop to avoid resource_config issue
max_wait_time = 3600 # 1 hour timeout
poll_interval = 30 # Check every 30 seconds
start_time = time.time()
while time.time() - start_time < max_wait_time:
training_job.refresh()
status = training_job.training_job_status
if status in ["Completed", "Failed", "Stopped"]:
break
time.sleep(poll_interval)
# Verify job completed successfully
assert training_job.training_job_status == "Completed"
assert hasattr(training_job, 'output_model_package_arn')
assert training_job.output_model_package_arn is not None
def test_dpo_trainer_with_validation_dataset(sagemaker_session):
"""Test DPO trainer with both training and validation datasets."""
unique_id = f"{int(time.time())}-{random.randint(1000, 9999)}"
dpo_trainer = DPOTrainer(
model="meta-textgeneration-llama-3-2-1b-instruct",
training_type=TrainingType.LORA,
model_package_group="sdk-test-finetuned-models",
training_dataset="s3://mc-flows-sdk-testing/input_data/dpo/preference_dataset_train_256.jsonl",
validation_dataset="s3://mc-flows-sdk-testing/input_data/dpo/preference_dataset_train_256.jsonl",
s3_output_path="s3://mc-flows-sdk-testing/output/",
accept_eula=True,
base_job_name=f"dpo-val-integ-{unique_id}",
)
# Customize hyperparameters for quick training
dpo_trainer.hyperparameters.max_epochs = 1
training_job = dpo_trainer.train(wait=False)
# Manual wait loop
max_wait_time = 3600
poll_interval = 30
start_time = time.time()
while time.time() - start_time < max_wait_time:
training_job.refresh()
status = training_job.training_job_status
if status in ["Completed", "Failed", "Stopped"]:
break
time.sleep(poll_interval)
# Verify job completed successfully
assert training_job.training_job_status == "Completed"
assert hasattr(training_job, 'output_model_package_arn')
assert training_job.output_model_package_arn is not None