MCPcopy Create free account
hub / github.com/pytorch/pytorch / forward

Method forward

torch/nn/modules/transformer.py:594–717  ·  view source on GitHub ↗

r"""Pass the input through the encoder layer. Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). is_causal: If spec

(
            self,
            src: Tensor,
            src_mask: Optional[Tensor] = None,
            src_key_padding_mask: Optional[Tensor] = None,
            is_causal: bool = False)

Source from the content-addressed store, hash-verified

592
593
594 def forward(
595 self,
596 src: Tensor,
597 src_mask: Optional[Tensor] = None,
598 src_key_padding_mask: Optional[Tensor] = None,
599 is_causal: bool = False) -> Tensor:
600 r"""Pass the input through the encoder layer.
601
602 Args:
603 src: the sequence to the encoder layer (required).
604 src_mask: the mask for the src sequence (optional).
605 src_key_padding_mask: the mask for the src keys per batch (optional).
606 is_causal: If specified, applies a causal mask as ``src mask``.
607 Default: ``False``.
608 Warning:
609 ``is_causal`` provides a hint that ``src_mask`` is the
610 causal mask. Providing incorrect hints can result in
611 incorrect execution, including forward and backward
612 compatibility.
613
614 Shape:
615 see the docs in Transformer class.
616 """
617 src_key_padding_mask = F._canonical_mask(
618 mask=src_key_padding_mask,
619 mask_name="src_key_padding_mask",
620 other_type=F._none_or_dtype(src_mask),
621 other_name="src_mask",
622 target_type=src.dtype
623 )
624
625 src_mask = F._canonical_mask(
626 mask=src_mask,
627 mask_name="src_mask",
628 other_type=None,
629 other_name="",
630 target_type=src.dtype,
631 check_other=False,
632 )
633
634 # see Fig. 1 of https://arxiv.org/pdf/2002.04745v1.pdf
635 why_not_sparsity_fast_path = ''
636 if not src.dim() == 3:
637 why_not_sparsity_fast_path = f"input not batched; expected src.dim() of 3 but got {src.dim()}"
638 elif self.training:
639 why_not_sparsity_fast_path = "training is enabled"
640 elif not self.self_attn.batch_first :
641 why_not_sparsity_fast_path = "self_attn.batch_first was not True"
642 elif not self.self_attn._qkv_same_embed_dim :
643 why_not_sparsity_fast_path = "self_attn._qkv_same_embed_dim was not True"
644 elif not self.activation_relu_or_gelu:
645 why_not_sparsity_fast_path = "activation_relu_or_gelu was not True"
646 elif not (self.norm1.eps == self.norm2.eps):
647 why_not_sparsity_fast_path = "norm1.eps is not equal to norm2.eps"
648 elif src.is_nested and (src_key_padding_mask is not None or src_mask is not None):
649 why_not_sparsity_fast_path = "neither src_key_padding_mask nor src_mask are not supported with NestedTensor input"
650 elif self.self_attn.num_heads % 2 == 1:
651 why_not_sparsity_fast_path = "num_head is odd"

Callers

nothing calls this directly

Calls 7

_sa_blockMethod · 0.95
_ff_blockMethod · 0.95
is_autocast_enabledMethod · 0.80
merge_masksMethod · 0.80
allFunction · 0.50
anyFunction · 0.50
dimMethod · 0.45

Tested by

no test coverage detected