Skip to content

Commit 25b0313

Browse files
Fix: save tensors and prompt/output to different files
1 parent 791fa52 commit 25b0313

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

examples/eval-callback/eval-callback.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
#include <vector>
1212
#include <iostream>
1313

14+
#include <fstream>
15+
16+
std::ofstream prompt_output_file;
17+
std::ofstream tensor_output_file;
18+
19+
1420
struct callback_data {
1521
std::vector<uint8_t> data;
1622
std::string parse_layer_name;
@@ -30,10 +36,10 @@ static std::string ggml_ne_string(const ggml_tensor * t) {
3036

3137
static void ggml_print_tensor_block(const std::string& tensor_name, uint8_t * data, ggml_type type, const int64_t * ne, const size_t * nb, int64_t token_idx) {
3238
const int64_t dim = ne[0];
33-
std::cout << "=== TOKEN " << token_idx << " ===\n";
34-
std::cout << "--- TENSOR: " << tensor_name << " ---\n";
35-
std::cout << "SHAPE: [" << dim << "]\n";
36-
std::cout << "DATA:\n";
39+
tensor_output_file << "=== TOKEN " << token_idx << " ===\n";
40+
tensor_output_file << "--- TENSOR: " << tensor_name << " ---\n";
41+
tensor_output_file << "SHAPE: [" << dim << "]\n";
42+
tensor_output_file << "DATA:\n";
3743

3844
for (int64_t i = 0; i < dim; ++i) {
3945
size_t offset = i * nb[0];
@@ -45,19 +51,19 @@ static void ggml_print_tensor_block(const std::string& tensor_name, uint8_t * da
4551
default: GGML_ABORT("Unsupported tensor type");
4652
}
4753

48-
std::cout << v;
49-
if (i < dim - 1) std::cout << ", ";
54+
tensor_output_file << v;
55+
if (i < dim - 1) tensor_output_file << ", ";
5056
}
5157

52-
std::cout << "\n\n";
58+
tensor_output_file << "\n\n";
5359
}
5460

5561
static bool ggml_debug(struct ggml_tensor * t, bool ask, void * user_data) {
5662
auto * cb_data = (callback_data *) user_data;
5763

5864
if (ask) {
5965
if (cb_data->parse_layer_name == "__LIST__") {
60-
std::cout << t->name << "\n";
66+
tensor_output_file << t->name << "\n";
6167
return false;
6268
}
6369
return std::string(t->name) == cb_data->parse_layer_name;
@@ -133,13 +139,21 @@ static bool run(llama_context * ctx, const common_params & params, callback_data
133139
llama_sampler_free(sampler);
134140

135141
// Output final result
136-
std::cout << "\n\nFull output:\n" << result << "\n";
142+
prompt_output_file << "\n\nFull output:\n" << result << "\n";
137143

138144
return true;
139145
}
140146

141147

142148
int main(int argc, char **argv) {
149+
prompt_output_file.open("prompt_output.txt");
150+
tensor_output_file.open("tensor_output.txt");
151+
152+
if (!prompt_output_file || !tensor_output_file) {
153+
std::cerr << "❌ Failed to open output files.\n";
154+
return 1;
155+
}
156+
143157
callback_data cb_data;
144158
common_params params;
145159
bool list_layers = false;
@@ -239,12 +253,15 @@ int main(int argc, char **argv) {
239253
LOG_ERR("Failed during layer listing run\n");
240254
return 1;
241255
}
256+
prompt_output_file.close();
257+
tensor_output_file.close();
258+
242259
return 0;
243260

244261
}
245262

246263
for (const auto& prompt : prompts) {
247-
LOG_INF("Running prompt: %s\n", prompt.c_str());
264+
prompt_output_file << "Running prompt: " << prompt << "\n";
248265
params.prompt = prompt;
249266
if (!run(ctx, params, cb_data)) {
250267
LOG_ERR("Failed on prompt: %s\n", prompt.c_str());
@@ -256,6 +273,8 @@ int main(int argc, char **argv) {
256273
llama_perf_context_print(ctx);
257274

258275
llama_backend_free();
276+
prompt_output_file.close();
277+
tensor_output_file.close();
259278

260279
return 0;
261280
}

0 commit comments

Comments
 (0)