<commit_before>#pragma once

#include "rnn/rnn.h"
#include "layers/factory.h"

namespace marian {
namespace rnn {

struct StackableFactory : public Factory {
  StackableFactory(Ptr<ExpressionGraph> graph) : Factory(graph) {}
  StackableFactory(const StackableFactory&) = default;
  StackableFactory(StackableFactory&&) = default;

  virtual ~StackableFactory() {}

  template <typename Cast>
  inline Ptr<Cast> as() {
    return std::dynamic_pointer_cast<Cast>(shared_from_this());
  }

  template <typename Cast>
  inline bool is() {
    return as<Cast>() != nullptr;
  }
};

struct InputFactory : public StackableFactory {
  InputFactory(Ptr<ExpressionGraph> graph) : StackableFactory(graph) {}
  virtual Ptr<CellInput> construct() = 0;
};

class CellFactory : public StackableFactory {
public:
  CellFactory(Ptr<ExpressionGraph> graph) : StackableFactory(graph) {}

  virtual Ptr<Cell> construct() {
    std::string type = options_->get<std::string>("type");
    if(type == "gru")
      return New<GRU>(graph_, options_);
    if(type == "lstm")
      return New<LSTM>(graph_, options_);
    if(type == "mlstm")
      return New<MLSTM>(graph_, options_);
    if(type == "mgru")
      return New<MGRU>(graph_, options_);
    if(type == "tanh")
      return New<Tanh>(graph_, options_);
    return New<GRU>(graph_, options_);
  }

  CellFactory clone() {
    CellFactory aClone(graph_);
    aClone.options_->merge(options_);
    return aClone;
  }

};

typedef Accumulator<CellFactory> cell;

class StackedCellFactory : public CellFactory {
protected:
  std::vector<Ptr<StackableFactory>> stackableFactories_;

public:
  StackedCellFactory(Ptr<ExpressionGraph> graph) : CellFactory(graph) {}

  Ptr<Cell> construct() {
    auto stacked = New<StackedCell>(graph_, options_);

    int lastDimInput = options_->get<int>("dimInput");

    for(int i = 0; i < stackableFactories_.size(); ++i) {
      auto sf = stackableFactories_[i];

      if(sf->is<CellFactory>()) {
        auto cellFactory = sf->as<CellFactory>();
        cellFactory->getOptions()->merge(options_);

        if(i > 0) {
          sf->getOptions()->set("dimInput", lastDimInput);
          lastDimInput = 0;
        }

        stacked->push_back(cellFactory->construct());<commit_msg>shift dimInput<commit_after>#pragma once

#include "rnn/rnn.h"
#include "layers/factory.h"

namespace marian {
namespace rnn {

struct StackableFactory : public Factory {
  StackableFactory(Ptr<ExpressionGraph> graph) : Factory(graph) {}
  StackableFactory(const StackableFactory&) = default;
  StackableFactory(StackableFactory&&) = default;

  virtual ~StackableFactory() {}

  template <typename Cast>
  inline Ptr<Cast> as() {
    return std::dynamic_pointer_cast<Cast>(shared_from_this());
  }

  template <typename Cast>
  inline bool is() {
    return as<Cast>() != nullptr;
  }
};

struct InputFactory : public StackableFactory {
  InputFactory(Ptr<ExpressionGraph> graph) : StackableFactory(graph) {}
  virtual Ptr<CellInput> construct() = 0;
};

class CellFactory : public StackableFactory {
public:
  CellFactory(Ptr<ExpressionGraph> graph) : StackableFactory(graph) {}

  virtual Ptr<Cell> construct() {
    std::string type = options_->get<std::string>("type");
    if(type == "gru")
      return New<GRU>(graph_, options_);
    if(type == "lstm")
      return New<LSTM>(graph_, options_);
    if(type == "mlstm")
      return New<MLSTM>(graph_, options_);
    if(type == "mgru")
      return New<MGRU>(graph_, options_);
    if(type == "tanh")
      return New<Tanh>(graph_, options_);
    return New<GRU>(graph_, options_);
  }

  CellFactory clone() {
    CellFactory aClone(graph_);
    aClone.options_->merge(options_);
    return aClone;
  }

};

typedef Accumulator<CellFactory> cell;

class StackedCellFactory : public CellFactory {
protected:
  std::vector<Ptr<StackableFactory>> stackableFactories_;

public:
  StackedCellFactory(Ptr<ExpressionGraph> graph) : CellFactory(graph) {}

  Ptr<Cell> construct() {
    auto stacked = New<StackedCell>(graph_, options_);

    int lastDimInput = options_->get<int>("dimInput");

    for(int i = 0; i < stackableFactories_.size(); ++i) {
      auto sf = stackableFactories_[i];

      if(sf->is<CellFactory>()) {
        auto cellFactory = sf->as<CellFactory>();
        cellFactory->getOptions()->merge(options_);

        sf->getOptions()->set("dimInput", lastDimInput);
        lastDimInput = 0;


        stacked->push_back(cellFactory->construct());
{'fraction_non_alphanumeric': 0.14346463742166518, 'fraction_numerical': 0.001566696508504924, 'mean_word_length': 4.25764705882353, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '884416', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1372, 'n_words': 288}