char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
e6dd0e29a
char/aliceai-t5-moe
1#include "arg.h" 2#include "chat.h" 3#include "common.h" 4#include "diffusion.h" 5#include "llama.h" 6#include "log.h" 7 8#include <limits.h> 9 10#include <clocale> 11#include <cstring> 12#include <string> 13#include <vector> 14 15struct callback_data { 16diffusion_params * diff_params ; 17const llama_vocab * vocab ; 18int32_t n_input ; 19}; 20 21static bool diffusion_step_callback (int32_t step , 22int32_t total_steps , 23const llama_token * tokens , 24int32_t n_tokens , 25void * user_data ) { 26 (void )user_data ; 27 28callback_data * data = static_cast < callback_data *> (user_data ); 29 30auto print_progress_bar = [](int32_t step ,int32_t total_steps ) { 31int progress_percent = (step * 100 ) /total_steps ; 32int progress_bars = (step * 50 ) /total_steps ; 33LOG_INF ("\rdiffusion step: %d/%d [%s%s] %d%%" , 34step , 35total_steps , 36 std::string (progress_bars ,'=' ).c_str (), 37 std::string (50 - progress_bars ,' ' ).c_str (), 38progress_percent ); 39 }; 40 41if (data -> diff_params -> visual_mode ) { 42// Visual mode: clear 43LOG_INF ("\033[2J\033[H" );// Clear screen and move cursor to top-left 44 45print_progress_bar (step ,total_steps ); 46 47LOG_INF ("\n" ); 48 49 std::string current_text = " " ; 50 51for (int32_t i = data -> n_input ;i < n_tokens ;i ++ ) { 52 std::string token_str ; 53if (tokens [i ]!= llama_vocab_mask (data -> vocab )) { 54char piece [256 ]; 55int n_chars = llama_token_to_piece (data -> vocab ,tokens [i ],piece ,sizeof (piece ),0 , false); 56if (n_chars > 0 ) { 57piece [n_chars ]= '\0' ; 58token_str = piece ; 59 } 60 }else { 61token_str = " " ; 62 } 63 64current_text += token_str ; 65 } 66 67LOG_INF ("%s\n" ,current_text .c_str ()); 68 }else { 69print_progress_bar (step ,total_steps ); 70 } 71 72return true; 73} 74 75static std::string format_input_text (const std::string & prompt ,const std::string & system_prompt ,bool use_chat_template ,llama_model * model ) { 76if (!use_chat_template ) { 77return prompt ; 78 } 79 80auto chat_templates = common_chat_templates_init (model ,"" ); 81common_chat_templates_inputs inputs ; 82common_chat_msg system_msg ; 83 84if (!system_prompt .empty ()) { 85system_msg .role = "system" ; 86system_msg .content = system_prompt ; 87inputs .messages .push_back (system_msg ); 88 } 89 90common_chat_msg user_msg ; 91user_msg .role = "user" ; 92user_msg .content = prompt ; 93 94inputs .messages .push_back (user_msg ); 95inputs .add_generation_prompt = true; 96 97auto result = common_chat_templates_apply (chat_templates .get (),inputs ); 98 99return result .prompt ; 100} 101 102int main (int argc ,char ** argv ) { 103 std::setlocale (LC_NUMERIC ,"C" ); 104 105ggml_time_init (); 106 107common_params params ; 108 109common_init (); 110 111if (!common_params_parse (argc ,argv ,params ,LLAMA_EXAMPLE_DIFFUSION )) { 112return 1 ; 113 } 114 115llama_backend_init (); 116 117llama_model_params model_params = llama_model_default_params (); 118model_params .n_gpu_layers = params .n_gpu_layers ; 119model_params .devices = params .devices .data (); 120model_params .load_mode = params .load_mode ; 121model_params .check_tensors = params .check_tensors ; 122 123llama_model * model = llama_model_load_from_file (params .model .path .c_str (),model_params ); 124if (!model ) { 125LOG_ERR ("error: failed to load model '%s'\n" ,params .model .path .c_str ()); 126return 1 ; 127 } 128 129if (!llama_model_is_diffusion (model )) { 130LOG_ERR ("error: unsupported model for diffusion" ); 131llama_model_free (model ); 132return 1 ; 133 } 134 135llama_context_params ctx_params = llama_context_default_params (); 136ctx_params .n_ctx = params .n_ctx ; 137ctx_params .n_batch = params .n_batch ; 138ctx_params .n_ubatch = params .n_ubatch ; 139ctx_params .flash_attn_type = params .flash_attn_type ; 140ctx_params .no_perf = params .no_perf ; 141ctx_params .type_k = params .cache_type_k ; 142ctx_params .type_v = params .cache_type_v ; 143 144llama_context * ctx = llama_init_from_model (model ,ctx_params ); 145if (!ctx ) { 146LOG_ERR ("error: failed to create context\n" ); 147llama_model_free (model ); 148return 1 ; 149 } 150 151llama_set_n_threads (ctx ,params .cpuparams .n_threads ,params .cpuparams_batch .n_threads ); 152 153const llama_vocab * vocab = llama_model_get_vocab (model ); 154 155 std::string formatted_prompt = format_input_text (params .prompt ,params .system_prompt ,params .enable_chat_template ,model ); 156 157 std::vector < llama_token > input_tokens = common_tokenize (vocab , 158formatted_prompt , 159/*add special tokens*/ true, 160/*parse special*/ true); 161 162int n_input = input_tokens .size (); 163 164if (static_cast < uint32_t > (n_input ) >=llama_n_ctx (ctx )) { 165LOG_ERR ("error: input too long (%d tokens), max context is %d\n" ,n_input ,llama_n_ctx (ctx )); 166llama_free (ctx ); 167llama_model_free (model ); 168return 1 ; 169 } 170 171llama_token mask_token_id = llama_vocab_mask (vocab ); 172 173GGML_ASSERT (mask_token_id != LLAMA_TOKEN_NULL ); 174 175bool visual_mode = params .diffusion .visual_mode ; 176 177int32_t n_generated = 0 ; 178 std::vector < llama_token > output_tokens (params .n_ubatch ); 179 180struct diffusion_params diff_params ; 181 182char shift_logits_str [8 ]; 183if (llama_model_meta_val_str (model ,"diffusion.shift_logits" ,shift_logits_str ,sizeof (shift_logits_str )) >=0 ) { 184diff_params .shift_logits = (strcmp (shift_logits_str ,"true" )== 0 ); 185 }else { 186diff_params .shift_logits = true; 187 } 188 189//Use either eps or block length, but not both 190GGML_ASSERT ((params .diffusion .eps == 0 ) ^ (params .diffusion .block_length == 0 )); 191 192if (params .diffusion .eps ) { 193diff_params .schedule = DIFFUSION_TRANSFER_SCHEDULE_TIMESTEP_BASED ; 194diff_params .eps = params .diffusion .eps ; 195 }else if (params .diffusion .block_length ) { 196diff_params .schedule = DIFFUSION_TRANSFER_SCHEDULE_BLOCK_BASED ; 197diff_params .block_length = params .diffusion .block_length ; 198 } 199 200diff_params .mask_token_id = mask_token_id ; 201diff_params .seed = params .sampling .seed ; 202diff_params .temperature = params .sampling .temp ; 203diff_params .steps = params .diffusion .steps ; 204diff_params .algorithm = static_cast < diffusion_algorithm > (params .diffusion .algorithm ); 205diff_params .max_length = params .n_ubatch ; 206diff_params .top_p = params .sampling .top_p ; 207diff_params .top_k = params .sampling .top_k ; 208diff_params .visual_mode = params .diffusion .visual_mode ; 209diff_params .add_gumbel_noise = params .diffusion .add_gumbel_noise ; 210 211diff_params .step_callback = diffusion_step_callback ; 212callback_data cb_data = {& diff_params ,vocab ,n_input }; 213diff_params .step_callback_user_data = & cb_data ; 214 215const char * alg_names []= { 216"DIFFUSION_ALGORITHM_ORIGIN" , 217"DIFFUSION_ALGORITHM_ENTROPY_BASED" , 218"DIFFUSION_ALGORITHM_MARGIN_BASED" , 219"DIFFUSION_ALGORITHM_RANDOM" , 220"DIFFUSION_ALGORITHM_CONFIDENCE_BASED" , 221 }; 222const char * sched_names []= { 223"DIFFUSION_TRANSFER_SCHEDULE_TIMESTEP_BASED" , 224"DIFFUSION_TRANSFER_SCHEDULE_BLOCK_BASED" , 225 }; 226const char * alg_name = 227 (diff_params .algorithm >=0 && diff_params .algorithm <=4 ) ?alg_names [diff_params .algorithm ] :"UNKNOWN" ; 228const char * sched_name = 229 (diff_params .schedule >=0 && diff_params .schedule <=1 ) ?sched_names [diff_params .schedule ] :"UNKNOWN" ; 230 231LOG_INF ("diffusion_params: - %-25s llama_token = %d\n" ,"mask_token_id" ,mask_token_id ); 232LOG_INF ("diffusion_params: - %-25s u32 = %d\n" ,"steps" ,diff_params .steps ); 233LOG_INF ("diffusion_params: - %-25s u32 = %d\n" ,"max_length" ,diff_params .max_length ); 234LOG_INF ("diffusion_params: - %-25s enum = %d (%s)\n" ,"algorithm" ,diff_params .algorithm ,alg_name ); 235LOG_INF ("diffusion_params: - %-25s enum = %d (%s)\n" ,"schedule" ,diff_params .schedule ,sched_name ); 236LOG_INF ("diffusion_params: - %-25s f32 = %.3f\n" ,"temperature" ,diff_params .temperature ); 237if (diff_params .schedule == DIFFUSION_TRANSFER_SCHEDULE_TIMESTEP_BASED ) { 238LOG_INF ("diffusion_params: - %-25s f32 = %.6f\n" ,"eps" ,diff_params .eps ); 239LOG_INF ("diffusion_params: - %-25s f32 = %.3f\n" ,"alg_temp" ,diff_params .alg_temp ); 240 } 241if (diff_params .schedule == DIFFUSION_TRANSFER_SCHEDULE_BLOCK_BASED ) { 242LOG_INF ("diffusion_params: - %-25s u32 = %d\n" ,"block_length" ,diff_params .block_length ); 243LOG_INF ("diffusion_params: - %-25s f32 = %.3f\n" ,"cfg_scale" ,diff_params .cfg_scale ); 244 } 245 246diffusion_generate (ctx ,input_tokens .data (),output_tokens .data (),n_input ,diff_params ,n_generated ); 247 248if (n_generated > 0 ) { 249if (visual_mode ) { 250//clear screen and move cursor to top-left 251LOG_INF ("\033[2J\033[H" ); 252 } 253 254output_tokens .erase (output_tokens .begin (),output_tokens .begin ()+ n_input ); 255 std::string output_data = common_detokenize (vocab ,output_tokens , false); 256LOG_INF ("\n%s\n" ,output_data .c_str ()); 257 }else { 258LOG_INF ("Error: diffusion generation failed\n" ); 259 } 260 261llama_free (ctx ); 262llama_model_free (model ); 263llama_backend_free (); 264 265return 0 ; 266}