LiveWorld / LiveWorld-Code /scripts /infer_panodata_test.py
cszhilu1998's picture
Upload folder using huggingface_hub
e6af083 verified
Raw History Blame Contribute Delete
3.79 kB
"""Portable K=1/K=21 dispatcher with optional launcher-based sample sharding."""
import argparse
import os
from pathlib import Path
import sys
def replace_option(arguments, option, value):
"""Replace both --option value and --option=value forms."""
result = []
index = 0
while index < len(arguments):
argument = arguments[index]
if argument == option:
index += 2
elif argument.startswith(option + '='):
index += 1
else:
result.append(argument)
index += 1
return [*result, option, str(value)]
def resolve_parallel_args(arguments, environment, requested=False):
"""Capture launcher rank before SAM3 initializes its own single-GPU state."""
world = int(environment.get('WORLD_SIZE', '1'))
if world <= 1 and not requested:
return list(arguments), None
rank = int(environment.get('RANK', '0'))
local_rank = int(environment.get('LOCAL_RANK', '0'))
if world < 1 or not 0 <= rank < world or local_rank < 0:
raise ValueError(f'Invalid launcher ranks: world={world}, rank={rank}, local_rank={local_rank}')
result = replace_option(arguments, '--num_shards', world)
result = replace_option(result, '--shard_rank', rank)
result = replace_option(result, '--device', f'cuda:{local_rank}')
return result, local_rank
def default_model_root(code_root):
"""HF whole-repo layout: sibling models; also accept standalone ./ckpt."""
code_root = Path(code_root).resolve()
parent = code_root.parent
if (parent / 'Wan2.1-T2V-14B').is_dir() or code_root.name == 'LiveWorld-Code':
return parent
return code_root / 'ckpt'
def main():
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--K', '--k', type=int, choices=[1, 21], default=1)
parser.add_argument('--auto_shard', action='store_true', help='Use accelerator/torchrun process ranks for sample sharding.')
selected, remaining = parser.parse_known_args()
# SAM3 overwrites RANK/WORLD_SIZE for its own predictor; capture the
# outer launcher metadata and encode it in CLI arguments first.
launcher = dict(os.environ)
remaining, local_rank = resolve_parallel_args(remaining, launcher, selected.auto_shard)
help_requested = '--help' in remaining or '-h' in remaining
if local_rank is not None and not help_requested:
import torch
if local_rank >= torch.cuda.device_count():
parser.error(f'LOCAL_RANK={local_rank} exceeds visible CUDA device count {torch.cuda.device_count()}')
torch.cuda.set_device(local_rank)
# Each independent sample worker owns a single-GPU SAM3 predictor.
os.environ['IS_MAIN_PROCESS'] = '1'
print(f'[launcher] rank={launcher.get("RANK", "0")}/{launcher.get("WORLD_SIZE", "1")} '
f'local_rank={local_rank}; independent sample shard on cuda:{local_rank}', flush=True)
root = Path(__file__).resolve().parents[1]
if not any(arg == '--model_root' or arg.startswith('--model_root=') for arg in remaining):
remaining += ['--model_root', str(default_model_root(root))]
if not any(arg == '--system-config' or arg.startswith('--system-config=') for arg in remaining):
remaining += ['--system-config', str(root / 'configs/infer_system_config_portable_14B.yaml')]
if help_requested:
print('Unified selection: --K {1,21} (default 1); --auto_shard uses launcher ranks.')
print('WORLD_SIZE>1 automatically enables per-process GPU binding and sample sharding.')
sys.argv = [sys.argv[0], *remaining]
if selected.K == 1:
from infer_panodata import main as infer
else:
from infer_panodata_k21 import main as infer
infer()
if __name__ == '__main__':
main()