|
1 | | - |
2 | 1 | using System; |
3 | 2 | using System.Threading; |
4 | 3 | using System.Threading.Tasks; |
| 4 | +using LLama.Exceptions; |
5 | 5 | using LLama.Native; |
6 | 6 |
|
7 | 7 | namespace LLama; |
8 | 8 |
|
9 | 9 | /// <summary> |
10 | 10 | /// Lightweight wrapper around the MTMD native context and its helpers. |
11 | 11 | /// </summary> |
12 | | -public sealed class MtmdWeights : IDisposable |
| 12 | +public sealed class MtmdWeights |
| 13 | + : IDisposable |
13 | 14 | { |
| 15 | + /// <summary> |
| 16 | + /// The native handle, which is used in the native APIs |
| 17 | + /// </summary> |
| 18 | + /// <remarks>Be careful how you use this!</remarks> |
14 | 19 | public SafeMtmdModelHandle NativeHandle { get; } |
15 | 20 |
|
16 | 21 | private MtmdWeights(SafeMtmdModelHandle handle) |
17 | 22 | { |
18 | 23 | NativeHandle = handle ?? throw new ArgumentNullException(nameof(handle)); |
19 | 24 | } |
20 | 25 |
|
| 26 | + /// <summary> |
| 27 | + /// Load weights into memory |
| 28 | + /// </summary> |
| 29 | + /// <param name="mmProject">Path to the mmproj file</param> |
| 30 | + /// <param name="textModel">The text model</param> |
| 31 | + /// <param name="mtmdCtxParams">Parameters for MTMD context creation</param> |
| 32 | + /// <returns></returns> |
21 | 33 | public static MtmdWeights LoadFromFile(string mmProject, LLamaWeights textModel, MtmdContextParams mtmdCtxParams) |
22 | 34 | { |
23 | | - if (mmProject == null) throw new ArgumentNullException(nameof(mmProject)); |
24 | | - if (textModel == null) throw new ArgumentNullException(nameof(textModel)); |
25 | | - if (mtmdCtxParams == null) throw new ArgumentNullException(nameof(mtmdCtxParams)); |
26 | | - |
27 | | - var handle = SafeMtmdModelHandle.LoadFromFile(mmProject, textModel, mtmdCtxParams); |
28 | | - return new MtmdWeights(handle); |
| 35 | + return new MtmdWeights(SafeMtmdModelHandle.LoadFromFile( |
| 36 | + mmProject ?? throw new ArgumentNullException(nameof(mmProject)), |
| 37 | + textModel ?? throw new ArgumentNullException(nameof(textModel)), |
| 38 | + mtmdCtxParams ?? throw new ArgumentNullException(nameof(mtmdCtxParams)) |
| 39 | + )); |
29 | 40 | } |
30 | 41 |
|
31 | | - public static Task<MtmdWeights> LoadFromFileAsync(string mmProject, LLamaWeights textModel, MtmdContextParams mtmdCtxParams, CancellationToken token = default) |
| 42 | + /// <summary> |
| 43 | + /// Load weights into memory |
| 44 | + /// </summary> |
| 45 | + /// <param name="mmProject">Path to the mmproj file</param> |
| 46 | + /// <param name="textModel">The text model</param> |
| 47 | + /// <param name="mtmdCtxParams">Parameters for MTMD context creation</param> |
| 48 | + /// <param name="token"></param> |
| 49 | + /// <returns></returns> |
| 50 | + public static async Task<MtmdWeights> LoadFromFileAsync(string mmProject, LLamaWeights textModel, MtmdContextParams mtmdCtxParams, CancellationToken token = default) |
32 | 51 | { |
33 | | - return Task.Run(() => LoadFromFile(mmProject, textModel, mtmdCtxParams), token); |
| 52 | + if (mmProject == null) |
| 53 | + throw new ArgumentNullException(nameof(mmProject)); |
| 54 | + if (textModel == null) |
| 55 | + throw new ArgumentNullException(nameof(textModel)); |
| 56 | + if (mtmdCtxParams == null) |
| 57 | + throw new ArgumentNullException(nameof(mtmdCtxParams)); |
| 58 | + |
| 59 | + var model = await Task.Run(() => |
| 60 | + { |
| 61 | + try |
| 62 | + { |
| 63 | + // Load the model |
| 64 | + return LoadFromFile(mmProject, textModel, mtmdCtxParams); |
| 65 | + } |
| 66 | + catch (LoadWeightsFailedException) |
| 67 | + { |
| 68 | + // Convert a LoadWeightsFailedException into a cancellation exception if possible. |
| 69 | + token.ThrowIfCancellationRequested(); |
| 70 | + |
| 71 | + // Ok the weights failed to load for some reason other than cancellation. |
| 72 | + throw; |
| 73 | + } |
| 74 | + }, token); |
| 75 | + |
| 76 | + return model; |
34 | 77 | } |
35 | 78 |
|
36 | 79 | /// <summary> |
@@ -73,15 +116,31 @@ public int EvaluateChunk(IntPtr chunkPtr, SafeLLamaContextHandle llamaContext, r |
73 | 116 | public int DecodeImageChunk(IntPtr chunkPtr, SafeLLamaContextHandle llamaContext, IntPtr encodedEmbeddings, ref int nPast, int seqId, int nBatch) |
74 | 117 | => NativeHandle.DecodeImageChunk(chunkPtr, llamaContext, encodedEmbeddings, ref nPast, seqId, nBatch); |
75 | 118 |
|
76 | | - public ulong CountTokens(SafeMtmdInputChunks chunks) => NativeHandle.CountTokens(chunks); |
77 | | - |
78 | | - public long CountPositions(SafeMtmdInputChunks chunks) => NativeHandle.CountPositions(chunks); |
79 | | - |
| 119 | + /// <summary> |
| 120 | + /// Indicates whether the model supports vision inputs. |
| 121 | + /// </summary> |
80 | 122 | public bool SupportsVision => NativeHandle.SupportVision(); |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Indicates whether the model supports audio inputs. |
| 126 | + /// </summary> |
81 | 127 | public bool SupportsAudio => NativeHandle.SupportAudio(); |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Indicates whether the model decodes using the non-causal path. |
| 131 | + /// </summary> |
82 | 132 | public bool UsesNonCausalAttention => NativeHandle.DecodeUseNonCausal(); |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Indicates whether the model decodes using multi-scale RoPE. |
| 136 | + /// </summary> |
83 | 137 | public bool UsesMRope => NativeHandle.DecodeUseMRope(); |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Gets the audio bitrate advertised by the model. |
| 141 | + /// </summary> |
84 | 142 | public int AudioBitrate => NativeHandle.GetAudioBitrate(); |
85 | 143 |
|
| 144 | + /// <inheritdoc /> |
86 | 145 | public void Dispose() => NativeHandle.Dispose(); |
87 | 146 | } |
0 commit comments