Skip to content

Commit

Permalink
Moved Tensor forward pass logic to tensor_forward_wrapper.cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroarmas committed Apr 29, 2022
1 parent 028e46b commit 755f5f7
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 280 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ endif
VPATH = shared

MAIN = main.o
OBJS = main.o matrix.o generator.o matrix_printer.o functions.o network_layer.o m_algorithms_concepts.o m_algorithms.o m_algorithms_utilities.o m_algorithms_register.o matrix_benchmark.o activation_functions.o tensor.o
OBJS = main.o matrix.o generator.o matrix_printer.o functions.o network_layer.o m_algorithms_concepts.o m_algorithms.o m_algorithms_utilities.o m_algorithms_register.o matrix_benchmark.o activation_functions.o tensor.o tensor_forward_wrapper.o
OBJS_FOR_UNIT_TEST = $(foreach obj, $(OBJS), $(filter-out $(MAIN), $(wildcard *.o)))


Expand Down
1 change: 1 addition & 0 deletions activation_functions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <memory>

#include "tensor_forward_wrapper.h"
#include "activation_functions.h"
#include "m_algorithms.h"
// #include "matrix_printer.h"
Expand Down
125 changes: 13 additions & 112 deletions include/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "m_algorithms.h"
#include "m_algorithms_register.h"
#include "m_algorithms_concepts.h"
#include "matrix_benchmark.h"

#include <chrono>
#include <optional>
Expand All @@ -18,8 +17,21 @@ namespace NeuralNetwork {

namespace Graph {

using IsTrackable =
Matrix::NamedType<bool, struct TrackParameter>;


using IsLeaf =
Matrix::NamedType<bool, struct LeafParameter>;


using IsRecordable =
Matrix::NamedType<bool, struct RecordParameter>;


class RegisteredOperation;


class TensorStatistics {


Expand Down Expand Up @@ -55,14 +67,6 @@ namespace NeuralNetwork {
time_t matrix_operation_t2;
};

using IsTrackable =
Matrix::NamedType<bool, struct TrackParameter>;

using IsLeaf =
Matrix::NamedType<bool, struct LeafParameter>;

using IsRecordable =
Matrix::NamedType<bool, struct RecordParameter>;


class Tensor {
Expand Down Expand Up @@ -119,109 +123,6 @@ namespace NeuralNetwork {
};



/*
DESCRIPTION:
Functor follows 'Strategy' behavioral pattern for defining a family
of functions on the permutations of either benchmarking (or not) an
operation as well as the operation being binary (or unary).
USAGE:
TensorOp mm(std::make_unique<
Matrix::Operations::Binary::Multiplication::ParallelDNC>());
auto out = mm(input, this->matrix);
*/


template <Matrix::Operations::MatrixOperatable Operator>
class TensorOp {

public:
TensorOp(const Operator& _op) : op_type(_op) {}

std::shared_ptr<Tensor> operator()(
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r = nullptr);
private:
Operator op_type;
};


class ComputeTag;
class RecordTag;


class PerformTensorStrategy {

public:
PerformTensorStrategy() = default;

template <Matrix::Operations::MatrixOperatable Operator>
std::shared_ptr<Tensor> compute(
Operator _op,
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r,
ComputeTag _);

template <Matrix::Operations::MatrixOperatable Operator>
std::shared_ptr<Tensor> compute(
Operator _op,
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r,
RecordTag _);

};



/*
DESCRIPTION:
Curiosily recurring Template Pattern for
accepting Strategy implementation visitor
USAGE:
if (recordTensorOperation && isBinaryOp) {
RecordBinaryTag _;
return _.compute_tensor(std::move(op_type), l, r, implementation);
}
*/
template <class StrategyType>
struct StrategyTag {

template <Matrix::Operations::MatrixOperatable Operator>
std::shared_ptr<Tensor> compute_tensor(
Operator _op,
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r,
PerformTensorStrategy& strat_implementation) {

return strat_implementation.compute(
_op, l, r, *static_cast<
StrategyType const*>(this));
} };

class ComputeTag : public StrategyTag<ComputeTag> {
public:
ComputeTag() = default;
};
class RecordTag : public StrategyTag<RecordTag> {
public:
RecordTag() = default;
};


}

Expand Down
137 changes: 137 additions & 0 deletions include/tensor_forward_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#ifndef TENSOR_FORWARD_WRAPPER
#define TENSOR_FORWARD_WRAPPER


#include "tensor.h"
#include "matrix.h"
#include "m_algorithms.h"
#include "m_algorithms_register.h"
#include "m_algorithms_concepts.h"
#include "matrix_benchmark.h"

#include <chrono>
#include <optional>
#include <memory>



namespace NeuralNetwork {

namespace Computation {

namespace Graph {


/*
DESCRIPTION:
Functor follows 'Strategy' behavioral pattern for defining a family
of functions on the permutations of either benchmarking (or not) an
operation as well as the operation being binary (or unary).
USAGE:
TensorOp mm(std::make_unique<
Matrix::Operations::Binary::Multiplication::ParallelDNC>());
auto out = mm(input, this->matrix);
*/


template <Matrix::Operations::MatrixOperatable Operator>
class TensorOp {

public:
TensorOp(const Operator& _op) : op_type(_op) {}

std::shared_ptr<Tensor> operator()(
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r = nullptr);
private:
Operator op_type;
};


class ComputeTag;
class RecordTag;


class PerformTensorStrategy {

public:
PerformTensorStrategy() = default;

template <Matrix::Operations::MatrixOperatable Operator>
std::shared_ptr<Tensor> compute(
Operator _op,
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r,
ComputeTag _);

template <Matrix::Operations::MatrixOperatable Operator>
std::shared_ptr<Tensor> compute(
Operator _op,
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r,
RecordTag _);

};



/*
DESCRIPTION:
Curiosily recurring Template Pattern for
accepting Strategy implementation visitor
USAGE:
if (recordTensorOperation && isBinaryOp) {
RecordBinaryTag _;
return _.compute_tensor(std::move(op_type), l, r, implementation);
}
*/
template <class StrategyType>
struct StrategyTag {

template <Matrix::Operations::MatrixOperatable Operator>
std::shared_ptr<Tensor> compute_tensor(
Operator _op,
const std::shared_ptr<Tensor> l,
const std::shared_ptr<Tensor> r,
PerformTensorStrategy& strat_implementation) {

return strat_implementation.compute(
_op, l, r, *static_cast<
StrategyType const*>(this));
} };

class ComputeTag : public StrategyTag<ComputeTag> {
public:
ComputeTag() = default;
};
class RecordTag : public StrategyTag<RecordTag> {
public:
RecordTag() = default;
};




}

}

}


#endif // TENSOR_FORWARD_WRAPPER
5 changes: 0 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ int main(void) {

auto out = model.forward(ma);

// NeuralNetwork::Computation::Tree::ComputeOperation handler;
// handler.setNextHandler(std::make_unique<NeuralNetwork::Computation::Tree::TimerHandler>());
// handler.setNextHandler(std::make_unique<NeuralNetwork::Computation::Tree::CreateAutoDiffNode>());
// handler.handle("test");


return 0;
}
1 change: 1 addition & 0 deletions network_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <algorithm> // std::for_each

#include "tensor.h"
#include "tensor_forward_wrapper.h"
#include "network_layer.h"
#include "m_algorithms.h"
// #include "matrix_printer.h"
Expand Down
Loading

0 comments on commit 755f5f7

Please sign in to comment.