Skip to content
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
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_scatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CTYPE>(value);
auto opt_val = utils::internal::check_overflow_scalar_cast<CTYPE>(value);
ET_KERNEL_CHECK(ctx, opt_val.has_value(), InvalidArgument, );
auto val = opt_val.value();
scatter_value_helper<CTYPE>(in, dim, index, val, out);
});

Expand Down
16 changes: 16 additions & 0 deletions kernels/test/op_scatter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include <executorch/kernels/test/FunctionHeaderWrapper.h> // Declares the operator
#include <executorch/kernels/test/ScalarOverflowTestMacros.h>
#include <executorch/kernels/test/TestUtil.h>
#include <executorch/kernels/test/supported_features.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
Expand Down Expand Up @@ -364,6 +365,19 @@ class OpScatterValueOutTest : public OperatorTest {
op_scatter_value_out(input, 2, index, value, out);
EXPECT_TENSOR_EQ(out, expected);
}

template <ScalarType DTYPE>
void expect_bad_scalar_value_dies(const Scalar& bad_value) {
TensorFactory<DTYPE> tf;
TensorFactory<ScalarType::Long> 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) {
Expand Down Expand Up @@ -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)
Loading