Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark of compiler #3

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions include/easy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ THE SOFTWARE.
#include <iostream>
#include <random>
#include <utility>
#include <chrono>

// #include "micrograd.hpp"
// #include "micrograd.hpp"
#include "types.hpp"
#include "loss.hpp"
#include "mlp.hpp"
#include "sgd.hpp"

using namespace microgradCpp;
inline
DatasetType get_iris()
inline DatasetType get_iris()
{
// Load Iris dataset
std::vector<std::vector<std::shared_ptr<Value>>> inputs;
Expand All @@ -57,22 +57,20 @@ DatasetType get_iris()
}
return dataset;
}
inline
void shuffle(DatasetType &dataset)
inline void shuffle(DatasetType &dataset)
{
std::random_device rd;
std::mt19937 gen(rd());
gen.seed(42); // A fixed seed for reproducibility
std::shuffle(dataset.begin(), dataset.end(), gen);
}
inline
void train_test_split(
const DatasetType &dataset,
double TRAIN_SIZE,
ColRows &train_inputs,
ColRows &train_targets,
ColRows &test_inputs,
ColRows &test_targets)
inline void train_test_split(
const DatasetType &dataset,
double TRAIN_SIZE,
ColRows &train_inputs,
ColRows &train_targets,
ColRows &test_inputs,
ColRows &test_targets)
{

size_t train_size = static_cast<size_t>(dataset.size() * TRAIN_SIZE);
Expand All @@ -88,9 +86,8 @@ void train_test_split(
test_targets.push_back(dataset[i].second);
}
}

inline
void train_eval(const DatasetType &dataset, double TRAIN_SIZE, MLP &model, double lr = 0.01, int epochs = 100)

inline void train_eval(const DatasetType &dataset, double TRAIN_SIZE, MLP &model, double lr = 0.01, int epochs = 100)
{

// Split into train and test sets (80-20 split)
Expand All @@ -102,6 +99,8 @@ void train_eval(const DatasetType &dataset, double TRAIN_SIZE, MLP &model, dou
// Create SGD optimizer with a learning rate of 0.005
SGD optimizer(lr);

auto start = std::chrono::high_resolution_clock::now();

// int epochs = 100;
for (int epoch = 0; epoch < epochs; ++epoch)
{
Expand Down Expand Up @@ -161,10 +160,15 @@ void train_eval(const DatasetType &dataset, double TRAIN_SIZE, MLP &model, dou

double accuracy = static_cast<double>(correct) / test_inputs.size();
std::cout << "Epoch " << epoch + 1 << ": Test Accuracy = " << accuracy * 100.0 << "%" << std::endl;

if (epoch == epochs - 1)
{
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
std::cout << "Duration: " << duration.count() << " seconds" << std::endl;
}
}
}
}



#endif // EASY_HPP
69 changes: 55 additions & 14 deletions include/value.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef VALUE_HPP
#define VALUE_HPP

/*
MIT License

Expand Down Expand Up @@ -41,16 +40,26 @@ class Value : public std::enable_shared_from_this<Value>
double data; // Scalar value
double grad; // Gradient value
std::string label; // Optional label for debugging
bool cache_topology;
std::vector<std::shared_ptr<Value>> parents;
std::function<void()> _backward; // Backward function for autograd

mutable std::vector<std::shared_ptr<Value>> topo_cache;
mutable bool topo_cached = false;

// Constructor
Value(double data, const std::string &label = "")
: data(data), grad(0.0), label(label), _backward([]() {}) {}
Value(double data, const std::string &label = "", bool cache_topology = true )
: data(data),
grad(0.0),
label(label),
_backward([]() {}),
cache_topology(cache_topology) {}

// Copy constructor
Value(const Value &other)
: data(other.data), grad(other.grad), label(other.label), parents(other.parents), _backward(other._backward) {}
Value(const Value &other)
: data(other.data), grad(other.grad), label(other.label),
parents(other.parents), _backward(other._backward),
cache_topology(other.cache_topology) {}

// Add a parent to the computational graph (ensuring no duplicates)
void add_parent(const std::shared_ptr<Value> &parent)
Expand All @@ -61,10 +70,15 @@ class Value : public std::enable_shared_from_this<Value>
}
}

// Build topological order for backpropagation
// Build topological order for backpropagation (with caching)
std::vector<std::shared_ptr<Value>> build_topological_order()
{
std::vector<std::shared_ptr<Value>> topo;
if (topo_cached and cache_topology)
{
return topo_cache;
}

topo_cache.clear();
std::unordered_set<Value *> visited;

std::function<void(Value *)> visit = [&](Value *v)
Expand All @@ -76,14 +90,45 @@ class Value : public std::enable_shared_from_this<Value>
{
visit(p.get());
}
topo.push_back(v->shared_from_this());
topo_cache.push_back(v->shared_from_this());
}
};

visit(this);
return topo;
topo_cached = true;
return topo_cache;
}

// Method to reset the cache manually (if needed)
void reset_topo_cache()
{
topo_cached = false;
topo_cache.clear();
}

// Build topological order for backpropagation
// std::vector<std::shared_ptr<Value>> build_topological_order()
// {
// std::vector<std::shared_ptr<Value>> topo;
// std::unordered_set<Value *> visited;

// std::function<void(Value *)> visit = [&](Value *v)
// {
// if (v && visited.find(v) == visited.end())
// {
// visited.insert(v);
// for (auto &p : v->parents)
// {
// visit(p.get());
// }
// topo.push_back(v->shared_from_this());
// }
// };

// visit(this);
// return topo;
// }

// Backward propagation
void backward(double grad_init = 1.0)
{
Expand Down Expand Up @@ -204,8 +249,6 @@ std::shared_ptr<Value> operator+(const T &lhs, const std::shared_ptr<Value> &rhs
return rhs + lhs; // Reuse the above operator to avoid duplication
}



// ========================================================================
// Subtraction for two std::shared_ptr<Value> operands
// ========================================================================
Expand Down Expand Up @@ -362,8 +405,6 @@ std::shared_ptr<Value> operator/(const T &lhs, const std::shared_ptr<Value> &rhs
return rhs / lhs_value; // Reuse the previous operator
}



// Division
// std::shared_ptr<Value> operator/(const std::shared_ptr<Value>& lhs, const std::shared_ptr<Value>& rhs) {
// auto out = std::make_shared<Value>(lhs->data / rhs->data);
Expand All @@ -388,4 +429,4 @@ inline std::shared_ptr<Value> operator-(const std::shared_ptr<Value> &lhs)
return out;
}

#endif // VALUE_HPP
#endif // VALUE_HPP
45 changes: 45 additions & 0 deletions tests/main_no_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "micrograd.hpp"
using namespace microgradCpp;

int main()
{

DatasetType dataset = get_iris();
shuffle(dataset);
double TRAIN_SIZE{0.8};

// Create MLP model
// Input: 4 features, hidden layers: [7,7], output: 3 classes
// Define the model and hyperparameters
MLP model(4, {10, 10,10,10,10, 3});
double learning_rate = 0.01;
int epochs = 1000;

// Train and evaluate the model
train_eval(dataset, TRAIN_SIZE, model, learning_rate, epochs);

return 0;
}


/*
Notes
-----------

g++ -std=c++17 -Iinclude -O2 -o main main_no_cache.cpp

// or
make run

cache
14.6639 seconds
no cache
duration: 14.6024 seconds

MLP model(4, {10, 10,10,10,10, 3});
cache
37.644 seconds
no cache
37.5242 seconds

*/
Loading