Skip to content

Add transpose ops make convolutions channels-last. #6459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions backends/cadence/aot/ops_registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def quantized_conv_meta(
out_shift: torch.Tensor,
channel_last: bool = False,
) -> torch.Tensor:
out_channels, _in_channels, *kernel_size = weight.shape
if channel_last:
out_channels, *kernel_size, _ = weight.shape
else:
out_channels, _, *kernel_size = weight.shape

in_size = input.shape
# Assert that the input tensor has at least 3 dimensions, and at most 6
assert len(in_size) > 2
Expand All @@ -141,7 +145,13 @@ def quantized_conv_meta(
# Compute the output tensor size
output_size = (
get_conv1d_output_size(
in_size, out_channels, stride[1], padding[1], dilation[1], kernel_size[0]
in_size,
out_channels,
stride[1],
padding[1],
dilation[1],
kernel_size[0],
channel_last,
)
if len(in_size) == 3
else get_conv2d_output_size(
Expand Down
13 changes: 10 additions & 3 deletions backends/cadence/aot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ def get_conv1d_output_size(
padding: int,
dilation: int,
kernel_size: int,
channel_last: bool,
) -> torch.Size:
assert len(in_size) == 3
N, C, L = in_size
if channel_last:
N, L, C = in_size
else:
N, C, L = in_size

# Reference: https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
lout = (L + 2 * padding - dilation * (kernel_size - 1) - 1) // stride + 1

return torch.Size((in_size[0], out_channels, lout))
if channel_last:
return torch.Size((N, lout, out_channels))
return torch.Size((N, out_channels, lout))


# Get the output size of a 2D convolution given the input size and parameters
Expand All @@ -76,7 +82,8 @@ def get_conv2d_output_size(
wout = (W + 2 * padding[1] - dilation[1] * (kernel_size[1] - 1) - 1) // stride[
1
] + 1

if channel_last:
return torch.Size((N, hout, wout, out_channels))
return torch.Size((in_size[0], out_channels, hout, wout))


Expand Down
Loading