include/tenzr/modules/checkpoint/checkpoint_manager.h¶
Namespaces¶
| Name |
|---|
| tenzr |
| tenzr::checkpoint |
Classes¶
| Name | |
|---|---|
| struct | tenzr::checkpoint::CheckpointOptions Configuration for a CheckpointManager. |
| struct | tenzr::checkpoint::CheckpointMetadata Bookkeeping record for one saved checkpoint. |
| class | tenzr::checkpoint::CheckpointManager Saves and restores named state with retention and best-metric tracking. |
Source code¶
// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef TENZR_MODULES_CHECKPOINT_CHECKPOINT_MANAGER_H_
#define TENZR_MODULES_CHECKPOINT_CHECKPOINT_MANAGER_H_
#include <cstdint>
#include <future>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "tenzr/frontend/tensor.h"
#include "tenzr/modules/nn/module.h"
namespace tenzr::checkpoint {
struct CheckpointOptions {
std::string directory{"checkpoints"};
size_t max_to_keep{3};
bool keep_best{true};
bool higher_is_better{false};
std::string prefix{"step_"};
bool async{false};
};
struct CheckpointMetadata {
uint64_t step{0};
std::string path;
double metric{0.0};
bool has_metric{false};
uint64_t timestamp{0};
};
class CheckpointManager {
public:
explicit CheckpointManager(CheckpointOptions options = {});
~CheckpointManager();
CheckpointManager(const CheckpointManager&) = delete;
CheckpointManager& operator=(const CheckpointManager&) = delete;
CheckpointManager(CheckpointManager&&) noexcept;
CheckpointManager& operator=(CheckpointManager&&) noexcept;
void Save(uint64_t step, const std::unordered_map<std::string, Tensor>& state, std::optional<double> metric = std::nullopt);
void Save(uint64_t step, nn::Module& module, std::optional<double> metric = std::nullopt);
std::unordered_map<std::string, Tensor> Restore(uint64_t step);
std::unordered_map<std::string, Tensor> RestoreLatest();
std::unordered_map<std::string, Tensor> RestoreBest();
void Restore(uint64_t step, nn::Module& module);
void RestoreLatest(nn::Module& module);
void RestoreBest(nn::Module& module);
bool HasCheckpoints() const;
uint64_t LatestStep() const;
uint64_t BestStep() const;
std::vector<uint64_t> AllSteps() const;
void WaitUntilIdle();
const CheckpointOptions& Options() const { return options_; }
private:
void SaveInternal(uint64_t step, std::unordered_map<std::string, Tensor> state, std::optional<double> metric);
void ApplyRetentionPolicy();
void ScanExistingCheckpoints();
CheckpointOptions options_;
std::vector<CheckpointMetadata> checkpoints_;
std::optional<uint64_t> best_step_;
double best_metric_{0.0};
std::vector<std::future<void>> pending_tasks_;
};
} // namespace tenzr::checkpoint
#endif // TENZR_MODULES_CHECKPOINT_CHECKPOINT_MANAGER_H_