ModelTrainer¶
- class sagemaker.modules.train.model_trainer.ModelTrainer(*, training_mode=Mode.SAGEMAKER_TRAINING_JOB, sagemaker_session=None, role=None, base_job_name=None, source_code=None, distributed=None, compute=None, networking=None, stopping_condition=None, training_image=None, training_image_config=None, algorithm_name=None, output_data_config=None, input_data_config=None, checkpoint_config=None, training_input_mode='File', environment={}, hyperparameters={}, tags=None, local_container_root='/home/docs/checkouts/readthedocs.org/user_builds/sagemaker/checkouts/stable/doc')¶
Class that trains a model using AWS SageMaker.
Example:
from sagemaker.modules.train import ModelTrainer from sagemaker.modules.configs import SourceCode, Compute, InputData source_code = SourceCode(source_dir="source", entry_script="train.py") training_image = "123456789012.dkr.ecr.us-west-2.amazonaws.com/my-training-image" model_trainer = ModelTrainer( training_image=training_image, source_code=source_code, ) train_data = InputData(channel_name="train", data_source="s3://bucket/train") model_trainer.train(input_data_config=[train_data]) training_job = model_trainer._latest_training_job
- Parameters
training_mode (Mode) – The training mode. Valid values are “Mode.LOCAL_CONTAINER” or “Mode.SAGEMAKER_TRAINING_JOB”.
sagemaker_session (Optiona(Session)) – The SageMakerCore session. For convinience, can be imported like:
from sagemaker.modules import Session
. If not specified, a new session will be created. If the default bucket for the artifacts needs to be updated, it can be done by passing it in the Session object.role (Optional(str)) – The IAM role ARN for the training job. If not specified, the default SageMaker execution role will be used.
base_job_name (Optional[str]) – The base name for the training job. If not specified, a default name will be generated using the algorithm name or training image.
source_code (Optional[SourceCode]) – The source code configuration. This is used to configure the source code for running the training job.
distributed (Optional[Union[MPI, Torchrun]]) – The distributed runner for the training job. This is used to configure a distributed training job. If specifed,
source_code
must also be provided.compute (Optional[Compute]) – The compute configuration. This is used to specify the compute resources for the training job. If not specified, will default to 1 instance of ml.m5.xlarge.
networking (Optional[Networking]) – The networking configuration. This is used to specify the networking settings for the training job.
stopping_condition (Optional[StoppingCondition]) – The stopping condition. This is used to specify the different stopping conditions for the training job. If not specified, will default to 1 hour max run time.
algorithm_name (Optional[str]) – The SageMaker marketplace algorithm name/arn to use for the training job. algorithm_name cannot be specified if training_image is specified.
training_image (Optional[str]) – The training image URI to use for the training job container. training_image cannot be specified if algorithm_name is specified. To find available sagemaker distributed images, see: https://docs.aws.amazon.com/sagemaker/latest/dg-ecr-paths/sagemaker-algo-docker-registry-paths
training_image_config (Optional[TrainingImageConfig]) – Training image Config. This is the configuration to use an image from a private Docker registry for a training job.
output_data_config (Optional[OutputDataConfig]) – The output data configuration. This is used to specify the output data location for the training job. If not specified in the session, will default to
s3://<default_bucket>/<default_prefix>/<base_job_name>/
.input_data_config (Optional[List[Union[Channel, InputData]]]) – The input data config for the training job. Takes a list of Channel or InputData objects. An InputDataSource can be an S3 URI string, local file path string, S3DataSource object, or FileSystemDataSource object.
checkpoint_config (Optional[CheckpointConfig]) – Contains information about the output location for managed spot training checkpoint data.
training_input_mode (Optional[str]) – The input mode for the training job. Valid values are “Pipe”, “File”, “FastFile”. Defaults to “File”.
environment (Optional[Dict[str, str]]) – The environment variables for the training job.
hyperparameters (Optional[Dict[str, Any]]) – The hyperparameters for the training job.
tags (Optional[List[Tag]]) – An array of key-value pairs. You can use tags to categorize your AWS resources in different ways, for example, by purpose, owner, or environment.
local_container_root (Optional[str]) – The local root directory to store artifacts from a training job launched in “LOCAL_CONTAINER” mode.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'forbid'}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_post_init(context, /)¶
We need to both initialize private attributes and call the user-defined model_post_init method.
- Parameters
self (BaseModel) –
context (Any) –
- Return type
None
- train(input_data_config=None, wait=True, logs=True)¶
Train a model using AWS SageMaker.
- Parameters
input_data_config (Optional[Union[List[Channel], Dict[str, DataSourceType]]]) – The input data config for the training job. Takes a list of Channel objects or a dictionary of channel names to DataSourceType. DataSourceType can be an S3 URI string, local file path string, S3DataSource object, or FileSystemDataSource object.
wait (Optional[bool]) – Whether to wait for the training job to complete before returning. Defaults to True.
logs (Optional[bool]) – Whether to display the training container logs while training. Defaults to True.
- create_input_data_channel(channel_name, data_source, key_prefix=None)¶
Create an input data channel for the training job.
- Parameters
channel_name (str) – The name of the input data channel.
data_source (DataSourceType) – The data source for the input data channel. DataSourceType can be an S3 URI string, local file path string, S3DataSource object, or FileSystemDataSource object.
key_prefix (Optional[str]) –
The key prefix to use when uploading data to S3. Only applicable when data_source is a local file path string. If not specified, local data will be uploaded to:
s3://<default_bucket_path>/<base_job_name>/input/<channel_name>/
If specified, local data will be uploaded to:
s3://<default_bucket_path>/<key_prefix>/<channel_name>/
- Return type
- classmethod from_recipe(training_recipe, compute, recipe_overrides=None, networking=None, stopping_condition=None, requirements=None, training_image=None, training_image_config=None, output_data_config=None, input_data_config=None, checkpoint_config=None, training_input_mode='File', environment=None, tags=None, sagemaker_session=None, role=None, base_job_name=None)¶
Create a ModelTrainer from a training recipe.
- Parameters
training_recipe (str) – The training recipe to use for training the model. This must be the name of a sagemaker training recipe or a path to a local training recipe .yaml file.
compute (Compute) – The compute configuration. This is used to specify the compute resources for the training job. If not specified, will default to 1 instance of ml.m5.xlarge.
recipe_overrides (Optional[Dict[str, Any]]) – The recipe overrides. This is used to override the default recipe parameters.
networking (Optional[Networking]) – The networking configuration. This is used to specify the networking settings for the training job.
stopping_condition (Optional[StoppingCondition]) – The stopping condition. This is used to specify the different stopping conditions for the training job. If not specified, will default to 1 hour max run time.
requirements (Optional[str]) – The path to a requirements file to install in the training job container.
training_image (Optional[str]) – The training image URI to use for the training job container. If not specified, the training image will be determined from the recipe.
training_image_config (Optional[TrainingImageConfig]) – Training image Config. This is the configuration to use an image from a private Docker registry for a training job.
output_data_config (Optional[OutputDataConfig]) – The output data configuration. This is used to specify the output data location for the training job. If not specified, will default to
s3://<default_bucket>/<base_job_name>/output/
.input_data_config (Optional[List[Union[Channel, InputData]]]) – The input data config for the training job. Takes a list of Channel or InputData objects. An InputDataSource can be an S3 URI string, local file path string, S3DataSource object, or FileSystemDataSource object.
checkpoint_config (Optional[CheckpointConfig]) – Contains information about the output location for managed spot training checkpoint data.
training_input_mode (Optional[str]) – The input mode for the training job. Valid values are “Pipe”, “File”, “FastFile”. Defaults to “File”.
environment (Optional[Dict[str, str]]) – The environment variables for the training job.
tags (Optional[List[Tag]]) – An array of key-value pairs. You can use tags to categorize your AWS resources in different ways, for example, by purpose, owner, or environment.
sagemaker_session (Optional[Session]) – The SageMakerCore session. If not specified, a new session will be created.
role (Optional[str]) – The IAM role ARN for the training job. If not specified, the default SageMaker execution role will be used.
base_job_name (Optional[str]) – The base name for the training job. If not specified, a default name will be generated using the algorithm name or training image.
- Return type
- with_tensorboard_output_config(tensorboard_output_config)¶
Set the TensorBoard output configuration.
- Parameters
tensorboard_output_config (sagemaker.modules.configs.TensorBoardOutputConfig) – The TensorBoard output configuration.
- Return type
- with_retry_strategy(retry_strategy)¶
Set the retry strategy for the training job.
- Parameters
retry_strategy (RetryStrategy) – The retry strategy for the training job.
- Return type
- with_infra_check_config(infra_check_config)¶
Set the infra check configuration for the training job.
- Parameters
infra_check_config (InfraCheckConfig) – The infra check configuration for the training job.
- Return type
- with_session_chaining_config(session_chaining_config)¶
Set the session chaining configuration for the training job.
- Parameters
session_chaining_config (SessionChainingConfig) – The session chaining configuration for the training job.
- Return type
- with_remote_debug_config(remote_debug_config)¶
Set the remote debug configuration for the training job.
- Parameters
remote_debug_config (RemoteDebugConfig) – The remote debug configuration for the training job.
- Return type
Configs¶
This module provides the configuration classes used in sagemaker.modules
.
Some of these classes are re-exported from sagemaker_core.shapes
. For convinence,
users can import these classes directly from sagemaker.modules.configs
.
- For more documentation on
sagemaker_core.shapes
, see:
- class sagemaker.modules.configs.SourceCode(*, source_dir=None, requirements=None, entry_script=None, command=None)¶
SourceCode.
The SourceCode class allows the user to specify the source code location, dependencies, entry script, or commands to be executed in the training job container.
- Parameters
source_dir (Optional[str]) – The local directory containing the source code to be used in the training job container.
requirements (Optional[str]) – The path within
source_dir
to arequirements.txt
file. If specified, the listed requirements will be installed in the training job container.entry_script (Optional[str]) – The path within
source_dir
to the entry script that will be executed in the training job container. If not specified, command must be provided.command (Optional[str]) – The command(s) to execute in the training job container. Example: “python my_script.py”. If not specified, entry_script must be provided.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.StoppingCondition(*, max_runtime_in_seconds=<sagemaker_core.main.utils.Unassigned object>, max_wait_time_in_seconds=<sagemaker_core.main.utils.Unassigned object>, max_pending_time_in_seconds=<sagemaker_core.main.utils.Unassigned object>)¶
Specifies a limit to how long a job can run. When the job reaches the time limit, SageMaker ends the job. Use this API to cap costs. To stop a training job, SageMaker sends the algorithm the SIGTERM signal, which delays job termination for 120 seconds. Algorithms can use this 120-second window to save the model artifacts, so the results of training are not lost. The training algorithms provided by SageMaker automatically save the intermediate results of a model training job when possible. This attempt to save artifacts is only a best effort case as model might not be in a state from which it can be saved. For example, if training has just started, the model might not be ready to save. When saved, this intermediate data is a valid model artifact. You can use it to create a model with CreateModel. The Neural Topic Model (NTM) currently does not support saving intermediate model artifacts. When training NTMs, make sure that the maximum runtime is sufficient for the training job to complete.
- Parameters
- max_runtime_in_seconds¶
- Type
The maximum length of time, in seconds, that a training or compilation job can run before it is stopped. For compilation jobs, if the job does not complete during this time, a TimeOut error is generated. We recommend starting with 900 seconds and increasing as necessary based on your model. For all other jobs, if the job does not complete during this time, SageMaker ends the job. When RetryStrategy is specified in the job request, MaxRuntimeInSeconds specifies the maximum time for all of the attempts in total, not each individual attempt. The default value is 1 day. The maximum value is 28 days. The maximum time that a TrainingJob can run in total, including any time spent publishing metrics or archiving and uploading models after it has been stopped, is 30 days.
- max_wait_time_in_seconds¶
- Type
The maximum length of time, in seconds, that a managed Spot training job has to complete. It is the amount of time spent waiting for Spot capacity plus the amount of time the job can run. It must be equal to or greater than MaxRuntimeInSeconds. If the job does not complete during this time, SageMaker ends the job. When RetryStrategy is specified in the job request, MaxWaitTimeInSeconds specifies the maximum time for all of the attempts in total, not each individual attempt.
- max_pending_time_in_seconds¶
- Type
The maximum length of time, in seconds, that a training or compilation job can be pending before it is stopped.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.RetryStrategy(*, maximum_retry_attempts)¶
The retry strategy to use when a training job fails due to an InternalServerError. RetryStrategy is specified as part of the CreateTrainingJob and CreateHyperParameterTuningJob requests. You can add the StoppingCondition parameter to the request to limit the training time for the complete job.
- Parameters
maximum_retry_attempts (int) –
- maximum_retry_attempts¶
- Type
The number of times to retry the job. When the job is retried, it’s SecondaryStatus is changed to STARTING.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.OutputDataConfig(*, s3_output_path, kms_key_id=<sagemaker_core.main.utils.Unassigned object>, compression_type=<sagemaker_core.main.utils.Unassigned object>)¶
Provides information about how to store model training results (model artifacts).
- kms_key_id¶
- Type
The Amazon Web Services Key Management Service (Amazon Web Services KMS) key that SageMaker uses to encrypt the model artifacts at rest using Amazon S3 server-side encryption. The KmsKeyId can be any of the following formats: // KMS Key ID “1234abcd-12ab-34cd-56ef-1234567890ab” // Amazon Resource Name (ARN) of a KMS Key “arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab” // KMS Key Alias “alias/ExampleAlias” // Amazon Resource Name (ARN) of a KMS Key Alias “arn:aws:kms:us-west-2:111122223333:alias/ExampleAlias” If you use a KMS key ID or an alias of your KMS key, the SageMaker execution role must include permissions to call kms:Encrypt. If you don’t provide a KMS key ID, SageMaker uses the default KMS key for Amazon S3 for your role’s account. For more information, see KMS-Managed Encryption Keys in the Amazon Simple Storage Service Developer Guide. If the output data is stored in Amazon S3 Express One Zone, it is encrypted with server-side encryption with Amazon S3 managed keys (SSE-S3). KMS key is not supported for Amazon S3 Express One Zone The KMS key policy must grant permission to the IAM role that you specify in your CreateTrainingJob, CreateTransformJob, or CreateHyperParameterTuningJob requests. For more information, see Using Key Policies in Amazon Web Services KMS in the Amazon Web Services Key Management Service Developer Guide.
- s3_output_path¶
- Type
Identifies the S3 path where you want SageMaker to store the model artifacts. For example, s3://bucket-name/key-name-prefix.
- compression_type¶
- Type
The model output compression type. Select None to output an uncompressed model, recommended for large model outputs. Defaults to gzip.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.Channel(*, channel_name, data_source, content_type=<sagemaker_core.main.utils.Unassigned object>, compression_type=<sagemaker_core.main.utils.Unassigned object>, record_wrapper_type=<sagemaker_core.main.utils.Unassigned object>, input_mode=<sagemaker_core.main.utils.Unassigned object>, shuffle_config=<sagemaker_core.main.utils.Unassigned object>)¶
A channel is a named input source that training algorithms can consume.
- Parameters
- channel_name¶
- Type
The name of the channel.
- data_source¶
- Type
The location of the channel data.
- content_type¶
- Type
The MIME type of the data.
- compression_type¶
- Type
If training data is compressed, the compression type. The default value is None. CompressionType is used only in Pipe input mode. In File mode, leave this field unset or set it to None.
- record_wrapper_type¶
- Type
Specify RecordIO as the value when input data is in raw format but the training algorithm requires the RecordIO format. In this case, SageMaker wraps each individual S3 object in a RecordIO record. If the input data is already in RecordIO format, you don’t need to set this attribute. For more information, see Create a Dataset Using RecordIO. In File mode, leave this field unset or set it to None.
- input_mode¶
- Type
(Optional) The input mode to use for the data channel in a training job. If you don’t set a value for InputMode, SageMaker uses the value set for TrainingInputMode. Use this parameter to override the TrainingInputMode setting in a AlgorithmSpecification request when you have a channel that needs a different input mode from the training job’s general setting. To download the data from Amazon Simple Storage Service (Amazon S3) to the provisioned ML storage volume, and mount the directory to a Docker volume, use File input mode. To stream data directly from Amazon S3 to the container, choose Pipe input mode. To use a model for incremental training, choose File input model.
- shuffle_config¶
- Type
A configuration for a shuffle option for input data in a channel. If you use S3Prefix for S3DataType, this shuffles the results of the S3 key prefix matches. If you use ManifestFile, the order of the S3 object references in the ManifestFile is shuffled. If you use AugmentedManifestFile, the order of the JSON lines in the AugmentedManifestFile is shuffled. The shuffling order is determined using the Seed value. For Pipe input mode, shuffling is done at the start of every epoch. With large datasets this ensures that the order of the training data is different for each epoch, it helps reduce bias and possible overfitting. In a multi-node training job when ShuffleConfig is combined with S3DataDistributionType of ShardedByS3Key, the data is shuffled across nodes so that the content sent to a particular node on the first epoch might be sent to a different node on the second epoch.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.ShuffleConfig(*, seed)¶
A configuration for a shuffle option for input data in a channel. If you use S3Prefix for S3DataType, the results of the S3 key prefix matches are shuffled. If you use ManifestFile, the order of the S3 object references in the ManifestFile is shuffled. If you use AugmentedManifestFile, the order of the JSON lines in the AugmentedManifestFile is shuffled. The shuffling order is determined using the Seed value. For Pipe input mode, when ShuffleConfig is specified shuffling is done at the start of every epoch. With large datasets, this ensures that the order of the training data is different for each epoch, and it helps reduce bias and possible overfitting. In a multi-node training job when ShuffleConfig is combined with S3DataDistributionType of ShardedByS3Key, the data is shuffled across nodes so that the content sent to a particular node on the first epoch might be sent to a different node on the second epoch.
- Parameters
seed (int) –
- seed¶
- Type
Determines the shuffling order in ShuffleConfig value.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.DataSource(*, s3_data_source=<sagemaker_core.main.utils.Unassigned object>, file_system_data_source=<sagemaker_core.main.utils.Unassigned object>)¶
Describes the location of the channel data.
- Parameters
s3_data_source (Optional[S3DataSource]) –
file_system_data_source (Optional[FileSystemDataSource]) –
- s3_data_source¶
- Type
The S3 location of the data source that is associated with a channel.
- file_system_data_source¶
- Type
The file system that is associated with a channel.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.S3DataSource(*, s3_data_type, s3_uri, s3_data_distribution_type=<sagemaker_core.main.utils.Unassigned object>, attribute_names=<sagemaker_core.main.utils.Unassigned object>, instance_group_names=<sagemaker_core.main.utils.Unassigned object>)¶
Describes the S3 data source. Your input bucket must be in the same Amazon Web Services region as your training job.
- Parameters
- s3_data_type¶
- Type
If you choose S3Prefix, S3Uri identifies a key name prefix. SageMaker uses all objects that match the specified key name prefix for model training. If you choose ManifestFile, S3Uri identifies an object that is a manifest file containing a list of object keys that you want SageMaker to use for model training. If you choose AugmentedManifestFile, S3Uri identifies an object that is an augmented manifest file in JSON lines format. This file contains the data you want to use for model training. AugmentedManifestFile can only be used if the Channel’s input mode is Pipe.
- s3_uri¶
- Type
Depending on the value specified for the S3DataType, identifies either a key name prefix or a manifest. For example: A key name prefix might look like this: s3://bucketname/exampleprefix/ A manifest might look like this: s3://bucketname/example.manifest A manifest is an S3 object which is a JSON file consisting of an array of elements. The first element is a prefix which is followed by one or more suffixes. SageMaker appends the suffix elements to the prefix to get a full set of S3Uri. Note that the prefix must be a valid non-empty S3Uri that precludes users from specifying a manifest whose individual S3Uri is sourced from different S3 buckets. The following code example shows a valid manifest format: [ {“prefix”: “s3://customer_bucket/some/prefix/”}, “relative/path/to/custdata-1”, “relative/path/custdata-2”, … “relative/path/custdata-N” ] This JSON is equivalent to the following S3Uri list: s3://customer_bucket/some/prefix/relative/path/to/custdata-1 s3://customer_bucket/some/prefix/relative/path/custdata-2 … s3://customer_bucket/some/prefix/relative/path/custdata-N The complete set of S3Uri in this manifest is the input data for the channel for this data source. The object that each S3Uri points to must be readable by the IAM role that SageMaker uses to perform tasks on your behalf. Your input bucket must be located in same Amazon Web Services region as your training job.
- s3_data_distribution_type¶
- Type
If you want SageMaker to replicate the entire dataset on each ML compute instance that is launched for model training, specify FullyReplicated. If you want SageMaker to replicate a subset of data on each ML compute instance that is launched for model training, specify ShardedByS3Key. If there are n ML compute instances launched for a training job, each instance gets approximately 1/n of the number of S3 objects. In this case, model training on each machine uses only the subset of training data. Don’t choose more ML compute instances for training than available S3 objects. If you do, some nodes won’t get any data and you will pay for nodes that aren’t getting any training data. This applies in both File and Pipe modes. Keep this in mind when developing algorithms. In distributed training, where you use multiple ML compute EC2 instances, you might choose ShardedByS3Key. If the algorithm requires copying training data to the ML storage volume (when TrainingInputMode is set to File), this copies 1/n of the number of objects.
- attribute_names¶
- Type
A list of one or more attribute names to use that are found in a specified augmented manifest file.
- instance_group_names¶
- Type
A list of names of instance groups that get data from the S3 data source.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.FileSystemDataSource(*, file_system_id, file_system_access_mode, file_system_type, directory_path)¶
Specifies a file system data source for a channel.
- Parameters
- file_system_id¶
- Type
The file system id.
- file_system_access_mode¶
- Type
The access mode of the mount of the directory associated with the channel. A directory can be mounted either in ro (read-only) or rw (read-write) mode.
- file_system_type¶
- Type
The file system type.
- directory_path¶
- Type
The full path to the directory to associate with the channel.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.TrainingImageConfig(*, training_repository_access_mode, training_repository_auth_config=<sagemaker_core.main.utils.Unassigned object>)¶
The configuration to use an image from a private Docker registry for a training job.
- Parameters
training_repository_access_mode (str) –
training_repository_auth_config (Optional[TrainingRepositoryAuthConfig]) –
- training_repository_access_mode¶
- Type
The method that your training job will use to gain access to the images in your private Docker registry. For access to an image in a private Docker registry, set to Vpc.
- training_repository_auth_config¶
- Type
An object containing authentication information for a private Docker registry containing your training images.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.TrainingRepositoryAuthConfig(*, training_repository_credentials_provider_arn)¶
An object containing authentication information for a private Docker registry.
- Parameters
training_repository_credentials_provider_arn (str) –
- training_repository_credentials_provider_arn¶
- Type
The Amazon Resource Name (ARN) of an Amazon Web Services Lambda function used to give SageMaker access credentials to your private Docker registry.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.Tag(*, key, value)¶
A tag object that consists of a key and an optional value, used to manage metadata for SageMaker Amazon Web Services resources. You can add tags to notebook instances, training jobs, hyperparameter tuning jobs, batch transform jobs, models, labeling jobs, work teams, endpoint configurations, and endpoints. For more information on adding tags to SageMaker resources, see AddTags. For more information on adding metadata to your Amazon Web Services resources with tagging, see Tagging Amazon Web Services resources. For advice on best practices for managing Amazon Web Services resources with tagging, see Tagging Best Practices: Implement an Effective Amazon Web Services Resource Tagging Strategy.
- key¶
- Type
The tag key. Tag keys must be unique per resource.
- value¶
- Type
The tag value.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.InfraCheckConfig(*, enable_infra_check=<sagemaker_core.main.utils.Unassigned object>)¶
Configuration information for the infrastructure health check of a training job. A SageMaker-provided health check tests the health of instance hardware and cluster network connectivity.
- enable_infra_check¶
- Type
Enables an infrastructure health check.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.RemoteDebugConfig(*, enable_remote_debug=<sagemaker_core.main.utils.Unassigned object>)¶
Configuration for remote debugging for the CreateTrainingJob API. To learn more about the remote debugging functionality of SageMaker, see Access a training container through Amazon Web Services Systems Manager (SSM) for remote debugging.
- enable_remote_debug¶
- Type
If set to True, enables remote debugging.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.SessionChainingConfig(*, enable_session_tag_chaining=<sagemaker_core.main.utils.Unassigned object>)¶
Contains information about attribute-based access control (ABAC) for a training job. The session chaining configuration uses Amazon Security Token Service (STS) for your training job to request temporary, limited-privilege credentials to tenants. For more information, see Attribute-based access control (ABAC) for multi-tenancy training.
- enable_session_tag_chaining¶
- Type
Set to True to allow SageMaker to extract session tags from a training job creation role and reuse these tags when assuming the training job execution role.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.InstanceGroup(*, instance_type, instance_count, instance_group_name)¶
Defines an instance group for heterogeneous cluster training. When requesting a training job using the CreateTrainingJob API, you can configure multiple instance groups .
- instance_type¶
- Type
Specifies the instance type of the instance group.
- instance_count¶
- Type
Specifies the number of instances of the instance group.
- instance_group_name¶
- Type
Specifies the name of the instance group.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.TensorBoardOutputConfig(*, s3_output_path, local_path=<sagemaker_core.main.utils.Unassigned object>)¶
Configuration of storage locations for the Amazon SageMaker Debugger TensorBoard output data.
- local_path¶
- Type
Path to local storage location for tensorBoard output. Defaults to /opt/ml/output/tensorboard.
- s3_output_path¶
- Type
Path to Amazon S3 storage location for TensorBoard output.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.CheckpointConfig(*, s3_uri, local_path=<sagemaker_core.main.utils.Unassigned object>)¶
Contains information about the output location for managed spot training checkpoint data.
- s3_uri¶
- Type
Identifies the S3 path where you want SageMaker to store checkpoints. For example, s3://bucket-name/key-name-prefix.
- local_path¶
- Type
(Optional) The local directory where checkpoints are written. The default directory is /opt/ml/checkpoints/.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.Compute(*, volume_size_in_gb=30, instance_type=<sagemaker_core.main.utils.Unassigned object>, instance_count=<sagemaker_core.main.utils.Unassigned object>, volume_kms_key_id=<sagemaker_core.main.utils.Unassigned object>, keep_alive_period_in_seconds=<sagemaker_core.main.utils.Unassigned object>, instance_groups=<sagemaker_core.main.utils.Unassigned object>, training_plan_arn=<sagemaker_core.main.utils.Unassigned object>, enable_managed_spot_training=None)¶
Compute.
The Compute class is a subclass of
sagemaker_core.shapes.ResourceConfig
and allows the user to specify the compute resources for the training job.- Parameters
instance_type (Optional[str]) – The ML compute instance type. For information about available instance types, see https://aws.amazon.com/sagemaker/pricing/.
instance_count (Optional[int]) – The number of ML compute instances to use. For distributed training, provide a value greater than 1.
volume_size_in_gb (Optional[int]) – The size of the ML storage volume that you want to provision. ML storage volumes store model artifacts and incremental states. Training algorithms might also use the ML storage volume for scratch space. Default: 30
volume_kms_key_id (Optional[str]) – The Amazon Web Services KMS key that SageMaker uses to encrypt data on the storage volume attached to the ML compute instance(s) that run the training job.
keep_alive_period_in_seconds (Optional[int]) – The duration of time in seconds to retain configured resources in a warm pool for subsequent training jobs.
instance_groups (Optional[List[InstanceGroup]]) – A list of instance groups for heterogeneous clusters to be used in the training job.
enable_managed_spot_training (Optional[bool]) – To train models using managed spot training, choose True. Managed spot training provides a fully managed and scalable infrastructure for training machine learning models. this option is useful when training jobs can be interrupted and when there is flexibility when the training job is run.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.Networking(*, security_group_ids, subnets, enable_network_isolation=None, enable_inter_container_traffic_encryption=None)¶
Networking.
The Networking class is a subclass of
sagemaker_core.shapes.VpcConfig
and allows the user to specify the networking configuration for the training job.- Parameters
security_group_ids (Optional[List[str]]) – The VPC security group IDs, in the form sg-xxxxxxxx. Specify the security groups for the VPC that is specified in the Subnets field.
subnets (Optional[List[str]]) – The ID of the subnets in the VPC to which you want to connect your training job or model.
enable_network_isolation (Optional[bool]) – Isolates the training container. No inbound or outbound network calls can be made, except for calls between peers within a training cluster for distributed training. If you enable network isolation for training jobs that are configured to use a VPC, SageMaker downloads and uploads customer data and model artifacts through the specified VPC, but the training container does not have network access.
enable_inter_container_traffic_encryption (Optional[bool]) – To encrypt all communications between ML compute instances in distributed training choose True. Encryption provides greater security for distributed training, but training might take longer. How long it takes depends on the amount of communication between compute instances, especially if you use a deep learning algorithm in distributed training.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'protected_namespaces': (), 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.configs.InputData(*, channel_name=None, data_source=None)¶
InputData.
This config allows the user to specify an input data source for the training job.
Will be found at
/opt/ml/input/data/<channel_name>
within the training container. For convience, can be referenced inside the training container like:import os input_data_dir = os.environ['SM_CHANNEL_<channel_name>']
- Parameters
channel_name (str) – The name of the input data source channel.
data_source (Union[str, S3DataSource, FileSystemDataSource]) – The data source for the channel. Can be an S3 URI string, local file path string, S3DataSource object, or FileSystemDataSource object.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Distributed¶
Distributed module.
- class sagemaker.modules.distributed.SMP(*, hybrid_shard_degree=None, sm_activation_offloading=None, activation_loading_horizon=None, fsdp_cache_flush_warnings=None, allow_empty_shards=None, tensor_parallel_degree=None, context_parallel_degree=None, expert_parallel_degree=None, random_seed=None)¶
SMP.
This class is used for configuring the SageMaker Model Parallelism v2 parameters. For more information on the model parallelism parameters, see: https://docs.aws.amazon.com/sagemaker/latest/dg/distributed-model-parallel-v2-reference.html#distributed-model-parallel-v2-reference-init-config
- Parameters
hybrid_shard_degree (Optional[int]) – Specifies a sharded parallelism degree for the model.
sm_activation_offloading (Optional[bool]) – Specifies whether to enable the SMP activation offloading implementation.
activation_loading_horizon (Optional[int]) – An integer specifying the activation offloading horizon type for FSDP. This is the maximum number of checkpointed or offloaded layers whose inputs can be in the GPU memory simultaneously.
fsdp_cache_flush_warnings (Optional[bool]) – Detects and warns if cache flushes happen in the PyTorch memory manager, because they can degrade computational performance.
allow_empty_shards (Optional[bool]) – Whether to allow empty shards when sharding tensors if tensor is not divisible. This is an experimental fix for crash during checkpointing in certain scenarios. Disabling this falls back to the original PyTorch behavior.
tensor_parallel_degree (Optional[int]) – Specifies a tensor parallelism degree. The value must be between 1 and world_size.
context_parallel_degree (Optional[int]) – Specifies the context parallelism degree. The value must be between 1 and world_size , and must be <= hybrid_shard_degree.
expert_parallel_degree (Optional[int]) – Specifies a expert parallelism degree. The value must be between 1 and world_size.
random_seed (Optional[int]) – A seed number for the random operations in distributed modules by SMP tensor parallelism or expert parallelism.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sagemaker.modules.distributed.DistributedConfig¶
Base class for distributed training configurations.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_dump(*args, **kwargs)¶
Dump the model to a dictionary.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_post_init(context, /)¶
This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since that’s what pydantic-core passes when calling it.
- Parameters
self (BaseModel) – The BaseModel instance.
context (Any) – The context.
- Return type
None
- class sagemaker.modules.distributed.Torchrun(*, process_count_per_node=None, smp=None)¶
Torchrun.
The Torchrun class configures a job that uses
torchrun
ortorch.distributed.launch
in the backend to launch distributed training.- Parameters
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_post_init(context, /)¶
We need to both initialize private attributes and call the user-defined model_post_init method.
- Parameters
self (BaseModel) –
context (Any) –
- Return type
None
- class sagemaker.modules.distributed.MPI(*, process_count_per_node=None, mpi_additional_options=None)¶
MPI.
The MPI class configures a job that uses
mpirun
in the backend to launch distributed training.- Parameters
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_post_init(context, /)¶
We need to both initialize private attributes and call the user-defined model_post_init method.
- Parameters
self (BaseModel) –
context (Any) –
- Return type
None