Skip to content

Commit ffa019e

Browse files
committed
exit/fprintf/printf/fflush
1 parent dc36258 commit ffa019e

File tree

5 files changed

+96
-74
lines changed

5 files changed

+96
-74
lines changed

src/whisper_cpp/common-ggml.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <Rcpp.h>
12
#include "common-ggml.h"
23

34
#include <regex>
@@ -27,7 +28,7 @@ enum ggml_ftype ggml_parse_ftype(const char * str) {
2728
if (str[0] == 'q') {
2829
const auto it = GGML_FTYPE_MAP.find(str);
2930
if (it == GGML_FTYPE_MAP.end()) {
30-
fprintf(stderr, "%s: unknown ftype '%s'\n", __func__, str);
31+
Rprintf("%s: unknown ftype '%s'\n", __func__, str);
3132
return GGML_FTYPE_UNKNOWN;
3233
}
3334
ftype = it->second;
@@ -63,13 +64,13 @@ bool ggml_common_quantize_0(
6364
case GGML_FTYPE_MOSTLY_F16:
6465
case GGML_FTYPE_MOSTLY_Q4_1_SOME_F16:
6566
{
66-
fprintf(stderr, "%s: invalid model type %d\n", __func__, ftype);
67+
Rprintf("%s: invalid model type %d\n", __func__, ftype);
6768
return false;
6869
}
6970
};
7071

7172
if (!ggml_is_quantized(qtype)) {
72-
fprintf(stderr, "%s: invalid quantization type %d (%s)\n", __func__, qtype, ggml_type_name(qtype));
73+
Rprintf("%s: invalid quantization type %d (%s)\n", __func__, qtype, ggml_type_name(qtype));
7374
return false;
7475
}
7576

@@ -107,7 +108,7 @@ bool ggml_common_quantize_0(
107108
std::string name(length, 0);
108109
finp.read (&name[0], length);
109110

110-
printf("%64s - [%5d, %5d, %5d], type = %6s ", name.data(), ne[0], ne[1], ne[2], ggml_type_name((ggml_type) ttype));
111+
Rprintf("%64s - [%5d, %5d, %5d], type = %6s ", name.data(), ne[0], ne[1], ne[2], ggml_type_name((ggml_type) ttype));
111112

112113
bool quantize = false;
113114

@@ -132,7 +133,7 @@ bool ggml_common_quantize_0(
132133

133134
if (quantize) {
134135
if (ttype != GGML_TYPE_F32 && ttype != GGML_TYPE_F16) {
135-
fprintf(stderr, "%s: unsupported ttype %d (%s) for integer quantization\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
136+
Rprintf("%s: unsupported ttype %d (%s) for integer quantization\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
136137
return false;
137138
}
138139

@@ -193,46 +194,46 @@ bool ggml_common_quantize_0(
193194
case GGML_TYPE_Q8_K:
194195
case GGML_TYPE_COUNT:
195196
{
196-
fprintf(stderr, "%s: unsupported quantization type %d (%s)\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
197+
Rprintf("%s: unsupported quantization type %d (%s)\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
197198
return false;
198199
}
199200
}
200201

201202
fout.write(reinterpret_cast<char *>(work.data()), cur_size);
202203
total_size_new += cur_size;
203204

204-
printf("size = %8.2f MB -> %8.2f MB | hist: ", nelements * sizeof(float)/1024.0/1024.0, cur_size/1024.0/1024.0);
205+
Rprintf("size = %8.2f MB -> %8.2f MB | hist: ", nelements * sizeof(float)/1024.0/1024.0, cur_size/1024.0/1024.0);
205206
for (int i = 0; i < (int) hist_cur.size(); ++i) {
206207
hist_all[i] += hist_cur[i];
207208
}
208209

209210
for (int i = 0; i < (int) hist_cur.size(); ++i) {
210-
printf("%5.3f ", hist_cur[i] / (float)nelements);
211+
Rprintf("%5.3f ", hist_cur[i] / (float)nelements);
211212
}
212-
printf("\n");
213+
Rprintf("\n");
213214
} else {
214-
printf("size = %8.3f MB\n", data_u8.size()/1024.0/1024.0);
215+
Rprintf("size = %8.3f MB\n", data_u8.size()/1024.0/1024.0);
215216
fout.write(reinterpret_cast<char *>(data_u8.data()), data_u8.size());
216217
total_size_new += data_u8.size();
217218
}
218219

219220
total_size_org += nelements * sizeof(float);
220221
}
221222

222-
printf("%s: model size = %8.2f MB\n", __func__, total_size_org/1024.0/1024.0);
223-
printf("%s: quant size = %8.2f MB | ftype = %d (%s)\n", __func__, total_size_new/1024.0/1024.0, ftype, ggml_type_name(qtype));
223+
Rprintf("%s: model size = %8.2f MB\n", __func__, total_size_org/1024.0/1024.0);
224+
Rprintf("%s: quant size = %8.2f MB | ftype = %d (%s)\n", __func__, total_size_new/1024.0/1024.0, ftype, ggml_type_name(qtype));
224225

225226
{
226227
int64_t sum_all = 0;
227228
for (int i = 0; i < (int) hist_all.size(); ++i) {
228229
sum_all += hist_all[i];
229230
}
230231

231-
printf("%s: hist: ", __func__);
232+
Rprintf("%s: hist: ", __func__);
232233
for (int i = 0; i < (int) hist_all.size(); ++i) {
233-
printf("%5.3f ", hist_all[i] / (float)sum_all);
234+
Rprintf("%5.3f ", hist_all[i] / (float)sum_all);
234235
}
235-
printf("\n");
236+
Rprintf("\n");
236237
}
237238

238239
return true;

src/whisper_cpp/ggml-alloc.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "R.h"
12
#include "ggml-alloc.h"
23
#include "ggml-backend-impl.h"
34
#include "ggml.h"
@@ -14,7 +15,7 @@
1415

1516
//#define GGML_ALLOCATOR_DEBUG
1617

17-
//#define AT_PRINTF(...) fprintf(stderr, __VA_ARGS__)
18+
//#define AT_PRINTF(...) RRprintf(__VA_ARGS__)
1819
#define AT_PRINTF(...)
1920

2021
// TODO: GGML_PAD ?
@@ -111,7 +112,7 @@ void ggml_tallocr_alloc(ggml_tallocr_t alloc, struct ggml_tensor * tensor) {
111112
if (block->size >= size) {
112113
best_fit_block = alloc->n_free_blocks - 1;
113114
} else {
114-
fprintf(stderr, "%s: not enough space in the buffer (needed %zu, largest block available %zu)\n",
115+
Rprintf("%s: not enough space in the buffer (needed %zu, largest block available %zu)\n",
115116
__func__, size, max_avail);
116117
GGML_ASSERT(!"not enough space in the buffer");
117118
return;

src/whisper_cpp/ggml-backend.c

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "R.h"
12
#include "ggml-backend-impl.h"
23
#include "ggml-alloc.h"
34
#include "ggml-impl.h"
@@ -231,7 +232,7 @@ void ggml_backend_tensor_copy(struct ggml_tensor * src, struct ggml_tensor * dst
231232
//printf("dst: %s ne: [%d %d %d %d] nb: [%d %d %d %d]\n", dst->name, (int)dst->ne[0], (int)dst->ne[1], (int)dst->ne[2], (int)dst->ne[3], (int)dst->nb[0], (int)dst->nb[1], (int)dst->nb[2], (int)dst->nb[3]);
232233
GGML_ASSERT(ggml_are_same_layout(src, dst) && "cannot copy tensors with different layouts");
233234

234-
// fprintf(stderr, "cpy tensor %s from %s to %s (%lu bytes)\n", src->name, ggml_backend_name(src->backend), ggml_backend_name(dst->backend), ggml_nbytes(src));
235+
// Rprintf("cpy tensor %s from %s to %s (%lu bytes)\n", src->name, ggml_backend_name(src->backend), ggml_backend_name(dst->backend), ggml_nbytes(src));
235236

236237
if (src == dst) {
237238
return;
@@ -246,7 +247,7 @@ void ggml_backend_tensor_copy(struct ggml_tensor * src, struct ggml_tensor * dst
246247
} else {
247248
// shouldn't be hit when copying from/to CPU
248249
#ifndef NDEBUG
249-
fprintf(stderr, "ggml_backend_tensor_copy: neither cpy_tensor_from nor cpy_tensor_to "
250+
Rprintf("ggml_backend_tensor_copy: neither cpy_tensor_from nor cpy_tensor_to "
250251
"are implemented for %s and %s, falling back to get/set\n", src->name, dst->name);
251252
#endif
252253
size_t nbytes = ggml_nbytes(src);
@@ -312,7 +313,7 @@ void ggml_backend_register(const char * name, ggml_backend_init_fn init_fn, ggml
312313
snprintf(ggml_backend_registry[id].name, sizeof(ggml_backend_registry[id].name), "%s", name);
313314

314315
#ifndef NDEBUG
315-
fprintf(stderr, "%s: registered backend %s\n", __func__, name);
316+
Rprintf("%s: registered backend %s\n", __func__, name);
316317
#endif
317318

318319
ggml_backend_registry_count++;
@@ -355,7 +356,7 @@ ggml_backend_t ggml_backend_reg_init_backend_from_str(const char * backend_str)
355356
size_t backend_i = ggml_backend_reg_find_by_name(backend_name);
356357

357358
if (backend_i == SIZE_MAX) {
358-
fprintf(stderr, "%s: backend %s not found\n", __func__, backend_name);
359+
Rprintf("%s: backend %s not found\n", __func__, backend_name);
359360
return NULL;
360361
}
361362

@@ -510,7 +511,7 @@ static ggml_backend_buffer_t ggml_backend_cpu_hbm_buffer_type_alloc_buffer(ggml_
510511
void * ptr;
511512
int result = hbw_posix_memalign(&ptr, ggml_backend_cpu_buffer_type_get_alignment(buft), size);
512513
if (result != 0) {
513-
fprintf(stderr, "failed to allocate HBM buffer of size %zu\n", size);
514+
Rprintf("failed to allocate HBM buffer of size %zu\n", size);
514515
return NULL;
515516
}
516517

@@ -842,13 +843,13 @@ static void sched_print_assignments(ggml_backend_sched_t sched, struct ggml_cgra
842843
for (int i = 0; i < graph->n_nodes; i++) {
843844
if (cur_split < sched->n_splits && i == sched->splits[cur_split].i_start) {
844845
ggml_backend_t split_backend = get_allocr_backend(sched, sched->splits[cur_split].tallocr);
845-
fprintf(stderr, "\n## SPLIT #%d: %s # %d inputs: ", cur_split, ggml_backend_name(split_backend),
846+
Rprintf("\n## SPLIT #%d: %s # %d inputs: ", cur_split, ggml_backend_name(split_backend),
846847
sched->splits[cur_split].n_inputs);
847848
for (int j = 0; j < sched->splits[cur_split].n_inputs; j++) {
848-
fprintf(stderr, "[%s (%5.5s)] ", sched->splits[cur_split].inputs[j]->name,
849+
Rprintf("[%s (%5.5s)] ", sched->splits[cur_split].inputs[j]->name,
849850
fmt_size(ggml_nbytes(sched->splits[cur_split].inputs[j])));
850851
}
851-
fprintf(stderr, "\n");
852+
Rprintf("\n");
852853
cur_split++;
853854
}
854855
struct ggml_tensor * node = graph->nodes[i];
@@ -857,7 +858,7 @@ static void sched_print_assignments(ggml_backend_sched_t sched, struct ggml_cgra
857858
}
858859
ggml_tallocr_t node_allocr = node_allocr(node);
859860
ggml_backend_t node_backend = node_allocr ? get_allocr_backend(sched, node_allocr) : NULL; // FIXME:
860-
fprintf(stderr, "node #%3d (%10.10s): %20.20s (%4.4s) [%4.4s %8.8s]:", i, ggml_op_name(node->op), node->name,
861+
Rprintf("node #%3d (%10.10s): %20.20s (%4.4s) [%4.4s %8.8s]:", i, ggml_op_name(node->op), node->name,
861862
fmt_size(ggml_nbytes(node)), node_allocr ? ggml_backend_name(node_backend) : "NULL", GET_CAUSE(node));
862863
for (int j = 0; j < GGML_MAX_SRC; j++) {
863864
struct ggml_tensor * src = node->src[j];
@@ -866,10 +867,10 @@ static void sched_print_assignments(ggml_backend_sched_t sched, struct ggml_cgra
866867
}
867868
ggml_tallocr_t src_allocr = node_allocr(src);
868869
ggml_backend_t src_backend = src_allocr ? get_allocr_backend(sched, src_allocr) : NULL;
869-
fprintf(stderr, " %20.20s (%4.4s) [%4.4s %8.8s]", src->name,
870+
Rprintf(" %20.20s (%4.4s) [%4.4s %8.8s]", src->name,
870871
fmt_size(ggml_nbytes(src)), src_backend ? ggml_backend_name(src_backend) : "NULL", GET_CAUSE(src));
871872
}
872-
fprintf(stderr, "\n");
873+
Rprintf("\n");
873874
}
874875
}
875876

@@ -1049,15 +1050,15 @@ static void sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgraph * g
10491050
sched->splits[cur_split].i_end = graph->n_nodes;
10501051
sched->n_splits = cur_split + 1;
10511052

1052-
//fprintf(stderr, "PASS 4 ASSIGNMENTS\n"); sched_print_assignments(sched, graph); fflush(stdout);
1053+
//Rprintf("PASS 4 ASSIGNMENTS\n"); sched_print_assignments(sched, graph); fflush(stdout);
10531054

10541055
#if 1
10551056
// sanity check: all sources should have the same backend as the node
10561057
for (int i = 0; i < graph->n_nodes; i++) {
10571058
struct ggml_tensor * node = graph->nodes[i];
10581059
ggml_tallocr_t node_allocr = node_allocr(node);
10591060
if (node_allocr == NULL) {
1060-
fprintf(stderr, "!!!!!!! %s has no backend\n", node->name);
1061+
Rprintf("!!!!!!! %s has no backend\n", node->name);
10611062
}
10621063
for (int j = 0; j < GGML_MAX_SRC; j++) {
10631064
struct ggml_tensor * src = node->src[j];
@@ -1066,7 +1067,7 @@ static void sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgraph * g
10661067
}
10671068
ggml_tallocr_t src_allocr = node_allocr(src);
10681069
if (src_allocr != node_allocr /* && src_backend != NULL */) { // ignore nulls for now
1069-
fprintf(stderr, "!!!! %s has backend %s, src %d (%s) has backend %s\n",
1070+
Rprintf("!!!! %s has backend %s, src %d (%s) has backend %s\n",
10701071
node->name, node_allocr ? ggml_backend_name(get_allocr_backend(sched, node_allocr)) : "NULL",
10711072
j, src->name, src_allocr ? ggml_backend_name(get_allocr_backend(sched, src_allocr)) : "NULL");
10721073
}
@@ -1122,15 +1123,15 @@ static void sched_compute_splits(ggml_backend_sched_t sched) {
11221123
struct ggml_tensor * input_cpy = sched->node_copies[hash_id(input)][sched_backend_prio(sched, split_backend)];
11231124
if (input->buffer == NULL) {
11241125
if (input->view_src == NULL) {
1125-
fprintf(stderr, "input %s has no buffer and no view_src\n", input->name);
1126-
exit(1);
1126+
Rprintf("input %s has no buffer and no view_src\n", input->name);
1127+
Rf_error("whispercpp error");
11271128
}
11281129
// FIXME: may need to use the sched buffer instead
11291130
ggml_backend_view_init(input->view_src->buffer, input);
11301131
}
11311132
if (input_cpy->buffer == NULL) {
1132-
fprintf(stderr, "input_cpy %s has no buffer\n", input_cpy->name);
1133-
exit(1);
1133+
Rprintf("input_cpy %s has no buffer\n", input_cpy->name);
1134+
Rf_error("whispercpp error");
11341135
}
11351136
//GGML_ASSERT(input->buffer->backend != input_cpy->buffer->backend);
11361137
//GGML_ASSERT(input_cpy->buffer->backend == split_backend);
@@ -1155,10 +1156,10 @@ static void sched_compute_splits(ggml_backend_sched_t sched) {
11551156

11561157
#if 0
11571158
// per-backend timings
1158-
fprintf(stderr, "sched_compute_splits times (%d splits):\n", sched->n_splits);
1159+
Rprintf("sched_compute_splits times (%d splits):\n", sched->n_splits);
11591160
for (int i = 0; i < sched->n_backends; i++) {
11601161
if (copy_us[i] > 0 || compute_us[i] > 0) {
1161-
fprintf(stderr, "\t%5.5s: %lu us copy, %lu us compute\n", ggml_backend_name(sched->backends[i]), copy_us[i], compute_us[i]);
1162+
Rprintf("\t%5.5s: %lu us copy, %lu us compute\n", ggml_backend_name(sched->backends[i]), copy_us[i], compute_us[i]);
11621163
}
11631164
}
11641165
#endif

0 commit comments

Comments
 (0)