Cython Compiler Error When Running GymEnv Library in Python

The error happens on the last line of this code section:

import warnings
warnings.filterwarnings("ignore")
from torch import multiprocessing


from collections import defaultdict

import matplotlib.pyplot as plt
import torch

from tensordict.nn import TensorDictModule
from tensordict.nn.distributions import NormalParamExtractor
from torch import nn
from torchrl.collectors import SyncDataCollector
from torchrl.data.replay_buffers import ReplayBuffer
from torchrl.data.replay_buffers.samplers import SamplerWithoutReplacement
from torchrl.data.replay_buffers.storages import LazyTensorStorage
from torchrl.envs import (Compose, DoubleToFloat, ObservationNorm, StepCounter,
                          TransformedEnv)
from torchrl.envs.libs.gym import GymEnv
from torchrl.envs.utils import check_env_specs, ExplorationType, set_exploration_type
from torchrl.modules import ProbabilisticActor, TanhNormal, ValueOperator
from torchrl.objectives import ClipPPOLoss
from torchrl.objectives.value import GAE
from tqdm import tqdm

base_env = GymEnv("InvertedDoublePendulum-v2", device=device)

The error says:

Exception has occurred: CompileError
c:UsersforhaAppDataLocalProgramsPythonPython311Libsite-packagesmujoco_pycymj.pyx
  File "C:UsersforhaOneDriveDocumentsGitHubF1car-wAIPTtest.py", line 51, in <module>
    base_env = GymEnv("InvertedDoublePendulum-v2", device=device)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Cython.Compiler.Errors.CompileError: c:UsersforhaAppDataLocalProgramsPythonPython311Libsite-packagesmujoco_pycymj.pyx

And the full terminal compliant is:


A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "c:UsersforhaAppDataLocalProgramsPythonPython311Librunpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:UsersforhaAppDataLocalProgramsPythonPython311Librunpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpyadapter/../..debugpylauncher/../..debugpy__main__.py", line 39, in <module>
    cli.main()
  File "c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpyadapter/../..debugpylauncher/../..debugpy/..debugpyservercli.py", line 430, in main
    run()
  File "c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpyadapter/../..debugpylauncher/../..debugpy/..debugpyservercli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 321, in run_path        
    return _run_module_code(code, init_globals, run_name,
  File "c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 124, in _run_code       
    exec(code, run_globals)
  File "C:UsersforhaOneDriveDocumentsGitHubF1car-wAIPTtest.py", line 32, in <module>
    else torch.device("cpu")
Compiling c:UsersforhaAppDataLocalProgramsPythonPython311Libsite-packagesmujoco_pycymj.pyx because it changed.
[1/1] Cythonizing c:UsersforhaAppDataLocalProgramsPythonPython311Libsite-packagesmujoco_pycymj.pyx

Error compiling Cython file:
------------------------------------------------------------
...
#       mj_somefunc()
cdef object py_warning_exception = None
cdef object py_error_exception = None


cdef void c_warning_callback(const char *msg) with gil except *:
                                                       ^
------------------------------------------------------------

c:UsersforhaAppDataLocalProgramsPythonPython311Libsite-packagesmujoco_pycymj.pyx:67:55: Syntax error in C variable declaration
c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpy_vendoredpydevdpydev_ipythonmatplotlibtools.py:55: MatplotlibDeprecationWarning: The interactive_bk attribute was deprecated in Matplotlib 3.9 and will be removed in 3.11. Use ``matplotlib.backends.backend_registry.list_builtin(matplotlib.backends.BackendFilter.INTERACTIVE)`` instead.
  from matplotlib.rcsetup import interactive_bk, non_interactive_bk  # @UnresolvedImport
c:Usersforha.vscodeextensionsms-python.debugpy-2024.6.0-win32-x64bundledlibsdebugpy_vendoredpydevdpydev_ipythonmatplotlibtools.py:55: MatplotlibDeprecationWarning: The non_interactive_bk attribute was deprecated in Matplotlib 3.9 and will be removed in 3.11. Use ``matplotlib.backends.backend_registry.list_builtin(matplotlib.backends.BackendFilter.NON_INTERACTIVE)`` instead.
  from matplotlib.rcsetup import interactive_bk, non_interactive_bk  # @UnresolvedImport
Backend tkagg is interactive backend. Turning interactive mode on.

After doing some research, I thought I needed to change the cymj file mujoco_py, since apparently GymEnv uses it and it’s complaining. So if it helps, here’s that file that I edited, but it still wasn’t working.

# cython: language_level=3
import copy
import logging
import os
import platform
import tempfile
import sys
from collections import namedtuple
from libc.stdlib cimport malloc, free
from libc.string cimport strncpy
from numbers import Number
from tempfile import TemporaryDirectory

import numpy as np
from cython cimport view
from cython.parallel import parallel, prange
from mujoco_py.generated import const

include "generated/wrappers.pxi"
include "opengl_context.pyx"
include "mjsim.pyx"
include "mjsimstate.pyx"
include "mjrendercontext.pyx"
include "mjbatchrenderer.pyx"
include "mjpid.pyx"

cdef extern from "gl/glshim.h":

    cdef int usingEGL()
    cdef int initOpenGL(int device_id)
    cdef void closeOpenGL()
    cdef int makeOpenGLContextCurrent(int device_id)
    cdef int setOpenGLBufferSize(int device_id, int width, int height)

    cdef unsigned int createPBO(int width, int height, int batchSize, int use_short)
    cdef void freePBO(unsigned int pixelBuffer)
    cdef void copyFBOToPBO(mjrContext* con,
                           unsigned int pbo_rgb, unsigned int pbo_depth,
                           mjrRect viewport, int bufferOffset)
    cdef void readPBO(unsigned char *buffer_rgb, unsigned short *buffer_depth,
                      unsigned int pbo_rgb, unsigned int pbo_depth,
                      int width, int height, int batchSize)



# TODO: make this function or class so these comments turn into doc strings:

# Python warning callback function, which is set
# MuJoCo has a user-settable callback function for warnings: mju_user_warning()
# We want to supply a python function, and be able to raise exceptions.
# To do this we have to wrap two things:

# This is the python callback function.  We save it in the global() context
# so we can access it from a C wrapper function (c_warning_callback)
cdef object py_warning_callback
cdef object py_error_callback
# This is the saved exception.  Because the C callback can not propagate
# exceptions, this must be set to None before calling into MuJoCo, and then
# inspected afterwards.
# These are combined in a simple class which handles both:
#   with wrap_mujoco_warning():
#       mj_somefunc()
cdef object py_warning_exception = None
cdef object py_error_exception = None


cdef void c_warning_callback(const char *msg) with gil except *:
    '''
    Wraps the warning callback so we can raise exceptions.
    Because callbacks can't propagate exceptions, we set a global that has
        to be inspected later.
    Use wrap_mujoco_warning() to check for that saved exception and
        re-raise it back to the caller.
    '''
    global py_warning_callback
    try:
        (<object> py_warning_callback)(msg)
    except Exception as e:
        global py_warning_exception
        py_warning_exception = e

def set_warning_callback(warn):
    '''
    Set a user-defined warning callback.  It should take in a string message
        (the warning string) and raise an Exception.
    See c_warning_callback, which is the C wrapper to the user defined function
    '''
    global py_warning_callback
    global mju_user_warning
    py_warning_callback = warn
    mju_user_warning = c_warning_callback

def set_error_callback(err_callback):
    '''
    Set a user-defined error callback.  It should take in a string message
        (the error string) and raise an Exception.
    See c_error_callback, which is the C wrapper to the user defined function
    '''
    global py_error_callback
    global mju_user_error
    py_error_callback = err_callback
    mju_user_error = c_error_callback


def get_warning_callback():
    '''
    Returns the user-defined warning callback, for use in e.g. a conext
    manager.
    '''
    global py_warning_callback
    return py_warning_callback


cdef void c_error_callback(const char *msg) with gil except *:
    '''
    Wraps the error callback so that we can pass a python function to the callback.
    MuJoCo error handlers are expected to terminate the program and never return.
    '''
    global py_error_callback
    try:
        (<object> py_error_callback)(msg)
    except Exception as e:
        global py_error_exception
        py_error_exception = e


def set_error_callback(err_callback):
    '''
    Set a user-defined error callback.  It should take in a string message
        (the error string) and raise an Exception.
    See c_error_callback, which is the C wrapper to the user defined function
    '''
    global py_error_callback
    global mju_user_error
    py_error_callback = err_callback
    mju_user_error = c_error_callback

def get_error_callback():
    '''
    Returns the user-defined warning callback, for use in e.g. a context
    manager.
    '''
    global py_error_callback
    return py_error_callback


class wrap_mujoco_warning(object):
    '''
    Class to wrap capturing exceptions raised during warning callbacks.
    Use this to capture warnings in mujoco calls.  Example:
        with wrap_mujoco_warning():
            mj_somefunc()
    '''
    def __enter__(self):
        global py_warning_exception
        py_warning_exception = None
        global py_error_exception
        py_error_exception = None

    def __exit__(self, type, value, traceback):
        global py_warning_exception
        global py_error_exception

        if py_warning_exception is not None:
            raise py_warning_exception

        if py_error_exception is not None:
            raise py_error_exception

def load_model_from_path(str path):
    """Loads model from path."""
    cdef char errstr[300]
    cdef mjModel *model
    with wrap_mujoco_warning():
        if (path.endswith(".mjb")):
            model = mj_loadModel(path.encode(), NULL)
        elif (path.endswith(".xml")):
            model = mj_loadXML(path.encode(), NULL, errstr, 300)
        else:
            raise RuntimeError("Unrecognized extension for %s. Expected .xml or .mjb" % path)

    if model == NULL:
        raise Exception('Failed to load XML file: %s. mj_loadXML error: %s' % (path, errstr,))
    return WrapMjModel(model)

def load_model_from_xml(str xml_str):
    """
    Loads and returns a PyMjModel model from a string containing XML markup.
    Saves the XML string used to create the returned model in `model.xml`.
    """
    cdef char errstr[300]
    cdef mjModel *model
    with wrap_mujoco_warning():
        with tempfile.NamedTemporaryFile(suffix=".xml", delete=True) as fp:
            fp.write(xml_str.encode())
            fp.flush()
            model = mj_loadXML(fp.name.encode(), NULL, errstr, 300)
    if model == NULL:
        raise Exception('%snFailed to load XML from string. mj_loadXML error: %s' % (xml_str, errstr,))
    return WrapMjModel(model)


def load_model_from_mjb(bytes mjb_bytes):
    """
    Loads and returns a PyMjModel model from bytes encoded MJB.
    MJB is a MuJoCo-custom format that includes assets like meshes/textures.
    """
    cdef mjModel *model
    with wrap_mujoco_warning():
        with TemporaryDirectory() as td:
            filename = os.path.join(td, 'model.mjb')
            with open(filename, 'wb') as f:
                f.write(mjb_bytes)
            model = mj_loadModel(filename.encode(), NULL)
    if model == NULL:
        raise Exception('%snFailed to load MJB')
    return WrapMjModel(model)

And thanks in advance for the help.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật