Skip to content

[ExecuTorch] support BF16 in op_to_copy #4976

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 12 commits into from
Sep 5, 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
9 changes: 5 additions & 4 deletions kernels/portable/cpu/op_to_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ Tensor& to_copy_out(
InvalidArgument,
out);

ET_SWITCH_REALHB_TYPES(self.scalar_type(), ctx, "to_copy", CTYPE_IN, [&] {
ET_SWITCH_REALHB_TYPES(out.scalar_type(), ctx, "to_copy", CTYPE_OUT, [&] {
_to_impl<CTYPE_IN, CTYPE_OUT>(self, out);
});
ET_SWITCH_REALHBBF16_TYPES(self.scalar_type(), ctx, "to_copy", CTYPE_IN, [&] {
ET_SWITCH_REALHBBF16_TYPES(
out.scalar_type(), ctx, "to_copy", CTYPE_OUT, [&] {
_to_impl<CTYPE_IN, CTYPE_OUT>(self, out);
});
});

return out;
Expand Down
18 changes: 8 additions & 10 deletions kernels/portable/cpu/scalar_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ struct promote_type_with_scalar_type {
static_assert(
!is_bits_type<T1>::value,
"promote_type_with_scalar_type not valid for bits dtypes");
static_assert(
!std::is_same<
T1,
typename ScalarTypeToCppType<exec_aten::ScalarType::BFloat16>::type>::
value,
"promote_type_with_scalar_type not valid for BFloat16");
using promote_type_with_scalar_type_not_respecting_half_to_float =
typename std::conditional<
is_complex_type<T1>::value ||
Expand All @@ -119,10 +113,14 @@ struct promote_type_with_scalar_type {
public:
using type = typename std::conditional<
half_to_float &&
std::is_same<
promote_type_with_scalar_type_not_respecting_half_to_float,
typename ScalarTypeToCppType<exec_aten::ScalarType::Half>::type>::
value,
(std::is_same<
promote_type_with_scalar_type_not_respecting_half_to_float,
typename ScalarTypeToCppType<
exec_aten::ScalarType::Half>::type>::value ||
std::is_same<
promote_type_with_scalar_type_not_respecting_half_to_float,
typename ScalarTypeToCppType<
exec_aten::ScalarType::BFloat16>::type>::value),
typename ScalarTypeToCppType<exec_aten::ScalarType::Float>::type,
promote_type_with_scalar_type_not_respecting_half_to_float>::type;
};
Expand Down
27 changes: 19 additions & 8 deletions kernels/test/op_to_copy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ typedef std::map<
std::type_index,
std::variant<
std::vector<float>,
std::vector<double>>>
std::vector<double>,
std::vector<exec_aten::Half>,
std::vector<exec_aten::BFloat16>>>
FloatingTypeToDataMap;

typedef std::map<
Expand Down Expand Up @@ -309,9 +311,9 @@ TEST_F(OpToTest, AllDtypesSupported) {
ScalarType::OUTPUT_DTYPE>(test_cases);

#define TEST_ENTRY(INPUT_CTYPE, INPUT_DTYPE) \
ET_FORALL_REAL_TYPES_WITH2(INPUT_CTYPE, INPUT_DTYPE, TEST_KERNEL);
ET_FORALL_REALHBF16_TYPES_WITH2(INPUT_CTYPE, INPUT_DTYPE, TEST_KERNEL);

ET_FORALL_REAL_TYPES(TEST_ENTRY);
ET_FORALL_REALHBF16_TYPES(TEST_ENTRY);

#undef TEST_ENTRY
#undef TEST_KERNEL
Expand All @@ -323,14 +325,14 @@ TEST_F(OpToTest, BoolTests) {
#define TEST_TO_BOOL(INPUT_CTYPE, INPUT_DTYPE) \
test_runner_to_bool<INPUT_CTYPE, ScalarType::INPUT_DTYPE>( \
test_case_to_bool, result_to_bool);
ET_FORALL_REAL_TYPES(TEST_TO_BOOL);
ET_FORALL_REALHBF16_TYPES(TEST_TO_BOOL);

std::vector<uint8_t> test_case_from_bool = {true, true, false};
std::vector<double> result_from_bool = {1.0, 1.0, 0};
#define TEST_FROM_BOOL(OUTPUT_CTYPE, OUTPUT_DTYPE) \
test_runner_from_bool<OUTPUT_CTYPE, ScalarType::OUTPUT_DTYPE>( \
test_case_from_bool, result_from_bool);
ET_FORALL_REAL_TYPES(TEST_FROM_BOOL);
ET_FORALL_REALHBF16_TYPES(TEST_FROM_BOOL);
}

TEST_F(OpToTest, NanInfSupported) {
Expand All @@ -349,9 +351,9 @@ TEST_F(OpToTest, NanInfSupported) {
ScalarType::OUTPUT_DTYPE>(test_cases);

#define TEST_ENTRY(INPUT_CTYPE, INPUT_DTYPE) \
ET_FORALL_FLOAT_TYPES_WITH2(INPUT_CTYPE, INPUT_DTYPE, TEST_KERNEL);
ET_FORALL_FLOATHBF16_TYPES_WITH2(INPUT_CTYPE, INPUT_DTYPE, TEST_KERNEL);

ET_FORALL_FLOAT_TYPES(TEST_ENTRY);
ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY);

#undef TEST_ENTRY
#undef TEST_KERNEL
Expand Down Expand Up @@ -381,6 +383,13 @@ TEST_F(OpToTest, HardcodeFloatConvertInt) {
-0.30919688936285893988};
// clang-format on

std::vector<exec_aten::Half> half_data;
std::vector<exec_aten::BFloat16> bf16_data;
for (auto d : double_data) {
half_data.emplace_back(d);
bf16_data.emplace_back(d);
}

std::vector<int64_t> int64_data = {
-1, -4, 2, -2, 3, 3, -3, -4, 3, 3, 0, 2, 0, -1, 0};
std::vector<int32_t> int32_data = {
Expand All @@ -394,6 +403,8 @@ TEST_F(OpToTest, HardcodeFloatConvertInt) {
FloatingTypeToDataMap floating_point_data;
floating_point_data[typeid(float)] = float_data;
floating_point_data[typeid(double)] = double_data;
floating_point_data[typeid(exec_aten::Half)] = half_data;
floating_point_data[typeid(exec_aten::BFloat16)] = bf16_data;

// Gathering all int data together for better traversial
IntTypeToDataMap int_data;
Expand All @@ -412,7 +423,7 @@ TEST_F(OpToTest, HardcodeFloatConvertInt) {
#define TEST_ENTRY(INPUT_CTYPE, INPUT_DTYPE) \
ET_FORALL_INT_TYPES_WITH2(INPUT_CTYPE, INPUT_DTYPE, TEST_KERNEL);

ET_FORALL_FLOAT_TYPES(TEST_ENTRY);
ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY);
}

TEST_F(OpToTest, MismatchedSizesDie) {
Expand Down
2 changes: 2 additions & 0 deletions runtime/core/exec_aten/exec_aten.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <c10/core/MemoryFormat.h> // @manual
#include <c10/core/Scalar.h> // @manual
#include <c10/util/ArrayRef.h> // @manual
#include <c10/util/BFloat16-math.h> // @manual
#include <c10/util/BFloat16.h> // @manual
#include <c10/util/Half.h> // @manual
#include <c10/util/Optional.h> // @manual
Expand All @@ -31,6 +32,7 @@
#else // use executor
#include <executorch/runtime/core/array_ref.h> // @manual
#include <executorch/runtime/core/portable_type/bfloat16.h> // @manual
#include <executorch/runtime/core/portable_type/bfloat16_math.h> // @manual
#include <executorch/runtime/core/portable_type/complex.h> // @manual
#include <executorch/runtime/core/portable_type/device.h> // @manual
#include <executorch/runtime/core/portable_type/half.h> // @manual
Expand Down
20 changes: 17 additions & 3 deletions runtime/core/exec_aten/testing_util/tensor_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>

using exec_aten::BFloat16;
using exec_aten::Half;
using exec_aten::ScalarType;
using exec_aten::Tensor;

Expand All @@ -32,9 +34,7 @@ namespace {
* T must be a floating point type. Non-floating point data should be compared
* directly.
*/
template <
typename T,
typename = std::enable_if_t<std::is_floating_point<T>::value>>
template <typename T>
bool data_is_close(
const T* a,
const T* b,
Expand Down Expand Up @@ -119,6 +119,20 @@ bool tensors_are_close(
a.numel(),
rtol,
atol);
} else if (a.scalar_type() == ScalarType::Half) {
return data_is_close<Half>(
a.const_data_ptr<Half>(),
b.const_data_ptr<Half>(),
a.numel(),
rtol,
atol);
} else if (a.scalar_type() == ScalarType::BFloat16) {
return data_is_close<BFloat16>(
a.const_data_ptr<BFloat16>(),
b.const_data_ptr<BFloat16>(),
a.numel(),
rtol,
atol);
} else {
// Non-floating-point types can be compared bitwise.
return memcmp(a.const_data_ptr(), b.const_data_ptr(), a.nbytes()) == 0;
Expand Down
41 changes: 28 additions & 13 deletions runtime/core/exec_aten/util/genScalarTypeTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

indexToType = ["U1", "I1", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "B1"]
indexToType = [
"U1",
"I1",
"I2",
"I4",
"I8",
"F2",
"F4",
"F8",
"C2",
"C4",
"C8",
"B1",
"BF",
]
promoteTypesLookup = [
["U1", "I2", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "U1"],
["I2", "I1", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I1"],
["I2", "I2", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I2"],
["I4", "I4", "I4", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I4"],
["I8", "I8", "I8", "I8", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I8"],
["F2", "F2", "F2", "F2", "F2", "F2", "F4", "F8", "C2", "C4", "C8", "F2"],
["F4", "F4", "F4", "F4", "F4", "F4", "F4", "F8", "C4", "C4", "C8", "F4"],
["F8", "F8", "F8", "F8", "F8", "F8", "F8", "F8", "C8", "C8", "C8", "F8"],
["C2", "C2", "C2", "C2", "C2", "C2", "C4", "C8", "C2", "C4", "C8", "C2"],
["C4", "C4", "C4", "C4", "C4", "C4", "C4", "C8", "C4", "C4", "C8", "C4"],
["C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8"],
["U1", "I1", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "B1"],
["U1", "I2", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "U1", "BF"],
["I2", "I1", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I1", "BF"],
["I2", "I2", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I2", "BF"],
["I4", "I4", "I4", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I4", "BF"],
["I8", "I8", "I8", "I8", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "I8", "BF"],
["F2", "F2", "F2", "F2", "F2", "F2", "F4", "F8", "C2", "C4", "C8", "F2", "F4"],
["F4", "F4", "F4", "F4", "F4", "F4", "F4", "F8", "C4", "C4", "C8", "F4", "F4"],
["F8", "F8", "F8", "F8", "F8", "F8", "F8", "F8", "C8", "C8", "C8", "F8", "F8"],
["C2", "C2", "C2", "C2", "C2", "C2", "C4", "C8", "C2", "C4", "C8", "C2", "C4"],
["C4", "C4", "C4", "C4", "C4", "C4", "C4", "C8", "C4", "C4", "C8", "C4", "C4"],
["C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8", "C8"],
["U1", "I1", "I2", "I4", "I8", "F2", "F4", "F8", "C2", "C4", "C8", "B1", "BF"],
["BF", "BF", "BF", "BF", "BF", "F4", "F4", "F8", "C4", "C4", "C8", "BF", "BF"],
]
for rowIndex, row in enumerate(promoteTypesLookup):
for colIndex, col in enumerate(row):
Expand Down
Loading
Loading