:github_url: https://github.com/pytorch/pytorch .. _program_listing_file_torch_csrc_api_include_torch_nn_modules_transformerlayer.h: Program Listing for File transformerlayer.h =========================================== |exhale_lsh| :ref:`Return to documentation for file ` (``torch/csrc/api/include/torch/nn/modules/transformerlayer.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include #include #include #include #include #include #include namespace torch::nn { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TransformerEncoderLayer // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class TORCH_API TransformerEncoderLayerImpl : public Cloneable { public: TransformerEncoderLayerImpl(int64_t d_model, int64_t nhead) : TransformerEncoderLayerImpl( TransformerEncoderLayerOptions(d_model, nhead)) {} explicit TransformerEncoderLayerImpl(TransformerEncoderLayerOptions options_); Tensor forward( const Tensor& src, const Tensor& src_mask = {}, const Tensor& src_key_padding_mask = {}); void reset() override; void reset_parameters(); protected: FORWARD_HAS_DEFAULT_ARGS({1, AnyValue(Tensor())}, {2, AnyValue(Tensor())}) public: TransformerEncoderLayerOptions options; MultiheadAttention self_attn = nullptr; Linear linear1 = nullptr; Dropout dropout = nullptr; Linear linear2 = nullptr; LayerNorm norm1 = nullptr; LayerNorm norm2 = nullptr; Dropout dropout1 = nullptr; Dropout dropout2 = nullptr; }; TORCH_MODULE(TransformerEncoderLayer); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TransformerDecoderLayer // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class TORCH_API TransformerDecoderLayerImpl : public Cloneable { public: TransformerDecoderLayerImpl(int64_t d_model, int64_t nhead) : TransformerDecoderLayerImpl( TransformerDecoderLayerOptions(d_model, nhead)) {} explicit TransformerDecoderLayerImpl(TransformerDecoderLayerOptions options_); void reset() override; void reset_parameters(); Tensor forward( Tensor tgt, const Tensor& memory, const Tensor& tgt_mask = {}, const Tensor& memory_mask = {}, const Tensor& tgt_key_padding_mask = {}, const Tensor& memory_key_padding_mask = {}); TransformerDecoderLayerOptions options; MultiheadAttention self_attn{nullptr}; Dropout dropout1{nullptr}; LayerNorm norm1{nullptr}; MultiheadAttention multihead_attn{nullptr}; Dropout dropout2{nullptr}; LayerNorm norm2{nullptr}; Linear linear1{nullptr}; Dropout dropout{nullptr}; Linear linear2{nullptr}; Dropout dropout3{nullptr}; LayerNorm norm3{nullptr}; protected: FORWARD_HAS_DEFAULT_ARGS( {2, AnyValue(Tensor())}, {3, AnyValue(Tensor())}, {4, AnyValue(Tensor())}, {5, AnyValue(Tensor())}) Tensor activation(const Tensor& input); }; TORCH_MODULE(TransformerDecoderLayer); } // namespace torch::nn