From 8fbd71f648b3250a098922172c083a859f5c09d6 Mon Sep 17 00:00:00 2001 From: Manuel Candales Date: Fri, 27 Jun 2025 07:28:00 -0700 Subject: [PATCH 1/6] [ET][Portable] Check scalar overflow: op_full Pull Request resolved: https://github.com/pytorch/executorch/pull/12014 ghstack-source-id: 293086873 @exported-using-ghexport Differential Revision: [D77386635](https://our.internmc.facebook.com/intern/diff/D77386635/) --- kernels/portable/cpu/op_full.cpp | 5 ++++- kernels/test/op_full_test.cpp | 27 +++++++++++++-------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/kernels/portable/cpu/op_full.cpp b/kernels/portable/cpu/op_full.cpp index 83ffcad45a6..b83637f2b91 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/test/op_full_test.cpp b/kernels/test/op_full_test.cpp index 93129679087..35115dc7ed6 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; From 1096366d1b09b73ec31ae17c0d5b0f062bb04d1b Mon Sep 17 00:00:00 2001 From: Manuel Candales Date: Fri, 27 Jun 2025 07:28:01 -0700 Subject: [PATCH 2/6] [ET][Portable] Check scalar overflow: op_full_like Pull Request resolved: https://github.com/pytorch/executorch/pull/12015 ghstack-source-id: 293086874 @exported-using-ghexport Differential Revision: [D77386637](https://our.internmc.facebook.com/intern/diff/D77386637/) --- kernels/portable/cpu/op_full_like.cpp | 22 +++++++++------------- kernels/test/op_full_like_test.cpp | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/kernels/portable/cpu/op_full_like.cpp b/kernels/portable/cpu/op_full_like.cpp index 7671cd61ea9..0e263fb9c10 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/test/op_full_like_test.cpp b/kernels/test/op_full_like_test.cpp index c0b9dcc4107..6e7692f5347 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) From 489838aced52d2395b45d38445105445b03868ab Mon Sep 17 00:00:00 2001 From: Manuel Candales Date: Fri, 27 Jun 2025 07:28:01 -0700 Subject: [PATCH 3/6] [ET][Portable] Check scalar overflow: op_scalar_tensor Pull Request resolved: https://github.com/pytorch/executorch/pull/12025 ghstack-source-id: 293086871 @exported-using-ghexport Differential Revision: [D77401092](https://our.internmc.facebook.com/intern/diff/D77401092/) --- kernels/portable/cpu/op_scalar_tensor.cpp | 16 +++++----------- kernels/test/op_scalar_tensor_test.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/kernels/portable/cpu/op_scalar_tensor.cpp b/kernels/portable/cpu/op_scalar_tensor.cpp index e111a9ac869..bff4ecc318c 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_scalar_tensor_test.cpp b/kernels/test/op_scalar_tensor_test.cpp index db4816e8847..0be6f395eb0 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) From 502b1ad003e337b13e67ac3d4471b8320b9631f7 Mon Sep 17 00:00:00 2001 From: Manuel Candales Date: Fri, 27 Jun 2025 07:28:02 -0700 Subject: [PATCH 4/6] [ET][Portable] Check scalar overflow: op_constant_pad_nd Pull Request resolved: https://github.com/pytorch/executorch/pull/12026 ghstack-source-id: 293086872 @exported-using-ghexport Differential Revision: [D77401089](https://our.internmc.facebook.com/intern/diff/D77401089/) --- kernels/portable/cpu/op_constant_pad_nd.cpp | 5 ++++- kernels/test/op_constant_pad_nd_test.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/kernels/portable/cpu/op_constant_pad_nd.cpp b/kernels/portable/cpu/op_constant_pad_nd.cpp index 6e643e1b945..be3962e018c 100644 --- a/kernels/portable/cpu/op_constant_pad_nd.cpp +++ b/kernels/portable/cpu/op_constant_pad_nd.cpp @@ -185,7 +185,10 @@ Tensor& constant_pad_nd_out( ScalarType in_type = in.scalar_type(); ET_SWITCH_REALHBBF16_TYPES(in_type, ctx, "constant_pad_nd.out", CTYPE, [&]() { - const CTYPE value_casted = utils::scalar_to(value); + auto opt_value_casted = + utils::internal::check_overflow_scalar_cast(value); + ET_KERNEL_CHECK(ctx, opt_value_casted.has_value(), InvalidArgument, ); + auto value_casted = opt_value_casted.value(); constant_pad_nd_out_impl(in, pad, value_casted, out); }); diff --git a/kernels/test/op_constant_pad_nd_test.cpp b/kernels/test/op_constant_pad_nd_test.cpp index 88bee1d0ad9..7f44068d9cb 100644 --- a/kernels/test/op_constant_pad_nd_test.cpp +++ b/kernels/test/op_constant_pad_nd_test.cpp @@ -7,6 +7,7 @@ */ #include // Declares the operator +#include #include #include #include @@ -347,6 +348,21 @@ class OpConstantPadNDOutTest : public OperatorTest { op_constant_pad_nd_out(self, padding_ref, 7, out); EXPECT_TENSOR_CLOSE(out, expected); } + + template + void expect_bad_scalar_value_dies(const Scalar& bad_value) { + TensorFactory tf; + const std::vector sizes = {2, 2}; + const std::vector sizes_out = {2, 4}; + const std::vector padding = {1, 1}; + + IntArrayRef padding_ref = IntArrayRef(padding.data(), padding.size()); + Tensor self = tf.ones(sizes); + Tensor out = tf.zeros(sizes_out); + + ET_EXPECT_KERNEL_FAILURE( + context_, op_constant_pad_nd_out(self, padding_ref, bad_value, out)); + } }; TEST_F(OpConstantPadNDOutTest, TestPadDim2) { @@ -465,3 +481,5 @@ TEST_F(OpConstantPadNDOutTest, IncorrectOutputShapeFail) { ET_EXPECT_KERNEL_FAILURE( context_, op_constant_pad_nd_out(self, padding_ref, 0, out)); } + +GENERATE_SCALAR_OVERFLOW_TESTS(OpConstantPadNDOutTest) From 447376da34c8cfcab1307935a1220c6efcc8cca6 Mon Sep 17 00:00:00 2001 From: Manuel Candales Date: Fri, 27 Jun 2025 07:28:03 -0700 Subject: [PATCH 5/6] [ET][Portable] Check scalar overflow: op_scatter Pull Request resolved: https://github.com/pytorch/executorch/pull/12028 ghstack-source-id: 293086870 @exported-using-ghexport Differential Revision: [D77401093](https://our.internmc.facebook.com/intern/diff/D77401093/) --- kernels/portable/cpu/op_scatter.cpp | 4 +++- kernels/test/op_scatter_test.cpp | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/kernels/portable/cpu/op_scatter.cpp b/kernels/portable/cpu/op_scatter.cpp index 7de0ec4d5f9..965afbb4b66 100644 --- a/kernels/portable/cpu/op_scatter.cpp +++ b/kernels/portable/cpu/op_scatter.cpp @@ -154,7 +154,9 @@ Tensor& scatter_value_out( constexpr auto name = "scatter.value_out"; ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, name, CTYPE, [&]() { - const CTYPE val = utils::scalar_to(value); + auto opt_val = utils::internal::check_overflow_scalar_cast(value); + ET_KERNEL_CHECK(ctx, opt_val.has_value(), InvalidArgument, ); + auto val = opt_val.value(); scatter_value_helper(in, dim, index, val, out); }); diff --git a/kernels/test/op_scatter_test.cpp b/kernels/test/op_scatter_test.cpp index 0e55aadaeda..dac9017d188 100644 --- a/kernels/test/op_scatter_test.cpp +++ b/kernels/test/op_scatter_test.cpp @@ -7,6 +7,7 @@ */ #include // Declares the operator +#include #include #include #include @@ -364,6 +365,19 @@ class OpScatterValueOutTest : public OperatorTest { op_scatter_value_out(input, 2, index, value, out); EXPECT_TENSOR_EQ(out, expected); } + + template + void expect_bad_scalar_value_dies(const Scalar& bad_value) { + TensorFactory tf; + TensorFactory tf_index; + + Tensor self = tf.ones({2, 2}); + Tensor index = tf_index.zeros({2, 2}); + Tensor out = tf.zeros({2, 2}); + + ET_EXPECT_KERNEL_FAILURE( + context_, op_scatter_value_out(self, 0, index, bad_value, out)); + } }; TEST_F(OpScatterSrcOutTest, AllValidInputOutputSupport) { @@ -652,3 +666,5 @@ TEST_F(OpScatterSrcOutTest, InvalidOneDimInputAndZeroDimIndex) { ET_EXPECT_KERNEL_FAILURE( context_, op_scatter_src_out(self, 0, index, src, out)); } + +GENERATE_SCALAR_OVERFLOW_TESTS(OpScatterValueOutTest) From 057ba65b3d422ef1b13b72534630ac5b25843942 Mon Sep 17 00:00:00 2001 From: Manuel Candales Date: Fri, 27 Jun 2025 07:28:04 -0700 Subject: [PATCH 6/6] [ET][Portable] Check scalar overflow: op_hardtanh Pull Request resolved: https://github.com/pytorch/executorch/pull/12029 ghstack-source-id: 293086875 @exported-using-ghexport Differential Revision: [D77401090](https://our.internmc.facebook.com/intern/diff/D77401090/) --- kernels/portable/cpu/op_hardtanh.cpp | 11 +++++++++-- kernels/test/op_hardtanh_test.cpp | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/kernels/portable/cpu/op_hardtanh.cpp b/kernels/portable/cpu/op_hardtanh.cpp index 8ec73b07856..65411d5f6b0 100644 --- a/kernels/portable/cpu/op_hardtanh.cpp +++ b/kernels/portable/cpu/op_hardtanh.cpp @@ -45,8 +45,15 @@ Tensor& hardtanh_out( ET_KERNEL_CHECK(ctx, in_type == out_type, InvalidArgument, out); ET_SWITCH_REALHBF16_TYPES(in_type, ctx, "hardtanh.out", CTYPE, [&]() { - const CTYPE min_casted = utils::scalar_to(min); - const CTYPE max_casted = utils::scalar_to(max); + auto opt_min_casted = + utils::internal::check_overflow_scalar_cast(min); + ET_KERNEL_CHECK(ctx, opt_min_casted.has_value(), InvalidArgument, ); + auto min_casted = opt_min_casted.value(); + + auto opt_max_casted = + utils::internal::check_overflow_scalar_cast(max); + ET_KERNEL_CHECK(ctx, opt_max_casted.has_value(), InvalidArgument, ); + auto max_casted = opt_max_casted.value(); apply_unary_map_fn( [min_casted, max_casted](const CTYPE val_in) { diff --git a/kernels/test/op_hardtanh_test.cpp b/kernels/test/op_hardtanh_test.cpp index 72d09063d3e..38b0eeea40f 100644 --- a/kernels/test/op_hardtanh_test.cpp +++ b/kernels/test/op_hardtanh_test.cpp @@ -7,6 +7,7 @@ */ #include // Declares the operator +#include #include #include #include @@ -51,6 +52,21 @@ class OpHardTanhTest : public OperatorTest { EXPECT_TENSOR_EQ(out, ret); EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {lower_bound, 0, 1, 2})); } + + template + void expect_bad_scalar_value_dies(const Scalar& bad_value) { + TensorFactory tf; + Tensor in = tf.ones({2, 2}); + Tensor out = tf.zeros({2, 2}); + + // Test overflow for min parameter (using valid max) + ET_EXPECT_KERNEL_FAILURE( + context_, op_hardtanh_out(in, bad_value, 1.0, out)); + + // Test overflow for max parameter (using valid min) + ET_EXPECT_KERNEL_FAILURE( + context_, op_hardtanh_out(in, -1.0, bad_value, out)); + } }; TEST_F(OpHardTanhTest, SanityCheck) { @@ -58,3 +74,5 @@ TEST_F(OpHardTanhTest, SanityCheck) { ET_FORALL_REALHBF16_TYPES(TEST_ENTRY); #undef TEST_ENTRY } + +GENERATE_SCALAR_OVERFLOW_TESTS(OpHardTanhTest)