1
+ #include < Rcpp.h>
1
2
#define _USE_MATH_DEFINES // for M_PI
2
3
3
4
#include " common.h"
@@ -24,7 +25,7 @@ std::string get_next_arg(int& i, int argc, char** argv, const std::string& flag,
24
25
if (i + 1 < argc && argv[i + 1 ][0 ] != ' -' ) {
25
26
return argv[++i];
26
27
} else {
27
- fprintf (stderr, " error: %s requires one argument.\n " , flag.c_str ());
28
+ Rprintf ( " error: %s requires one argument.\n " , flag.c_str ());
28
29
gpt_print_usage (argc, argv, params);
29
30
exit (0 );
30
31
}
@@ -76,7 +77,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
76
77
get_next_arg (i, argc, argv, arg, params);
77
78
std::ifstream file (argv[i]);
78
79
if (!file) {
79
- fprintf (stderr, " error: failed to open file '%s'\n " , argv[i]);
80
+ Rprintf ( " error: failed to open file '%s'\n " , argv[i]);
80
81
break ;
81
82
}
82
83
std::copy (std::istreambuf_iterator<char >(file), std::istreambuf_iterator<char >(), back_inserter (params.prompt ));
@@ -87,7 +88,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
87
88
params.token_test = get_next_arg (i, argc, argv, arg, params);
88
89
}
89
90
else {
90
- fprintf (stderr, " error: unknown argument: %s\n " , arg.c_str ());
91
+ Rprintf ( " error: unknown argument: %s\n " , arg.c_str ());
91
92
gpt_print_usage (argc, argv, params);
92
93
exit (0 );
93
94
}
@@ -97,31 +98,31 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
97
98
}
98
99
99
100
void gpt_print_usage (int /* argc*/ , char ** argv, const gpt_params & params) {
100
- fprintf (stderr, " usage: %s [options]\n " , argv[0 ]);
101
- fprintf (stderr, " \n " );
102
- fprintf (stderr, " options:\n " );
103
- fprintf (stderr, " -h, --help show this help message and exit\n " );
104
- fprintf (stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n " );
105
- fprintf (stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n " , params.n_threads );
106
- fprintf (stderr, " -p PROMPT, --prompt PROMPT\n " );
107
- fprintf (stderr, " prompt to start generation with (default: random)\n " );
108
- fprintf (stderr, " -f FNAME, --file FNAME\n " );
109
- fprintf (stderr, " load prompt from a file\n " );
110
- fprintf (stderr, " -tt TOKEN_TEST, --token_test TOKEN_TEST\n " );
111
- fprintf (stderr, " test tokenization\n " );
112
- fprintf (stderr, " -n N, --n_predict N number of tokens to predict (default: %d)\n " , params.n_predict );
113
- fprintf (stderr, " --top_k N top-k sampling (default: %d)\n " , params.top_k );
114
- fprintf (stderr, " --top_p N top-p sampling (default: %.1f)\n " , params.top_p );
115
- fprintf (stderr, " --temp N temperature (default: %.1f)\n " , params.temp );
116
- fprintf (stderr, " --repeat-last-n N last n tokens to consider for penalize (default: %d, 0 = disabled)\n " , params.repeat_last_n );
117
- fprintf (stderr, " --repeat-penalty N penalize repeat sequence of tokens (default: %.2f, 1.0 = disabled)\n " , (double )params.repeat_penalty );
118
- fprintf (stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n " , params.n_batch );
119
- fprintf (stderr, " -c N, --context N context / KV cache size (default: %d)\n " , params.n_ctx );
120
- fprintf (stderr, " --ignore-eos ignore EOS token during generation\n " );
121
- fprintf (stderr, " -ngl N, --gpu-layers N number of layers to offload to GPU on supported models (default: %d)\n " , params.n_gpu_layers );
122
- fprintf (stderr, " -m FNAME, --model FNAME\n " );
123
- fprintf (stderr, " model path (default: %s)\n " , params.model .c_str ());
124
- fprintf (stderr, " \n " );
101
+ Rprintf ( " usage: %s [options]\n " , argv[0 ]);
102
+ Rprintf ( " \n " );
103
+ Rprintf ( " options:\n " );
104
+ Rprintf ( " -h, --help show this help message and exit\n " );
105
+ Rprintf ( " -s SEED, --seed SEED RNG seed (default: -1)\n " );
106
+ Rprintf ( " -t N, --threads N number of threads to use during computation (default: %d)\n " , params.n_threads );
107
+ Rprintf ( " -p PROMPT, --prompt PROMPT\n " );
108
+ Rprintf ( " prompt to start generation with (default: random)\n " );
109
+ Rprintf ( " -f FNAME, --file FNAME\n " );
110
+ Rprintf ( " load prompt from a file\n " );
111
+ Rprintf ( " -tt TOKEN_TEST, --token_test TOKEN_TEST\n " );
112
+ Rprintf ( " test tokenization\n " );
113
+ Rprintf ( " -n N, --n_predict N number of tokens to predict (default: %d)\n " , params.n_predict );
114
+ Rprintf ( " --top_k N top-k sampling (default: %d)\n " , params.top_k );
115
+ Rprintf ( " --top_p N top-p sampling (default: %.1f)\n " , params.top_p );
116
+ Rprintf ( " --temp N temperature (default: %.1f)\n " , params.temp );
117
+ Rprintf ( " --repeat-last-n N last n tokens to consider for penalize (default: %d, 0 = disabled)\n " , params.repeat_last_n );
118
+ Rprintf ( " --repeat-penalty N penalize repeat sequence of tokens (default: %.2f, 1.0 = disabled)\n " , (double )params.repeat_penalty );
119
+ Rprintf ( " -b N, --batch_size N batch size for prompt processing (default: %d)\n " , params.n_batch );
120
+ Rprintf ( " -c N, --context N context / KV cache size (default: %d)\n " , params.n_ctx );
121
+ Rprintf ( " --ignore-eos ignore EOS token during generation\n " );
122
+ Rprintf ( " -ngl N, --gpu-layers N number of layers to offload to GPU on supported models (default: %d)\n " , params.n_gpu_layers );
123
+ Rprintf ( " -m FNAME, --model FNAME\n " );
124
+ Rprintf ( " model path (default: %s)\n " , params.model .c_str ());
125
+ Rprintf ( " \n " );
125
126
}
126
127
127
128
std::string gpt_random_prompt (std::mt19937 & rng) {
@@ -170,7 +171,7 @@ std::map<std::string, int32_t> json_parse(const std::string & fname) {
170
171
{
171
172
std::ifstream ifs (fname);
172
173
if (!ifs) {
173
- fprintf (stderr, " Failed to open %s\n " , fname.c_str ());
174
+ Rprintf ( " Failed to open %s\n " , fname.c_str ());
174
175
exit (1 );
175
176
}
176
177
@@ -233,7 +234,7 @@ std::map<std::string, int32_t> json_parse(const std::string & fname) {
233
234
try {
234
235
result[str_key] = std::stoi (str_val);
235
236
} catch (...) {
236
- // fprintf(stderr, "%s: ignoring key '%s' with value '%s'\n", fname.c_str(), str_key.c_str(), str_val.c_str());
237
+ // Rprintf( "%s: ignoring key '%s' with value '%s'\n", fname.c_str(), str_key.c_str(), str_val.c_str());
237
238
238
239
}
239
240
str_key = " " ;
@@ -326,7 +327,7 @@ std::vector<gpt_vocab::id> gpt_tokenize(const gpt_vocab & vocab, const std::stri
326
327
break ;
327
328
}
328
329
else if (j == i){ // word.substr(i, 1) has no matching
329
- fprintf (stderr, " %s: unknown token '%s'\n " , __func__, word.substr (i, 1 ).data ());
330
+ Rprintf ( " %s: unknown token '%s'\n " , __func__, word.substr (i, 1 ).data ());
330
331
i++;
331
332
}
332
333
}
@@ -350,7 +351,7 @@ std::vector<gpt_vocab::id> parse_tokens_from_string(const std::string& input, ch
350
351
351
352
std::map<std::string, std::vector<gpt_vocab::id>> extract_tests_from_file (const std::string & fpath_test){
352
353
if (fpath_test.empty ()){
353
- fprintf (stderr, " %s : No test file found.\n " , __func__);
354
+ Rprintf ( " %s : No test file found.\n " , __func__);
354
355
return std::map<std::string, std::vector<gpt_vocab::id>>();
355
356
}
356
357
@@ -383,37 +384,37 @@ void test_gpt_tokenizer(gpt_vocab & vocab, const std::string & fpath_test){
383
384
n_fails++;
384
385
385
386
// print out failure cases
386
- fprintf (stderr, " %s : failed test: '%s'\n " , __func__, test.first .c_str ());
387
- fprintf (stderr, " %s : tokens in hf: " , __func__);
387
+ Rprintf ( " %s : failed test: '%s'\n " , __func__, test.first .c_str ());
388
+ Rprintf ( " %s : tokens in hf: " , __func__);
388
389
for (const auto & t : test.second ) {
389
- fprintf (stderr, " %s(%d), " , vocab.id_to_token [t].c_str (), t);
390
+ Rprintf ( " %s(%d), " , vocab.id_to_token [t].c_str (), t);
390
391
}
391
- fprintf (stderr, " \n " );
392
- fprintf (stderr, " %s : tokens in ggml: " , __func__);
392
+ Rprintf ( " \n " );
393
+ Rprintf ( " %s : tokens in ggml: " , __func__);
393
394
for (const auto & t : tokens) {
394
- fprintf (stderr, " %s(%d), " , vocab.id_to_token [t].c_str (), t);
395
+ Rprintf ( " %s(%d), " , vocab.id_to_token [t].c_str (), t);
395
396
}
396
- fprintf (stderr, " \n " );
397
+ Rprintf ( " \n " );
397
398
}
398
399
}
399
400
400
- fprintf (stderr, " %s : %zu tests failed out of %zu tests.\n " , __func__, n_fails, tests.size ());
401
+ Rprintf ( " %s : %zu tests failed out of %zu tests.\n " , __func__, n_fails, tests.size ());
401
402
}
402
403
403
404
bool gpt_vocab_init (const std::string & fname, gpt_vocab & vocab) {
404
- printf (" %s: loading vocab from '%s'\n " , __func__, fname.c_str ());
405
+ Rprintf (" %s: loading vocab from '%s'\n " , __func__, fname.c_str ());
405
406
406
407
vocab.token_to_id = ::json_parse (fname);
407
408
408
409
for (const auto & kv : vocab.token_to_id ) {
409
410
vocab.id_to_token [kv.second ] = kv.first ;
410
411
}
411
412
412
- printf (" %s: vocab size = %d\n " , __func__, (int ) vocab.token_to_id .size ());
413
+ Rprintf (" %s: vocab size = %d\n " , __func__, (int ) vocab.token_to_id .size ());
413
414
414
415
// print the vocabulary
415
416
// for (auto kv : vocab.token_to_id) {
416
- // printf ("'%s' -> %d\n", kv.first.data(), kv.second);
417
+ // Rprintf ("'%s' -> %d\n", kv.first.data(), kv.second);
417
418
// }
418
419
419
420
return true ;
@@ -489,7 +490,7 @@ gpt_vocab::id gpt_sample_top_k_top_p(
489
490
490
491
// printf("\n");
491
492
// for (int i = 0; i < (int) probs.size(); i++) {
492
- // printf ("%d: '%s' %f\n", i, vocab.id_to_token.at(logits_id[i].second).c_str(), probs[i]);
493
+ // Rprintf ("%d: '%s' %f\n", i, vocab.id_to_token.at(logits_id[i].second).c_str(), probs[i]);
493
494
// }
494
495
// exit(0);
495
496
@@ -602,10 +603,10 @@ gpt_vocab::id gpt_sample_top_k_top_p_repeat(
602
603
}
603
604
}
604
605
605
- // printf ("\n");
606
+ // Rprintf ("\n");
606
607
// for (int i = 0; i < (int) probs.size(); i++) {
607
608
// for (int i = 0; i < 10; i++) {
608
- // printf ("%d: '%s' %f\n", i, vocab.id_to_token.at(logits_id[i].second).c_str(), probs[i]);
609
+ // Rprintf ("%d: '%s' %f\n", i, vocab.id_to_token.at(logits_id[i].second).c_str(), probs[i]);
609
610
// }
610
611
611
612
std::discrete_distribution<> dist (probs.begin (), probs.end ());
@@ -633,34 +634,34 @@ bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector
633
634
}
634
635
635
636
if (drwav_init_memory (&wav, wav_data.data (), wav_data.size (), nullptr ) == false ) {
636
- fprintf (stderr, " error: failed to open WAV file from stdin\n " );
637
+ Rprintf ( " error: failed to open WAV file from stdin\n " );
637
638
return false ;
638
639
}
639
640
640
- fprintf (stderr, " %s: read %zu bytes from stdin\n " , __func__, wav_data.size ());
641
+ Rprintf ( " %s: read %zu bytes from stdin\n " , __func__, wav_data.size ());
641
642
}
642
643
else if (drwav_init_file (&wav, fname.c_str (), nullptr ) == false ) {
643
- fprintf (stderr, " error: failed to open '%s' as WAV file\n " , fname.c_str ());
644
+ Rprintf ( " error: failed to open '%s' as WAV file\n " , fname.c_str ());
644
645
return false ;
645
646
}
646
647
647
648
if (wav.channels != 1 && wav.channels != 2 ) {
648
- fprintf (stderr, " %s: WAV file '%s' must be mono or stereo\n " , __func__, fname.c_str ());
649
+ Rprintf ( " %s: WAV file '%s' must be mono or stereo\n " , __func__, fname.c_str ());
649
650
return false ;
650
651
}
651
652
652
653
if (stereo && wav.channels != 2 ) {
653
- fprintf (stderr, " %s: WAV file '%s' must be stereo for diarization\n " , __func__, fname.c_str ());
654
+ Rprintf ( " %s: WAV file '%s' must be stereo for diarization\n " , __func__, fname.c_str ());
654
655
return false ;
655
656
}
656
657
657
658
if (wav.sampleRate != COMMON_SAMPLE_RATE) {
658
- fprintf (stderr, " %s: WAV file '%s' must be %i kHz\n " , __func__, fname.c_str (), COMMON_SAMPLE_RATE/1000 );
659
+ Rprintf ( " %s: WAV file '%s' must be %i kHz\n " , __func__, fname.c_str (), COMMON_SAMPLE_RATE/1000 );
659
660
return false ;
660
661
}
661
662
662
663
if (wav.bitsPerSample != 16 ) {
663
- fprintf (stderr, " %s: WAV file '%s' must be 16-bit\n " , __func__, fname.c_str ());
664
+ Rprintf ( " %s: WAV file '%s' must be 16-bit\n " , __func__, fname.c_str ());
664
665
return false ;
665
666
}
666
667
@@ -739,7 +740,7 @@ bool vad_simple(std::vector<float> & pcmf32, int sample_rate, int last_ms, float
739
740
energy_last /= n_samples_last;
740
741
741
742
if (verbose) {
742
- fprintf (stderr, " %s: energy_all: %f, energy_last: %f, vad_thold: %f, freq_thold: %f\n " , __func__, energy_all, energy_last, vad_thold, freq_thold);
743
+ Rprintf ( " %s: energy_all: %f, energy_last: %f, vad_thold: %f, freq_thold: %f\n " , __func__, energy_all, energy_last, vad_thold, freq_thold);
743
744
}
744
745
745
746
if (energy_last > vad_thold*energy_all) {
@@ -791,7 +792,7 @@ bool sam_params_parse(int argc, char ** argv, sam_params & params) {
791
792
sam_print_usage (argc, argv, params);
792
793
exit (0 );
793
794
} else {
794
- fprintf (stderr, " error: unknown argument: %s\n " , arg.c_str ());
795
+ Rprintf ( " error: unknown argument: %s\n " , arg.c_str ());
795
796
sam_print_usage (argc, argv, params);
796
797
exit (0 );
797
798
}
@@ -801,17 +802,17 @@ bool sam_params_parse(int argc, char ** argv, sam_params & params) {
801
802
}
802
803
803
804
void sam_print_usage (int /* argc*/ , char ** argv, const sam_params & params) {
804
- fprintf (stderr, " usage: %s [options]\n " , argv[0 ]);
805
- fprintf (stderr, " \n " );
806
- fprintf (stderr, " options:\n " );
807
- fprintf (stderr, " -h, --help show this help message and exit\n " );
808
- fprintf (stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n " );
809
- fprintf (stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n " , params.n_threads );
810
- fprintf (stderr, " -m FNAME, --model FNAME\n " );
811
- fprintf (stderr, " model path (default: %s)\n " , params.model .c_str ());
812
- fprintf (stderr, " -i FNAME, --inp FNAME\n " );
813
- fprintf (stderr, " input file (default: %s)\n " , params.fname_inp .c_str ());
814
- fprintf (stderr, " -o FNAME, --out FNAME\n " );
815
- fprintf (stderr, " output file (default: %s)\n " , params.fname_out .c_str ());
816
- fprintf (stderr, " \n " );
805
+ Rprintf ( " usage: %s [options]\n " , argv[0 ]);
806
+ Rprintf ( " \n " );
807
+ Rprintf ( " options:\n " );
808
+ Rprintf ( " -h, --help show this help message and exit\n " );
809
+ Rprintf ( " -s SEED, --seed SEED RNG seed (default: -1)\n " );
810
+ Rprintf ( " -t N, --threads N number of threads to use during computation (default: %d)\n " , params.n_threads );
811
+ Rprintf ( " -m FNAME, --model FNAME\n " );
812
+ Rprintf ( " model path (default: %s)\n " , params.model .c_str ());
813
+ Rprintf ( " -i FNAME, --inp FNAME\n " );
814
+ Rprintf ( " input file (default: %s)\n " , params.fname_inp .c_str ());
815
+ Rprintf ( " -o FNAME, --out FNAME\n " );
816
+ Rprintf ( " output file (default: %s)\n " , params.fname_out .c_str ());
817
+ Rprintf ( " \n " );
817
818
}
0 commit comments