Based on the MLS-C01 exam guideReport an error
ShardedByS3Key only works when the data is split across multiple S3 objects. It has no effect on a single giant file.There are several ways for a SageMaker training job to bring data from S3 into the container. "How do you stream the data into the training container" directly affects performance and cost.
Downloads all of the S3 data to the container's EBS volume before training starts, and only then begins training.
Instead of downloading in advance, it streams directly from S3 during training.
A relatively newer mode that is a compromise between File mode's ease of use (access like POSIX files) and Pipe mode's immediate start. It fetches files by streaming when needed but lets you treat them like local files. Because it allows random access without waiting for a full download, it is often recommended for large datasets.
# Specify the input mode in a SageMaker Estimator (conceptual example)
from sagemaker.estimator import Estimator
from sagemaker.inputs import TrainingInput
estimator = Estimator(
image_uri=image,
role=role,
instance_count=1,
instance_type="ml.p3.2xlarge",
input_mode="Pipe", # "File" | "Pipe" | "FastFile"
)
train_input = TrainingInput(
s3_data="s3://ml-train/clicks/",
input_mode="FastFile", # the mode can be specified again per input
)
estimator.fit({"train": train_input})File mode is "download everything, then start," and Pipe mode is "start while downloading." The key tradeoff is startup delay vs random-access flexibility. If the data is hundreds of GB to TB and you worry about startup delay and disk cost, Pipe is better; if the data is small and read often and flexibly, File is better.
⚠️ Trap: The simplification "Pipe mode is always faster" is dangerous. Pipe is a sequential stream, so random access and shuffling are difficult and it requires a specific format. For small data or data that needs random access, File/FastFile is actually the better fit. You must weigh data size, access pattern, and format together.
When you want to cut the latency of direct S3 access even further for large-scale, repeated training, use Amazon FSx for Lustre. Lustre is a parallel file system for HPC (high-performance computing) that provides very high throughput and low latency.
Key features:
The classic situation where FSx for Lustre shines is when (1) the dataset is large, (2) it is read repeatedly across multiple training jobs and epochs, and (3) low latency matters. For one-off or small training jobs, direct S3 access (File/FastFile) is enough, and FSx's extra cost and setup are not justified.
| Option | Characteristics | Good fit |
|---|---|---|
| File mode | Download everything, then start | Small scale, repeated reads, simplicity first |
| Pipe mode | S3 streaming, starts immediately | Very large data, sequential processing, saving disk |
| FastFile mode | Streaming + POSIX access | Large data + convenient random access |
| FSx for Lustre | Ultra-fast parallel FS, S3 integration | Large scale + repeated + low latency + distributed |
| EFS | Managed NFS, multiple mounts | General-purpose data shared by notebooks and jobs |
When the data or the model does not fit on one machine, you use distributed training. The key is how you divide the work across multiple nodes.
| Type | What is split | When to use | Communication between nodes |
|---|---|---|---|
| Data parallelism | Split the data and replicate the model | Most cases, when the data is large | Gradient synchronization |
| Model parallelism | Split the model itself | When the model does not fit in GPU memory (large language models, etc.) | Passing activations between layers |
[Data parallelism]
Data ┌── Shard 1 ──▶ GPU1 (model replica) ─┐
├── Shard 2 ──▶ GPU2 (model replica) ─┼─▶ Gradient sync ─▶ Weight update
├── Shard 3 ──▶ GPU3 (model replica) ─┤
└── Shard 4 ──▶ GPU4 (model replica) ─┘
[Model parallelism]
Batch ──▶ GPU1(layers 1-4) ──▶ GPU2(layers 5-8) ──▶ GPU3(layers 9-12) ──▶ Output
In data-parallel training, it is efficient for each node to read only its own share of the data. SageMaker controls this with S3DataDistributionType.
# Specify sharding in distributed data-parallel training (conceptual example)
train_input = TrainingInput(
s3_data="s3://ml-train/clicks/",
distribution="ShardedByS3Key", # each instance receives only part of the data
)
estimator = Estimator(
image_uri=image, role=role,
instance_count=4, # distributed across 4 nodes
instance_type="ml.p3.16xlarge",
)
estimator.fit({"train": train_input})For ShardedByS3Key to be effective, the data must be split into multiple S3 objects (files). A single giant file cannot be split at the object level, so sharding has no effect at all. Therefore, for large data it is distributed-training friendly to pre-split it into multiple moderately sized files (for example, hundreds of Parquet/RecordIO shards).
⚠️ Trap: If you also split the validation set with
ShardedByS3Key, each node sees different validation data and the metrics become inconsistent. The validation channel is usually correct withFullyReplicated.
Distribution is not free. Adding nodes adds communication cost, and speed does not increase linearly with the number of nodes.
| Situation | Choice |
|---|---|
| The data is so large that one epoch takes too long | Data parallelism + sharding |
| The model does not fit in a single GPU's memory | Model parallelism |
| Training is long and you want to cut cost | Managed Spot training + checkpoints |
| Adding nodes does not make it faster | Check for communication/I/O bottlenecks |
A setup that runs long training on Spot instances without checkpoints starts over from the beginning when interrupted. Remember "cost savings" and "checkpoints" as a pair.
When a scenario says "training is slow," suspect the supply side first, not the computation.
ShardedByS3Key.🎯 Scenario: "We train data-parallel on several TB of data across 8 GPU nodes, but each node receives the entire dataset, so startup is too slow." → Split the data into multiple files and use
ShardedByS3Keyso each node receives only its own share. Duplicate downloads and duplicate processing disappear. If there are many repeated reads, attach FSx for Lustre as shared storage to cut latency further.
The same data strategy gives different results depending on the instance type.
| Instance family | Nature | Typical use |
|---|---|---|
ml.m5 / ml.c5 | General-purpose / compute-optimized CPU | Preprocessing, light training, batch transform |
ml.r5 | Memory-optimized | Preprocessing large DataFrames, memory-intensive work |
ml.p3 / ml.p4d | GPU | Deep learning training, large-scale distributed |
ml.g5 | Inference-friendly GPU | Inference, light training |
ml.inf family | Inference-only accelerator | Cost optimization for high-volume inference |
"Training is slow, so let's switch to a bigger GPU" is only right when GPU utilization is already high. If utilization is low and you only enlarge the GPU, the idle time just gets more expensive.
Tomorrow we tie all of Week 2 (transformation, pipelines, augmentation, storage optimization) together in a comprehensive review.
Click a choice to reveal the answer and explanation.
Question 1
You are using hundreds of GB of training data and want to reduce the wait for the full download before training starts and the EBS disk cost. You do not need much random access. Which input mode is most suitable?
Question 2
Which mode is suitable when you want a compromise between File mode's POSIX file access convenience and Pipe mode's immediate-start advantage, so that you stream a large dataset yet still access it randomly like local files?
Question 3
Which is the most correct situation in which Amazon FSx for Lustre is especially advantageous for accessing training data?
Question 4
When doing data-parallel training on 4 nodes, which distribution setting in SageMaker makes each node receive only its own share instead of downloading the entire data redundantly?
Question 5
What is the most correct precondition for ShardedByS3Key sharding to actually be effective?