Repository navigation
Expand file tree
/
Copy pathfeed_forward_modules.cpp
More file actions
254 lines (221 loc) · 8.53 KB
/
Copy pathfeed_forward_modules.cpp
File metadata and controls
254 lines (221 loc) · 8.53 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "attention/attention_internal.h"
#include "engine/framework/modules/streaming_conv_modules.h"
namespace engine::modules {
using namespace attention::internal;
FeedForwardModule::FeedForwardModule(FeedForwardConfig config) : config_(config) {
validate_hidden_positive(config_.hidden_size, "FeedForwardConfig.hidden_size");
validate_hidden_positive(config_.intermediate_size, "FeedForwardConfig.intermediate_size");
}
const FeedForwardConfig & FeedForwardModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & FeedForwardModule::schema() const noexcept {
return static_schema();
}
core::TensorValue FeedForwardModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const FeedForwardWeights & weights) const {
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
core::validate_last_dim(input, config_.hidden_size, "input");
return build_feed_forward_impl(ctx, input, config_, require_feed_forward_weights(weights, config_.use_bias));
}
const core::ModuleSchema & FeedForwardModule::static_schema() noexcept {
return kFeedForwardSchema;
}
FeedForwardGeluModule::FeedForwardGeluModule(FeedForwardConfig config) : config_(config) {
config_.gelu_approximation = GeluApproximation::Tanh;
validate_hidden_positive(config_.hidden_size, "FeedForwardGeluConfig.hidden_size");
validate_hidden_positive(config_.intermediate_size, "FeedForwardGeluConfig.intermediate_size");
}
const FeedForwardConfig & FeedForwardGeluModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & FeedForwardGeluModule::schema() const noexcept {
return static_schema();
}
core::TensorValue FeedForwardGeluModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const FeedForwardWeights & weights) const {
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
core::validate_last_dim(input, config_.hidden_size, "input");
return build_feed_forward_impl(ctx, input, config_, require_feed_forward_weights(weights, config_.use_bias));
}
const core::ModuleSchema & FeedForwardGeluModule::static_schema() noexcept {
return kFeedForwardGeluSchema;
}
GatedFeedForwardModule::GatedFeedForwardModule(GatedFeedForwardConfig config) : config_(config) {
validate_hidden_positive(config_.hidden_size, "GatedFeedForwardConfig.hidden_size");
validate_hidden_positive(config_.intermediate_size, "GatedFeedForwardConfig.intermediate_size");
}
const GatedFeedForwardConfig & GatedFeedForwardModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & GatedFeedForwardModule::schema() const noexcept {
return static_schema();
}
core::TensorValue GatedFeedForwardModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const GatedFeedForwardWeights & weights) const {
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
core::validate_last_dim(input, config_.hidden_size, "input");
return build_gated_feed_forward_impl(ctx, input, config_, require_gated_feed_forward_weights(weights, config_.use_bias));
}
const core::ModuleSchema & GatedFeedForwardModule::static_schema() noexcept {
return kGatedFeedForwardSchema;
}
namespace {
core::TensorValue build_conv_feed_forward_conv(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const Conv1dWeights & weights,
int64_t in_channels,
int64_t out_channels,
int64_t kernel_size,
bool causal,
bool explicit_symmetric_padding,
bool use_bias) {
if (causal) {
return StreamingConv1dModule({
in_channels,
out_channels,
kernel_size,
1,
1,
use_bias,
StreamingPadMode::Constant,
StreamingConv1dPaddingMode::StrictCausal,
}).build(ctx, input, weights);
}
if (explicit_symmetric_padding) {
const int64_t pad = kernel_size / 2;
return StreamingConv1dModule({
in_channels,
out_channels,
kernel_size,
1,
1,
use_bias,
StreamingPadMode::Constant,
StreamingConv1dPaddingMode::Explicit,
pad,
pad,
}).build(ctx, input, weights);
}
return Conv1dModule({in_channels, out_channels, kernel_size, 1, static_cast<int>(kernel_size / 2), 1, use_bias})
.build(ctx, input, weights);
}
} // namespace
ConvFeedForwardModule::ConvFeedForwardModule(ConvFeedForwardConfig config) : config_(config) {
validate_hidden_positive(config_.hidden_size, "ConvFeedForwardConfig.hidden_size");
validate_hidden_positive(config_.intermediate_size, "ConvFeedForwardConfig.intermediate_size");
if (config_.kernel_size <= 0) {
throw std::runtime_error("ConvFeedForwardConfig.kernel_size must be positive");
}
}
const ConvFeedForwardConfig & ConvFeedForwardModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & ConvFeedForwardModule::schema() const noexcept {
return static_schema();
}
core::TensorValue ConvFeedForwardModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const ConvFeedForwardWeights & weights) const {
core::validate_rank_between(input, 3, 3, "input");
core::validate_last_dim(input, config_.hidden_size, "input");
auto x = TransposeModule({{0, 2, 1, 3}, 3}).build(ctx, input);
x = build_conv_feed_forward_conv(
ctx,
x,
weights.proj,
config_.hidden_size,
config_.intermediate_size,
config_.kernel_size,
config_.causal,
false,
config_.use_bias);
x = GeluModule({config_.gelu_approximation}).build(ctx, x);
x = build_conv_feed_forward_conv(
ctx,
x,
weights.out,
config_.intermediate_size,
config_.hidden_size,
config_.kernel_size,
config_.causal,
false,
config_.use_bias);
return TransposeModule({{0, 2, 1, 3}, 3}).build(ctx, x);
}
const core::ModuleSchema & ConvFeedForwardModule::static_schema() noexcept {
return kConvFeedForwardSchema;
}
GatedConvFeedForwardModule::GatedConvFeedForwardModule(GatedConvFeedForwardConfig config) : config_(config) {
validate_hidden_positive(config_.hidden_size, "GatedConvFeedForwardConfig.hidden_size");
validate_hidden_positive(config_.intermediate_size, "GatedConvFeedForwardConfig.intermediate_size");
if (config_.kernel_size <= 0) {
throw std::runtime_error("GatedConvFeedForwardConfig.kernel_size must be positive");
}
}
const GatedConvFeedForwardConfig & GatedConvFeedForwardModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & GatedConvFeedForwardModule::schema() const noexcept {
return static_schema();
}
core::TensorValue GatedConvFeedForwardModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const GatedConvFeedForwardWeights & weights) const {
core::validate_rank_between(input, 3, 3, "input");
core::validate_last_dim(input, config_.hidden_size, "input");
auto x = TransposeModule({{0, 2, 1, 3}, 3}).build(ctx, input);
auto gate = build_conv_feed_forward_conv(
ctx,
x,
weights.gate_proj,
config_.hidden_size,
config_.intermediate_size,
config_.kernel_size,
config_.causal,
config_.explicit_symmetric_padding,
config_.use_bias);
switch (config_.activation) {
case GatedFeedForwardActivation::Gelu:
gate = GeluModule({GeluApproximation::Tanh}).build(ctx, gate);
break;
case GatedFeedForwardActivation::Silu:
gate = SiluModule{}.build(ctx, gate);
break;
}
auto up = build_conv_feed_forward_conv(
ctx,
x,
weights.up_proj,
config_.hidden_size,
config_.intermediate_size,
config_.kernel_size,
config_.causal,
config_.explicit_symmetric_padding,
config_.use_bias);
auto hidden = MulModule{}.build(ctx, gate, up);
hidden = build_conv_feed_forward_conv(
ctx,
hidden,
weights.down_proj,
config_.intermediate_size,
config_.hidden_size,
config_.kernel_size,
config_.causal,
config_.explicit_symmetric_padding,
config_.use_bias);
return TransposeModule({{0, 2, 1, 3}, 3}).build(ctx, hidden);
}
const core::ModuleSchema & GatedConvFeedForwardModule::static_schema() noexcept {
return kGatedConvFeedForwardSchema;
}
} // namespace engine::modules