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

Method forward

OSUM/wenet/squeezeformer/convolution.py:121–177  ·  view source on GitHub ↗

Compute convolution module. Args: x (torch.Tensor): Input tensor (#batch, time, channels). mask_pad (torch.Tensor): used for batch padding (#batch, 1, time), (0, 0, 0) means fake mask. cache (torch.Tensor): left context cache, it is only

(
        self,
        x: torch.Tensor,
        mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
        cache: torch.Tensor = torch.zeros((0, 0, 0)),
    )

Source from the content-addressed store, hash-verified

119 pw_max)
120
121 def forward(
122 self,
123 x: torch.Tensor,
124 mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
125 cache: torch.Tensor = torch.zeros((0, 0, 0)),
126 ) -> Tuple[torch.Tensor, torch.Tensor]:
127 """Compute convolution module.
128 Args:
129 x (torch.Tensor): Input tensor (#batch, time, channels).
130 mask_pad (torch.Tensor): used for batch padding (#batch, 1, time),
131 (0, 0, 0) means fake mask.
132 cache (torch.Tensor): left context cache, it is only
133 used in causal convolution (#batch, channels, cache_t),
134 (0, 0, 0) meas fake cache.
135 Returns:
136 torch.Tensor: Output tensor (#batch, time, channels).
137 """
138 if self.adaptive_scale:
139 x = self.ada_scale * x + self.ada_bias
140 # exchange the temporal dimension and the feature dimension
141 x = x.transpose(1, 2) # (#batch, channels, time)
142 # mask batch padding
143 if mask_pad.size(2) > 0: # time > 0
144 x.masked_fill_(~mask_pad, 0.0)
145
146 if self.lorder > 0:
147 if cache.size(2) == 0: # cache_t == 0
148 x = nn.functional.pad(x, (self.lorder, 0), 'constant', 0.0)
149 else:
150 assert cache.size(0) == x.size(0) # equal batch
151 assert cache.size(1) == x.size(1) # equal channel
152 x = torch.cat((cache, x), dim=2)
153 assert (x.size(2) > self.lorder)
154 new_cache = x[:, :, -self.lorder:]
155 else:
156 # It's better we just return None if no cache is required,
157 # However, for JIT export, here we just fake one tensor instead of
158 # None.
159 new_cache = torch.zeros((0, 0, 0), dtype=x.dtype, device=x.device)
160
161 # GLU mechanism
162 x = self.pointwise_conv1(x) # (batch, 2*channel, dim)
163 x = nn.functional.glu(x, dim=1) # (batch, channel, dim)
164
165 # 1D Depthwise Conv
166 x = self.depthwise_conv(x)
167 if self.use_layer_norm:
168 x = x.transpose(1, 2)
169 x = self.activation(self.norm(x))
170 if self.use_layer_norm:
171 x = x.transpose(1, 2)
172 x = self.pointwise_conv2(x)
173 # mask batch padding
174 if mask_pad.size(2) > 0: # time > 0
175 x.masked_fill_(~mask_pad, 0.0)
176
177 return x.transpose(1, 2), new_cache

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected