Compare commits

..

1 Commits

Author SHA1 Message Date
Joseph Hopfmüller
744c5f5166 rename dir;
add torch import test script
2024-11-16 00:39:19 +01:00
3 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
import torch
import time
def print_torch_env():
print("Torch version: ", torch.__version__)
print("CUDA available: ", torch.cuda.is_available())
print("CUDA version: ", torch.version.cuda)
print("CUDNN version: ", torch.backends.cudnn.version())
print("Device count: ", torch.cuda.device_count())
print("Current device: ", torch.cuda.current_device())
print("Device name: ", torch.cuda.get_device_name(0))
print("Device capability: ", torch.cuda.get_device_capability(0))
print("Device memory: ", torch.cuda.get_device_properties(0).total_memory)
def measure_runtime(func):
"""
Measure the runtime of a function.
:param func: Function to measure
:type func: function
:return: Wrapped function with runtime measurement
:rtype: function
"""
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"Runtime: {end_time - start_time:.6f} seconds")
return result, end_time - start_time
return wrapper
@measure_runtime
def tensor_addition(a, b):
"""
Perform tensor addition.
:param a: First tensor
:type a: torch.Tensor
:param b: Second tensor
:type b: torch.Tensor
:return: Sum of tensors
:rtype: torch.Tensor
"""
return a + b
def runtime_test():
x = torch.rand(2**18, 2**10)
y = torch.rand(2**18, 2**10)
print("Tensor addition on CPU")
_, cpu_time = tensor_addition(x, y)
print()
print("Tensor addition on GPU")
if not torch.cuda.is_available():
print("CUDA is not available")
return
_, gpu_time = tensor_addition(x.cuda(), y.cuda())
print()
print(f"Speedup: {cpu_time / gpu_time *100:.2f}%")
if __name__ == "__main__":
print_torch_env()
print()
runtime_test()