gst-code-collection / tools /gpu_keepalive.py
SitongGong's picture
merge code dirs 1031 (part 28)
0e2032e verified
Raw History Blame Contribute Delete
883 Bytes
# 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()