"""Resolve EGL's own device enumeration before running a MuJoCo renderer. CUDA ordinals are not EGL ordinals. CPU/Mesa containers can expose a single EGL device despite having several CUDA GPUs. Never pass CUDA=3 blindly to EGL. """ import os from pathlib import Path import runpy import sys def selected_device(count, requested): if count < 1: raise RuntimeError("No EGL rendering device available") if count == 1: return "0" if requested is None: return None value = int(requested) if not 0 <= value < count: raise ValueError(f"EGL device {value} outside available 0..{count-1}") return str(value) def main(): from mujoco.egl import egl_ext as EGL count = len(EGL.eglQueryDevicesEXT()) requested = os.environ.get("HFD_RENDER_EGL_DEVICE_ID", os.environ.get("MUJOCO_EGL_DEVICE_ID")) selected = selected_device(count, requested) if selected is not None: os.environ["MUJOCO_EGL_DEVICE_ID"] = selected print(f"[render] EGL devices={count} selected={selected or 'auto'} " f"(CUDA allocation={os.environ.get('CUDA_VISIBLE_DEVICES', 'all')}; independent enumerations)", flush=True) target = Path(sys.argv[1]).resolve() sys.argv = [str(target), *sys.argv[2:]] sys.path.insert(0, str(target.parent)) runpy.run_path(str(target), run_name="__main__") if __name__ == "__main__": main()