11using System ;
2+ using static LLama . Native . SafeMtmdInputChunk ;
23
34namespace LLama . Native ;
45
@@ -42,64 +43,61 @@ internal struct mtmd_context_params
4243
4344 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_decode_use_non_causal" , CallingConvention = CallingConvention . Cdecl ) ]
4445 [ return : MarshalAs ( UnmanagedType . I1 ) ]
45- internal static extern bool mtmd_decode_use_non_causal ( IntPtr ctx ) ;
46+ internal static extern bool mtmd_decode_use_non_causal ( SafeMtmdModelHandle ctx ) ;
4647
4748 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_decode_use_mrope" , CallingConvention = CallingConvention . Cdecl ) ]
4849 [ return : MarshalAs ( UnmanagedType . I1 ) ]
49- internal static extern bool mtmd_decode_use_mrope ( IntPtr ctx ) ;
50+ internal static extern bool mtmd_decode_use_mrope ( SafeMtmdModelHandle ctx ) ;
5051
5152 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_support_vision" , CallingConvention = CallingConvention . Cdecl ) ]
5253 [ return : MarshalAs ( UnmanagedType . I1 ) ]
53- internal static extern bool mtmd_support_vision ( IntPtr ctx ) ;
54+ internal static extern bool mtmd_support_vision ( SafeMtmdModelHandle ctx ) ;
5455
5556 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_support_audio" , CallingConvention = CallingConvention . Cdecl ) ]
5657 [ return : MarshalAs ( UnmanagedType . I1 ) ]
57- internal static extern bool mtmd_support_audio ( IntPtr ctx ) ;
58+ internal static extern bool mtmd_support_audio ( SafeMtmdModelHandle ctx ) ;
5859
5960 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_get_audio_bitrate" , CallingConvention = CallingConvention . Cdecl ) ]
60- internal static extern int mtmd_get_audio_bitrate ( IntPtr ctx ) ;
61+ internal static extern int mtmd_get_audio_bitrate ( SafeMtmdModelHandle ctx ) ;
6162
6263 // bitmap ------------------------------------------------------------
6364
6465 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_init" , CallingConvention = CallingConvention . Cdecl ) ]
65- internal static extern IntPtr mtmd_bitmap_init ( uint nx , uint ny , IntPtr data ) ;
66+ internal static extern unsafe IntPtr mtmd_bitmap_init ( uint nx , uint ny , byte * data ) ;
6667
6768 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_init_from_audio" , CallingConvention = CallingConvention . Cdecl ) ]
68- internal static extern IntPtr mtmd_bitmap_init_from_audio ( ulong n_samples , IntPtr data ) ;
69+ internal static extern unsafe IntPtr mtmd_bitmap_init_from_audio ( ulong n_samples , float * data ) ;
6970
7071 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_get_nx" , CallingConvention = CallingConvention . Cdecl ) ]
71- internal static extern uint mtmd_bitmap_get_nx ( IntPtr bitmap ) ;
72+ internal static extern uint mtmd_bitmap_get_nx ( SafeMtmdEmbed bitmap ) ;
7273
7374 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_get_ny" , CallingConvention = CallingConvention . Cdecl ) ]
74- internal static extern uint mtmd_bitmap_get_ny ( IntPtr bitmap ) ;
75+ internal static extern uint mtmd_bitmap_get_ny ( SafeMtmdEmbed bitmap ) ;
7576
7677 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_get_data" , CallingConvention = CallingConvention . Cdecl ) ]
77- internal static extern IntPtr mtmd_bitmap_get_data ( IntPtr bitmap ) ;
78+ internal static extern IntPtr mtmd_bitmap_get_data ( SafeMtmdEmbed bitmap ) ;
7879
7980 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_get_n_bytes" , CallingConvention = CallingConvention . Cdecl ) ]
80- internal static extern UIntPtr mtmd_bitmap_get_n_bytes ( IntPtr bitmap ) ;
81+ internal static extern UIntPtr mtmd_bitmap_get_n_bytes ( SafeMtmdEmbed bitmap ) ;
8182
8283 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_is_audio" , CallingConvention = CallingConvention . Cdecl ) ]
8384 [ return : MarshalAs ( UnmanagedType . I1 ) ]
84- internal static extern bool mtmd_bitmap_is_audio ( IntPtr bitmap ) ;
85+ internal static extern bool mtmd_bitmap_is_audio ( SafeMtmdEmbed bitmap ) ;
8586
8687 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_free" , CallingConvention = CallingConvention . Cdecl ) ]
8788 internal static extern void mtmd_bitmap_free ( IntPtr bitmap ) ;
8889
8990 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_get_id" , CallingConvention = CallingConvention . Cdecl ) ]
90- internal static extern IntPtr mtmd_bitmap_get_id ( IntPtr bitmap ) ;
91+ internal static extern IntPtr mtmd_bitmap_get_id ( SafeMtmdEmbed bitmap ) ;
9192
9293 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_bitmap_set_id" , CallingConvention = CallingConvention . Cdecl ) ]
93- private static extern unsafe void mtmd_bitmap_set_id_native ( IntPtr bitmap , byte * id ) ;
94+ private static extern unsafe void mtmd_bitmap_set_id_native ( SafeMtmdEmbed bitmap , byte * id ) ;
9495
9596 /// <summary>
9697 /// Assign an identifier to a bitmap using a UTF-8 encoded string.
9798 /// </summary>
98- internal static unsafe void mtmd_bitmap_set_id ( IntPtr bitmap , string ? id )
99+ internal static unsafe void mtmd_bitmap_set_id ( SafeMtmdEmbed bitmap , string ? id )
99100 {
100- if ( bitmap == IntPtr . Zero )
101- throw new ArgumentNullException ( nameof ( bitmap ) ) ;
102-
103101 if ( id is null )
104102 {
105103 mtmd_bitmap_set_id_native ( bitmap , null ) ;
@@ -127,25 +125,25 @@ internal static unsafe void mtmd_bitmap_set_id(IntPtr bitmap, string? id)
127125 // input_chunk -------------------------------------------------------
128126
129127 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_get_type" , CallingConvention = CallingConvention . Cdecl ) ]
130- internal static extern int mtmd_input_chunk_get_type ( IntPtr chunk ) ;
128+ internal static extern SafeMtmdInputChunkType mtmd_input_chunk_get_type ( SafeMtmdInputChunk chunk ) ;
131129
132130 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_get_tokens_text" , CallingConvention = CallingConvention . Cdecl ) ]
133- internal static extern IntPtr mtmd_input_chunk_get_tokens_text ( IntPtr chunk , out UIntPtr n_tokens ) ;
131+ internal static extern IntPtr mtmd_input_chunk_get_tokens_text ( SafeMtmdInputChunk chunk , out UIntPtr n_tokens ) ;
134132
135133 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_get_tokens_image" , CallingConvention = CallingConvention . Cdecl ) ]
136134 internal static extern IntPtr mtmd_input_chunk_get_tokens_image ( IntPtr chunk ) ;
137135
138136 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_get_n_tokens" , CallingConvention = CallingConvention . Cdecl ) ]
139- internal static extern UIntPtr mtmd_input_chunk_get_n_tokens ( IntPtr chunk ) ;
137+ internal static extern UIntPtr mtmd_input_chunk_get_n_tokens ( SafeMtmdInputChunk chunk ) ;
140138
141139 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_get_id" , CallingConvention = CallingConvention . Cdecl ) ]
142- internal static extern IntPtr mtmd_input_chunk_get_id ( IntPtr chunk ) ;
140+ internal static extern IntPtr mtmd_input_chunk_get_id ( SafeMtmdInputChunk chunk ) ;
143141
144142 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_get_n_pos" , CallingConvention = CallingConvention . Cdecl ) ]
145- internal static extern int mtmd_input_chunk_get_n_pos ( IntPtr chunk ) ;
143+ internal static extern int mtmd_input_chunk_get_n_pos ( SafeMtmdInputChunk chunk ) ;
146144
147145 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_copy" , CallingConvention = CallingConvention . Cdecl ) ]
148- internal static extern IntPtr mtmd_input_chunk_copy ( IntPtr chunk ) ;
146+ internal static extern IntPtr mtmd_input_chunk_copy ( SafeMtmdInputChunk chunk ) ;
149147
150148 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_input_chunk_free" , CallingConvention = CallingConvention . Cdecl ) ]
151149 internal static extern void mtmd_input_chunk_free ( IntPtr chunk ) ;
@@ -203,19 +201,27 @@ public MtmdInputTextScope(string text, bool addSpecial, bool parseSpecial)
203201
204202 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_tokenize" , CallingConvention = CallingConvention . Cdecl ) ]
205203 private static extern unsafe int mtmd_tokenize_native (
206- IntPtr ctx ,
204+ SafeMtmdModelHandle ctx ,
207205 IntPtr output ,
208206 mtmd_input_text_native * text ,
209207 IntPtr [ ] bitmaps ,
210208 UIntPtr n_bitmaps ) ;
211209
212- internal static unsafe int mtmd_tokenize ( IntPtr ctx , IntPtr output , in mtmd_input_text_native text , IntPtr [ ] bitmaps , UIntPtr n_bitmaps )
210+ [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_tokenize" , CallingConvention = CallingConvention . Cdecl ) ]
211+ private static extern unsafe int mtmd_tokenize_native (
212+ SafeMtmdModelHandle ctx ,
213+ IntPtr output ,
214+ mtmd_input_text_native * text ,
215+ SafeMtmdEmbed [ ] bitmaps ,
216+ UIntPtr n_bitmaps ) ;
217+
218+ internal static unsafe int mtmd_tokenize ( SafeMtmdModelHandle ctx , IntPtr output , in mtmd_input_text_native text , IntPtr [ ] bitmaps , nuint n_bitmaps )
213219 {
214220 var temp = text ;
215221 return mtmd_tokenize_native ( ctx , output , & temp , bitmaps , n_bitmaps ) ;
216222 }
217223
218- internal static unsafe int mtmd_tokenize ( IntPtr ctx , IntPtr output , string text , bool addSpecial , bool parseSpecial , IntPtr [ ] bitmaps , UIntPtr n_bitmaps )
224+ internal static unsafe int mtmd_tokenize ( SafeMtmdModelHandle ctx , IntPtr output , string text , bool addSpecial , bool parseSpecial , IntPtr [ ] bitmaps , nuint n_bitmaps )
219225 {
220226 using var scope = new MtmdInputTextScope ( text , addSpecial , parseSpecial ) ;
221227 return mtmd_tokenize_native ( ctx , output , & scope . Value , bitmaps , n_bitmaps ) ;
@@ -236,28 +242,28 @@ internal static unsafe int mtmd_tokenize(IntPtr ctx, IntPtr output, string text,
236242 internal static extern IntPtr mtmd_test_create_input_chunks ( ) ;
237243
238244 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_helper_bitmap_init_from_file" , CallingConvention = CallingConvention . Cdecl ) ]
239- private static extern unsafe IntPtr mtmd_helper_bitmap_init_from_file_native ( IntPtr ctx , byte * fname ) ;
245+ private static extern unsafe IntPtr mtmd_helper_bitmap_init_from_file_native ( SafeMtmdModelHandle ctx , byte * fname ) ;
240246
241- internal static unsafe IntPtr mtmd_helper_bitmap_init_from_file ( IntPtr ctx , string fname )
247+ internal static unsafe IntPtr mtmd_helper_bitmap_init_from_file ( SafeMtmdModelHandle ctx , string fname )
242248 {
243249 using var pinned = PinnedUtf8String . Create ( fname ) ?? throw new ArgumentNullException ( nameof ( fname ) ) ;
244250 return mtmd_helper_bitmap_init_from_file_native ( ctx , ( byte * ) pinned . Pointer ) ;
245251 }
246252
247253 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_helper_bitmap_init_from_buf" , CallingConvention = CallingConvention . Cdecl ) ]
248- internal static extern IntPtr mtmd_helper_bitmap_init_from_buf ( IntPtr ctx , IntPtr buf , UIntPtr len ) ;
254+ internal static extern unsafe IntPtr mtmd_helper_bitmap_init_from_buf ( SafeMtmdModelHandle ctx , byte * buf , nuint len ) ;
249255
250256 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_helper_get_n_tokens" , CallingConvention = CallingConvention . Cdecl ) ]
251- internal static extern UIntPtr mtmd_helper_get_n_tokens ( IntPtr chunks ) ;
257+ internal static extern UIntPtr mtmd_helper_get_n_tokens ( SafeMtmdInputChunks chunks ) ;
252258
253259 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_helper_get_n_pos" , CallingConvention = CallingConvention . Cdecl ) ]
254- internal static extern int mtmd_helper_get_n_pos ( IntPtr chunks ) ;
260+ internal static extern int mtmd_helper_get_n_pos ( SafeMtmdInputChunks chunks ) ;
255261
256262 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_helper_eval_chunks" , CallingConvention = CallingConvention . Cdecl ) ]
257263 internal static extern int mtmd_helper_eval_chunks (
258- IntPtr ctx ,
259- IntPtr lctx ,
260- IntPtr chunks ,
264+ SafeMtmdModelHandle ctx ,
265+ SafeLLamaContextHandle lctx ,
266+ SafeMtmdInputChunks chunks ,
261267 int n_past ,
262268 int seq_id ,
263269 int n_batch ,
@@ -266,8 +272,8 @@ internal static extern int mtmd_helper_eval_chunks(
266272
267273 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_helper_eval_chunk_single" , CallingConvention = CallingConvention . Cdecl ) ]
268274 internal static extern int mtmd_helper_eval_chunk_single (
269- IntPtr ctx ,
270- IntPtr lctx ,
275+ SafeMtmdModelHandle ctx ,
276+ SafeLLamaContextHandle lctx ,
271277 IntPtr chunk ,
272278 int n_past ,
273279 int seq_id ,
@@ -277,8 +283,8 @@ internal static extern int mtmd_helper_eval_chunk_single(
277283
278284 [ DllImport ( mtmdLibraryName , EntryPoint = "mtmd_helper_decode_image_chunk" , CallingConvention = CallingConvention . Cdecl ) ]
279285 internal static extern int mtmd_helper_decode_image_chunk (
280- IntPtr ctx ,
281- IntPtr lctx ,
286+ SafeMtmdModelHandle ctx ,
287+ SafeLLamaContextHandle lctx ,
282288 IntPtr chunk ,
283289 IntPtr encoded_embd ,
284290 int n_past ,
0 commit comments