|
| 1 | +#include "flag_gems/operators.h" |
| 2 | +#include "gtest/gtest.h" |
| 3 | +#include "torch/torch.h" |
| 4 | + |
| 5 | +TEST(TritonCatTest, basictest) { |
| 6 | + const torch::Device device(torch::kCUDA, 0); |
| 7 | + torch::Tensor t1 = torch::randn({2, 3}, device); |
| 8 | + torch::Tensor t2 = torch::randn({4, 3}, device); |
| 9 | + |
| 10 | + torch::Tensor out_torch = torch::cat({t1, t2}, 0); |
| 11 | + torch::Tensor out_gems = flag_gems::cat({t1, t2}, 0); |
| 12 | + |
| 13 | + EXPECT_TRUE(torch::equal(out_torch, out_gems)); |
| 14 | +} |
| 15 | + |
| 16 | +TEST(TritonCatTest, 2dimtest) { |
| 17 | + const torch::Device device(torch::kCUDA, 0); |
| 18 | + torch::Tensor t1 = torch::randn({3, 2}, device); |
| 19 | + torch::Tensor t2 = torch::randn({3, 4}, device); |
| 20 | + |
| 21 | + int dim_to_test = 1; |
| 22 | + torch::Tensor out_torch = torch::cat({t1, t2}, dim_to_test); |
| 23 | + torch::Tensor out_gems = flag_gems::cat({t1, t2}, dim_to_test); |
| 24 | + |
| 25 | + EXPECT_TRUE(torch::equal(out_gems, out_torch)); |
| 26 | + |
| 27 | + EXPECT_EQ(out_gems.size(0), 3); |
| 28 | + EXPECT_EQ(out_gems.size(1), 6); |
| 29 | +} |
| 30 | + |
| 31 | +TEST(TritonCatTest, 3dimtest) { |
| 32 | + const torch::Device device(torch::kCUDA, 0); |
| 33 | + |
| 34 | + torch::Tensor t1 = torch::randn({3, 2, 4}, device); |
| 35 | + torch::Tensor t2 = torch::randn({3, 5, 4}, device); |
| 36 | + |
| 37 | + int dim_to_test = 1; |
| 38 | + |
| 39 | + torch::Tensor out_torch = torch::cat({t1, t2}, dim_to_test); |
| 40 | + torch::Tensor out_gems = flag_gems::cat({t1, t2}, dim_to_test); |
| 41 | + |
| 42 | + EXPECT_TRUE(torch::equal(out_torch, out_gems)); |
| 43 | +} |
| 44 | + |
| 45 | +TEST(TritonCatTest, 4dimtest) { |
| 46 | + const torch::Device device(torch::kCUDA, 0); |
| 47 | + auto options = torch::TensorOptions().device(device).dtype(torch::kFloat32); |
| 48 | + |
| 49 | + torch::Tensor t1 = torch::randn({2, 3, 4, 5}, options); |
| 50 | + torch::Tensor t2 = torch::randn({2, 6, 4, 5}, options); |
| 51 | + |
| 52 | + int dim_to_test = 1; |
| 53 | + torch::Tensor out_torch = torch::cat({t1, t2}, dim_to_test); |
| 54 | + torch::Tensor out_gems = flag_gems::cat({t1, t2}, dim_to_test); |
| 55 | + |
| 56 | + EXPECT_TRUE(torch::equal(out_torch, out_gems)); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(TritonCatTest, IntegerConcatenation) { |
| 60 | + const torch::Device device(torch::kCUDA, 0); |
| 61 | + auto options = torch::TensorOptions().device(device).dtype(torch::kInt32); |
| 62 | + |
| 63 | + torch::Tensor t1 = torch::randint(0, 100, {2, 3, 4}, options); |
| 64 | + torch::Tensor t2 = torch::randint(0, 100, {2, 3, 4}, options); |
| 65 | + |
| 66 | + int dim_to_test = 2; |
| 67 | + torch::Tensor out_torch = torch::cat({t1, t2}, dim_to_test); |
| 68 | + torch::Tensor out_gems = flag_gems::cat({t1, t2}, dim_to_test); |
| 69 | + |
| 70 | + EXPECT_TRUE(torch::equal(out_torch, out_gems)); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(TritonCatTest, EmptyTensorConcatenation) { |
| 74 | + const torch::Device device(torch::kCUDA, 0); |
| 75 | + auto options = torch::TensorOptions().device(device).dtype(torch::kFloat32); |
| 76 | + |
| 77 | + torch::Tensor t1 = torch::randn({0, 3}, options); |
| 78 | + torch::Tensor t2 = torch::randn({2, 3}, options); |
| 79 | + |
| 80 | + torch::Tensor out_torch = torch::cat({t1, t2}, 0); |
| 81 | + torch::Tensor out_gems = flag_gems::cat({t1, t2}, 0); |
| 82 | + |
| 83 | + EXPECT_TRUE(torch::equal(out_torch, out_gems)); |
| 84 | + |
| 85 | + torch::Tensor t3 = torch::randn({0, 3}, options); |
| 86 | + torch::Tensor t4 = torch::randn({0, 3}, options); |
| 87 | + |
| 88 | + torch::Tensor out_torch_both_empty = torch::cat({t3, t4}, 0); |
| 89 | + torch::Tensor out_gems_both_empty = flag_gems::cat({t3, t4}, 0); |
| 90 | + |
| 91 | + EXPECT_TRUE(torch::equal(out_torch_both_empty, out_gems_both_empty)); |
| 92 | + EXPECT_EQ(out_gems_both_empty.numel(), 0); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(TritonCatTest, 3tensorcat) { |
| 96 | + const torch::Device device(torch::kCUDA, 0); |
| 97 | + auto options = torch::TensorOptions().device(device).dtype(torch::kFloat32); |
| 98 | + |
| 99 | + torch::Tensor t1 = torch::randn({2, 3}, options); |
| 100 | + torch::Tensor t2 = torch::randn({4, 3}, options); |
| 101 | + torch::Tensor t3 = torch::randn({1, 3}, options); |
| 102 | + |
| 103 | + int dim_to_test = 0; |
| 104 | + torch::Tensor out_torch = torch::cat({t1, t2, t3}, dim_to_test); |
| 105 | + torch::Tensor out_gems = flag_gems::cat({t1, t2, t3}, dim_to_test); |
| 106 | + |
| 107 | + EXPECT_TRUE(torch::equal(out_torch, out_gems)); |
| 108 | + EXPECT_EQ(out_gems.size(0), 7); |
| 109 | + EXPECT_EQ(out_gems.size(1), 3); |
| 110 | +} |
| 111 | + |
| 112 | +TEST(TritonCatTest, HandlesNonContiguousInput) { |
| 113 | + const torch::Device device(torch::kCUDA, 0); |
| 114 | + auto options = torch::TensorOptions().device(device).dtype(torch::kFloat32); |
| 115 | + |
| 116 | + torch::Tensor t_base = torch::randn({2, 3, 4}, options); |
| 117 | + |
| 118 | + torch::Tensor t1_non_contiguous = t_base.transpose(1, 2); |
| 119 | + |
| 120 | + torch::Tensor t2_contiguous = torch::randn({2, 4, 5}, options); |
| 121 | + |
| 122 | + ASSERT_FALSE(t1_non_contiguous.is_contiguous()); |
| 123 | + |
| 124 | + int dim_to_test = 2; |
| 125 | + torch::Tensor out_torch = torch::cat({t1_non_contiguous, t2_contiguous}, dim_to_test); |
| 126 | + torch::Tensor out_gems = flag_gems::cat({t1_non_contiguous, t2_contiguous}, dim_to_test); |
| 127 | + |
| 128 | + EXPECT_TRUE(torch::equal(out_torch, out_gems)); |
| 129 | + |
| 130 | + EXPECT_EQ(out_gems.size(0), 2); |
| 131 | + EXPECT_EQ(out_gems.size(1), 4); |
| 132 | + EXPECT_EQ(out_gems.size(2), 8); |
| 133 | +} |
0 commit comments