Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary import (gin, gym) #1570

Open
wants to merge 2 commits into
base: pytorch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions alf/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

from absl import logging
import functools
import gin
import inspect
from inspect import Parameter
import os
import pprint
import runpy
import shutil

USE_GIN = os.environ.get('ALF_USE_GIN', "1") == "1"
if USE_GIN:
import gin

__all__ = [
'config',
'config1',
Expand Down Expand Up @@ -611,8 +614,7 @@ def _decorate(fn_or_cls, name, whitelist, blacklist):
else:
fn_or_cls = _make_wrapper(fn_or_cls, configs, signature, has_self=0)

if fn_or_cls.__module__ != '<run_path>' and os.environ.get(
'ALF_USE_GIN', "1") == "1":
if fn_or_cls.__module__ != '<run_path>' and USE_GIN:
# If a file is executed using runpy.run_path(), the module name is
# '<run_path>', which is not an acceptable name by gin.
return gin.configurable(
Expand Down
17 changes: 11 additions & 6 deletions alf/environments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import random

import alf
from alf.environments import suite_gym
from alf.environments import thread_environment, parallel_environment, fast_parallel_environment
from alf.environments import alf_wrappers

Expand Down Expand Up @@ -89,8 +88,8 @@ def _env_constructor(env_load_fn, env_name, batch_size_per_env, seed, env_id):


@alf.configurable
def create_environment(env_name='CartPole-v0',
env_load_fn=suite_gym.load,
def create_environment(env_name=None,
env_load_fn=None,
eval_env_load_fn=None,
for_evaluation=False,
num_parallel_environments=30,
Expand All @@ -108,10 +107,10 @@ def create_environment(env_name='CartPole-v0',
"""Create a batched environment.

Args:
env_name (str|list[str]): env name. If it is a list, ``MultitaskWrapper``
env_name (str|list[str]): env name (e.g 'CartPole-v0'). If it is a list, ``MultitaskWrapper``
will be used to create multi-task environments. Each one of them
consists of the environments listed in ``env_name``.
env_load_fn (Callable) : callable that create an environment
env_load_fn (Callable) : callable that create an environment (e.g. suite_gym.load)
If env_load_fn has attribute ``batched`` and it is True,
``evn_load_fn(env_name, env_id=env_id, batch_size=batch_size_per_env)``
will be used to create the batched environment. Otherwise,
Expand Down Expand Up @@ -172,6 +171,12 @@ def create_environment(env_name='CartPole-v0',

"""

if env_name is None:
env_name = 'CartPole-v0'
Copy link
Contributor

@Haichao-Zhang Haichao-Zhang Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the same as using CartPole-v0 as the default value for the argument env_name? Not sure if there is any particular reason for this change.

if env_load_fn is None:
from alf.environments import suite_gym
env_load_fn = suite_gym.load

if for_evaluation:
# for creating an evaluation environment, use ``eval_env_load_fn`` if
# provided and fall back to ``env_load_fn`` otherwise
Expand Down Expand Up @@ -266,7 +271,7 @@ def create_environment(env_name='CartPole-v0',

@alf.configurable
def load_with_random_max_episode_steps(env_name,
env_load_fn=suite_gym.load,
env_load_fn,
min_steps=200,
max_steps=250):
"""Create environment with random max_episode_steps in range
Expand Down
Loading