|
| 1 | +--- examples/main/main.cpp 2025-10-31 22:21:46 |
| 2 | ++++ ../whisper.cpp.patches/main.cpp 2025-10-31 16:01:53 |
| 3 | +@@ -1,4 +1,11 @@ |
| 4 | ++// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*- |
| 5 | ++// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi |
| 6 | ++ |
| 7 | ++#include "llamafile/version.h" |
| 8 | ++#include "llamafile/llamafile.h" |
| 9 | ++#include "llama.cpp/cores.h" |
| 10 | + #include "common.h" |
| 11 | ++#include "slurp.h" |
| 12 | + |
| 13 | + #include "whisper.h" |
| 14 | + #include "grammar-parser.h" |
| 15 | +@@ -11,24 +18,19 @@ |
| 16 | + #include <thread> |
| 17 | + #include <vector> |
| 18 | + #include <cstring> |
| 19 | ++#include <cosmo.h> |
| 20 | + |
| 21 | ++#include "llamafile/llamafile.h" |
| 22 | ++#include "llamafile/debug.h" |
| 23 | ++#include "llama.cpp/string.h" |
| 24 | ++ |
| 25 | + #if defined(_MSC_VER) |
| 26 | + #pragma warning(disable: 4244 4267) // possible loss of data |
| 27 | + #endif |
| 28 | + |
| 29 | +-// helper function to replace substrings |
| 30 | +-static void replace_all(std::string & s, const std::string & search, const std::string & replace) { |
| 31 | +- for (size_t pos = 0; ; pos += replace.length()) { |
| 32 | +- pos = s.find(search, pos); |
| 33 | +- if (pos == std::string::npos) break; |
| 34 | +- s.erase(pos, search.length()); |
| 35 | +- s.insert(pos, replace); |
| 36 | +- } |
| 37 | +-} |
| 38 | +- |
| 39 | + // command-line parameters |
| 40 | + struct whisper_params { |
| 41 | +- int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()); |
| 42 | ++ int32_t n_threads = std::min(16, cpu_get_num_math()); |
| 43 | + int32_t n_processors = 1; |
| 44 | + int32_t offset_t_ms = 0; |
| 45 | + int32_t offset_n = 0; |
| 46 | +@@ -68,7 +70,6 @@ |
| 47 | + bool print_progress = false; |
| 48 | + bool no_timestamps = false; |
| 49 | + bool log_score = false; |
| 50 | +- bool use_gpu = true; |
| 51 | + bool flash_attn = false; |
| 52 | + |
| 53 | + std::string language = "en"; |
| 54 | +@@ -91,7 +92,7 @@ |
| 55 | + std::vector<std::string> fname_inp = {}; |
| 56 | + std::vector<std::string> fname_out = {}; |
| 57 | + |
| 58 | +- grammar_parser::parse_state grammar_parsed; |
| 59 | ++ whisper_grammar_parser::parse_state grammar_parsed; |
| 60 | + }; |
| 61 | + |
| 62 | + static void whisper_print_usage(int argc, char ** argv, const whisper_params & params); |
| 63 | +@@ -118,6 +119,40 @@ |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | ++ if (arg == "--log-disable") { |
| 68 | ++ FLAG_log_disable = true; |
| 69 | ++ } else if (arg == "--cli") { |
| 70 | ++ } else if (arg == "--server") { |
| 71 | ++ } else if (arg == "--fast") { |
| 72 | ++ FLAG_fast = true; |
| 73 | ++ } else if (arg == "--precise") { |
| 74 | ++ FLAG_precise = true; |
| 75 | ++ } else if (arg == "--trace") { |
| 76 | ++ FLAG_trace = true; |
| 77 | ++ } else if (arg == "--trap") { |
| 78 | ++ FLAG_trap = true; |
| 79 | ++ FLAG_unsecure = true; // for better backtraces |
| 80 | ++ llamafile_trapping_enabled(+1); |
| 81 | ++ } else if (arg == "--unsecure") { |
| 82 | ++ FLAG_unsecure = true; |
| 83 | ++ } else if (arg == "--nocompile") { |
| 84 | ++ FLAG_nocompile = true; |
| 85 | ++ } else if (arg == "--recompile") { |
| 86 | ++ FLAG_recompile = true; |
| 87 | ++ } else if (arg == "--tinyblas") { |
| 88 | ++ FLAG_tinyblas = true; // undocumented |
| 89 | ++ } else if (arg == "--gpu") { |
| 90 | ++ if (++i >= argc) { |
| 91 | ++ fprintf(stderr, "error: missing --gpu flag value\n"); |
| 92 | ++ exit(1); |
| 93 | ++ } |
| 94 | ++ FLAG_gpu = llamafile_gpu_parse(argv[i]); |
| 95 | ++ if (FLAG_gpu == LLAMAFILE_GPU_ERROR) { |
| 96 | ++ fprintf(stderr, "error: invalid --gpu flag value: %s\n", argv[i]); |
| 97 | ++ exit(1); |
| 98 | ++ } |
| 99 | ++ } else |
| 100 | ++ |
| 101 | + if (arg == "-h" || arg == "--help") { |
| 102 | + whisper_print_usage(argc, argv, params); |
| 103 | + exit(0); |
| 104 | +@@ -153,7 +188,7 @@ |
| 105 | + else if (arg == "-oj" || arg == "--output-json") { params.output_jsn = true; } |
| 106 | + else if (arg == "-ojf" || arg == "--output-json-full"){ params.output_jsn_full = params.output_jsn = true; } |
| 107 | + else if (arg == "-of" || arg == "--output-file") { params.fname_out.emplace_back(argv[++i]); } |
| 108 | +- else if (arg == "-np" || arg == "--no-prints") { params.no_prints = true; } |
| 109 | ++ else if (arg == "-np" || arg == "--no-prints") { params.no_prints = true; FLAG_log_disable = true; } |
| 110 | + else if (arg == "-ps" || arg == "--print-special") { params.print_special = true; } |
| 111 | + else if (arg == "-pc" || arg == "--print-colors") { params.print_colors = true; } |
| 112 | + else if (arg == "-pp" || arg == "--print-progress") { params.print_progress = true; } |
| 113 | +@@ -166,18 +201,20 @@ |
| 114 | + else if (arg == "-oved" || arg == "--ov-e-device") { params.openvino_encode_device = argv[++i]; } |
| 115 | + else if (arg == "-dtw" || arg == "--dtw") { params.dtw = argv[++i]; } |
| 116 | + else if (arg == "-ls" || arg == "--log-score") { params.log_score = true; } |
| 117 | +- else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; } |
| 118 | ++ else if (arg == "-ng" || arg == "--no-gpu") { FLAG_gpu = LLAMAFILE_GPU_DISABLE; } |
| 119 | + else if (arg == "-fa" || arg == "--flash-attn") { params.flash_attn = true; } |
| 120 | + else if ( arg == "--suppress-regex") { params.suppress_regex = argv[++i]; } |
| 121 | + else if ( arg == "--grammar") { params.grammar = argv[++i]; } |
| 122 | + else if ( arg == "--grammar-rule") { params.grammar_rule = argv[++i]; } |
| 123 | + else if ( arg == "--grammar-penalty") { params.grammar_penalty = std::stof(argv[++i]); } |
| 124 | + else { |
| 125 | +- fprintf(stderr, "error: unknown argument: %s\n", arg.c_str()); |
| 126 | +- whisper_print_usage(argc, argv, params); |
| 127 | ++ fprintf(stderr, "error: unknown argument: %s\n", arg.c_str()); // [jart] |
| 128 | ++ // whisper_print_usage(argc, argv, params); // [jart] |
| 129 | + exit(0); |
| 130 | + } |
| 131 | + } |
| 132 | ++ |
| 133 | ++ FLAGS_READY = true; |
| 134 | + |
| 135 | + return true; |
| 136 | + } |
| 137 | +@@ -232,7 +269,7 @@ |
| 138 | + fprintf(stderr, " -oved D, --ov-e-device DNAME [%-7s] the OpenVINO device used for encode inference\n", params.openvino_encode_device.c_str()); |
| 139 | + fprintf(stderr, " -dtw MODEL --dtw MODEL [%-7s] compute token-level timestamps\n", params.dtw.c_str()); |
| 140 | + fprintf(stderr, " -ls, --log-score [%-7s] log best decoder scores of tokens\n", params.log_score?"true":"false"); |
| 141 | +- fprintf(stderr, " -ng, --no-gpu [%-7s] disable GPU\n", params.use_gpu ? "false" : "true"); |
| 142 | ++ fprintf(stderr, " -ng, --no-gpu [%-7s] disable GPU\n", FLAG_gpu == LLAMAFILE_GPU_DISABLE ? "false" : "true"); |
| 143 | + fprintf(stderr, " -fa, --flash-attn [%-7s] flash attention\n", params.flash_attn ? "true" : "false"); |
| 144 | + fprintf(stderr, " --suppress-regex REGEX [%-7s] regular expression matching tokens to suppress\n", params.suppress_regex.c_str()); |
| 145 | + fprintf(stderr, " --grammar GRAMMAR [%-7s] GBNF grammar to guide decoding\n", params.grammar.c_str()); |
| 146 | +@@ -825,10 +862,10 @@ |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +- ::replace_all(txt_bg, "'", "\u2019"); |
| 151 | +- ::replace_all(txt_bg, "\"", "\\\""); |
| 152 | +- ::replace_all(txt_fg, "'", "\u2019"); |
| 153 | +- ::replace_all(txt_fg, "\"", "\\\""); |
| 154 | ++ txt_bg = replace_all(txt_bg, "'", "\u2019"); |
| 155 | ++ txt_bg = replace_all(txt_bg, "\"", "\\\""); |
| 156 | ++ txt_fg = replace_all(txt_fg, "'", "\u2019"); |
| 157 | ++ txt_fg = replace_all(txt_fg, "\"", "\\\""); |
| 158 | + } |
| 159 | + |
| 160 | + if (is_first) { |
| 161 | +@@ -903,9 +940,37 @@ |
| 162 | + |
| 163 | + static void cb_log_disable(enum ggml_log_level , const char * , void * ) { } |
| 164 | + |
| 165 | ++int whisper_server_main(int argc, char ** argv); |
| 166 | ++ |
| 167 | + int main(int argc, char ** argv) { |
| 168 | +- whisper_params params; |
| 169 | + |
| 170 | ++ mallopt(M_GRANULARITY, 1 * 1024 * 1024); |
| 171 | ++ mallopt(M_MMAP_THRESHOLD, 1 * 1024 * 1024); |
| 172 | ++ mallopt(M_TRIM_THRESHOLD, 32 * 1024 * 1024); |
| 173 | ++ FLAG_gpu = LLAMAFILE_GPU_DISABLE; // pass `--gpu auto` to enable it |
| 174 | ++ llamafile_check_cpu(); |
| 175 | ++ ShowCrashReports(); |
| 176 | ++ |
| 177 | ++ if (llamafile_has(argv, "--version")) { |
| 178 | ++ puts("whisperfile v" LLAMAFILE_VERSION_STRING); |
| 179 | ++ return 0; |
| 180 | ++ } |
| 181 | ++ |
| 182 | ++ if (llamafile_has(argv, "-h") || |
| 183 | ++ llamafile_has(argv, "-help") || |
| 184 | ++ llamafile_has(argv, "--help")) { |
| 185 | ++ llamafile_help("/zip/whisper.cpp/main.1.asc"); |
| 186 | ++ __builtin_unreachable(); |
| 187 | ++ } |
| 188 | ++ |
| 189 | ++ argc = cosmo_args("/zip/.args", &argv); |
| 190 | ++ |
| 191 | ++ if (!llamafile_has(argv, "--cli") && |
| 192 | ++ (llamafile_has(argv, "--server") || |
| 193 | ++ !llamafile_has(argv, "-f"))) { |
| 194 | ++ return whisper_server_main(argc, argv); |
| 195 | ++ } |
| 196 | ++ |
| 197 | + // If the only argument starts with "@", read arguments line-by-line |
| 198 | + // from the given file. |
| 199 | + std::vector<std::string> vec_args; |
| 200 | +@@ -935,6 +1000,7 @@ |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | ++ whisper_params params; |
| 205 | + if (whisper_params_parse(argc, argv, params) == false) { |
| 206 | + whisper_print_usage(argc, argv, params); |
| 207 | + return 1; |
| 208 | +@@ -979,7 +1045,6 @@ |
| 209 | + |
| 210 | + struct whisper_context_params cparams = whisper_context_default_params(); |
| 211 | + |
| 212 | +- cparams.use_gpu = params.use_gpu; |
| 213 | + cparams.flash_attn = params.flash_attn; |
| 214 | + |
| 215 | + if (!params.dtw.empty()) { |
| 216 | +@@ -1020,10 +1085,10 @@ |
| 217 | + // read grammar from file |
| 218 | + std::ifstream ifs(params.grammar.c_str()); |
| 219 | + const std::string txt = std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); |
| 220 | +- grammar = grammar_parser::parse(txt.c_str()); |
| 221 | ++ grammar = whisper_grammar_parser::parse(txt.c_str()); |
| 222 | + } else { |
| 223 | + // read grammar from string |
| 224 | +- grammar = grammar_parser::parse(params.grammar.c_str()); |
| 225 | ++ grammar = whisper_grammar_parser::parse(params.grammar.c_str()); |
| 226 | + } |
| 227 | + |
| 228 | + // will be empty (default) if there are parse errors |
| 229 | +@@ -1032,7 +1097,7 @@ |
| 230 | + return 4; |
| 231 | + } else { |
| 232 | + fprintf(stderr, "%s: grammar:\n", __func__); |
| 233 | +- grammar_parser::print_grammar(stderr, grammar); |
| 234 | ++ whisper_grammar_parser::print_grammar(stderr, grammar); |
| 235 | + fprintf(stderr, "\n"); |
| 236 | + } |
| 237 | + } |
| 238 | +@@ -1044,8 +1109,8 @@ |
| 239 | + std::vector<float> pcmf32; // mono-channel F32 PCM |
| 240 | + std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM |
| 241 | + |
| 242 | +- if (!::read_wav(fname_inp, pcmf32, pcmf32s, params.diarize)) { |
| 243 | +- fprintf(stderr, "error: failed to read WAV file '%s'\n", fname_inp.c_str()); |
| 244 | ++ if (!slurp_audio_file(fname_inp.c_str(), pcmf32, pcmf32s, params.diarize)) { |
| 245 | ++ fprintf(stderr, "error: failed to read audio file '%s'\n", fname_inp.c_str()); |
| 246 | + continue; |
| 247 | + } |
| 248 | + |
0 commit comments