From d380c23b2cc0b02e10819e779c73cde2c62603b2 Mon Sep 17 00:00:00 2001 From: Eugene Zhulenev Date: Mon, 14 Oct 2019 14:31:59 -0700 Subject: Block evaluation for TensorGenerator/TensorReverse/TensorShuffling --- unsupported/test/cxx11_tensor_executor.cpp | 131 +++++++++++++++++------------ 1 file changed, 77 insertions(+), 54 deletions(-) (limited to 'unsupported/test/cxx11_tensor_executor.cpp') diff --git a/unsupported/test/cxx11_tensor_executor.cpp b/unsupported/test/cxx11_tensor_executor.cpp index 66f932746..dd68ddf17 100644 --- a/unsupported/test/cxx11_tensor_executor.cpp +++ b/unsupported/test/cxx11_tensor_executor.cpp @@ -21,6 +21,30 @@ using Eigen::internal::TiledEvaluation; // A set of tests to verify that different TensorExecutor strategies yields the // same results for all the ops, supporting tiled evaluation. +// Default assignment that does no use block evaluation or vectorization. +// We assume that default coefficient evaluation is well tested and correct. +template +static void DefaultAssign(Dst& dst, Expr expr) { + using Assign = Eigen::TensorAssignOp; + using Executor = + Eigen::internal::TensorExecutor; + + Executor::run(Assign(dst, expr), DefaultDevice()); +} + +// Assignment with specified device and tiling strategy. +template +static void DeviceAssign(Device& d, Dst& dst, Expr expr) { + using Assign = Eigen::TensorAssignOp; + using Executor = Eigen::internal::TensorExecutor; + + Executor::run(Assign(dst, expr), d); +} + template static array RandomDims(int min_dim = 1, int max_dim = 20) { array dims; @@ -222,30 +246,32 @@ static void test_execute_shuffle_rvalue(Device d) Tensor src(dims); src.setRandom(); - // Create a random dimension re-ordering/shuffle. - std::vector shuffle; - for (int i = 0; i < NumDims; ++i) shuffle.push_back(i); - std::shuffle(shuffle.begin(), shuffle.end(), std::mt19937()); + DSizes shuffle; + for (int i = 0; i < NumDims; ++i) shuffle[i] = i; - const auto expr = src.shuffle(shuffle); + // Test all possible shuffle permutations. + do { + DSizes shuffled_dims; + for (int i = 0; i < NumDims; ++i) { + shuffled_dims[i] = dims[shuffle[i]]; + } - // We assume that shuffling on a default device is tested and correct, so - // we can rely on it to verify correctness of tensor executor and tiling. - Tensor golden; - golden = expr; + const auto expr = src.shuffle(shuffle); - // Now do the shuffling using configured tensor executor. - Tensor dst(golden.dimensions()); + // We assume that shuffling on a default device is tested and correct, so + // we can rely on it to verify correctness of tensor executor and tiling. + Tensor golden(shuffled_dims); + DefaultAssign(golden, expr); - using Assign = TensorAssignOp; - using Executor = - internal::TensorExecutor; + // Now do the shuffling using configured tensor executor. + Tensor dst(shuffled_dims); + DeviceAssign(d, dst, expr); - Executor::run(Assign(dst, expr), d); + for (Index i = 0; i < dst.dimensions().TotalSize(); ++i) { + VERIFY_IS_EQUAL(dst.coeff(i), golden.coeff(i)); + } - for (Index i = 0; i < dst.dimensions().TotalSize(); ++i) { - VERIFY_IS_EQUAL(dst.coeff(i), golden.coeff(i)); - } + } while (std::next_permutation(&shuffle[0], &shuffle[0] + NumDims)); } template src(dims); src.setRandom(); - // Create a random dimension re-ordering/shuffle. - std::vector shuffle; - for (int i = 0; i < NumDims; ++i) shuffle.push_back(i); - std::shuffle(shuffle.begin(), shuffle.end(), std::mt19937()); + DSizes shuffle; + for (int i = 0; i < NumDims; ++i) shuffle[i] = i; - array shuffled_dims; - for (int i = 0; i < NumDims; ++i) shuffled_dims[shuffle[i]] = dims[i]; + // Test all possible shuffle permutations. + do { + DSizes shuffled_dims; + for (int i = 0; i < NumDims; ++i) shuffled_dims[shuffle[i]] = dims[i]; - // We assume that shuffling on a default device is tested and correct, so - // we can rely on it to verify correctness of tensor executor and tiling. - Tensor golden(shuffled_dims); - golden.shuffle(shuffle) = src; + // We assume that shuffling on a default device is tested and correct, so + // we can rely on it to verify correctness of tensor executor and tiling. + Tensor golden(shuffled_dims); + auto golden_shuffle = golden.shuffle(shuffle); + DefaultAssign(golden_shuffle, src); - // Now do the shuffling using configured tensor executor. - Tensor dst(shuffled_dims); + // Now do the shuffling using configured tensor executor. + Tensor dst(shuffled_dims); + auto dst_shuffle = dst.shuffle(shuffle); + DeviceAssign(d, dst_shuffle, src); - auto expr = dst.shuffle(shuffle); - - using Assign = TensorAssignOp; - using Executor = - internal::TensorExecutor; - - Executor::run(Assign(expr, src), d); + for (Index i = 0; i < dst.dimensions().TotalSize(); ++i) { + VERIFY_IS_EQUAL(dst.coeff(i), golden.coeff(i)); + } - for (Index i = 0; i < dst.dimensions().TotalSize(); ++i) { - VERIFY_IS_EQUAL(dst.coeff(i), golden.coeff(i)); - } + } while (std::next_permutation(&shuffle[0], &shuffle[0] + NumDims)); } template