Skip to content

Add support for the default float, int and bool types for executorch::runtime::etensor::Scalar to` method #10040

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 19 additions & 22 deletions runtime/core/portable_type/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <executorch/runtime/core/portable_type/bfloat16.h>
#include <executorch/runtime/core/portable_type/half.h>
#include <executorch/runtime/core/portable_type/complex.h>
#include <executorch/runtime/core/portable_type/scalar_type.h>
#include <executorch/runtime/core/tag.h>
#include <executorch/runtime/platform/assert.h>

Expand Down Expand Up @@ -42,6 +44,7 @@ class Scalar {
/*implicit*/ Scalar(double val) : tag(Tag::Double) {
v.as_double = val;
}
/*implicit*/ Scalar(float val) : Scalar((double)val) {}
/*implicit*/ Scalar(BFloat16 val) : Scalar((double)(float)val) {}
/*implicit*/ Scalar(Half val) : Scalar((double)(float)val) {}

Expand All @@ -65,31 +68,27 @@ class Scalar {
}

private:
int64_t toInt() const {
if (isIntegral(/*includeBool=*/false)) {
return v.as_int;
} else if (isBoolean()) {
return static_cast<int64_t>(v.as_bool);
} else {
ET_CHECK_MSG(false, "Scalar is not an int nor a Boolean.");
}
#define DEFINE_ACCESSOR(type, name) \
type to##name() const { \
if (Tag::Double == tag) { \
return static_cast<type>(v.as_double); \
} \
if (Tag::Int == tag) { \
return static_cast<type>(v.as_int); \
} \
if (Tag::Bool == tag) { \
return static_cast<type>(v.as_bool); \
} \
ET_CHECK_MSG(false, "Scalar Tag is not Double, Int, or Bool."); \
}

ET_FORALL_FLOAT_INT_BOOL_TYPES(DEFINE_ACCESSOR)
#undef DEFINE_ACCESSOR

double toFloatingPoint() const {
ET_CHECK_MSG(isFloatingPoint(), "Scalar is not a Double.");
return v.as_double;
}

double toDouble() const {
ET_CHECK_MSG(isFloatingPoint(), "Scalar is not a Double.");
return v.as_double;
}

bool toBool() const {
ET_CHECK_MSG(isBoolean(), "Scalar is not a Boolean.");
return v.as_bool;
}

Tag tag;
union v_t {
double as_double;
Expand All @@ -105,9 +104,7 @@ class Scalar {
return to##name(); \
}

ET_DEFINE_SCALAR_TO_METHOD(double, Double)
ET_DEFINE_SCALAR_TO_METHOD(int64_t, Int)
ET_DEFINE_SCALAR_TO_METHOD(bool, Bool)
ET_FORALL_FLOAT_INT_BOOL_TYPES(ET_DEFINE_SCALAR_TO_METHOD)
#undef ET_DEFINE_SCALAR_TO_METHOD

} // namespace etensor
Expand Down
13 changes: 13 additions & 0 deletions runtime/core/portable_type/scalar_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ struct alignas(1) Float8_e4m3fnuz {
_(uint32_t, UInt32) /* 28 */ \
_(uint64_t, UInt64) /* 29 */


#define ET_FORALL_FLOAT_INT_BOOL_TYPES(_) \
_(uint8_t, Byte) \
_(int8_t, Char) \
_(int16_t, Short) \
_(int, Int) \
_(int64_t, Long) \
_(::executorch::runtime::etensor::Half, Half) \
_(float, Float) \
_(double, Double) \
_(bool, Bool) \
_(::executorch::runtime::etensor::BFloat16, BFloat16)

/**
* Data types (dtypes) that can be used as element types in ETensors.
*/
Expand Down
10 changes: 10 additions & 0 deletions runtime/core/portable_type/test/scalar_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

#include <executorch/runtime/core/portable_type/scalar.h>
#include <executorch/runtime/core/portable_type/bfloat16.h>
#include <executorch/runtime/core/portable_type/half.h>
#include <executorch/test/utils/DeathTest.h>
#include <gtest/gtest.h>

Expand All @@ -15,8 +17,16 @@ using executorch::runtime::etensor::Scalar;
TEST(ScalarTest, ToScalarType) {
Scalar s_d((double)3.141);
EXPECT_EQ(s_d.to<double>(), 3.141);
EXPECT_EQ(s_d.to<float>(), (float)3.141);
EXPECT_EQ(s_d.to<::executorch::runtime::etensor::Half>(), (::executorch::runtime::etensor::Half)3.141);
EXPECT_EQ(s_d.to<::executorch::runtime::etensor::BFloat16>(), (::executorch::runtime::etensor::BFloat16)3.141);

Scalar s_i((int64_t)3);
EXPECT_EQ(s_i.to<int64_t>(), 3);
EXPECT_EQ(s_i.to<int32_t>(), 3);
EXPECT_EQ(s_i.to<int16_t>(), 3);
EXPECT_EQ(s_i.to<int8_t>(), 3);
EXPECT_EQ(s_i.to<bool>(), true);
Scalar s_b(true);
EXPECT_EQ(s_b.to<bool>(), true);
}
Expand Down
Loading