Skip to content

Commit

Permalink
Bug Fix - C# API order incompatibile with C API (#13191)
Browse files Browse the repository at this point in the history
### Description
Training C# bindings (ReleaseTrainingSession and ReleaseCheckpointState)
broke after an API order change in Training C API. This PR fixes this
issue.



### Motivation and Context
Bug Fix for Training C# bindings
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
  • Loading branch information
askhade authored Oct 4, 2022
1 parent 595a0c8 commit 4fc8f71
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Microsoft.ML.OnnxRuntime
{
#if __ENABLE_TRAINING_ON_DEVICE__
// NOTE: The order of the APIs in this struct should match exactly that in
// OrtTrainingApi (onnxruntime_training_c_api.cc)
[StructLayout(LayoutKind.Sequential)]
public struct OrtTrainingApi
Expand All @@ -26,6 +27,9 @@ public struct OrtTrainingApi
public IntPtr OptimizerStep;
public IntPtr RegisterLinearLRScheduler;
public IntPtr SchedulerStep;
public IntPtr GetParametersSize;
public IntPtr CopyParametersToBuffer;
public IntPtr CopyBufferToParameters;
public IntPtr ReleaseTrainingSession;
public IntPtr ReleaseCheckpointState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ ORT_API_STATUS_IMPL(OrtTrainingApis::CopyParametersToBuffer, _Inout_ OrtTraining
}
auto session = reinterpret_cast<onnxruntime::training::api::TrainingSession*>(sess);
ORT_API_RETURN_IF_STATUS_NOT_OK(session->CopyParametersToBuffer(*parameters_buffer, trainable_only));

return nullptr;
API_IMPL_END
}
Expand All @@ -339,6 +339,9 @@ ORT_API(void, OrtTrainingApis::ReleaseCheckpointState, _Frees_ptr_opt_ OrtCheckp
}

static constexpr OrtTrainingApi ort_training_api = {
// NOTE: The C# bindings depend on the API order within this struct. Since Training APIs are not officially
// released, it is OK to change the order here, however a corresponding matching change should also be done in the
// "OrtTrainingApi" struct in NativeTrainingMethods.shared.cs
&OrtTrainingApis::LoadCheckpoint,
&OrtTrainingApis::SaveCheckpoint,
&OrtTrainingApis::CreateTrainingSession,
Expand Down

0 comments on commit 4fc8f71

Please sign in to comment.