Skip to content

test rtd #3001

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions src/sagemaker/estimator.py
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
from sagemaker.debugger import TensorBoardOutputConfig # noqa: F401 # pylint: disable=unused-import
from sagemaker.debugger import get_rule_container_image_uri
from sagemaker.s3 import S3Uploader
from sagemaker.sdk_metrics import SDKMetrics

from sagemaker.fw_utils import (
create_image_uri,
@@ -493,7 +494,8 @@ def fit(self, inputs=None, wait=True, logs="All", job_name=None, experiment_conf
"""
self._prepare_for_training(job_name=job_name)

self.latest_training_job = _TrainingJob.start_new(self, inputs, experiment_config)
self.latest_training_job = _TrainingJob.start_new(self, inputs, experiment_config,
SDKMetrics(__name__, "fit"))
self.jobs.append(self.latest_training_job)
if wait:
self.latest_training_job.wait(logs=logs)
@@ -988,7 +990,7 @@ class _TrainingJob(_Job):
"""Placeholder docstring"""

@classmethod
def start_new(cls, estimator, inputs, experiment_config):
def start_new(cls, estimator, inputs, experiment_config, sdk_metrics=None):
"""Create a new Amazon SageMaker training job from the estimator.

Args:
@@ -1065,7 +1067,7 @@ def start_new(cls, estimator, inputs, experiment_config):
if estimator.enable_sagemaker_metrics is not None:
train_args["enable_sagemaker_metrics"] = estimator.enable_sagemaker_metrics

estimator.sagemaker_session.train(**train_args)
estimator.sagemaker_session.train(**train_args, sdk_metrics=sdk_metrics)

return cls(estimator.sagemaker_session, estimator._current_job_name)

32 changes: 32 additions & 0 deletions src/sagemaker/sdk_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2020 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 datetime import datetime


class SDKMetrics(object):
def __init__(self, module, function=None):
self.module = module
self.function = function
self.timestamp = datetime.now()

def get_metrics_string(self):
metrics = "MODULE/{}, DURATION/{}".format(
self.module,
(datetime.now() - self.timestamp).microseconds
)

if self.function:
metrics = "FUNCTION/{} {}".format(self.function, metrics)

return metrics
32 changes: 26 additions & 6 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@

# import s3_input for backward compatibility
from sagemaker.inputs import s3_input # noqa # pylint: disable=unused-import
from sagemaker.user_agent import prepend_user_agent
from sagemaker.user_agent import prepend_user_agent, update_sdk_metrics
from sagemaker.utils import (
name_from_image,
secondary_training_status_changed,
@@ -132,7 +132,7 @@ def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client):
)

self.sagemaker_client = sagemaker_client or self.boto_session.client("sagemaker")
prepend_user_agent(self.sagemaker_client)
self._sagemaker_client_default_user_agent = prepend_user_agent(self.sagemaker_client)

if sagemaker_runtime_client is not None:
self.sagemaker_runtime_client = sagemaker_runtime_client
@@ -142,10 +142,28 @@ def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client):
"runtime.sagemaker", config=config
)

prepend_user_agent(self.sagemaker_runtime_client)
self._sagemaker_runtime_client_default_user_agent = prepend_user_agent(
self.sagemaker_runtime_client
)

self.local_mode = False

def sagemaker_client_sdk_metrics(self, sdk_metrics):
"""
sdk_metrics (sagemaker.SDKMetrics): An object that defines
the Python SDK telemetry metrics used to track Python SDK usage
Returns:
boto3.SageMaker.Client: Client which makes Amazon SageMaker service
calls other than ``InvokeEndpoint`` with updated user_agent string
with sdk_metrics.
"""
update_sdk_metrics(
self.sagemaker_client,
self._sagemaker_client_default_user_agent,
sdk_metrics
)
return self.sagemaker_client

@property
def boto_region_name(self):
"""Placeholder docstring"""
@@ -440,6 +458,7 @@ def train( # noqa: C901
debugger_hook_config=None,
tensorboard_output_config=None,
enable_sagemaker_metrics=None,
sdk_metrics=None,
):
"""Create an Amazon SageMaker training job.

@@ -509,7 +528,6 @@ def train( # noqa: C901
Series. For more information see:
https://docs.aws.amazon.com/sagemaker/latest/dg/API_AlgorithmSpecification.html#SageMaker-Type-AlgorithmSpecification-EnableSageMakerMetricsTimeSeries
(default: ``None``).

Returns:
str: ARN of the training job, if it is created.
"""
@@ -587,7 +605,9 @@ def train( # noqa: C901

LOGGER.info("Creating training-job with name: %s", job_name)
LOGGER.debug("train request: %s", json.dumps(train_request, indent=4))
self.sagemaker_client.create_training_job(**train_request)

self.sagemaker_client_sdk_metrics(sdk_metrics=sdk_metrics)\
.create_training_job(**train_request)

def process(
self,
@@ -664,7 +684,7 @@ def process(

LOGGER.info("Creating processing-job with name %s", job_name)
LOGGER.debug("process request: %s", json.dumps(process_request, indent=4))
self.sagemaker_client.create_processing_job(**process_request)
self.sagemaker_client_sdk_metrics([]).create_processing_job(**process_request)

def create_monitoring_schedule(
self,
23 changes: 22 additions & 1 deletion src/sagemaker/user_agent.py
Original file line number Diff line number Diff line change
@@ -52,11 +52,32 @@ def determine_prefix(user_agent=""):
def prepend_user_agent(client):
"""
Args:
client:
client: boto3 client (e.g. sagemaker or sagemaker-runtime client)
Returns:
(str): Prepended user_agent string
"""
prefix = determine_prefix(client._client_config.user_agent)

if client._client_config.user_agent is None:
client._client_config.user_agent = prefix
else:
client._client_config.user_agent = "{} {}".format(prefix, client._client_config.user_agent)

return client._client_config.user_agent


def update_sdk_metrics(client, default_user_agent, sdk_metrics):
"""
Args:
client (boto3.*.Client): boto3 client (e.g. boto3.SageMaker.Client or
boto3.SageMakerRuntime.Client client)
default_user_agent (str): default user agent string without any call specific SDK metrics
sdk_metrics (sagemaker.SDKMetrics): an object of SDKMetrics specific to a call
"""
try:
client._client_config.user_agent = "{} {}".format(
default_user_agent, sdk_metrics.get_metrics_string()
)
except:
# Any telemetry is secondary and we DO want to silently ignore failures.
pass