Skip to content

Commit

Permalink
clang-format -i
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieu Hoang committed Mar 6, 2021
1 parent 55f4216 commit ba19663
Show file tree
Hide file tree
Showing 14 changed files with 1,068 additions and 863 deletions.
70 changes: 40 additions & 30 deletions src/layers/constructors.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "layers/embedding.h"
#include "layers/factory.h"
#include "layers/generic.h"
#include "layers/embedding.h"
#include "layers/output.h"

namespace marian {
Expand Down Expand Up @@ -45,6 +45,7 @@ struct LogitLayerFactory : public Factory {
// @TODO: In the long run, I hope we can get rid of the abstract factories altogether.
class OutputFactory : public LogitLayerFactory {
using LogitLayerFactory::LogitLayerFactory;

protected:
std::string tiedTransposedName_;
Ptr<data::Shortlist> shortlist_;
Expand All @@ -55,9 +56,7 @@ class OutputFactory : public LogitLayerFactory {
return Accumulator<OutputFactory>(*this);
}

void setShortlist(Ptr<data::Shortlist> shortlist) {
shortlist_ = shortlist;
}
void setShortlist(Ptr<data::Shortlist> shortlist) { shortlist_ = shortlist; }

Ptr<IUnaryLogitLayer> construct(Ptr<ExpressionGraph> graph) override {
auto output = New<Output>(graph, options_);
Expand Down Expand Up @@ -89,8 +88,7 @@ class MLP : public IUnaryLogitLayer, public IHasShortList {
std::vector<Ptr<IUnaryLayer>> layers_;

public:
MLP(Ptr<ExpressionGraph> graph, Ptr<Options> options)
: graph_(graph), options_(options) {}
MLP(Ptr<ExpressionGraph> graph, Ptr<Options> options) : graph_(graph), options_(options) {}

Expr apply(const std::vector<Expr>& av) override {
Expr output;
Expand All @@ -106,46 +104,53 @@ class MLP : public IUnaryLogitLayer, public IHasShortList {
}

Logits applyAsLogits(const std::vector<Expr>& av) override {
// same as apply() except for the last layer, we invoke applyAsLogits(), which has a different return type
// same as apply() except for the last layer, we invoke applyAsLogits(), which has a different
// return type
auto lastLayer = std::dynamic_pointer_cast<IUnaryLogitLayer>(layers_.back());
ABORT_IF(!lastLayer, "MLP::applyAsLogits() was called on an MLP whose last layer is not an IUnaryLogitLayer");
if (layers_.size() == 1) {
if (av.size() == 1)
ABORT_IF(
!lastLayer,
"MLP::applyAsLogits() was called on an MLP whose last layer is not an IUnaryLogitLayer");
if(layers_.size() == 1) {
if(av.size() == 1)
return lastLayer->applyAsLogits(av[0]);
else
return lastLayer->applyAsLogits(av);
}
else {
} else {
Expr output;
if (av.size() == 1)
if(av.size() == 1)
output = layers_[0]->apply(av[0]);
else
output = layers_[0]->apply(av);
for (size_t i = 1; i < layers_.size() - 1; ++i)
for(size_t i = 1; i < layers_.size() - 1; ++i)
output = layers_[i]->apply(output);
return lastLayer->applyAsLogits(output);
}
}

Expr apply(Expr e) override { return apply(std::vector<Expr>{ e }); }
Logits applyAsLogits(Expr e) override { return applyAsLogits(std::vector<Expr>{ e }); }
Expr apply(Expr e) override { return apply(std::vector<Expr>{e}); }
Logits applyAsLogits(Expr e) override { return applyAsLogits(std::vector<Expr>{e}); }

void push_back(Ptr<IUnaryLayer> layer) { layers_.push_back(layer); }
void push_back(Ptr<IUnaryLogitLayer> layer) { layers_.push_back(layer); }

void setShortlist(Ptr<data::Shortlist> shortlist) override final {
auto p = tryAsHasShortlist();
ABORT_IF(!p, "setShortlist() called on an MLP with an output layer that does not support short lists");
ABORT_IF(
!p,
"setShortlist() called on an MLP with an output layer that does not support short lists");
p->setShortlist(shortlist);
}

void clear() override final {
auto p = tryAsHasShortlist();
if (p)
if(p)
p->clear();
}

private:
Ptr<IHasShortList> tryAsHasShortlist() const { return std::dynamic_pointer_cast<IHasShortList>(layers_.back()); }
Ptr<IHasShortList> tryAsHasShortlist() const {
return std::dynamic_pointer_cast<IHasShortList>(layers_.back());
}
};

/**
Expand All @@ -154,6 +159,7 @@ class MLP : public IUnaryLogitLayer, public IHasShortList {
*/
class MLPFactory : public Factory {
using Factory::Factory;

private:
std::vector<Ptr<LayerFactory>> layers_;

Expand All @@ -177,23 +183,27 @@ class MLPFactory : public Factory {
// which will go away if we get rid of the abstract factories, and instead just construct
// all layers immediately, which is my long-term goal for Marian.
private:
template<class WrappedFactory>
template <class WrappedFactory>
class AsLayerFactory : public LayerFactory {
WrappedFactory us;
WrappedFactory us;

public:
AsLayerFactory(const WrappedFactory& wrapped) : us(wrapped) {}
Ptr<IUnaryLayer> construct(Ptr<ExpressionGraph> graph) override final {
auto p = std::static_pointer_cast<IUnaryLayer>(us.construct(graph));
ABORT_IF(!p, "Attempted to cast a Factory to LayerFactory that isn't one");
return p;
}
AsLayerFactory(const WrappedFactory& wrapped) : us(wrapped) {}
Ptr<IUnaryLayer> construct(Ptr<ExpressionGraph> graph) override final {
auto p = std::static_pointer_cast<IUnaryLayer>(us.construct(graph));
ABORT_IF(!p, "Attempted to cast a Factory to LayerFactory that isn't one");
return p;
}
};
template<class WrappedFactory>
static inline AsLayerFactory<WrappedFactory> asLayerFactory(const WrappedFactory& wrapped) { return wrapped; }
template <class WrappedFactory>
static inline AsLayerFactory<WrappedFactory> asLayerFactory(const WrappedFactory& wrapped) {
return wrapped;
}

public:
Accumulator<MLPFactory> push_back(const Accumulator<OutputFactory>& lf) {
push_back(AsLayerFactory<OutputFactory>(lf));
//layers_.push_back(New<AsLayerFactory<OutputFactory>>(asLayerFactory((OutputFactory&)lf)));
// layers_.push_back(New<AsLayerFactory<OutputFactory>>(asLayerFactory((OutputFactory&)lf)));
return Accumulator<MLPFactory>(*this);
}
};
Expand Down
Loading

0 comments on commit ba19663

Please sign in to comment.