File size: 13,424 Bytes
f44acc0 |
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
#include <torch/torch.h>
#include <iostream>
#include <vector>
// Define device
torch::Device device(torch::kCUDA);
// Define constants
const int batch_size = 8;
const int block_size = 32;
const int max_iters = 1000;
const int eval_interval = 50;
const int eval_iters = 5;
const int d_model = 256;
const int n_layer = 16;
const int n_head = 12;
const float dropout = 0.2;
const float norm_eps = 1e-5;
const int vocab_size = 5;
// sample data
torch::Tensor train_data = torch::rand({1000, block_size});
torch::Tensor val_data = torch::rand({500, block_size});
// Data loading function
std::pair<torch::Tensor, torch::Tensor> get_batch(const std::string& split) {
torch::Tensor data = (split == "train") ? train_data : val_data;
torch::Tensor ix = torch::randint(data.size(0) - block_size, {batch_size});
torch::Tensor x = torch::empty({batch_size, block_size});
torch::Tensor y = torch::empty({batch_size, block_size});
for (int i = 0; i < batch_size; ++i) {
x[i] = data.index({ix[i], ix[i] + block_size});
y[i] = data.index({ix[i] + 1, ix[i] + block_size + 1});
}
return std::make_pair(x.to(device), y.to(device));
}
// Custom classes and functions
class SWiGLU : public torch::nn::Module {
public:
SWiGLU() {}
torch::Tensor forward(torch::Tensor x) {
torch::Tensor sigmoid_output = torch::sigmoid(x);
torch::Tensor relu_output = torch::relu(x);
torch::Tensor out = sigmoid_output * relu_output + (1 - sigmoid_output) * x;
return out;
}
};
class UnMaskedHeadImpl : public torch::nn::Module {
public:
UnMaskedHeadImpl(int d_model, int head_size, float dropout)
: key(register_module("key", torch::nn::Linear(d_model, head_size))),
query(register_module("query", torch::nn::Linear(d_model, head_size))),
value(register_module("value", torch::nn::Linear(d_model, head_size))),
dropout(torch::nn::Dropout(dropout)) {
register_module("dropout", dropout);
}
torch::Tensor forward(torch::Tensor x) {
torch::Tensor key_out = key->forward(x);
torch::Tensor query_out = query->forward(x);
torch::Tensor weights = query_out.matmul(key_out.transpose(-2, -1)) * std::sqrt(key_out.size(-1));
weights = torch::softmax(weights, -1);
weights = dropout(weights);
torch::Tensor value_out = value->forward(x);
torch::Tensor out = weights.matmul(value_out);
return out;
}
private:
torch::nn::Linear key, query, value;
torch::nn::Dropout dropout;
};
TORCH_MODULE(UnMaskedHead);
class MaskedHeadImpl : public torch::nn::Module {
public:
MaskedHeadImpl(int head_size, float dropout, int d_model)
: key(register_module("key", torch::nn::Linear(d_model, head_size))),
query(register_module("query", torch::nn::Linear(d_model, head_size))),
value(register_module("value", torch::nn::Linear(d_model, head_size))),
dropout(torch::nn::Dropout(dropout)) {
register_buffer("tril", torch::tril(torch::ones(block_size, block_size)));
}
torch::Tensor forward(torch::Tensor x) {
torch::Tensor key_out = key->forward(x);
torch::Tensor query_out = query->forward(x);
torch::Tensor weights = query_out.matmul(key_out.transpose(-2, -1)) * std::sqrt(key_out.size(-1));
weights = weights.masked_fill(tril[:x.size(1), :x.size(1)] == 0, std::numeric_limits<float>::lowest());
weights = torch::softmax(weights, -1);
weights = dropout(weights);
torch::Tensor value_out = value->forward(x);
torch::Tensor out = weights.matmul(value_out);
return out;
}
private:
torch::nn::Linear key, query, value;
torch::nn::Dropout dropout;
torch::Tensor tril;
};
TORCH_MODULE(MaskedHead);
class MultiUnMaskedImpl : public torch::nn::Module {
public:
MultiUnMaskedImpl(int d_model, int n_head, float dropout)
: proj(register_module("proj", torch::nn::Linear(n_head * (d_model / n_head), d_model))),
dropout(torch::nn::Dropout(dropout)) {
for (int i = 0; i < n_head; ++i) {
heads.push_back(register_module("head" + std::to_string(i), UnMaskedHead(d_model, d_model / n_head, dropout)));
}
}
torch::Tensor forward(torch::Tensor x) {
std::vector<torch::Tensor> head_outputs;
for (auto& head : heads) {
head_outputs.push_back(head->forward(x));
}
torch::Tensor out = torch::cat(head_outputs, -1);
out = dropout(out);
out = proj(out);
return out;
}
private:
torch::nn::Linear proj;
torch::nn::Dropout dropout;
std::vector<UnMaskedHead> heads;
};
TORCH_MODULE(MultiUnMasked);
class MultiMaskedImpl : public torch::nn::Module {
public:
MultiMaskedImpl(int d_model, int n_head, float dropout)
: proj(register_module("proj", torch::nn::Linear(n_head * (d_model / n_head), d_model))),
dropout(torch::nn::Dropout(dropout)) {
for (int i = 0; i < n_head; ++i) {
heads.push_back(register_module("head" + std::to_string(i), MaskedHead(d_model, d_model / n_head, dropout)));
}
}
torch::Tensor forward(torch::Tensor x) {
std::vector<torch::Tensor> head_outputs;
for (auto& head : heads) {
head_outputs.push_back(head->forward(x));
}
torch::Tensor out = torch::cat(head_outputs, -1);
out = dropout(out);
out = proj(out);
return out;
}
private:
torch::nn::Linear proj;
torch::nn::Dropout dropout;
std::vector<MaskedHead> heads;
};
TORCH_MODULE(MultiMasked);
class FeedForwardImpl : public torch::nn::Module {
public:
FeedForwardImpl(int d_model, float dropout)
: net(register_module("net", torch::nn::Sequential(
torch::nn::Linear(d_model, 4 * d_model),
torch::nn::GELU(),
torch::nn::Linear(4 * d_model, d_model),
torch::nn::Dropout(dropout)
))) {}
torch::Tensor forward(torch::Tensor x) {
return net->forward(x);
}
private:
torch::nn::Sequential net;
};
TORCH_MODULE(FeedForward);
class BlockImpl : public torch::nn::Module {
public:
BlockImpl(int d_model, int n_head, float norm_eps, float dropout)
: sa_masked(MultiMasked(d_model, n_head, dropout)),
sa_unmasked(MultiUnMasked(d_model, n_head, dropout)),
ffwd(FeedForward(d_model, dropout)),
norm1(torch::nn::LayerNorm(torch::nn::LayerNormOptions({d_model}).eps(norm_eps))),
norm2(torch::nn::LayerNorm(torch::nn::LayerNormOptions({d_model}).eps(norm_eps))) {}
torch::Tensor forward(torch::Tensor x) {
torch::Tensor x2 = x + sa_unmasked->forward(norm1->forward(x));
x = x2 + ffwd->forward(norm2->forward(x2));
x2 = x + sa_masked->forward(norm1->forward(x));
x = x2 + ffwd->forward(norm2->forward(x2));
return x;
}
private:
MultiMasked sa_masked;
MultiUnMasked sa_unmasked;
FeedForward ffwd;
torch::nn::LayerNorm norm1, norm2;
};
TORCH_MODULE(Block);
class EnigmaImpl : public torch::nn::Module {
public:
EnigmaImpl(int vocab_size, int block_size, int d_model, int n_layer, int n_head, float dropout, float norm_eps)
: toked_model(register_module("toked_model", torch::nn::Embedding(vocab_size, d_model))),
pos_encod(register_module("pos_encod", torch::nn::Embedding(block_size, d_model))),
norm_final(torch::nn::LayerNorm(torch::nn::LayerNormOptions({d_model}).eps(norm_eps))),
linear_final(register_module("linear_final", torch::nn::Linear(d_model, vocab_size))) {
for (int i = 0; i < n_layer; ++i) {
block_layers.push_back(register_module("block" + std::to_string(i), Block(d_model, n_head, norm_eps, dropout)));
}
register_buffer("block_size", torch::tensor(block_size));
_init_weights(this);
}
void _init_weights(torch::nn::Module* module) {
auto parameters = module->named_parameters();
for (auto& param : parameters) {
if (param.key().find("weight") != std::string::npos) {
torch::nn::init::normal_(param.value(), 0.0, 0.02);
} else if (param.key().find("bias") != std::string::npos) {
torch::nn::init::zeros_(param.value());
}
}
}
std::pair<torch::Tensor, torch::Tensor> forward(torch::Tensor idx, torch::Tensor targets=torch::Tensor()) {
torch::Tensor toked_model_out = toked_model->forward(idx);
torch::Tensor pos_encod_out = pos_encod->forward(torch::arange(idx.size(1)));
torch::Tensor x = toked_model_out + pos_encod_out;
for (auto& block : block_layers) {
x = block->forward(x);
}
torch::Tensor logits = linear_final->forward(norm_final->forward(x));
if (!targets.numel()) {
return {logits, torch::Tensor()};
} else {
logits = logits.view({-1, logits.size(-1)});
targets = targets.view({-1});
torch::Tensor loss = torch::nn::functional::cross_entropy(logits, targets);
return {logits, loss};
}
}
std::vector<std::vector<std::pair<torch::Tensor, float>>> complex_generate(torch::Tensor idx, int max_new_tokens, float temperature=1.0, int top_k=3, int beam_width=5) {
std::vector<std::vector<std::pair<torch::Tensor, float>>> completed_beams;
torch::Tensor current_idx = idx.clone();
std::vector<std::pair<torch::Tensor, float>> beam = {std::make_pair(current_idx, 0.0)};
for (int i = 0; i < max_new_tokens; ++i) {
std::vector<std::pair<torch::Tensor, float>> new_beam;
for (auto& beam_item : beam) {
torch::Tensor& current_idx = beam_item.first;
torch::Tensor logits, loss;
std::tie(logits, loss) = forward(current_idx);
logits = logits.index({torch::indexing::Slice(), -1}); // Get last token predictions
// Apply softmax and temperature
torch::Tensor probs = torch::nn::functional::softmax(logits / temperature, -1);
// Top-k sampling
if (top_k > 0) {
probs = top_k_filtering(probs, top_k);
}
// Sample from the distribution
torch::Tensor sampled_idx = torch::multinomial(probs, beam_width, true);
for (int j = 0; j < beam_width; ++j) {
torch::Tensor new_idx = torch::cat({current_idx, sampled_idx.index({torch::indexing::Slice(), j})}, 1);
torch::Tensor new_log_prob = beam_item.second + torch::log(probs.index({torch::indexing::Slice(), sampled_idx.index({torch::indexing::Slice(), j})}));
new_beam.push_back(std::make_pair(new_idx, new_log_prob.item()));
}
}
// Sort new beam by log probabilities
std::sort(new_beam.begin(), new_beam.end(), [](const std::pair<torch::Tensor, float>& a, const std::pair<torch::Tensor, float>& b) {
return a.second > b.second;
});
// Only keep top beams
beam = std::vector<std::pair<torch::Tensor, float>>(new_beam.begin(), new_beam.begin() + beam_width);
}
completed_beams.push_back(beam);
return completed_beams;
}
std::vector<std::vector<std::pair<torch::Tensor, float>>> top_k_filtering(torch::Tensor logits, int top_k) {
torch::Tensor top_values, top_indices;
std::tie(top_values, top_indices) = torch::topk(logits, top_k, -1);
torch::Tensor min_value = torch::index_select(top_values, -1, torch::tensor({top_k-1}));
torch::Tensor filtered_logits = torch::where(logits < min_value, torch::full_like(logits, -std::numeric_limits<float>::infinity()), logits);
return filtered_logits;
}
private:
torch::nn::Embedding toked_model, pos_encod;
std::vector<Block> block_layers;
torch::nn::LayerNorm norm_final;
torch::nn::Linear linear_final;
int block_size;
};
TORCH_MODULE(Enigma);
int main() {
// Set seed
torch::manual_seed(1400);
// Create model
Enigma model(vocab_size, block_size, d_model, n_layer, n_head, dropout, norm_eps);
model->to(device);
// Define optimizer
torch::optim::AdamW optimizer(model->parameters(), torch::optim::AdamWOptions(learning_rate));
// Training loop
std::vector<float> train_losses, val_losses;
for (int iter = 0; iter < max_iters; ++iter) {
if (iter % eval_interval == 0 || iter == max_iters - 1) {
// Evaluate and print losses
auto losses = estimate_loss();
std::cout << "step " << iter << ": train loss " << losses["train"] << ", val loss " << losses["val"] << std::endl;
// Save losses for plotting
train_losses.push_back(losses["train"]);
val_losses.push_back(losses["val"]);
}
// Get batch, forward pass, loss calculation, backward pass, optimizer step
auto [xb, yb] = get_batch("train");
torch::Tensor logits, loss;
std::tie(logits, loss) = model->forward(xb, yb);
optimizer.zero_grad();
loss.backward();
optimizer.step();
}
return 0;
}
|