Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
def get_gpu_info():
|
| 6 |
+
"""Get information about available GPUs."""
|
| 7 |
+
if torch.cuda.is_available():
|
| 8 |
+
gpu_count = torch.cuda.device_count()
|
| 9 |
+
gpu_info = []
|
| 10 |
+
for i in range(gpu_count):
|
| 11 |
+
name = torch.cuda.get_device_name(i)
|
| 12 |
+
mem_total = torch.cuda.get_device_properties(i).total_memory / (1024**3)
|
| 13 |
+
gpu_info.append(f"GPU {i}: {name} ({mem_total:.1f}GB)")
|
| 14 |
+
return "\n".join(gpu_info)
|
| 15 |
+
return "No GPU available"
|
| 16 |
+
|
| 17 |
+
def test_gpu(gpu_name):
|
| 18 |
+
"""Test the selected GPU with a simple computation."""
|
| 19 |
+
if not torch.cuda.is_available():
|
| 20 |
+
return "No GPU available on this system"
|
| 21 |
+
|
| 22 |
+
# Find the GPU index based on the name
|
| 23 |
+
device_idx = 0
|
| 24 |
+
for i in range(torch.cuda.device_count()):
|
| 25 |
+
if gpu_name in torch.cuda.get_device_name(i):
|
| 26 |
+
device_idx = i
|
| 27 |
+
break
|
| 28 |
+
|
| 29 |
+
device = torch.device(f"cuda:{device_idx}")
|
| 30 |
+
|
| 31 |
+
# Simple GPU computation test
|
| 32 |
+
start_time = time.time()
|
| 33 |
+
|
| 34 |
+
# Create a tensor on GPU
|
| 35 |
+
x = torch.randn(1000, 1000, device=device)
|
| 36 |
+
y = torch.randn(1000, 1000, device=device)
|
| 37 |
+
|
| 38 |
+
# Perform matrix multiplication
|
| 39 |
+
z = torch.matmul(x, y)
|
| 40 |
+
|
| 41 |
+
# Move back to CPU
|
| 42 |
+
result = z.cpu()
|
| 43 |
+
|
| 44 |
+
elapsed = time.time() - start_time
|
| 45 |
+
|
| 46 |
+
return f"GPU Test Successful!\n\nSelected GPU: {gpu_name}\nComputation Time: {elapsed:.4f} seconds\nResult shape: {result.shape}\nResult sum: {result.sum().item():.2f}"
|
| 47 |
+
|
| 48 |
+
def get_available_gpus():
|
| 49 |
+
"""Get list of available GPU names."""
|
| 50 |
+
if torch.cuda.is_available():
|
| 51 |
+
return [torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())]
|
| 52 |
+
return ["No GPU available"]
|
| 53 |
+
|
| 54 |
+
# Get available GPUs
|
| 55 |
+
available_gpus = get_available_gpus()
|
| 56 |
+
gpu_info = get_gpu_info()
|
| 57 |
+
|
| 58 |
+
# Create Gradio interface
|
| 59 |
+
with gr.Blocks(title="GPU Tester") as demo:
|
| 60 |
+
gr.Markdown("# GPU Selector and Tester")
|
| 61 |
+
gr.Markdown(f"### Available GPU Information:\n```\n{gpu_info}\n```")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column():
|
| 65 |
+
gpu_dropdown = gr.Dropdown(
|
| 66 |
+
choices=available_gpus if available_gpus else ["No GPU available"],
|
| 67 |
+
label="Select GPU",
|
| 68 |
+
value=available_gpus[0] if available_gpus else None
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
test_button = gr.Button("Test GPU", variant="primary")
|
| 72 |
+
|
| 73 |
+
with gr.Column():
|
| 74 |
+
output = gr.Textbox(label="Test Results", lines=10)
|
| 75 |
+
|
| 76 |
+
test_button.click(fn=test_gpu, inputs=gpu_dropdown, outputs=output)
|
| 77 |
+
|
| 78 |
+
gr.Markdown("---")
|
| 79 |
+
gr.Markdown("This app allows you to select a GPU and test its computation capability using PyTorch.")
|
| 80 |
+
|
| 81 |
+
# Launch the app
|
| 82 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|