EFS FSx for ML
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Advanced Data Preparation: Leveraging Amazon EFS and FSx for Machine Learning
Introduction: The Data Bottleneck in Machine Learning
In the world of machine learning, we often spend a disproportionate amount of time talking about model architectures, hyperparameter tuning, and loss functions. However, experienced practitioners know that the true engine of any successful machine learning project is the data pipeline. When you scale your training jobs from a few gigabytes to hundreds of terabytes, the way you store, access, and feed data to your models becomes the primary factor in your training speed and cost.
If your training instances are spending more time waiting for data to download from a standard object store like Amazon S3 than they are performing matrix multiplications, you have a data bottleneck. This is where high-performance file systems like Amazon Elastic File System (EFS) and Amazon FSx for Lustre come into play. These services move beyond simple object storage, providing a POSIX-compliant file system interface that allows your training clusters to stream data with the low latency and high throughput required for modern deep learning. Understanding when to use EFS versus FSx, and how to configure them for your specific workloads, is a critical skill for any machine learning engineer.
Understanding the Storage Landscape for ML
To appreciate why EFS and FSx are necessary, we must first look at the limitations of standard storage solutions. Object storage, such as Amazon S3, is excellent for durability, cost-effectiveness, and massive capacity. However, it is an object store, not a file system. This means your training code cannot perform random reads or seek operations efficiently; it must download entire objects or use specific SDK calls to retrieve byte ranges.
When training large-scale models—such as those used in computer vision or natural language processing—your training instances often need to access millions of small files or perform frequent, random reads across massive datasets. If you try to do this over a standard network interface from object storage, the latency overhead of HTTP requests will cripple your training throughput.
Callout: Object Storage vs. File Systems Object storage (like S3) is designed for massive scale and durability but lacks the hierarchical file system semantics required for many high-performance computing tasks. File systems (like EFS or FSx) provide POSIX compliance, meaning they support standard file operations like
read,write,seek, andopen, allowing your training code to interact with data as if it were on a local disk drive, while still providing the scale of distributed network storage.
Amazon EFS: When Consistency and Simplicity Matter
Amazon Elastic File System (EFS) is a serverless, fully managed network file system. It is designed to be highly available and durable, providing a standard interface that multiple EC2 instances or containers can mount simultaneously.
The Use Case for EFS
EFS is best suited for scenarios where you have shared datasets that require concurrent access from multiple training nodes and where the data patterns involve a mix of read and write operations. Because EFS is POSIX-compliant, your existing data processing scripts written in Python, C++, or R will work without any modifications. You do not need to rewrite your data loading logic to use S3 SDKs; you simply mount the file system to a directory and treat it like local storage.
Performance Characteristics of EFS
EFS scales its throughput based on the amount of data stored in the file system. For larger datasets, EFS provides higher throughput levels. However, it is important to note that EFS is not designed for the extreme, sub-millisecond latency required by some high-frequency trading applications or massive parallel HPC workloads. It is ideal for:
- Shared model checkpoints and training logs.
- Datasets that are updated frequently by multiple preprocessing jobs.
- Standard deep learning workloads that require shared access to a dataset across a cluster of training instances.
Tip: Monitoring EFS Performance Always monitor the
PercentIOLimitmetric in CloudWatch when using EFS. If this metric stays high, you are hitting the performance limits of your provisioned throughput or your storage-based throughput, indicating that you may need to switch to "Provisioned Throughput" mode or migrate to a more performance-oriented service like FSx.
Amazon FSx for Lustre: The Speed Demon
When your machine learning workload requires extreme throughput and low latency, Amazon FSx for Lustre is the industry standard. Lustre is a high-performance, parallel file system that was originally designed for supercomputing environments. AWS has managed this technology, allowing you to spin up a high-performance file system in minutes.
Why FSx for Lustre is Different
FSx for Lustre is designed specifically for massive scale-out processing. It can provide hundreds of gigabytes per second of throughput and millions of IOPS. The way it achieves this is by striping data across multiple storage servers, allowing your training nodes to read different parts of the same file simultaneously from different servers.
The most powerful feature of FSx for Lustre for ML practitioners is its deep integration with Amazon S3. You can link an S3 bucket to an FSx file system. When you start your training job, FSx automatically pulls data from S3 into the file system on demand. This provides the durability of S3 with the performance of a high-speed parallel file system.
Choosing Between EFS and FSx for Lustre
Choosing between these two services depends entirely on your workload characteristics. Use the following table as a quick reference:
| Feature | Amazon EFS | Amazon FSx for Lustre |
|---|---|---|
| Primary Design | General-purpose, shared storage | High-performance, parallel computing |
| Throughput | Moderate; scales with storage | Extremely high; parallelized |
| Latency | Low (milliseconds) | Ultra-low (sub-millisecond) |
| S3 Integration | Manual data management | Native, automated data repository linking |
| Best For | Shared models, logs, small datasets | Massive training sets, heavy I/O workloads |
Implementing EFS in Your ML Workflow
Setting up EFS for a machine learning environment involves three primary steps: creating the file system, configuring the mount targets, and mounting the file system on your training nodes.
Step-by-Step: Mounting EFS on an EC2 Training Instance
- Create the EFS File System: Navigate to the EFS console in AWS, click "Create file system," and select your VPC. Ensure you enable "General Purpose" performance mode, which is optimal for most ML training tasks.
- Configure Security Groups: EFS requires that you allow inbound traffic on port 2049 (NFS) from the security groups associated with your training instances. This is a common point of failure—without the correct security group rule, your instances will hang indefinitely while trying to mount the drive.
- Install the EFS Helper: On your training instances (assuming Amazon Linux 2 or Ubuntu), install the Amazon EFS client.
# For Amazon Linux 2 sudo yum install -y amazon-efs-utils # For Ubuntu sudo apt-get update sudo apt-get install -y amazon-efs-utils - Mount the File System: Create a mount point and mount the EFS ID.
sudo mkdir /mnt/ml_data sudo mount -t efs -o tls fs-12345678:/ /mnt/ml_data
Warning: Data Security Always use the
tlsoption when mounting EFS. This ensures that the data in transit between your training instance and the EFS service is encrypted. Without this, your data is susceptible to interception within the VPC if your network is not fully isolated.
Implementing FSx for Lustre in Your ML Workflow
Using FSx for Lustre is slightly more involved but provides significant performance gains for large-scale training.
Linking S3 to FSx
The most efficient way to use FSx for ML is to link your existing S3 bucket containing your training data. When you create the FSx file system, you specify the S3 bucket path. Once created, you can mount the file system, and the data will appear as if it is already there.
# Example command to mount FSx for Lustre on an instance
sudo mount -t lustre -o relatime,flock fs-0123456789abcdef0.fsx.us-east-1.amazonaws.com@tcp:/fsx /mnt/fsx_data
Best Practices for FSx Data Loading
- Lazy Loading: When you link an S3 bucket to FSx, the data is not copied immediately. It is pulled into the file system the first time it is accessed. If you have a massive dataset, consider a "warm-up" pass through the data before your actual training begins to ensure the files are cached on the file system.
- Data Stripping: For very large files (e.g., multi-gigabyte video files), ensure your FSx configuration is set up to stripe data across multiple OSTs (Object Storage Targets) to maximize throughput.
- Auto-Export: If your model generates training outputs (like augmented images or intermediate feature maps) and you want them saved to S3, configure the FSx "Data Repository Association" to automatically export changes back to your S3 bucket.
Common Pitfalls and How to Avoid Them
Even with powerful tools like EFS and FSx, there are common traps that can derail a machine learning project.
1. The "Small File" Problem
Machine learning datasets often consist of millions of small files (e.g., individual JPEG images). If you are reading these files one by one, the metadata overhead can become significant.
- Solution: Use a data format like TFRecord (for TensorFlow) or WebDataset (for PyTorch). These formats pack thousands of small files into a single large binary file, which is much more efficient for file systems to stream.
2. Misconfigured Network Throughput
On cloud instances, network bandwidth is often capped based on the instance size. If you are using a small instance type but connecting to a high-performance FSx file system, your performance will be capped by the instance's network card, not the storage.
- Solution: Always check the network performance specification of your chosen EC2 instance (e.g.,
p4d.24xlargehas much higher network throughput thang4dn.xlarge).
3. Ignoring Data Locality
While EFS and FSx are network-attached, they still reside within a specific Availability Zone (AZ). If you mount an EFS volume from an instance in us-east-1a to an instance in us-east-1b, you will incur cross-AZ data transfer costs and higher latency.
- Solution: Always keep your training instances and your file system storage in the same Availability Zone.
Note: Cost Optimization EFS has an "Infrequent Access" (IA) storage class. If you have a large dataset that you are not accessing every day, move it to IA storage to save up to 90% on storage costs. FSx for Lustre also has a "Scratch" deployment type, which is cheaper but not persistent—use this for short-lived training jobs where data can be easily re-imported from S3.
Designing a Scalable Data Pipeline
To build a truly robust ML data pipeline, you should think of your storage as a tiered architecture:
- Long-term Storage (S3): The source of truth. All raw data, checkpoints, and logs are stored here for long-term durability.
- Processing/Training Storage (FSx/EFS): The high-performance buffer. You pull data here only when needed for training.
- Local Instance Storage (NVMe/SSD): The hot cache. For the absolute fastest training, copy the data from your file system to the local NVMe storage on the training node during the first epoch.
This tiered approach balances cost, durability, and speed. You only pay for the high-performance storage during the duration of the training job, and you rely on the cheaper S3 storage for the majority of the time.
Practical Code Example: Efficient Loading in PyTorch
When using a file system like FSx, your PyTorch DataLoader can be optimized to take advantage of the POSIX interface. Instead of downloading from S3, you read directly from the mount point.
import torch
from torch.utils.data import Dataset, DataLoader
import os
class FileSystemDataset(Dataset):
def __init__(self, root_dir):
self.root_dir = root_dir
self.file_list = [os.path.join(root_dir, f) for f in os.listdir(root_dir)]
def __len__(self):
return len(self.file_list)
def __getitem__(self, idx):
# Direct access to the POSIX file system
# No network SDK calls required
with open(self.file_list[idx], 'rb') as f:
data = f.read()
return data
# Usage
# Point the mount directory to your mounted FSx/EFS volume
dataset = FileSystemDataset(root_dir='/mnt/fsx_data/train_images')
dataloader = DataLoader(dataset, batch_size=64, num_workers=8)
By using standard Python open() calls, you bypass the overhead of the S3 API. The operating system handles the caching and pre-fetching of data from the network file system, which is significantly faster for random access patterns.
Advanced Configuration: Tuning the Mount Options
For high-performance Linux environments, the mount options you use can impact throughput. When mounting FSx for Lustre, consider the rsize and wsize options. These control the size of the read and write buffers.
# Example of tuned mount for high-performance training
sudo mount -t lustre -o relatime,rsize=1048576,wsize=1048576 fs-0123456789abcdef0.fsx.us-east-1.amazonaws.com@tcp:/fsx /mnt/fsx_data
The rsize and wsize values (in bytes) allow the operating system to request larger chunks of data in a single network packet, reducing the number of requests per second. This is particularly useful when reading large image files or serialized model weights.
Troubleshooting Performance Issues
If your training job is still slow, follow this troubleshooting checklist:
- Check CPU/Memory: Is your data loading code CPU-bound? Are you doing heavy image resizing or augmentation in the main thread? If so, increase
num_workersin yourDataLoader. - Check Network: Use
iftopornloadon your training instance to monitor the actual network throughput. If you are hitting the limit of the instance's network interface, you must upgrade your instance type. - Check File System IOPS: Use CloudWatch metrics to see if you are hitting the IOPS limit of your EFS or FSx file system. If you are, you may need to increase the provisioned throughput or add more storage capacity (for EFS).
- Check Data Layout: Are you reading thousands of tiny files? If so, convert them into a few large files (e.g., using
tarorTFRecord).
Summary of Key Takeaways
Mastering data preparation and storage for machine learning is what separates amateur experimentation from production-grade engineering. By understanding how to move beyond simple object storage and utilize high-performance file systems, you can significantly accelerate your training cycles and reduce overall project costs.
- Move Beyond S3 for Active Training: While S3 is perfect for long-term storage, high-performance file systems like EFS and FSx for Lustre are essential for high-throughput training loops.
- Choose the Right Tool: Use EFS for shared, general-purpose needs and FSx for Lustre when you require the extreme throughput and low latency of a parallel file system.
- Leverage Data Locality: Keep your storage and your compute in the same Availability Zone to minimize latency and avoid unnecessary data transfer costs.
- Optimize Data Formats: Avoid the "Small File Problem" by packing data into large binary formats like TFRecord or WebDataset, which are much more efficient for network file systems to stream.
- Monitor and Tune: Use CloudWatch to monitor throughput and IOPS, and tune your mount options (
rsize,wsize) to ensure your training nodes are getting the maximum performance from your file system. - Cost-Aware Design: Use storage tiers like EFS Infrequent Access or FSx Scratch deployment to balance your performance needs with your budget.
- POSIX is Your Friend: The primary advantage of EFS and FSx is their POSIX compliance, which allows you to use standard file I/O operations, simplifying your code and increasing portability.
By applying these principles, you will be able to build data pipelines that can handle the most demanding machine learning workloads, ensuring that your models are limited by your ingenuity, not your infrastructure.
Continue the course
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons