"""Build Detectron2 in the isolated R2 vendor directory on a Slurm CPU node.""" from pathlib import Path import subprocess,urllib.request,zipfile,io,json,os,sysconfig,tarfile ROOT=Path(__file__).resolve().parents[1];vendor=ROOT/'vendor' url='https://download.pytorch.org/whl/cpu/torchvision-0.20.1%2Bcpu-cp39-cp39-linux_x86_64.whl' if not (vendor/'torchvision').exists(): with urllib.request.urlopen(url,timeout=120) as r:blob=r.read() zipfile.ZipFile(io.BytesIO(blob)).extractall(vendor) print('TORCH_IMPORT',flush=True) import torch print('TORCH',torch.__version__,torch.cuda.is_available(),flush=True) d2=ROOT/'third_party/detectron2-0.6' if not (Path(sysconfig.get_path('include'))/'Python.h').exists(): headers=ROOT/'third_party/Python-3.9.16' if not headers.exists(): with urllib.request.urlopen('https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz',timeout=120) as r:blob=r.read() with tarfile.open(fileobj=io.BytesIO(blob),mode='r:gz') as archive:archive.extractall(ROOT/'third_party') if not (headers/'pyconfig.h').exists(): subprocess.run(['./configure','--without-ensurepip'],cwd=headers,check=True) os.environ['CPATH']=str(headers/'Include')+':'+str(headers)+(':'+os.environ['CPATH'] if os.getenv('CPATH') else '') subprocess.run(['python3','-m','pip','install','--target',str(vendor),'--no-deps','--no-build-isolation',str(d2)],check=True) # Activate the official PyTorch fallback, which is already implemented in the # unmodified MSDeformAttn.forward method. No parameter or numerical rule change. repo=ROOT/'third_party/ReLA-3ca955198da8ace68f8980b6fcbada0791cc51c2' p=repo/'gres_model/modeling/pixel_decoder/ops/functions/ms_deform_attn_func.py' s=p.read_text();needle=' raise ModuleNotFoundError(info_string)' if needle in s:p.write_text(s.replace(needle,' MSDA = None # R2: use the repository reference fallback')) (ROOT/'protocol/rela_runtime_patch.json').write_text(json.dumps(dict(file=str(p.relative_to(ROOT)),change='Replace import-time error with MSDA=None; existing forward try/except invokes official grid_sample reference implementation',torch=torch.__version__,torchvision='0.20.1+cpu',slurm_job_id=os.getenv('SLURM_JOB_ID')),indent=2)) print('D2_BUILD_COMPLETE',flush=True)