forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_xgboost.py
More file actions
176 lines (152 loc) · 5.9 KB
/
test_xgboost.py
File metadata and controls
176 lines (152 loc) · 5.9 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
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
# 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.
from __future__ import absolute_import
import os
import pytest
from sagemaker.serverless import ServerlessInferenceConfig
from sagemaker.utils import unique_name_from_base
from sagemaker.xgboost import XGBoost, XGBoostModel
from sagemaker.xgboost.processing import XGBoostProcessor
from tests.integ import DATA_DIR, TRAINING_DEFAULT_TIMEOUT_MINUTES
from tests.integ.timeout import timeout, timeout_and_delete_endpoint_by_name
ROLE = "SageMakerRole"
@pytest.fixture(scope="module")
def xgboost_training_job(
sagemaker_session,
xgboost_latest_version,
xgboost_latest_py_version,
cpu_instance_type,
):
return _run_mnist_training_job(
sagemaker_session,
cpu_instance_type,
xgboost_latest_version,
xgboost_latest_py_version,
)
def test_sourcedir_naming(
sagemaker_session,
xgboost_latest_version,
xgboost_latest_py_version,
cpu_instance_type,
):
with pytest.raises(RuntimeError):
processor = XGBoostProcessor(
framework_version=xgboost_latest_version,
role=ROLE,
instance_count=1,
instance_type=cpu_instance_type,
sagemaker_session=sagemaker_session,
)
processor.run(
source_dir="s3://bucket/deps.tar.gz",
code="main_script.py",
)
@pytest.mark.release
def test_framework_processing_job_with_deps(
sagemaker_session,
xgboost_latest_version,
xgboost_latest_py_version,
cpu_instance_type,
):
with timeout(minutes=TRAINING_DEFAULT_TIMEOUT_MINUTES):
code_path = os.path.join(DATA_DIR, "dummy_code_bundle_with_reqs")
entry_point = "main_script.py"
processor = XGBoostProcessor(
framework_version=xgboost_latest_version,
py_version=xgboost_latest_py_version,
role=ROLE,
instance_count=1,
instance_type=cpu_instance_type,
sagemaker_session=sagemaker_session,
base_job_name="test-xgboost",
)
processor.run(
code=entry_point,
source_dir=code_path,
inputs=[],
wait=True,
)
def test_training_with_network_isolation(
sagemaker_session,
xgboost_latest_version,
xgboost_latest_py_version,
cpu_instance_type,
):
with timeout(minutes=TRAINING_DEFAULT_TIMEOUT_MINUTES):
base_job_name = "test-network-isolation-xgboost"
xgboost = XGBoost(
entry_point=os.path.join(DATA_DIR, "xgboost_abalone", "abalone.py"),
role=ROLE,
instance_type=cpu_instance_type,
instance_count=1,
framework_version=xgboost_latest_version,
py_version=xgboost_latest_py_version,
base_job_name=base_job_name,
sagemaker_session=sagemaker_session,
enable_network_isolation=True,
)
train_input = xgboost.sagemaker_session.upload_data(
path=os.path.join(DATA_DIR, "xgboost_abalone", "abalone"),
key_prefix="integ-test-data/xgboost_abalone/abalone",
)
job_name = unique_name_from_base(base_job_name)
xgboost.fit(inputs={"train": train_input}, job_name=job_name)
assert sagemaker_session.sagemaker_client.describe_training_job(TrainingJobName=job_name)[
"EnableNetworkIsolation"
]
def test_xgboost_serverless_inference(
xgboost_training_job,
sagemaker_session,
):
endpoint_name = unique_name_from_base("test-xgboost-deploy-model-serverless")
with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session):
desc = sagemaker_session.sagemaker_client.describe_training_job(
TrainingJobName=xgboost_training_job
)
model_data = desc["ModelArtifacts"]["S3ModelArtifacts"]
xgboost = XGBoostModel(
sagemaker_session=sagemaker_session,
model_data=model_data,
role=ROLE,
entry_point=os.path.join(DATA_DIR, "xgboost_abalone", "abalone.py"),
# XGBoost changed its default model format:
# - Old XGBoost (< 2.0): Default save format was binary
# - New XGBoost (>= 2.0): Default save format is JSON/UBJSON
framework_version="3.0-5",
)
xgboost.deploy(
serverless_inference_config=ServerlessInferenceConfig(), endpoint_name=endpoint_name
)
def _run_mnist_training_job(
sagemaker_session, cpu_instance_type, xgboost_latest_version, xgboost_latest_py_version
):
with timeout(minutes=TRAINING_DEFAULT_TIMEOUT_MINUTES):
base_job_name = "test-xgboost-mnist"
xgboost = XGBoost(
entry_point=os.path.join(DATA_DIR, "xgboost_abalone", "abalone.py"),
role=ROLE,
instance_type=cpu_instance_type,
instance_count=1,
framework_version=xgboost_latest_version,
py_version=xgboost_latest_py_version,
base_job_name=base_job_name,
sagemaker_session=sagemaker_session,
enable_network_isolation=True,
)
train_input = xgboost.sagemaker_session.upload_data(
path=os.path.join(DATA_DIR, "xgboost_abalone", "abalone"),
key_prefix="integ-test-data/xgboost_abalone/abalone",
)
job_name = unique_name_from_base(base_job_name)
xgboost.fit(inputs={"train": train_input}, job_name=job_name)
return xgboost.latest_training_job.name