-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathdecoder.h
More file actions
118 lines (89 loc) · 3.87 KB
/
decoder.h
File metadata and controls
118 lines (89 loc) · 3.87 KB
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
#pragma once
#include <map>
#include <mutex>
#include <string>
#include <unordered_map>
#include "ctranslate2/layers/common.h"
#include "ctranslate2/storage_view.h"
namespace ctranslate2 {
namespace layers {
using DecoderState = std::unordered_map<std::string, StorageView>;
void zero_first_timestep(StorageView& x, dim_t step);
// Base class for decoders.
class Decoder : public Layer {
public:
Decoder(Device device);
// Configure which attention heads to collect when return_attention is enabled.
virtual void set_alignment_heads(const std::vector<std::pair<dim_t, dim_t>>& alignment_heads) {
(void)alignment_heads;
}
virtual DecoderState initial_state(bool iterative_decoding = true) const = 0;
// Forwards one step.
virtual void operator()(dim_t step,
const StorageView& ids,
DecoderState& state,
StorageView* logits = nullptr,
StorageView* attention = nullptr) = 0;
// Forwards a full sequence.
virtual void operator()(const StorageView& ids,
const StorageView& lengths,
DecoderState& state,
StorageView& logits,
StorageView* attention = nullptr) = 0;
// Update the decoder state in greedy search.
void update_state(DecoderState& state, const StorageView& alive_batches) const;
// Update the decoder state in beam search.
void update_state(DecoderState& state,
StorageView beam_indices,
const dim_t beam_size,
const StorageView* alive_batches = nullptr) const;
// Replicate the decoder state beam_size times.
void replicate_state(DecoderState& state, const dim_t beam_size) const;
// Returns true if the state must be replicated beam_size times.
virtual bool replicate_state(const std::string& name) const;
// Restrict the output layer to a set of ids and/or resize it to a preferred size multiple.
// Elements in restrict_ids must be unique and sorted.
void update_output_layer(const dim_t size_multiple = 1,
const std::vector<size_t>& restrict_ids = {});
bool output_layer_is_updated() const {
return !_to_original_word_id.empty();
}
bool is_in_output(size_t word_id) const {
return _to_output_word_id.find(word_id) != _to_output_word_id.end();
}
size_t to_output_word_id(size_t original_id) const {
return _to_output_word_id.empty() ? original_id : _to_output_word_id.at(original_id);
}
size_t to_original_word_id(size_t output_id) const {
return _to_original_word_id.empty() ? output_id : _to_original_word_id.at(output_id);
}
Device device() const {
return _device;
}
DataType output_type() const override {
return const_cast<Decoder&>(*this).output_layer().output_type();
}
dim_t output_size() const override {
return const_cast<Decoder&>(*this).output_layer().output_size();
}
protected:
// Returns the current batch size from the decoder state.
virtual dim_t batch_size(const DecoderState& state) const;
// Returns the output linear layer.
virtual Dense& output_layer() = 0;
const Device _device;
private:
std::vector<size_t> _to_original_word_id;
std::unordered_map<size_t, size_t> _to_output_word_id;
dim_t _vocabulary_size = 0;
};
class DecoderStateCache {
public:
void save(std::vector<size_t> prompt, DecoderState state);
const DecoderState* get(const std::vector<size_t>& prompt) const;
private:
std::map<std::vector<size_t>, DecoderState> _cache;
mutable std::mutex _mutex;
};
}
}