mujoco-py icon indicating copy to clipboard operation
mujoco-py copied to clipboard

Failed to initialize OpenGL

Open richardrl opened this issue 7 years ago • 25 comments

I want to use raw image observations, so I am trying to call the _read_pixels_as_in_window(self) in mjviewer.py. However, getting this error:

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-384/libGL.so python trpo_run_mujoco.py --env 'Reacher-Pixel-v0'
Logging to /tmp/openai-2018-01-28-23-59-19-815938
2018-01-28 23:59:19.816385: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Device mapping: no known devices.
2018-01-28 23:59:19.816722: I tensorflow/core/common_runtime/direct_session.cc:297] Device mapping:

[2018-01-28 23:59:19,816] Making new env: Reacher-Pixel-v0
Compiling /home/richard/lib/mujoco-py/mujoco_py/cymj.pyx because it changed.
[1/1] Cythonizing /home/richard/lib/mujoco-py/mujoco_py/cymj.pyx
running build_ext
building 'mujoco_py.cymj' extension
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/gl
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/richard/lib/mujoco-py/mujoco_py -I/home/richard/.mujoco/mjpro150/include -I/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/numpy/core/include -I/home/richard/lib/mujoco-py/mujoco_py/vendor/egl -I/home/richard/anaconda3/envs/py35/include/python3.5m -c /home/richard/lib/mujoco-py/mujoco_py/cymj.c -o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/cymj.o -fopenmp -w
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/richard/lib/mujoco-py/mujoco_py -I/home/richard/.mujoco/mjpro150/include -I/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/numpy/core/include -I/home/richard/lib/mujoco-py/mujoco_py/vendor/egl -I/home/richard/anaconda3/envs/py35/include/python3.5m -c /home/richard/lib/mujoco-py/mujoco_py/gl/eglshim.c -o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/gl/eglshim.o -fopenmp -w
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/lib.linux-x86_64-3.5
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/lib.linux-x86_64-3.5/mujoco_py
gcc -pthread -shared -L/home/richard/anaconda3/envs/py35/lib -Wl,-rpath=/home/richard/anaconda3/envs/py35/lib,--no-as-needed /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/cymj.o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/gl/eglshim.o -L/home/richard/.mujoco/mjpro150/bin -L/home/richard/anaconda3/envs/py35/lib -Wl,--enable-new-dtags,-R/home/richard/.mujoco/mjpro150/bin -lmujoco150 -lglewegl -lpython3.5m -o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/lib.linux-x86_64-3.5/mujoco_py/cymj.cpython-35m-x86_64-linux-gnu.so -fopenmp
get_obs
Creating window glfw
Found 1 GPUs for rendering. Using device 0.
Could not make EGL context current
Traceback (most recent call last):
  File "trpo_run_mujoco.py", line 35, in <module>
    main()
  File "trpo_run_mujoco.py", line 31, in main
    train(args.env, num_timesteps=args.num_timesteps, seed=args.seed)
  File "trpo_run_mujoco.py", line 21, in train
    env = make_mujoco_env(env_id, workerseed)
  File "/home/richard/baselines/baselines/common/cmd_util.py", line 34, in make_mujoco_env
    env = gym.make(env_id)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/registration.py", line 164, in make
    return registry.make(id)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/registration.py", line 122, in make
    env = spec.make()
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/registration.py", line 89, in make
    env = cls(**self._kwargs)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/reacher_pixel.py", line 10, in __init__
    mujoco_env.MujocoEnv.__init__(self, 'reacher.xml', 2)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/mujoco_env.py", line 39, in __init__
    observation, _reward, done, _info = self._step(np.zeros(self.model.nu))
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/reacher_pixel.py", line 18, in _step
    ob = self._get_obs()
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/reacher_pixel.py", line 40, in _get_obs
    print(np.shape(self._get_viewer()._read_pixels_as_in_window()))
  File "/home/richard/lib/mujoco-py/mujoco_py/mjviewer.py", line 218, in _read_pixels_as_in_window
    self.sim.render(resolution[0], resolution[1])
  File "mjsim.pyx", line 149, in mujoco_py.cymj.MjSim.render
  File "mjsim.pyx", line 151, in mujoco_py.cymj.MjSim.render
  File "mjrendercontext.pyx", line 43, in mujoco_py.cymj.MjRenderContext.__init__
  File "mjrendercontext.pyx", line 99, in mujoco_py.cymj.MjRenderContext._setup_opengl_context
  File "opengl_context.pyx", line 128, in mujoco_py.cymj.OffscreenOpenGLContext.__init__
RuntimeError: Failed to initialize OpenGL

My setup works for training and rendering video of the non-pixel Mujoco environment on my screen, but this '_read_pixels_as_in_window(self)', which I am using to access raw image data, fails. And actually even this method was working until I tried installing CUDA (which overwrote my nvidia-384 driver with nvidia-390') and installing CUDA broke everything, so now I think I rolled everything back to nvidia-384 properly and yet this read_pixels is still not working.

richardrl avatar Jan 29 '18 08:01 richardrl

Are there any updates on this?

febert avatar Feb 26 '18 21:02 febert

met same issue here, track into this file: ./mujoco_py/gl/eglshim.c call eglGetError() with the place this issue happens(after eglMakeCurrent(eglDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, eglCtx)), eglGetError returns 0x3002, EGL_BAD_ACCESS, no idea from this place...

mjpro150/sample/record with same logic works fine ...

adonishong avatar Mar 05 '18 09:03 adonishong

Did anyone solve this problem?

HailinRen avatar Apr 16 '18 17:04 HailinRen

I've found that if I want to call env.render() then I need to set LD_PRELOAD to /usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-384/libGL.so.

However, if I want to call env.sim.render(w, h), then I need to not set LD_PRELOAD (e.g. run unset LD_PRELOAD).

vitchyr avatar Apr 27 '18 08:04 vitchyr

@vitchyr Thx a lot!

lutein avatar Oct 31 '18 08:10 lutein

I got the same problem,too.How to fix it?

chenyiwen97 avatar Mar 11 '19 12:03 chenyiwen97

@chenyiwen97 have you tried the solution posted above?

vitchyr avatar Mar 11 '19 15:03 vitchyr

@chenyiwen97 have you tried the solution posted above?

Yes.I add the 'export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-415/libGL.so' to the bashrc, but it still doesn't work.

chenyiwen97 avatar Mar 13 '19 08:03 chenyiwen97

@chenyiwen97 have you tried the solution posted above?

Sorry, I misunderstood what you said. Now it works.Thanks a lot.

chenyiwen97 avatar Mar 13 '19 11:03 chenyiwen97

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia-384 This works for me.

beark007 avatar May 17 '19 05:05 beark007

when training, the following has to be commented, unless do testing. export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so

beark007 avatar May 17 '19 09:05 beark007

Is there a solution to this (can't use env.render and env.sim.render in the same environment) yet? I'm experiencing the same issue. And my current work around is to set and unset environment variable "LD_PRELOAD" everytime in python scripts. Basically there is no way to call env.render() and env.sim.render(w, h) in the same environment because the first one requires export LD_PRELOAD=/usr/lib/...../libGLEW.so but the second one requires this line commented.

kracon7 avatar May 28 '19 02:05 kracon7

After lots of trying and lots of reading, with no success, I tried (for no reason at all) something that worked! Using the mode as rgb_array in env.sim.render(), I received a warning that mode should be offscreen or window. Offscreen was exactly the mode in that the error was happening... so I changed to window. At first, the only change was that, immediately after loading the env, a render screen has shown up, without the error from offscreen mode. Then, without closing the render window, I decided to run the command env.sim.render() with offscreen mode on the python shell, and it worked! I received the image without any error! So I included in my code a line calling env.sim.render() with mode window before the line with mode offscreen, which gets the camera image.

my code lines (inside my gym environment): self.sim.render(mode='window', camera_name='first-person', width=16, height=16, depth=False) img = self.sim.render(mode='offscreen', camera_name='first-person', width=16, height=16, depth=False)

luizmarao avatar Jun 14 '19 01:06 luizmarao

The most import is to check the usage inside env.render():

viewer =mujoco_py.MjRenderContextOffscreen(sim, dveice_id=)

or

viewer = mujoco_py.MjViewer(sim)

The second one should has a LD_PRELOAD= to /usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-384/libGL.so as mentioned above.

huangjiancong1 avatar Apr 23 '20 10:04 huangjiancong1

undsetting LD_PRELOAD does't work for me, is there a workaround that doesn't involve it? edit : This is an issue related to openGL and ubuntu nvidia drivers, move to a non ubuntu distro to fix this issue

madhan001 avatar Jul 02 '20 09:07 madhan001

I also had this problem, but it had to do with incorrect Ubuntu version. I had this error message using env.sim.render with Ubuntu 20.04, but once I reinstalled my machine with Ubuntu 18.04, the program ran perfectly.

arthur801031 avatar Jan 11 '21 00:01 arthur801031

Here is a better solution I found: https://github.com/openai/mujoco-py/issues/390#issuecomment-525385434

drozzy avatar Jun 08 '21 19:06 drozzy

Here is a full-writing targeting our HPC cluster: https://github.com/geyang/jaynes-starter-kit/tree/master/07_supercloud_setup

geyang avatar Jan 03 '22 01:01 geyang

I don't even have a /usr/lib/nvidia-384 folder. Only nvidia folder without any .so files inside. But actually CUDA is installed and working properly. Normal rendering in widow is working but in offscreen context not. it fails on: data = self.sim.render(width=width, height=height, camera_name=camera_name) unset LD_PRELOAD helped

axmav avatar May 03 '22 08:05 axmav

I don't even have a /usr/lib/nvidia-384 folder. Only nvidia folder without any .so files inside. But actually CUDA is installed and working properly. Normal rendering in widow is working but in offscreen context not. it fails on: data = self.sim.render(width=width, height=height, camera_name=camera_name) unset LD_PRELOAD helped

I have the same issue as you, but unset LD_PRELOAD is useless for me.

zichunxx avatar May 12 '22 02:05 zichunxx

@QUIlToT as far as I understand you can not use the gym for rendering in window as well as in background at the same time on Ubuntu. You need to switch between using LD_PRELOAD and not.

axmav avatar May 12 '22 06:05 axmav

@QUIlToT as far as I understand you can not use the gym for rendering in window as well as in background at the same time on Ubuntu. You need to switch between using LD_PRELOAD and not.

Thx! You made my day!

zichunxx avatar May 12 '22 07:05 zichunxx

I don't even have a /usr/lib/nvidia-384 folder. Only nvidia folder without any .so files inside. But actually CUDA is installed and working properly. Normal rendering in widow is working but in offscreen context not. it fails on: data = self.sim.render(width=width, height=height, camera_name=camera_name) unset LD_PRELOAD helped

Hi, I'm running code below, image

which throws RunTimeError at env.render(mode='rgb_array') like the figure below: image

Would you like to give any advice?

Thanks, Mango

Irismoon avatar Jul 06 '22 23:07 Irismoon

I was rendering on a remote server and met the same problem. I tried to open an VNC server with port 9 and export DISPLAY=:9 in the terminal, and then the problem was fixed.

geng-haoran avatar Jan 04 '24 03:01 geng-haoran

@huangjiancong1's and @vitchyr's answer guided me to the right solution on an ARM M1 Mac running an Ubuntu 20.04 VM. The NVIDIA drivers don't exist on this system (which has an Apple GPU), but making libGLX_mesa available for linking fixed things for me.

export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libGLEW.so:/usr/lib/aarch64-linux-gnu/libGLX_mesa.so.0

albee avatar Mar 30 '24 00:03 albee