Skip to content

fix: move sagemaker config loading to LocalSession since it is only used for local code support. #1137

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 4 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ def read_version():
"protobuf>=3.1",
"scipy>=0.19.0",
"protobuf3-to-dict>=0.1.5",
"PyYAML>=3.10, <5", # required for session
"requests>=2.20.0, <2.21",
]

# Specific use case dependencies
extras = {
"analytics": ["pandas"],
"local": ["urllib3>=1.21, <1.25", "docker-compose>=1.23.0"],
"local": [
"urllib3>=1.21, <1.25",
"docker-compose>=1.23.0",
"PyYAML>=3.10, <5", # PyYAML version has to match docker-compose requirements
],
"tensorflow": ["tensorflow>=1.3.0"],
}
# Meta dependency groups
Expand Down
9 changes: 7 additions & 2 deletions src/sagemaker/local/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
from threading import Thread
from six.moves.urllib.parse import urlparse

import yaml

import sagemaker
import sagemaker.local.data
import sagemaker.local.utils
Expand Down Expand Up @@ -461,6 +459,13 @@ def _generate_compose_file(self, command, additional_volumes=None, additional_en
}

docker_compose_path = os.path.join(self.container_root, DOCKER_COMPOSE_FILENAME)

try:
import yaml
except ImportError as e:
logging.error(sagemaker.utils._module_import_error("yaml", "Local mode", "local"))
raise e

yaml_content = yaml.dump(content, default_flow_style=False)
logger.info("docker compose file: \n%s", yaml_content)
with open(docker_compose_path, "w") as f:
Expand Down
26 changes: 18 additions & 8 deletions src/sagemaker/local/local_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import absolute_import

import logging
import os
import platform

import boto3
Expand All @@ -28,14 +29,7 @@
_LocalTransformJob,
)
from sagemaker.session import Session
from sagemaker.utils import DeferredError, get_config_value

try:
import urllib3
except ImportError as e:
logging.warning("urllib3 failed to import. Local mode features will be impaired or broken.")
# Any subsequent attempt to use urllib3 will raise the ImportError
urllib3 = DeferredError(e)
from sagemaker.utils import get_config_value, _module_import_error

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -329,6 +323,12 @@ def __init__(self, config=None):
config (dict): Optional configuration for this client. In particular only
the local port is read.
"""
try:
import urllib3
except ImportError as e:
logging.error(_module_import_error("urllib3", "Local mode", "local"))
raise e

self.http = urllib3.PoolManager()
self.serving_port = 8080
self.config = config
Expand Down Expand Up @@ -403,6 +403,16 @@ def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client):
self.sagemaker_runtime_client = LocalSagemakerRuntimeClient(self.config)
self.local_mode = True

sagemaker_config_file = os.path.join(os.path.expanduser("~"), ".sagemaker", "config.yaml")
if os.path.exists(sagemaker_config_file):
try:
import yaml
except ImportError as e:
logging.error(_module_import_error("yaml", "Local mode", "local"))
raise e

self.config = yaml.load(open(sagemaker_config_file, "r"))

def logs_for_job(self, job_name, wait=False, poll=5):
"""
Expand Down
8 changes: 2 additions & 6 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import boto3
import botocore.config
from botocore.exceptions import ClientError
import yaml

import sagemaker.logs
from sagemaker import vpc_utils
Expand Down Expand Up @@ -95,11 +94,8 @@ def __init__(self, boto_session=None, sagemaker_client=None, sagemaker_runtime_c
"""
self._default_bucket = None

sagemaker_config_file = os.path.join(os.path.expanduser("~"), ".sagemaker", "config.yaml")
if os.path.exists(sagemaker_config_file):
self.config = yaml.load(open(sagemaker_config_file, "r"))
else:
self.config = None
# currently is used for local_code in local mode
self.config = None

self._initialize(boto_session, sagemaker_client, sagemaker_runtime_client)

Expand Down
19 changes: 19 additions & 0 deletions src/sagemaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,22 @@ def __getattr__(self, name):
name:
"""
raise self.exc


def _module_import_error(py_module, feature, extras):
"""Return error message for module import errors, provide
installation details.
Args:
py_module (str): Module that failed to be imported
feature (str): Affected sagemaker feature
Copy link
Contributor

Choose a reason for hiding this comment

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

s/sagemaker/SageMaker

extras (str): Name of the extra_requirements to install all of the dependencies
Copy link
Contributor

Choose a reason for hiding this comment

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

  • s/extra_requirements/extras_require (with backticks)
  • s/all of the/the relevant

Returns:
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: add a newline before "Returns"

str: Error message with installation instructions.
"""
error_msg = (
"Failed to import {}. {} features will be impaired or broken."
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: need a space after the second period

"Please run \"pip install 'sagemaker[{}]'\" "
"to install all required dependencies."
)
return error_msg.format(py_module, feature, extras)