File size: 4,743 Bytes
e6e8290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
"""
GPU initialization script for MuJoCo EGL rendering in containerized environments.
This should be run before starting the notebook to ensure GPU is properly set up.
"""

import os
import subprocess
import sys

def check_nvidia_driver():
    """Check if NVIDIA driver is accessible."""
    try:
        result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)
        if result.returncode == 0:
            print("βœ“ NVIDIA driver accessible")
            print(result.stdout.split('\n')[2])  # Driver info line
            return True
        else:
            print("βœ— NVIDIA driver not accessible")
            return False
    except FileNotFoundError:
        print("βœ— nvidia-smi not found")
        return False

def check_egl_libs():
    """Check if EGL libraries are available."""
    import ctypes
    libs_to_check = [
        'libEGL.so.1',
        'libGL.so.1', 
        'libEGL_nvidia.so.0'
    ]
    
    for lib in libs_to_check:
        try:
            ctypes.CDLL(lib)
            print(f"βœ“ {lib} loaded successfully")
        except OSError as e:
            print(f"βœ— Failed to load {lib}: {e}")

def test_egl_device():
    """Test EGL device creation."""
    try:
        # Try to create an EGL display
        from OpenGL import EGL
        import ctypes
        
        # Get EGL display
        display = EGL.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY)
        if display == EGL.EGL_NO_DISPLAY:
            print("βœ— Failed to get EGL display")
            return False
            
        # Initialize EGL
        major = ctypes.c_long()
        minor = ctypes.c_long()
        if not EGL.eglInitialize(display, ctypes.byref(major), ctypes.byref(minor)):
            print("βœ— Failed to initialize EGL")
            return False
            
        print(f"βœ“ EGL initialized successfully (version {major.value}.{minor.value})")
        
        # Clean up
        EGL.eglTerminate(display)
        return True
        
    except Exception as e:
        print(f"βœ— EGL test failed: {e}")
        return False

def test_mujoco_rendering():
    """Test MuJoCo rendering capability."""
    try:
        import mujoco
        
        # Create a simple model
        xml = """
        <mujoco>
          <worldbody>
            <body>
              <geom type="box" size="1 1 1"/>
            </body>
          </worldbody>
        </mujoco>
        """
        
        model = mujoco.MjModel.from_xml_string(xml)
        
        # Try to create a renderer (this is where EGL issues usually surface)
        try:
            renderer = mujoco.Renderer(model, height=240, width=320)
            print("βœ“ MuJoCo renderer created successfully")
            return True
        except Exception as e:
            print(f"βœ— MuJoCo renderer creation failed: {e}")
            return False
            
    except ImportError:
        print("βœ— MuJoCo not installed")
        return False
    except Exception as e:
        print(f"βœ— MuJoCo test failed: {e}")
        return False

def main():
    """Run all GPU initialization checks."""
    print("πŸ”§ Initializing GPU for MuJoCo rendering...")
    print("=" * 50)
    
    # Set environment variables
    os.environ['MUJOCO_GL'] = 'egl'
    os.environ['PYOPENGL_PLATFORM'] = 'egl'
    os.environ['EGL_PLATFORM'] = 'device'
    
    print("Environment variables set:")
    print(f"  MUJOCO_GL: {os.environ.get('MUJOCO_GL')}")
    print(f"  PYOPENGL_PLATFORM: {os.environ.get('PYOPENGL_PLATFORM')}")
    print(f"  EGL_PLATFORM: {os.environ.get('EGL_PLATFORM')}")
    print()
    
    # Run checks
    checks = [
        ("NVIDIA Driver", check_nvidia_driver),
        ("EGL Libraries", lambda: check_egl_libs() or True),  # Always continue
        ("EGL Device", test_egl_device),
        ("MuJoCo Rendering", test_mujoco_rendering),
    ]
    
    results = []
    for name, check_func in checks:
        print(f"Checking {name}...")
        try:
            result = check_func()
            results.append((name, result))
        except Exception as e:
            print(f"βœ— {name} check failed with exception: {e}")
            results.append((name, False))
        print()
    
    # Summary
    print("=" * 50)
    print("πŸ” Summary:")
    all_passed = True
    for name, passed in results:
        status = "βœ“ PASS" if passed else "βœ— FAIL"
        print(f"  {name}: {status}")
        if not passed:
            all_passed = False
    
    if all_passed:
        print("\nπŸŽ‰ All checks passed! GPU rendering should work.")
        return 0
    else:
        print("\n⚠️  Some checks failed. GPU rendering may not work properly.")
        return 1

if __name__ == "__main__":
    sys.exit(main())