Asynchronous FileSystemWriter Implementation
Storage writer for PyT Distributed format allowing asynchronous save.
- class nvidia_resiliency_ext.checkpointing.async_ckpt.filesystem_async.ConsistentDataIdentifier(key)[source]
Bases:
objectIdentifier for consistent data structure stored in worker cache.
This allows passing a lightweight identifier instead of pickling the entire data structure (which includes IPC handles) across process boundaries.
- Parameters:
key (str)
- class nvidia_resiliency_ext.checkpointing.async_ckpt.filesystem_async.FileSystemWriterAsync(path, *args, separation_hint=None, use_msc=False, is_multiproc_io=False, use_cached_data_structure=False, use_cpu_shm_for_gpu_tensors=False, msc_writer_options=None, **kwargs)[source]
Bases:
FileSystemWriterAsync-enabled implementation of FileSystemWriter using file I/O.
This class does not spawn the async process itself but relies on an external async mechanism.
Flow:
Call write_data
Externally start an async process with get_save_function_and_args and its arguments.
The async function calls write_preloaded_data_multithread, which writes the buckets on worker threads.
Once saving is finalized on all ranks, call super().finish with the results stored in self.writer_result.
Note: Step (3) can also be executed synchronously.
Currently, it is assumed that a separate writer is created for each ckpt save (intermediate state is stored as writer attributes).
With
use_msc=True,msc_writer_options(a picklablemultistorageclient.StreamingWriterOptions) is passed tomsc.openfor every shard, so shards stream through a bounded multipart upload instead of being buffered whole. The MSC profile must use a provider that supports streaming writes: in multi-storage-client 1.6.0, an S3-compatible provider with the Rust client enabled. Other providers, such as POSIX, raiseNotImplementedErrorwhen the first shard is opened.Initialize the writer pointing to path.
- Parameters:
path (str | PathLike) – directory where the checkpoint will be written to.
single_file_per_rank – Produce one file per rank instead of one file per tensor/blob. Default to True.
sync_files – force files to be synced to permanent storage. Default to True.
thread_count – Number of IO threads to use to write. Default to 1.
per_thread_copy_ahead – How many bytes to copy from the GPU ahead of saving them. Default 10Mb.
cache_staged_state_dict – Whether to cache the staged state_dict. This option decreases staging latency at the cost of increased memory usage. Additionally, if this parameter is set to True, it’s the expectation that the stager is maintained and reused for multiple dcp.async_save calls. Default to False.
overwrite – Whether to allow overwriting existing checkpoints. Defaults to True.
_extensions – Extensions to apply to output streams (EXPERIMENTAL)
separation_hint (str | None)
use_msc (bool)
is_multiproc_io (bool)
use_cached_data_structure (bool)
use_cpu_shm_for_gpu_tensors (bool)
msc_writer_options (StreamingWriterOptions | None)
If sync_files is disabled, there’s no guarantee that the checkpoint will be consistent in the case of a failure.
- property checkpoint_id: str | PathLike
return the checkpoint_id that will be used to save the checkpoint.
- classmethod cleanup_tensor_caches()[source]
Release training-side tensor caches and invalidate the identifier set.
Must be called whenever the worker process is restarted. A fresh worker has an empty
_worker_data_cache, so both paths need re-priming:CPU shm path (
use_cpu_shm_for_gpu_tensors=True): clears_shm_tensor_cacheso the next checkpoint re-allocates shm tensors and sends them to the worker instead of sendingNone(which would cause a worker-side cache miss).GPU IPC path (
use_cached_data_structure=True): clearing_cached_identifiersforces the next checkpoint to re-send the GPU tensor IPC handles rather than assuming the worker already has them.
- Return type:
None
- finish(metadata, results)[source]
Finish the checkpointing process by writing the global metadata file.
Follows
FileSystemWriter.finishof the torch versions in_metadata_pickler.TESTED_TORCH_VERSIONS, but writes.metadatawith_metadata_pickler.dump_metadata: the same pickle format aspickle.dump, written much faster. On other torch versions, or withNVRX_FAST_METADATA_PICKLE=0, it usesFileSystemWriter.finish(or, for MSC,pickle.dump).- Parameters:
metadata (Metadata) – metadata to save
results (List[List[WriteResult]]) – results to save
- Return type:
None
- get_save_function_and_args()[source]
Get function that saves the data to storage along with its arguments. Allows the external caller to apply the save function synchronously or asynchronously.
- Returns: None (if there is nothing to write on this rank) or a tuple of:
the function that saves the data.
the function that stages tensors to a destination for async checkpointing. This function should be self-contained.
arguments to that function in 1).
- static preload_tensors(resolved_plan_data, non_blocking=True)[source]
Creates write_buckets and preloads tensors to host memory.
This runs in the persistent worker process. Bucket creation is done here (not in prepare_write_data) so that cached tensor data stored in the worker process can be retrieved and reused without re-pickling.
- Parameters:
resolved_plan_data (Tuple) –
Tuple containing (checkpoint_dir, (identifier, data_structure)) where:
identifier: ConsistentDataIdentifier (caching) or None
data_structure: (separation_hint, cached_tensor_data, uncached_tensor_data, byte_io_data, thread_count, storage_plan)
non_blocking (bool, optional) – Enable pinned D2H memcpy. Default is True.
- Returns:
List of write buckets with tensors moved to CPU
- Return type:
List[WriteBucket]
- prepare_decentralized_global_plan(local_plan)[source]
Instead of assigning indices by plan order, uses PyT rank (same outcome).
- Parameters:
local_plan (SavePlan) – local plan to turn to a global plan (without interactions with other ranks)
- Returns:
- SavePlan - locally transformed plan equivalent to the plan that would be
created by the coordinator
- Return type:
- prepare_write_data(plan, planner)[source]
First stage of async saving. Resolve data and store in compact format.
Separates tensor data into cacheable and uncached groups based on the transfer mode. ByteIO data is always resolved fresh. Bucket creation is deferred to preload_tensors so that it can run in the persistent worker process and use the data cache.
- Parameters:
plan (SavePlan) – save plan generated by the PyT Distributed compatible planner
planner (SavePlanner) – save planner used to resolve the bytes and tensor data
- Return type:
None
Returns: None, but stores the resolved plan data in instance attributes
- classmethod register_shm_drain_callback(fn)[source]
Register (or clear with None) the blocking drain called before reusing shm tensors.
AsyncCallsQueue registers this when cpu_shm_mode=True so that prepare_write_data drains any in-flight write before overwriting shm tensors.
- Parameters:
fn (Callable[[], None] | None)
- Return type:
None
- retrieve_write_results()[source]
- Turn the latest dict including write results from self.results_queue
into a single results lists. Includes error check.
- Returns (Union(List[WriteResult], WRAPPED_EXCEPTION): the list of write results
from all local workers performing the save, or a WRAPPED_EXCEPTION if an exception was raised during the writing process.
- Return type:
List[WriteResult] | tuple[BaseException, StackSummary]
- classmethod validate_checkpoint_id(checkpoint_id)[source]
Validate the checkpoint_id that will be used to save the checkpoint.
This method is available in PyTorch 2.3 and above.
- write_data(plan, planner)[source]
Write all items from
plan.- Parameters:
plan (SavePlan)
planner (SavePlanner)
- Return type:
- static write_preloaded_data(transform_list, open_file, local_thread_idx, write_bucket, use_fsync, use_msc)[source]
Performs actual data saving to storage (used by worker threads in multithread mode).
- Parameters:
transform_list (List[_StorageWriterTransforms]) – streaming transforms list
open_file (Callable) – file open callable
local_thread_idx (int) – index of the worker thread that performs writing
write_bucket (WriteBucket) – data to write to storage
use_fsync (bool) – if True, calls os.fsync at the end of saving
use_msc (bool) – flag to indicate use of multi storage client for storage access
- Return type:
List[WriteResult]
Returns: the write results of this bucket. Raises if writing fails.
- static write_preloaded_data_multithread(transform_list, use_msc, open_file, rank, write_buckets, global_results_queue)[source]
Performs saving data to storage with one thread per write bucket, all running concurrently.
Uses threads (not processes) so that this can run safely inside a daemon process without spawning child processes.
Triggering GC during execution can lead to CUDA errors when tensors are shared. To prevent this, we disable the GC explicitly for this function with _disable_gc. The worker threads run inside it, so GC stays disabled until they have all finished.
- Parameters:
transform_list (List[_StorageWriterTransforms]) – streaming transforms list
use_msc (bool) – flag to indicate use of multi storage client for storage access
open_file (Callable) – file open callable
rank (int) – training rank
write_buckets (List[WriteBucket]) – write plan
global_results_queue (mp.Queue) – queue to send Dict[List[WriteResults]] (or an Exception) back to the main training process
- Return type:
None
Returns: None
- nvidia_resiliency_ext.checkpointing.async_ckpt.filesystem_async.get_write_results_queue(mp_mode='spawn')[source]
Get or create a multiprocessing queue for write results.
- Parameters:
mp_mode (str) – Multiprocessing context mode. Defaults to ‘spawn’.
- Returns:
Queue for collecting write results.
- Return type:
mp.Queue