Skip to content

feat: Make DistributedConfig Extensible #5039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 5, 2025

Conversation

benieric
Copy link
Collaborator

@benieric benieric commented Feb 14, 2025

Issue #, if available:

Description of changes:

  • This change is being introduced to update the DistributedConfig class to be more extensible

Base Class Update:

class DistributedConfig(BaseModel, ABC):
    @property
    @abstractmethod
    def driver_dir(self) -> str:
        pass
    
    @property
    @abstractmethod
    def driver_script(self) -> str:
        pass

Customer Usage Example:

# Create Subclass
class CustomDriver(DistributedConfig):
    process_count_per_node: Optional[int] = None
	driver_args: Optional[List[str]] = None
    
    @property
    def driver_dir(self) -> str:
        return "drivers" 
    
    @property
    def driver_script(self) -> str:
        return "custom_driver.py"
        

# Instantiate CustomDriver
custom_driver = CustomDriver(
    process_count_per_node=2
)

source_code = SourceCode(
    source_dir="code",
    entry_script="train.py"
)

model_trainer = ModelTrainer(
    source_code=source_code,
    distributed=custom_driver
)

How to create acustom_driver.py:

import json
import os
import subprocess


def main():
    # Referencing custom driver config properties
    driver_config = json.loads(os.environ["SM_DISTRIBUTED_CONFIG"])
	process_count_per_node = driver_config["process_count_per_node"]
    
    # Because subclassing allows for user to setup custom properties
    # they can configure their driver config to accept a list of driver args
    # which would be seperate from the hyperparameters
    driver_args = driver_config["driver_args"]
    
    
    # Referencing hyperparamter
    hps = json.loads(os.enviorn["SM_HPS"])
    
    # Referencing their entry script defined in SourceCode(entry_script=...)
    entry_script = os.enviorn["SM_ENTRY_SCRIPT"]
    
    # Utilize existing env vars for other parameters
    host_count = os.enviorn["SM_HOST_COUNT"]
	master_addr = os.enviorn["SM_MASTER_ADDR"]
	master_port = os.enviorn["SM_MASTER_PORT"]
	current_node_rank = os.environ["SM_CURRENT_HOST_RANK"]
    
    # Reference path to code defined under SourceCode(source_dir=...)
    source_dir = os.enviorn["SM_SOURCE_DIR"]
    
    # Reference path to code defined under CustomDriver(driver_dir=...)
    driver_dir = os.enviorn["SM_DISTRIBUTED_DRIVER_DIR"]
    
    command = [
        "deepspeed",
        f"--num_nodes={host_count}"
        f"--nproc_per_node={process_count_per_node}",
        driver_args,
        entry_script,
    ]
 
    subprocess.run(command, check=True)
 
if __name__ == "__main__":
    main()

What changes in the container?

In order to prevent conflicts with user provided code in the driver_dir and sdk scripts, the directory structure inside the container will change to be like below.

Before:

/opt/ml/input/data
                |--code/ # path for code defined in SourceCode(source_dir=...)
                    
                |--sm_drivers/ # path to all sagemaker drivers, and extra code that sdk packages
                    |--scripts/
                        |-- enviornment.py 
                    |-- utility.py
                    |-- mpi_driver.py
                    |-- torchrun_driver.py
                    |-- basic_script_driver.py

After:

/opt/ml/input/data
                |--code/ # path for code defined in SourceCode(source_dir=...)
                
                |--sm_drivers/
                    |--scripts/
                            |-- enviornment.py 
                    |--distributed_drivers/ # path to code defined in DistributedConfig(driver_dir=...)
                            |-- mpi_driver.py
                            |-- torchrun_driver.py
                            |-- basic_script_driver.py
                    |--common/
                       	|-- utility.py

What env vars are being added?

  • os.environ["SM_ENTRY_SCRIPT"] - points to entry script
  • os.environ["SM_DISTRIBUTED_CONFIG"] - points to a json dump of the DistributedConfig class
  • os.environ["SM_SOURCE_DIR"] - points to the "/opt/ml/input/data/code"
  • os.environ["SM_DISTRIBUTED_DRIVER_DIR"] - points to "/opt/ml/input/data/sm_drivers/distributed_drivers"

Testing done:

  • Tested changes are not breaking existing integs, new integs in progress

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

  • I have read the CONTRIBUTING doc
  • I certify that the changes I am introducing will be backward compatible, and I have discussed concerns about this, if any, with the Python SDK team
  • I used the commit message format described in CONTRIBUTING
  • I have passed the region in to all S3 and STS clients that I've initialized as part of this change.
  • I have updated any necessary documentation, including READMEs and API docs (if appropriate)

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added unit and/or integration tests as appropriate to ensure backward compatibility of the changes
  • I have checked that my tests are not configured for a specific region or account (if appropriate)
  • I have used unique_name_from_base to create resource names in integ tests (if appropriate)
  • If adding any dependency in requirements.txt files, I have spell checked and ensured they exist in PyPi

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@benieric benieric force-pushed the master-distributed-config-extensible branch from 5954ecb to d2448b3 Compare February 14, 2025 00:55
@benieric benieric force-pushed the master-distributed-config-extensible branch from da5c441 to 9b8bf0b Compare February 14, 2025 02:51
@benieric benieric force-pushed the master-distributed-config-extensible branch from 7846a60 to a1ea9e7 Compare February 14, 2025 20:13
@benieric benieric force-pushed the master-distributed-config-extensible branch from a1ea9e7 to 5dafe09 Compare February 14, 2025 21:56
@benieric benieric marked this pull request as ready for review February 24, 2025 21:01
@benieric benieric requested a review from a team as a code owner February 24, 2025 21:01
@benieric benieric requested a review from liujiaorr February 24, 2025 21:01
@benieric benieric force-pushed the master-distributed-config-extensible branch from c81f8da to c5b1e12 Compare March 5, 2025 02:41
@mufaddal-rohawala
Copy link
Member

mufaddal-rohawala commented Mar 5, 2025

Can we change the package path for drivers to "sm_drivers/distributed_drivers/" (# path to code defined in DistributedConfig(driver_dir=...)) for clarity.

Also probably - SM_DRIVER_DIR -> SM_DISTRIBUTED_DRIVER_DIR

@benieric benieric force-pushed the master-distributed-config-extensible branch from 34d2bff to 50b373c Compare March 5, 2025 04:59
@benieric benieric force-pushed the master-distributed-config-extensible branch from 50b373c to 202e559 Compare March 5, 2025 05:45
@benieric benieric force-pushed the master-distributed-config-extensible branch from 202e559 to 591bca8 Compare March 5, 2025 05:53
@benieric benieric force-pushed the master-distributed-config-extensible branch from 591bca8 to 566ce38 Compare March 5, 2025 06:14
@benieric benieric force-pushed the master-distributed-config-extensible branch from 566ce38 to b562d69 Compare March 5, 2025 06:52
@benieric benieric merged commit fd45957 into aws:master Mar 5, 2025
14 checks passed
mollyheamazon pushed a commit to mollyheamazon/sagemaker-python-sdk that referenced this pull request Mar 14, 2025
* feat: Make DistributedConfig Extensible

* pylint

* Include none types when creating config jsons for safer reference

* fix: update test to account for changes

* format

* Add integ test

* pylint

* prepare release v2.240.0

* update development version to v2.240.1.dev0

* Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

* fix: Added check for the presence of model package group before creating one (aws#5063)

Co-authored-by: Keshav Chandak <[email protected]>

* Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

* remove union

* fix merge artifact

* Change dir path to distributed_drivers

* update paths

---------

Co-authored-by: ci <ci>
Co-authored-by: pintaoz-aws <[email protected]>
Co-authored-by: pintaoz <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
evakravi pushed a commit to evakravi/sagemaker-python-sdk that referenced this pull request Mar 20, 2025
* feat: Make DistributedConfig Extensible

* pylint

* Include none types when creating config jsons for safer reference

* fix: update test to account for changes

* format

* Add integ test

* pylint

* prepare release v2.240.0

* update development version to v2.240.1.dev0

* Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

* fix: Added check for the presence of model package group before creating one (aws#5063)

Co-authored-by: Keshav Chandak <[email protected]>

* Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

* remove union

* fix merge artifact

* Change dir path to distributed_drivers

* update paths

---------

Co-authored-by: ci <ci>
Co-authored-by: pintaoz-aws <[email protected]>
Co-authored-by: pintaoz <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
mollyheamazon pushed a commit to mollyheamazon/sagemaker-python-sdk that referenced this pull request Apr 8, 2025
* feat: Make DistributedConfig Extensible

* pylint

* Include none types when creating config jsons for safer reference

* fix: update test to account for changes

* format

* Add integ test

* pylint

* prepare release v2.240.0

* update development version to v2.240.1.dev0

* Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

* fix: Added check for the presence of model package group before creating one (aws#5063)

Co-authored-by: Keshav Chandak <[email protected]>

* Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

* remove union

* fix merge artifact

* Change dir path to distributed_drivers

* update paths

---------

Co-authored-by: ci <ci>
Co-authored-by: pintaoz-aws <[email protected]>
Co-authored-by: pintaoz <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
mollyheamazon added a commit to mollyheamazon/sagemaker-python-sdk that referenced this pull request Apr 8, 2025
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744156034 -0700

parent fb22b91
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744155953 -0700

parent fb22b91
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744155703 -0700

parent fb22b91
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744155683 -0700

Test py312 ci support

Test py312 ci support

first commit

test to point to personal stack

changed tox.ini

Revert "changed tox.ini"

This reverts commit 696cb47.

Revert "Revert "changed tox.ini""

This reverts commit 5d35bd0.

Revert "Revert "Revert "changed tox.ini"""

This reverts commit 2ab95fb.

Revert "Revert "changed tox.ini""

This reverts commit 5d35bd0.

Revert "changed tox.ini"

This reverts commit 696cb47.

Revert "test to point to personal stack"

This reverts commit 49de844.

Revert "first commit"

This reverts commit 1374184.

add pyproject.toml

add py312 to ci

bump numpy version

numpy version change

add py312

upgrade pip version

add setuptools wheel to tox.ini

add pyyaml version constraint, remove py312 from docstring because there is no py312 image yet

update pyyaml version constraint

update pyyaml version constraint

deprecate py38

bump scipy

bump tensorflow and tensorboard

bump dill

bump apache-airflow to ensure dill and greenlet version

remove constraint for apache-airflow

remove constraint for apache-airflow

bump torch version

bump torchvision version

new tests

try changing some tests

update model trainer test

fix test_pipeline

add constraint to apache in tox

Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

Fix error when there is no session to call _create_model_request() (aws#5062)

* Fix error when there is no session to call _create_model_request()

* Fix codestyle

---------

Co-authored-by: pintaoz <[email protected]>

Ensure Model.is_repack() returns a boolean (aws#5060)

* Ensure Model.is_repack() returns a boolean

* update test

---------

Co-authored-by: pintaoz <[email protected]>

feat: Make DistributedConfig Extensible (aws#5039)

* feat: Make DistributedConfig Extensible

* pylint

* Include none types when creating config jsons for safer reference

* fix: update test to account for changes

* format

* Add integ test

* pylint

* prepare release v2.240.0

* update development version to v2.240.1.dev0

* Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

* fix: Added check for the presence of model package group before creating one (aws#5063)

Co-authored-by: Keshav Chandak <[email protected]>

* Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

* remove union

* fix merge artifact

* Change dir path to distributed_drivers

* update paths

---------

Co-authored-by: ci <ci>
Co-authored-by: pintaoz-aws <[email protected]>
Co-authored-by: pintaoz <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>

Skip tests with deprecated instance type (aws#5077)

Co-authored-by: pintaoz <[email protected]>

pipeline definition function doc update (aws#5074)

Co-authored-by: Rohan Gujarathi <[email protected]>

feat: add integ tests for training JumpStart models in private hub (aws#5076)

* feat: add integ tests for training JumpStart models in private hub

* fixed formatting

* remove unused imports

* fix unused imports

* fix unit test failure and fix bug around versioning

* fix formatting

* fix unit tests

* fix model_uri usage issue

* fix some formatting

* separate private hub setup code

* add try catch block

* fix flake8 issue so except clause is not bare

* black formatting

fix: resolve infinite loop in _find_config on Windows systems (aws#4970)

* fix: resolve Windows path handling in _find_config

* Replace Path.match("/") with Path.anchor comparison
* Fix infinite loop in _studio.py path traversal

* test: Add tests for the new root path exploration

* Fix formatting style

* Fixed line to long

* Fix docstyle by running black manually

* Fix testcase with \\ when running on non-windows machines

* Fix formatting style

* cleanup unused import

change: update image_uri_configs  03-11-2025 07:18:09 PST

Fixing Pytorch training python version in tests (aws#5084)

* Fixing Pytorch training python version in tests

* Updating Inference test handling

remove s3 output location requirement from hub class init (aws#5081)

* remove s3 output location requirement from hub class init

* fix integ test hub

* lint

* fix test

---------

Co-authored-by: Gokul Anantha Narayanan <[email protected]>

fix: Prevent RunContext overlap between test_run tests (aws#5083)

Co-authored-by: Gokul Anantha Narayanan <[email protected]>

remove py38 from unit testing

fix integ test by bumping py38 to py39 for PyTorch

change framework_version that supports py39 in integ tests

remove py38 from unit testing

Update estimator.py
mollyheamazon added a commit to mollyheamazon/sagemaker-python-sdk that referenced this pull request Apr 16, 2025
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744156034 -0700

parent fb22b91
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744155953 -0700

parent fb22b91
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744155703 -0700

parent fb22b91
author Molly He <[email protected]> 1740529460 -0800
committer Molly He <[email protected]> 1744155683 -0700

Test py312 ci support

Test py312 ci support

first commit

test to point to personal stack

changed tox.ini

Revert "changed tox.ini"

This reverts commit 696cb47.

Revert "Revert "changed tox.ini""

This reverts commit 5d35bd0.

Revert "Revert "Revert "changed tox.ini"""

This reverts commit 2ab95fb.

Revert "Revert "changed tox.ini""

This reverts commit 5d35bd0.

Revert "changed tox.ini"

This reverts commit 696cb47.

Revert "test to point to personal stack"

This reverts commit 49de844.

Revert "first commit"

This reverts commit 1374184.

add pyproject.toml

add py312 to ci

bump numpy version

numpy version change

add py312

upgrade pip version

add setuptools wheel to tox.ini

add pyyaml version constraint, remove py312 from docstring because there is no py312 image yet

update pyyaml version constraint

update pyyaml version constraint

deprecate py38

bump scipy

bump tensorflow and tensorboard

bump dill

bump apache-airflow to ensure dill and greenlet version

remove constraint for apache-airflow

remove constraint for apache-airflow

bump torch version

bump torchvision version

new tests

try changing some tests

update model trainer test

fix test_pipeline

add constraint to apache in tox

Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

Fix error when there is no session to call _create_model_request() (aws#5062)

* Fix error when there is no session to call _create_model_request()

* Fix codestyle

---------

Co-authored-by: pintaoz <[email protected]>

Ensure Model.is_repack() returns a boolean (aws#5060)

* Ensure Model.is_repack() returns a boolean

* update test

---------

Co-authored-by: pintaoz <[email protected]>

feat: Make DistributedConfig Extensible (aws#5039)

* feat: Make DistributedConfig Extensible

* pylint

* Include none types when creating config jsons for safer reference

* fix: update test to account for changes

* format

* Add integ test

* pylint

* prepare release v2.240.0

* update development version to v2.240.1.dev0

* Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

* fix: Added check for the presence of model package group before creating one (aws#5063)

Co-authored-by: Keshav Chandak <[email protected]>

* Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

* remove union

* fix merge artifact

* Change dir path to distributed_drivers

* update paths

---------

Co-authored-by: ci <ci>
Co-authored-by: pintaoz-aws <[email protected]>
Co-authored-by: pintaoz <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>

Skip tests with deprecated instance type (aws#5077)

Co-authored-by: pintaoz <[email protected]>

pipeline definition function doc update (aws#5074)

Co-authored-by: Rohan Gujarathi <[email protected]>

feat: add integ tests for training JumpStart models in private hub (aws#5076)

* feat: add integ tests for training JumpStart models in private hub

* fixed formatting

* remove unused imports

* fix unused imports

* fix unit test failure and fix bug around versioning

* fix formatting

* fix unit tests

* fix model_uri usage issue

* fix some formatting

* separate private hub setup code

* add try catch block

* fix flake8 issue so except clause is not bare

* black formatting

fix: resolve infinite loop in _find_config on Windows systems (aws#4970)

* fix: resolve Windows path handling in _find_config

* Replace Path.match("/") with Path.anchor comparison
* Fix infinite loop in _studio.py path traversal

* test: Add tests for the new root path exploration

* Fix formatting style

* Fixed line to long

* Fix docstyle by running black manually

* Fix testcase with \\ when running on non-windows machines

* Fix formatting style

* cleanup unused import

change: update image_uri_configs  03-11-2025 07:18:09 PST

Fixing Pytorch training python version in tests (aws#5084)

* Fixing Pytorch training python version in tests

* Updating Inference test handling

remove s3 output location requirement from hub class init (aws#5081)

* remove s3 output location requirement from hub class init

* fix integ test hub

* lint

* fix test

---------

Co-authored-by: Gokul Anantha Narayanan <[email protected]>

fix: Prevent RunContext overlap between test_run tests (aws#5083)

Co-authored-by: Gokul Anantha Narayanan <[email protected]>

remove py38 from unit testing

fix integ test by bumping py38 to py39 for PyTorch

change framework_version that supports py39 in integ tests

remove py38 from unit testing

Update estimator.py
pravali96 pushed a commit to pravali96/sagemaker-python-sdk that referenced this pull request Apr 21, 2025
* feat: Make DistributedConfig Extensible

* pylint

* Include none types when creating config jsons for safer reference

* fix: update test to account for changes

* format

* Add integ test

* pylint

* prepare release v2.240.0

* update development version to v2.240.1.dev0

* Fix key error in _send_metrics() (aws#5068)

Co-authored-by: pintaoz <[email protected]>

* fix: Added check for the presence of model package group before creating one (aws#5063)

Co-authored-by: Keshav Chandak <[email protected]>

* Use sagemaker session's s3_resource in download_folder (aws#5064)

Co-authored-by: pintaoz <[email protected]>

* remove union

* fix merge artifact

* Change dir path to distributed_drivers

* update paths

---------

Co-authored-by: ci <ci>
Co-authored-by: pintaoz-aws <[email protected]>
Co-authored-by: pintaoz <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
mollyheamazon pushed a commit that referenced this pull request Apr 21, 2025
* feat: Make DistributedConfig Extensible

* pylint

* Include none types when creating config jsons for safer reference

* fix: update test to account for changes

* format

* Add integ test

* pylint

* prepare release v2.240.0

* update development version to v2.240.1.dev0

* Fix key error in _send_metrics() (#5068)

Co-authored-by: pintaoz <[email protected]>

* fix: Added check for the presence of model package group before creating one (#5063)

Co-authored-by: Keshav Chandak <[email protected]>

* Use sagemaker session's s3_resource in download_folder (#5064)

Co-authored-by: pintaoz <[email protected]>

* remove union

* fix merge artifact

* Change dir path to distributed_drivers

* update paths

---------

Co-authored-by: ci <ci>
Co-authored-by: pintaoz-aws <[email protected]>
Co-authored-by: pintaoz <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Co-authored-by: Keshav Chandak <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants