mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-30 20:18:08 +00:00
Training Documentation (#15612)
This commit is contained in:
parent
d00197aaa7
commit
5885abfb35
5 changed files with 510 additions and 155 deletions
1
.github/workflows/publish-c-apidocs.yml
vendored
1
.github/workflows/publish-c-apidocs.yml
vendored
|
|
@ -7,6 +7,7 @@ on:
|
|||
- main
|
||||
paths:
|
||||
- include/onnxruntime/core/session
|
||||
- orttraining/orttraining/training_api/include/
|
||||
schedule:
|
||||
- cron: '0 0 1 * *'
|
||||
workflow_dispatch:
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ DOXYFILE_ENCODING = UTF-8
|
|||
# title of most generated pages and in a few other places.
|
||||
# The default value is: My Project.
|
||||
|
||||
PROJECT_NAME = OnnxRuntime
|
||||
PROJECT_NAME = "ONNX Runtime"
|
||||
|
||||
# The PROJECT_NUMBER tag can be used to enter a project or revision number. This
|
||||
# could be handy for archiving the generated documentation or if some version
|
||||
|
|
@ -872,7 +872,9 @@ WARN_LOGFILE =
|
|||
# Note: If this tag is empty the current directory is searched.
|
||||
|
||||
INPUT = ../../include/onnxruntime/core/session/onnxruntime_c_api.h \
|
||||
../../include/onnxruntime/core/session/onnxruntime_cxx_api.h
|
||||
../../include/onnxruntime/core/session/onnxruntime_cxx_api.h \
|
||||
../../orttraining/orttraining/training_api/include/onnxruntime_training_c_api.h \
|
||||
../../orttraining/orttraining/training_api/include/onnxruntime_training_cxx_api.h
|
||||
|
||||
# This tag can be used to specify the character encoding of the source files
|
||||
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
|
||||
|
|
@ -2251,7 +2253,13 @@ SEARCH_INCLUDES = YES
|
|||
# preprocessor.
|
||||
# This tag requires that the tag SEARCH_INCLUDES is set to YES.
|
||||
|
||||
INCLUDE_PATH =
|
||||
# onnxruntime-training and onnxruntime core headers are in different directories.
|
||||
# Since the onnxruntime-training header file depends on macros defined in the
|
||||
# onnxruntime core header files, we need to INCLUDE_PATH to point to the path where
|
||||
# doxygen can find the onnxruntime core header files.
|
||||
# Else, doxygen will not be able to resolve the macros defined in the onnxruntime
|
||||
# core header files.
|
||||
INCLUDE_PATH = "../../include/onnxruntime/core/session/"
|
||||
|
||||
# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard
|
||||
# patterns (like *.h and *.hpp) to filter out the header-files in the
|
||||
|
|
|
|||
|
|
@ -3,8 +3,15 @@
|
|||
|
||||
// See docs\c_cxx\README.md on generating the Doxygen documentation from this file
|
||||
|
||||
/** \mainpage C & C++ APIs
|
||||
/** \mainpage ONNX Runtime
|
||||
*
|
||||
* ONNX Runtime is a high-performance inference and training graph execution engine for deeplearning models.
|
||||
*
|
||||
* ONNX Runtime's C, C++ APIs offer an easy to use interface to onboard and execute onnx models.
|
||||
* - \subpage c_cpp_api "Core C, C++ APIs"
|
||||
* - \subpage training_c_cpp_api "Training C, C++ APIs for learning on the edge"
|
||||
*
|
||||
* \page c_cpp_api Core C, C++ APIs
|
||||
* <h1>C</h1>
|
||||
*
|
||||
* ::OrtApi - Click here to go to the structure with all C API functions.
|
||||
|
|
@ -1347,7 +1354,7 @@ struct OrtApi {
|
|||
* \param[out] out Do not free this value, it will be valid until type_info is freed.
|
||||
* If type_info does not represent tensor, this value will be set to nullptr.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value. Always returns nullptr.
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*/
|
||||
ORT_API2_STATUS(CastTypeInfoToTensorInfo, _In_ const OrtTypeInfo* type_info,
|
||||
_Outptr_result_maybenull_ const OrtTensorTypeAndShapeInfo** out);
|
||||
|
|
@ -1859,7 +1866,7 @@ struct OrtApi {
|
|||
* \param[out] out A pointer to the ::OrtMapTypeInfo. Do not free this value. If type_info
|
||||
* does not contain a map, this value will be set to nullptr.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value. Always returns nullptr.
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*/
|
||||
ORT_API2_STATUS(CastTypeInfoToMapTypeInfo, _In_ const OrtTypeInfo* type_info,
|
||||
_Outptr_result_maybenull_ const OrtMapTypeInfo** out);
|
||||
|
|
@ -1875,7 +1882,7 @@ struct OrtApi {
|
|||
* \param[out] out A pointer to the OrtSequenceTypeInfo. Do not free this value. If type_info
|
||||
* doesn not contain a sequence, this value will be set to nullptr.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value. Always returns nullptr.
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*/
|
||||
ORT_API2_STATUS(CastTypeInfoToSequenceTypeInfo, _In_ const OrtTypeInfo* type_info,
|
||||
_Outptr_result_maybenull_ const OrtSequenceTypeInfo** out);
|
||||
|
|
@ -3580,19 +3587,26 @@ struct OrtApi {
|
|||
*/
|
||||
ORT_CLASS_RELEASE(KernelInfo);
|
||||
|
||||
/* \brief: Get the training C Api
|
||||
/// \name Ort Training
|
||||
/// @{
|
||||
/** \brief Gets the Training C Api struct
|
||||
*
|
||||
* Call this function to access the ::OrtTrainingApi structure that holds pointers to functions that enable
|
||||
* training with onnxruntime.
|
||||
* \note A NULL pointer will be returned and no error message will be printed if the training api
|
||||
* is not supported with this build. A NULL pointer will be returned and an error message will be
|
||||
* printed if the provided version is unsupported, for example when using a runtime older than the
|
||||
* version created with this header file.
|
||||
*
|
||||
* \param[in] version Must be ::ORT_API_VERSION
|
||||
* \return The ::OrtTrainingApi for the version requested.
|
||||
* nullptr will be returned and no error message will be printed if the training api is not supported with
|
||||
* this build.
|
||||
* nullptr will be returned and an error message will be printed if the provided version is unsupported, for
|
||||
* example when using a runtime older than the version created with this header file.
|
||||
* \return The ::OrtTrainingApi struct for the version requested.
|
||||
*
|
||||
* \since Version 1.13
|
||||
*/
|
||||
const OrtTrainingApi*(ORT_API_CALL* GetTrainingApi)(uint32_t version)NO_EXCEPTION;
|
||||
|
||||
/// @}
|
||||
|
||||
/** \brief Append CANN provider to session options
|
||||
*
|
||||
* If CANN is not available (due to a non CANN enabled build, or if CANN is not installed on the system), this function will return failure.
|
||||
|
|
@ -4125,7 +4139,7 @@ struct OrtApi {
|
|||
* it is owned by OrtTypeInfo instance. When the type_info does not represent
|
||||
* optional type, nullptr is returned in out.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value. Always returns nullptr.
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
* \since Version 1.15.
|
||||
*/
|
||||
|
|
@ -4144,7 +4158,7 @@ struct OrtApi {
|
|||
* \param[out] out A pointer to the ::OrtTypeInfo for what the optional value could be.
|
||||
* it is owned by OrtOptionalTypeInfo instance.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value.
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
* \since Version 1.15.
|
||||
*/
|
||||
|
|
@ -4157,7 +4171,7 @@ struct OrtApi {
|
|||
* \param[in] value A string tensor
|
||||
* \param[in] index - flat index of the element
|
||||
* \param[in] length_in_bytes length of the buffer in utf-8 bytes (without the null terminator)
|
||||
* \param[in/out] buffer - address of return value
|
||||
* \param[inout] buffer - address of return value
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*/
|
||||
|
|
@ -4169,7 +4183,7 @@ struct OrtApi {
|
|||
* \param[in] mem_info OrtMemoryInfo instance
|
||||
* \param[out] out A pointer to OrtAllocator.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value. Always returns nullptr.
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
* \since Version 1.15.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -7,23 +7,129 @@
|
|||
#include <stdbool.h>
|
||||
#include "onnxruntime_c_api.h"
|
||||
|
||||
ORT_RUNTIME_CLASS(TrainingSession); /// Type that enables performing training for the given user models.
|
||||
ORT_RUNTIME_CLASS(CheckpointState); /// Type that holds the training states for the training session.
|
||||
/** \page training_c_cpp_api Training C & C++ APIs
|
||||
*
|
||||
* Training C and C++ APIs are an extension of the \ref c_cpp_api "onnxruntime core C and C++ APIs" and should be used in conjunction with them.
|
||||
*
|
||||
* In order to train a model with onnxruntime, the following training artifacts must be generated:
|
||||
* - The training onnx model
|
||||
* - The checkpoint directory
|
||||
* - The optimizer onnx model
|
||||
* - The eval onnx model model (optional)
|
||||
*
|
||||
* These training artifacts can be generated as part of an offline step using the python [utilities](https://github.com/microsoft/onnxruntime/blob/main/orttraining/orttraining/python/training/onnxblock/README.md) made available in the `onnxruntime-training` python package.
|
||||
*
|
||||
* After these artifacts have been generated, the C and C++ utilities listed in this documentation can be leveraged to perform training.
|
||||
*
|
||||
* If any problem is encountered, please create an [issue](https://github.com/microsoft/onnxruntime/issues/new) with your scenario and requirements, and we will be sure to respond and follow up on the request.
|
||||
*
|
||||
* <h1>Training C API</h1>
|
||||
*
|
||||
* ::OrtTrainingApi - Training C API functions.
|
||||
*
|
||||
* This C structure contains functions that enable users to perform training with onnxruntime.
|
||||
*
|
||||
* _Sample Code_:
|
||||
*
|
||||
* ```c
|
||||
* #include <onnxruntime_training_api.h>
|
||||
*
|
||||
* OrtApi* g_ort_api = OrtGetApiBase()->GetApi(ORT_API_VERSION);
|
||||
* OrtTrainingApi* g_ort_training_api = g_ort_api->GetTrainingApi(ORT_API_VERSION);
|
||||
*
|
||||
* OrtEnv* env = NULL;
|
||||
* g_ort_api->CreateEnv(logging_level, logid, &env);
|
||||
* OrtSessionOptions* session_options = NULL;
|
||||
* g_ort_api->CreateSessionOptions(&session_options);
|
||||
*
|
||||
* OrtCheckpointState* state = NULL;
|
||||
* g_ort_training_api->LoadCheckpoint(path_to_checkpoint, &state);
|
||||
*
|
||||
* OrtTrainingSession* training_session = NULL;
|
||||
* g_ort_training_api->CreateTrainingSession(env, session_options, training_model_path,
|
||||
* state, eval_model_path, optimizer_model_path,
|
||||
* &training_session);
|
||||
* // Training loop
|
||||
* {
|
||||
* g_ort_training_api->TrainStep(...);
|
||||
* g_ort_training_api->OptimizerStep(...);
|
||||
* g_ort_training_api->LazyResetGrad(...);
|
||||
* }
|
||||
*
|
||||
* g_ort_training_api->ExportModelForInferencing(training_session, inference_model_path, ...);
|
||||
* g_ort_training_api->SaveCheckpoint(state, path_to_checkpoint, false);
|
||||
*
|
||||
* g_ort_training_api->ReleaseTrainingSession(training_session);
|
||||
* g_ort_training_api->ReleaseCheckpointState(state);
|
||||
* ```
|
||||
*
|
||||
* > **Note**
|
||||
* > The ::OrtCheckpointState contains the entire training state that the ::OrtTrainingSession uses. As a result, the training session must always have access to the state. That is to say, the ::OrtCheckpointState instance must outlive the lifetime of the ::OrtTrainingSession instance.
|
||||
*
|
||||
* <h1>Training C++ API</h1>
|
||||
*
|
||||
* @ref TrainingCpp - Training C++ API classes and functions.
|
||||
*
|
||||
* These C++ classes and functions enable users to perform training with onnxruntime.
|
||||
*
|
||||
* _Sample Code_:
|
||||
*
|
||||
* ```cc
|
||||
* #include <onnxruntime_training_cxx_api.h>
|
||||
*
|
||||
* Ort::Env env;
|
||||
* Ort::SessionOptions session_options;
|
||||
*
|
||||
* auto state = Ort::CheckpointState::LoadCheckpoint(path_to_checkpoint);
|
||||
* auto training_session = Ort::TrainingSession(env, session_options, state, training_model_path,
|
||||
* eval_model_path, optimizer_model_path);
|
||||
*
|
||||
* // Training Loop
|
||||
* {
|
||||
* training_session.TrainStep(...);
|
||||
* training_session.OptimizerStep(...);
|
||||
* training_session.LazyResetGrad(...);
|
||||
* }
|
||||
*
|
||||
* training_session->ExportModelForInferencing(inference_model_path, ...);
|
||||
* Ort::CheckpointState::SaveCheckpoint(state, path_to_checkpoint, false);
|
||||
* ```
|
||||
* > **Note**
|
||||
* > The ::Ort::CheckpointState contains the entire training state that the ::Ort::TrainingSession uses. As a result, the training session must always have access to the state. That is to say, the ::Ort::CheckpointState instance must outlive the lifetime of the ::Ort::TrainingSession instance.
|
||||
*/
|
||||
|
||||
/** @defgroup TrainingC Ort Training C API
|
||||
* @{
|
||||
*/
|
||||
ORT_RUNTIME_CLASS(TrainingSession); // Type that enables performing training for the given user models.
|
||||
ORT_RUNTIME_CLASS(CheckpointState); // Type that holds the training states for the training session.
|
||||
|
||||
/** \brief Type of property to be added to or returned from the ::OrtCheckpointState.
|
||||
*/
|
||||
typedef enum OrtPropertyType {
|
||||
OrtIntProperty = 0,
|
||||
OrtFloatProperty = 1,
|
||||
OrtStringProperty = 2,
|
||||
} OrtPropertyType;
|
||||
|
||||
/** \brief The Training C API that holds onnxruntime training function pointers
|
||||
*
|
||||
* All the Training C API functions are defined inside this structure as pointers to functions.
|
||||
* Call OrtApi::GetTrainingApi to get a pointer to this struct.
|
||||
*
|
||||
* \nosubgrouping
|
||||
*/
|
||||
struct OrtTrainingApi {
|
||||
/// \name Accessing The Training Session State
|
||||
/// @{
|
||||
|
||||
/** \brief Load a checkpoint state from directory on disk into checkpoint_state.
|
||||
*
|
||||
* This function will parse a checkpoint directory, pull relevant files and load the training
|
||||
* state into the checkpoint_state. This checkpoint state can then be used to create the
|
||||
* training session by invoking CreateTrainingSession. By doing so, the training session will resume
|
||||
* training from the given checkpoint state.
|
||||
* Note that the training session created with a checkpoint state uses this state to store the entire
|
||||
* training session by invoking OrtTrainingApi::CreateTrainingSession. By doing so, the training
|
||||
* session will resume training from the given checkpoint state.
|
||||
* \note Note that the training session created with a checkpoint state uses this state to store the entire
|
||||
* training state (including model parameters, its gradients, the optimizer states and the properties).
|
||||
* As a result, it is required that the checkpoint state outlive the lifetime of the training session.
|
||||
*
|
||||
|
|
@ -39,7 +145,8 @@ struct OrtTrainingApi {
|
|||
/** \brief Save the given state to a checkpoint directory on disk.
|
||||
*
|
||||
* This function serializes the provided checkpoint state to a directory on disk.
|
||||
* This checkpoint can later be loaded by invoking LoadCheckpoint to continue the training with the same state.
|
||||
* This checkpoint can later be loaded by invoking OrtTrainingApi::LoadCheckpoint to resume
|
||||
* training from this snapshot of the state.
|
||||
*
|
||||
* \param[in] checkpoint_state The checkpoint state to save.
|
||||
* \param[in] checkpoint_path Path to the checkpoint directory.
|
||||
|
|
@ -51,19 +158,30 @@ struct OrtTrainingApi {
|
|||
ORT_API2_STATUS(SaveCheckpoint, _In_ OrtCheckpointState* checkpoint_state, _In_ const ORTCHAR_T* checkpoint_path,
|
||||
const bool include_optimizer_state);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Implementing The Training Loop
|
||||
/// @{
|
||||
/** \brief Create a training session that can be used to begin or resume training.
|
||||
*
|
||||
* This function creates a training session based on the env and session options provided that can
|
||||
* begin or resume training from a given checkpoint state for the given onnx models.
|
||||
* The checkpoint state represents the parameters of the training session which will be moved
|
||||
* to the device specified by the user through the session options (if necessary).
|
||||
* The training session requires four training artifacts
|
||||
* - The training onnx model
|
||||
* - The evaluation onnx model (optional)
|
||||
* - The optimizer onnx model
|
||||
* - The checkpoint directory
|
||||
*
|
||||
* These artifacts can be generated using the `onnxruntime-training` python [utility](https://github.com/microsoft/onnxruntime/blob/main/orttraining/orttraining/python/training/onnxblock/README.md).
|
||||
*
|
||||
* \param[in] env Environment to be used for the training session.
|
||||
* \param[in] options Session options that the user can customize for this training session.
|
||||
* \param[in] checkpoint_state Training states that the training session uses as a starting point for training.
|
||||
* \param[in] train_model_path Model to be used to perform training that can be generated using the offline tooling library.
|
||||
* \param[in] eval_model_path Model to be used to perform evaluation that can be generated using the offline tooling library.
|
||||
* \param[in] optimizer_model_path Model to be used to the optimizer step for weight updates. The model can be generated using the offline tooling library.
|
||||
* \param[in] train_model_path Model to be used to perform training.
|
||||
* \param[in] eval_model_path Model to be used to perform evaluation.
|
||||
* \param[in] optimizer_model_path Model to be used to perform gradient descent.
|
||||
* \param[out] out Created training session.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
|
|
@ -74,12 +192,17 @@ struct OrtTrainingApi {
|
|||
_In_ const ORTCHAR_T* eval_model_path, _In_ const ORTCHAR_T* optimizer_model_path,
|
||||
_Outptr_ OrtTrainingSession** out);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Model IO Information
|
||||
/// @{
|
||||
|
||||
/** \brief Retrieves the number of user outputs in the training model.
|
||||
*
|
||||
* This function returns the number of outputs of the training model so that the user can
|
||||
* allocate space for the number of outputs when TrainStep is invoked.
|
||||
* allocate space for the number of outputs when OrtTrainingApi::TrainStep is invoked.
|
||||
*
|
||||
* \param[in] sess The training session which owns the training model.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[out] out Number of user outputs in the training model.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
|
|
@ -90,9 +213,9 @@ struct OrtTrainingApi {
|
|||
/** \brief Retrieves the number of user outputs in the eval model.
|
||||
*
|
||||
* This function returns the number of outputs of the eval model so that the user can
|
||||
* allocate space for the number of outputs when EvalStep is invoked.
|
||||
* allocate space for the number of outputs when OrtTrainingApi::EvalStep is invoked.
|
||||
*
|
||||
* \param[in] sess The training session which owns the eval model.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[out] out Number of user outputs in the eval model.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
|
|
@ -100,32 +223,66 @@ struct OrtTrainingApi {
|
|||
*/
|
||||
ORT_API2_STATUS(TrainingSessionGetEvalModelOutputCount, _In_ const OrtTrainingSession* sess, _Out_ size_t* out);
|
||||
|
||||
/** \brief Retrieves the names of user outputs in the training model.
|
||||
*
|
||||
* This function returns the names of outputs of the training model that can be associated with the OrtValue(s)
|
||||
* returned by the OrtTrainingApi::TrainStep function.
|
||||
*
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] index Index of the output name requested.
|
||||
* \param[in] allocator Allocator to use to allocate the memory for the name.
|
||||
* \param[out] output Name of the training model output at the given index.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
*/
|
||||
ORT_API2_STATUS(TrainingSessionGetTrainingModelOutputName, _In_ const OrtTrainingSession* sess, size_t index, _Inout_ OrtAllocator* allocator, _Outptr_ char** output);
|
||||
|
||||
/** \brief Retrieves the names of user outputs in the eval model.
|
||||
*
|
||||
* This function returns the names of outputs of the eval model that can be associated with the OrtValue(s) returned
|
||||
* by the OrtTrainingApi::EvalStep function.
|
||||
*
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] index Index of the output name requested.
|
||||
* \param[in] allocator Allocator to use to allocate the memory for the name.
|
||||
* \param[out] output Name of the eval model output at the given index.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
*/
|
||||
ORT_API2_STATUS(TrainingSessionGetEvalModelOutputName, _In_ const OrtTrainingSession* sess, size_t index, _Inout_ OrtAllocator* allocator, _Outptr_ char** output);
|
||||
|
||||
/** \brief Reset the training model gradients to zero lazily.
|
||||
/// @}
|
||||
|
||||
/// \name Implementing The Training Loop
|
||||
/// @{
|
||||
|
||||
/** \brief Reset the gradients of all trainable parameters to zero lazily.
|
||||
*
|
||||
* This function sets the internal state of the training session such that the training model gradients
|
||||
* will be scheduled to be reset just before the new gradients are computed on the next invocation
|
||||
* of TrainStep.
|
||||
* This function sets the internal state of the training session such that the gradients of the trainable
|
||||
* parameters in the OrtCheckpointState will be scheduled to be reset just before the new gradients are
|
||||
* computed on the next invocation of the next OrtTrainingApi::TrainStep.
|
||||
*
|
||||
* \param[in] session The training session which owns the eval model.
|
||||
* \param[in] session The `this` pointer to the training session.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
*/
|
||||
ORT_API2_STATUS(LazyResetGrad, _Inout_ OrtTrainingSession* session);
|
||||
|
||||
/** \brief Computes the outputs and the gradients for the training model for the given inputs
|
||||
/** \brief Computes the outputs of the training model and the gradients of the trainable parameters for the given inputs
|
||||
*
|
||||
* This function performs a training step that computes the outputs and the gradients of the training model
|
||||
* for the given inputs. The train step is performed based on the training model that was provided
|
||||
* to the training session.
|
||||
* The gradients computed are stored inside the training session so they can be later consumed
|
||||
* by the OptimizerStep function.
|
||||
* This function performs a training step that computes the outputs of the training model and the gradients
|
||||
* of the trainable parameters for the given inputs. The train step is performed based on the training model
|
||||
* that was provided to the training session.
|
||||
* The OrtTrainingApi::TrainStep is equivalent of running forward propagation and backward propagation in a single
|
||||
* step.
|
||||
* The gradients computed are stored inside the training session state so they can be later consumed
|
||||
* by the OrtTrainingApi::OptimizerStep function.
|
||||
* The gradients can be lazily reset by invoking the OrtTrainingApi::LazyResetGrad function.
|
||||
*
|
||||
* \param[in] sess The training session which owns the eval model.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] run_options Run options for this training step.
|
||||
* \param[in] inputs_len Number of user inputs to the training model.
|
||||
* \param[in] inputs The user inputs to the training model.
|
||||
|
|
@ -144,7 +301,7 @@ struct OrtTrainingApi {
|
|||
* This function performs an eval step that computes the outputs of the eval model for the given inputs.
|
||||
* The eval step is performed based on the eval model that was provided to the training session.
|
||||
*
|
||||
* \param[in] sess The training session which owns the eval model.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] run_options Run options for this eval step.
|
||||
* \param[in] inputs_len Number of user inputs to the eval model.
|
||||
* \param[in] inputs The user inputs to the eval model.
|
||||
|
|
@ -164,14 +321,14 @@ struct OrtTrainingApi {
|
|||
* learning rate is maintained by the training session and can be overwritten by invoking
|
||||
* this function with the desired learning rate. This function should not be used when a valid
|
||||
* learning rate scheduler is registered. It should be used either to set the learning rate
|
||||
* derived from a custom learning rate scheduler or to set the learning rate constant to be used
|
||||
* derived from a custom learning rate scheduler or to set a constant learning rate to be used
|
||||
* throughout the training session.
|
||||
* Please note that this function does not set the initial learning rate that may be needed
|
||||
* \note Please note that this function does not set the initial learning rate that may be needed
|
||||
* by the predefined learning rate schedulers. To set the initial learning rate for learning
|
||||
* rate schedulers, please look at the function `RegisterLinearLRScheduler`.
|
||||
* rate schedulers, please look at the function OrtTrainingApi::RegisterLinearLRScheduler.
|
||||
*
|
||||
* \param[in] sess The training session on which the learning rate needs to be set.
|
||||
* \param[in] learning_rate Desired learning rate to set.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] learning_rate Desired learning rate to be set.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
|
|
@ -181,9 +338,11 @@ struct OrtTrainingApi {
|
|||
/** \brief Gets the current learning rate for this training session.
|
||||
*
|
||||
* This function allows users to get the learning rate for the training session. The current
|
||||
* learning rate is maintained by the training session
|
||||
* learning rate is maintained by the training session, and users can query it for the purpose
|
||||
* of implementing their own learning rate schedulers.
|
||||
*
|
||||
* \param[in] sess The training session on which the learning rate needs to be set.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[out] learning_rate Learning rate currently in use by the training session.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
|
|
@ -193,13 +352,13 @@ struct OrtTrainingApi {
|
|||
/** \brief Performs the weight updates for the trainable parameters using the optimizer model.
|
||||
*
|
||||
* This function performs the weight update step that updates the trainable parameters such that they
|
||||
* take a step in the direction of their gradients. The optimizer step is performed based on the optimizer
|
||||
* model that was provided to the training session.
|
||||
* The updated parameters are stored inside the training session so that they can be used by the next
|
||||
* TrainStep function call.
|
||||
* take a step in the direction of their gradients (gradient descent). The optimizer step is performed
|
||||
* based on the optimizer model that was provided to the training session.
|
||||
* The updated parameters are stored inside the training state so that they can be used by the next
|
||||
* OrtTrainingApi::TrainStep function call.
|
||||
*
|
||||
* \param[in] sess The training session which owns the optimizer model.
|
||||
* \param[in] run_options Run options for this eval step.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] run_options Run options for this optimizer step.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
|
|
@ -207,14 +366,14 @@ struct OrtTrainingApi {
|
|||
ORT_API2_STATUS(OptimizerStep, _Inout_ OrtTrainingSession* sess,
|
||||
_In_opt_ const OrtRunOptions* run_options);
|
||||
|
||||
/** \brief Registers a Linear learning rate scheduler for the training session.
|
||||
/** \brief Registers a linear learning rate scheduler for the training session.
|
||||
*
|
||||
* Register a linear learning rate scheduler that decays the learning rate by linearly updated
|
||||
* multiplicative factor from the initial learning rate set on the training session to 0. The decay
|
||||
* is performed after the initial warm up phase where the learning rate is linearly incremented
|
||||
* from 0 to the initial learning rate provided.
|
||||
*
|
||||
* \param[in] sess The training session that should use the linear learning rate scheduler.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] warmup_step_count Warmup steps for LR warmup.
|
||||
* \param[in] total_step_count Total step count.
|
||||
* \param[in] initial_lr The initial learning rate to be used by the training session.
|
||||
|
|
@ -230,40 +389,45 @@ struct OrtTrainingApi {
|
|||
* Takes a scheduler step that updates the learning rate that is being used by the training session.
|
||||
* This function should typically be called before invoking the optimizer step for each round,
|
||||
* or as determined necessary to update the learning rate being used by the training session.
|
||||
* Please note that a valid predefined learning rate scheduler must be first registered to invoke this
|
||||
* \note Please note that a valid predefined learning rate scheduler must be first registered to invoke this
|
||||
* function.
|
||||
*
|
||||
* \param[in] sess The training session that has the registered learning rate scheduler.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
*/
|
||||
ORT_API2_STATUS(SchedulerStep, _Inout_ OrtTrainingSession* sess);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Accessing The Training Session State
|
||||
/// @{
|
||||
/** \brief Retrieves the size of all the parameters.
|
||||
*
|
||||
* Calculates the size of all the parameters for the training session.
|
||||
* When 'trainable_only' is true, the size is calculated for trainable params only.
|
||||
* Calculates the total number of primitive (datatype of the parameters) elements of all the parameters in the
|
||||
* training state.
|
||||
* When trainable_only argument is true, the size is calculated for trainable params only.
|
||||
*
|
||||
* \param[in] sess The training session.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[out] out Size of all parameter elements.
|
||||
* \param[in] trainable_only Whether to skip non-trainable parameters
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
*/
|
||||
ORT_API2_STATUS(GetParametersSize, _Inout_ OrtTrainingSession* sess,
|
||||
_Out_ size_t* out, bool trainable_only);
|
||||
ORT_API2_STATUS(GetParametersSize, _Inout_ OrtTrainingSession* sess, _Out_ size_t* out, bool trainable_only);
|
||||
|
||||
/** \brief Copy parameters onto contiguous buffer held by parameters_buffer
|
||||
/** \brief Copy all parameters to a contiguous buffer held by the argument parameters_buffer
|
||||
*
|
||||
* The parameters_buffer has to be of the size given by GetParametersSize api call,
|
||||
* with matching setting for 'trainable_only'. All the target parameters must be of the same
|
||||
* with matching setting for the argument trainable_only. All the target parameters must be of the same
|
||||
* datatype. The OrtValue must be pre-allocated onto
|
||||
* the desired device. This is a complementary function to 'CopyBufferToParameters'.
|
||||
* the desired device. This is a complementary function to OrtTrainingApi::CopyBufferToParameters.
|
||||
* Parameter ordering is preserved.
|
||||
* User is responsible for allocating/freeing the 'parameters_buffer'.
|
||||
* User is responsible for allocating and freeing the resources used by the parameters_buffer.
|
||||
*
|
||||
* \param[in] sess The training session.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] trainable_only Whether to skip non-trainable parameters
|
||||
* \param[out] parameters_buffer The pre-allocated OrtValue buffer to copy onto.
|
||||
*
|
||||
|
|
@ -273,16 +437,16 @@ struct OrtTrainingApi {
|
|||
ORT_API2_STATUS(CopyParametersToBuffer, _Inout_ OrtTrainingSession* sess,
|
||||
_Inout_ OrtValue* parameters_buffer, bool trainable_only);
|
||||
|
||||
/** \brief Copy parameter values from contiguous buffer held by parameters_buffer onto parameters
|
||||
/** \brief Copy parameter values from the given contiguous buffer held by parameters_buffer to the training state
|
||||
*
|
||||
* The parameters_buffer has to be of the size given by GetParametersSize api call,
|
||||
* with matching setting for 'trainable_only'. All the target parameters must be of the same
|
||||
* datatype. This is a complementary function to 'CopyBufferToParameters'
|
||||
* and can be used to load updated buffer values onto the parameters.
|
||||
* The parameters_buffer argument has to be of the size given by OrtTrainingApi::GetParametersSize api call,
|
||||
* with matching setting for trainable_only argument. All the target parameters must be of the same
|
||||
* datatype. This is a complementary function to OrtTrainingApi::CopyBufferToParameters
|
||||
* and can be used to load updated buffer values onto the training state.
|
||||
* Parameter ordering is preserved.
|
||||
* User is responsible for allocating/freeing the 'parameters_buffer'.
|
||||
* User is responsible for allocating and freeing the resources used by the parameters_buffer.
|
||||
*
|
||||
* \param[in] sess The training session.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] trainable_only Whether to skip non-trainable parameters
|
||||
* \param[out] parameters_buffer The pre-allocated OrtValue buffer to copy from.
|
||||
*
|
||||
|
|
@ -292,6 +456,11 @@ struct OrtTrainingApi {
|
|||
ORT_API2_STATUS(CopyBufferToParameters, _Inout_ OrtTrainingSession* sess,
|
||||
_Inout_ OrtValue* parameters_buffer, bool trainable_only);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Release Training Resources
|
||||
/// @{
|
||||
|
||||
/** \brief Frees up the memory used up by the training session.
|
||||
*
|
||||
* This function frees up any memory that was allocated in the training session. The training
|
||||
|
|
@ -304,21 +473,28 @@ struct OrtTrainingApi {
|
|||
*
|
||||
* This function frees up any memory that was allocated in the checkpoint state. The checkpoint
|
||||
* state can no longer be used after this call.
|
||||
* \note Note that the checkpoint state must be released only after the training session has been released.
|
||||
*
|
||||
*/
|
||||
ORT_CLASS_RELEASE(CheckpointState);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Prepare For Inferencing
|
||||
/// @{
|
||||
/** \brief Export a model that can be used for inferencing.
|
||||
*
|
||||
* If the training session was provided with an eval model, the training session can generate
|
||||
* an inference model if it knows the inference graph outputs. The input inference graph outputs
|
||||
* are used to prune the eval model so that the output model's outputs align with the provided outputs.
|
||||
* are used to prune the eval model so that the inference model's outputs align with the provided outputs.
|
||||
* The exported model is saved at the path provided and can be used for inferencing with InferenceSession.
|
||||
* Note that the function re-loads the eval model from the path provided to CreateTrainingSession and expects
|
||||
* that this path still be valid.
|
||||
* \note Note that the function re-loads the eval model from the path provided to OrtTrainingApi::CreateTrainingSession
|
||||
* and expects that this path still be valid.
|
||||
*
|
||||
* \param[in] sess The training session.
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] inference_model_path Path where the inference model should be serialized to.
|
||||
* \param[in] graph_outputs_len Size of the graph output names array.
|
||||
* \param[in] graph_output_names Names of the outputs that are needed in the inference model.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
|
|
@ -327,9 +503,14 @@ struct OrtTrainingApi {
|
|||
_In_ const ORTCHAR_T* inference_model_path, size_t graph_outputs_len,
|
||||
_In_reads_(graph_outputs_len) const char* const* graph_output_names);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Training Utilities
|
||||
/// @{
|
||||
/** \brief Sets the seed used for random number generation in Onnxruntime.
|
||||
*
|
||||
* Use this to get deterministic results.
|
||||
* Use this function to generate reproducible results. It should be noted that completely reproducible
|
||||
* results are not guaranteed.
|
||||
*
|
||||
* \param[in] seed The seed to be set.
|
||||
*
|
||||
|
|
@ -338,9 +519,16 @@ struct OrtTrainingApi {
|
|||
*/
|
||||
ORT_API2_STATUS(SetSeed, _In_ const int64_t seed);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Model IO Information
|
||||
/// @{
|
||||
/** \brief Retrieves the number of user inputs in the training model.
|
||||
*
|
||||
* \param[in] sess The training session which owns the training model.
|
||||
* This function returns the number of inputs of the training model so that the user can accordingly
|
||||
* allocate the OrtValue(s) provided to the OrtTrainingApi::TrainStep function.
|
||||
*
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[out] out Number of user inputs in the training model.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
|
|
@ -350,7 +538,10 @@ struct OrtTrainingApi {
|
|||
|
||||
/** \brief Retrieves the number of user inputs in the eval model.
|
||||
*
|
||||
* \param[in] sess The training session which owns the eval model.
|
||||
* This function returns the number of inputs of the eval model so that the user can accordingly
|
||||
* allocate the OrtValue(s) provided to the OrtTrainingApi::EvalStep function.
|
||||
*
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[out] out Number of user inputs in the eval model.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
|
|
@ -360,8 +551,13 @@ struct OrtTrainingApi {
|
|||
|
||||
/** \brief Retrieves the name of the user input at given index in the training model.
|
||||
*
|
||||
* \param[in] sess The training session which owns the training model.
|
||||
* \param[out] out Number of user inputs in the eval model.
|
||||
* This function returns the names of inputs of the training model that can be associated with the
|
||||
* OrtValue(s) provided to the OrtTrainingApi::TrainStep function.
|
||||
*
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] index The index of the training model input name requested.
|
||||
* \param[in] allocator The allocator to use to allocate the memory for the requested name.
|
||||
* \param[out] output Name of the user input for the training model at the given index.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
|
|
@ -371,8 +567,13 @@ struct OrtTrainingApi {
|
|||
|
||||
/** \brief Retrieves the name of the user input at given index in the eval model.
|
||||
*
|
||||
* \param[in] sess The training session which owns the training model.
|
||||
* \param[out] out Number of user inputs in the eval model.
|
||||
* This function returns the names of inputs of the eval model that can be associated with the OrtValue(s) provided
|
||||
* to the OrtTrainingApi::EvalStep function.
|
||||
*
|
||||
* \param[in] sess The `this` pointer to the training session.
|
||||
* \param[in] index The index of the eval model input name requested.
|
||||
* \param[in] allocator The allocator to use to allocate the memory for the requested name.
|
||||
* \param[out] output Name of the user input for the eval model at the given index.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
|
|
@ -380,6 +581,11 @@ struct OrtTrainingApi {
|
|||
ORT_API2_STATUS(TrainingSessionGetEvalModelInputName, _In_ const OrtTrainingSession* sess, size_t index,
|
||||
_In_ OrtAllocator* allocator, _Outptr_ char** output);
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Accessing The Training Session State
|
||||
/// @{
|
||||
|
||||
/** \brief Adds the given property to the checkpoint state.
|
||||
*
|
||||
* Runtime properties such as epoch, training step, best score, and others can be added to the checkpoint
|
||||
|
|
@ -402,7 +608,6 @@ struct OrtTrainingApi {
|
|||
*
|
||||
* Gets the property value from an existing entry in the checkpoint state. The property must
|
||||
* exist in the checkpoint state to be able to retrieve it successfully.
|
||||
* Property values are allocated on the heap. The user must free up the memory as needed by their application.
|
||||
*
|
||||
* \param[in] checkpoint_state The checkpoint state that is currently holding the property.
|
||||
* \param[in] property_name Unique name of the property being retrieved.
|
||||
|
|
@ -416,6 +621,10 @@ struct OrtTrainingApi {
|
|||
ORT_API2_STATUS(GetProperty, _In_ const OrtCheckpointState* checkpoint_state,
|
||||
_In_ const char* property_name, _Inout_ OrtAllocator* allocator,
|
||||
_Out_ enum OrtPropertyType* property_type, _Outptr_ void** property_value);
|
||||
|
||||
/// @}
|
||||
};
|
||||
|
||||
typedef struct OrtTrainingApi OrtTrainingApi;
|
||||
|
||||
/// @}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,11 @@ ORT_DECLARE_TRAINING_RELEASE(TrainingSession);
|
|||
|
||||
namespace Ort {
|
||||
|
||||
/// <summary>
|
||||
/// This function returns the C training api struct with the pointers to the ort training C functions.
|
||||
/// If using C++, please use the class instances instead of invoking the C functions directly.
|
||||
/// </summary>
|
||||
/// <returns>OrtTrainingApi struct with ort training C function pointers.</returns>
|
||||
inline const OrtTrainingApi& GetTrainingApi() { return *GetApi().GetTrainingApi(ORT_API_VERSION); }
|
||||
|
||||
namespace detail {
|
||||
|
|
@ -39,9 +44,20 @@ ORT_DEFINE_TRAINING_RELEASE(TrainingSession);
|
|||
|
||||
using Property = std::variant<int64_t, float, std::string>;
|
||||
|
||||
/** \brief Class that holds the state of the training session state
|
||||
/**
|
||||
* \defgroup TrainingCpp Ort Training C++ API
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** \brief Holds the state of the training session.
|
||||
*
|
||||
* Wraps OrtCheckpointState
|
||||
* This class holds the entire training session state that includes model parameters, their gradients,
|
||||
* optimizer parameters, and user properties. The Ort::TrainingSession leverages the Ort::CheckpointState
|
||||
* by accessing and updating the contained training state.
|
||||
* \note Note that the training session created with a checkpoint state uses this state to store the entire
|
||||
* training state (including model parameters, its gradients, the optimizer states and the properties).
|
||||
* The Ort::TrainingSession does not hold a copy of the Ort::CheckpointState and as a result, it is required
|
||||
* that the checkpoint state outlive the lifetime of the training session.
|
||||
*
|
||||
*/
|
||||
class CheckpointState : public detail::Base<OrtCheckpointState> {
|
||||
|
|
@ -49,51 +65,75 @@ class CheckpointState : public detail::Base<OrtCheckpointState> {
|
|||
CheckpointState(OrtCheckpointState* checkpoint_state) { p_ = checkpoint_state; }
|
||||
|
||||
public:
|
||||
// Construct the checkpoint state by loading the checkpoint by calling LoadCheckpoint
|
||||
CheckpointState() = delete;
|
||||
|
||||
/** \brief Loads the checkpoint at provided path and returns the checkpoint state
|
||||
/// \name Accessing The Training Session State
|
||||
/// @{
|
||||
|
||||
/** \brief Load a checkpoint state from directory on disk into checkpoint_state.
|
||||
*
|
||||
* Wraps OrtTrainingApi::LoadCheckpoint
|
||||
* This function will parse a checkpoint directory, pull relevant files and load the training
|
||||
* state and return an instance of Ort::CheckpointState. This checkpoint state can then be used to create the
|
||||
* training session by instantiating Ort::TrainingSession. By doing so, the training session will resume
|
||||
* training from the given checkpoint state.
|
||||
*
|
||||
* \param[in] path_to_checkpoint Path to the checkpoint directory
|
||||
* \return Ort::CheckpointState object which holds the state of the training session parameters.
|
||||
*
|
||||
* \param[in] path_to_checkpoint Path to the checkpoint file to load
|
||||
* \return CheckpointState object which holds the state of the training session parameters.
|
||||
*/
|
||||
static CheckpointState LoadCheckpoint(const std::basic_string<ORTCHAR_T>& path_to_checkpoint);
|
||||
|
||||
/** \brief Saves the state of the training session to a checkpoint file provided by the given path.
|
||||
/** \brief Save the given state to a checkpoint directory on disk.
|
||||
*
|
||||
* Wraps OrtTrainingApi::SaveCheckpoint
|
||||
* This function serializes the provided checkpoint state to a directory on disk.
|
||||
* This checkpoint can later be loaded by invoking Ort::CheckpointState::LoadCheckpoint to resume
|
||||
* training from this snapshot of the state.
|
||||
*
|
||||
* \param[in] checkpoint_state The checkpoint state to save.
|
||||
* \param[in] path_to_checkpoint Path to the checkpoint directory.
|
||||
* \param[in] include_optimizer_state Flag to indicate whether to save the optimizer state or not.
|
||||
*
|
||||
* \param[in] checkpoint_state Training session checkpoint state to save to the checkpoint file
|
||||
* \param[in] path_to_checkpoint Path to the checkpoint file to load
|
||||
*/
|
||||
static void SaveCheckpoint(const CheckpointState& checkpoint_state,
|
||||
const std::basic_string<ORTCHAR_T>& path_to_checkpoint,
|
||||
const bool include_optimizer_state = false);
|
||||
|
||||
/** \brief Adds the given property to the state.
|
||||
/** \brief Adds the given property to the checkpoint state.
|
||||
*
|
||||
* Wraps OrtTrainingApi::AddProperty
|
||||
* Runtime properties such as epoch, training step, best score, and others can be added to the checkpoint
|
||||
* state by the user if they desire by calling this function with the appropriate property name and
|
||||
* value. The given property name must be unique to be able to successfully add the property.
|
||||
*
|
||||
* \param[in] property_name Unique name of the property being added.
|
||||
* \param[in] property_value Property value associated with the given name.
|
||||
*
|
||||
* \param[in] property_name Name of the property to add to the state.
|
||||
* \param[in] property_value Value of the property to add to the state.
|
||||
*/
|
||||
void AddProperty(const std::string& property_name, const Property& property_value);
|
||||
|
||||
/** \brief Gets the property associated with the given name from the state.
|
||||
/** \brief Gets the property value associated with the given name from the checkpoint state.
|
||||
*
|
||||
* Wraps OrtTrainingApi::GetProperty
|
||||
* Gets the property value from an existing entry in the checkpoint state. The property must
|
||||
* exist in the checkpoint state to be able to retrieve it successfully.
|
||||
*
|
||||
* \param[in] property_name Unique name of the property being retrieved.
|
||||
* \return Property value associated with the given property name.
|
||||
*
|
||||
* \param[in] property_name Name of the property to get from the state.
|
||||
* \return Property value associated with the property name.
|
||||
*/
|
||||
Property GetProperty(const std::string& property_name);
|
||||
|
||||
/// @}
|
||||
};
|
||||
|
||||
/** \brief Trainer class that provides training, evaluation and optimizer methods for
|
||||
* executing ONNX models.
|
||||
/** \brief Trainer class that provides training, evaluation and optimizer methods for training an ONNX models.
|
||||
*
|
||||
* Wraps OrtTrainingSession
|
||||
* The training session requires four training artifacts
|
||||
* - The training onnx model
|
||||
* - The evaluation onnx model (optional)
|
||||
* - The optimizer onnx model
|
||||
* - The checkpoint directory
|
||||
*
|
||||
* These artifacts can be generated using the `onnxruntime-training` python [utility](https://github.com/microsoft/onnxruntime/blob/main/orttraining/orttraining/python/training/onnxblock/README.md).
|
||||
*
|
||||
*/
|
||||
class TrainingSession : public detail::Base<OrtTrainingSession> {
|
||||
|
|
@ -101,93 +141,166 @@ class TrainingSession : public detail::Base<OrtTrainingSession> {
|
|||
size_t training_model_output_count_, eval_model_output_count_;
|
||||
|
||||
public:
|
||||
/// \name Constructing the Training Session
|
||||
/// @{
|
||||
/** \brief Create a training session that can be used to begin or resume training.
|
||||
*
|
||||
* This constructor instantiates the training session based on the env and session options provided that can
|
||||
* begin or resume training from a given checkpoint state for the given onnx models.
|
||||
* The checkpoint state represents the parameters of the training session which will be moved
|
||||
* to the device specified by the user through the session options (if necessary).
|
||||
*
|
||||
* \param[in] env Env to be used for the training session.
|
||||
* \param[in] session_options SessionOptions that the user can customize for this training session.
|
||||
* \param[in] checkpoint_state Training states that the training session uses as a starting point for training.
|
||||
* \param[in] train_model_path Model to be used to perform training.
|
||||
* \param[in] eval_model_path Model to be used to perform evaluation.
|
||||
* \param[in] optimizer_model_path Model to be used to perform gradient descent.
|
||||
*
|
||||
*/
|
||||
TrainingSession(const Env& env, const SessionOptions& session_options, CheckpointState& checkpoint_state,
|
||||
const std::basic_string<ORTCHAR_T>& train_model_path,
|
||||
const std::optional<std::basic_string<ORTCHAR_T>>& eval_model_path = std::nullopt,
|
||||
const std::optional<std::basic_string<ORTCHAR_T>>& optimizer_model_path = std::nullopt);
|
||||
|
||||
/** \brief Run the train step returning results in an Ort allocated vector.
|
||||
/// @}
|
||||
|
||||
/// \name Implementing The Training Loop
|
||||
/// @{
|
||||
/** \brief Computes the outputs of the training model and the gradients of the trainable parameters for the given inputs
|
||||
*
|
||||
* Wraps OrtTrainingApi::TrainStep
|
||||
* This function performs a training step that computes the outputs of the training model and the gradients
|
||||
* of the trainable parameters for the given inputs. The train step is performed based on the training model
|
||||
* that was provided to the training session.
|
||||
* The Ort::TrainingSession::TrainStep is equivalent of running forward propagation and backward propagation in a single
|
||||
* step.
|
||||
* The gradients computed are stored inside the training session state so they can be later consumed
|
||||
* by the Ort::TrainingSession::OptimizerStep function.
|
||||
* The gradients can be lazily reset by invoking the Ort::TrainingSession::LazyResetGrad function.
|
||||
*
|
||||
* \param[in] input_values The user inputs to the training model.
|
||||
* \return A std::vector of Ort::Value objects that represents the output of the forward pass of the training model.
|
||||
*
|
||||
* \snippet{doc} snippets.dox OrtStatus Return Value
|
||||
*
|
||||
* \param[in] input_values Array of Value objects in the order expected by the training model.
|
||||
* \return A std::vector of Value objects that represents the output of the forward pass.
|
||||
*/
|
||||
std::vector<Value> TrainStep(const std::vector<Value>& input_values);
|
||||
|
||||
/** \brief Lazily resets the gradients of the trainable parameters.
|
||||
/** \brief Reset the gradients of all trainable parameters to zero lazily.
|
||||
*
|
||||
* Wraps OrtTrainingApi::LazyResetGrad
|
||||
* This function sets the internal state of the training session such that the gradients of the trainable
|
||||
* parameters in the OrtCheckpointState will be scheduled to be reset just before the new gradients are
|
||||
* computed on the next invocation of the next Ort::TrainingSession::TrainStep.
|
||||
*
|
||||
*/
|
||||
void LazyResetGrad();
|
||||
|
||||
/** \brief Run the evaluation step returning results in an Ort allocated vector.
|
||||
/** \brief Computes the outputs for the eval model for the given inputs
|
||||
*
|
||||
* Wraps OrtTrainingApi::EvalStep
|
||||
* This function performs an eval step that computes the outputs of the eval model for the given inputs.
|
||||
* The eval step is performed based on the eval model that was provided to the training session.
|
||||
*
|
||||
* \param[in] input_values The user inputs to the eval model.
|
||||
* \return A std::vector of Ort::Value objects that represents the output of the eval pass.
|
||||
*
|
||||
* \param[in] input_values Array of Value objects in the order expected by the eval model.
|
||||
* \return A std::vector of Value objects that represents the output of the eval pass.
|
||||
*/
|
||||
std::vector<Value> EvalStep(const std::vector<Value>& input_values);
|
||||
|
||||
/** \brief Set the learning rate to be used by the optimizer for parameter updates.
|
||||
/** \brief Sets the learning rate for this training session.
|
||||
*
|
||||
* Wraps OrtTrainingApi::SetLearningRate
|
||||
* This function allows users to set the learning rate for the training session. The current
|
||||
* learning rate is maintained by the training session and can be overwritten by invoking
|
||||
* this function with the desired learning rate. This function should not be used when a valid
|
||||
* learning rate scheduler is registered. It should be used either to set the learning rate
|
||||
* derived from a custom learning rate scheduler or to set a constant learning rate to be used
|
||||
* throughout the training session.
|
||||
* \note Please note that this function does not set the initial learning rate that may be needed
|
||||
* by the predefined learning rate schedulers. To set the initial learning rate for learning
|
||||
* rate schedulers, please look at the function Ort::TrainingSession::RegisterLinearLRScheduler.
|
||||
*
|
||||
* \param[in] learning_rate Desired learning rate to be set.
|
||||
*
|
||||
* \param[in] learning_rate float value representing the constant learning rate to be used.
|
||||
*/
|
||||
void SetLearningRate(float learning_rate);
|
||||
|
||||
/** \brief Get the current learning rate that is being used by the optimizer.
|
||||
/** \brief Gets the current learning rate for this training session.
|
||||
*
|
||||
* Wraps OrtTrainingApi::GetLearningRate
|
||||
* This function allows users to get the learning rate for the training session. The current
|
||||
* learning rate is maintained by the training session, and users can query it for the purpose
|
||||
* of implementing their own learning rate schedulers.
|
||||
*
|
||||
* \return float representing the current learning rate.
|
||||
*
|
||||
*/
|
||||
float GetLearningRate() const;
|
||||
|
||||
/** \brief Register the linear learning rate scheduler for the training session.
|
||||
/** \brief Registers a linear learning rate scheduler for the training session.
|
||||
*
|
||||
* Wraps OrtTrainingApi::RegisterLinearLRScheduler
|
||||
* Register a linear learning rate scheduler that decays the learning rate by linearly updated
|
||||
* multiplicative factor from the initial learning rate set on the training session to 0. The decay
|
||||
* is performed after the initial warm up phase where the learning rate is linearly incremented
|
||||
* from 0 to the initial learning rate provided.
|
||||
*
|
||||
* \param[in] warmup_step_count Warmup steps for LR warmup.
|
||||
* \param[in] total_step_count Total step count.
|
||||
* \param[in] initial_lr The initial learning rate to be used by the training session.
|
||||
*
|
||||
* \param[in] warmup_step_count Number of steps in the warmup phase.
|
||||
* \param[in] total_step_count Total number of training steps.
|
||||
* \param[in] initial_lr Initial learning rate to use.
|
||||
*/
|
||||
void RegisterLinearLRScheduler(int64_t warmup_step_count, int64_t total_step_count,
|
||||
float initial_lr);
|
||||
|
||||
/** \brief Updates the learning rate based on the lr scheduler.
|
||||
/** \brief Update the learning rate based on the registered learing rate scheduler.
|
||||
*
|
||||
* Wraps OrtTrainingApi::SchedulerStep
|
||||
* Takes a scheduler step that updates the learning rate that is being used by the training session.
|
||||
* This function should typically be called before invoking the optimizer step for each round,
|
||||
* or as determined necessary to update the learning rate being used by the training session.
|
||||
* \note Please note that a valid predefined learning rate scheduler must be first registered to invoke this
|
||||
* function.
|
||||
*
|
||||
*/
|
||||
void SchedulerStep();
|
||||
|
||||
/** \brief Runs the optimizer model and updates the model parameters.
|
||||
/** \brief Performs the weight updates for the trainable parameters using the optimizer model.
|
||||
*
|
||||
* Wraps OrtTrainingApi::OptimizerStep
|
||||
* This function performs the weight update step that updates the trainable parameters such that they
|
||||
* take a step in the direction of their gradients (gradient descent). The optimizer step is performed
|
||||
* based on the optimizer model that was provided to the training session.
|
||||
* The updated parameters are stored inside the training state so that they can be used by the next
|
||||
* Ort::TrainingSession::TrainStep function call.
|
||||
*
|
||||
*/
|
||||
void OptimizerStep();
|
||||
|
||||
/** \brief Exports a model that can be used for inferencing with the inference session.
|
||||
/// @}
|
||||
|
||||
/// \name Prepare For Inferencing
|
||||
/// @{
|
||||
|
||||
/** \brief Export a model that can be used for inferencing.
|
||||
*
|
||||
* Wraps OrtTrainingApi::ExportModelForInferencing
|
||||
* If the training session was provided with an eval model, the training session can generate
|
||||
* an inference model if it knows the inference graph outputs. The input inference graph outputs
|
||||
* are used to prune the eval model so that the inference model's outputs align with the provided outputs.
|
||||
* The exported model is saved at the path provided and can be used for inferencing with Ort::Session.
|
||||
* \note Note that the function re-loads the eval model from the path provided to Ort::TrainingSession
|
||||
* and expects that this path still be valid.
|
||||
*
|
||||
* \param[in] inference_model_path Path where the inference model should be serialized to.
|
||||
* \param[in] graph_output_names Names of the outputs that are needed in the inference model.
|
||||
*
|
||||
* \param[in] inference_model_path Path to a location where the inference ready onnx model should be saved to.
|
||||
* \param[in] graph_output_names Vector of output names that the inference model should have.
|
||||
*/
|
||||
void ExportModelForInferencing(const std::basic_string<ORTCHAR_T>& inference_model_path,
|
||||
const std::vector<std::string>& graph_output_names);
|
||||
|
||||
/** \brief Gets the graph input names.
|
||||
/// @}
|
||||
|
||||
/// \name Model IO Information
|
||||
/// @{
|
||||
/** \brief Retrieves the names of the user inputs for the training and eval models.
|
||||
*
|
||||
* Wraps OrtTrainingApi::TrainingSessionGetTrainingModelInputName,
|
||||
* OrtTrainingApi::TrainingSessionGetEvalModelInputName,
|
||||
* OrtTrainingApi::TrainingSessionGetTrainingModelInputCount
|
||||
* OrtTrainingApi::TrainingSessionGetEvalModelInputCount
|
||||
* This function returns the names of inputs of the training or eval model that can be associated
|
||||
* with the Ort::Value(s) provided to the Ort::TrainingSession::TrainStep or Ort::TrainingSession::EvalStep
|
||||
* function.
|
||||
*
|
||||
* \param[in] training Whether the training model input names are requested or eval model input names.
|
||||
* \return Graph input names for either the training model or the eval model.
|
||||
|
|
@ -195,43 +308,53 @@ class TrainingSession : public detail::Base<OrtTrainingSession> {
|
|||
*/
|
||||
std::vector<std::string> InputNames(const bool training);
|
||||
|
||||
/** \brief Gets the graph output names.
|
||||
/** \brief Retrieves the names of the user outputs for the training and eval models.
|
||||
*
|
||||
* Wraps OrtTrainingApi::TrainingSessionGetTrainingModelOutputName,
|
||||
* OrtTrainingApi::TrainingSessionGetEvalModelOutputName,
|
||||
* OrtTrainingApi::TrainingSessionGetTrainingModelOutputCount
|
||||
* OrtTrainingApi::TrainingSessionGetEvalModelOutputCount
|
||||
* This function returns the names of outputs of the training or eval model that can be associated
|
||||
* with the Ort::Value(s) returned by the Ort::TrainingSession::TrainStep or Ort::TrainingSession::EvalStep
|
||||
* function.
|
||||
*
|
||||
* \param[in] training Whether the training model output names are requested or eval model output names.
|
||||
* \return Graph output names for either the training model or the eval model.
|
||||
*
|
||||
*/
|
||||
std::vector<std::string> OutputNames(const bool training);
|
||||
|
||||
/** \brief Copies the training session model parameters to a contiguous buffer
|
||||
*
|
||||
* Wraps OrtTrainingApi::CopyParametersToBuffer
|
||||
/// @}
|
||||
|
||||
/// \name Accessing The Training Session State
|
||||
/// @{
|
||||
|
||||
/** \brief Returns a contiguous buffer that holds a copy of all training state parameters
|
||||
*
|
||||
* \param[in] only_trainable Whether to only copy trainable parameters or to copy all parameters.
|
||||
* \return Contiguous buffer to the model parameters.
|
||||
*
|
||||
*/
|
||||
Value ToBuffer(const bool only_trainable);
|
||||
|
||||
/** \brief Loads the training session model parameters from a contiguous buffer
|
||||
*
|
||||
* Wraps OrtTrainingApi::CopyBufferToParameters
|
||||
*
|
||||
* \param[in] buffer Contiguous buffer to load the parameters from.
|
||||
*/
|
||||
void FromBuffer(Value& buffer);
|
||||
|
||||
/// @}
|
||||
};
|
||||
|
||||
/** \brief Sets the given seed for random number generation.
|
||||
/// \name Training Utilities
|
||||
/// @{
|
||||
/** \brief This function sets the seed for generating random numbers.
|
||||
*
|
||||
* Wraps OrtTrainingApi::SetSeed
|
||||
* Use this function to generate reproducible results. It should be noted that completely
|
||||
* reproducible results are not guaranteed.
|
||||
*
|
||||
* \param[in] seed Manual seed to use for random number generation.
|
||||
*/
|
||||
void SetSeed(const int64_t seed);
|
||||
/// @}
|
||||
|
||||
/// @}
|
||||
|
||||
} // namespace Ort
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue