MCPcopy Create free account
hub / github.com/ASLP-lab/OSUM / forward

Method forward

OSUM-EChat/wenet/transformer/decoder_layer.py:68–153  ·  view source on GitHub ↗

Compute decoded features. Args: tgt (torch.Tensor): Input tensor (#batch, maxlen_out, size). tgt_mask (torch.Tensor): Mask for input tensor (#batch, maxlen_out). memory (torch.Tensor): Encoded memory (#batch, maxlen_in, siz

(
        self,
        tgt: torch.Tensor,
        tgt_mask: torch.Tensor,
        memory: torch.Tensor,
        memory_mask: torch.Tensor,
        cache: Optional[Dict[str, Optional[T_CACHE]]] = None
    )

Source from the content-addressed store, hash-verified

66 self.normalize_before = normalize_before
67
68 def forward(
69 self,
70 tgt: torch.Tensor,
71 tgt_mask: torch.Tensor,
72 memory: torch.Tensor,
73 memory_mask: torch.Tensor,
74 cache: Optional[Dict[str, Optional[T_CACHE]]] = None
75 ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
76 """Compute decoded features.
77
78 Args:
79 tgt (torch.Tensor): Input tensor (#batch, maxlen_out, size).
80 tgt_mask (torch.Tensor): Mask for input tensor
81 (#batch, maxlen_out).
82 memory (torch.Tensor): Encoded memory
83 (#batch, maxlen_in, size).
84 memory_mask (torch.Tensor): Encoded memory mask
85 (#batch, maxlen_in).
86 cache (torch.Tensor): cached tensors.
87 (#batch, maxlen_out - 1, size).
88
89 Returns:
90 torch.Tensor: Output tensor (#batch, maxlen_out, size).
91 torch.Tensor: Mask for output tensor (#batch, maxlen_out).
92 torch.Tensor: Encoded memory (#batch, maxlen_in, size).
93 torch.Tensor: Encoded memory mask (#batch, maxlen_in).
94
95 """
96 if cache is not None:
97 att_cache = cache['self_att_cache']
98 cross_att_cache = cache['cross_att_cache']
99 else:
100 att_cache, cross_att_cache = None, None
101
102 residual = tgt
103 if self.normalize_before:
104 tgt = self.norm1(tgt)
105
106 if att_cache is None:
107 tgt_q = tgt
108 tgt_q_mask = tgt_mask
109 att_cache = (torch.empty(0, 0, 0, 0), torch.empty(0, 0, 0, 0))
110 else:
111 tgt_q = tgt[:, -1:, :]
112 residual = residual[:, -1:, :]
113 tgt_q_mask = tgt_mask[:, -1:, :]
114
115 x, new_att_cache = self.self_attn(
116 tgt_q,
117 tgt_q,
118 tgt_q,
119 tgt_q_mask,
120 cache=att_cache,
121 )
122 if cache is not None:
123 cache['self_att_cache'] = new_att_cache
124 x = residual + self.dropout(x)
125 if not self.normalize_before:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected