Uzito писал(а): ↑can you make a python script to test every CUDA function with torch 2.1.1-cu118?
Certainly! To create a comprehensive test script, it’s critical to note that PyTorch has many CUDA functions, and testing all of them could be quite extensive. However, I'll provide a basic framework for such a script which you can extend further. This example will focus on a few common CUDA functions.
First, make sure you have PyTorch installed with CUDA 11.8 support:
```bash
pip install torch==2.1.1+cu118 torchvision torchaudio -f
https://download.pytorch.org/whl/torch_stable.html
```
Here is a simple Python script to test some CUDA functions in PyTorch 2.1.1 with CUDA 11.8:
```python
import torch
def test_tensor_creation_on_cuda():
print("Testing tensor creation on CUDA...")
assert torch.cuda.is_available(), "CUDA is not available."
tensor = torch.tensor([1.0, 2.0, 3.0], device='cuda')
print(f"Tensor on CUDA: {tensor}")
assert tensor.device.type == 'cuda', "Tensor is not on CUDA."
def test_tensor_operations_on_cuda():
print("Testing tensor operations on CUDA...")
tensor_a = torch.tensor([1.0, 2.0, 3.0], device='cuda')
tensor_b = torch.tensor([4.0, 5.0, 6.0], device='cuda')
result = tensor_a + tensor_b
print(f"Result of addition: {result}")
assert result.equal(torch.tensor([5.0, 7.0, 9.0], device='cuda')), "Addition operation failed on CUDA."
def test_cudnn_functionality():
print("Testing cuDNN functionality...")
assert torch.backends.cudnn.is_available(), "cuDNN is not available."
assert torch.backends.cudnn.version() is not None, "cuDNN version could not be verified."
print(f"cuDNN version: {torch.backends.cudnn.version()}")
def test_cuda_memory_management():
print("Testing CUDA memory management...")
initial_memory = torch.cuda.memory_allocated()
tensor = torch.randn(1024, 1024, device='cuda')
post_memory = torch.cuda.memory_allocated()
print(f"Memory usage increased from {initial_memory} to {post_memory}")
assert post_memory > initial_memory, "Memory allocation on CUDA failed."
del tensor
torch.cuda.empty_cache()
final_memory = torch.cuda.memory_allocated()
print(f"Memory after clearing cache: {final_memory}")
assert final_memory <= initial_memory, "Memory freeing on CUDA failed."
def main():
test_tensor_creation_on_cuda()
test_tensor_operations_on_cuda()
test_cudnn_functionality()
test_cuda_memory_management()
print("All tests passed successfully!")
if __name__ == "__main__":
main()
```
### Explanation:
1. **test_tensor_creation_on_cuda**: Creates a tensor directly on the CUDA device and verifies it's correctly allocated.
2. **test_tensor_operations_on_cuda**: Performs basic tensor operations (addition) on CUDA and checks the results.
3. **test_cudnn_functionality**: Verifies the availability and version of cuDNN.
4. **test_cuda_memory_management**: Checks CUDA memory allocation and deallocation.
Feel free to expand this framework by adding more CUDA functions you wish to test. Each function should include proper assertions and checks to ensure everything is working as expected.
Model o1-preview ⁝ Training data up to October 2023 ⁝ Temperature 1 ⁝ Context window 32K