diff --git a/kernels/portable/cpu/op_full.cpp b/kernels/portable/cpu/op_full.cpp index 83ffcad45a..b83637f2b9 100644 --- a/kernels/portable/cpu/op_full.cpp +++ b/kernels/portable/cpu/op_full.cpp @@ -37,7 +37,10 @@ Tensor& full_out( constexpr auto name = "full.out"; ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, name, CTYPE_OUT, [&] { - CTYPE_OUT val_casted = utils::scalar_to(fill_value); + auto opt_val_casted = + utils::internal::check_overflow_scalar_cast(fill_value); + ET_KERNEL_CHECK(ctx, opt_val_casted.has_value(), InvalidArgument, ); + auto val_casted = opt_val_casted.value(); auto data_out = out.mutable_data_ptr(); for (const auto i : c10::irange(out.numel())) { data_out[i] = val_casted; diff --git a/kernels/portable/cpu/op_full_like.cpp b/kernels/portable/cpu/op_full_like.cpp index 7671cd61ea..0e263fb9c1 100644 --- a/kernels/portable/cpu/op_full_like.cpp +++ b/kernels/portable/cpu/op_full_like.cpp @@ -48,23 +48,19 @@ Tensor& full_like_out( out, "Failed to resize output tensor."); - ScalarType val_type = utils::get_scalar_dtype(fill_value); ScalarType out_type = out.scalar_type(); constexpr auto name = "scalar_tensor.out"; - ET_SWITCH_REALB_TYPES(val_type, ctx, name, CTYPE_VAL, [&] { - CTYPE_VAL val; - ET_KERNEL_CHECK( - ctx, utils::extract_scalar(fill_value, &val), InvalidArgument, ); - - ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, name, CTYPE_OUT, [&] { - CTYPE_OUT val_casted = static_cast(val); - auto data_out = out.mutable_data_ptr(); - for (const auto i : c10::irange(out.numel())) { - data_out[i] = val_casted; - } - }); + ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, name, CTYPE_OUT, [&] { + auto opt_val_casted = + utils::internal::check_overflow_scalar_cast(fill_value); + ET_KERNEL_CHECK(ctx, opt_val_casted.has_value(), InvalidArgument, ); + auto val_casted = opt_val_casted.value(); + auto data_out = out.mutable_data_ptr(); + for (const auto i : c10::irange(out.numel())) { + data_out[i] = val_casted; + } }); return out; diff --git a/kernels/portable/cpu/op_scalar_tensor.cpp b/kernels/portable/cpu/op_scalar_tensor.cpp index e111a9ac86..bff4ecc318 100644 --- a/kernels/portable/cpu/op_scalar_tensor.cpp +++ b/kernels/portable/cpu/op_scalar_tensor.cpp @@ -24,17 +24,11 @@ scalar_tensor_out(KernelRuntimeContext& ctx, const Scalar& s, Tensor& out) { constexpr auto name = "scalar_tensor.out"; - if (s.isFloatingPoint() && - executorch::runtime::isIntegralType(out_type, false)) { - ET_SWITCH_INT_TYPES(out_type, ctx, name, CTYPE, [&]() { - out.mutable_data_ptr()[0] = - static_cast(utils::scalar_to(s)); - }); - } else { - ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, name, CTYPE, [&]() { - out.mutable_data_ptr()[0] = utils::scalar_to(s); - }); - } + ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, name, CTYPE, [&]() { + auto opt_val_casted = utils::internal::check_overflow_scalar_cast(s); + ET_KERNEL_CHECK(ctx, opt_val_casted.has_value(), InvalidArgument, ); + out.mutable_data_ptr()[0] = opt_val_casted.value(); + }); return out; } diff --git a/kernels/test/op_full_like_test.cpp b/kernels/test/op_full_like_test.cpp index c0b9dcc410..6e7692f534 100644 --- a/kernels/test/op_full_like_test.cpp +++ b/kernels/test/op_full_like_test.cpp @@ -7,6 +7,7 @@ */ #include // Declares the operator +#include #include #include #include @@ -65,6 +66,18 @@ class OpFullLikeTest : public OperatorTest { ET_EXPECT_KERNEL_FAILURE( context_, op_full_like_out(in, value, memory_format, out)); } + + template + void expect_bad_scalar_value_dies(const Scalar& bad_value) { + TensorFactory tf; + const std::vector sizes = {2, 2}; + Tensor in = tf.zeros(sizes); + Tensor out = tf.zeros(sizes); + optional memory_format; + + ET_EXPECT_KERNEL_FAILURE( + context_, op_full_like_out(in, bad_value, memory_format, out)); + } }; template <> @@ -209,3 +222,5 @@ TEST_F(OpFullLikeTest, HalfSupport) { op_full_like_out(in, INFINITY, memory_format, out); EXPECT_TENSOR_CLOSE(out, tf.full({2, 3}, INFINITY)); } + +GENERATE_SCALAR_OVERFLOW_TESTS(OpFullLikeTest) diff --git a/kernels/test/op_full_test.cpp b/kernels/test/op_full_test.cpp index 9312967908..35115dc7ed 100644 --- a/kernels/test/op_full_test.cpp +++ b/kernels/test/op_full_test.cpp @@ -7,6 +7,7 @@ */ #include // Declares the operator +#include #include #include #include @@ -59,6 +60,17 @@ class OpFullOutTest : public OperatorTest { op_full_out(aref, 1.0, out); EXPECT_TENSOR_EQ(out, tf.ones(size_int32_t)); } + + template + void expect_bad_scalar_value_dies(const Scalar& bad_value) { + TensorFactory tf; + std::vector sizes = {2, 2}; + std::vector sizes_int64_t(sizes.begin(), sizes.end()); + auto aref = IntArrayRef(sizes_int64_t.data(), sizes_int64_t.size()); + Tensor out = tf.zeros(sizes); + + ET_EXPECT_KERNEL_FAILURE(context_, op_full_out(aref, bad_value, out)); + } }; #define GENERATE_TEST(_, DTYPE) \ @@ -72,20 +84,7 @@ class OpFullOutTest : public OperatorTest { ET_FORALL_REALHBF16_TYPES(GENERATE_TEST) -TEST_F(OpFullOutTest, ValueOverflow) { - if (torch::executor::testing::SupportedFeatures::get()->is_aten) { - GTEST_SKIP() << "ATen kernel doesn't handle overflow"; - } - TensorFactory tf; - - std::vector sizes_int64_t_vec = {2, 3}; - std::vector sizes_in32_t_vec = {2, 3}; - auto sizes = IntArrayRef(sizes_int64_t_vec.data(), sizes_int64_t_vec.size()); - - Tensor out = tf.zeros(sizes_in32_t_vec); - - op_full_out(sizes, 1000, out); -} +GENERATE_SCALAR_OVERFLOW_TESTS(OpFullOutTest) TEST_F(OpFullOutTest, HalfSupport) { TensorFactory tf; diff --git a/kernels/test/op_scalar_tensor_test.cpp b/kernels/test/op_scalar_tensor_test.cpp index db4816e884..0be6f395eb 100644 --- a/kernels/test/op_scalar_tensor_test.cpp +++ b/kernels/test/op_scalar_tensor_test.cpp @@ -7,6 +7,7 @@ */ #include // Declares the operator +#include #include #include #include @@ -71,6 +72,14 @@ class OpScalarTensorOutTest : public OperatorTest { ET_EXPECT_KERNEL_FAILURE(context_, op_scalar_tensor_out(value, out)); } + + template + void expect_bad_scalar_value_dies(const Scalar& bad_value) { + TensorFactory tf; + Tensor out = tf.zeros({}); + + ET_EXPECT_KERNEL_FAILURE(context_, op_scalar_tensor_out(bad_value, out)); + } }; #define GENERATE_TEST_0D(ctype, dtype) \ @@ -131,3 +140,5 @@ TEST_F(OpScalarTensorOutTest, HalfSupport) { op_scalar_tensor_out(INFINITY, out); EXPECT_TENSOR_CLOSE(out, tf.make({}, {INFINITY})); } + +GENERATE_SCALAR_OVERFLOW_TESTS(OpScalarTensorOutTest)