Repository navigation
Expand file tree
/
Copy pathlookup_modules.cpp
More file actions
193 lines (159 loc) · 5.91 KB
/
Copy pathlookup_modules.cpp
File metadata and controls
193 lines (159 loc) · 5.91 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "engine/framework/modules/lookup_modules.h"
#include <stdexcept>
namespace engine::modules {
namespace {
const core::ModulePortSpec kEmbeddingInputs[] = {
{"indices", core::PortKind::Activation, false},
{"weight", core::PortKind::Parameter, false},
};
const core::ModulePortSpec kEmbeddingOutputs[] = {
{"output", core::PortKind::Activation, false},
};
const core::ModuleSchema kEmbeddingSchema = {
"Embedding",
"nn.lookup",
kEmbeddingInputs,
2,
kEmbeddingOutputs,
1,
"Looks up token or frame ids in an embedding table.",
};
const core::ModuleSchema kPitchEmbedSchema = {
"PitchEmbed",
"nn.lookup",
kEmbeddingInputs,
2,
kEmbeddingOutputs,
1,
"Looks up per-frame pitch bins in an embedding table.",
};
const core::ModuleSchema kEnergyEmbedSchema = {
"EnergyEmbed",
"nn.lookup",
kEmbeddingInputs,
2,
kEmbeddingOutputs,
1,
"Looks up per-frame energy bins in an embedding table.",
};
const core::ModuleSchema kCodebookLookupSchema = {
"CodebookLookup",
"nn.lookup",
kEmbeddingInputs,
2,
kEmbeddingOutputs,
1,
"Looks up vector-quantizer codebook entries from discrete indices.",
};
core::TensorShape embedding_output_shape(const core::TensorShape & index_shape, int64_t embedding_dim) {
if (index_shape.rank == 0 || index_shape.rank >= core::kMaxTensorRank) {
throw std::runtime_error("Embedding indices rank must be between 1 and 3");
}
core::TensorShape output = {};
output.rank = index_shape.rank + 1;
for (size_t i = 0; i < index_shape.rank; ++i) {
output.dims[i] = index_shape.dims[i];
}
output.dims[output.rank - 1] = embedding_dim;
return output;
}
} // namespace
EmbeddingModule::EmbeddingModule(EmbeddingConfig config) : config_(config) {
if (config_.num_embeddings <= 0 || config_.embedding_dim <= 0) {
throw std::runtime_error("EmbeddingConfig dimensions must be positive");
}
}
const EmbeddingConfig & EmbeddingModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & EmbeddingModule::schema() const noexcept {
return static_schema();
}
core::TensorValue EmbeddingModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & indices,
const core::TensorValue & weight) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
if (indices.type != GGML_TYPE_I32) {
throw std::runtime_error("Embedding indices must be GGML_TYPE_I32");
}
core::validate_rank_between(indices, 1, core::kMaxTensorRank - 1, "indices");
core::validate_shape(
weight,
core::TensorShape::from_dims({config_.num_embeddings, config_.embedding_dim}),
"weight");
const auto output_shape = embedding_output_shape(indices.shape, config_.embedding_dim);
const auto flat_indices_shape = core::TensorShape::from_dims({indices.shape.num_elements()});
const auto flat_indices = core::reshape_tensor(ctx, indices, flat_indices_shape);
const auto flat_output = core::wrap_tensor(
ggml_get_rows(ctx.ggml, weight.tensor, flat_indices.tensor),
core::TensorShape::from_dims({indices.shape.num_elements(), config_.embedding_dim}),
GGML_TYPE_F32);
return core::reshape_tensor(ctx, flat_output, output_shape);
}
const core::ModuleSchema & EmbeddingModule::static_schema() noexcept {
return kEmbeddingSchema;
}
PitchEmbedModule::PitchEmbedModule(IndexedEmbeddingConfig config) : config_(config) {
if (config_.num_embeddings <= 0 || config_.embedding_dim <= 0) {
throw std::runtime_error("IndexedEmbeddingConfig dimensions must be positive");
}
}
const IndexedEmbeddingConfig & PitchEmbedModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & PitchEmbedModule::schema() const noexcept {
return static_schema();
}
core::TensorValue PitchEmbedModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & indices,
const core::TensorValue & weight) const {
return EmbeddingModule({config_.num_embeddings, config_.embedding_dim}).build(ctx, indices, weight);
}
const core::ModuleSchema & PitchEmbedModule::static_schema() noexcept {
return kPitchEmbedSchema;
}
EnergyEmbedModule::EnergyEmbedModule(IndexedEmbeddingConfig config) : config_(config) {
if (config_.num_embeddings <= 0 || config_.embedding_dim <= 0) {
throw std::runtime_error("IndexedEmbeddingConfig dimensions must be positive");
}
}
const IndexedEmbeddingConfig & EnergyEmbedModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & EnergyEmbedModule::schema() const noexcept {
return static_schema();
}
core::TensorValue EnergyEmbedModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & indices,
const core::TensorValue & weight) const {
return EmbeddingModule({config_.num_embeddings, config_.embedding_dim}).build(ctx, indices, weight);
}
const core::ModuleSchema & EnergyEmbedModule::static_schema() noexcept {
return kEnergyEmbedSchema;
}
CodebookLookupModule::CodebookLookupModule(IndexedEmbeddingConfig config) : config_(config) {
if (config_.num_embeddings <= 0 || config_.embedding_dim <= 0) {
throw std::runtime_error("IndexedEmbeddingConfig dimensions must be positive");
}
}
const IndexedEmbeddingConfig & CodebookLookupModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & CodebookLookupModule::schema() const noexcept {
return static_schema();
}
core::TensorValue CodebookLookupModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & indices,
const core::TensorValue & weight) const {
return EmbeddingModule({config_.num_embeddings, config_.embedding_dim}).build(ctx, indices, weight);
}
const core::ModuleSchema & CodebookLookupModule::static_schema() noexcept {
return kCodebookLookupSchema;
}
} // namespace engine::modules