File size: 19,635 Bytes
476455e | 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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | # 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 json
import os
import boto3
import pytest
import tests.integ
from botocore.config import Config
from packaging.version import Version
from sagemaker import Session, image_uris, utils
from sagemaker.local import LocalSession
from sagemaker.workflow.pipeline_context import PipelineSession, LocalPipelineSession
DEFAULT_REGION = "us-west-2"
CUSTOM_BUCKET_NAME_PREFIX = "sagemaker-custom-bucket"
NO_M4_REGIONS = [
"eu-west-3",
"eu-north-1",
"ap-east-1",
"ap-northeast-1", # it has m4.xl, but not enough in all AZs
"sa-east-1",
"me-south-1",
]
NO_P3_REGIONS = [
"af-south-1",
"ap-east-1",
"ap-southeast-1", # it has p3, but not enough
"ap-southeast-2", # it has p3, but not enough
"ca-central-1", # it has p3, but not enough
"eu-central-1", # it has p3, but not enough
"eu-north-1",
"eu-west-1", # it has p3, but not enough
"eu-west-2", # it has p3, but not enough
"eu-west-3",
"eu-south-1",
"me-south-1",
"sa-east-1",
"us-west-1",
"ap-northeast-1", # it has p3, but not enough
"ap-south-1",
"ap-northeast-2", # it has p3, but not enough
"us-east-2", # it has p3, but not enough
]
NO_T2_REGIONS = ["eu-north-1", "ap-east-1", "me-south-1"]
FRAMEWORKS_FOR_GENERATED_VERSION_FIXTURES = (
"chainer",
"coach_mxnet",
"coach_tensorflow",
"inferentia_mxnet",
"inferentia_tensorflow",
"inferentia_pytorch",
"mxnet",
"neo_mxnet",
"neo_pytorch",
"neo_tensorflow",
"pytorch",
"ray_pytorch",
"ray_tensorflow",
"sklearn",
"tensorflow",
"vw",
"xgboost",
"spark",
"huggingface",
"autogluon",
"huggingface_training_compiler",
)
def pytest_addoption(parser):
parser.addoption("--sagemaker-client-config", action="store", default=None)
parser.addoption("--sagemaker-runtime-config", action="store", default=None)
parser.addoption("--boto-config", action="store", default=None)
def pytest_configure(config):
bc = config.getoption("--boto-config")
parsed = json.loads(bc) if bc else {}
region = parsed.get("region_name", boto3.session.Session().region_name)
if region:
os.environ["TEST_AWS_REGION_NAME"] = region
@pytest.fixture(scope="session")
def sagemaker_client_config(request):
config = request.config.getoption("--sagemaker-client-config")
return json.loads(config) if config else dict()
@pytest.fixture(scope="session")
def sagemaker_runtime_config(request):
config = request.config.getoption("--sagemaker-runtime-config")
return json.loads(config) if config else None
@pytest.fixture(scope="session")
def boto_session(request):
config = request.config.getoption("--boto-config")
if config:
return boto3.Session(**json.loads(config))
else:
return boto3.Session(region_name=DEFAULT_REGION)
@pytest.fixture(scope="session")
def account(boto_session):
return boto_session.client("sts").get_caller_identity()["Account"]
@pytest.fixture(scope="session")
def region(boto_session):
return boto_session.region_name
@pytest.fixture(scope="session")
def sagemaker_session(sagemaker_client_config, sagemaker_runtime_config, boto_session):
sagemaker_client_config.setdefault("config", Config(retries=dict(max_attempts=10)))
sagemaker_client = (
boto_session.client("sagemaker", **sagemaker_client_config)
if sagemaker_client_config
else None
)
runtime_client = (
boto_session.client("sagemaker-runtime", **sagemaker_runtime_config)
if sagemaker_runtime_config
else None
)
return Session(
boto_session=boto_session,
sagemaker_client=sagemaker_client,
sagemaker_runtime_client=runtime_client,
)
@pytest.fixture(scope="session")
def sagemaker_local_session(boto_session):
return LocalSession(boto_session=boto_session)
@pytest.fixture(scope="session")
def pipeline_session(boto_session):
return PipelineSession(boto_session=boto_session)
@pytest.fixture(scope="session")
def local_pipeline_session(boto_session):
return LocalPipelineSession(boto_session=boto_session)
@pytest.fixture(scope="module")
def custom_bucket_name(boto_session):
region = boto_session.region_name
account = boto_session.client(
"sts", region_name=region, endpoint_url=utils.sts_regional_endpoint(region)
).get_caller_identity()["Account"]
return "{}-{}-{}".format(CUSTOM_BUCKET_NAME_PREFIX, region, account)
@pytest.fixture(scope="module", params=["py2", "py3"])
def chainer_py_version(request):
return request.param
@pytest.fixture(scope="module", params=["py2", "py3"])
def mxnet_inference_py_version(mxnet_inference_version, request):
if Version(mxnet_inference_version) < Version("1.7.0"):
return request.param
elif Version(mxnet_inference_version) == Version("1.8.0"):
return "py37"
elif Version(mxnet_inference_version) == Version("1.9.0"):
return "py38"
else:
return "py3"
@pytest.fixture(scope="module", params=["py2", "py3"])
def mxnet_training_py_version(mxnet_training_version, request):
if Version(mxnet_training_version) < Version("1.7.0"):
return request.param
elif Version(mxnet_training_version) == Version("1.8.0"):
return "py37"
elif Version(mxnet_training_version) == Version("1.9.0"):
return "py38"
else:
return "py3"
@pytest.fixture(scope="module", params=["py2", "py3"])
def mxnet_eia_py_version(mxnet_eia_version, request):
if Version(mxnet_eia_version) < Version("1.7.0"):
return request.param
else:
return "py3"
@pytest.fixture(scope="module")
def mxnet_eia_latest_py_version():
return "py3"
@pytest.fixture(scope="module", params=["py2", "py3"])
def pytorch_training_py_version(pytorch_training_version, request):
if Version(pytorch_training_version) < Version("1.5.0"):
return request.param
elif Version(pytorch_training_version) >= Version("1.9"):
return "py38"
else:
return "py3"
@pytest.fixture(scope="module", params=["py2", "py3"])
def pytorch_inference_py_version(pytorch_inference_version, request):
if Version(pytorch_inference_version) < Version("1.4.0"):
return request.param
elif Version(pytorch_inference_version) >= Version("1.9"):
return "py38"
else:
return "py3"
@pytest.fixture(scope="module")
def huggingface_pytorch_training_version(huggingface_training_version):
return _huggingface_base_fm_version(
huggingface_training_version, "pytorch", "huggingface_training"
)[0]
@pytest.fixture(scope="module")
def huggingface_pytorch_training_py_version(huggingface_pytorch_training_version):
return "py38" if Version(huggingface_pytorch_training_version) >= Version("1.9") else "py36"
@pytest.fixture(scope="module")
def huggingface_training_compiler_pytorch_version(huggingface_training_compiler_version):
versions = _huggingface_base_fm_version(
huggingface_training_compiler_version, "pytorch", "huggingface_training_compiler"
)
if not versions:
pytest.skip(
f"Hugging Face Training Compiler version {huggingface_training_compiler_version} does "
f"not have a PyTorch release."
)
return versions[0]
@pytest.fixture(scope="module")
def huggingface_training_compiler_tensorflow_version(huggingface_training_compiler_version):
versions = _huggingface_base_fm_version(
huggingface_training_compiler_version, "tensorflow", "huggingface_training_compiler"
)
if not versions:
pytest.skip(
f"Hugging Face Training Compiler version {huggingface_training_compiler_version} "
f"does not have a TensorFlow release."
)
return versions[0]
@pytest.fixture(scope="module")
def huggingface_training_compiler_tensorflow_py_version(
huggingface_training_compiler_tensorflow_version,
):
return (
"py37"
if Version(huggingface_training_compiler_tensorflow_version) < Version("2.6")
else "py38"
)
@pytest.fixture(scope="module")
def huggingface_training_compiler_pytorch_py_version(huggingface_training_compiler_pytorch_version):
return "py38"
@pytest.fixture(scope="module")
def huggingface_pytorch_latest_training_py_version(huggingface_training_pytorch_latest_version):
return (
"py38" if Version(huggingface_training_pytorch_latest_version) >= Version("1.9") else "py36"
)
@pytest.fixture(scope="module")
def huggingface_pytorch_latest_inference_py_version(huggingface_inference_pytorch_latest_version):
return (
"py38"
if Version(huggingface_inference_pytorch_latest_version) >= Version("1.9")
else "py36"
)
@pytest.fixture(scope="module")
def huggingface_tensorflow_latest_training_py_version():
return "py38"
@pytest.fixture(scope="module")
def huggingface_neuron_latest_inference_pytorch_version():
return "1.9"
@pytest.fixture(scope="module")
def huggingface_neuron_latest_inference_transformer_version():
return "4.12"
@pytest.fixture(scope="module")
def huggingface_neuron_latest_inference_py_version():
return "py37"
@pytest.fixture(scope="module")
def pytorch_eia_py_version():
return "py3"
@pytest.fixture(scope="module")
def neo_pytorch_latest_py_version():
return "py3"
@pytest.fixture(scope="module")
def neo_pytorch_compilation_job_name():
return utils.name_from_base("pytorch-neo-model")
@pytest.fixture(scope="module")
def neo_pytorch_target_device():
return "ml_c5"
@pytest.fixture(scope="module")
def neo_pytorch_cpu_instance_type():
return "ml.c5.xlarge"
@pytest.fixture(scope="module")
def xgboost_framework_version(xgboost_version):
if xgboost_version in ("1", "latest"):
pytest.skip("Skipping XGBoost algorithm version.")
return xgboost_version
@pytest.fixture(scope="module")
def xgboost_gpu_framework_version(xgboost_version):
if xgboost_version in ("1", "latest"):
pytest.skip("Skipping XGBoost algorithm version.")
if Version(xgboost_version) < Version("1.2"):
pytest.skip("Skipping XGBoost cpu-only version.")
return xgboost_version
@pytest.fixture(scope="module", params=["py2", "py3"])
def tensorflow_training_py_version(tensorflow_training_version, request):
return _tf_py_version(tensorflow_training_version, request)
@pytest.fixture(scope="module", params=["py2", "py3"])
def tensorflow_inference_py_version(tensorflow_inference_version, request):
version = Version(tensorflow_inference_version)
if version == Version("1.15") or Version("1.15.4") <= version < Version("1.16"):
return "py36"
return _tf_py_version(tensorflow_inference_version, request)
def _tf_py_version(tf_version, request):
version = Version(tf_version)
if version == Version("1.15") or Version("1.15.4") <= version < Version("1.16"):
return "py3"
if version < Version("1.11"):
return "py2"
if version == Version("2.0") or Version("2.0.3") <= version < Version("2.1"):
return "py3"
if version == Version("2.1") or Version("2.1.2") <= version < Version("2.2"):
return "py3"
if version < Version("2.2"):
return request.param
if Version("2.2") <= version < Version("2.6"):
return "py37"
if Version("2.6") <= version < Version("2.8"):
return "py38"
return "py39"
@pytest.fixture(scope="module")
def tf_full_version(tensorflow_training_latest_version, tensorflow_inference_latest_version):
"""Fixture for TF tests that test both training and inference.
Fixture exists as such, since TF training and TFS have different latest versions.
Otherwise, this would simply be a single latest version.
"""
return str(
min(
Version(tensorflow_training_latest_version),
Version(tensorflow_inference_latest_version),
)
)
@pytest.fixture(scope="module")
def tf_full_py_version(tf_full_version):
"""Fixture to match tf_full_version
Fixture exists as such, since TF training and TFS have different latest versions.
Otherwise, this would simply be py37 to match the latest version support.
"""
version = Version(tf_full_version)
if version < Version("1.11"):
return "py2"
if version < Version("2.2"):
return "py3"
if version < Version("2.6"):
return "py37"
if version < Version("2.8"):
return "py38"
return "py39"
@pytest.fixture(scope="module")
def pytorch_ddp_py_version():
return "py3"
@pytest.fixture(
scope="module", params=["1.10", "1.10.0", "1.10.2", "1.11", "1.11.0", "1.12", "1.12.0"]
)
def pytorch_ddp_framework_version(request):
return request.param
@pytest.fixture(scope="module")
def torch_distributed_py_version():
return "py3"
@pytest.fixture(scope="module", params=["1.11.0"])
def torch_distributed_framework_version(request):
return request.param
@pytest.fixture(scope="session")
def cpu_instance_type(sagemaker_session, request):
region = sagemaker_session.boto_session.region_name
if region in NO_M4_REGIONS:
return "ml.m5.xlarge"
else:
return "ml.m4.xlarge"
@pytest.fixture(scope="session")
def gpu_instance_type(sagemaker_session, request):
region = sagemaker_session.boto_session.region_name
if region in NO_P3_REGIONS:
return "ml.p2.xlarge"
else:
return "ml.p3.2xlarge"
@pytest.fixture(scope="session")
def gpu_instance_type_list(sagemaker_session, request):
region = sagemaker_session.boto_session.region_name
if region in NO_P3_REGIONS:
return ["ml.p2.xlarge"]
else:
return ["ml.p3.2xlarge", "ml.p2.xlarge"]
@pytest.fixture(scope="session")
def inf_instance_type(sagemaker_session, request):
return "ml.inf1.xlarge"
@pytest.fixture(scope="session")
def ec2_instance_type(cpu_instance_type):
return cpu_instance_type[3:]
@pytest.fixture(scope="session")
def alternative_cpu_instance_type(sagemaker_session, request):
region = sagemaker_session.boto_session.region_name
if region in NO_T2_REGIONS:
# T3 is not supported by hosting yet
return "ml.c5.xlarge"
else:
return "ml.t2.medium"
@pytest.fixture(scope="session")
def cpu_instance_family(cpu_instance_type):
return "_".join(cpu_instance_type.split(".")[0:2])
@pytest.fixture(scope="session")
def inf_instance_family(inf_instance_type):
return "_".join(inf_instance_type.split(".")[0:2])
def pytest_generate_tests(metafunc):
if "instance_type" in metafunc.fixturenames:
boto_config = metafunc.config.getoption("--boto-config")
parsed_config = json.loads(boto_config) if boto_config else {}
region = parsed_config.get("region_name", DEFAULT_REGION)
cpu_instance_type = "ml.m5.xlarge" if region in NO_M4_REGIONS else "ml.m4.xlarge"
params = [cpu_instance_type]
if not (
region in tests.integ.HOSTING_NO_P3_REGIONS
or region in tests.integ.TRAINING_NO_P3_REGIONS
):
params.append("ml.p3.2xlarge")
elif not (
region in tests.integ.HOSTING_NO_P2_REGIONS
or region in tests.integ.TRAINING_NO_P2_REGIONS
):
params.append("ml.p2.xlarge")
metafunc.parametrize("instance_type", params, scope="session")
_generate_all_framework_version_fixtures(metafunc)
def _generate_all_framework_version_fixtures(metafunc):
for fw in FRAMEWORKS_FOR_GENERATED_VERSION_FIXTURES:
config = image_uris.config_for_framework(fw.replace("_", "-"))
if "scope" in config:
_parametrize_framework_version_fixtures(metafunc, fw, config)
else:
for image_scope in config.keys():
fixture_prefix = f"{fw}_{image_scope}" if image_scope not in fw else fw
_parametrize_framework_version_fixtures(
metafunc, fixture_prefix, config[image_scope]
)
def _huggingface_base_fm_version(huggingface_version, base_fw, fixture_prefix):
config_name = (
"huggingface-training-compiler" if "training_compiler" in fixture_prefix else "huggingface"
)
config = image_uris.config_for_framework(config_name)
if "training" in fixture_prefix:
hf_config = config.get("training")
else:
hf_config = config.get("inference")
original_version = huggingface_version
if "version_aliases" in hf_config:
huggingface_version = hf_config.get("version_aliases").get(
huggingface_version, huggingface_version
)
version_config = hf_config.get("versions").get(huggingface_version)
versions = list()
for key in list(version_config.keys()):
if key.startswith(base_fw):
base_fw_version = key[len(base_fw) :]
if len(original_version.split(".")) == 2:
base_fw_version = ".".join(base_fw_version.split(".")[:-1])
versions.append(base_fw_version)
return sorted(versions, reverse=True)
def _generate_huggingface_base_fw_latest_versions(
metafunc, fixture_prefix, huggingface_version, base_fw
):
versions = _huggingface_base_fm_version(huggingface_version, base_fw, fixture_prefix)
fixture_name = f"{fixture_prefix}_{base_fw}_latest_version"
if fixture_name in metafunc.fixturenames:
metafunc.parametrize(fixture_name, versions, scope="session")
def _parametrize_framework_version_fixtures(metafunc, fixture_prefix, config):
fixture_name = "{}_version".format(fixture_prefix)
if fixture_name in metafunc.fixturenames:
versions = list(config["versions"].keys()) + list(config.get("version_aliases", {}).keys())
metafunc.parametrize(fixture_name, versions, scope="session")
latest_version = sorted(config["versions"].keys(), key=lambda v: Version(v))[-1]
fixture_name = "{}_latest_version".format(fixture_prefix)
if fixture_name in metafunc.fixturenames:
metafunc.parametrize(fixture_name, (latest_version,), scope="session")
if "huggingface" in fixture_prefix:
_generate_huggingface_base_fw_latest_versions(
metafunc, fixture_prefix, latest_version, "pytorch"
)
_generate_huggingface_base_fw_latest_versions(
metafunc, fixture_prefix, latest_version, "tensorflow"
)
fixture_name = "{}_latest_py_version".format(fixture_prefix)
if fixture_name in metafunc.fixturenames:
config = config["versions"]
py_versions = config[latest_version].get("py_versions", config[latest_version].keys())
if "repository" in py_versions or "registries" in py_versions:
# Config did not specify `py_versions` and is not arranged by py_version. Assume py3
metafunc.parametrize(fixture_name, ("py3",), scope="session")
else:
metafunc.parametrize(fixture_name, (sorted(py_versions)[-1],), scope="session")
|