Skip to content
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

#shlo_ref Rework quantized tensor element types. #65134

Merged
merged 1 commit into from
Apr 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion tensorflow/lite/experimental/shlo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ cc_library(
hdrs = ["tensor.h"],
deps = [
":data_type",
":overload",
":quantized_tensor_element_type",
":shape",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/types:span",
],
)
Expand Down Expand Up @@ -74,13 +76,15 @@ cc_test(

cc_library(
name = "quantized_tensor_element_type",
srcs = ["quantized_tensor_element_type.cc"],
hdrs = ["quantized_tensor_element_type.h"],
deps = [
":data_type",
":shape",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/types:span",
],
)
Expand All @@ -91,7 +95,6 @@ cc_test(
linkopts = shlo_ref_linkopts(),
deps = [
":data_type",
":f16",
":quantized_tensor_element_type",
"@com_google_googletest//:gtest_main",
],
Expand Down Expand Up @@ -150,6 +153,23 @@ cc_test(
],
)

cc_library(
name = "overload",
hdrs = ["overload.h"],
visibility = ["//tensorflow/lite/experimental/shlo:__subpackages__"],
)

cc_test(
name = "overload_test",
srcs = ["overload_test.cc"],
deps = [
":overload",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "quantize",
hdrs = ["quantize.h"],
Expand Down
1 change: 0 additions & 1 deletion tensorflow/lite/experimental/shlo/ops/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ cc_library(
":unary_elementwise",
":util",
"//tensorflow/lite/experimental/shlo:bf16",
"//tensorflow/lite/experimental/shlo:data_type",
"//tensorflow/lite/experimental/shlo:dispatch",
"//tensorflow/lite/experimental/shlo:f16",
"//tensorflow/lite/experimental/shlo:tensor",
Expand Down
9 changes: 5 additions & 4 deletions tensorflow/lite/experimental/shlo/ops/abs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ absl::Status Prepare(AbsOp& op, const Tensor& input, Tensor& output) {
absl::Status Evaluate(AbsOp& op, const Tensor& input, Tensor& output) {
Abs abs;
if (input.IsPerTensorQuantized()) {
DISPATCH_QUANTIZED(detail::DequantizeOpQuantizePerTensor,
input.quantized_tensor_element_type().StorageType(),
input.quantized_tensor_element_type().ExpressedType(),
abs, input, output)
DISPATCH_QUANTIZED(
detail::DequantizeOpQuantizePerTensor,
input.quantized_per_tensor_element_type().StorageType(),
input.quantized_per_tensor_element_type().ExpressedType(), abs, input,
output)
} else if (IsSignedIntTensor(input) || IsFloatTensor(input)) {
DISPATCH_INT_FLOAT(detail::EvaluateNoQuantization,
input.tensor_element_type(), abs, input, output);
Expand Down
13 changes: 7 additions & 6 deletions tensorflow/lite/experimental/shlo/ops/abs_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ TYPED_TEST(QuantizedAbsTest, QuantizedPerTensor) {
Vector<StorageT> output_data(shape.NumElements());
const ExpressedT scale = static_cast<ExpressedT>(1.5);
const StorageT zero_point = static_cast<StorageT>(5);
const QuantizedTensorElementType tensor_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(scale,
zero_point);
const QuantizedElementTypePerTensor tensor_type =
QuantizedElementTypePerTensor(TypeParam::kStorage, zero_point,
TypeParam::kExpressed, scale);
Tensor input_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = input_data.data()};
Tensor output_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = output_data.data()};

Vector<StorageT> expected_data(shape.NumElements());
Expand Down
12 changes: 6 additions & 6 deletions tensorflow/lite/experimental/shlo/ops/binary_elementwise.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ void DequantizeOpQuantizePerTensor(F&& func, const Tensor& lhs,
using ExpressedT = StorageType<expressed_type>;
const DimensionSize num_elements = lhs.NumElements();
const StorageT lhs_zero_point =
lhs.quantized_tensor_element_type().ZeroPoints<storage_type>()[0];
lhs.quantized_per_tensor_element_type().ZeroPointAs<storage_type>();
const ExpressedT lhs_scale =
lhs.quantized_tensor_element_type().Scales<expressed_type>()[0];
lhs.quantized_per_tensor_element_type().ScaleAs<expressed_type>();
const StorageT rhs_zero_point =
rhs.quantized_tensor_element_type().ZeroPoints<storage_type>()[0];
rhs.quantized_per_tensor_element_type().ZeroPointAs<storage_type>();
const ExpressedT rhs_scale =
rhs.quantized_tensor_element_type().Scales<expressed_type>()[0];
rhs.quantized_per_tensor_element_type().ScaleAs<expressed_type>();
const StorageT output_zero_point =
output.quantized_tensor_element_type().ZeroPoints<storage_type>()[0];
output.quantized_per_tensor_element_type().ZeroPointAs<storage_type>();
const ExpressedT output_scale =
output.quantized_tensor_element_type().Scales<expressed_type>()[0];
output.quantized_per_tensor_element_type().ScaleAs<expressed_type>();
const StorageT* lhs_data = lhs.GetDataAs<storage_type>();
const StorageT* rhs_data = rhs.GetDataAs<storage_type>();
StorageT* output_data = output.GetDataAs<storage_type>();
Expand Down
48 changes: 21 additions & 27 deletions tensorflow/lite/experimental/shlo/ops/binary_elementwise_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,27 @@ TYPED_TEST(DequantizeOpQuantizePerTensor, QuantizedPerTensorWithTestOp) {
const StorageT rhs_zero_point = static_cast<StorageT>(5);
const ExpressedT output_scale = static_cast<ExpressedT>(1.5);
const StorageT output_zero_point = static_cast<StorageT>(3);
Tensor lhs_tensor{
.type =
QuantizedTensorType{
.shape = shape,
.element_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(
lhs_scale, lhs_zero_point)},
.data = lhs_data.data()};
Tensor rhs_tensor{
.type =
QuantizedTensorType{
.shape = shape,
.element_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(
rhs_scale, rhs_zero_point)},
.data = rhs_data.data()};
Tensor output_tensor{
.type =
QuantizedTensorType{
.shape = shape,
.element_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(
output_scale, output_zero_point)},
.data = output_data.data()};
Tensor lhs_tensor{.type =
QuantizedPerTensorTensorType{
.shape = shape,
.element_type = QuantizedElementTypePerTensor(
TypeParam::kStorage, lhs_zero_point,
TypeParam::kExpressed, lhs_scale)},
.data = lhs_data.data()};
Tensor rhs_tensor{.type =
QuantizedPerTensorTensorType{
.shape = shape,
.element_type = QuantizedElementTypePerTensor(
TypeParam::kStorage, rhs_zero_point,
TypeParam::kExpressed, rhs_scale)},
.data = rhs_data.data()};
Tensor output_tensor{.type =
QuantizedPerTensorTensorType{
.shape = shape,
.element_type = QuantizedElementTypePerTensor(
TypeParam::kStorage, output_zero_point,
TypeParam::kExpressed, output_scale)},
.data = output_data.data()};

Vector<StorageT> expected_data(shape.NumElements());
absl::c_transform(
Expand Down
9 changes: 5 additions & 4 deletions tensorflow/lite/experimental/shlo/ops/cbrt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ absl::Status Prepare(CbrtOp& op, const Tensor& input, Tensor& output) {
absl::Status Evaluate(CbrtOp& op, const Tensor& input, Tensor& output) {
Cbrt cbrt;
if (input.IsPerTensorQuantized()) {
DISPATCH_QUANTIZED(detail::DequantizeOpQuantizePerTensor,
input.quantized_tensor_element_type().StorageType(),
input.quantized_tensor_element_type().ExpressedType(),
cbrt, input, output)
DISPATCH_QUANTIZED(
detail::DequantizeOpQuantizePerTensor,
input.quantized_per_tensor_element_type().StorageType(),
input.quantized_per_tensor_element_type().ExpressedType(), cbrt, input,
output)
} else if (IsFloatTensor(input)) {
DISPATCH_FLOAT(detail::EvaluateNoQuantization, input.tensor_element_type(),
cbrt, input, output);
Expand Down
13 changes: 7 additions & 6 deletions tensorflow/lite/experimental/shlo/ops/cbrt_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,16 @@ TYPED_TEST(QuantizedCbrtTest, PerTensorWorks) {
Vector<StorageT> output_data(shape.NumElements());
const ExpressedT scale = static_cast<ExpressedT>(1.5);
const StorageT zero_point = static_cast<StorageT>(5);
const QuantizedTensorElementType tensor_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(scale,
zero_point);
const QuantizedElementTypePerTensor tensor_type =
QuantizedElementTypePerTensor(TypeParam::kStorage, zero_point,
TypeParam::kExpressed, scale);
Tensor input_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = input_data.data()};
Tensor output_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = output_data.data()};

Vector<StorageT> expected_data(shape.NumElements());
Expand Down
10 changes: 5 additions & 5 deletions tensorflow/lite/experimental/shlo/ops/ceil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.

#include "absl/status/status.h"
#include "tensorflow/lite/experimental/shlo/bf16.h"
#include "tensorflow/lite/experimental/shlo/data_type.h"
#include "tensorflow/lite/experimental/shlo/dispatch.h"
#include "tensorflow/lite/experimental/shlo/f16.h"
#include "tensorflow/lite/experimental/shlo/ops/unary_elementwise.h"
Expand Down Expand Up @@ -59,10 +58,11 @@ absl::Status Prepare(CeilOp& op, const Tensor& input, Tensor& output) {
absl::Status Evaluate(CeilOp& op, const Tensor& input, Tensor& output) {
Ceil ceil;
if (input.IsPerTensorQuantized()) {
DISPATCH_QUANTIZED(detail::DequantizeOpQuantizePerTensor,
input.quantized_tensor_element_type().StorageType(),
input.quantized_tensor_element_type().ExpressedType(),
ceil, input, output)
DISPATCH_QUANTIZED(
detail::DequantizeOpQuantizePerTensor,
input.quantized_per_tensor_element_type().StorageType(),
input.quantized_per_tensor_element_type().ExpressedType(), ceil, input,
output)
} else if (IsFloatTensor(input)) {
DISPATCH_FLOAT(detail::EvaluateNoQuantization, input.tensor_element_type(),
ceil, input, output);
Expand Down
13 changes: 7 additions & 6 deletions tensorflow/lite/experimental/shlo/ops/ceil_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,16 @@ TYPED_TEST(QuantizedCeilTest, PerTensorWorks) {
Vector<StorageT> output_data(shape.NumElements());
const ExpressedT scale = static_cast<ExpressedT>(1.5);
const StorageT zero_point = static_cast<StorageT>(5);
const QuantizedTensorElementType tensor_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(scale,
zero_point);
const QuantizedElementTypePerTensor tensor_type =
QuantizedElementTypePerTensor(TypeParam::kStorage, zero_point,
TypeParam::kExpressed, scale);
Tensor input_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = input_data.data()};
Tensor output_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = output_data.data()};

Vector<StorageT> expected_data(shape.NumElements());
Expand Down
35 changes: 18 additions & 17 deletions tensorflow/lite/experimental/shlo/ops/compare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ void DequantizeCompare(F&& func, const Tensor& lhs, const Tensor& rhs,
using ExpressedT = StorageType<expressed_type>;
const DimensionSize num_elements = lhs.NumElements();
const StorageT lhs_zero_point =
lhs.quantized_tensor_element_type().ZeroPoints<storage_type>()[0];
lhs.quantized_per_tensor_element_type().ZeroPointAs<storage_type>();
const ExpressedT lhs_scale =
lhs.quantized_tensor_element_type().Scales<expressed_type>()[0];
lhs.quantized_per_tensor_element_type().ScaleAs<expressed_type>();
const StorageT rhs_zero_point =
rhs.quantized_tensor_element_type().ZeroPoints<storage_type>()[0];
rhs.quantized_per_tensor_element_type().ZeroPointAs<storage_type>();
const ExpressedT rhs_scale =
rhs.quantized_tensor_element_type().Scales<expressed_type>()[0];
rhs.quantized_per_tensor_element_type().ScaleAs<expressed_type>();
const StorageT* lhs_data = lhs.GetDataAs<storage_type>();
const StorageT* rhs_data = rhs.GetDataAs<storage_type>();
bool* output_data = output.GetDataAs<DataType::kI1>();
Expand Down Expand Up @@ -79,19 +79,20 @@ absl::Status Prepare(CompareOp& op, const Tensor& lhs, const Tensor& rhs,
// NOLINTNEXTLINE(google-readability-function-size)
absl::Status Evaluate(CompareOp& op, const Tensor& lhs, const Tensor& rhs,
Tensor& output) {
#define SHLO_REF_COMPARISON_DIRECTION_CASE(DIRECTION, COMPARISON_OP) \
case CompareOp::ComparisonDirection::DIRECTION: { \
if (IsBoolTensor(lhs) || IsIntTensor(lhs) || IsFloatTensor(lhs)) { \
DISPATCH_BOOL_INT_FLOAT(detail::EvaluateNoQuantization, \
lhs.tensor_element_type(), COMPARISON_OP, lhs, \
rhs, output); \
} else if (IsQuantizedPerTensorTensor(lhs)) { \
DISPATCH_QUANTIZED(DequantizeCompare, \
lhs.quantized_tensor_element_type().StorageType(), \
lhs.quantized_tensor_element_type().ExpressedType(), \
COMPARISON_OP, lhs, rhs, output) \
} \
break; \
#define SHLO_REF_COMPARISON_DIRECTION_CASE(DIRECTION, COMPARISON_OP) \
case CompareOp::ComparisonDirection::DIRECTION: { \
if (IsBoolTensor(lhs) || IsIntTensor(lhs) || IsFloatTensor(lhs)) { \
DISPATCH_BOOL_INT_FLOAT(detail::EvaluateNoQuantization, \
lhs.tensor_element_type(), COMPARISON_OP, lhs, \
rhs, output); \
} else if (IsQuantizedPerTensorTensor(lhs)) { \
DISPATCH_QUANTIZED( \
DequantizeCompare, \
lhs.quantized_per_tensor_element_type().StorageType(), \
lhs.quantized_per_tensor_element_type().ExpressedType(), \
COMPARISON_OP, lhs, rhs, output) \
} \
break; \
}

switch (op.attributes.comparison_direction) {
Expand Down
13 changes: 7 additions & 6 deletions tensorflow/lite/experimental/shlo/ops/compare_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,16 @@ TYPED_TEST(QuantizedCompareTest, PerTensorWorks) {
RandomBuffer<TypeParam::kStorage>(shape, /*min=*/zero_point + 1,
/*max=*/zero_point + 5);
Vector<StorageType<DataType::kI1>> output_data(shape.NumElements());
const QuantizedTensorElementType tensor_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(scale,
zero_point);
const QuantizedElementTypePerTensor tensor_type =
QuantizedElementTypePerTensor(TypeParam::kStorage, zero_point,
TypeParam::kExpressed, scale);
Tensor lhs_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = lhs_data.data()};
Tensor rhs_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = rhs_data.data()};
Tensor output_tensor{
.type = TensorType{.shape = shape, .element_type = DataType::kI1},
Expand Down
9 changes: 5 additions & 4 deletions tensorflow/lite/experimental/shlo/ops/cosine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ absl::Status Prepare(CosineOp& op, const Tensor& input, Tensor& output) {
absl::Status Evaluate(CosineOp& op, const Tensor& input, Tensor& output) {
Cosine cosine;
if (input.IsPerTensorQuantized()) {
DISPATCH_QUANTIZED(detail::DequantizeOpQuantizePerTensor,
input.quantized_tensor_element_type().StorageType(),
input.quantized_tensor_element_type().ExpressedType(),
cosine, input, output)
DISPATCH_QUANTIZED(
detail::DequantizeOpQuantizePerTensor,
input.quantized_per_tensor_element_type().StorageType(),
input.quantized_per_tensor_element_type().ExpressedType(), cosine,
input, output)
} else if (IsFloatTensor(input)) {
DISPATCH_FLOAT(detail::EvaluateNoQuantization, input.tensor_element_type(),
cosine, input, output);
Expand Down
13 changes: 7 additions & 6 deletions tensorflow/lite/experimental/shlo/ops/cosine_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ TYPED_TEST(QuantizedCosineTest, PerTensorWorks) {
Vector<StorageT> output_data(shape.NumElements());
const ExpressedT scale = static_cast<ExpressedT>(1.5);
const StorageT zero_point = static_cast<StorageT>(5);
const QuantizedTensorElementType tensor_type =
QuantizedTensorElementType::PerTensor<TypeParam::kStorage,
TypeParam::kExpressed>(scale,
zero_point);
const QuantizedElementTypePerTensor tensor_type =
QuantizedElementTypePerTensor(TypeParam::kStorage, zero_point,
TypeParam::kExpressed, scale);
Tensor input_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = input_data.data()};
Tensor output_tensor{
.type = QuantizedTensorType{.shape = shape, .element_type = tensor_type},
.type = QuantizedPerTensorTensorType{.shape = shape,
.element_type = tensor_type},
.data = output_data.data()};

Vector<StorageT> expected_data(shape.NumElements());
Expand Down