Skip to content

Commit

Permalink
Network benchmark graph build and matrix computation on a forward pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroarmas committed Apr 20, 2022
1 parent 4247d79 commit bf6223a
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 134 deletions.
3 changes: 1 addition & 2 deletions activation_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ namespace NeuralNetwork {



Computation::Graph::TensorOp relu(std::make_unique<Matrix::Operations::Timer>(
std::make_unique<Matrix::Operations::Unary::ReLU>()));
Computation::Graph::TensorOp<Matrix::Operations::Unary::ReLU> relu(Matrix::Operations::Unary::ReLU{});



Expand Down
85 changes: 49 additions & 36 deletions include/m_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include <algorithm>
#include <functional>
#include <exception>
#include <iostream>
#include <variant>

#include "matrix.h"
#include "m_algorithms_concepts.h"


namespace Matrix {
Expand All @@ -16,8 +19,9 @@ namespace Matrix {

enum struct Code {
NOP, MULTIPLY, PLUS, ReLU, OUTER_PRODUCT, HADAMARD,

};


class BaseInterface {

Expand All @@ -36,23 +40,24 @@ namespace Matrix {
template <class Implementation>
class UnaryAdapter : public BaseInterface {

std::unique_ptr<Matrix::Representation> operator()(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r = nullptr) override {
public:
std::unique_ptr<Matrix::Representation> operator()(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r = nullptr) override {

if (!l) {
throw std::invalid_argument("Left operand not referencing a matrix.");
}
if (!l) {
throw std::invalid_argument("Left operand not referencing a matrix.");
}


if (r != nullptr) {
throw std::invalid_argument("Unary Operation needs one operand.");
}
return Impl().operator()(l);
};
if (r != nullptr) {
throw std::invalid_argument("Unary Operation needs one operand.");
}
return Impl().operate(l);
};

~UnaryAdapter() = default;
Code get_operation_code() override { return Impl().get_operation_code(); };
Code get_operation_code() override { return Impl().get_code(); };

private:
Implementation& Impl() { return *static_cast<Implementation*>(this); }
Expand All @@ -61,16 +66,19 @@ namespace Matrix {

};




class ReLU : public UnaryAdapter<ReLU> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& m);
Code get_operation_code() { return Code::ReLU; };
Code get_code() { return Code::ReLU; };

};

static_assert(MatrixOperatable<ReLU>);


}
Expand All @@ -94,10 +102,10 @@ namespace Matrix {
throw std::invalid_argument("Right operand not referencing a matrix.");
}

return Impl().operator()(l, r);
return Impl().operate(l, r);
};
virtual ~BaseOp() = default;
Code get_operation_code() override { return Impl().get_operation_code(); };
Code get_operation_code() override { return Impl().get_code(); };
private:
Implementation& Impl() { return *static_cast<Implementation*>(this); }
friend Implementation;
Expand All @@ -106,11 +114,6 @@ namespace Matrix {



std::string debug_message(const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);

std::string debug_message_2(const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);



Expand All @@ -119,32 +122,36 @@ namespace Matrix {

class Std : public BaseOp<Std> {
public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_operation_code() { return Code::PLUS; };
Code get_code() { return Code::PLUS; };

};

}

static_assert(MatrixOperatable<Addition::Std>);


namespace OuterProduct {



class Naive : public BaseOp<Naive> {
public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_operation_code() { return Code::OUTER_PRODUCT; };
Code get_code() { return Code::OUTER_PRODUCT; };

};



}

static_assert(MatrixOperatable<OuterProduct::Naive>);



Expand All @@ -156,27 +163,29 @@ namespace Matrix {
class Naive : public BaseOp<Naive> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_operation_code() { return Code::HADAMARD; };
Code get_code() { return Code::HADAMARD; };

};


class Std : public BaseOp<Naive> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_operation_code() { return Code::HADAMARD; };
Code get_code() { return Code::HADAMARD; };

};


}

static_assert(MatrixOperatable<HadamardProduct::Naive>);
static_assert(MatrixOperatable<HadamardProduct::Std>);



Expand All @@ -196,10 +205,10 @@ namespace Matrix {
class Naive : public BaseOp<Naive> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_operation_code() { return Code::MULTIPLY; };
Code get_code() { return Code::MULTIPLY; };


};
Expand All @@ -208,21 +217,21 @@ namespace Matrix {
class Square : public BaseOp<Square> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r) ;
Code get_operation_code() { return Code::MULTIPLY; };
Code get_code() { return Code::MULTIPLY; };

};


class ParallelDNC : public BaseOp<ParallelDNC> {

public:
std::unique_ptr<Matrix::Representation> operator()(
std::unique_ptr<Matrix::Representation> operate(
const std::unique_ptr<Matrix::Representation>& l,
const std::unique_ptr<Matrix::Representation>& r);
Code get_operation_code() { return Code::MULTIPLY; };
Code get_code() { return Code::MULTIPLY; };

};

Expand All @@ -232,6 +241,10 @@ namespace Matrix {


} // namespace Multiplication

static_assert(MatrixOperatable<Multiplication::Naive>);
static_assert(MatrixOperatable<Multiplication::Square>);
static_assert(MatrixOperatable<Multiplication::ParallelDNC>);

} // namespace Binary

Expand Down
38 changes: 32 additions & 6 deletions include/network_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
#include <functional>
#include <map>

#include <concepts>
#include <iostream>

#define FLAT 1

template<typename T>
const char* getClassName(T) {
return typeid(T).name();
}


namespace NeuralNetwork {

Expand Down Expand Up @@ -52,12 +56,36 @@ namespace NeuralNetwork {

public:
std::shared_ptr<Tensor> forward(std::shared_ptr<Tensor> input) override {


// TODO: print, or error checking.

auto out = Impl().doForward(input);


if (out->stats.has_value()) {

auto m2 = out->stats->get_matrix_end();
auto m1 = out->stats->get_matrix_start();

// TODO: print, or error checking.
std::cout << "Entered ComputationalStep()" << std::endl;
auto g2 = out->stats->get_graph_end();
auto g1 = out->stats->get_graph_start();

return Impl().doForward(input);
auto op_str = out->stats->get_operation_string();


auto time_performing_operation = std::chrono::duration_cast<std::chrono::duration<int, std::micro>>(m2 - m1).count();
auto time_making_graph = std::chrono::duration_cast<std::chrono::duration<int, std::micro>>(g2 - g1).count() - time_performing_operation;

std::cout << op_str << " performance: " << std::endl;
std::cout << "\t Time making graph (ms): " << time_making_graph << std::endl;
std::cout << "\t Time performing operation (ms): " << time_performing_operation << std::endl;

}

return out;
}

~ComputationalStep() {}
private:
Implementation& Impl() { return *static_cast<Implementation*>(this); }
Expand Down Expand Up @@ -191,8 +219,6 @@ namespace NeuralNetwork {
std::map<const unsigned int, std::unique_ptr<StepInterface>> _modules;
unsigned int last_key;



};


Expand Down
Loading

0 comments on commit bf6223a

Please sign in to comment.