You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This massive bottleneck is caused by the way gym registers environments from v0.22 onwards. Consider the gym/envs/registrations.py::EnvRegistry::register() method in v0.21:
defregister(self, id, **kwargs):
ifself._nsisnotNone:
if"/"inid:
namespace, id=id.split("/")
logger.warn(
f"Custom namespace '{namespace}' is being overrode by namespace '{self._ns}'. ""If you are developing a plugin you shouldn't specify a namespace in `register` calls. ""The namespace is specified through the entry point key."
)
id=f"{self._ns}/{id}"ifidinself.env_specs:
logger.warn("Overriding environment {}".format(id))
self.env_specs[id] =EnvSpec(id, **kwargs)
defregister(self, id: str, **kwargs) ->None:
spec=EnvSpec(id, **kwargs)
ifself._nsisnotNone:
ifspec.namespaceisnotNone:
logger.warn(
f"Custom namespace `{spec.namespace}` is being overridden "f"by namespace `{self._ns}`. If you are developing a ""plugin you shouldn't specify a namespace in `register` ""calls. The namespace is specified through the ""entry point package metadata."
)
# Replace namespacespec.namespace=self._nstry:
# Get all versions of this spec.versions=self.env_specs.versions(spec.namespace, spec.name)
# We raise an error if the user is attempting to initialize an# unversioned environment when a versioned one already exists.latest_versioned_spec=max(
filter(lambdaspec: isinstance(spec.version, int), versions),
key=lambdaspec: cast(int, spec.version),
default=None,
)
unversioned_spec=next(
filter(lambdaspec: spec.versionisNone, versions), None
)
# Trying to register an unversioned spec when versioned spec existsifunversioned_specandspec.versionisnotNone:
message= (
"Can't register the versioned environment "f"`{spec.id}` when the unversioned environment "f"`{unversioned_spec.id}` of the same name already exists."
)
raiseerror.RegistrationError(message)
eliflatest_versioned_specandspec.versionisNone:
message= (
f"Can't register the unversioned environment `{spec.id}` "f"when version `{latest_versioned_spec.version}` ""of the same name already exists. Note: the default ""behavior is that the `gym.make` with the unversioned ""environment will return the latest versioned environment."
)
raiseerror.RegistrationError(message)
# We might not find this namespace or name in which case# we should continue to register the environment.except (error.NamespaceNotFound, error.NameNotFound):
passfinally:
ifspec.idinself.env_specs:
logger.warn(f"Overriding environment {id}")
self.env_specs[spec.id] =spec
Herein lies the problem: lbforaging attempts to register a large number of environments (9720 by default). With each new environment, gym checks whether any environment has already been registered with a similar name. Naturally, this fuzzy match scales really badly with a large number of environments being registered.
def_check_spec_register(spec: EnvSpec):
"""Checks whether the spec is valid to be registered. Helper function for `register`."""globalregistrylatest_versioned_spec=max(
(
spec_forspec_inregistry.values()
ifspec_.namespace==spec.namespaceandspec_.name==spec.nameandspec_.versionisnotNone
),
key=lambdaspec_: int(spec_.version), # type: ignoredefault=None,
)
unversioned_spec=next(
(
spec_forspec_inregistry.values()
ifspec_.namespace==spec.namespaceandspec_.name==spec.nameandspec_.versionisNone
),
None,
)
thus still yielding bad performance (definitely not as bad as v0.22 & v0.23 though):
$ python import_tests.py
time = 8.575s
I'm not sure what the solution for lbforaging is here?
Advise users that gym v0.22 & v0.23 are incompatible
Don't pre-register loads of environment configs. Perhaps do a lazy registration only when user asks for a given set-up? Not sure if/why this isn't a preferred approach.
PS: GitHub Markdown should renders code blocks that are linked from GitHub itself, but that isn't working here (weirdly). I've pasted the code manually.
When installing
lb-foraging
with certain versions ofgym
, the import hangs for a (very) long time.Consider a simple script,
import_tests.py
:With
gym==0.21.*
:vs. with
gym==0.22.*
:This massive bottleneck is caused by the way
gym
registers environments from v0.22 onwards. Consider thegym/envs/registrations.py::EnvRegistry::register()
method in v0.21:https://github.com/openai/gym/blob/c755d5c35a25ab118746e2ba885894ff66fb8c43/gym/envs/registration.py#L205-L217
See how this is different in v0.22:
https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L542-L596
Specifically, the issue here is the addition of:
https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L559
which calls
https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L220
which eventually hits:
https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L293
Herein lies the problem:
lbforaging
attempts to register a large number of environments (9720 by default). With each new environment,gym
checks whether any environment has already been registered with a similar name. Naturally, this fuzzy match scales really badly with a large number of environments being registered.v0.23 of
gym
takes the same approach to registration as v0.22, and thus faces the same issue. In v0.24-v0.26, the registration no longer looks for a fuzzy match (with difflib), but there is still a bottleneck due to an iteration over all registered envs:https://github.com/openai/gym/blob/dcd185843a62953e27c2d54dc8c2d647d604b635/gym/envs/registration.py#L379-L403
thus still yielding bad performance (definitely not as bad as v0.22 & v0.23 though):
I'm not sure what the solution for
lbforaging
is here?Note: I am aware that gym has officially moved to https://github.com/Farama-Foundation/Gymnasium, but the issue remains there with gymnasium v0.27:
https://github.com/Farama-Foundation/Gymnasium/blob/6f35e7f87fc5b455b8cc70e366016c463fa52850/gymnasium/envs/registration.py#L298-L321
The text was updated successfully, but these errors were encountered: