File size: 883 Bytes
0e2032e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# GPU 保活脚本:每张卡按占空比跑矩阵乘,使利用率维持在目标值附近
import sys, time, torch, multiprocessing as mp

def worker(dev, target):
    torch.cuda.set_device(dev)
    a = torch.randn(4096, 4096, device=dev, dtype=torch.float16)
    b = torch.randn(4096, 4096, device=dev, dtype=torch.float16)
    period = 0.05  # 每个周期 0.05s,忙 target 比例,其余时间 sleep
    while True:
        t0 = time.time()
        while time.time() - t0 < period * target:
            torch.mm(a, b); torch.cuda.synchronize()
        time.sleep(max(0, period - (time.time() - t0)))

if __name__ == "__main__":
    target = float(sys.argv[1]) if len(sys.argv) > 1 else 0.35
    mp.set_start_method("spawn")
    ps = [mp.Process(target=worker, args=(i, target)) for i in range(torch.cuda.device_count())]
    for p in ps: p.start()
    for p in ps: p.join()