File size: 24,851 Bytes
4021124 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | # Copyright 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.
"""Placeholder docstring"""
from __future__ import absolute_import
import logging
import os
import platform
from datetime import datetime
import boto3
from botocore.exceptions import ClientError
from sagemaker.local.image import _SageMakerContainer
from sagemaker.local.utils import get_docker_host
from sagemaker.local.entities import (
_LocalEndpointConfig,
_LocalEndpoint,
_LocalModel,
_LocalProcessingJob,
_LocalTrainingJob,
_LocalTransformJob,
_LocalPipeline,
)
from sagemaker.session import Session
from sagemaker.utils import get_config_value, _module_import_error
logger = logging.getLogger(__name__)
class LocalSagemakerClient(object): # pylint: disable=too-many-public-methods
"""A SageMakerClient that implements the API calls locally.
Used for doing local training and hosting local endpoints. It still needs access to
a boto client to interact with S3 but it won't perform any SageMaker call.
Implements the methods with the same signature as the boto SageMakerClient.
Args:
Returns:
"""
_processing_jobs = {}
_training_jobs = {}
_transform_jobs = {}
_models = {}
_endpoint_configs = {}
_endpoints = {}
_pipelines = {}
def __init__(self, sagemaker_session=None):
"""Initialize a LocalSageMakerClient.
Args:
sagemaker_session (sagemaker.session.Session): a session to use to read configurations
from, and use its boto client.
"""
self.sagemaker_session = sagemaker_session or LocalSession()
def create_processing_job(
self,
ProcessingJobName,
AppSpecification,
ProcessingResources,
Environment=None,
ProcessingInputs=None,
ProcessingOutputConfig=None,
**kwargs
):
"""Creates a processing job in Local Mode
Args:
ProcessingJobName(str): local processing job name.
AppSpecification(dict): Identifies the container and application to run.
ProcessingResources(dict): Identifies the resources to use for local processing.
Environment(dict, optional): Describes the environment variables to pass
to the container. (Default value = None)
ProcessingInputs(dict, optional): Describes the processing input data.
(Default value = None)
ProcessingOutputConfig(dict, optional): Describes the processing output
configuration. (Default value = None)
**kwargs: Keyword arguments
Returns:
"""
Environment = Environment or {}
ProcessingInputs = ProcessingInputs or []
ProcessingOutputConfig = ProcessingOutputConfig or {}
container_entrypoint = None
if "ContainerEntrypoint" in AppSpecification:
container_entrypoint = AppSpecification["ContainerEntrypoint"]
container_arguments = None
if "ContainerArguments" in AppSpecification:
container_arguments = AppSpecification["ContainerArguments"]
if "ExperimentConfig" in kwargs:
logger.warning("Experiment configuration is not supported in local mode.")
if "NetworkConfig" in kwargs:
logger.warning("Network configuration is not supported in local mode.")
if "StoppingCondition" in kwargs:
logger.warning("Stopping condition is not supported in local mode.")
container = _SageMakerContainer(
ProcessingResources["ClusterConfig"]["InstanceType"],
ProcessingResources["ClusterConfig"]["InstanceCount"],
AppSpecification["ImageUri"],
sagemaker_session=self.sagemaker_session,
container_entrypoint=container_entrypoint,
container_arguments=container_arguments,
)
processing_job = _LocalProcessingJob(container)
logger.info("Starting processing job")
processing_job.start(
ProcessingInputs, ProcessingOutputConfig, Environment, ProcessingJobName
)
LocalSagemakerClient._processing_jobs[ProcessingJobName] = processing_job
def describe_processing_job(self, ProcessingJobName):
"""Describes a local processing job.
Args:
ProcessingJobName(str): Processing job name to describe.
Returns: (dict) DescribeProcessingJob Response.
Returns:
"""
if ProcessingJobName not in LocalSagemakerClient._processing_jobs:
error_response = {
"Error": {
"Code": "ValidationException",
"Message": "Could not find local processing job",
}
}
raise ClientError(error_response, "describe_processing_job")
return LocalSagemakerClient._processing_jobs[ProcessingJobName].describe()
def create_training_job(
self,
TrainingJobName,
AlgorithmSpecification,
OutputDataConfig,
ResourceConfig,
InputDataConfig=None,
Environment=None,
**kwargs
):
"""Create a training job in Local Mode.
Args:
TrainingJobName(str): local training job name.
AlgorithmSpecification(dict): Identifies the training algorithm to use.
InputDataConfig(dict, optional): Describes the training dataset and the location where
it is stored. (Default value = None)
OutputDataConfig(dict): Identifies the location where you want to save the results of
model training.
ResourceConfig(dict): Identifies the resources to use for local model training.
Environment(dict, optional): Describes the environment variables to pass
to the container. (Default value = None)
HyperParameters(dict) [optional]: Specifies these algorithm-specific parameters to
influence the quality of the final model.
**kwargs:
Returns:
"""
InputDataConfig = InputDataConfig or {}
Environment = Environment or {}
container = _SageMakerContainer(
ResourceConfig["InstanceType"],
ResourceConfig["InstanceCount"],
AlgorithmSpecification["TrainingImage"],
sagemaker_session=self.sagemaker_session,
)
training_job = _LocalTrainingJob(container)
hyperparameters = kwargs["HyperParameters"] if "HyperParameters" in kwargs else {}
logger.info("Starting training job")
training_job.start(
InputDataConfig, OutputDataConfig, hyperparameters, Environment, TrainingJobName
)
LocalSagemakerClient._training_jobs[TrainingJobName] = training_job
def describe_training_job(self, TrainingJobName):
"""Describe a local training job.
Args:
TrainingJobName(str): Training job name to describe.
Returns: (dict) DescribeTrainingJob Response.
Returns:
"""
if TrainingJobName not in LocalSagemakerClient._training_jobs:
error_response = {
"Error": {
"Code": "ValidationException",
"Message": "Could not find local training job",
}
}
raise ClientError(error_response, "describe_training_job")
return LocalSagemakerClient._training_jobs[TrainingJobName].describe()
def create_transform_job(
self,
TransformJobName,
ModelName,
TransformInput,
TransformOutput,
TransformResources,
**kwargs
):
"""Create the transform job.
Args:
TransformJobName:
ModelName:
TransformInput:
TransformOutput:
TransformResources:
**kwargs:
Returns:
"""
transform_job = _LocalTransformJob(TransformJobName, ModelName, self.sagemaker_session)
LocalSagemakerClient._transform_jobs[TransformJobName] = transform_job
transform_job.start(TransformInput, TransformOutput, TransformResources, **kwargs)
def describe_transform_job(self, TransformJobName):
"""Describe the transform job.
Args:
TransformJobName:
Returns:
"""
if TransformJobName not in LocalSagemakerClient._transform_jobs:
error_response = {
"Error": {
"Code": "ValidationException",
"Message": "Could not find local transform job",
}
}
raise ClientError(error_response, "describe_transform_job")
return LocalSagemakerClient._transform_jobs[TransformJobName].describe()
def create_model(
self, ModelName, PrimaryContainer, *args, **kwargs
): # pylint: disable=unused-argument
"""Create a Local Model Object.
Args:
ModelName (str): the Model Name
PrimaryContainer (dict): a SageMaker primary container definition
*args:
**kwargs:
Returns:
"""
LocalSagemakerClient._models[ModelName] = _LocalModel(ModelName, PrimaryContainer)
def describe_model(self, ModelName):
"""Describe the model.
Args:
ModelName:
Returns:
"""
if ModelName not in LocalSagemakerClient._models:
error_response = {
"Error": {"Code": "ValidationException", "Message": "Could not find local model"}
}
raise ClientError(error_response, "describe_model")
return LocalSagemakerClient._models[ModelName].describe()
def describe_endpoint_config(self, EndpointConfigName):
"""Describe the endpoint configuration.
Args:
EndpointConfigName:
Returns:
"""
if EndpointConfigName not in LocalSagemakerClient._endpoint_configs:
error_response = {
"Error": {
"Code": "ValidationException",
"Message": "Could not find local endpoint config",
}
}
raise ClientError(error_response, "describe_endpoint_config")
return LocalSagemakerClient._endpoint_configs[EndpointConfigName].describe()
def create_endpoint_config(self, EndpointConfigName, ProductionVariants, Tags=None):
"""Create the endpoint configuration.
Args:
EndpointConfigName:
ProductionVariants:
Tags: (Default value = None)
Returns:
"""
LocalSagemakerClient._endpoint_configs[EndpointConfigName] = _LocalEndpointConfig(
EndpointConfigName, ProductionVariants, Tags
)
def describe_endpoint(self, EndpointName):
"""Describe the endpoint.
Args:
EndpointName:
Returns:
"""
if EndpointName not in LocalSagemakerClient._endpoints:
error_response = {
"Error": {"Code": "ValidationException", "Message": "Could not find local endpoint"}
}
raise ClientError(error_response, "describe_endpoint")
return LocalSagemakerClient._endpoints[EndpointName].describe()
def create_endpoint(self, EndpointName, EndpointConfigName, Tags=None):
"""Create the endpoint.
Args:
EndpointName:
EndpointConfigName:
Tags: (Default value = None)
Returns:
"""
endpoint = _LocalEndpoint(EndpointName, EndpointConfigName, Tags, self.sagemaker_session)
LocalSagemakerClient._endpoints[EndpointName] = endpoint
endpoint.serve()
def update_endpoint(self, EndpointName, EndpointConfigName): # pylint: disable=unused-argument
"""Update the endpoint.
Args:
EndpointName:
EndpointConfigName:
Returns:
"""
raise NotImplementedError("Update endpoint name is not supported in local session.")
def delete_endpoint(self, EndpointName):
"""Delete the endpoint.
Args:
EndpointName:
Returns:
"""
if EndpointName in LocalSagemakerClient._endpoints:
LocalSagemakerClient._endpoints[EndpointName].stop()
def delete_endpoint_config(self, EndpointConfigName):
"""Delete the endpoint configuration.
Args:
EndpointConfigName:
Returns:
"""
if EndpointConfigName in LocalSagemakerClient._endpoint_configs:
del LocalSagemakerClient._endpoint_configs[EndpointConfigName]
def delete_model(self, ModelName):
"""Delete the model.
Args:
ModelName:
Returns:
"""
if ModelName in LocalSagemakerClient._models:
del LocalSagemakerClient._models[ModelName]
def create_pipeline(
self, pipeline, pipeline_description, **kwargs # pylint: disable=unused-argument
):
"""Create a local pipeline.
Args:
pipeline (Pipeline): Pipeline object
pipeline_description (str): Description of the pipeline
Returns:
Pipeline metadata (PipelineArn)
"""
local_pipeline = _LocalPipeline(
pipeline=pipeline,
pipeline_description=pipeline_description,
local_session=self.sagemaker_session,
)
LocalSagemakerClient._pipelines[pipeline.name] = local_pipeline
return {"PipelineArn": pipeline.name}
def update_pipeline(
self, pipeline, pipeline_description, **kwargs # pylint: disable=unused-argument
):
"""Update a local pipeline.
Args:
pipeline (Pipeline): Pipeline object
pipeline_description (str): Description of the pipeline
Returns:
Pipeline metadata (PipelineArn)
"""
if pipeline.name not in LocalSagemakerClient._pipelines:
error_response = {
"Error": {
"Code": "ResourceNotFound",
"Message": "Pipeline {} does not exist".format(pipeline.name),
}
}
raise ClientError(error_response, "update_pipeline")
LocalSagemakerClient._pipelines[pipeline.name].pipeline_description = pipeline_description
LocalSagemakerClient._pipelines[pipeline.name].pipeline = pipeline
LocalSagemakerClient._pipelines[
pipeline.name
].last_modified_time = datetime.now().timestamp()
return {"PipelineArn": pipeline.name}
def describe_pipeline(self, PipelineName):
"""Describe the pipeline.
Args:
PipelineName (str):
Returns:
Pipeline metadata (PipelineArn, PipelineDefinition, LastModifiedTime, etc)
"""
if PipelineName not in LocalSagemakerClient._pipelines:
error_response = {
"Error": {
"Code": "ResourceNotFound",
"Message": "Pipeline {} does not exist".format(PipelineName),
}
}
raise ClientError(error_response, "describe_pipeline")
return LocalSagemakerClient._pipelines[PipelineName].describe()
def delete_pipeline(self, PipelineName):
"""Delete the local pipeline.
Args:
PipelineName (str):
Returns:
Pipeline metadata (PipelineArn)
"""
if PipelineName in LocalSagemakerClient._pipelines:
del LocalSagemakerClient._pipelines[PipelineName]
return {"PipelineArn": PipelineName}
def start_pipeline_execution(self, PipelineName, **kwargs):
"""Start the pipeline.
Args:
PipelineName (str):
Returns: _LocalPipelineExecution object
"""
if "ParallelismConfiguration" in kwargs:
logger.warning("Parallelism configuration is not supported in local mode.")
if PipelineName not in LocalSagemakerClient._pipelines:
error_response = {
"Error": {
"Code": "ResourceNotFound",
"Message": "Pipeline {} does not exist".format(PipelineName),
}
}
raise ClientError(error_response, "start_pipeline_execution")
return LocalSagemakerClient._pipelines[PipelineName].start(**kwargs)
class LocalSagemakerRuntimeClient(object):
"""A SageMaker Runtime client that calls a local endpoint only."""
def __init__(self, config=None):
"""Initializes a LocalSageMakerRuntimeClient.
Args:
config (dict): Optional configuration for this client. In particular only
the local port is read.
"""
try:
import urllib3
except ImportError as e:
logger.error(_module_import_error("urllib3", "Local mode", "local"))
raise e
self.http = urllib3.PoolManager()
self.serving_port = 8080
self.config = config
self.serving_port = get_config_value("local.serving_port", config) or 8080
def invoke_endpoint(
self,
Body,
EndpointName, # pylint: disable=unused-argument
ContentType=None,
Accept=None,
CustomAttributes=None,
TargetModel=None,
TargetVariant=None,
InferenceId=None,
):
"""Invoke the endpoint.
Args:
Body: Input data for which you want the model to provide inference.
EndpointName: The name of the endpoint that you specified when you
created the endpoint using the CreateEndpoint API.
ContentType: The MIME type of the input data in the request body (Default value = None)
Accept: The desired MIME type of the inference in the response (Default value = None)
CustomAttributes: Provides additional information about a request for an inference
submitted to a model hosted at an Amazon SageMaker endpoint (Default value = None)
TargetModel: The model to request for inference when invoking a multi-model endpoint
(Default value = None)
TargetVariant: Specify the production variant to send the inference request to when
invoking an endpoint that is running two or more variants (Default value = None)
InferenceId: If you provide a value, it is added to the captured data when you enable
data capture on the endpoint (Default value = None)
Returns:
object: Inference for the given input.
"""
url = "http://%s:%d/invocations" % (get_docker_host(), self.serving_port)
headers = {}
if ContentType is not None:
headers["Content-type"] = ContentType
if Accept is not None:
headers["Accept"] = Accept
if CustomAttributes is not None:
headers["X-Amzn-SageMaker-Custom-Attributes"] = CustomAttributes
if TargetModel is not None:
headers["X-Amzn-SageMaker-Target-Model"] = TargetModel
if TargetVariant is not None:
headers["X-Amzn-SageMaker-Target-Variant"] = TargetVariant
if InferenceId is not None:
headers["X-Amzn-SageMaker-Inference-Id"] = InferenceId
# The http client encodes all strings using latin-1, which is not what we want.
if isinstance(Body, str):
Body = Body.encode("utf-8")
r = self.http.request("POST", url, body=Body, preload_content=False, headers=headers)
return {"Body": r, "ContentType": Accept}
class LocalSession(Session):
"""A SageMaker ``Session`` class for Local Mode.
This class provides alternative Local Mode implementations for the functionality of
:class:`~sagemaker.session.Session`.
"""
def __init__(
self, boto_session=None, default_bucket=None, s3_endpoint_url=None, disable_local_code=False
):
"""Create a Local SageMaker Session.
Args:
boto_session (boto3.session.Session): The underlying Boto3 session which AWS service
calls are delegated to (default: None). If not provided, one is created with
default AWS configuration chain.
s3_endpoint_url (str): Override the default endpoint URL for Amazon S3, if set
(default: None).
disable_local_code (bool): Set ``True`` to override the default AWS configuration
chain to disable the ``local.local_code`` setting, which may not be supported for
some SDK features (default: False).
"""
self.s3_endpoint_url = s3_endpoint_url
# We use this local variable to avoid disrupting the __init__->_initialize API of the
# parent class... But overwriting it after constructor won't do anything, so prefix _ to
# discourage external use:
self._disable_local_code = disable_local_code
super(LocalSession, self).__init__(boto_session=boto_session, default_bucket=default_bucket)
if platform.system() == "Windows":
logger.warning("Windows Support for Local Mode is Experimental")
def _initialize(
self, boto_session, sagemaker_client, sagemaker_runtime_client, **kwargs
): # pylint: disable=unused-argument
"""Initialize this Local SageMaker Session.
Args:
boto_session:
sagemaker_client:
sagemaker_runtime_client:
kwargs:
Returns:
"""
if boto_session is None:
self.boto_session = boto3.Session()
else:
self.boto_session = boto_session
self._region_name = self.boto_session.region_name
if self._region_name is None:
raise ValueError(
"Must setup local AWS configuration with a region supported by SageMaker."
)
self.sagemaker_client = LocalSagemakerClient(self)
self.sagemaker_runtime_client = LocalSagemakerRuntimeClient(self.config)
self.local_mode = True
if self.s3_endpoint_url is not None:
self.s3_resource = boto_session.resource("s3", endpoint_url=self.s3_endpoint_url)
self.s3_client = boto_session.client("s3", endpoint_url=self.s3_endpoint_url)
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:
logger.error(_module_import_error("yaml", "Local mode", "local"))
raise e
self.config = yaml.safe_load(open(sagemaker_config_file, "r"))
if self._disable_local_code and "local" in self.config:
self.config["local"]["local_code"] = False
def logs_for_job(self, job_name, wait=False, poll=5, log_type="All"):
"""A no-op method meant to override the sagemaker client.
Args:
job_name:
wait: (Default value = False)
poll: (Default value = 5)
Returns:
"""
# override logs_for_job() as it doesn't need to perform any action
# on local mode.
pass # pylint: disable=unnecessary-pass
def logs_for_processing_job(self, job_name, wait=False, poll=10):
"""A no-op method meant to override the sagemaker client.
Args:
job_name:
wait: (Default value = False)
poll: (Default value = 10)
Returns:
"""
# override logs_for_job() as it doesn't need to perform any action
# on local mode.
pass # pylint: disable=unnecessary-pass
class file_input(object):
"""Amazon SageMaker channel configuration for FILE data sources, used in local mode."""
def __init__(self, fileUri, content_type=None):
"""Create a definition for input data used by an SageMaker training job in local mode."""
self.config = {
"DataSource": {
"FileDataSource": {
"FileDataDistributionType": "FullyReplicated",
"FileUri": fileUri,
}
}
}
if content_type is not None:
self.config["ContentType"] = content_type
|