|
73 | 73 | Channel, |
74 | 74 | DataSource, |
75 | 75 | MetricDefinition, |
| 76 | + InstanceGroup, |
76 | 77 | ) |
77 | 78 | from sagemaker.train.distributed import Torchrun, SMP, MPI |
78 | 79 | from sagemaker.train.sm_recipes.utils import _load_recipes_cfg, _is_nova_recipe, _get_args_from_nova_recipe |
@@ -472,6 +473,153 @@ def test_create_input_data_channel(mock_default_bucket, mock_upload_data, model_ |
472 | 473 | assert channel.data_source.s3_data_source.s3_uri == expected_s3_uri |
473 | 474 |
|
474 | 475 |
|
| 476 | +def test_create_input_data_channel_with_instance_group_names(model_trainer): |
| 477 | + """instance_group_names is propagated onto the channel's S3DataSource.""" |
| 478 | + channel = model_trainer.create_input_data_channel( |
| 479 | + channel_name="code", |
| 480 | + data_source=f"s3://{DEFAULT_BUCKET}/{DEFAULT_BASE_NAME}-job/input/code", |
| 481 | + instance_group_names=["head-instance-group", "worker-instance-group-1"], |
| 482 | + ) |
| 483 | + assert channel.data_source.s3_data_source.instance_group_names == [ |
| 484 | + "head-instance-group", |
| 485 | + "worker-instance-group-1", |
| 486 | + ] |
| 487 | + |
| 488 | + |
| 489 | +HETEROGENEOUS_INSTANCE_GROUPS = [ |
| 490 | + InstanceGroup( |
| 491 | + instance_type="ml.t3.large", instance_count=1, instance_group_name="head-instance-group" |
| 492 | + ), |
| 493 | + InstanceGroup( |
| 494 | + instance_type="ml.m5.2xlarge", |
| 495 | + instance_count=2, |
| 496 | + instance_group_name="worker-instance-group-1", |
| 497 | + ), |
| 498 | +] |
| 499 | + |
| 500 | + |
| 501 | +def _instance_group_names(channel): |
| 502 | + """Return the channel's assigned instance_group_names as a list (or None).""" |
| 503 | + s3_data_source = channel.data_source.s3_data_source if channel.data_source else None |
| 504 | + names = getattr(s3_data_source, "instance_group_names", None) if s3_data_source else None |
| 505 | + return names if isinstance(names, list) else None |
| 506 | + |
| 507 | + |
| 508 | +def _managed_channel_names(input_data_config): |
| 509 | + return { |
| 510 | + channel.channel_name: _instance_group_names(channel) for channel in input_data_config |
| 511 | + } |
| 512 | + |
| 513 | + |
| 514 | +@patch("sagemaker.train.model_trainer.Session.upload_data") |
| 515 | +@patch("sagemaker.train.model_trainer.Session.default_bucket") |
| 516 | +def test_managed_channels_assigned_instance_groups_when_user_channel_assigns( |
| 517 | + mock_default_bucket, mock_upload_data |
| 518 | +): |
| 519 | + """Regression test for issue #6089. |
| 520 | +
|
| 521 | + On a heterogeneous cluster, when a user assigns instance_group_names to any of their |
| 522 | + channels, the SDK-managed ``code``/``sm_drivers`` channels must also be assigned to the |
| 523 | + full set of instance groups, otherwise CreateTrainingJob fails validation with |
| 524 | + "Some channels have assigned instance groups ... while others not". |
| 525 | + """ |
| 526 | + mock_upload_data.return_value = f"s3://{DEFAULT_BUCKET}/{DEFAULT_BASE_NAME}-job/input/code" |
| 527 | + mock_default_bucket.return_value = DEFAULT_BUCKET |
| 528 | + expected_names = ["head-instance-group", "worker-instance-group-1"] |
| 529 | + |
| 530 | + trainer = ModelTrainer( |
| 531 | + training_image=DEFAULT_IMAGE, |
| 532 | + role=DEFAULT_ROLE, |
| 533 | + source_code=DEFAULT_SOURCE_CODE, |
| 534 | + compute=Compute(instance_groups=HETEROGENEOUS_INSTANCE_GROUPS), |
| 535 | + stopping_condition=DEFAULT_STOPPING_CONDITION, |
| 536 | + output_data_config=DEFAULT_OUTPUT_DATA_CONFIG, |
| 537 | + ) |
| 538 | + |
| 539 | + user_channel = InputData( |
| 540 | + channel_name="processing", |
| 541 | + data_source=S3DataSource( |
| 542 | + s3_data_type="S3Prefix", |
| 543 | + s3_uri=f"s3://{DEFAULT_BUCKET}/data/", |
| 544 | + s3_data_distribution_type="FullyReplicated", |
| 545 | + instance_group_names=expected_names, |
| 546 | + ), |
| 547 | + ) |
| 548 | + |
| 549 | + args = trainer._create_training_job_args(input_data_config=[user_channel]) |
| 550 | + channels = _managed_channel_names(args["input_data_config"]) |
| 551 | + |
| 552 | + assert channels["processing"] == expected_names |
| 553 | + assert channels["code"] == expected_names |
| 554 | + assert channels["sm_drivers"] == expected_names |
| 555 | + |
| 556 | + |
| 557 | +@patch("sagemaker.train.model_trainer.Session.upload_data") |
| 558 | +@patch("sagemaker.train.model_trainer.Session.default_bucket") |
| 559 | +def test_managed_channels_not_assigned_when_user_channel_unassigned( |
| 560 | + mock_default_bucket, mock_upload_data |
| 561 | +): |
| 562 | + """Managed channels stay unassigned when the user does not use instance groups. |
| 563 | +
|
| 564 | + Even on a heterogeneous cluster, if no user channel assigns instance_group_names, the |
| 565 | + SDK must not assign them to managed channels (which would itself violate the |
| 566 | + all-or-nothing rule against the unassigned user channel). |
| 567 | + """ |
| 568 | + mock_upload_data.return_value = f"s3://{DEFAULT_BUCKET}/{DEFAULT_BASE_NAME}-job/input/code" |
| 569 | + mock_default_bucket.return_value = DEFAULT_BUCKET |
| 570 | + |
| 571 | + trainer = ModelTrainer( |
| 572 | + training_image=DEFAULT_IMAGE, |
| 573 | + role=DEFAULT_ROLE, |
| 574 | + source_code=DEFAULT_SOURCE_CODE, |
| 575 | + compute=Compute(instance_groups=HETEROGENEOUS_INSTANCE_GROUPS), |
| 576 | + stopping_condition=DEFAULT_STOPPING_CONDITION, |
| 577 | + output_data_config=DEFAULT_OUTPUT_DATA_CONFIG, |
| 578 | + ) |
| 579 | + |
| 580 | + user_channel = InputData( |
| 581 | + channel_name="processing", |
| 582 | + data_source=S3DataSource( |
| 583 | + s3_data_type="S3Prefix", |
| 584 | + s3_uri=f"s3://{DEFAULT_BUCKET}/data/", |
| 585 | + s3_data_distribution_type="FullyReplicated", |
| 586 | + ), |
| 587 | + ) |
| 588 | + |
| 589 | + args = trainer._create_training_job_args(input_data_config=[user_channel]) |
| 590 | + channels = _managed_channel_names(args["input_data_config"]) |
| 591 | + |
| 592 | + assert channels["code"] is None |
| 593 | + assert channels["sm_drivers"] is None |
| 594 | + |
| 595 | + |
| 596 | +@patch("sagemaker.train.model_trainer.Session.upload_data") |
| 597 | +@patch("sagemaker.train.model_trainer.Session.default_bucket") |
| 598 | +def test_managed_channels_not_assigned_on_homogeneous_cluster( |
| 599 | + mock_default_bucket, mock_upload_data |
| 600 | +): |
| 601 | + """No instance groups configured -> managed channels are never assigned.""" |
| 602 | + mock_upload_data.return_value = f"s3://{DEFAULT_BUCKET}/{DEFAULT_BASE_NAME}-job/input/code" |
| 603 | + mock_default_bucket.return_value = DEFAULT_BUCKET |
| 604 | + |
| 605 | + trainer = ModelTrainer( |
| 606 | + training_image=DEFAULT_IMAGE, |
| 607 | + role=DEFAULT_ROLE, |
| 608 | + source_code=DEFAULT_SOURCE_CODE, |
| 609 | + compute=DEFAULT_COMPUTE_CONFIG, |
| 610 | + stopping_condition=DEFAULT_STOPPING_CONDITION, |
| 611 | + output_data_config=DEFAULT_OUTPUT_DATA_CONFIG, |
| 612 | + ) |
| 613 | + |
| 614 | + user_channel = InputData(channel_name="train", data_source=f"s3://{DEFAULT_BUCKET}/train/") |
| 615 | + |
| 616 | + args = trainer._create_training_job_args(input_data_config=[user_channel]) |
| 617 | + channels = _managed_channel_names(args["input_data_config"]) |
| 618 | + |
| 619 | + assert channels["code"] is None |
| 620 | + assert channels["sm_drivers"] is None |
| 621 | + |
| 622 | + |
475 | 623 | @pytest.mark.parametrize( |
476 | 624 | "test_case", |
477 | 625 | [ |
|
0 commit comments