|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +import torch |
| 17 | + |
| 18 | + |
| 19 | +def test_gpu_availability(): |
| 20 | + """Test that GPU is available and accessible""" |
| 21 | + if not torch.cuda.is_available(): |
| 22 | + pytest.skip("CUDA not available") |
| 23 | + |
| 24 | + gpu_count = torch.cuda.device_count() |
| 25 | + print(f"Number of GPUs available: {gpu_count}") |
| 26 | + assert gpu_count >= 1, f"Expected at least 1 GPU, found {gpu_count}" |
| 27 | + |
| 28 | + # Print GPU information |
| 29 | + for i in range(gpu_count): |
| 30 | + gpu_name = torch.cuda.get_device_name(i) |
| 31 | + print(f"GPU {i}: {gpu_name}") |
| 32 | + print(f" Memory allocated: {torch.cuda.memory_allocated(i) / 1024**2:.2f} MB") |
| 33 | + print(f" Memory reserved: {torch.cuda.memory_reserved(i) / 1024**2:.2f} MB") |
| 34 | + |
| 35 | + print("✓ GPU availability test passed") |
| 36 | + |
| 37 | + |
| 38 | +def test_gpu_tensor_operations(): |
| 39 | + """Test basic GPU tensor operations""" |
| 40 | + if not torch.cuda.is_available(): |
| 41 | + pytest.skip("CUDA not available") |
| 42 | + |
| 43 | + # Create tensors on GPU |
| 44 | + device = torch.device("cuda:0") |
| 45 | + a = torch.tensor([1.0, 2.0, 3.0], device=device) |
| 46 | + b = torch.tensor([4.0, 5.0, 6.0], device=device) |
| 47 | + |
| 48 | + # Verify tensors are on GPU |
| 49 | + assert a.is_cuda, "Tensor a is not on GPU" |
| 50 | + assert b.is_cuda, "Tensor b is not on GPU" |
| 51 | + |
| 52 | + # Test addition |
| 53 | + c = a + b |
| 54 | + expected = torch.tensor([5.0, 7.0, 9.0], device=device) |
| 55 | + assert torch.allclose(c, expected), f"Expected {expected}, got {c}" |
| 56 | + assert c.is_cuda, "Result tensor is not on GPU" |
| 57 | + |
| 58 | + # Test multiplication |
| 59 | + d = a * b |
| 60 | + expected = torch.tensor([4.0, 10.0, 18.0], device=device) |
| 61 | + assert torch.allclose(d, expected), f"Expected {expected}, got {d}" |
| 62 | + |
| 63 | + print("✓ GPU tensor operations test passed") |
| 64 | + |
| 65 | + |
| 66 | +def test_gpu_matrix_multiplication(): |
| 67 | + """Test matrix multiplication on GPU""" |
| 68 | + if not torch.cuda.is_available(): |
| 69 | + pytest.skip("CUDA not available") |
| 70 | + |
| 71 | + device = torch.device("cuda:0") |
| 72 | + |
| 73 | + # Create random matrices on GPU |
| 74 | + matrix_a = torch.randn(100, 200, device=device) |
| 75 | + matrix_b = torch.randn(200, 300, device=device) |
| 76 | + |
| 77 | + # Perform matrix multiplication |
| 78 | + result = torch.matmul(matrix_a, matrix_b) |
| 79 | + |
| 80 | + # Verify shape |
| 81 | + assert result.shape == (100, 300), f"Expected shape (100, 300), got {result.shape}" |
| 82 | + |
| 83 | + # Verify result is on GPU |
| 84 | + assert result.is_cuda, "Result is not on GPU" |
| 85 | + |
| 86 | + # Verify result is finite |
| 87 | + assert torch.isfinite(result).all(), "Result contains non-finite values" |
| 88 | + |
| 89 | + print("✓ GPU matrix multiplication test passed") |
| 90 | + |
| 91 | + |
| 92 | +def test_multi_gpu_tensor_transfer(): |
| 93 | + """Test tensor transfer between GPUs if multiple GPUs are available""" |
| 94 | + if not torch.cuda.is_available(): |
| 95 | + pytest.skip("CUDA not available") |
| 96 | + |
| 97 | + gpu_count = torch.cuda.device_count() |
| 98 | + print(f"Testing with {gpu_count} GPU(s)") |
| 99 | + |
| 100 | + if gpu_count < 2: |
| 101 | + print("Only 1 GPU available, testing single GPU operations") |
| 102 | + device = torch.device("cuda:0") |
| 103 | + tensor = torch.randn(10, 10, device=device) |
| 104 | + assert tensor.is_cuda, "Tensor is not on GPU" |
| 105 | + else: |
| 106 | + print("Multiple GPUs available, testing cross-GPU transfer") |
| 107 | + # Create tensor on GPU 0 |
| 108 | + tensor_gpu0 = torch.randn(10, 10, device="cuda:0") |
| 109 | + assert tensor_gpu0.device.index == 0, "Tensor not on GPU 0" |
| 110 | + |
| 111 | + # Transfer to GPU 1 |
| 112 | + tensor_gpu1 = tensor_gpu0.to("cuda:1") |
| 113 | + assert tensor_gpu1.device.index == 1, "Tensor not on GPU 1" |
| 114 | + |
| 115 | + # Verify data is preserved |
| 116 | + assert torch.allclose(tensor_gpu0.cpu(), tensor_gpu1.cpu()), "Data changed during transfer" |
| 117 | + |
| 118 | + print("✓ Multi-GPU tensor transfer test passed") |
| 119 | + |
| 120 | + |
| 121 | +def test_gpu_memory_allocation(): |
| 122 | + """Test GPU memory allocation and deallocation""" |
| 123 | + if not torch.cuda.is_available(): |
| 124 | + pytest.skip("CUDA not available") |
| 125 | + |
| 126 | + device = torch.device("cuda:0") |
| 127 | + |
| 128 | + # Record initial memory |
| 129 | + torch.cuda.empty_cache() |
| 130 | + initial_memory = torch.cuda.memory_allocated(0) |
| 131 | + print(f"Initial GPU memory allocated: {initial_memory / 1024**2:.2f} MB") |
| 132 | + |
| 133 | + # Allocate large tensor |
| 134 | + large_tensor = torch.randn(1000, 1000, device=device) |
| 135 | + memory_after_alloc = torch.cuda.memory_allocated(0) |
| 136 | + print(f"Memory after allocation: {memory_after_alloc / 1024**2:.2f} MB") |
| 137 | + |
| 138 | + # Verify memory increased |
| 139 | + assert memory_after_alloc > initial_memory, "GPU memory did not increase after allocation" |
| 140 | + |
| 141 | + # Delete tensor and clear cache |
| 142 | + del large_tensor |
| 143 | + torch.cuda.empty_cache() |
| 144 | + memory_after_dealloc = torch.cuda.memory_allocated(0) |
| 145 | + print(f"Memory after deallocation: {memory_after_dealloc / 1024**2:.2f} MB") |
| 146 | + |
| 147 | + print("✓ GPU memory allocation test passed") |
| 148 | + |
| 149 | + |
| 150 | +def test_cuda_compute_capability(): |
| 151 | + """Test CUDA compute capability""" |
| 152 | + if not torch.cuda.is_available(): |
| 153 | + pytest.skip("CUDA not available") |
| 154 | + |
| 155 | + for i in range(torch.cuda.device_count()): |
| 156 | + capability = torch.cuda.get_device_capability(i) |
| 157 | + print(f"GPU {i} compute capability: {capability[0]}.{capability[1]}") |
| 158 | + |
| 159 | + # Verify compute capability is reasonable (at least 3.5) |
| 160 | + assert capability[0] >= 3, f"GPU {i} compute capability too old: {capability}" |
| 161 | + |
| 162 | + print("✓ CUDA compute capability test passed") |
0 commit comments