-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathConfig.cs
91 lines (81 loc) · 3.18 KB
/
Config.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
namespace Microsoft.ML.OnnxRuntimeGenAI
{
/// <summary>
/// Use Config to set the ORT execution providers (EPs) and their options. The EPs are applied based on
/// insertion order.
/// </summary>
public class Config : IDisposable
{
private IntPtr _configHandle;
private bool _disposed = false;
/// <summary>
/// Creates a Config from the given configuration directory.
/// </summary>
/// <param name="modelPath">The path to the configuration directory.</param>
/// <exception cref="OnnxRuntimeGenAIException">
/// Thrown when the call to the GenAI native API fails.
/// </exception>
public Config(string modelPath)
{
Result.VerifySuccess(NativeMethods.OgaCreateConfig(StringUtils.ToUtf8(modelPath), out _configHandle));
}
internal IntPtr Handle { get { return _configHandle; } }
/// <summary>
/// Clear the list of providers in the config.
/// </summary>
/// <exception cref="OnnxRuntimeGenAIException">
/// Thrown when the call to the GenAI native API fails.
/// </exception>
public void ClearProviders()
{
Result.VerifySuccess(NativeMethods.OgaConfigClearProviders(_configHandle));
}
/// <summary>
/// Add the provider at the end of the list of providers in the given config if it doesn't already
/// exist. If it already exists, does nothing.
/// </summary>
/// <param name="providerName">Name of the provider</param>
/// <exception cref="OnnxRuntimeGenAIException">
/// Thrown when the call to the GenAI native API fails.
/// </exception>
public void AppendProvider(string providerName)
{
Result.VerifySuccess(NativeMethods.OgaConfigAppendProvider(_configHandle, StringUtils.ToUtf8(providerName)));
}
/// <summary>
/// Set a provider option.
/// </summary>
/// <param name="providerName">Name of the provider</param>
/// <param name="optionKey">Name of the option</param>
/// <param name="optionValue">Value of the option</param>
/// <exception cref="OnnxRuntimeGenAIException">
/// Thrown when the call to the GenAI native API fails.
/// </exception>
public void SetProviderOption(string providerName, string optionKey, string optionValue)
{
Result.VerifySuccess(NativeMethods.OgaConfigSetProviderOption(_configHandle, StringUtils.ToUtf8(providerName), StringUtils.ToUtf8(optionKey), StringUtils.ToUtf8(optionValue)));
}
~Config()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
NativeMethods.OgaDestroyConfig(_configHandle);
_configHandle = IntPtr.Zero;
_disposed = true;
}
}
}