Skip to content

Commit

Permalink
fix device issue when host don't have cuda device
Browse files Browse the repository at this point in the history
  • Loading branch information
clemente0731 committed Jul 3, 2024
1 parent f9a33a3 commit 5507623
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
14 changes: 9 additions & 5 deletions pprobe/tests/xtest_device_conversion_detection.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import torch
import torch.nn as nn

# Create a tensor on GPU
tensor = torch.randn(3, 3).cuda()
# 获取可用设备
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Create a model on GPU
model = nn.Linear(3, 3).cuda()
tensor = torch.randn(3, 3).to(device)
model = nn.Linear(3, 3).to(device)

# 将张量移动到 CPU 的三种方法
# Method 1: Using tensor.to("cpu")
tensor_to_cpu_1 = tensor.to("cpu")

Expand All @@ -16,11 +17,14 @@
# Method 3: Using tensor.cpu()
tensor_to_cpu_3 = tensor.cpu()

# 将模型移动到 CPU 的三种方法
# Method 1: Using model.to("cpu")
model_to_cpu_1 = model.to("cpu")

# Method 2: Using model.to(torch.device("cpu"))
model_to_cpu_2 = model.to(torch.device("cpu"))

# Method 3: Using model.cpu()
model_to_cpu_3 = model.cpu()
model_to_cpu_3 = model.cpu()

print(f"Using device: {device}")
29 changes: 22 additions & 7 deletions pprobe/toggle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@
=================================================
██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ███████╗
██╔══██╗██╔══██╗██╔══██╗██╔═══██╗██╔══██╗██╔════╝
██████╔╝██████╔╝██████╔╝██║ ██║██████╔╝█████╗
██╔═══╝ ██╔═══╝ ██╔══██╗██║ ██║██╔══██╗██╔══╝
██████╔╝██████╔╝██████╔╝██║ ██║██████╔╝█████╗
██╔═══╝ ██╔═══╝ ██╔══██╗██║ ██║██╔══██╗██╔══╝
██║ ██║ ██║ ██║╚██████╔╝██████╔╝███████╗
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝
=================================================
MIT License
Copyright (c) [2024] [[email protected]]
Copyright (c) 2024 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
"""


Expand Down Expand Up @@ -131,17 +146,17 @@ def show_status(self):
True values in green
False values in red
"""

print(HELLO_PPROBE)

def colorize(value):
"""Return the value in green if True, in red if False."""
if value in (True, "True"):
return f"\033[92m{value}\033[0m" # GREEN
elif value in (False, "False"):
return f"\033[91m{value}\033[0m" # RED
return value

status_in_color = []

for name, value in self.running_toggle.items():
Expand Down

0 comments on commit 5507623

Please sign in to comment.