Skip to content

Commit

Permalink
[onert-micro] Introduce OMCheckpointLoader and OMCheckpointSaver (#13147
Browse files Browse the repository at this point in the history
)

This pr introduces OMCheckpointLoader and OMCheckpointSaver entities.

ONE-DCO-1.0-Signed-off-by: Artem Balyshev <[email protected]>
  • Loading branch information
BalyshevArtem authored Jun 14, 2024
1 parent 6f1d1d3 commit fdfb249
Show file tree
Hide file tree
Showing 4 changed files with 851 additions and 0 deletions.
65 changes: 65 additions & 0 deletions onert-micro/onert-micro/include/core/train/OMCheckpointLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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
*
* http://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 ONERT_MICRO_CORE_TRAIN_CHECKPOINT_LOADER_H
#define ONERT_MICRO_CORE_TRAIN_CHECKPOINT_LOADER_H

#include "OMStatus.h"
#include "OMConfig.h"
#include "core/OMRuntimeContext.h"
#include "core/OMRuntimeStorage.h"
#include "core/train/OMTrainingStorage.h"

namespace onert_micro
{
namespace core
{
namespace train
{

/*
* Class to load checkpoints files
* Note: class is stateless
*/
class OMCheckpointLoader
{
public:
OMCheckpointLoader() = default;
OMCheckpointLoader(const OMCheckpointLoader &) = delete;
OMCheckpointLoader(OMCheckpointLoader &&) = delete;
OMCheckpointLoader &operator=(const OMCheckpointLoader &) = delete;
OMCheckpointLoader &&operator=(const OMCheckpointLoader &&) = delete;
~OMCheckpointLoader() = default;

// Load and save states from checkpoint data in model and in config
// To check checkpoint file format please see https://github.com/Samsung/ONE/discussions/13037
static OMStatus loadCheckpointData(core::OMRuntimeContext &context,
OMTrainingStorage &train_storage, const char *data,
OMConfig &config);

private:
static OMStatus validateCheckpointData(core::OMRuntimeContext &context, const char *data,
OMConfig &config);

static OMStatus loadBuffers(core::OMRuntimeContext &context, OMTrainingStorage &train_storage,
const char *data, OMConfig &config);
};

} // namespace train
} // namespace core
} // namespace onert_micro

#endif // ONERT_MICRO_CORE_TRAIN_CHECKPOINT_LOADER_H
66 changes: 66 additions & 0 deletions onert-micro/onert-micro/include/core/train/OMCheckpointSaver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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
*
* http://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 ONERT_MICRO_CORE_TRAIN_CHECKPOINT_SAVER_H
#define ONERT_MICRO_CORE_TRAIN_CHECKPOINT_SAVER_H

#include "OMStatus.h"
#include "OMConfig.h"
#include "core/OMRuntimeContext.h"
#include "core/OMRuntimeStorage.h"
#include "core/train/OMTrainingStorage.h"

namespace onert_micro
{
namespace core
{
namespace train
{

/*
* Class to save checkpoints
* Note: class is stateless
*/
class OMCheckpointSaver
{
public:
OMCheckpointSaver() = default;
OMCheckpointSaver(const OMCheckpointSaver &) = delete;
OMCheckpointSaver(OMCheckpointSaver &&) = delete;
OMCheckpointSaver &operator=(const OMCheckpointSaver &) = delete;
OMCheckpointSaver &&operator=(const OMCheckpointSaver &&) = delete;
~OMCheckpointSaver() = default;

// Create checkpoint data for current state
// To check checkpoint file format please see https://github.com/Samsung/ONE/discussions/13037
static OMStatus createCheckpointData(core::OMRuntimeContext &context,
OMTrainingStorage &train_storage, std::vector<char> &data,
const OMConfig &config);

private:
static size_t calculateFileSize(core::OMRuntimeContext &context, OMTrainingStorage &train_storage,
const OMConfig &config);

static OMStatus writeOffsetsAndBuffers(core::OMRuntimeContext &context,
OMTrainingStorage &train_storage, const OMConfig &config,
std::vector<char> &data);
};

} // namespace train
} // namespace core
} // namespace onert_micro

#endif // ONERT_MICRO_CORE_TRAIN_CHECKPOINT_SAVER_H
Loading

0 comments on commit fdfb249

Please sign in to comment.