LiveWorld / LiveWorld-Code /scripts /run_panodata_sharded.py
cszhilu1998's picture
Upload folder using huggingface_hub
5024339 verified
Raw History Blame Contribute Delete
3.12 kB
"""Run one PanoData setting across independent GPU sample shards."""
import argparse
import fcntl
import json
import os
from pathlib import Path
import signal
import subprocess
import sys
import time
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--k',type=int,choices=[1,21],required=True)
parser.add_argument('--gpus',type=int,nargs='+',required=True)
args=parser.parse_args()
if len(set(args.gpus))!=len(args.gpus):parser.error('GPU IDs must be distinct')
root=Path(__file__).resolve().parents[1]
logs=root/'logs'
lock=(logs/f'panodata_k{args.k}_sharded.lock').open('w')
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
status_path=logs/f'panodata_k{args.k}_sharded_status.json'
count=sum(bool(line.strip()) for line in Path('/home/zhangzhilu/shared/datasets/Pano-World/PanoData-v4/test.txt').read_text().splitlines())
script='scripts/infer_panodata.py' if args.k==1 else 'scripts/infer_panodata_k21.py'
workers=[]
children=[]
stopping=False
def save(state):
payload={'state':state,'prefix_frames':args.k,'gpus':args.gpus,'total_samples':count,
'updated_at':time.strftime('%Y-%m-%d %H:%M:%S %z'),'workers':workers,
'output':str(root/f'outputs_panodata/LiveWorld-K{args.k}')}
tmp=status_path.with_suffix('.tmp');tmp.write_text(json.dumps(payload,indent=2));tmp.replace(status_path)
print(json.dumps(payload),flush=True)
def stop(signum,frame):
nonlocal stopping
stopping=True
for child in children:
if child.poll() is None:child.terminate()
signal.signal(signal.SIGTERM,stop)
signal.signal(signal.SIGINT,stop)
for rank,gpu in enumerate(args.gpus):
env=os.environ.copy();env['CUDA_VISIBLE_DEVICES']=str(gpu)
env.setdefault('OMP_NUM_THREADS','8')
env.setdefault('PYTORCH_CUDA_ALLOC_CONF','expandable_segments:True')
command=[sys.executable,'-u',script,'--max_samples',str(count),
'--perspective_num_frames',str(args.k),'--num_frames','81',
'--num_shards',str(len(args.gpus)),'--shard_rank',str(rank),
'--skip_existing','--device','cuda:0']
logpath=logs/f'infer_panodata_k{args.k}_gpu{gpu}.log'
with logpath.open('ab',buffering=0) as output:
child=subprocess.Popen(command,cwd=root,env=env,stdin=subprocess.DEVNULL,
stdout=output,stderr=subprocess.STDOUT)
children.append(child)
workers.append({'pid':child.pid,'gpu':gpu,'shard_rank':rank,'num_shards':len(args.gpus),
'state':'running','log':str(logpath),'command':command})
save('running')
while True:
changed=False
for child,worker in zip(children,workers):
code=child.poll()
if code is not None and worker['state']=='running':
worker.update(state='complete' if code==0 else 'failed',returncode=code);changed=True
if changed:save('stopping' if stopping else 'running')
if all(child.poll() is not None for child in children):break
time.sleep(2)
failed=any(worker.get('returncode')!=0 for worker in workers)
save('stopped' if stopping else ('failed' if failed else 'complete'))
return 1 if failed else 0
if __name__=='__main__':
raise SystemExit(main())