-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
277 lines (235 loc) · 9.23 KB
/
Copy pathmain.cpp
File metadata and controls
277 lines (235 loc) · 9.23 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "nn.hpp"
#include "tokenizer.hpp"
#include "dataset.hpp"
#include "config.hpp"
#include "visualizer.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <fstream>
#include <cstdlib>
#include <chrono>
namespace fs = std::filesystem;
// Default dataset JSONL content
const std::string DEFAULT_DATASET_JSONL = R"({"prompt": "What is 2 + 2?", "answer": "4"}
{"prompt": "What is the capital of France?", "answer": "Paris"}
{"prompt": "What color is the sky?", "answer": "Blue"}
{"prompt": "Is water wet?", "answer": "Yes"}
{"prompt": "What is the speed of light?", "answer": "299,792,458 m/s"}
{"prompt": "Who invented the telephone?", "answer": "Alexander Graham Bell"}
{"prompt": "What is the largest planet?", "answer": "Jupiter"}
{"prompt": "How many days in a week?", "answer": "7"}
{"prompt": "What gas do plants absorb?", "answer": "Carbon dioxide"}
{"prompt": "What is the boiling point of water?", "answer": "100 degrees Celsius"}
{"prompt": "What is the capital of Japan?", "answer": "Tokyo"}
{"prompt": "What is the capital of Germany?", "answer": "Berlin"}
{"prompt": "What color is grass?", "answer": "Green"}
{"prompt": "How many continents are there?", "answer": "7"}
{"prompt": "Who wrote Hamlet?", "answer": "William Shakespeare"}
{"prompt": "What is the square root of 9?", "answer": "3"}
{"prompt": "What is the chemical formula for water?", "answer": "H2O"}
{"prompt": "Who was the first president of the United States?", "answer": "George Washington"}
{"prompt": "Which country is home to the kangaroo?", "answer": "Australia"}
{"prompt": "How many legs does a spider have?", "answer": "8"}
)";
std::string get_home_dir() {
const char* h = std::getenv("HOME");
return h ? std::string(h) : std::string("/root");
}
void ensure_directory_exists(const std::string& path) {
try {
fs::create_directories(path);
} catch (...) {}
}
void setup_default_files(const std::string& config_dir) {
ensure_directory_exists(config_dir);
ensure_directory_exists(config_dir + "/dataset");
std::string config_path = config_dir + "/config.conf";
if (!fs::exists(config_path)) {
Config::write_default(config_path);
}
std::string dataset_path = config_dir + "/dataset/dataset.jsonl";
if (!fs::exists(dataset_path)) {
std::ofstream f(dataset_path);
if (f) {
f << DEFAULT_DATASET_JSONL;
}
}
}
// Train
bool run_training(NeuralNet& nn, Tokenizer& tok, const std::string& dataset_path,
const std::string& model_path, const std::string& tokenizer_path,
int epochs, float lr, Visualizer* vis) {
Dataset ds;
if (ds.load(dataset_path) == 0) {
if (vis) {
vis->draw_training(nn, 0, epochs, 0.0f, 0.0f, 0, "Error: Dataset is empty or not found!");
vis->sleep_ms(3000);
} else {
std::cerr << "Error: Dataset is empty or not found at " << dataset_path << std::endl;
}
return false;
}
// Vocab
tok.build_vocab(ds.all_prompts());
nn.init(ds.labels());
if (vis) {
vis->draw_training(nn, 0, epochs, 0.0f, 0.0f, ds.size(), "Starting training...");
vis->sleep_ms(500);
} else {
std::cout << "Starting training on " << ds.size() << " examples..." << std::endl;
}
// Prep Train input
struct TrainItem {
std::vector<int> tokens;
int label_idx;
};
std::vector<TrainItem> train_data;
train_data.reserve(ds.size());
for (const auto& ex : ds.examples()) {
train_data.push_back({
tok.tokenize(ex.prompt),
ds.label_index(ex.answer)
});
}
// Train (Main)
for (int epoch = 1; epoch <= epochs; ++epoch) {
float total_loss = 0.0f;
int correct = 0;
for (const auto& item : train_data) {
float loss = nn.train_step(item.tokens, item.label_idx, lr);
total_loss += loss;
int pred = nn.predict(item.tokens);
if (pred == item.label_idx) {
correct++;
}
}
float avg_loss = total_loss / train_data.size();
float accuracy = static_cast<float>(correct) / train_data.size();
if (vis) {
vis->draw_training(nn, epoch, epochs, avg_loss, accuracy, ds.size(), "Optimizing parameters...");
vis->sleep_ms(15); // visual smooth updates
// Check for exit
int ch = vis->get_key();
if (ch == 3 || ch == 'q') {
break;
}
} else {
if (epoch % 10 == 0 || epoch == epochs) {
std::cout << "Epoch " << epoch << "/" << epochs
<< " - Loss: " << avg_loss
<< " - Accuracy: " << (accuracy * 100.0f) << "%" << std::endl;
}
}
}
nn.save(model_path);
tok.save(tokenizer_path);
if (vis) {
vis->draw_training(nn, epochs, epochs, 0.0f, 1.0f, ds.size(), "Training complete! Model saved.");
vis->sleep_ms(1500);
} else {
std::cout << "Training complete! Saved model to " << model_path << std::endl;
}
return true;
}
int main(int argc, char* argv[]) {
bool train_only = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--train" || arg == "-t") {
train_only = true;
} else if (arg == "--help" || arg == "-h") {
std::cout << "nvisual - CLI Neural Network Visualizer\n\n"
<< "Usage:\n"
<< " nvisual Run the CLI neural network visualizer\n"
<< " nvisual --train Train/Fine-tune the neural network on the local dataset\n\n"
<< "Configuration:\n"
<< " Config file: ~/.config/nvisual/config.conf\n"
<< " Dataset: ~/.config/nvisual/dataset/dataset.jsonl\n"
<< " Model weights: ~/.config/nvisual/model.bin\n";
return 0;
}
}
std::string home_dir = get_home_dir();
std::string config_dir = home_dir + "/.config/nvisual";
setup_default_files(config_dir);
std::string config_path = config_dir + "/config.conf";
std::string dataset_path = config_dir + "/dataset/dataset.jsonl";
std::string model_path = config_dir + "/model.bin";
std::string tokenizer_path = config_dir + "/tokenizer.txt";
Config cfg;
cfg.load(config_path);
NeuralNet nn;
Tokenizer tok;
if (train_only) {
Visualizer vis;
vis.init();
run_training(nn, tok, dataset_path, model_path, tokenizer_path,
cfg.get().train_epochs, cfg.get().learning_rate, &vis);
vis.cleanup();
return 0;
}
if (!fs::exists(model_path) || !fs::exists(tokenizer_path)) {
Visualizer vis;
vis.init();
vis.draw_splash(nn, 0);
vis.sleep_ms(1500);
run_training(nn, tok, dataset_path, model_path, tokenizer_path,
cfg.get().train_epochs, cfg.get().learning_rate, &vis);
vis.cleanup();
}
// Load model
if (!nn.load(model_path) || !tok.load(tokenizer_path)) {
std::cerr << "Failed to load model or tokenizer. Please run: nvisual --train" << std::endl;
return 1;
}
Visualizer vis;
vis.init();
std::vector<std::string> prompts = cfg.get().prompts;
if (prompts.empty()) {
prompts = {
"What is 2 + 2?",
"What is the capital of France?",
"What color is the sky?",
"Is water wet?",
"What is the speed of light?"
};
}
int prompt_idx = 0;
bool running = true;
while (running) {
std::string current_prompt = prompts[prompt_idx];
auto tokens = tok.tokenize(current_prompt);
auto probs = nn.forward(tokens);
int pred_idx = nn.predict(tokens);
std::string pred_label = nn.labels[pred_idx];
float confidence = probs[pred_idx];
vis.animate_ball(nn, current_prompt, "Processing...", confidence, cfg.get().warn_accuracy, 0, 1, 15, 30);
vis.animate_ball(nn, current_prompt, "Processing...", confidence, cfg.get().warn_accuracy, 1, 2, 15, 30);
vis.animate_ball(nn, current_prompt, pred_label, confidence, cfg.get().warn_accuracy, 2, 3, 15, 30);
BallPos static_ball;
static_ball.x = 1.0f;
static_ball.src_layer = 2;
static_ball.dst_layer = 3;
static_ball.src_neuron = 0;
static_ball.dst_neuron = pred_idx;
vis.draw_inference(nn, current_prompt, pred_label, confidence, cfg.get().warn_accuracy, static_ball, -1);
int delay = cfg.get().cycle_delay_ms;
auto start_wait = std::chrono::steady_clock::now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start_wait).count() < delay) {
int ch = vis.get_key();
if (vis.is_quit_key(ch)) {
running = false;
break;
} else if (ch == ' ' || ch == '\n' || ch == KEY_RIGHT) {
break;
}
vis.sleep_ms(20);
}
prompt_idx = (prompt_idx + 1) % prompts.size();
}
vis.cleanup();
return 0;
}