Skip to content

Commit 16c4111

Browse files
authored
Fix flaky rand() tests. (#8340)
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 57891b7 commit 16c4111

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

extension/tensor/tensor_ptr_maker.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,22 @@ 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
145+
// lower-precision types.
146+
if (type == executorch::aten::ScalarType::Half) {
147+
upper_bound -=
148+
float(std::numeric_limits<executorch::aten::Half>::epsilon()) / 2;
149+
} else if (type == executorch::aten::ScalarType::BFloat16) {
150+
upper_bound -=
151+
float(std::numeric_limits<executorch::aten::BFloat16>::epsilon()) / 2;
152+
}
143153
return random_strided(
144154
std::move(sizes),
145155
std::move(strides),
146156
type,
147157
dynamism,
148-
std::uniform_real_distribution<float>(0.0f, 1.0f));
158+
std::uniform_real_distribution<float>(0.0f, upper_bound));
149159
}
150160

151161
TensorPtr randn_strided(

0 commit comments

Comments
 (0)