mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Harden unsupported tensor tests for sanitizers
libeigen/eigen!2321 Co-authored-by: Rasmus Munk Larsen <rlarsen@nvidia.com> Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
This commit is contained in:
@@ -892,6 +892,14 @@ std::string type_name() {
|
||||
return type_name(T());
|
||||
}
|
||||
|
||||
template <typename DataContainer>
|
||||
void setRandomDataInRange(DataContainer& data_container, typename DataContainer::Scalar min_value,
|
||||
typename DataContainer::Scalar max_value) {
|
||||
for (Eigen::Index i = 0; i < data_container.size(); ++i) {
|
||||
data_container.data()[i] = Eigen::internal::random<typename DataContainer::Scalar>(min_value, max_value);
|
||||
}
|
||||
}
|
||||
|
||||
using namespace Eigen;
|
||||
|
||||
/**
|
||||
|
||||
@@ -250,8 +250,13 @@ static void test_auto_resize() {
|
||||
static void test_compound_assign() {
|
||||
Tensor<int, 1> start_tensor(10);
|
||||
Tensor<int, 1> offset_tensor(10);
|
||||
start_tensor.setRandom();
|
||||
offset_tensor.setRandom();
|
||||
setRandomDataInRange(start_tensor, -1000, 1000);
|
||||
setRandomDataInRange(offset_tensor, -1000, 1000);
|
||||
|
||||
// Avoid division by zero in the final compound assignment check.
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
if (offset_tensor(i) == 0) offset_tensor(i) = 1;
|
||||
}
|
||||
|
||||
Tensor<int, 1> tensor = start_tensor;
|
||||
tensor += offset_tensor;
|
||||
|
||||
@@ -13,6 +13,16 @@
|
||||
using Eigen::internal::TensorBlockDescriptor;
|
||||
using Eigen::internal::TensorExecutor;
|
||||
|
||||
template <typename T, int NumDims, int Layout>
|
||||
static void setRandomForBinaryProduct(Tensor<T, NumDims, Layout>& tensor) {
|
||||
EIGEN_IF_CONSTEXPR((std::is_integral<T>::value && !std::is_same<T, bool>::value)) {
|
||||
setRandomDataInRange(tensor, T(-1000), T(1000));
|
||||
}
|
||||
else {
|
||||
tensor.setRandom();
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
// Utility functions to generate random tensors, blocks, and evaluate them.
|
||||
|
||||
@@ -220,8 +230,8 @@ template <typename T, int NumDims, int Layout>
|
||||
static void test_eval_tensor_binary_expr_block() {
|
||||
DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
|
||||
Tensor<T, NumDims, Layout> lhs(dims), rhs(dims);
|
||||
lhs.setRandom();
|
||||
rhs.setRandom();
|
||||
setRandomForBinaryProduct(lhs);
|
||||
setRandomForBinaryProduct(rhs);
|
||||
|
||||
VerifyBlockEvaluator<T, NumDims, Layout>(lhs * rhs, [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
|
||||
}
|
||||
|
||||
@@ -81,11 +81,15 @@ void test_unsigned_64bit() {
|
||||
}
|
||||
|
||||
void test_powers_32bit() {
|
||||
const int32_t int32_max = (std::numeric_limits<int32_t>::max)();
|
||||
for (int expon = 1; expon < 31; expon++) {
|
||||
int32_t div = (1 << expon);
|
||||
for (int num_expon = 0; num_expon < 32; num_expon++) {
|
||||
int32_t start_num = (1 << num_expon) - 100;
|
||||
int32_t end_num = (1 << num_expon) + 100;
|
||||
const int64_t pivot = int64_t(1) << num_expon;
|
||||
int32_t start_num = pivot > int32_max ? int32_max : static_cast<int32_t>(pivot);
|
||||
int32_t end_num = start_num;
|
||||
start_num = (std::max)(int32_t(0), start_num - 100);
|
||||
end_num = static_cast<int32_t>((std::min)(int64_t(int32_max), int64_t(end_num) + 100));
|
||||
if (start_num < 0) start_num = 0;
|
||||
for (int32_t num = start_num; num < end_num; num++) {
|
||||
Eigen::internal::TensorIntDivisor<int32_t> divider = Eigen::internal::TensorIntDivisor<int32_t>(div);
|
||||
|
||||
@@ -335,7 +335,7 @@ static void test_tensor_maps() {
|
||||
TensorMap<Tensor<const int, 4, DataLayout>> tensor_map_const(inputs, 2, 3, 5, 7);
|
||||
const TensorMap<Tensor<const int, 4, DataLayout>> tensor_map_const_const(inputs, 2, 3, 5, 7);
|
||||
|
||||
tensor_map.setRandom();
|
||||
setRandomDataInRange(tensor_map, -1000, 1000);
|
||||
array<ptrdiff_t, 2> reduction_axis;
|
||||
reduction_axis[0] = 1;
|
||||
reduction_axis[1] = 3;
|
||||
|
||||
@@ -45,9 +45,9 @@ static void test_simple_lvalue_ref() {
|
||||
|
||||
static void test_simple_rvalue_ref() {
|
||||
Tensor<int, 1> input1(6);
|
||||
input1.setRandom();
|
||||
setRandomDataInRange(input1, -1000, 1000);
|
||||
Tensor<int, 1> input2(6);
|
||||
input2.setRandom();
|
||||
setRandomDataInRange(input2, -1000, 1000);
|
||||
|
||||
TensorRef<const Tensor<int, 1>> ref3(input1 + input2);
|
||||
TensorRef<const Tensor<int, 1>> ref4 = input1 + input2;
|
||||
@@ -110,7 +110,7 @@ static void test_slice() {
|
||||
|
||||
static void test_ref_of_trace() {
|
||||
Tensor<int, 2> input(6, 6);
|
||||
input.setRandom();
|
||||
setRandomDataInRange(input, -1000, 1000);
|
||||
int trace = 0;
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
trace += input(i, i);
|
||||
|
||||
@@ -85,7 +85,7 @@ template <int DataLayout>
|
||||
static void test_tensor_maps() {
|
||||
int inputs[20];
|
||||
TensorMap<Tensor<int, 1, DataLayout> > tensor_map(inputs, 20);
|
||||
tensor_map.setRandom();
|
||||
setRandomDataInRange(tensor_map, -1000, 1000);
|
||||
|
||||
Tensor<int, 1, DataLayout> result = tensor_map.cumsum(0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user