Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scorers.h « translator « src - github.com/marian-nmt/marian.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72ebff5dfff889071e6b8ebdb772f715180e67a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once

#include "marian.h"

#include "data/shortlist.h"
#include "models/model_factory.h"
#include "3rd_party/mio/mio.hpp"

namespace marian {

class ScorerState {
public:
  virtual ~ScorerState(){}

  virtual Logits getLogProbs() const = 0;

  virtual void blacklist(Expr /*totalCosts*/, Ptr<data::CorpusBatch> /*batch*/){};
};

class Scorer {
protected:
  std::string name_;
  float weight_;

public:
  Scorer(const std::string& name, float weight)
      : name_(name), weight_(weight) {}

  virtual ~Scorer(){}

  std::string getName() { return name_; }
  float getWeight() { return weight_; }

  virtual void clear(Ptr<ExpressionGraph>) = 0;
  virtual Ptr<ScorerState> startState(Ptr<ExpressionGraph>,
                                      Ptr<data::CorpusBatch>)
      = 0;
  virtual Ptr<ScorerState> step(Ptr<ExpressionGraph>,
                                Ptr<ScorerState>,
                                const std::vector<IndexType>&,
                                const Words&,
                                const std::vector<IndexType>& batchIndices,
                                int beamSize)
      = 0;

  virtual void init(Ptr<ExpressionGraph>) {}

  virtual void setShortlistGenerator(Ptr<const data::ShortlistGenerator> /*shortlistGenerator*/){};
  virtual Ptr<data::Shortlist> getShortlist() { return nullptr; };

  virtual std::vector<float> getAlignment() { return {}; };
};

class ScorerWrapperState : public ScorerState {
protected:
  Ptr<DecoderState> state_;

public:
  ScorerWrapperState(Ptr<DecoderState> state) : state_(state) {}
  virtual ~ScorerWrapperState() {}

  virtual Ptr<DecoderState> getState() { return state_; }

  virtual Logits getLogProbs() const override { return state_->getLogProbs(); };

  virtual void blacklist(Expr totalCosts, Ptr<data::CorpusBatch> batch) override {
    state_->blacklist(totalCosts, batch);
  }
};

// class to wrap IEncoderDecoder in a Scorer interface
class ScorerWrapper : public Scorer {
private:
  Ptr<IEncoderDecoder> encdec_;
  std::string fname_;
  std::vector<io::Item> items_;
  const void* ptr_;

public:
  ScorerWrapper(Ptr<models::IModel> encdec,
                const std::string& name,
                float weight,
                std::vector<io::Item>& items)
      : Scorer(name, weight),
        encdec_(std::static_pointer_cast<IEncoderDecoder>(encdec)),
        items_(items),
        ptr_{0} {}

  ScorerWrapper(Ptr<models::IModel> encdec,
                const std::string& name,
                float weight,
                const std::string& fname)
      : Scorer(name, weight),
        encdec_(std::static_pointer_cast<IEncoderDecoder>(encdec)),
        fname_(fname),
        ptr_{0} {}

  ScorerWrapper(Ptr<models::IModel> encdec,
                const std::string& name,
                float weight,
                const void* ptr)
      : Scorer(name, weight),
        encdec_(std::static_pointer_cast<IEncoderDecoder>(encdec)),
        ptr_{ptr} {}

  virtual ~ScorerWrapper() {}

  virtual void init(Ptr<ExpressionGraph> graph) override {
    graph->switchParams(getName());
    if(!items_.empty())
      encdec_->load(graph, items_);
    else if(ptr_)
      encdec_->mmap(graph, ptr_);
    else
      encdec_->load(graph, fname_);
  }

  virtual void clear(Ptr<ExpressionGraph> graph) override {
    graph->switchParams(getName());
    encdec_->clear(graph);
  }

  virtual Ptr<ScorerState> startState(Ptr<ExpressionGraph> graph,
                                      Ptr<data::CorpusBatch> batch) override {
    graph->switchParams(getName());
    return New<ScorerWrapperState>(encdec_->startState(graph, batch));
  }

  virtual Ptr<ScorerState> step(Ptr<ExpressionGraph> graph,
                                Ptr<ScorerState> state,
                                const std::vector<IndexType>& hypIndices,
                                const Words& words,
                                const std::vector<IndexType>& batchIndices,
                                int beamSize) override {
    graph->switchParams(getName());
    auto wrapperState = std::dynamic_pointer_cast<ScorerWrapperState>(state);
    auto newState = encdec_->step(graph, wrapperState->getState(), hypIndices, words, batchIndices, beamSize);
    return New<ScorerWrapperState>(newState);
  }

  virtual void setShortlistGenerator(
      Ptr<const data::ShortlistGenerator> shortlistGenerator) override {
    encdec_->setShortlistGenerator(shortlistGenerator);
  };

  virtual Ptr<data::Shortlist> getShortlist() override {
    return encdec_->getShortlist();
  };

  virtual std::vector<float> getAlignment() override {
    // This is called during decoding, where alignments only exist for the last time step. Hence front().
    // This makes as copy. @TODO: It should be OK to return this as a const&.
    return encdec_->getAlignment().front(); // [beam depth * max src length * batch size]
  }
};

Ptr<Scorer> scorerByType(const std::string& fname,
                        float weight,
                        std::vector<io::Item> items,
                        Ptr<Options> options);

Ptr<Scorer> scorerByType(const std::string& fname,
                         float weight,
                         const std::string& model,
                         Ptr<Options> config);


std::vector<Ptr<Scorer>> createScorers(Ptr<Options> options);
std::vector<Ptr<Scorer>> createScorers(Ptr<Options> options, const std::vector<std::vector<io::Item>> models);

Ptr<Scorer> scorerByType(const std::string& fname,
                         float weight,
                         const void* ptr,
                         Ptr<Options> config);

std::vector<Ptr<Scorer>> createScorers(Ptr<Options> options, const std::vector<const void*>& ptrs);
std::vector<Ptr<Scorer>> createScorers(Ptr<Options> options, const std::vector<mio::mmap_source>& mmaps);

}  // namespace marian