#Code suggested by Grok-
import tensorflow as tf
import time
# Check GPU availability #first we are checking if the GPU is available/running or not??
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
# Define matrix size
matrix_size = 10000 #a large matrix to imitate the deep learning operations
# Create two large matrices
a = tf.random.normal([matrix_size, matrix_size])
b = tf.random.normal([matrix_size, matrix_size])
# Benchmark GPU performance
def benchmark(gpu):
with tf.device('/device:GPU:0' if gpu else '/device:CPU:0'):
start_time = time.time()
c = tf.matmul(a, b)
delta = time.time() - start_time
print(f"{device_name if gpu else 'CPU'} matrix multiplication took {delta:.2f} seconds")
# Run benchmarks
benchmark(True) # GPU
benchmark(False) # CPU for comparison
GPU P100 on Kaggle
GPU T4 x 2 on Kaggle
import tensorflow as tf
import time
import platform
import psutil
import numpy as np
from datetime import datetime #importing all the required libaries- generally Claude is better at coding
def get_system_info():
gpu_info = []
try:
gpu_devices = tf.config.list_physical_devices('GPU')
for device in gpu_devices:
gpu_details = tf.config.experimental.get_device_details(device)
gpu_info.append(gpu_details)
except:
gpu_info = "Error getting GPU info"
return {
"Platform": platform.platform(),
"Python Version": platform.python_version(),
"TensorFlow Version": tf.__version__,
"RAM": f"{psutil.virtual_memory().total / (1024.0 ** 3):.1f} GB",
"GPU Info": gpu_info
} #accessing all the platform and version details, running in the background
def run_benchmark(matrix_sizes=[5000, 10000, 15000]): #Claude improved, suggesting 3 different sizes of matrix, rest its matmul @
results = []
for size in matrix_sizes:
print(f"\nRunning benchmark for {size}x{size} matrix...")
a = tf.random.normal([size, size])
b = tf.random.normal([size, size])
with tf.device('/device:GPU:0'):
_ = tf.matmul(a, b)
gpu_times = []
for _ in range(3):
start_time = time.time()
_ = tf.matmul(a, b)
gpu_times.append(time.time() - start_time)
with tf.device('/device:CPU:0'):
_ = tf.matmul(a, b)
cpu_times = []
for _ in range(3):
start_time = time.time()
_ = tf.matmul(a, b)
cpu_times.append(time.time() - start_time)
results.append({
"matrix_size": size,
"gpu_mean_time": np.mean(gpu_times),
"gpu_std_time": np.std(gpu_times),
"cpu_mean_time": np.mean(cpu_times),
"cpu_std_time": np.std(cpu_times),
"speedup": np.mean(cpu_times) / np.mean(gpu_times)
})
print(f"GPU mean time: {results[-1]['gpu_mean_time']:.3f}s ± {results[-1]['gpu_std_time']:.3f}s")
print(f"CPU mean time: {results[-1]['cpu_mean_time']:.3f}s ± {results[-1]['cpu_std_time']:.3f}s")
print(f"GPU Speedup: {results[-1]['speedup']:.1f}x")
return results #saving and printing results
def save_results(system_info, results, platform_name):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
result_str = f"# GPU Benchmark Results - {platform_name}\nRun at: {timestamp}\n\n## System Information\n```\n{system_info}\n```\n\n## Benchmark Results\n| Matrix Size | GPU Time (s) | CPU Time (s) | Speedup |\n|------------|--------------|--------------|---------|"
for r in results:
result_str += f"\n| {r['matrix_size']}x{r['matrix_size']} | {r['gpu_mean_time']:.3f} ± {r['gpu_std_time']:.3f} | {r['cpu_mean_time']:.3f} ± {r['cpu_std_time']:.3f} | {r['speedup']:.1f}x |"
return result_str
if __name__ == "__main__":
PLATFORM_NAME = "YOUR_PLATFORM_NAME"
print("Getting system information...")
system_info = get_system_info()
print("\nRunning benchmarks...")
results = run_benchmark()
print("\nGenerating report...")
report = save_results(system_info, results, PLATFORM_NAME)
print("\nReport:")
print(report) #again, quite a bit of improvement, detailing a comprehensive report