RWKV-Runner/backend-python/utils/torch.py

31 lines
907 B
Python
Raw Normal View History

2023-05-07 17:27:54 +08:00
import os
import sysconfig
def set_torch():
2023-05-17 21:20:41 +08:00
torch_path = os.path.join(sysconfig.get_paths()["purelib"], f"torch{os.sep}lib")
2023-05-07 17:27:54 +08:00
paths = os.environ.get("PATH", "")
if os.path.exists(torch_path):
print(f"torch found: {torch_path}")
if torch_path in paths:
print("torch already set")
else:
2023-05-17 11:39:00 +08:00
os.environ["PATH"] = paths + os.pathsep + torch_path + os.pathsep
2023-05-19 20:10:30 +08:00
print("torch set")
# print("run:")
# print(f"set Path={paths + os.pathsep + torch_path + os.pathsep}")
2023-05-07 17:27:54 +08:00
else:
print("torch not found")
def torch_gc():
try:
import torch
2023-05-07 17:27:54 +08:00
if torch.cuda.is_available():
with torch.cuda.device(0):
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
except:
pass # prevent 'torch' has no attribute 'cuda' error, so user can use CPU or WebGPU