|
19 | 19 | #include <c10/core/ScalarType.h> |
20 | 20 | #include <c10/core/TensorOptions.h> |
21 | 21 |
|
| 22 | +#include <limits> |
| 23 | +#include <vector> |
| 24 | + |
22 | 25 | #include "ATen/ATen.h" |
23 | 26 | #include "gtest/gtest.h" |
24 | 27 | #include "torch/all.h" |
25 | 28 |
|
26 | 29 | // ======================== resize_ tests ======================== |
27 | | -// Note: Paddle's resize_ is implemented via reshape, which requires |
28 | | -// total element count to remain unchanged. |
| 30 | +// Note: compat resize_ mutates the underlying DenseTensor directly so |
| 31 | +// shrink/grow round-trips preserve storage semantics without introducing new |
| 32 | +// memory_format hard errors in this split PR. |
29 | 33 |
|
30 | 34 | TEST(TensorResizeTest, ResizeBasic) { |
31 | 35 | // Create a 2x3 tensor |
@@ -109,6 +113,92 @@ TEST(TensorResizeTest, ResizePreservesData) { |
109 | 113 | ASSERT_FLOAT_EQ(data[5], 5.0f); |
110 | 114 | } |
111 | 115 |
|
| 116 | +TEST(TensorResizeTest, ResizeShrinkDifferentNumel) { |
| 117 | + at::Tensor t = at::arange(24, at::kFloat).reshape({2, 3, 4}); |
| 118 | + |
| 119 | + t.resize_({4, 5}); |
| 120 | + |
| 121 | + ASSERT_EQ(t.sizes()[0], 4); |
| 122 | + ASSERT_EQ(t.sizes()[1], 5); |
| 123 | + |
| 124 | + float* data = t.data_ptr<float>(); |
| 125 | + for (int i = 0; i < 20; ++i) { |
| 126 | + ASSERT_FLOAT_EQ(data[i], static_cast<float>(i)); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +TEST(TensorResizeTest, ResizeGrowDifferentNumelPreservesPrefix) { |
| 131 | + at::Tensor t = at::arange(6, at::kFloat).reshape({2, 3}); |
| 132 | + |
| 133 | + t.resize_({2, 5}); |
| 134 | + |
| 135 | + ASSERT_EQ(t.sizes()[0], 2); |
| 136 | + ASSERT_EQ(t.sizes()[1], 5); |
| 137 | + |
| 138 | + float* data = t.data_ptr<float>(); |
| 139 | + for (int i = 0; i < 6; ++i) { |
| 140 | + ASSERT_FLOAT_EQ(data[i], static_cast<float>(i)); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +TEST(TensorResizeTest, ResizeShrinkGrowRoundTripPreservesTail) { |
| 145 | + at::Tensor t = at::arange(24, at::kFloat).reshape({2, 3, 4}); |
| 146 | + |
| 147 | + t.resize_({4, 5}); |
| 148 | + t.resize_({2, 3, 4}); |
| 149 | + |
| 150 | + ASSERT_EQ(t.sizes()[0], 2); |
| 151 | + ASSERT_EQ(t.sizes()[1], 3); |
| 152 | + ASSERT_EQ(t.sizes()[2], 4); |
| 153 | + |
| 154 | + float* data = t.data_ptr<float>(); |
| 155 | + for (int i = 0; i < 24; ++i) { |
| 156 | + ASSERT_FLOAT_EQ(data[i], static_cast<float>(i)); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +TEST(TensorResizeTest, ResizeChannelsLastMemoryFormatDoesNotThrow) { |
| 161 | + at::Tensor t = at::arange(24, at::kFloat).reshape({1, 2, 3, 4}); |
| 162 | + |
| 163 | + EXPECT_NO_THROW({ |
| 164 | + t.resize_(std::vector<int64_t>{1, 3, 2, 4}, at::MemoryFormat::ChannelsLast); |
| 165 | + }); |
| 166 | + |
| 167 | + ASSERT_EQ(t.sizes()[0], 1); |
| 168 | + ASSERT_EQ(t.sizes()[1], 3); |
| 169 | + ASSERT_EQ(t.sizes()[2], 2); |
| 170 | + ASSERT_EQ(t.sizes()[3], 4); |
| 171 | +} |
| 172 | + |
| 173 | +TEST(TensorResizeTest, ResizeChannelsLast3dMemoryFormatDoesNotThrow) { |
| 174 | + at::Tensor t = at::arange(24, at::kFloat).reshape({1, 2, 2, 2, 3}); |
| 175 | + |
| 176 | + EXPECT_NO_THROW({ |
| 177 | + t.resize_(std::vector<int64_t>{1, 2, 2, 3, 2}, |
| 178 | + at::MemoryFormat::ChannelsLast3d); |
| 179 | + }); |
| 180 | + |
| 181 | + ASSERT_EQ(t.sizes()[0], 1); |
| 182 | + ASSERT_EQ(t.sizes()[1], 2); |
| 183 | + ASSERT_EQ(t.sizes()[2], 2); |
| 184 | + ASSERT_EQ(t.sizes()[3], 3); |
| 185 | + ASSERT_EQ(t.sizes()[4], 2); |
| 186 | +} |
| 187 | + |
| 188 | +TEST(TensorResizeTest, ResizeRejectsNegativeDimension) { |
| 189 | + at::Tensor t = at::arange(6, at::kFloat); |
| 190 | + auto bad_size = std::vector<int64_t>{2, -1}; |
| 191 | + |
| 192 | + EXPECT_THROW(t.resize_(bad_size), std::exception); |
| 193 | +} |
| 194 | + |
| 195 | +TEST(TensorResizeTest, ResizeRejectsNumelOverflow) { |
| 196 | + at::Tensor t = at::arange(1, at::kFloat); |
| 197 | + auto huge_size = std::vector<int64_t>{std::numeric_limits<int64_t>::max(), 2}; |
| 198 | + |
| 199 | + EXPECT_THROW(t.resize_(huge_size), std::exception); |
| 200 | +} |
| 201 | + |
112 | 202 | TEST(TensorResizeTest, ResizeReturnReference) { |
113 | 203 | // Create a tensor |
114 | 204 | at::Tensor t = at::zeros({2, 3}); |
|
0 commit comments