jyh-zero / app.py
hiascend's picture
Update app.py
0237ca0 verified
raw
history blame
825 Bytes
import gradio as gr
import spaces # 导入spaces时会应用torch补丁
import torch
spaces.zero.torch.unpatch() # 手动调用解除torch补丁方法
print('cuda avaliable: ' + str(torch.cuda.is_available())) # false
torch.set_default_device('cuda')
zero = torch.Tensor([0]).cuda() # 报错:无GPU资源
one = torch.ones(2, 2, device='cuda')
two = torch.matmul(one, one).cuda()
def print_device():
print('cuda avaliable: ' + str(torch.cuda.is_available()))
print('zero device: ' + str(zero.device))
print('one device: ' + str(one.device))
print('two device: ' + str(two.device))
print_device() # cpu
@spaces.GPU
def greet(n):
print('on zero gpu')
print_device() # cuda
return f"Hello {zero + n} Tensor"
gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text()).launch()