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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using TrustformersMobile.Native;
namespace TrustformersMobile
{
/// <summary>
/// Configuration class for TrustformeRS Mobile inference
/// </summary>
public class MobileConfig : IDisposable
{
private UIntPtr _handle;
private bool _disposed = false;
/// <summary>
/// Internal handle to the native configuration
/// </summary>
internal UIntPtr Handle => _handle;
/// <summary>
/// Creates a new configuration with default settings
/// </summary>
public MobileConfig()
{
var error = TrustformersMobileNative.trustformers_mobile_config_create_default(out _handle);
ThrowIfError(error);
}
/// <summary>
/// Creates a configuration optimized for iOS devices
/// </summary>
/// <returns>iOS-optimized configuration</returns>
public static MobileConfig CreateiOSOptimized()
{
var config = new MobileConfig();
config.Dispose(); // Free the default config
var error = TrustformersMobileNative.trustformers_mobile_config_create_ios_optimized(out config._handle);
ThrowIfError(error);
config._disposed = false;
return config;
}
/// <summary>
/// Creates a configuration optimized for Android devices
/// </summary>
/// <returns>Android-optimized configuration</returns>
public static MobileConfig CreateAndroidOptimized()
{
var config = new MobileConfig();
config.Dispose(); // Free the default config
var error = TrustformersMobileNative.trustformers_mobile_config_create_android_optimized(out config._handle);
ThrowIfError(error);
config._disposed = false;
return config;
}
/// <summary>
/// Creates a configuration for ultra-low memory usage
/// </summary>
/// <returns>Ultra-low memory configuration</returns>
public static MobileConfig CreateUltraLowMemory()
{
var config = new MobileConfig();
config.Dispose(); // Free the default config
var error = TrustformersMobileNative.trustformers_mobile_config_create_ultra_low_memory(out config._handle);
ThrowIfError(error);
config._disposed = false;
return config;
}
/// <summary>
/// Sets the target mobile platform
/// </summary>
/// <param name="platform">Target platform</param>
public void SetPlatform(MobilePlatform platform)
{
ThrowIfDisposed();
var error = TrustformersMobileNative.trustformers_mobile_config_set_platform(_handle, (int)platform);
ThrowIfError(error);
}
/// <summary>
/// Sets the inference backend
/// </summary>
/// <param name="backend">Inference backend</param>
public void SetBackend(MobileBackend backend)
{
ThrowIfDisposed();
var error = TrustformersMobileNative.trustformers_mobile_config_set_backend(_handle, (int)backend);
ThrowIfError(error);
}
/// <summary>
/// Sets the memory optimization level
/// </summary>
/// <param name="optimization">Memory optimization level</param>
public void SetMemoryOptimization(MemoryOptimization optimization)
{
ThrowIfDisposed();
var error = TrustformersMobileNative.trustformers_mobile_config_set_memory_optimization(_handle, (int)optimization);
ThrowIfError(error);
}
/// <summary>
/// Sets the maximum memory usage in megabytes
/// </summary>
/// <param name="maxMemoryMb">Maximum memory usage in MB</param>
public void SetMaxMemoryMb(ulong maxMemoryMb)
{
ThrowIfDisposed();
var error = TrustformersMobileNative.trustformers_mobile_config_set_max_memory_mb(_handle, (UIntPtr)maxMemoryMb);
ThrowIfError(error);
}
/// <summary>
/// Enables or disables FP16 precision
/// </summary>
/// <param name="useFp16">True to enable FP16, false to disable</param>
public void SetUseFp16(bool useFp16)
{
ThrowIfDisposed();
var error = TrustformersMobileNative.trustformers_mobile_config_set_use_fp16(_handle, useFp16 ? 1 : 0);
ThrowIfError(error);
}
/// <summary>
/// Sets the number of threads for inference (0 = auto-detect)
/// </summary>
/// <param name="numThreads">Number of threads</param>
public void SetNumThreads(ulong numThreads)
{
ThrowIfDisposed();
var error = TrustformersMobileNative.trustformers_mobile_config_set_num_threads(_handle, (UIntPtr)numThreads);
ThrowIfError(error);
}
/// <summary>
/// Validates the configuration
/// </summary>
/// <exception cref="TrustformersMobileException">Thrown if configuration is invalid</exception>
public void Validate()
{
ThrowIfDisposed();
var error = TrustformersMobileNative.trustformers_mobile_config_validate(_handle);
ThrowIfError(error);
}
/// <summary>
/// Checks if a platform is supported
/// </summary>
/// <param name="platform">Platform to check</param>
/// <returns>True if supported, false otherwise</returns>
public static bool IsPlatformSupported(MobilePlatform platform)
{
return TrustformersMobileNative.trustformers_mobile_is_platform_supported((int)platform) != 0;
}
/// <summary>
/// Checks if a backend is supported
/// </summary>
/// <param name="backend">Backend to check</param>
/// <returns>True if supported, false otherwise</returns>
public static bool IsBackendSupported(MobileBackend backend)
{
return TrustformersMobileNative.trustformers_mobile_is_backend_supported((int)backend) != 0;
}
private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(nameof(MobileConfig));
}
private static void ThrowIfError(TrustformersMobileError error)
{
if (error != TrustformersMobileError.Success)
throw new TrustformersMobileException(error);
}
/// <summary>
/// Disposes the configuration and frees native resources
/// </summary>
public void Dispose()
{
if (!_disposed && _handle != UIntPtr.Zero)
{
TrustformersMobileNative.trustformers_mobile_config_free(_handle);
_handle = UIntPtr.Zero;
_disposed = true;
}
GC.SuppressFinalize(this);
}
/// <summary>
/// Finalizer
/// </summary>
~MobileConfig()
{
Dispose();
}
}
}