Skip to content

Commit 3b52cb1

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Fix flaky rand() tests. (#8340)
Summary: Adjusts the upper bound to prevent rounding to 1.0 when converting to lower-precision types. Reviewed By: kirklandsign Differential Revision: D69402222
1 parent 71b8b75 commit 3b52cb1

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

extension/tensor/tensor_ptr_maker.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,19 @@ TensorPtr rand_strided(
140140
std::vector<executorch::aten::StridesType> strides,
141141
executorch::aten::ScalarType type,
142142
executorch::aten::TensorShapeDynamism dynamism) {
143+
auto upper_bound = 1.0f;
144+
// Adjusts the upper bound to prevent rounding to 1.0 when converting to lower-precision types.
145+
if (type == executorch::aten::ScalarType::Half) {
146+
upper_bound -= static_cast<float>(std::numeric_limits<executorch::aten::Half>::epsilon()) / 2;
147+
} else if (type == executorch::aten::ScalarType::BFloat16) {
148+
upper_bound -= static_cast<float>(std::numeric_limits<executorch::aten::BFloat16>::epsilon()) / 2;
149+
}
143150
return random_strided(
144151
std::move(sizes),
145152
std::move(strides),
146153
type,
147154
dynamism,
148-
std::uniform_real_distribution<float>(0.0f, 1.0f));
155+
std::uniform_real_distribution<float>(0.0f, upper_bound));
149156
}
150157

151158
TensorPtr randn_strided(

0 commit comments

Comments
 (0)