Skip to content
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
60 changes: 60 additions & 0 deletions backends/cadence/aot/ops_registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,11 @@ def quantized_conv_nchw_asym8sxsym8s_asym8s_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.int8
and weight.dtype == torch.int8
and bias.dtype == torch.int32
)
out_channels, _, *kernel_size = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -917,6 +922,11 @@ def quantized_conv_nchw_asym8uxsym8u_asym8u_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.uint8
and weight.dtype == torch.uint8
and bias.dtype == torch.int32
)
out_channels, _, *kernel_size = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -961,6 +971,11 @@ def quantized_conv_nhwc_asym8sxsym8s_asym8s_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.int8
and weight.dtype == torch.int8
and bias.dtype == torch.int32
)
out_channels, *kernel_size, _ = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1005,6 +1020,11 @@ def quantized_conv_nhwc_asym8uxsym8u_asym8u_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.uint8
and weight.dtype == torch.uint8
and bias.dtype == torch.int32
)
out_channels, *kernel_size, _ = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1049,6 +1069,11 @@ def quantized_conv_nchw_dilated_asym8sxsym8s_asym8s_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.int8
and weight.dtype == torch.int8
and bias.dtype == torch.int32
)
out_channels, _, *kernel_size = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1093,6 +1118,11 @@ def quantized_conv_nchw_dilated_asym8uxsym8u_asym8u_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.uint8
and weight.dtype == torch.uint8
and bias.dtype == torch.int32
)
out_channels, _, *kernel_size = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1137,6 +1167,11 @@ def quantized_conv_nhwc_dilated_asym8sxsym8s_asym8s_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.int8
and weight.dtype == torch.int8
and bias.dtype == torch.int32
)
out_channels, *kernel_size, _ = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1181,6 +1216,11 @@ def quantized_conv_nhwc_dilated_asym8uxsym8u_asym8u_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.uint8
and weight.dtype == torch.uint8
and bias.dtype == torch.int32
)
out_channels, *kernel_size, _ = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1225,6 +1265,11 @@ def quantized_conv_nchw_depthwise_asym8sxsym8s_asym8s_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.int8
and weight.dtype == torch.int8
and bias.dtype == torch.int32
)
out_channels, _, *kernel_size = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1269,6 +1314,11 @@ def quantized_conv_nchw_depthwise_asym8uxsym8u_asym8u_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.uint8
and weight.dtype == torch.uint8
and bias.dtype == torch.int32
)
out_channels, _, *kernel_size = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1313,6 +1363,11 @@ def quantized_conv_nhwc_depthwise_asym8sxsym8s_asym8s_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.int8
and weight.dtype == torch.int8
and bias.dtype == torch.int32
)
out_channels, *kernel_size, _ = weight.shape

in_size = input.shape
Expand Down Expand Up @@ -1357,6 +1412,11 @@ def quantized_conv_nhwc_depthwise_asym8uxsym8u_asym8u_per_tensor_meta(
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input.dtype == torch.uint8
and weight.dtype == torch.uint8
and bias.dtype == torch.int32
)
out_channels, *kernel_size, _ = weight.shape

in_size = input.shape
Expand Down
141 changes: 140 additions & 1 deletion backends/cadence/aot/ref_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# pyre-strict


from typing import Optional
from typing import Callable, Optional

import torch

Expand Down Expand Up @@ -479,6 +479,145 @@ def quantized_conv_nhwc_per_tensor(
)


def quantized_conv_variant(
layout: str,
input_dtype: torch.dtype,
weight_dtype: torch.dtype,
) -> Callable[[Callable[..., torch.Tensor]], Callable[..., torch.Tensor]]:
"""Create a quantized conv variant with type checking."""

def decorator(_: Callable[..., torch.Tensor]) -> Callable[..., torch.Tensor]:
def variant(
input_tensor: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
stride: tuple[int, int],
padding: tuple[int, int],
dilation: tuple[int, int],
groups: int,
in_zero_point: int,
weight_zero_point: int,
bias_scale: float,
output_scale: float,
output_zero_point: int,
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
assert (
input_tensor.dtype == input_dtype
), f"Expected input dtype {input_dtype}, got {input_tensor.dtype}"
assert (
weight.dtype == weight_dtype
), f"Expected weight dtype {weight_dtype}, got {weight.dtype}"

assert (
bias.dtype == torch.int32
), f"Expected bias dtype int32, got {bias.dtype}"

# Call the appropriate base function
match layout:
case "nchw":
return quantized_conv_nchw_per_tensor(
input_tensor,
weight,
bias,
stride,
padding,
dilation,
groups,
in_zero_point,
weight_zero_point,
bias_scale,
output_scale,
output_zero_point,
out_multiplier,
out_shift,
)
case "nhwc":
return quantized_conv_nhwc_per_tensor(
input_tensor,
weight,
bias,
stride,
padding,
dilation,
groups,
in_zero_point,
weight_zero_point,
bias_scale,
output_scale,
output_zero_point,
out_multiplier,
out_shift,
)
case _:
raise ValueError(f"Unknown layout {layout}")

return variant

return decorator


@impl(m, "quantized_conv_nchw_asym8sxsym8s_asym8s_per_tensor")
@quantized_conv_variant("nchw", torch.int8, torch.int8)
def quantized_conv_nchw_asym8sxsym8s_asym8s_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nchw_asym8uxsym8u_asym8u_per_tensor")
@quantized_conv_variant("nchw", torch.uint8, torch.uint8)
def quantized_conv_nchw_asym8uxsym8u_asym8u_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nhwc_asym8sxsym8s_asym8s_per_tensor")
@quantized_conv_variant("nhwc", torch.int8, torch.int8)
def quantized_conv_nhwc_asym8sxsym8s_asym8s_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nhwc_asym8uxsym8u_asym8u_per_tensor")
@quantized_conv_variant("nhwc", torch.uint8, torch.uint8)
def quantized_conv_nhwc_asym8uxsym8u_asym8u_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nchw_dilated_asym8sxsym8s_asym8s_per_tensor")
@quantized_conv_variant("nchw", torch.int8, torch.int8)
def quantized_conv_nchw_dilated_asym8sxsym8s_asym8s_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nchw_dilated_asym8uxsym8u_asym8u_per_tensor")
@quantized_conv_variant("nchw", torch.uint8, torch.uint8)
def quantized_conv_nchw_dilated_asym8uxsym8u_asym8u_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nhwc_dilated_asym8sxsym8s_asym8s_per_tensor")
@quantized_conv_variant("nhwc", torch.int8, torch.int8)
def quantized_conv_nhwc_dilated_asym8sxsym8s_asym8s_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nhwc_dilated_asym8uxsym8u_asym8u_per_tensor")
@quantized_conv_variant("nhwc", torch.uint8, torch.uint8)
def quantized_conv_nhwc_dilated_asym8uxsym8u_asym8u_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nchw_depthwise_asym8sxsym8s_asym8s_per_tensor")
@quantized_conv_variant("nchw", torch.int8, torch.int8)
def quantized_conv_nchw_depthwise_asym8sxsym8s_asym8s_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nchw_depthwise_asym8uxsym8u_asym8u_per_tensor")
@quantized_conv_variant("nchw", torch.uint8, torch.uint8)
def quantized_conv_nchw_depthwise_asym8uxsym8u_asym8u_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nhwc_depthwise_asym8sxsym8s_asym8s_per_tensor")
@quantized_conv_variant("nhwc", torch.int8, torch.int8)
def quantized_conv_nhwc_depthwise_asym8sxsym8s_asym8s_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_conv_nhwc_depthwise_asym8uxsym8u_asym8u_per_tensor")
@quantized_conv_variant("nhwc", torch.uint8, torch.uint8)
def quantized_conv_nhwc_depthwise_asym8uxsym8u_asym8u_per_tensor() -> torch.Tensor: ...


@impl(m, "quantized_relu")
def quantized_relu(
X: torch.Tensor,
Expand Down
Loading
Loading