Skip to content

Commit 350e1fd

Browse files
committed
remove cli_utils wildcard imports from examples
1 parent c7e863d commit 350e1fd

15 files changed

Lines changed: 140 additions & 124 deletions

examples/00_meta_frozen_lake.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from argparse import ArgumentParser
2+
13
import amago
24
from amago.envs.builtin.toy_gym import MetaFrozenLake
35
from amago.envs import AMAGOEnv
46
from amago.loading import DiskTrajDataset
5-
from amago.cli_utils import *
7+
from amago import cli_utils
68

79

810
def add_cli(parser):
@@ -35,27 +37,27 @@ def add_cli(parser):
3537

3638
config = {}
3739
# configure trajectory encoder (seq2seq memory model)
38-
traj_encoder_type = switch_traj_encoder(
40+
traj_encoder_type = cli_utils.switch_traj_encoder(
3941
config,
4042
arch=args.seq_model,
4143
memory_size=128,
4244
layers=3,
4345
)
4446
# configure timestep encoder
45-
tstep_encoder_type = switch_tstep_encoder(
47+
tstep_encoder_type = cli_utils.switch_tstep_encoder(
4648
config, arch="ff", n_layers=1, d_hidden=128, d_output=64, normalize_inputs=False
4749
)
4850

4951
# we're using the default exploration strategy but being overly verbose about it for the example
50-
exploration_wrapper_type = switch_exploration(
52+
exploration_wrapper_type = cli_utils.switch_exploration(
5153
config,
5254
strategy="egreedy",
5355
eps_start=1.0,
5456
eps_end=0.05,
5557
steps_anneal=1_000_000,
5658
randomize_eps=True,
5759
)
58-
use_config(config)
60+
cli_utils.use_config(config)
5961

6062
group_name = f"{args.run_name}_{args.seq_model}"
6163
for trial in range(args.trials):

examples/01_basic_gym.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import amago
77
from amago.envs import AMAGOEnv
8-
from amago.cli_utils import *
8+
from amago import cli_utils
99

1010

1111
def add_cli(parser):
@@ -26,7 +26,7 @@ def add_cli(parser):
2626

2727
if __name__ == "__main__":
2828
parser = ArgumentParser()
29-
add_common_cli(parser)
29+
cli_utils.add_common_cli(parser)
3030
add_cli(parser)
3131
args = parser.parse_args()
3232

@@ -43,24 +43,24 @@ def add_cli(parser):
4343
"amago.nets.policy_dists.Discrete.clip_prob_low": 1e-6,
4444
}
4545
# switch sequence model
46-
traj_encoder_type = switch_traj_encoder(
46+
traj_encoder_type = cli_utils.switch_traj_encoder(
4747
config,
4848
arch=args.traj_encoder,
4949
memory_size=args.memory_size,
5050
layers=args.memory_layers,
5151
)
5252
# switch agent
53-
agent_type = switch_agent(
53+
agent_type = cli_utils.switch_agent(
5454
config,
5555
args.agent_type,
5656
reward_multiplier=1.0,
5757
)
58-
use_config(config, args.configs)
58+
cli_utils.use_config(config, args.configs)
5959

6060
group_name = f"{args.run_name}_{env_name}"
6161
for trial in range(args.trials):
6262
run_name = group_name + f"_trial_{trial}"
63-
experiment = create_experiment_from_cli(
63+
experiment = cli_utils.create_experiment_from_cli(
6464
args,
6565
make_train_env=make_train_env,
6666
make_val_env=make_train_env,
@@ -73,7 +73,7 @@ def add_cli(parser):
7373
group_name=group_name,
7474
val_timesteps_per_epoch=args.eval_timesteps,
7575
)
76-
experiment = switch_async_mode(experiment, args.mode)
76+
experiment = cli_utils.switch_async_mode(experiment, args.mode)
7777
experiment.start()
7878
if args.ckpt is not None:
7979
experiment.load_checkpoint(args.ckpt)

examples/02_gymnax.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
Support for gymnax is experimental and mainly meant to test the already_vectorized
3-
env API used by XLand MiniGrid (an unsolved environment) with classic gym envs.
2+
Support for gymnax is experimental and mainly meant to test the already_vectorized
3+
env API used by XLand MiniGrid (an unsolved environment) with classic gym envs.
44
Many of the gymnax envs appear to be broken by recent versions of jax.
55
There are a couple memory/meta-RL bsuite envs where AMAGO+Transformer
66
is significantly better than the gymnax reference scores though.
@@ -24,7 +24,7 @@
2424
from amago.envs import AMAGOEnv
2525
from amago.envs.builtin.gymnax_envs import GymnaxCompatibility
2626
from amago.nets.cnn import GridworldCNN
27-
from amago.cli_utils import *
27+
from amago import cli_utils
2828

2929

3030
def add_cli(parser):
@@ -52,7 +52,7 @@ def guess_tstep_encoder(config, obs_shape):
5252
if len(obs_shape) == 3:
5353
print(f"Guessing CNN for observation of shape {obs_shape}")
5454
channels_first = np.argmin(obs_shape).item() == 0
55-
return switch_tstep_encoder(
55+
return cli_utils.switch_tstep_encoder(
5656
config,
5757
"cnn",
5858
cnn_type=GridworldCNN,
@@ -61,7 +61,7 @@ def guess_tstep_encoder(config, obs_shape):
6161
else:
6262
print(f"Guessing MLP for observation of shape {obs_shape}")
6363
dim = math.prod(obs_shape) # FFTstepEncoder will flatten the obs on input
64-
return switch_tstep_encoder(
64+
return cli_utils.switch_tstep_encoder(
6565
config,
6666
"ff",
6767
d_hidden=max(dim // 3, 128),
@@ -72,7 +72,7 @@ def guess_tstep_encoder(config, obs_shape):
7272

7373
if __name__ == "__main__":
7474
parser = ArgumentParser()
75-
add_common_cli(parser)
75+
cli_utils.add_common_cli(parser)
7676
add_cli(parser)
7777
args = parser.parse_args()
7878

@@ -81,7 +81,7 @@ def guess_tstep_encoder(config, obs_shape):
8181

8282
# config
8383
config = {}
84-
traj_encoder_type = switch_traj_encoder(
84+
traj_encoder_type = cli_utils.switch_traj_encoder(
8585
config,
8686
arch=args.traj_encoder,
8787
memory_size=args.memory_size,
@@ -91,16 +91,16 @@ def guess_tstep_encoder(config, obs_shape):
9191
test_env, env_params = gymnax.make(args.env)
9292
test_obs_shape = test_env.observation_space(env_params).shape
9393
tstep_encoder_type = guess_tstep_encoder(config, test_obs_shape)
94-
agent_type = switch_agent(config, args.agent_type)
94+
agent_type = cli_utils.switch_agent(config, args.agent_type)
9595

96-
use_config(config, args.configs)
96+
cli_utils.use_config(config, args.configs)
9797
make_env = partial(
9898
make_gymnax_amago, env_name=args.env, parallel_envs=args.parallel_actors
9999
)
100100
group_name = f"{args.run_name}_{args.env}"
101101
for trial in range(args.trials):
102102
run_name = group_name + f"_trial_{trial}"
103-
experiment = create_experiment_from_cli(
103+
experiment = cli_utils.create_experiment_from_cli(
104104
args,
105105
make_train_env=make_env,
106106
make_val_env=make_env,
@@ -116,7 +116,7 @@ def guess_tstep_encoder(config, obs_shape):
116116
l2_coeff=1e-4,
117117
save_trajs_as="npz-compressed",
118118
)
119-
experiment = switch_async_mode(experiment, args.mode)
119+
experiment = cli_utils.switch_async_mode(experiment, args.mode)
120120
amago_device = experiment.DEVICE.index or torch.cuda.current_device()
121121
env_device = jax.devices("gpu")[amago_device]
122122
with jax.default_device(env_device):

examples/03_popgym_suite.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import wandb
44

5+
import amago
56
from amago.envs.builtin.popgym_envs import POPGymAMAGO, MultiDomainPOPGymAMAGO
6-
from amago.cli_utils import *
7+
from amago import cli_utils
78

89

910
def add_cli(parser):
@@ -19,7 +20,7 @@ def add_cli(parser):
1920

2021
if __name__ == "__main__":
2122
parser = ArgumentParser()
22-
add_common_cli(parser)
23+
cli_utils.add_common_cli(parser)
2324
add_cli(parser)
2425
args = parser.parse_args()
2526

@@ -43,32 +44,32 @@ def add_cli(parser):
4344
}
4445
# fmt: on
4546

46-
traj_encoder_type = switch_traj_encoder(
47+
traj_encoder_type = cli_utils.switch_traj_encoder(
4748
config,
4849
arch=args.traj_encoder,
4950
memory_size=args.memory_size, # paper: 256
5051
layers=args.memory_layers, # paper: 3
5152
)
52-
tstep_encoder_type = switch_tstep_encoder(
53+
tstep_encoder_type = cli_utils.switch_tstep_encoder(
5354
config,
5455
arch="ff",
5556
n_layers=2,
5657
d_hidden=512,
5758
d_output=200,
5859
)
59-
agent_type = switch_agent(
60+
agent_type = cli_utils.switch_agent(
6061
config,
6162
args.agent_type,
6263
reward_multiplier=200.0 if args.multidomain else 100.0,
6364
tau=0.0025,
6465
)
6566
# steps_anneal can safely be set much lower (<500k) in most tasks. More sweeps needed.
66-
exploration_type = switch_exploration(
67+
exploration_type = cli_utils.switch_exploration(
6768
config,
6869
"egreedy",
6970
steps_anneal=1_000_000,
7071
)
71-
use_config(config, args.configs)
72+
cli_utils.use_config(config, args.configs)
7273

7374
group_name = f"{args.run_name}_{args.env}"
7475
for trial in range(args.trials):
@@ -81,7 +82,7 @@ def add_cli(parser):
8182
make_train_env = lambda: POPGymAMAGO(
8283
f"popgym-{args.env}-v0", truncated_is_done=True
8384
)
84-
experiment = create_experiment_from_cli(
85+
experiment = cli_utils.create_experiment_from_cli(
8586
args,
8687
make_train_env=make_train_env,
8788
make_val_env=make_train_env,
@@ -98,7 +99,7 @@ def add_cli(parser):
9899
grad_clip=1.0,
99100
lr_warmup_steps=2000,
100101
)
101-
experiment = switch_async_mode(experiment, args.mode)
102+
experiment = cli_utils.switch_async_mode(experiment, args.mode)
102103
experiment.start()
103104
if args.ckpt is not None:
104105
experiment.load_checkpoint(args.ckpt)

examples/04_tmaze.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from argparse import ArgumentParser
22

33
import wandb
4+
import gin
45

56
from amago.envs import AMAGOEnv
67
from amago.envs.builtin.tmaze import TMazeAltPassive, TMazeAltActive
78
from amago.envs.exploration import EpsilonGreedy
8-
from amago.cli_utils import *
9+
from amago import cli_utils
910

1011

1112
def add_cli(parser):
@@ -60,31 +61,31 @@ def current_eps(self, local_step: int):
6061

6162
if __name__ == "__main__":
6263
parser = ArgumentParser()
63-
add_common_cli(parser)
64+
cli_utils.add_common_cli(parser)
6465
add_cli(parser)
6566
args = parser.parse_args()
6667

6768
config = {
6869
"TMazeExploration.horizon": args.horizon,
6970
}
70-
traj_encoder_type = switch_traj_encoder(
71+
traj_encoder_type = cli_utils.switch_traj_encoder(
7172
config,
7273
arch=args.traj_encoder,
7374
memory_size=args.memory_size,
7475
layers=args.memory_layers,
7576
)
76-
tstep_encoder_type = switch_tstep_encoder(
77+
tstep_encoder_type = cli_utils.switch_tstep_encoder(
7778
config,
7879
arch="ff",
7980
n_layers=2,
8081
d_hidden=128,
8182
d_output=128,
8283
normalize_inputs=False,
8384
)
84-
agent_type = switch_agent(
85+
agent_type = cli_utils.switch_agent(
8586
config, args.agent_type, reward_multiplier=100.0, gamma=0.9999
8687
)
87-
use_config(config, args.configs)
88+
cli_utils.use_config(config, args.configs)
8889

8990
group_name = f"{args.run_name}_TMazePassive_H{args.horizon}"
9091
for trial in range(args.trials):
@@ -95,7 +96,7 @@ def current_eps(self, local_step: int):
9596
),
9697
env_name=f"TMazePassive-H{args.horizon}",
9798
)
98-
experiment = create_experiment_from_cli(
99+
experiment = cli_utils.create_experiment_from_cli(
99100
args,
100101
make_train_env=make_env,
101102
make_val_env=make_env,
@@ -110,7 +111,7 @@ def current_eps(self, local_step: int):
110111
sample_actions=False, # even softmax prob .999 isn't good enough for this env...
111112
exploration_wrapper_type=TMazeExploration,
112113
)
113-
switch_async_mode(experiment, args.mode)
114+
experiment = cli_utils.switch_async_mode(experiment, args.mode)
114115
experiment.start()
115116
if args.ckpt is not None:
116117
experiment.load_checkpoint(args.ckpt)

examples/05_dark_key_door.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from amago.envs.builtin.toy_gym import RoomKeyDoor
66
from amago.envs import AMAGOEnv
7-
from amago.cli_utils import *
7+
from amago import cli_utils
88

99

1010
def add_cli(parser):
@@ -41,28 +41,30 @@ def add_cli(parser):
4141

4242
if __name__ == "__main__":
4343
parser = ArgumentParser()
44-
add_common_cli(parser)
44+
cli_utils.add_common_cli(parser)
4545
add_cli(parser)
4646
args = parser.parse_args()
4747

4848
config = {}
49-
tstep_encoder_type = switch_tstep_encoder(
49+
tstep_encoder_type = cli_utils.switch_tstep_encoder(
5050
config, arch="ff", n_layers=2, d_hidden=128, d_output=64
5151
)
52-
traj_encoder_type = switch_traj_encoder(
52+
traj_encoder_type = cli_utils.switch_traj_encoder(
5353
config,
5454
arch=args.traj_encoder,
5555
memory_size=args.memory_size,
5656
layers=args.memory_layers,
5757
)
58-
agent_type = switch_agent(config, args.agent_type, reward_multiplier=100.0)
58+
agent_type = cli_utils.switch_agent(
59+
config, args.agent_type, reward_multiplier=100.0
60+
)
5961
# the fancier exploration schedule mentioned in the appendix can help
6062
# when the domain is a true meta-RL problem and the "horizon" time limit
6163
# (above) is actually relevant for resetting the task.
62-
exploration_type = switch_exploration(
64+
exploration_type = cli_utils.switch_exploration(
6365
config, "bilevel", steps_anneal=500_000, rollout_horizon=args.meta_horizon
6466
)
65-
use_config(config, args.configs)
67+
cli_utils.use_config(config, args.configs)
6668

6769
group_name = f"{args.run_name}_dark_key_door"
6870
for trial in range(args.trials):
@@ -77,7 +79,7 @@ def add_cli(parser):
7779
),
7880
env_name=f"Dark-Key-To-Door-{args.room_size}x{args.room_size}",
7981
)
80-
experiment = create_experiment_from_cli(
82+
experiment = cli_utils.create_experiment_from_cli(
8183
args,
8284
agent_type=agent_type,
8385
tstep_encoder_type=tstep_encoder_type,
@@ -91,7 +93,7 @@ def add_cli(parser):
9193
val_timesteps_per_epoch=args.meta_horizon * 4,
9294
exploration_wrapper_type=exploration_type,
9395
)
94-
switch_async_mode(experiment, args.mode)
96+
experiment = cli_utils.switch_async_mode(experiment, args.mode)
9597
experiment.start()
9698
if args.ckpt is not None:
9799
experiment.load_checkpoint(args.ckpt)

0 commit comments

Comments
 (0)