File size: 1,157 Bytes
d4f211f
7d4b235
d4f211f
563fb2c
d4f211f
7d4b235
 
 
 
 
 
 
 
 
 
376c53e
 
7d4b235
0237ca0
7d4b235
0237ca0
7d4b235
5326ed4
f384f81
7d4b235
e89c6d2
7d4b235
 
 
cdab2e6
e89c6d2
7d4b235
e89c6d2
 
 
 
7d4b235
 
 
d4f211f
1ee0472
7d4b235
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
import spaces  # 导入spaces时会应用torch补丁
import torch
import os

spaces.zero.torch.unpatch()  # 手动调用解除torch补丁方法

os.environ['CUDA_VISIBLE_DEVICES'] = 'MIG-2f70e35e-577e-52c1-9054-bc9f9d04054e'

print('cuda visible devices: ' + str(os.getenv('CUDA_VISIBLE_DEVICES')))
print('cuda avaliable: ' + str(torch.cuda.is_available()))
print('cuda device count: ' + str(torch.cuda.device_count()))
print('cuda device name: ' + str(torch.cuda.get_device_name()))
print('cuda device capability: ' + str(torch.cuda.get_device_capability()))

torch.set_default_device('cuda')

zero = torch.Tensor([0]).cuda()

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()