| 22 | } |
| 23 | |
| 24 | core::TensorValue TransformerEncoderBlockModule::build( |
| 25 | core::ModuleBuildContext & ctx, |
| 26 | const core::TensorValue & input, |
| 27 | const TransformerEncoderBlockWeights & weights) const { |
| 28 | validate_sequence_input(input, config_.hidden_size, "input"); |
| 29 | |
| 30 | const LayerNormModule norm1(make_norm_config(config_.hidden_size, config_.eps)); |
| 31 | const SelfAttentionModule self_attention({config_.hidden_size, config_.num_heads, config_.use_bias}); |
| 32 | const LayerNormModule norm2(make_norm_config(config_.hidden_size, config_.eps)); |
| 33 | const FeedForwardModule feed_forward( |
| 34 | {config_.hidden_size, config_.intermediate_size, config_.use_bias, GeluApproximation::ExactErf}); |
| 35 | const ResidualAddModule add; |
| 36 | |
| 37 | auto cur = norm1.build(ctx, input, weights.norm1); |
| 38 | cur = self_attention.build(ctx, cur, weights.self_attention); |
| 39 | if (weights.layer_scale1.has_value()) { |
| 40 | cur = LayerScaleModule().build(ctx, cur, *weights.layer_scale1); |
| 41 | } |
| 42 | cur = add.build(ctx, input, cur); |
| 43 | auto ff_in = norm2.build(ctx, cur, weights.norm2); |
| 44 | auto ff_out = feed_forward.build(ctx, ff_in, weights.feed_forward); |
| 45 | if (weights.layer_scale2.has_value()) { |
| 46 | ff_out = LayerScaleModule().build(ctx, ff_out, *weights.layer_scale2); |
| 47 | } |
| 48 | return add.build(ctx, cur, ff_out); |
| 49 | } |
| 50 | |
| 51 | const core::ModuleSchema & TransformerEncoderBlockModule::static_schema() noexcept { |
| 52 | return kTransformerEncoderBlockSchema; |
nothing calls this directly
no test coverage detected