| 246 | const core::TensorValue & input_values, |
| 247 | const HubertEncoderWeights & weights) { |
| 248 | const auto & config = weights.config; |
| 249 | auto hidden = core::reshape_tensor( |
| 250 | ctx, |
| 251 | input_values, |
| 252 | core::TensorShape::from_dims({input_values.shape.dims[0], 1, input_values.shape.dims[1]})); |
| 253 | int64_t in_channels = 1; |
| 254 | int64_t frames = input_values.shape.dims[1]; |
| 255 | for (size_t index = 0; index < config.conv_dim.size(); ++index) { |
| 256 | const std::string prefix = "feature_extractor.conv_layers." + std::to_string(index); |
| 257 | hidden = Conv1dModule({ |
| 258 | in_channels, |
| 259 | config.conv_dim[index], |
| 260 | config.conv_kernel[index], |
| 261 | static_cast<int>(config.conv_stride[index]), |
| 262 | 0, |
| 263 | 1, |
| 264 | true}).build(ctx, hidden, conv1d_weights(weights, prefix + ".conv")); |
| 265 | hidden = transpose_bct_btc(ctx, hidden); |
| 266 | hidden = LayerNormModule({config.conv_dim[index], config.layer_norm_eps, true, true}) |
| 267 | .build(ctx, hidden, norm_weights(weights, prefix + ".layer_norm")); |
| 268 | hidden = transpose_bct_btc(ctx, hidden); |
| 269 | hidden = GeluModule({GeluApproximation::ExactErf}).build(ctx, hidden); |
| 270 | frames = conv1d_output_frames(frames, config.conv_kernel[index], config.conv_stride[index], 0); |
| 271 | in_channels = config.conv_dim[index]; |
| 272 | } |
| 273 | hidden = transpose_bct_btc(ctx, hidden); |
| 274 | hidden = LayerNormModule({config.conv_dim.back(), config.layer_norm_eps, true, true}) |
| 275 | .build(ctx, hidden, norm_weights(weights, "feature_projection.layer_norm")); |
| 276 | hidden = LinearModule({config.conv_dim.back(), config.hidden_size, true, GGML_PREC_F32}) |
| 277 | .build(ctx, hidden, linear_weights(weights, "feature_projection.projection")); |
| 278 | |
| 279 | if (config.apply_positional_embedding) { |
| 280 | auto pos = grouped_pos_conv( |
| 281 | ctx, |
| 282 | transpose_bct_btc(ctx, hidden), |
| 283 | conv1d_weights(weights, "encoder.pos_conv_embed.conv"), |
| 284 | config); |
| 285 | pos = transpose_bct_btc(ctx, pos); |
| 286 | hidden = add_same(ctx, hidden, pos); |
| 287 | } |
| 288 | |
| 289 | for (int64_t layer = 0; layer < config.output_hidden_layer; ++layer) { |
| 290 | const std::string prefix = "encoder.layers." + std::to_string(layer); |
| 291 | const auto attn_residual = hidden; |
| 292 | hidden = LayerNormModule({config.hidden_size, config.layer_norm_eps, true, true}) |
| 293 | .build(ctx, hidden, norm_weights(weights, prefix + ".layer_norm")); |
| 294 | hidden = build_self_attention(ctx, hidden, weights, layer); |
| 295 | hidden = add_same(ctx, attn_residual, hidden); |
| 296 | const auto ff_in = LayerNormModule({config.hidden_size, config.layer_norm_eps, true, true}) |
| 297 | .build(ctx, hidden, norm_weights(weights, prefix + ".final_layer_norm")); |
| 298 | hidden = add_same(ctx, hidden, build_feed_forward(ctx, ff_in, weights, layer)); |
| 299 | } |
| 300 | if (config.apply_final_layer_norm) { |
| 301 | hidden = LayerNormModule({config.hidden_size, config.layer_norm_eps, true, true}) |
| 302 | .build(ctx, hidden, norm_weights(weights, "encoder.layer_norm")); |
| 303 | } |
| 304 | return hidden; |
| 305 | } |
no test coverage detected