Skip to content
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
76 changes: 23 additions & 53 deletions kernels/quantized/cpu/op_dequantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void check_dequantize_per_tensor_args(
int64_t quant_min,
int64_t quant_max,
ScalarType dtype,
exec_aten::optional<ScalarType>& out_dtype,
Tensor& out) {
ET_CHECK_MSG(
input.scalar_type() == ScalarType::Byte ||
Expand All @@ -48,11 +47,10 @@ void check_dequantize_per_tensor_args(
"input.scalar_type() %" PRId8 " is not matching dtype argumenta:",
static_cast<int8_t>(input.scalar_type()));

if (out_dtype.has_value()) {
ET_CHECK_MSG(
out.scalar_type() == out_dtype.value(),
"output_dtype must match the dtype of the out tensor");
}
ET_CHECK_MSG(
out.scalar_type() == ScalarType::Float,
"out.scalar_type() %" PRId8 " is not supported:",
static_cast<int8_t>(out.scalar_type()));

ET_CHECK_MSG(
quant_min <= quant_max,
Expand All @@ -79,15 +77,13 @@ Tensor& dequantize_per_tensor_out(
int64_t quant_min,
int64_t quant_max,
ScalarType dtype,
exec_aten::optional<ScalarType> out_dtype,
Tensor& out) {
torch::executor::Error err = resize_tensor(out, input.sizes());
ET_CHECK_MSG(
err == torch::executor::Error::Ok,
"Failed to resize out Tensor in dequantize_per_tensor_out");

check_dequantize_per_tensor_args(
input, quant_min, quant_max, dtype, out_dtype, out);
check_dequantize_per_tensor_args(input, quant_min, quant_max, dtype, out);

// calculate the dequantized output, cast scale to float to match fbgemm
// behavior
Expand Down Expand Up @@ -132,7 +128,6 @@ Tensor& dequantize_per_tensor_tensor_args_out(
int64_t quant_min,
int64_t quant_max,
ScalarType dtype,
exec_aten::optional<ScalarType> out_dtype,
Tensor& out) {
ET_CHECK_MSG(
scale.scalar_type() == ScalarType::Double,
Expand All @@ -158,20 +153,18 @@ Tensor& dequantize_per_tensor_tensor_args_out(
quant_min,
quant_max,
dtype,
out_dtype,
out);
return out;
}

Tensor& dequantize_per_channel_out(
const Tensor& input,
const Tensor& scale,
const optional<Tensor>& opt_zero_points,
const Tensor& zero_point,
int64_t axis,
int64_t quant_min,
int64_t quant_max,
ScalarType dtype,
exec_aten::optional<ScalarType> out_dtype,
Tensor& out) {
torch::executor::Error err = resize_tensor(out, input.sizes());

Expand Down Expand Up @@ -201,22 +194,18 @@ Tensor& dequantize_per_channel_out(
ssize_t(scale.numel()),
ssize_t(input.size(axis)));

if (opt_zero_points.has_value()) {
auto zero_point = opt_zero_points.value();
ET_CHECK_MSG(
zero_point.scalar_type() == ScalarType::Long,
"zero_point.scalar_type() %" PRId8 " is not integer type",
static_cast<int8_t>(zero_point.scalar_type()));

ET_CHECK_MSG(
zero_point.numel() == input.size(axis),
"zero_point.numel() %zd != input.size(axis) %zd",
ssize_t(zero_point.numel()),
ssize_t(input.size(axis)));
}
ET_CHECK_MSG(
zero_point.scalar_type() == ScalarType::Long,
"zero_point.scalar_type() %" PRId8 " is not integer type",
static_cast<int8_t>(zero_point.scalar_type()));

check_dequantize_per_tensor_args(
input, quant_min, quant_max, dtype, out_dtype, out);
ET_CHECK_MSG(
zero_point.numel() == input.size(axis),
"zero_point.numel() %zd != input.size(axis) %zd",
ssize_t(zero_point.numel()),
ssize_t(input.size(axis)));

check_dequantize_per_tensor_args(input, quant_min, quant_max, dtype, out);

// a list contains all dimensions except axis
int64_t dims[input.dim() - 1];
Expand All @@ -228,12 +217,7 @@ Tensor& dequantize_per_channel_out(
}
}
const double* scale_data = scale.const_data_ptr<double>();
const int64_t* zero_point_data;
if (opt_zero_points.has_value()) {
zero_point_data = opt_zero_points.value().const_data_ptr<int64_t>();
} else {
zero_point_data = nullptr;
}
const int64_t* zero_point_data = zero_point.const_data_ptr<int64_t>();

exec_aten::optional<exec_aten::ArrayRef<int64_t>> optional_dim_list{
exec_aten::ArrayRef<int64_t>{dims, size_t(input.dim() - 1)}};
Expand All @@ -250,10 +234,7 @@ Tensor& dequantize_per_channel_out(
case ScalarType::out_dtype: \
for (size_t channel_ix = 0; channel_ix < input.size(axis); ++channel_ix) { \
double _scale = scale_data[channel_ix]; \
int64_t _zero_point = 0; \
if (zero_point_data != nullptr) { \
_zero_point = zero_point_data[channel_ix]; \
} \
int64_t _zero_point = zero_point_data[channel_ix]; \
apply_over_dim_list( \
[input, out, _scale, _zero_point](size_t in_ix) { \
out.mutable_data_ptr<CTYPE_OUT>()[in_ix] = static_cast<CTYPE_OUT>( \
Expand Down Expand Up @@ -295,24 +276,15 @@ Tensor& dequantize_per_channel_out(
RuntimeContext& context,
const Tensor& input,
const Tensor& scale,
const optional<Tensor>& opt_zero_points,
const Tensor& zero_point,
int64_t axis,
int64_t quant_min,
int64_t quant_max,
ScalarType dtype,
exec_aten::optional<ScalarType> out_dtype,
Tensor& out) {
(void)context;
return dequantize_per_channel_out(
input,
scale,
opt_zero_points,
axis,
quant_min,
quant_max,
dtype,
out_dtype,
out);
input, scale, zero_point, axis, quant_min, quant_max, dtype, out);
}

Tensor& dequantize_per_tensor_out(
Expand All @@ -323,13 +295,12 @@ Tensor& dequantize_per_tensor_out(
int64_t quant_min,
int64_t quant_max,
ScalarType dtype,
exec_aten::optional<ScalarType> out_dtype,
Tensor& out) {
// TODO(larryliu): Add a context arg to the real op function and remove this
// wrapper
(void)context;
return dequantize_per_tensor_out(
input, scale, zero_point, quant_min, quant_max, dtype, out_dtype, out);
input, scale, zero_point, quant_min, quant_max, dtype, out);
}

Tensor& dequantize_per_tensor_tensor_args_out(
Expand All @@ -340,13 +311,12 @@ Tensor& dequantize_per_tensor_tensor_args_out(
int64_t quant_min,
int64_t quant_max,
ScalarType dtype,
exec_aten::optional<ScalarType> out_dtype,
Tensor& out) {
// TODO(larryliu): Add a context arg to the real op function and remove this
// wrapper
(void)context;
return dequantize_per_tensor_tensor_args_out(
input, scale, zero_point, quant_min, quant_max, dtype, out_dtype, out);
input, scale, zero_point, quant_min, quant_max, dtype, out);
}

} // namespace native
Expand Down
6 changes: 3 additions & 3 deletions kernels/quantized/quantized.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
- arg_meta: null
kernel_name: torch::executor::choose_qparams_tensor_out

- func: quantized_decomposed::dequantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, ScalarType? out_dtype=None, Tensor(a!) out) -> Tensor(a!)
- func: quantized_decomposed::dequantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)
variants: function
kernels:
- arg_meta: null
kernel_name: torch::executor::dequantize_per_tensor_out

- func: quantized_decomposed::dequantize_per_tensor.Tensor_out(Tensor input, Tensor scale, Tensor zero_point, int quant_min, int quant_max, ScalarType dtype, *, ScalarType? out_dtype=None, Tensor(a!) out) -> Tensor(a!)
- func: quantized_decomposed::dequantize_per_tensor.Tensor_out(Tensor input, Tensor scale, Tensor zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)
variants: function
kernels:
- arg_meta: null
Expand All @@ -28,7 +28,7 @@
- arg_meta: null
kernel_name: torch::executor::quantize_per_channel_out

- func: quantized_decomposed::dequantize_per_channel.out(Tensor input, Tensor scales, Tensor? zero_points, int axis, int quant_min, int quant_max, ScalarType dtype, *, ScalarType? out_dtype=None, Tensor(a!) out) -> Tensor(a!)
- func: quantized_decomposed::dequantize_per_channel.out(Tensor input, Tensor scales, Tensor zero_points, int axis, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)
variants: function
kernels:
- arg_meta: null
Expand Down
7 changes: 0 additions & 7 deletions kernels/quantized/test/op_add_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

using namespace ::testing;
using exec_aten::ArrayRef;
using exec_aten::optional;
using exec_aten::RuntimeContext;
using exec_aten::Scalar;
using exec_aten::ScalarType;
Expand Down Expand Up @@ -191,8 +190,6 @@ TEST(OpQuantizeAddTest, ConsitencyWithReferencePattern) {
Tensor qinput2 = tfo.zeros({3, 5});
Tensor qoutput = tfo.zeros({3, 5});

optional<ScalarType> out_dtype = optional<ScalarType>();

RuntimeContext context{};
// q -> qadd -> dq
// 3.5 / 0.5 + 1 = 8
Expand Down Expand Up @@ -238,7 +235,6 @@ TEST(OpQuantizeAddTest, ConsitencyWithReferencePattern) {
quant_min,
quant_max,
ScalarType::Byte,
out_dtype,
reference_op_output);

// now get results for q -> dq -> fp add -> q -> dq
Expand All @@ -249,7 +245,6 @@ TEST(OpQuantizeAddTest, ConsitencyWithReferencePattern) {
quant_min,
quant_max,
ScalarType::Byte,
out_dtype,
dq_input1);

dequantize_per_tensor_out(
Expand All @@ -259,7 +254,6 @@ TEST(OpQuantizeAddTest, ConsitencyWithReferencePattern) {
quant_min,
quant_max,
ScalarType::Byte,
out_dtype,
dq_input2);

add_out(context, dq_input1, dq_input2, 1.0, fp_output);
Expand All @@ -280,7 +274,6 @@ TEST(OpQuantizeAddTest, ConsitencyWithReferencePattern) {
quant_min,
quant_max,
ScalarType::Byte,
out_dtype,
reference_pattern_output);

Tensor expected = tf.full({3, 5}, 7.0);
Expand Down
30 changes: 3 additions & 27 deletions kernels/quantized/test/op_dequantize_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using namespace ::testing;
using exec_aten::ArrayRef;
using exec_aten::optional;
using exec_aten::Scalar;
using exec_aten::ScalarType;
using exec_aten::Tensor;
Expand All @@ -44,14 +43,7 @@ void test_dtype() {
// (100 - 30) * 0.5
Tensor expected = tfo.full({3, 5}, 35);
dequantize_per_tensor_out(
input,
scale,
zero_point,
quant_min,
quant_max,
DTYPE,
optional<ScalarType>(),
out);
input, scale, zero_point, quant_min, quant_max, DTYPE, out);

EXPECT_TENSOR_EQ(out, expected);
}
Expand All @@ -74,14 +66,7 @@ TEST(OpDequantizeOutTest, NonWholeNumbers) {
// (100 - 30) * 0.5
Tensor expected = tfo.full({3, 5}, 31.5);
dequantize_per_tensor_out(
input,
scale,
zero_point,
quant_min,
quant_max,
ScalarType::Byte,
optional<ScalarType>(),
out);
input, scale, zero_point, quant_min, quant_max, ScalarType::Byte, out);

EXPECT_TENSOR_EQ(out, expected);
}
Expand All @@ -102,14 +87,7 @@ TEST(OpDequantizeOutTest, TensorArgOverload) {
// (100 - 30) * 0.5
Tensor expected = tfo.full({3, 5}, 31.5);
dequantize_per_tensor_tensor_args_out(
input,
scale,
zero_point,
quant_min,
quant_max,
ScalarType::Byte,
optional<ScalarType>(),
out);
input, scale, zero_point, quant_min, quant_max, ScalarType::Byte, out);

EXPECT_TENSOR_EQ(out, expected);
}
Expand Down Expand Up @@ -138,7 +116,6 @@ TEST(OpDequantizeOutTest, DequantizePerChannel) {
quant_min,
quant_max,
ScalarType::Byte,
optional<ScalarType>(),
out);

EXPECT_TENSOR_EQ(out, expected);
Expand All @@ -159,7 +136,6 @@ TEST(OpDequantizeOutTest, DequantizePerChannel) {
quant_min,
quant_max,
ScalarType::Byte,
optional<ScalarType>(),
out);

EXPECT_TENSOR_EQ(out, expected);
Expand Down
2 changes: 0 additions & 2 deletions kernels/quantized/test/op_embedding_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

using namespace ::testing;
using exec_aten::ArrayRef;
using exec_aten::optional;
using exec_aten::RuntimeContext;
using exec_aten::Scalar;
using exec_aten::ScalarType;
Expand Down Expand Up @@ -150,7 +149,6 @@ TEST(OpQuantizedEmbeddingTest, ConsitencyWithReferencePattern) {
quant_min,
quant_max,
ScalarType::Byte,
optional<ScalarType>(),
weight);

embedding_out(
Expand Down