char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
657e01125
char/aliceai-t5-moe
1#include "arg.h" 2#include "common.h" 3#include "log.h" 4#include "llama-cpp.h" 5#include "../src/llama-ext.h" 6#include "ggml.h" 7#include "gguf-model-data.h" 8#include "gguf.h" 9#include "ggml-backend.h" 10#include "download.h" 11 12#include <array> 13#include <vector> 14#include <set> 15#include <fstream> 16#include <iostream> 17#include <random> 18 19// Noop because weights are not needed 20static void set_tensor_data (struct ggml_tensor * tensor ,void * userdata ) { 21GGML_UNUSED (tensor ); 22GGML_UNUSED (userdata ); 23} 24 25struct input_tensor { 26ggml_type type ; 27 std::array < int64_t ,4 > ne ; 28 std::array < size_t ,4 > nb ; 29 30input_tensor (ggml_type type ,int64_t * ne ,size_t * nb ):type (type ) { 31memcpy (this -> ne .data (),ne ,4 * sizeof (int64_t )); 32memcpy (this -> nb .data (),nb ,4 * sizeof (size_t )); 33 } 34 35bool operator< (const input_tensor & b )const { 36return std::tie (type ,ne ,nb )< 37 std::tie (b .type ,b .ne ,b .nb ); 38 } 39 40void serialize (std::ostream & out )const { 41out <<type <<' ' ; 42for (size_t i = 0 ;i < 4 ;i ++ ) { 43out <<ne [i ] <<' ' ; 44 } 45for (size_t i = 0 ;i < 4 ;i ++ ) { 46out <<nb [i ] <<' ' ; 47 } 48 } 49}; 50 51struct test_object { 52ggml_op op ; 53ggml_type type ; 54 std::array < int64_t ,4 > ne ; 55 std::vector < int32_t > op_params ; 56 std::vector < input_tensor > sources ; 57 std::string name ; 58 59void serialize (std::ostream & out )const { 60out <<op <<' ' <<type <<' ' ; 61for (size_t i = 0 ;i < 4 ;i ++ ) { 62out <<ne [i ] <<' ' ; 63 } 64 65out <<op_params .size () <<' ' ; 66for (size_t i = 0 ;i < op_params .size ();i ++ ) { 67out <<op_params [i ] <<' ' ; 68 } 69 70out <<sources .size () <<' ' ; 71for (size_t s = 0 ;s < sources .size ();s ++ ) { 72sources [s ].serialize (out ); 73 } 74 75if (!name .empty ()) { 76out <<name ; 77 }else { 78out <<'-' ; 79 } 80 81out <<'\n' ; 82 } 83 84bool operator< (const test_object & b )const { 85return std::tie (op ,type ,ne ,op_params ,sources )< 86 std::tie (b .op ,b .type ,b .ne ,b .op_params ,b .sources ); 87 } 88}; 89 90static void extract_graph_ops (ggml_cgraph * cgraph ,const char * label , std::set < test_object > & tests ) { 91int n_nodes = ggml_graph_n_nodes (cgraph ); 92int n_skipped = 0 ; 93int n_before = (int )tests .size (); 94for (int i = 0 ;i < n_nodes ;i ++ ) { 95ggml_tensor * node = ggml_graph_node (cgraph ,i ); 96 97if (node -> op == GGML_OP_NONE || node -> op == GGML_OP_VIEW || node -> op == GGML_OP_RESHAPE || node -> op == GGML_OP_PERMUTE || node -> op == GGML_OP_TRANSPOSE ) { 98n_skipped ++ ; 99continue ; 100 } 101 102test_object test ; 103 104test .op = node -> op ; 105test .type = node -> type ; 106memcpy (& test .ne ,node -> ne ,4 * sizeof (int64_t )); 107 108test .op_params .resize (GGML_MAX_OP_PARAMS /sizeof (int32_t )); 109memcpy (test .op_params .data (),node -> op_params ,GGML_MAX_OP_PARAMS ); 110 111for (size_t s = 0 ;s < GGML_MAX_SRC ;s ++ ) { 112if (node -> src [s ]== nullptr ) { 113break ; 114 } 115 116test .sources .emplace_back (node -> src [s ]-> type ,node -> src [s ]-> ne ,node -> src [s ]-> nb ); 117 } 118 119test .name = node -> name ; 120tests .insert (test ); 121 } 122 123int n_new = (int )tests .size ()- n_before ; 124LOG_INF ("%s: %d unique ops, %d total nodes, %d skipped (view ops)\n" , 125label ,n_new ,n_nodes ,n_skipped ); 126} 127 128int main (int argc ,char ** argv ) { 129common_params params ; 130params .out_file = "tests.txt" ; 131 132common_init (); 133 134if (!common_params_parse (argc ,argv ,params ,LLAMA_EXAMPLE_EXPORT_GRAPH_OPS )) { 135return 1 ; 136 } 137 138// Load CPU-only 139ggml_backend_dev_t cpu_device = ggml_backend_dev_by_type (GGML_BACKEND_DEVICE_TYPE_CPU ); 140params .devices = {cpu_device ,nullptr }; 141params .fit_params = false; 142params .n_gpu_layers = 0 ; 143 144params .warmup = false; 145 146llama_context * ctx ; 147common_init_result_ptr init_result ; 148llama_context_ptr ctx2 ; 149llama_model_ptr model ; 150 151if (params .model .hf_repo .empty ()) { 152init_result = common_init_from_params (params ); 153 154ctx = init_result -> context (); 155if (!ctx ) { 156LOG_ERR ("failed to initialize params\n" ); 157return 1 ; 158 } 159 }else { 160#ifdef LLAMA_HF_FETCH 161auto [hf_repo ,hf_quant ]= common_download_split_repo_tag (params .model .hf_repo ); 162if (hf_quant .empty ()|| hf_quant == "latest" ) { 163hf_quant = "Q4_K_M" ; 164 } 165 166gguf_context_ptr gguf_ctx = gguf_fetch_gguf_ctx (hf_repo ,hf_quant ); 167if (!gguf_ctx ) { 168LOG_ERR ("failed to fetch GGUF metadata from %s\n" ,hf_repo .c_str ()); 169return 1 ; 170 } 171 172llama_model_params model_params = llama_model_default_params (); 173model_params .devices = params .devices .data (); 174model_params .no_alloc = true; 175 176model .reset (llama_model_init_from_user (gguf_ctx .get (),set_tensor_data ,nullptr ,model_params )); 177 178if (!model ) { 179LOG_ERR ("failed to create llama_model from %s\n" ,hf_repo .c_str ()); 180return 1 ; 181 } 182 183llama_context_params ctx_params = llama_context_default_params (); 184ctx2 .reset (llama_init_from_model (model .get (),ctx_params )); 185ctx = ctx2 .get (); 186 187if (!ctx ) { 188LOG_ERR ("failed to create llama_context\n" ); 189return 1 ; 190 } 191#else 192LOG_ERR ("test-export-graph-ops compiled without HF fetch support\n" ); 193return 1 ; 194#endif 195 } 196 197const uint32_t n_seqs = llama_n_seq_max (ctx ); 198const uint32_t n_tokens = std::min (llama_n_ctx (ctx ),llama_n_ubatch (ctx )); 199 200 std::set < test_object > tests ; 201 202auto * gf_pp = llama_graph_reserve (ctx ,n_tokens ,n_seqs ,n_tokens ); 203if (!gf_pp ) { 204LOG_ERR ("failed to reserve prompt processing graph\n" ); 205return 1 ; 206 } 207extract_graph_ops (gf_pp ,"pp" ,tests ); 208 209auto * gf_tg = llama_graph_reserve (ctx ,n_seqs ,n_seqs ,n_seqs ); 210if (!gf_tg ) { 211LOG_ERR ("failed to reserve token generation graph\n" ); 212return 1 ; 213 } 214extract_graph_ops (gf_tg ,"tg" ,tests ); 215 216LOG_INF ("%d unique ops total\n" , (int )tests .size ()); 217 218 std::ofstream f (params .out_file ); 219 220if (!f .is_open ()) { 221LOG_ERR ("unable to open output file: %s\n" ,params .out_file .c_str ()); 222return 1 ; 223 } 224 225for (const auto & test :tests ) { 226test .serialize (f ); 227 } 228 229return 0 ; 230}