Skip to content

Commit 2a2d6d9

Browse files
committed
1 parent e4d7c6c commit 2a2d6d9

13 files changed

Lines changed: 144 additions & 13 deletions

LLama.Unittest/MtmdWeightsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void BasicPropertyChecks()
8181
Assert.True(_mtmdWeights.SupportsVision);
8282
Assert.False(_mtmdWeights.UsesMRope);
8383
Assert.True(_mtmdWeights.UsesNonCausalAttention);
84-
Assert.Equal(-1, _mtmdWeights.AudioBitrate);
84+
Assert.Equal(-1, _mtmdWeights.SampleRate);
8585
}
8686

8787
[Fact,Trait("Category", "NoCI")]
@@ -143,8 +143,8 @@ public void TokenizeProvidesChunkMetadata()
143143
Assert.True(_mtmdWeights.SupportsVision);
144144
Assert.False(_mtmdWeights.SupportsAudio);
145145

146-
var audioBitrate = _mtmdWeights.AudioBitrate;
147-
Assert.True(audioBitrate <= 0);
146+
var audioSampleRate = _mtmdWeights.SampleRate;
147+
Assert.True(audioSampleRate <= 0);
148148
}
149149
}
150150
}

LLama/LLamaSharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
</ItemGroup>
5858

5959
<PropertyGroup>
60-
<BinaryReleaseId>73c9eb8ceda397b</BinaryReleaseId>
60+
<BinaryReleaseId>3f7c29d318e317b6</BinaryReleaseId>
6161
</PropertyGroup>
6262

6363
<PropertyGroup>

LLama/MtmdWeights.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public int DecodeImageChunk(IntPtr chunkPtr, SafeLLamaContextHandle llamaContext
137137
public bool UsesMRope => NativeHandle.DecodeUseMRope();
138138

139139
/// <summary>
140-
/// Gets the audio bitrate advertised by the model.
140+
/// Gets the audio sample rate advertised by the model.
141141
/// </summary>
142-
public int AudioBitrate => NativeHandle.GetAudioBitrate();
142+
public int SampleRate => NativeHandle.GetAudioSampleRate();
143143

144144
/// <inheritdoc />
145145
public void Dispose() => NativeHandle.Dispose();

LLama/Native/GPUSplitMode.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ public enum GPUSplitMode
2020
/// split layers and KV across GPUs, use tensor parallelism if supported
2121
/// </summary>
2222
Row = 2,
23+
24+
// Undocumented in llama.h
25+
/// <summary>
26+
///
27+
/// </summary>
28+
Tensor = 3,
2329
}

LLama/Native/LLamaFtype.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ public enum LLamaFtype
212212
/// </summary>
213213
LLAMA_FTYPE_MOSTLY_NVFP4 = 39,
214214

215+
/// <summary>
216+
/// Except 1d tensors
217+
/// </summary>
218+
LLAMA_FTYPE_MOSTLY_Q1_0 = 40,
219+
215220
/// <summary>
216221
/// File type was not specified
217222
/// </summary>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace LLama.Native;
2+
3+
/* /// <summary>
4+
///
5+
/// </summary>
6+
/// <remarks>llama_model_imatrix_data</remarks>
7+
public unsafe struct LLamaModelImatrixData
8+
{
9+
char* name;
10+
float* data;
11+
nuint size;
12+
} */

LLama/Native/LLamaModelQuantizeParams.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,22 @@ public bool dry_run
9292
/// <summary>
9393
/// pointer to importance matrix data
9494
/// </summary>
95-
public IntPtr imatrix;
95+
public IntPtr imatrix; // LLamaModelImatrixData *
9696

9797
/// <summary>
9898
/// pointer to vector containing overrides
9999
/// </summary>
100-
public IntPtr kv_overrides;
100+
public IntPtr kv_overrides; // llama_model_kv_override *
101101

102102
/// <summary>
103103
/// pointer to vector containing tensor types
104104
/// </summary>
105-
public IntPtr tensor_types;
105+
public IntPtr tensor_types; // llama_model_tensor_override *
106106

107107
/// <summary>
108108
/// Pointer to vector containing layer indices to prune
109109
/// </summary>
110-
public IntPtr prune_layers;
110+
public IntPtr prune_layers; // int32 *
111111

112112
/// <summary>
113113
/// Create a LLamaModelQuantizeParams with default values
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace LLama.Native;
2+
3+
// Unsupported - we can't handle ggml_type since LlamaSharp doesn't wrap/expose ggml
4+
/*
5+
* struct llama_model_tensor_override {
6+
const char * pattern;
7+
enum ggml_type type; // GGMLType might work?
8+
};
9+
*/

LLama/Native/LoraAdapter.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,42 @@ public class LoraAdapter
2222
/// </summary>
2323
internal IntPtr Pointer { get; }
2424

25+
/// <summary>
26+
/// Indicates if this adapter has been unloaded
27+
/// </summary>
28+
internal bool Loaded { get; private set; }
29+
2530
internal LoraAdapter(SafeLlamaModelHandle model, string path, IntPtr nativePtr)
2631
{
2732
Model = model;
2833
Path = path;
2934
Pointer = nativePtr;
35+
Loaded = true;
36+
}
37+
38+
/// <summary>
39+
/// Unload this adapter
40+
/// </summary>
41+
public void Unload()
42+
{
43+
// Early exit if already unloaded
44+
if (!Loaded)
45+
return;
46+
47+
// If the model has been unloaded this handle will have been auto unloaded
48+
if (Model.IsClosed)
49+
{
50+
Loaded = false;
51+
return;
52+
}
53+
54+
// Unload
55+
Loaded = false;
56+
llama_adapter_lora_free(Pointer);
57+
58+
// Manually free a LoRA adapter. loaded adapters which have not been
59+
// freed will be automatically freed when the associated model is deleted
60+
[DllImport(NativeApi.libraryName, CallingConvention = CallingConvention.Cdecl)]
61+
static extern void llama_adapter_lora_free(IntPtr adapter);
3062
}
3163
}

LLama/Native/NativeApi.Mtmd.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,50 @@ internal struct mtmd_context_params
4141
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_context_params_default", CallingConvention = CallingConvention.Cdecl)]
4242
internal static extern mtmd_context_params mtmd_context_params_default();
4343

44+
/// <summary>
45+
/// whether we need to set non-causal mask before llama_decode
46+
/// if chunk is nullptr, we assume the default case where chunk is an image chunk
47+
/// </summary>
48+
/// <param name="ctx"></param>
49+
/// <returns></returns>
4450
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_decode_use_non_causal", CallingConvention = CallingConvention.Cdecl)]
4551
[return: MarshalAs(UnmanagedType.I1)]
4652
internal static extern bool mtmd_decode_use_non_causal(SafeMtmdModelHandle ctx);
4753

54+
/// <summary>
55+
/// whether the current model use M-RoPE for llama_decode
56+
/// </summary>
57+
/// <param name="ctx"></param>
58+
/// <returns></returns>
4859
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_decode_use_mrope", CallingConvention = CallingConvention.Cdecl)]
4960
[return: MarshalAs(UnmanagedType.I1)]
5061
internal static extern bool mtmd_decode_use_mrope(SafeMtmdModelHandle ctx);
5162

63+
/// <summary>
64+
/// whether the current model supports vision input
65+
/// </summary>
66+
/// <param name="ctx"></param>
67+
/// <returns></returns>
5268
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_support_vision", CallingConvention = CallingConvention.Cdecl)]
5369
[return: MarshalAs(UnmanagedType.I1)]
5470
internal static extern bool mtmd_support_vision(SafeMtmdModelHandle ctx);
5571

72+
/// <summary>
73+
/// whether the current model supports audio input
74+
/// </summary>
75+
/// <param name="ctx"></param>
76+
/// <returns></returns>
5677
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_support_audio", CallingConvention = CallingConvention.Cdecl)]
5778
[return: MarshalAs(UnmanagedType.I1)]
5879
internal static extern bool mtmd_support_audio(SafeMtmdModelHandle ctx);
5980

60-
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_get_audio_bitrate", CallingConvention = CallingConvention.Cdecl)]
61-
internal static extern int mtmd_get_audio_bitrate(SafeMtmdModelHandle ctx);
81+
/// <summary>
82+
/// get audio sample rate in Hz, for example 16000 for Whisper
83+
/// </summary>
84+
/// <param name="ctx"></param>
85+
/// <returns></returns>
86+
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_get_audio_sample_rate", CallingConvention = CallingConvention.Cdecl)]
87+
internal static extern int mtmd_get_audio_sample_rate(SafeMtmdModelHandle ctx);
6288

6389
// bitmap ------------------------------------------------------------
6490

@@ -153,9 +179,11 @@ internal static unsafe void mtmd_bitmap_set_id(SafeMtmdEmbed bitmap, string? id)
153179
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_image_tokens_get_n_tokens", CallingConvention = CallingConvention.Cdecl)]
154180
internal static extern UIntPtr mtmd_image_tokens_get_n_tokens(IntPtr image_tokens);
155181

182+
[Obsolete("use mtmd_image_tokens_get_decoder_pos() instead")]
156183
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_image_tokens_get_nx", CallingConvention = CallingConvention.Cdecl)]
157184
internal static extern UIntPtr mtmd_image_tokens_get_nx(IntPtr image_tokens);
158185

186+
[Obsolete("use mtmd_image_tokens_get_decoder_pos() instead")]
159187
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_image_tokens_get_ny", CallingConvention = CallingConvention.Cdecl)]
160188
internal static extern UIntPtr mtmd_image_tokens_get_ny(IntPtr image_tokens);
161189

@@ -165,6 +193,28 @@ internal static unsafe void mtmd_bitmap_set_id(SafeMtmdEmbed bitmap, string? id)
165193
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_image_tokens_get_n_pos", CallingConvention = CallingConvention.Cdecl)]
166194
internal static extern int mtmd_image_tokens_get_n_pos(IntPtr image_tokens);
167195

196+
[StructLayout(LayoutKind.Explicit)]
197+
internal struct mtmd_decoder_pos
198+
{
199+
[FieldOffset(0)]
200+
uint t;
201+
202+
[FieldOffset(4)]
203+
uint x;
204+
205+
[FieldOffset(8)]
206+
uint y;
207+
};
208+
209+
/// <summary>
210+
/// get position for decoder attention, to be used by M-RoPE models
211+
/// </summary>
212+
/// <param name="image_tokens"></param>
213+
/// <param name="i">i is the index of the embedding token, ranging from 0 to mtmd_image_tokens_get_n_tokens() - 1</param>
214+
/// <returns>return relative position (for example, embedding 0 will have position (0, 0, 0); remember to adjust it to the current absolute position)</returns>
215+
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_image_tokens_get_decoder_pos", CallingConvention = CallingConvention.Cdecl)]
216+
internal static extern mtmd_decoder_pos mtmd_image_tokens_get_decoder_pos(IntPtr image_tokens, nuint i);
217+
168218
// tokenize ----------------------------------------------------------
169219

170220
/// <summary>
@@ -259,6 +309,11 @@ internal static unsafe IntPtr mtmd_helper_bitmap_init_from_file(SafeMtmdModelHan
259309
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_helper_get_n_pos", CallingConvention = CallingConvention.Cdecl)]
260310
internal static extern int mtmd_helper_get_n_pos(SafeMtmdInputChunks chunks);
261311

312+
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_helper_image_get_decoder_pos", CallingConvention = CallingConvention.Cdecl)]
313+
// helper to get the list of relative positions corresponding to the embedding tokens, to be used by M-RoPE
314+
// out_pos must have length == mtmd_helper_get_n_tokens(image)
315+
internal static extern void mtmd_helper_image_get_decoder_pos(IntPtr /* mtmd_image_tokens* */ image, IntPtr /* mtmd_decoder_pos* */ out_pos);
316+
262317
[DllImport(mtmdLibraryName, EntryPoint = "mtmd_helper_eval_chunks", CallingConvention = CallingConvention.Cdecl)]
263318
internal static extern int mtmd_helper_eval_chunks(
264319
SafeMtmdModelHandle ctx,

0 commit comments

Comments
 (0)