Skip to content
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

feat: Pulling in dependencies (in_process mode) using conda environment #4807

Merged
merged 53 commits into from
Aug 7, 2024

Conversation

bryannahm1
Copy link
Contributor

Issue #, if available:

Description of changes:
Added a new class (RequirementsManager) that detects if a .txt or .yml file provided for dependencies, and installs them accordingly via pip or conda env.

Testing done:
Integr tests run locally.

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)

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



class RequirementsManager:
"""Transformers build logic with ModelBuilder()"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

"""Transformers build logic with ModelBuilder()"""

def detect_file_exists(self, dependencies: str = None) -> str:
"""Creates snapshot of the user's environment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Not sure if this is entirely right? we are detecting not snapshotting anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is right, good catch, I will rephrase.



if __name__ == "__main__":
RequirementsManager().detect_file_exists(dependencies="auto_capture")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is auto_capture as a value used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It ended up being unused so I have removed it.

@@ -227,6 +234,22 @@ def _transformers_model_builder_deploy_wrapper(self, *args, **kwargs) -> Type[Pr
)
return predictor

if self.mode == Mode.IN_PROCESS:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean this up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be removed.

@patch(
"sagemaker.serve.builder.requirements_manager.RequirementsManager._capture_from_local_runtime"
)
def test_detect_file_exists_fail(self, mock__capture_from_local_runtime) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

mock_sample_output = [{"generated_text": mock_response}]


class TestInProcessMode(unittest.TestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this will be removed on revision

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will be removing in_process mode changes as that is a different PR: #4784


# Dependencies specified as either req.txt or conda_env.yml
if dependencies.endswith(".txt"):
self._install_requirements_txt()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're doing multiple things in one function. It's generally recommended to have functions as single tenant. Also, if you want to install while check path existence, you probably want to change the naming as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My naming was making the functions confusing to understand, I will rename so they better fit their purpose. Thank you for pointing this out.

"""Returns the conda prefix from the set environment variable. None otherwise."""
return os.getenv("CONDA_PREFIX")

def _capture_from_local_runtime(self) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I missed something here, but isn't this function just to generate an empty file? Can you point me to where dependencies are actually captured?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will be making a new .txt file similar to the .yml one with required dependencies, good catch, thank you.

@samruds samruds requested a review from makungaj1 August 2, 2024 17:20
@bryannahm1 bryannahm1 marked this pull request as ready for review August 7, 2024 05:14
@bryannahm1 bryannahm1 requested a review from a team as a code owner August 7, 2024 05:14
dependencies = self._detect_conda_env_and_local_dependencies()

# Dependencies specified as either req.txt or conda_env.yml
if dependencies.endswith(".txt"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_detect_conda_env_and_local_dependencies() will either return conda_in_process.yml or raise ValueError("No conda environment seems to be active.") , in other words, if dependencies.endswith(".txt") will never evaluate to true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a good find. Thanks for your help with modifying it!

class RequirementsManager:
"""Manages dependency installation by detecting file types"""

def capture_and_install_dependencies(self, dependencies: str = None) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input dependencies will always be shadowed since there's a local variable named as the same name

@sage-maker sage-maker merged commit bceefd1 into aws:master Aug 7, 2024
14 checks passed
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