Can Conv1D layers be compiled?

Q. Can Conv1D layers be compiled?

The Supported Operations list only includes the Convolution IR, so we often get asked whether Conv1D can be used as-is.

A. Yes, Conv1D compiles without any modification.

When qb Compiler parses Conv1D into the Convolution IR, it automatically converts it to Conv2D.
The input (N, C, L) is treated as (N, C, 1, L), and the kernel K as (1, K).
A Conv1D with kernel_size=1 is handled as a 1x1 Conv2D.

If the automatic conversion doesn’t work

You can replace the Conv1D with a Conv2D yourself before compiling. The weights carry over unchanged, so the outputs are identical.

conv2d = nn.Conv2d(c_in, c_out, kernel_size=(1, k), stride=(1, s), padding=(0, p))
conv2d.weight.data = conv1d.weight.data.unsqueeze(2)  # (C_out, C_in, K) -> (C_out, C_in, 1, K)
conv2d.bias.data = conv1d.bias.data

y = conv2d(x.unsqueeze(2)).squeeze(2)  # (N, C, L) -> (N, C, 1, L) -> (N, C_out, L_out)

If you still run into issues, please create a new post with your product/version information and reproduction code.

1 Like