Skip to content

op_clamp: add downcasting tests & fix #5798

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 10 additions & 6 deletions kernels/portable/cpu/op_clamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ Tensor& clamp_tensor_out(
ET_SWITCH_REALHB_TYPES(min_type, ctx, name, CTYPE_MIN, [&]() {
ET_SWITCH_REALHB_TYPES(max_type, ctx, name, CTYPE_MAX, [&]() {
ET_SWITCH_REALHB_TYPES(out_type, ctx, name, CTYPE_OUT, [&]() {
using CTYPE_MINMAX = typename torch::executor::
promote_types<CTYPE_MIN, CTYPE_MAX>::type;
using CTYPE = typename torch::executor::
promote_types<CTYPE_IN, CTYPE_MINMAX>::type;
apply_ternary_elementwise_fn<
CTYPE_IN,
CTYPE_MIN,
Expand All @@ -227,16 +231,16 @@ Tensor& clamp_tensor_out(
const CTYPE_IN val_in,
const CTYPE_MIN val_min,
const CTYPE_MAX val_max) {
CTYPE_OUT val_out = static_cast<CTYPE_OUT>(val_in);
CTYPE val_out = static_cast<CTYPE>(val_in);
if (has_min) {
val_out = utils::max_override(
val_out, static_cast<CTYPE_OUT>(val_min));
val_out =
utils::max_override(val_out, static_cast<CTYPE>(val_min));
}
if (has_max) {
val_out = utils::min_override(
val_out, static_cast<CTYPE_OUT>(val_max));
val_out =
utils::min_override(val_out, static_cast<CTYPE>(val_max));
}
return val_out;
return static_cast<CTYPE_OUT>(val_out);
},
in,
min,
Expand Down
48 changes: 48 additions & 0 deletions kernels/test/op_clamp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,51 @@ TEST_F(OpClampTensorOutTest, SmokeTest) {
op_clamp_tensor_out(in, min, max, out);
EXPECT_TENSOR_EQ(out, expected);
}

TEST_F(OpClampTensorOutTest, DowncastingSmokeTest) {
TensorFactory<ScalarType::Byte> tf_in;
TensorFactory<ScalarType::Short> tf_min;
TensorFactory<ScalarType::Int> tf_max;
TensorFactory<ScalarType::Char> tf_out;

Tensor in = tf_in.make({}, {5});
Tensor min = tf_min.make({}, {-129});
Tensor max = tf_max.make({}, {300});
Tensor out = tf_out.zeros({});
Tensor expected = tf_out.make({}, {5});

op_clamp_tensor_out(in, min, max, out);
EXPECT_TENSOR_EQ(out, expected);
}

TEST_F(OpClampTensorOutTest, DowncastingSmokeTest2) {
TensorFactory<ScalarType::Short> tf_in;
TensorFactory<ScalarType::Short> tf_min;
TensorFactory<ScalarType::Int> tf_max;
TensorFactory<ScalarType::Char> tf_out;

Tensor in = tf_in.make({}, {301});
Tensor min = tf_min.make({}, {-129});
Tensor max = tf_max.make({}, {300});
Tensor out = tf_out.zeros({});
Tensor expected = tf_out.make({}, {44});

op_clamp_tensor_out(in, min, max, out);
EXPECT_TENSOR_EQ(out, expected);
}

TEST_F(OpClampTensorOutTest, DowncastingSmokeTest3) {
TensorFactory<ScalarType::Short> tf_in;
TensorFactory<ScalarType::Short> tf_min;
TensorFactory<ScalarType::Int> tf_max;
TensorFactory<ScalarType::Char> tf_out;

Tensor in = tf_in.make({}, {45});
Tensor min = tf_min.make({}, {-129});
Tensor max = tf_max.make({}, {300});
Tensor out = tf_out.zeros({});
Tensor expected = tf_out.make({}, {45});

op_clamp_tensor_out(in, min, max, out);
EXPECT_TENSOR_EQ(out, expected);
}
Loading