Skip to content

Update the API of registering fake kernels to new standard #5084

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 12 additions & 0 deletions backends/cadence/aot/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ python_library(
],
)

python_library(
name = "ops_registrations",
srcs = [
"ops_registrations.py",
],
deps = [
":utils",
"//caffe2:torch",
"//executorch/exir:scalar_type",
],
)

export_file(name = "functions.yaml")

executorch_generated_lib(
Expand Down
32 changes: 17 additions & 15 deletions backends/cadence/aot/ops_registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-strict

from math import prod
from typing import Optional, Tuple

import torch
from executorch.exir.scalar_type import ScalarType
from torch.library import impl, Library
from torch.library import Library, register_fake

from .utils import get_conv1d_output_size, get_conv2d_output_size

Expand Down Expand Up @@ -67,31 +69,31 @@
m = Library("cadence", "IMPL", "Meta")


@impl(m, "quantize_per_tensor")
@register_fake("cadence::quantize_per_tensor")
def quantize_per_tensor_meta(
input: torch.Tensor,
scale: float,
zero_point: int,
quant_min: int,
quant_max: int,
dtype: ScalarType,
):
dtype: torch.dtype,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=dtype)


@impl(m, "dequantize_per_tensor")
@register_fake("cadence::dequantize_per_tensor")
def dequantize_per_tensor_meta(
input: torch.Tensor,
scale: float,
zero_point: int,
quant_min: int,
quant_max: int,
dtype: ScalarType,
):
dtype: torch.dtype,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=torch.float)


@impl(m, "quantized_linear")
@register_fake("cadence::quantized_linear")
def quantized_linear_meta(
src: torch.Tensor,
weight: torch.Tensor,
Expand All @@ -102,7 +104,7 @@ def quantized_linear_meta(
out_shift: torch.Tensor,
out_zero_point: int,
offset: Optional[torch.Tensor],
):
) -> torch.Tensor:
# src comes in shape [leading_dims, in_dim]
# weight comes in shape [out_dim, in_dim]
# output comes in empty with shape [leading_dims, out_dim]
Expand All @@ -113,7 +115,7 @@ def quantized_linear_meta(
return src.new_empty(out_size, dtype=torch.uint8)


@impl(m, "quantized_conv")
@register_fake("cadence::quantized_conv")
def quantized_conv_meta(
input: torch.Tensor,
weight: torch.Tensor,
Expand Down Expand Up @@ -151,7 +153,7 @@ def quantized_conv_meta(
return input.new_empty(output_size, dtype=input.dtype)


@impl(m, "quantized_layer_norm")
@register_fake("cadence::quantized_layer_norm")
def quantized_layer_norm_meta(
input: torch.Tensor,
X_scale: torch.Tensor,
Expand All @@ -162,22 +164,22 @@ def quantized_layer_norm_meta(
eps: float,
output_scale: float,
output_zero_point: int,
):
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=torch.uint8)


@impl(m, "quantized_relu")
@register_fake("cadence::quantized_relu")
def quantized_relu_meta(
X: torch.Tensor,
X_zero_point: torch.Tensor,
out_zero_point: int,
out_multiplier: torch.Tensor,
out_shift: torch.Tensor,
):
) -> torch.Tensor:
return X.new_empty(X.size(), dtype=torch.uint8)


@impl(m, "quantized_matmul")
@register_fake("cadence::quantized_matmul")
def quantized_matmul_meta(
X: torch.Tensor,
X_zero_point: int,
Expand Down