10 #include <Eigen/CXX11/Tensor>
19 template <
int NumDims>
22 for (
int i = 0;
i < NumDims; ++
i) {
23 dims[
i] = internal::random<Index>(
min,
max);
30 template <
int NumDims>
37 template <
int Layout,
int NumDims>
45 for (
int i = 0;
i < NumDims; ++
i) {
52 for (
int i = 0;
i < NumDims; ++
i) {
61 template <
int Layout,
int NumDims>
64 using BlockMapper = internal::TensorBlockMapper<NumDims, Layout, Index>;
65 BlockMapper block_mapper(dims,
66 {internal::TensorBlockShapeType::kSkewedInnerDims,
67 internal::random<size_t>(1, dims.
TotalSize()),
70 Index total_blocks = block_mapper.blockCount();
71 Index block_index = internal::random<Index>(0, total_blocks - 1);
72 auto block = block_mapper.blockDescriptor(block_index);
75 auto strides = internal::strides<Layout>(dims);
80 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
81 for (
int i = NumDims - 1;
i > 0; --
i) {
86 if (NumDims > 0)
offsets[0] = index;
88 for (
int i = 0;
i < NumDims - 1; ++
i) {
93 if (NumDims > 0)
offsets[NumDims - 1] = index;
99 template <
int NumDims>
108 Eigen::IndexList<Index, Eigen::type2index<1>>
ret;
113 Eigen::IndexList<Eigen::type2index<1>,
Index>
ret;
122 template <
typename T,
int NumDims,
int Layout,
typename Expression,
123 typename GenBlockParams>
129 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
130 TensorBlockScratch scratch(
d);
134 eval.evalSubExprsIfNeeded(
nullptr);
144 if (internal::random<bool>()) {
147 for (
int i = 0;
i < NumDims; ++
i) {
148 Index extent = internal::random<Index>(0, 5);
156 if (internal::random<bool>()) {
157 block_params.
desc.template AddDestinationBuffer<Layout>(
161 const bool root_of_expr = internal::random<bool>();
162 auto tensor_block =
eval.block(block_params.
desc, scratch, root_of_expr);
176 auto b_expr = tensor_block.expr();
181 using BlockExecutor =
TensorExecutor<
const BlockAssign, Device,
false,
187 tensor_block.cleanup();
191 auto s_expr = expr.slice(block_params.
offsets, block_params.
sizes);
195 using SliceExecutor =
TensorExecutor<
const SliceAssign, Device,
false,
207 template <
typename T,
int NumDims,
int Layout>
214 VerifyBlockEvaluator<T, NumDims, Layout>(
215 input, [&dims]() {
return RandomBlock<Layout>(dims, 1, 10); });
218 template <
typename T,
int NumDims,
int Layout>
224 VerifyBlockEvaluator<T, NumDims, Layout>(
225 input.abs(), [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
228 template <
typename T,
int NumDims,
int Layout>
235 VerifyBlockEvaluator<T, NumDims, Layout>(
236 lhs * rhs, [&dims]() {
return RandomBlock<Layout>(dims, 1, 10); });
239 template <
typename T,
int NumDims,
int Layout>
246 VerifyBlockEvaluator<T, NumDims, Layout>(
247 (lhs.square() + rhs.square()).sqrt(),
248 [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
251 template <
typename T,
int NumDims,
int Layout>
260 for (
int i = 0;
i < NumDims; ++
i) bcasted_dims[
i] = dims[
i] * bcast[
i];
262 VerifyBlockEvaluator<T, NumDims, Layout>(
263 input.broadcast(bcast),
264 [&bcasted_dims]() { return SkewedInnerBlock<Layout>(bcasted_dims); });
266 VerifyBlockEvaluator<T, NumDims, Layout>(
267 input.broadcast(bcast),
268 [&bcasted_dims]() { return RandomBlock<Layout>(bcasted_dims, 5, 10); });
270 VerifyBlockEvaluator<T, NumDims, Layout>(
271 input.broadcast(bcast),
272 [&bcasted_dims]() { return FixedSizeBlock(bcasted_dims); });
276 VerifyBlockEvaluator<T, NumDims, Layout>(
277 input.broadcast(bcast) * input.abs().broadcast(bcast),
278 [&bcasted_dims]() { return SkewedInnerBlock<Layout>(bcasted_dims); });
281 template <
typename T,
int NumDims,
int Layout>
286 std::shuffle(&shuffled[0], &shuffled[NumDims - 1], std::mt19937(
g_seed));
291 VerifyBlockEvaluator<T, NumDims, Layout>(
293 [&shuffled]() { return RandomBlock<Layout>(shuffled, 1, 10); });
295 VerifyBlockEvaluator<T, NumDims, Layout>(
297 [&shuffled]() { return SkewedInnerBlock<Layout>(shuffled); });
300 template <
typename T,
int NumDims,
int Layout>
306 VerifyBlockEvaluator<T, NumDims, Layout>(
307 input.template cast<int>().template cast<T>(),
308 [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
311 template <
typename T,
int NumDims,
int Layout>
321 VerifyBlockEvaluator<T, NumDims, Layout>(cond.select(lhs, rhs), [&dims]() {
322 return RandomBlock<Layout>(dims, 1, 20);
326 template <
typename T,
int NumDims,
int Layout>
328 const int inner_dim = Layout ==
static_cast<int>(
ColMajor) ? 0 : NumDims - 1;
337 for (
int i = 0;
i < NumDims; ++
i) {
338 paddings[
i] = std::make_pair(pad_before[
i], pad_after[
i]);
342 if (internal::random<bool>()) {
343 pad_before[inner_dim] = 0;
344 pad_after[inner_dim] = 0;
345 paddings[inner_dim] = std::make_pair(0, 0);
349 for (
int i = 0;
i < NumDims; ++
i) {
350 padded_dims[
i] = dims[
i] + pad_before[
i] + pad_after[
i];
353 VerifyBlockEvaluator<T, NumDims, Layout>(
355 [&padded_dims]() { return FixedSizeBlock(padded_dims); });
357 VerifyBlockEvaluator<T, NumDims, Layout>(
359 [&padded_dims]() { return RandomBlock<Layout>(padded_dims, 1, 10); });
361 VerifyBlockEvaluator<T, NumDims, Layout>(
363 [&padded_dims]() { return SkewedInnerBlock<Layout>(padded_dims); });
366 template <
typename T,
int NumDims,
int Layout>
372 Index chip_dim = internal::random<int>(0, NumDims - 1);
373 Index chip_offset = internal::random<Index>(0, dims[chip_dim] - 2);
376 for (
Index i = 0;
i < chip_dim; ++
i) {
377 chipped_dims[
i] = dims[
i];
379 for (
Index i = chip_dim + 1;
i < NumDims; ++
i) {
380 chipped_dims[
i - 1] = dims[
i];
385 input.
chip(chip_offset, chip_dim),
389 input.
chip(chip_offset, chip_dim),
390 [&chipped_dims]() {
return RandomBlock<Layout>(chipped_dims, 1, 10); });
394 input.abs().
chip(chip_offset, chip_dim),
398 input.abs().
chip(chip_offset, chip_dim),
399 [&chipped_dims]() {
return RandomBlock<Layout>(chipped_dims, 1, 10); });
403 template<
typename T,
int NumDims>
407 for (
int i = 0;
i < NumDims; ++
i) {
408 result +=
static_cast<T>((
i + 1) * coords[
i]);
415 template<
int NumDims>
419 for (
int i = 0;
i < NumDims; ++
i) {
427 template <
typename T,
int NumDims,
int Layout>
435 VerifyBlockEvaluator<T, NumDims, Layout>(
436 input.generate(generator), [&dims]() { return FixedSizeBlock(dims); });
438 VerifyBlockEvaluator<T, NumDims, Layout>(
439 input.generate(generator),
440 [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
443 template <
typename T,
int NumDims,
int Layout>
451 for (
int i = 0; i < NumDims; ++i) reverse[i] = internal::random<bool>();
453 VerifyBlockEvaluator<T, NumDims, Layout>(
454 input.
reverse(
reverse), [&dims]() { return FixedSizeBlock(dims); });
456 VerifyBlockEvaluator<T, NumDims, Layout>(input.
reverse(
reverse), [&dims]() {
457 return RandomBlock<Layout>(dims, 1, 10);
461 template <
typename T,
int NumDims,
int Layout>
472 for (
int i = 0;
i < NumDims; ++
i) {
477 VerifyBlockEvaluator<T, NumDims, Layout>(
478 input.
slice(slice_start, slice_size),
479 [&slice_size]() { return FixedSizeBlock(slice_size); });
481 VerifyBlockEvaluator<T, NumDims, Layout>(
482 input.
slice(slice_start, slice_size),
483 [&slice_size]() { return RandomBlock<Layout>(slice_size, 1, 10); });
486 template <
typename T,
int NumDims,
int Layout>
493 for (
int i = 0;
i < NumDims; ++
i) shuffle[
i] =
i;
497 for (
int i = 0;
i < NumDims; ++
i) shuffled_dims[
i] = dims[shuffle[
i]];
499 VerifyBlockEvaluator<T, NumDims, Layout>(
501 [&shuffled_dims]() { return FixedSizeBlock(shuffled_dims); });
503 VerifyBlockEvaluator<T, NumDims, Layout>(
504 input.
shuffle(shuffle), [&shuffled_dims]() {
505 return RandomBlock<Layout>(shuffled_dims, 1, 5);
510 }
while (std::next_permutation(&shuffle[0], &shuffle[0] + NumDims));
513 template <
typename T,
int Layout>
515 Index dim = internal::random<Index>(1, 100);
522 auto reshapeLhs =
NByOne(dim);
523 auto reshapeRhs =
OneByM(dim);
525 auto bcastLhs =
OneByM(dim);
526 auto bcastRhs =
NByOne(dim);
530 VerifyBlockEvaluator<T, 2, Layout>(
531 lhs.
reshape(reshapeLhs).broadcast(bcastLhs) *
532 rhs.
reshape(reshapeRhs).broadcast(bcastRhs),
533 [dims]() { return SkewedInnerBlock<Layout, 2>(dims); });
536 template <
typename T,
int Layout>
538 Index dim = internal::random<Index>(1, 100);
545 auto bcastLhs =
OneByM(dim);
546 auto bcastRhs =
NByOne(dim);
550 VerifyBlockEvaluator<T, 2, Layout>(
551 (lhs.broadcast(bcastLhs) * rhs.broadcast(bcastRhs)).eval().reshape(dims),
552 [dims]() { return SkewedInnerBlock<Layout, 2>(dims); });
554 VerifyBlockEvaluator<T, 2, Layout>(
555 (lhs.broadcast(bcastLhs) * rhs.broadcast(bcastRhs)).eval().reshape(dims),
556 [dims]() { return RandomBlock<Layout, 2>(dims, 1, 50); });
559 template <
typename T,
int Layout>
561 if (Layout !=
static_cast<int>(
RowMajor))
return;
563 Index dim0 = internal::random<Index>(1, 10);
564 Index dim1 = internal::random<Index>(1, 10);
565 Index dim2 = internal::random<Index>(1, 10);
573 VerifyBlockEvaluator<T, 2, Layout>(
574 input.broadcast(bcast).
chip(0, 1),
575 [chipped_dims]() { return FixedSizeBlock(chipped_dims); });
577 VerifyBlockEvaluator<T, 2, Layout>(
578 input.broadcast(bcast).
chip(0, 1),
579 [chipped_dims]() { return SkewedInnerBlock<Layout, 2>(chipped_dims); });
581 VerifyBlockEvaluator<T, 2, Layout>(
582 input.broadcast(bcast).
chip(0, 1),
583 [chipped_dims]() { return RandomBlock<Layout, 2>(chipped_dims, 1, 5); });
591 template <
typename T,
int NumDims,
int Layout,
int NumExprDims = NumDims,
592 typename Expression,
typename GenBlockParams>
594 Expression expr, GenBlockParams gen_block) {
612 internal::TensorMaterializedBlock<T, NumExprDims, Layout> blk(
619 eval.writeBlock(block_params.
desc, blk);
631 auto s_expr = expr.slice(block_params.
offsets, block_params.
sizes);
635 using SliceExecutor =
TensorExecutor<
const SliceAssign, Device,
false,
649 template <
typename T,
int NumDims,
int Layout>
656 VerifyBlockAssignment<T, NumDims, Layout>(
657 tensor, map, [&dims]() {
return RandomBlock<Layout>(dims, 10, 20); });
658 VerifyBlockAssignment<T, NumDims, Layout>(
662 template <
typename T,
int NumDims,
int Layout>
670 std::shuffle(&shuffled[0], &shuffled[NumDims - 1], std::mt19937(
g_seed));
672 VerifyBlockAssignment<T, NumDims, Layout>(
673 tensor, map.reshape(shuffled),
674 [&shuffled]() { return RandomBlock<Layout>(shuffled, 1, 10); });
676 VerifyBlockAssignment<T, NumDims, Layout>(
677 tensor, map.reshape(shuffled),
678 [&shuffled]() { return SkewedInnerBlock<Layout>(shuffled); });
680 VerifyBlockAssignment<T, NumDims, Layout>(
681 tensor, map.reshape(shuffled),
682 [&shuffled]() { return FixedSizeBlock(shuffled); });
685 template <
typename T,
int NumDims,
int Layout>
690 Index chip_dim = internal::random<int>(0, NumDims - 1);
691 Index chip_offset = internal::random<Index>(0, dims[chip_dim] - 2);
694 for (
Index i = 0;
i < chip_dim; ++
i) {
695 chipped_dims[
i] = dims[
i];
697 for (
Index i = chip_dim + 1;
i < NumDims; ++
i) {
698 chipped_dims[
i - 1] = dims[
i];
704 tensor, map.chip(chip_offset, chip_dim),
705 [&chipped_dims]() {
return RandomBlock<Layout>(chipped_dims, 1, 10); });
708 tensor, map.chip(chip_offset, chip_dim),
709 [&chipped_dims]() {
return SkewedInnerBlock<Layout>(chipped_dims); });
712 tensor, map.chip(chip_offset, chip_dim),
716 template <
typename T,
int NumDims,
int Layout>
726 for (
int i = 0;
i < NumDims; ++
i) {
733 VerifyBlockAssignment<T, NumDims, Layout>(
734 tensor, map.slice(slice_start, slice_size),
735 [&slice_size]() { return RandomBlock<Layout>(slice_size, 1, 10); });
737 VerifyBlockAssignment<T, NumDims, Layout>(
738 tensor, map.slice(slice_start, slice_size),
739 [&slice_size]() { return SkewedInnerBlock<Layout>(slice_size); });
741 VerifyBlockAssignment<T, NumDims, Layout>(
742 tensor, map.slice(slice_start, slice_size),
743 [&slice_size]() { return FixedSizeBlock(slice_size); });
746 template <
typename T,
int NumDims,
int Layout>
752 for (
int i = 0;
i < NumDims; ++
i) shuffle[
i] =
i;
758 for (
int i = 0;
i < NumDims; ++
i) shuffled_dims[
i] = dims[shuffle[
i]];
760 VerifyBlockAssignment<T, NumDims, Layout>(
761 tensor, map.shuffle(shuffle),
762 [&shuffled_dims]() { return FixedSizeBlock(shuffled_dims); });
764 VerifyBlockAssignment<T, NumDims, Layout>(
765 tensor, map.shuffle(shuffle), [&shuffled_dims]() {
766 return RandomBlock<Layout>(shuffled_dims, 1, 5);
769 }
while (std::next_permutation(&shuffle[0], &shuffle[0] + NumDims));
774 #define CALL_SUBTEST_PART(PART) \
777 #define CALL_SUBTESTS_DIMS_LAYOUTS_TYPES(PART, NAME) \
778 CALL_SUBTEST_PART(PART)((NAME<float, 1, RowMajor>())); \
779 CALL_SUBTEST_PART(PART)((NAME<float, 2, RowMajor>())); \
780 CALL_SUBTEST_PART(PART)((NAME<float, 3, RowMajor>())); \
781 CALL_SUBTEST_PART(PART)((NAME<float, 4, RowMajor>())); \
782 CALL_SUBTEST_PART(PART)((NAME<float, 5, RowMajor>())); \
783 CALL_SUBTEST_PART(PART)((NAME<float, 1, ColMajor>())); \
784 CALL_SUBTEST_PART(PART)((NAME<float, 2, ColMajor>())); \
785 CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
786 CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
787 CALL_SUBTEST_PART(PART)((NAME<float, 5, ColMajor>())); \
788 CALL_SUBTEST_PART(PART)((NAME<int, 1, RowMajor>())); \
789 CALL_SUBTEST_PART(PART)((NAME<int, 2, RowMajor>())); \
790 CALL_SUBTEST_PART(PART)((NAME<int, 3, RowMajor>())); \
791 CALL_SUBTEST_PART(PART)((NAME<int, 4, RowMajor>())); \
792 CALL_SUBTEST_PART(PART)((NAME<int, 5, RowMajor>())); \
793 CALL_SUBTEST_PART(PART)((NAME<int, 1, ColMajor>())); \
794 CALL_SUBTEST_PART(PART)((NAME<int, 2, ColMajor>())); \
795 CALL_SUBTEST_PART(PART)((NAME<int, 4, ColMajor>())); \
796 CALL_SUBTEST_PART(PART)((NAME<int, 4, ColMajor>())); \
797 CALL_SUBTEST_PART(PART)((NAME<int, 5, ColMajor>())); \
798 CALL_SUBTEST_PART(PART)((NAME<bool, 1, RowMajor>())); \
799 CALL_SUBTEST_PART(PART)((NAME<bool, 2, RowMajor>())); \
800 CALL_SUBTEST_PART(PART)((NAME<bool, 3, RowMajor>())); \
801 CALL_SUBTEST_PART(PART)((NAME<bool, 4, RowMajor>())); \
802 CALL_SUBTEST_PART(PART)((NAME<bool, 5, RowMajor>())); \
803 CALL_SUBTEST_PART(PART)((NAME<bool, 1, ColMajor>())); \
804 CALL_SUBTEST_PART(PART)((NAME<bool, 2, ColMajor>())); \
805 CALL_SUBTEST_PART(PART)((NAME<bool, 4, ColMajor>())); \
806 CALL_SUBTEST_PART(PART)((NAME<bool, 4, ColMajor>())); \
807 CALL_SUBTEST_PART(PART)((NAME<bool, 5, ColMajor>()))
809 #define CALL_SUBTESTS_DIMS_LAYOUTS(PART, NAME) \
810 CALL_SUBTEST_PART(PART)((NAME<float, 1, RowMajor>())); \
811 CALL_SUBTEST_PART(PART)((NAME<float, 2, RowMajor>())); \
812 CALL_SUBTEST_PART(PART)((NAME<float, 3, RowMajor>())); \
813 CALL_SUBTEST_PART(PART)((NAME<float, 4, RowMajor>())); \
814 CALL_SUBTEST_PART(PART)((NAME<float, 5, RowMajor>())); \
815 CALL_SUBTEST_PART(PART)((NAME<float, 1, ColMajor>())); \
816 CALL_SUBTEST_PART(PART)((NAME<float, 2, ColMajor>())); \
817 CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
818 CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
819 CALL_SUBTEST_PART(PART)((NAME<float, 5, ColMajor>()))
821 #define CALL_SUBTESTS_LAYOUTS_TYPES(PART, NAME) \
822 CALL_SUBTEST_PART(PART)((NAME<float, RowMajor>())); \
823 CALL_SUBTEST_PART(PART)((NAME<float, ColMajor>())); \
824 CALL_SUBTEST_PART(PART)((NAME<bool, RowMajor>())); \
825 CALL_SUBTEST_PART(PART)((NAME<bool, ColMajor>()))