Spaces:
Running
on
Zero
Running
on
Zero
debug gpu_compute and cpu_compute
Browse files- app.py +45 -43
- app/utils.py +33 -0
app.py
CHANGED
|
@@ -1,4 +1,8 @@
|
|
| 1 |
from app.logger_config import logger as logging
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
|
@@ -9,53 +13,51 @@ import torch
|
|
| 9 |
logging.info("-----------info------------")
|
| 10 |
logging.debug("-----------debug------------")
|
| 11 |
|
| 12 |
-
@spaces.GPU
|
| 13 |
-
def
|
| 14 |
-
logging.debug("=== Start of
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
logging.debug(f"Memory allocated : {memory_allocated:.2f} MB")
|
| 34 |
-
logging.debug(f"Memory reserved : {memory_reserved:.2f} MB")
|
| 35 |
-
logging.debug(f"Total memory : {memory_total:.2f} MB")
|
| 36 |
-
else:
|
| 37 |
-
device = torch.device("cpu")
|
| 38 |
-
device_name = "CPU (no GPU detected)"
|
| 39 |
-
logging.debug("No GPU detected, using CPU")
|
| 40 |
-
|
| 41 |
-
# Create tensor
|
| 42 |
tensor = torch.tensor([len(name)], dtype=torch.float32, device=device)
|
| 43 |
logging.debug(f"Tensor created: {tensor}")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if has_gpu:
|
| 47 |
torch.cuda.empty_cache()
|
| 48 |
logging.debug("GPU cache cleared")
|
|
|
|
| 49 |
|
| 50 |
-
logging.debug("=== End of greet() ===")
|
| 51 |
-
return f"Tensor: {tensor.cpu().numpy()} | Device: {device_name}"
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
| 1 |
from app.logger_config import logger as logging
|
| 2 |
+
from app.utils import (
|
| 3 |
+
debug_current_device,
|
| 4 |
+
get_current_device
|
| 5 |
+
)
|
| 6 |
import os
|
| 7 |
import gradio as gr
|
| 8 |
import spaces
|
|
|
|
| 13 |
logging.info("-----------info------------")
|
| 14 |
logging.debug("-----------debug------------")
|
| 15 |
|
| 16 |
+
@spaces.GPU
|
| 17 |
+
def gpu_compute(name):
|
| 18 |
+
logging.debug("=== Start of gpu_compute() ===")
|
| 19 |
+
debug_current_device()
|
| 20 |
+
tensor,device_name = compute(name)
|
| 21 |
+
logging.debug("=== End of gpu_compute() ===")
|
| 22 |
+
return f"Tensor: {tensor.cpu().numpy()} | Device: {device_name}"
|
| 23 |
+
|
| 24 |
+
def cpu_compute(name):
|
| 25 |
+
logging.debug("=== Start of cpu_compute() ===")
|
| 26 |
+
debug_current_device()
|
| 27 |
+
|
| 28 |
+
tensor,device_name = compute(name)
|
| 29 |
+
|
| 30 |
+
logging.debug("=== End of cpu_compute() ===")
|
| 31 |
+
return f"Tensor: {tensor.cpu().numpy()} | Device: {device_name}"
|
| 32 |
+
|
| 33 |
+
def compute(name) :
|
| 34 |
+
# Get device info
|
| 35 |
+
device, device_name = get_current_device()
|
| 36 |
+
# Create a tensor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
tensor = torch.tensor([len(name)], dtype=torch.float32, device=device)
|
| 38 |
logging.debug(f"Tensor created: {tensor}")
|
| 39 |
+
# Optional: free GPU memory
|
| 40 |
+
if torch.cuda.is_available():
|
|
|
|
| 41 |
torch.cuda.empty_cache()
|
| 42 |
logging.debug("GPU cache cleared")
|
| 43 |
+
return tensor, device_name
|
| 44 |
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
block = gr.Blocks()
|
| 47 |
+
|
| 48 |
+
with block as demo:
|
| 49 |
+
with gr.Row():
|
| 50 |
+
input_text = gr.Text()
|
| 51 |
+
output_text = gr.Text()
|
| 52 |
+
with gr.Row():
|
| 53 |
+
gpu_button = gr.Button("GPU compute")
|
| 54 |
+
cpu_button = gr.Button("CPU compute")
|
| 55 |
+
|
| 56 |
+
gpu_button.click(fn=gpu_compute, inputs=[input_text],outputs=[output_text])
|
| 57 |
+
cpu_button.click(fn=cpu_compute, inputs=[input_text],outputs=[output_text])
|
| 58 |
+
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
block.render()
|
| 61 |
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.queue(max_size=10, api_open=False).launch(show_api=False)
|
app/utils.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from app.logger_config import logger as logging
|
| 3 |
+
|
| 4 |
+
def debug_current_device():
|
| 5 |
+
"""Logs detailed information about the current GPU or CPU device."""
|
| 6 |
+
logging.debug("=== Debugging current device ===")
|
| 7 |
+
|
| 8 |
+
if torch.cuda.is_available():
|
| 9 |
+
device = torch.device("cuda")
|
| 10 |
+
device_name = torch.cuda.get_device_name(0)
|
| 11 |
+
memory_allocated = torch.cuda.memory_allocated(0) / (1024 ** 2)
|
| 12 |
+
memory_reserved = torch.cuda.memory_reserved(0) / (1024 ** 2)
|
| 13 |
+
memory_total = torch.cuda.get_device_properties(0).total_memory / (1024 ** 2)
|
| 14 |
+
capability = torch.cuda.get_device_capability(0)
|
| 15 |
+
current_device = torch.cuda.current_device()
|
| 16 |
+
|
| 17 |
+
logging.debug(f"GPU name : {device_name}")
|
| 18 |
+
logging.debug(f"Current device ID : {current_device}")
|
| 19 |
+
logging.debug(f"CUDA capability : {capability}")
|
| 20 |
+
logging.debug(f"Memory allocated : {memory_allocated:.2f} MB")
|
| 21 |
+
logging.debug(f"Memory reserved : {memory_reserved:.2f} MB")
|
| 22 |
+
logging.debug(f"Total memory : {memory_total:.2f} MB")
|
| 23 |
+
|
| 24 |
+
else:
|
| 25 |
+
logging.debug("No GPU detected, using CPU")
|
| 26 |
+
|
| 27 |
+
def get_current_device():
|
| 28 |
+
if torch.cuda.is_available():
|
| 29 |
+
device = torch.device("cuda")
|
| 30 |
+
device_name = torch.cuda.get_device_name(0)
|
| 31 |
+
return device, device_name
|
| 32 |
+
else:
|
| 33 |
+
return torch.device("cpu"), "CPU (no GPU detected)"
|