char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
733905474
char/aliceai-t5-moe
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <assert.h> 5 6#include "mtmd.h" 7#include "mtmd-helper.h" 8 9int main (void ) { 10printf ("\n\nTesting libmtmd C API...\n" ); 11printf ("--------\n\n" ); 12 13struct mtmd_context_params params = mtmd_context_params_default (); 14printf ("Default image marker: %s\n" ,params .image_marker ); 15 16mtmd_input_chunks * chunks = mtmd_test_create_input_chunks (); 17 18if (!chunks ) { 19fprintf (stderr ,"Failed to create input chunks\n" ); 20return 1 ; 21 } 22 23// simple test for the helper 24size_t n_tokens_total = mtmd_helper_get_n_tokens (chunks ); 25printf ("Total tokens in chunks: %zu\n" ,n_tokens_total ); 26assert (n_tokens_total > 0 ); 27 28size_t n_chunks = mtmd_input_chunks_size (chunks ); 29printf ("Number of chunks: %zu\n" ,n_chunks ); 30assert (n_chunks > 0 ); 31 32for (size_t i = 0 ;i < n_chunks ;i ++ ) { 33const mtmd_input_chunk * chunk = mtmd_input_chunks_get (chunks ,i ); 34assert (chunk != NULL ); 35enum mtmd_input_chunk_type type = mtmd_input_chunk_get_type (chunk ); 36printf ("Chunk %zu type: %d\n" ,i ,type ); 37 38if (type == MTMD_INPUT_CHUNK_TYPE_TEXT ) { 39size_t n_tokens ; 40const llama_token * tokens = mtmd_input_chunk_get_tokens_text (chunk ,& n_tokens ); 41printf (" Text chunk with %zu tokens\n" ,n_tokens ); 42assert (tokens != NULL ); 43assert (n_tokens > 0 ); 44for (size_t j = 0 ;j < n_tokens ;j ++ ) { 45assert (tokens [j ] >=0 ); 46printf (" > Token %zu: %d\n" ,j ,tokens [j ]); 47 } 48 49 }else if (type == MTMD_INPUT_CHUNK_TYPE_IMAGE ) { 50const mtmd_image_tokens * image_tokens = mtmd_input_chunk_get_tokens_image (chunk ); 51size_t n_tokens = mtmd_image_tokens_get_n_tokens (image_tokens ); 52// get position of the last token, which should be (nx - 1, ny - 1) 53struct mtmd_decoder_pos pos = mtmd_image_tokens_get_decoder_pos (image_tokens ,0 ,n_tokens - 1 ); 54size_t nx = pos .x + 1 ; 55size_t ny = pos .y + 1 ; 56const char * id = mtmd_image_tokens_get_id (image_tokens ); 57assert (n_tokens > 0 ); 58assert (nx > 0 ); 59assert (ny > 0 ); 60assert (id != NULL ); 61printf (" Image chunk with %zu tokens\n" ,n_tokens ); 62printf (" Image size: %zu x %zu\n" ,nx ,ny ); 63printf (" Image ID: %s\n" ,id ); 64 } 65 } 66 67// test chunk save/load round-trip 68for (size_t i = 0 ;i < n_chunks ;i ++ ) { 69const mtmd_input_chunk * chunk = mtmd_input_chunks_get (chunks ,i ); 70assert (chunk != NULL ); 71enum mtmd_input_chunk_type type = mtmd_input_chunk_get_type (chunk ); 72 73// query the required buffer size (out_buf == NULL) 74size_t expected_len = 0 ; 75int32_t rc = mtmd_input_chunk_save (chunk ,NULL ,0 ,& expected_len ); 76printf (" Chunk %zu: save query rc = %d, expected_len = %zu\n" ,i ,rc ,expected_len ); 77assert (rc == 0 ); 78assert (expected_len > 0 ); 79 80// saving into a too-small buffer must fail, not crash 81char tiny_buf [1 ]; 82rc = mtmd_input_chunk_save (chunk ,tiny_buf ,sizeof (tiny_buf ),NULL ); 83printf (" Chunk %zu: save into too-small buffer rc = %d (expect non-zero)\n" ,i ,rc ); 84assert (rc != 0 ); 85 86// save into a properly-sized buffer 87char * buf = (char * )malloc (expected_len ); 88assert (buf != NULL ); 89rc = mtmd_input_chunk_save (chunk ,buf ,expected_len ,NULL ); 90assert (rc == 0 ); 91 92// loading from a truncated buffer must fail gracefully, not crash 93if (expected_len > 1 ) { 94mtmd_input_chunk * bad = mtmd_input_chunk_load (buf ,expected_len - 1 ); 95printf (" Chunk %zu: load from truncated buffer = %p (expect NULL)\n" ,i , (void * )bad ); 96assert (bad == NULL ); 97 } 98 99// load it back 100mtmd_input_chunk * loaded = mtmd_input_chunk_load (buf ,expected_len ); 101assert (loaded != NULL ); 102 103// metadata must match the original chunk 104assert (mtmd_input_chunk_get_type (loaded )== type ); 105assert (mtmd_input_chunk_get_n_tokens (loaded )== mtmd_input_chunk_get_n_tokens (chunk )); 106assert (mtmd_input_chunk_get_n_pos (loaded )== mtmd_input_chunk_get_n_pos (chunk )); 107 108if (type == MTMD_INPUT_CHUNK_TYPE_TEXT ) { 109size_t n_tok_orig ,n_tok_loaded ; 110const llama_token * tok_orig = mtmd_input_chunk_get_tokens_text (chunk ,& n_tok_orig ); 111const llama_token * tok_loaded = mtmd_input_chunk_get_tokens_text (loaded ,& n_tok_loaded ); 112printf (" Chunk %zu: loaded %zu text tokens (orig %zu), first token %d (orig %d)\n" , 113i ,n_tok_loaded ,n_tok_orig , 114n_tok_loaded > 0 ?tok_loaded [0 ] :-1 , 115n_tok_orig > 0 ?tok_orig [0 ] :-1 ); 116assert (n_tok_orig == n_tok_loaded ); 117for (size_t j = 0 ;j < n_tok_orig ;j ++ ) { 118assert (tok_orig [j ]== tok_loaded [j ]); 119 } 120 }else if (type == MTMD_INPUT_CHUNK_TYPE_IMAGE || type == MTMD_INPUT_CHUNK_TYPE_AUDIO ) { 121const char * id_orig = mtmd_input_chunk_get_id (chunk ); 122const char * id_loaded = mtmd_input_chunk_get_id (loaded ); 123printf (" Chunk %zu: loaded id '%s' (orig '%s')\n" ,i ,id_loaded ,id_orig ); 124assert (id_orig != NULL && id_loaded != NULL ); 125assert (strcmp (id_orig ,id_loaded )== 0 ); 126 } 127 128mtmd_input_chunk_free (loaded ); 129free (buf ); 130 } 131printf ("Chunk save/load round-trip OK\n" ); 132 133// test input validation of mtmd_tokenize_from_parts() 134// invalid parts are rejected before the ctx is used, so NULL ctx is OK here 135 { 136mtmd_input_chunks * out = mtmd_input_chunks_init (); 137mtmd_bitmap * bmp = mtmd_bitmap_init (4 ,4 ,NULL );// placeholder bitmap 138struct mtmd_input_text txt = {"hello" ,5 , false, false }; 139struct mtmd_input_text txt_null = {NULL ,0 , false, false }; 140 141struct mtmd_input_part part_both = {& txt ,bmp }; 142struct mtmd_input_part part_neither = {NULL ,NULL }; 143struct mtmd_input_part part_null_text = {& txt_null ,NULL }; 144const mtmd_input_part * parts [1 ]; 145int32_t rc ; 146 147parts [0 ]= & part_both ; 148rc = mtmd_tokenize_from_parts (NULL ,out ,parts ,1 , false); 149printf ("tokenize part with both text and bitmap rc = %d (expect 1)\n" ,rc ); 150assert (rc == 1 ); 151 152parts [0 ]= & part_neither ; 153rc = mtmd_tokenize_from_parts (NULL ,out ,parts ,1 , false); 154printf ("tokenize part with neither text nor bitmap rc = %d (expect 1)\n" ,rc ); 155assert (rc == 1 ); 156 157parts [0 ]= & part_null_text ; 158rc = mtmd_tokenize_from_parts (NULL ,out ,parts ,1 , false); 159printf ("tokenize part with null text pointer rc = %d (expect 1)\n" ,rc ); 160assert (rc == 1 ); 161 162mtmd_bitmap_free (bmp ); 163mtmd_input_chunks_free (out ); 164 } 165 166// Free the chunks 167mtmd_input_chunks_free (chunks ); 168 169printf ("\n\nDONE: test libmtmd C API...\n" ); 170 171return 0 ; 172}