Manual depthwise conv1d using broadcast_mul + sum pattern.
(&self, x: &Tensor)
| 69 | |
| 70 | /// Manual depthwise conv1d using broadcast_mul + sum pattern. |
| 71 | fn depthwise_conv1d(&self, x: &Tensor) -> Result<Tensor> { |
| 72 | let (_batch, channels, seq_len) = x.dims3()?; |
| 73 | let pad = self.kernel_size / 2; |
| 74 | |
| 75 | let x = if pad > 0 { |
| 76 | x.pad_with_zeros(2, pad, pad)? |
| 77 | } else { |
| 78 | x.clone() |
| 79 | }; |
| 80 | |
| 81 | let w = self.depthwise_weight.squeeze(1)?; // [channels, kernel_size] |
| 82 | |
| 83 | let mut outputs = Vec::with_capacity(seq_len); |
| 84 | for i in 0..seq_len { |
| 85 | let slice = x.narrow(2, i, self.kernel_size)?; |
| 86 | let prod = slice.broadcast_mul(&w)?; |
| 87 | let summed = prod.sum(candle_core::D::Minus1)?; |
| 88 | outputs.push(summed); |
| 89 | } |
| 90 | let result = Tensor::stack(&outputs, 2)?; |
| 91 | let bias = self.depthwise_bias.reshape((1, channels, 1))?; |
| 92 | Ok(result.broadcast_add(&bias)?) |
| 93 | } |
| 94 | } |