Orbax Checkpoint

repository·main·Indexed 19 days ago

https://github.com/google/orbax

Orbax provides common checkpointing and persistence utilities for JAX users, supporting asynchronous checkpointing, custom types, and flexible storage formats. It features a modular configuration model in v1 that separates environment and I/O settings (via ocp.Context) from training lifecycle logic (via ocp.training.Checkpointer). The library includes policy-based systems for save decisions and checkpoint preservation, as well as an experimental Checkpoint Tiering Service (CTS) for managing data across multiple storage tiers.

Tokens
62.3K
Snippets
170
Records
296
Agent score
67%

What's inside Orbax

  1. Overview of Orbax

    main

    Orbax is a modular and customizable JAX checkpointing library designed for high-performance, large-scale model persistence and recovery. It provides a JAX-native approach to managing distributed array storage and checkpoint lifecycles.

    Key capabilities include:

    • Distributed Checkpointing: Unified API for single- and multi-process checkpointing, abstracting the complexities of persisting distributed arrays.
    • High Performance: Memory-efficient and fast checkpointing designed to minimize impact on training loops.
    • Lifecycle Management: Facilitates training loop integration through metadata management, garbage collection, and saving policies.
    • Advanced Workflows: Supports topology-agnostic loading (resharding), partial loading, and incremental saving.
    • Extensibility: Customizable handler interfaces for user-defined types and logic.
    • Model Exporting: Through the companion library orbax-export, JAX models can be exported to the TensorFlow SavedModel format.
  2. Overview of Orbax Checkpoint Tiering Service (CTS)

    main

    The Checkpoint Tiering Service (CTS) is an experimental feature in Orbax designed to manage machine learning checkpoints across multiple storage tiers, such as fast ephemeral storage and durable persistent storage. It provides APIs to coordinate the movement of checkpoint data and manage its lifecycle.

    Warning: This service is under heavy development. APIs, database schemas, and behaviors are unstable and subject to change without notice. It is not intended for general use and should only be used by those prepared for breaking changes.

  3. Migrate from Orbax v0 to v1 Checkpointing

    main

    Orbax v1 introduces a fundamental shift from flat configuration flags to structured policy composition.

    In v0, you passed independent arguments (like save_interval_steps or max_to_keep) to the CheckpointManager, which used internal priority logic to resolve conflicts. In v1, you must explicitly compose your logic using Policy Objects and pass them to the Checkpointer constructor.

    Key differences:

    • No Implicit Priority: In v1, the Checkpointer executes exactly what you compose. If you want multiple rules (e.g., 'save every 10 steps' AND 'save on preemption'), you must explicitly combine them using an AnyPolicy wrapper.
    • Explicit Composition: Instead of setting multiple flags, you construct a logic tree of policies.
    • Context vs. Checkpointer: Environmental/IO settings (like async or multiprocessing) move to ocp.Context, while training lifecycle logic (save/preserve rules) moves to the Checkpointer constructor.
  4. Configure checkpoint retention with PreservationPolicy

    main

    Orbax uses PreservationPolicy to manage which checkpoints are kept and which are deleted from storage. This prevents storage exhaustion by defining rules for how many or which specific checkpoints should be preserved based on time, training steps, or custom metrics.

    Policies can be combined using AnyPreservationPolicy to satisfy multiple conditions.

  5. Use Orbax Context for environment and I/O settings

    main

    The ocp.Context manages environment-specific and detailed I/O settings. It is intended to be set once for a training task and used as a context manager to apply settings globally to all operations within that scope.

    Key specialized options classes within Context include:

    • AsyncOptions: Configures asynchronous checkpoint saving, including timeout_secs and post-finalization callbacks.
    • FileOptions: Configures directory and file management (permissions, atomicity, custom paths).
    • MultiprocessingOptions: Configures multiprocessing behavior (primary host designation, active process subsets).
    • DeletionOptions: Configures checkpoint deletion behavior.
    • Component-Specific Options: Granular control for PyTreeOptions and ArrayOptions (storage formats, dtypes, concurrent I/O limits).
    • PathwaysOptions: Configures Pathways-specific saving and loading.
  6. Implement custom TypeHandlers

    main
    A TypeHandler is an abstraction used by Orbax to define how specific Python types or data structures are serialized to and deserialized from a checkpoint. If you have custom data types in your PyTree that are not covered by standard handlers, you can implement the TypeHandler interface to define custom saving and loading logic.
  7. Use the public `orbax.checkpoint` API instead of `_src`

    main
    The _src package is intended for internal implementations. Code within this directory is not part of the stable public API and should not be directly imported or relied upon by external users. To ensure compatibility and stability, always depend on symbols exported by the orbax.checkpoint package or other official subpackages.
  8. Configure checkpointing frequency with SaveDecisionPolicy

    main

    Orbax uses SaveDecisionPolicy to determine when a checkpoint should be saved during a training loop. Instead of manually checking conditions, you provide a policy to the checkpoint manager. The policy evaluates a DecisionContext (which contains information about the current step, etc.) to return a boolean indicating whether a save operation should proceed.

    Common implementations include:

    • FixedIntervalPolicy: Saves every $N$ steps.
    • SpecificStepsPolicy: Saves at a predefined list of step numbers.
    • ContinuousCheckpointingPolicy: Designed for continuous saving patterns.
    • PreemptionCheckpointingPolicy: Handles saving when a preemption signal (like a preemptible VM shutdown) is detected.
    • InitialSavePolicy: Ensures a save occurs at the very beginning.
    • AnySavePolicy: A catch-all policy.
    from orbax.checkpoint.checkpoint_managers import save_decision_policy
    
    # Example: Save every 100 steps
    policy = save_decision_policy.FixedIntervalPolicy(interval=100)
    # This policy is then passed to your CheckpointManager configuration
  9. Use CheckpointHandler for saving and restoring state

    main
    The CheckpointHandler is the base abstraction for managing checkpoint operations. It provides a unified interface for saving and restoring model states. Specialized handlers exist for different data types (PyTrees, JSON, Arrays, etc.) and execution modes (Synchronous vs. Asynchronous).
  10. Configure checkpoint preservation policies in Orbax

    main

    Orbax provides several preservation policies via the orbax.checkpoint.v1.training.preservation_policies module. These policies allow you to control which checkpoints are kept and which are deleted during training to manage storage space.

    Policies can be used individually or combined using AnyPreservationPolicy. Each policy implements a should_preserve method that evaluates a PreservationContext to decide if a checkpoint should be retained.