| 114 | // ============= Memory Management Tests ============= |
| 115 | |
| 116 | TEST_F(TensorStressTest, MaxMemoryAllocation) { |
| 117 | // Try to allocate tensors until we approach memory limit |
| 118 | std::vector<Tensor> tensors; |
| 119 | const size_t chunk_size = 10 * 1024 * 1024; // 10M floats = 40MB per tensor |
| 120 | |
| 121 | size_t total_allocated = 0; |
| 122 | |
| 123 | for (int i = 0; i < 50; ++i) { // Max 50 tensors (2GB) to avoid hanging |
| 124 | // Check available memory before allocating |
| 125 | size_t free_mem, total; |
| 126 | cudaMemGetInfo(&free_mem, &total); |
| 127 | if (free_mem < chunk_size * sizeof(float) * 3) { |
| 128 | // Stop before we run too low |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | auto t = Tensor::empty({chunk_size}, Device::CUDA); |
| 133 | if (!t.is_valid()) { |
| 134 | break; |
| 135 | } |
| 136 | tensors.emplace_back(std::move(t)); |
| 137 | total_allocated += chunk_size * sizeof(float); |
| 138 | } |
| 139 | |
| 140 | EXPECT_GT(tensors.size(), 0) << "Should be able to allocate at least one tensor"; |
| 141 | |
| 142 | // Clear tensors to free memory |
| 143 | tensors.clear(); |
| 144 | cudaDeviceSynchronize(); |
| 145 | |
| 146 | // Clear PyTorch cache to actually release memory |
| 147 | c10::cuda::CUDACachingAllocator::emptyCache(); |
| 148 | |
| 149 | // Clear tensor library's memory pool cache |
| 150 | Tensor::trim_memory_pool(); |
| 151 | cudaDeviceSynchronize(); |
| 152 | |
| 153 | // Memory should be freed |
| 154 | size_t free_after, total_after; |
| 155 | cudaMemGetInfo(&free_after, &total_after); |
| 156 | EXPECT_GT(free_after, initial_free_mem_ - 100 * 1024 * 1024) |
| 157 | << "Memory not properly freed after clearing tensors"; |
| 158 | } |
| 159 | |
| 160 | TEST_F(TensorStressTest, RapidAllocationDeallocation) { |
| 161 | // Rapidly allocate and deallocate tensors |
nothing calls this directly
no test coverage detected