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)),
)
| 93 | self.activation = activation |
| 94 | |
| 95 | def forward( |
| 96 | self, |
| 97 | x: torch.Tensor, |
| 98 | mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool), |
| 99 | cache: torch.Tensor = torch.zeros((0, 0, 0)), |
| 100 | ) -> Tuple[torch.Tensor, torch.Tensor]: |
| 101 | """Compute convolution module. |
| 102 | Args: |
| 103 | x (torch.Tensor): Input tensor (#batch, time, channels). |
| 104 | mask_pad (torch.Tensor): used for batch padding (#batch, 1, time), |
| 105 | (0, 0, 0) means fake mask. |
| 106 | cache (torch.Tensor): left context cache, it is only |
| 107 | used in causal convolution (#batch, channels, cache_t), |
| 108 | (0, 0, 0) meas fake cache. |
| 109 | Returns: |
| 110 | torch.Tensor: Output tensor (#batch, time, channels). |
| 111 | """ |
| 112 | # exchange the temporal dimension and the feature dimension |
| 113 | x = x.transpose(1, 2) # (#batch, channels, time) |
| 114 | |
| 115 | # mask batch padding |
| 116 | if mask_pad.size(2) > 0: # time > 0 |
| 117 | x.masked_fill_(~mask_pad, 0.0) |
| 118 | |
| 119 | if self.lorder > 0: |
| 120 | if cache.size(2) == 0: # cache_t == 0 |
| 121 | x = nn.functional.pad(x, (self.lorder, 0), 'constant', 0.0) |
| 122 | else: |
| 123 | assert cache.size(0) == x.size(0) # equal batch |
| 124 | assert cache.size(1) == x.size(1) # equal channel |
| 125 | x = torch.cat((cache, x), dim=2) |
| 126 | assert (x.size(2) > self.lorder) |
| 127 | new_cache = x[:, :, -self.lorder:] |
| 128 | else: |
| 129 | # It's better we just return None if no cache is required, |
| 130 | # However, for JIT export, here we just fake one tensor instead of |
| 131 | # None. |
| 132 | new_cache = torch.zeros((0, 0, 0), dtype=x.dtype, device=x.device) |
| 133 | |
| 134 | # GLU mechanism |
| 135 | x = self.pointwise_conv1(x) # (batch, 2*channel, dim) |
| 136 | x = nn.functional.glu(x, dim=1) # (batch, channel, dim) |
| 137 | |
| 138 | # 1D Depthwise Conv |
| 139 | x = self.depthwise_conv(x) |
| 140 | if self.use_layer_norm: |
| 141 | x = x.transpose(1, 2) |
| 142 | x = self.activation(self.norm(x)) |
| 143 | if self.use_layer_norm: |
| 144 | x = x.transpose(1, 2) |
| 145 | x = self.pointwise_conv2(x) |
| 146 | # mask batch padding |
| 147 | if mask_pad.size(2) > 0: # time > 0 |
| 148 | x.masked_fill_(~mask_pad, 0.0) |
| 149 | |
| 150 | return x.transpose(1, 2), new_cache |
nothing calls this directly
no outgoing calls
no test coverage detected