Implementation:Microsoft Onnxruntime OrtTrainingApis
| Knowledge Sources | |
|---|---|
| Domains | Training, API, C_API |
| Last Updated | 2026-02-10 04:00 GMT |
Overview
Declares the complete set of ORT Training C API function signatures that provide the public-facing interface for training session management, checkpoint operations, and parameter manipulation.
Description
The `ort_training_apis.h` header defines the `OrtTrainingApis` namespace containing all C API entry points for the ORT Training functionality. These APIs are consumed by language bindings (Python, C#, Java) and provide a stable ABI. The functions cover the full training lifecycle:
- Session Management: `CreateTrainingSession` and `CreateTrainingSessionFromBuffer` create training sessions from file paths or in-memory byte arrays. `ReleaseTrainingSession` handles cleanup.
- Training Execution: `TrainStep` and `EvalStep` execute forward/backward and forward-only passes respectively. `LazyResetGrad` resets gradients lazily. `OptimizerStep` performs parameter updates.
- Learning Rate: `SetLearningRate`, `GetLearningRate`, `RegisterLinearLRScheduler`, and `SchedulerStep` manage learning rate schedules.
- Checkpoint: `LoadCheckpoint`, `LoadCheckpointFromBuffer`, `SaveCheckpoint`, and `ReleaseCheckpointState` handle checkpoint persistence.
- Parameters: `GetParametersSize`, `CopyParametersToBuffer`, `CopyBufferToParameters`, `GetParameterTypeAndShape`, `UpdateParameter`, and `GetParameter` enable parameter introspection and manipulation.
- Properties: `AddProperty` and `GetProperty` manage user-defined checkpoint properties.
- Model Export: `ExportModelForInferencing` exports a trained model for deployment.
- Utilities: `SetSeed` sets the random seed, `GetTrainingApi` returns the API struct for a given version.
Usage
Use this header when implementing C API bindings or when calling the ORT Training API directly from C/C++ code. This is the primary entry point for all training operations exposed through the ORT C API.
Code Reference
Source Location
- Repository: Microsoft_Onnxruntime
- File: orttraining/orttraining/training_api/ort_training_apis.h
- Lines: 1-107
Signature
namespace OrtTrainingApis {
ORT_API(const OrtTrainingApi*, GetTrainingApi, uint32_t version);
ORT_API_STATUS_IMPL(CreateTrainingSession, _In_ const OrtEnv* env,
_In_ const OrtSessionOptions* options,
_Inout_ OrtCheckpointState* checkpoint_state,
_In_ const ORTCHAR_T* train_model_path,
_In_ const ORTCHAR_T* eval_model_path,
_In_ const ORTCHAR_T* optimizer_model_path,
_Outptr_result_maybenull_ OrtTrainingSession** out);
ORT_API_STATUS_IMPL(TrainStep, _Inout_ OrtTrainingSession* session,
_In_opt_ const OrtRunOptions* run_options,
_In_ size_t inputs_len,
_In_reads_(inputs_len) const OrtValue* const* inputs,
_In_ size_t outputs_len,
_Inout_updates_all_(outputs_len) OrtValue** outputs);
ORT_API_STATUS_IMPL(EvalStep, _In_ const OrtTrainingSession* session,
_In_opt_ const OrtRunOptions* run_options,
_In_ size_t inputs_len,
_In_reads_(inputs_len) const OrtValue* const* inputs,
_In_ size_t outputs_len,
_Inout_updates_all_(outputs_len) OrtValue** outputs);
ORT_API_STATUS_IMPL(OptimizerStep, _Inout_ OrtTrainingSession* session,
_In_opt_ const OrtRunOptions* run_options);
ORT_API_STATUS_IMPL(LoadCheckpoint, _In_ const ORTCHAR_T* checkpoint_path,
_Outptr_ OrtCheckpointState** checkpoint_state);
ORT_API_STATUS_IMPL(SaveCheckpoint, _In_ OrtCheckpointState* checkpoint_state,
_In_ const ORTCHAR_T* checkpoint_path,
const bool include_optimizer_state);
ORT_API(void, ReleaseCheckpointState, _Frees_ptr_opt_ OrtCheckpointState* checkpoint_state);
ORT_API(void, ReleaseTrainingSession, _Frees_ptr_opt_ OrtTrainingSession* session);
} // namespace OrtTrainingApis
Import
#include "orttraining/training_api/ort_training_apis.h"
I/O Contract
| API Function | Key Inputs | Key Outputs | Description |
|---|---|---|---|
| CreateTrainingSession | OrtEnv, OrtSessionOptions, checkpoint, model paths | OrtTrainingSession* | Creates a new training session from model files |
| TrainStep | OrtTrainingSession, inputs array | outputs array, OrtStatus | Executes forward+backward pass |
| EvalStep | OrtTrainingSession, inputs array | outputs array, OrtStatus | Executes forward-only evaluation pass |
| OptimizerStep | OrtTrainingSession | OrtStatus | Performs one optimizer update step |
| LoadCheckpoint | checkpoint path | OrtCheckpointState* | Loads checkpoint from file |
| SaveCheckpoint | OrtCheckpointState, path, bool | OrtStatus | Saves checkpoint to file |
| GetParameter | OrtCheckpointState, name | OrtValue* | Retrieves a named parameter value |
| UpdateParameter | OrtCheckpointState, name, OrtValue | OrtStatus | Updates a named parameter value |
Usage Examples
// C API usage pattern
OrtCheckpointState* ckpt = nullptr;
OrtTrainingApis::LoadCheckpoint(ORT_TSTR("checkpoint.ckpt"), &ckpt);
OrtTrainingSession* session = nullptr;
OrtTrainingApis::CreateTrainingSession(env, options, ckpt,
ORT_TSTR("train.onnx"), ORT_TSTR("eval.onnx"),
ORT_TSTR("optimizer.onnx"), &session);
// Run training step
OrtTrainingApis::TrainStep(session, nullptr, num_inputs, inputs, num_outputs, outputs);
OrtTrainingApis::OptimizerStep(session, nullptr);
// Cleanup
OrtTrainingApis::ReleaseTrainingSession(session);
OrtTrainingApis::ReleaseCheckpointState(ckpt);