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
5 changes: 4 additions & 1 deletion kernels/portable/cpu/op_constant_pad_nd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CTYPE>(value);
auto opt_value_casted =
utils::internal::check_overflow_scalar_cast<CTYPE>(value);
ET_KERNEL_CHECK(ctx, opt_value_casted.has_value(), InvalidArgument, );
auto value_casted = opt_value_casted.value();
constant_pad_nd_out_impl<CTYPE>(in, pad, value_casted, out);
});

Expand Down
18 changes: 18 additions & 0 deletions kernels/test/op_constant_pad_nd_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 @@ -347,6 +348,21 @@ class OpConstantPadNDOutTest : public OperatorTest {
op_constant_pad_nd_out(self, padding_ref, 7, out);
EXPECT_TENSOR_CLOSE(out, expected);
}

template <ScalarType DTYPE>
void expect_bad_scalar_value_dies(const Scalar& bad_value) {
TensorFactory<DTYPE> tf;
const std::vector<int32_t> sizes = {2, 2};
const std::vector<int32_t> sizes_out = {2, 4};
const std::vector<int64_t> 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) {
Expand Down Expand Up @@ -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)
Loading