forked from Tiiny-AI/PowerInfer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.cpp
More file actions
111 lines (90 loc) · 3.35 KB
/
global.cpp
File metadata and controls
111 lines (90 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "global.h"
#include <cstdlib>
#include <mutex>
#include <string>
static Global_States* g_instance = nullptr;
static std::mutex g_mutex;
// 这些函数必须在 extern "C" 块中实现,与 global.h 的声明对应
extern "C" {
double op_max_time_map_reader(Global_States* state, const char* key) {
// if (!state) return 0.0;
std::string cpp_key(key);
return state->op_max_time_map[cpp_key];
}
void op_max_time_map_writer(Global_States* state, const char* key, double value) {
// if (!state) return;
std::string cpp_key(key);
state->op_max_time_map[cpp_key] = value;
}
double finished_threads_map_reader(Global_States* state, const char* key) {
// if (!state) return 0.0;
std::string cpp_key(key);
return (double)state->finished_threads_map[cpp_key];
}
void finished_threads_map_writer(Global_States* state, const char* key, int value) {
if (!state) return;
std::string cpp_key(key);
state->finished_threads_map[cpp_key] = value;
}
} // extern "C"
extern "C" {
Global_States* get_global_states(void) {
if (!g_instance) {
init_global_states();
}
return g_instance;
}
void init_global_states(void) {
if (!g_instance) {
g_instance = new Global_States();
}
}
void export_data(Global_States* states, char * fname) {
std::lock_guard<std::mutex> lock(g_mutex);
// export cpu_time_records to a csv file:
printf("Exporting time_records to %s\n", fname);
FILE* fp = fopen(fname, "w");
if (fp) {
// Write headers
fprintf(fp, "CPU Time Records,Token Time Records\n");
// Determine the maximum size between the two vectors
size_t max_size = std::max(states->cpu_time_records.size(), states->token_time_records.size());
// Write data row by row
for (size_t i = 0; i < max_size; i++) {
double cpu_time = (i < states->cpu_time_records.size()) ? states->cpu_time_records[i] : 0.0;
double token_time = (i < states->token_time_records.size()) ? states->token_time_records[i] : 0.0;
fprintf(fp, "%f,%f\n", cpu_time, token_time);
}
fclose(fp);
}
}
void export_data_batch(Global_States* states, int batch_size, float pre_ts, float dec_ts) {
std::lock_guard<std::mutex> lock(g_mutex);
// Open the batch.csv file in append mode
FILE* fp = fopen("batch.csv", "a+");
if (fp) {
// Check if the file is empty (first time creation)
fseek(fp, 0, SEEK_END);
if (ftell(fp) == 0) {
// Write the header
fprintf(fp, "batchsize,mode,throughput,CPU-latency-ratio\n");
}
// fprintf(fp, "%d,%s,%f,%f\n", batch_size, "Prefill", pre_ts, states->cpu_time_records[0]/states->token_time_records[0]);
size_t max_size = std::max(states->cpu_time_records.size(), states->token_time_records.size());
double cpu_time = 0.0;
double total_time = 0.0;
// Write data row by row
for (size_t i = 1; i < max_size; i++) {
cpu_time += states->cpu_time_records[i];
total_time += states->token_time_records[i];
}
// Append the data
fprintf(fp, "%d,%s,%f,%f\n", batch_size, "Decode", dec_ts, cpu_time/total_time);
fclose(fp);
}
}
}
void cleanup_global_states(void) {
delete g_instance;
g_instance = nullptr;
}