-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (146 loc) · 4.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import random
from mpi4py import MPI
import numpy as np
import torch
from helpers import logger
from helpers.argparsers import argparser
from helpers.experiment import ExperimentInitializer
from helpers.distributed_util import setup_mpi_gpus
from helpers.env_makers import make_env
from agents import orchestrator
from helpers.dataset import DemoDataset
from agents.ddpg_agent import DDPGAgent
from agents.sam_agent import SAMAgent
def train(args):
"""Train an agent"""
# Get the current process rank
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
world_size = comm.Get_size()
args.algo = args.algo + '_' + str(world_size).zfill(3)
torch.set_num_threads(1)
# Initialize and configure experiment
experiment = ExperimentInitializer(args, rank=rank, world_size=world_size)
experiment.configure_logging()
# Create experiment name
experiment_name = experiment.get_name()
# Set device-related knobs
if args.cuda:
assert torch.cuda.is_available()
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
device = torch.device("cuda:0")
setup_mpi_gpus()
else:
os.environ["CUDA_VISIBLE_DEVICES"] = "" # kill any possibility of usage
device = torch.device("cpu")
args.device = device # add the device to hps for convenience
logger.info("device in use: {}".format(device))
# Seedify
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
worker_seed = args.seed + (1000000 * (rank + 1))
eval_seed = args.seed + 1000000
# Create environment
env = make_env(args.env_id, worker_seed)
# Create an agent wrapper
if args.algo.split('_')[0] == 'ddpg-td3':
def agent_wrapper():
return DDPGAgent(
env=env,
device=device,
hps=args,
)
elif args.algo.split('_')[0] == 'sam-dac':
# Create the expert demonstrations dataset from expert trajectories
expert_dataset = DemoDataset(
expert_path=args.expert_path,
num_demos=args.num_demos,
env=env,
wrap_absorb=args.wrap_absorb,
)
def agent_wrapper():
return SAMAgent(
env=env,
device=device,
hps=args,
expert_dataset=expert_dataset,
)
else:
raise NotImplementedError("algorithm not covered")
# Create an evaluation environment not to mess up with training rollouts
eval_env = None
if rank == 0:
eval_env = make_env(args.env_id, eval_seed)
# Train
orchestrator.learn(
args=args,
rank=rank,
env=env,
eval_env=eval_env,
agent_wrapper=agent_wrapper,
experiment_name=experiment_name,
)
# Close environment
env.close()
# Close the eval env
if eval_env is not None:
assert rank == 0
eval_env.close()
def evaluate(args):
"""Evaluate an agent"""
# Initialize and configure experiment
experiment = ExperimentInitializer(args)
experiment.configure_logging()
# Create experiment name
experiment_name = experiment.get_name()
# Seedify
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
random.seed(args.seed)
# Create environment
env = make_env(args.env_id, args.seed)
# Create an agent wrapper
if args.algo == 'ddpg-td3':
def agent_wrapper():
return DDPGAgent(
env=env,
device='cpu',
hps=args,
)
elif args.algo == 'sam-dac':
def agent_wrapper():
return SAMAgent(
env=env,
device='cpu',
hps=args,
expert_dataset=None,
)
else:
raise NotImplementedError("algorithm not covered")
# Evaluate
orchestrator.evaluate(
args=args,
env=env,
agent_wrapper=agent_wrapper,
experiment_name=experiment_name,
)
# Close environment
env.close()
if __name__ == '__main__':
_args = argparser().parse_args()
# Make the paths absolute
_args.root = os.path.dirname(os.path.abspath(__file__))
for k in ['checkpoints', 'logs', 'videos', 'replays']:
new_k = "{}_dir".format(k[:-1])
vars(_args)[new_k] = os.path.join(_args.root, 'data', k)
if _args.task == 'train':
train(_args)
elif _args.task == 'eval':
evaluate(_args)
else:
raise NotImplementedError