Skip to content

Add per_tensor overload for quantized_layer_norm #6554

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
Nov 2, 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
4 changes: 4 additions & 0 deletions backends/cadence/aot/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
kernels:
- arg_meta: null
kernel_name: impl::reference::quantized_layer_norm_out
- func: cadence::quantized_layer_norm.per_tensor_out(Tensor input, float in_scale, int in_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point, *, Tensor(a!) out) -> Tensor(a!)
kernels:
- arg_meta: null
kernel_name: impl::reference::quantized_layer_norm_per_tensor_out

- func: cadence::quantized_linear.out(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset, *, Tensor(a!) out) -> Tensor(a!)
kernels:
Expand Down
4 changes: 4 additions & 0 deletions backends/cadence/aot/functions_hifi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
kernels:
- arg_meta: null
kernel_name: cadence::impl::HiFi::quantized_layer_norm_out
- func: cadence::quantized_layer_norm.per_tensor_out(Tensor input, float in_scale, int in_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point, *, Tensor(a!) out) -> Tensor(a!)
kernels:
- arg_meta: null
kernel_name: cadence::impl::HiFi::quantized_layer_norm_per_tensor_out

- func: cadence::quantized_linear.out(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset, *, Tensor(a!) out) -> Tensor(a!)
kernels:
Expand Down
21 changes: 21 additions & 0 deletions backends/cadence/aot/ops_registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
lib.define(
"quantized_layer_norm.out(Tensor X, Tensor X_scale, Tensor X_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point, *, Tensor(a!) out) -> Tensor (a!)"
)
lib.define(
"quantized_layer_norm.per_tensor(Tensor X, float X_scale, int X_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point) -> (Tensor Y)"
)
lib.define(
"quantized_layer_norm.per_tensor_out(Tensor X, float X_scale, int X_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point, *, Tensor(a!) out) -> Tensor (a!)"
)

lib.define(
"quantized_linear(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset) -> (Tensor Z)"
Expand Down Expand Up @@ -180,6 +186,21 @@ def quantized_layer_norm_meta(
return input.new_empty(input.size(), dtype=input.dtype)


@register_fake("cadence::quantized_layer_norm.per_tensor")
def quantized_layer_norm_per_tensor_meta(
input: torch.Tensor,
X_scale: float,
X_zero_point: int,
normalized_shape: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float,
output_scale: float,
output_zero_point: int,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=input.dtype)


@register_fake("cadence::quantized_relu")
def quantized_relu_meta(
X: torch.Tensor,
Expand Down
44 changes: 41 additions & 3 deletions backends/cadence/hifi/operators/quantized_layer_norm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace native {
// Compute quantized layer_norm. The current implementation assumes that the
// input is per-tensor quantized.
template <typename T>
void quantized_layer_norm_(
void quantized_layer_norm_per_tensor_(
const Tensor& input,
float input_scale,
int64_t input_zero_point,
Expand Down Expand Up @@ -107,7 +107,7 @@ void quantized_layer_norm_(
int64_t input_zero_point = in_zero_point.const_data_ptr<int64_t>()[0];

// Call other overload
quantized_layer_norm_<T>(
quantized_layer_norm_per_tensor_<T>(
input,
input_scale,
input_zero_point,
Expand All @@ -120,7 +120,7 @@ void quantized_layer_norm_(
}

void quantized_layer_norm_out(
KernelRuntimeContext& ctx,
__ET_UNUSED KernelRuntimeContext& ctx,
const Tensor& input,
const Tensor& in_scale,
const Tensor& in_zero_point,
Expand Down Expand Up @@ -157,6 +157,44 @@ void quantized_layer_norm_out(
#undef typed_quantized_layer_norm
}

void quantized_layer_norm_per_tensor_out(
__ET_UNUSED KernelRuntimeContext& ctx,
const Tensor& input,
double in_scale,
int64_t in_zero_point,
__ET_UNUSED const IntArrayRef normalized_shape,
const Tensor& weight,
const Tensor& bias,
double eps,
double output_scale,
int64_t output_zero_point,
Tensor& out) {
#define typed_quantized_layer_norm(ctype, dtype) \
case ScalarType::dtype: { \
quantized_layer_norm_per_tensor_<ctype>( \
input, \
in_scale, \
in_zero_point, \
weight, \
bias, \
eps, \
output_scale, \
output_zero_point, \
out); \
break; \
}

ScalarType dtype = input.scalar_type();
switch (dtype) {
ET_FORALL_CADENCE_QUANTIZED_TYPES(typed_quantized_layer_norm)
default:
ET_DCHECK_MSG(
false, "Unhandled dtype %s", torch::executor::toString(dtype));
}

#undef typed_quantized_layer_norm
}

}; // namespace native
}; // namespace HiFi
}; // namespace impl
Expand Down
58 changes: 51 additions & 7 deletions backends/cadence/reference/operators/quantized_layer_norm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

#include <cmath>

using executorch::aten::Tensor;
using executorch::runtime::getLeadingDims;
using executorch::runtime::KernelRuntimeContext;
using ::executorch::aten::IntArrayRef;
using ::executorch::aten::ScalarType;
using ::executorch::aten::Tensor;
using ::executorch::runtime::getLeadingDims;
using ::executorch::runtime::KernelRuntimeContext;

namespace impl {
namespace reference {
Expand All @@ -22,7 +24,7 @@ namespace native {
// Compute quantized layer_norm. The current implementation assumes that the
// input is per-tensor quantized.
template <typename T>
void quantized_layer_norm_(
void quantized_layer_norm_per_tensor_(
const Tensor& input,
double input_scale,
int64_t input_zero_point,
Expand Down Expand Up @@ -98,7 +100,7 @@ void quantized_layer_norm_(
int64_t input_zero_point = in_zero_point.const_data_ptr<int64_t>()[0];

// Call other overload
quantized_layer_norm_<T>(
quantized_layer_norm_per_tensor_<T>(
input,
input_scale,
input_zero_point,
Expand All @@ -111,11 +113,11 @@ void quantized_layer_norm_(
}

void quantized_layer_norm_out(
KernelRuntimeContext& ctx,
__ET_UNUSED KernelRuntimeContext& ctx,
const Tensor& input,
const Tensor& in_scale,
const Tensor& in_zero_point,
const executorch::aten::IntArrayRef normalized_shape,
__ET_UNUSED const executorch::aten::IntArrayRef normalized_shape,
const Tensor& weight,
const Tensor& bias,
double eps,
Expand Down Expand Up @@ -152,6 +154,48 @@ void quantized_layer_norm_out(
}
}

void quantized_layer_norm_per_tensor_out(
__ET_UNUSED KernelRuntimeContext& ctx,
const Tensor& input,
double in_scale,
int64_t in_zero_point,
__ET_UNUSED const executorch::aten::IntArrayRef normalized_shape,
const Tensor& weight,
const Tensor& bias,
double eps,
double output_scale,
int64_t output_zero_point,
Tensor& out) {
if (input.scalar_type() == executorch::aten::ScalarType::Byte) {
quantized_layer_norm_per_tensor_<uint8_t>(
input,
in_scale,
in_zero_point,
weight,
bias,
eps,
output_scale,
output_zero_point,
out);
} else if (input.scalar_type() == executorch::aten::ScalarType::Char) {
quantized_layer_norm_per_tensor_<int8_t>(
input,
in_scale,
in_zero_point,
weight,
bias,
eps,
output_scale,
output_zero_point,
out);
} else {
ET_CHECK_MSG(
false,
"Unhandled input dtype %hhd",
static_cast<int8_t>(input.scalar_type()));
Comment on lines +192 to +195
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ET_KERNEL_CHECK instead for better error messages

Suggested change
ET_CHECK_MSG(
false,
"Unhandled input dtype %hhd",
static_cast<int8_t>(input.scalar_type()));
ET_KERNEL_CHECK(
context,
false,
InvalidArgument,
out);

}
}

}; // namespace native
}; // namespace reference
}; // namespace impl
Loading