-
Notifications
You must be signed in to change notification settings - Fork 1
/
tseq.py
286 lines (213 loc) · 9.75 KB
/
tseq.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from typing import Any, Dict, Sequence, List, Callable
from functools import partial
import os
import jax
import jax.numpy as jnp
import numba
import numpy as np
import optax
import flax
from flax.training.train_state import TrainState
from networks import MLPNormal
from normalizer import Normalizer
Dataset = Dict[str, jax.Array]
Params = flax.core.FrozenDict[str, Any]
@numba.njit # JAX jit inefficient here (because tracing 1M ops) !!!
def _multiscale_deltaseq(obs, done, scales):
# generate waypoints
N, D = obs.shape
NS, = scales.shape
result = np.zeros((N, NS, D), dtype=obs.dtype)
episode_boundary = N - 1
for i in range(N - 1, -1, -1):
if done[i]:
episode_boundary = i
result[i] = obs[np.minimum(i + scales, episode_boundary)] - np.expand_dims(obs[i], 0)
return result
def _load_dataset(dataset, scales):
transformed_dataset = dict(
obs=dataset["observations"],
act=dataset["actions"],
)
if len(scales):
transformed_dataset["seq"] = _multiscale_deltaseq(dataset["observations"], dataset["real_episode_terminals"], np.array(scales))
# To device
return jax.tree_map(lambda x: jax.device_put(x.astype(jnp.float32)), transformed_dataset)
@partial(jax.jit, static_argnames=["batch_size"])
def _sample_dataset(key: Any, dataset: Dataset, batch_size: int):
dataset_size = next(iter(dataset.values())).shape[0]
indices = jax.random.randint(key, (batch_size, ), 0, dataset_size)
return jax.tree_map(lambda x: x[indices], dataset)
@partial(jax.jit, static_argnames=["truncate_obs_for_actor_fn", "deterministic"])
def _infer_jit(key: Any, truncate_obs_for_actor_fn: Callable, trans: Sequence[TrainState], exec: TrainState, obs: jax.Array, deterministic: bool):
# Sequence plan
seq = [obs]
for t in reversed(trans):
pred = t.apply_fn(t.params, jnp.concatenate(seq, axis=-1), training=False).loc
seq.append(pred)
# Execute policy
# Truncate obs
seq = [truncate_obs_for_actor_fn(x) for x in seq]
act = exec.apply_fn(exec.params, jnp.concatenate(seq, axis=-1), training=False)
if deterministic:
act = act.loc
else:
act = act.sample(seed=key)
return act, seq
@partial(jax.jit, static_argnames=["truncate_obs_for_actor_fn", "idx"], donate_argnames=["trans", "exec"])
def _learn_jit(key: Any, truncate_obs_for_actor_fn: Callable, idx: int, trans: List[TrainState], exec: TrainState, batch: Dataset):
# loss fn & grad
def _mle_loss_fn(params: Params, dropout_key: Any, apply_fn: Callable, x: jax.Array, y: jax.Array):
dist = apply_fn(params, x, training=True, rngs={"dropout": dropout_key})
return -dist.log_prob(y).mean()
_mle_loss_grad = jax.value_and_grad(_mle_loss_fn)
metrics = {}
# get seq (all noisy upper level predictions)
sample_keys = jax.random.split(key, len(trans) - 1 - idx)
seq = [batch["obs"]]
for sample_key, next_stage_idx in zip(sample_keys, range(len(trans) - 1, idx, -1)):
upper_trans = trans[next_stage_idx]
upper_seq = batch["seq"][:, next_stage_idx]
noisy_seq = upper_trans.apply_fn(upper_trans.params, upper_seq, method=MLPNormal.distribution).sample(seed=sample_key)
seq.append(noisy_seq)
if idx >= 0:
# sequence predictor update
target = batch["seq"][:, idx]
# update
t_loss, t_grads = _mle_loss_grad(trans[idx].params, key, trans[idx].apply_fn, jnp.concatenate(seq, axis=-1), target)
trans[idx] = trans[idx].apply_gradients(grads=t_grads)
metrics["t_loss/{}".format(idx)] = t_loss
else:
# exec update
# truncate seq
seq = [truncate_obs_for_actor_fn(x) for x in seq]
target = batch["act"]
exec_loss, exec_grads = _mle_loss_grad(exec.params, key, exec.apply_fn, jnp.concatenate(seq, axis=-1), target)
exec = exec.apply_gradients(grads=exec_grads)
metrics["exec_loss"] = exec_loss
return metrics, trans, exec
def _create_optimizer(lr_schedule, lr, weight_decay, max_steps):
if lr_schedule == "cosine":
schedule_fn = optax.cosine_decay_schedule(-lr, max_steps)
return optax.chain(optax.scale_by_adam(),
optax.add_decayed_weights(weight_decay) if weight_decay > 0 else optax.identity(),
optax.scale_by_schedule(schedule_fn))
if lr_schedule == "constant":
return optax.adam(learning_rate=lr)
raise NotImplementedError()
class TSeqLearner:
def __init__(
self,
seed,
dataset,
actor_data_mask, # For action-free experiments
truncate_obs_for_actor_fn,
# [Hyperparameter] Sequence Learning
scales: Sequence[int],
deterministic: bool = False,
normalize_obs: bool = False,
# [Hyperparameter] Network
hidden_dims: Sequence[int] = (1024, 1024),
dropout: float = 0.0,
# [Hyperparameter] Optimization
max_steps: Dict[int, int] = None,
batch_size: int = 16384,
lr: float = 1e-3,
lr_schedule: str = "cosine",
weight_decay: float = 0.0, # 0 = off
**kwargs
):
# Config
self.batch_size = batch_size
self.max_steps = max_steps
self.normalize_obs = normalize_obs
self.deterministic = deterministic
self.truncate_obs_for_actor_fn = truncate_obs_for_actor_fn
# PRNG
self.key = jax.random.PRNGKey(seed)
self.infer_key = jax.random.PRNGKey(seed + 1)
# dataset
self.dataset = _load_dataset(dataset, scales)
if self.normalize_obs:
self.obs_normalizer = Normalizer.init_state(self.dataset["obs"])
self.dataset["obs"] = Normalizer.normalize(self.obs_normalizer, self.dataset["obs"])
if "seq" in self.dataset:
self.seq_normalizer = Normalizer.init_state(self.dataset["seq"])
self.dataset["seq"] = Normalizer.normalize(self.seq_normalizer, self.dataset["seq"])
# dataset info
example_batch = _sample_dataset(jax.random.PRNGKey(0), self.dataset, self.batch_size)
obs_dims = example_batch["obs"].shape[-1]
actor_obs_dims = truncate_obs_for_actor_fn(example_batch["obs"]).shape[-1]
actor_act_dims = example_batch["act"].shape[-1]
# actor dataset
self.actor_dataset = None
if actor_data_mask is not None:
actor_data_mask = jax.device_put(actor_data_mask)
self.actor_dataset = jax.tree_map(lambda x: x[actor_data_mask], self.dataset)
# trans models
trans_model = MLPNormal(
output_dims=obs_dims,
hidden_dims=hidden_dims,
dropout=dropout
)
num_trans = len(scales)
models_input_dims = [obs_dims * i for i in range(num_trans, 0, -1)]
self.key, *trans_keys = jax.random.split(self.key, 1 + num_trans)
self.trans = [TrainState.create(
apply_fn=trans_model.apply,
params=trans_model.init(trans_keys[idx], jnp.zeros((self.batch_size, models_input_dims[idx])), training=False),
tx=_create_optimizer(lr_schedule, lr, weight_decay, max_steps[idx] if max_steps is not None else 1)
) for idx in range(num_trans)]
# exec model
exec_model = MLPNormal(
output_dims=actor_act_dims,
hidden_dims=hidden_dims,
dropout=dropout
)
self.key, exec_key = jax.random.split(self.key)
self.exec = TrainState.create(
apply_fn=exec_model.apply,
params=exec_model.init(exec_key, jnp.zeros((self.batch_size, (num_trans + 1) * actor_obs_dims)), training=False),
tx=_create_optimizer(lr_schedule, lr, weight_decay, max_steps[-1] if max_steps is not None else 1)
)
def infer(self, obs):
# normalize obs
if hasattr(self, "obs_normalizer"):
obs_norm = Normalizer.normalize(self.obs_normalizer, obs)
else:
obs_norm = obs
# infer
self.infer_key, subkey = jax.random.split(self.infer_key)
act, seq = _infer_jit(subkey, self.truncate_obs_for_actor_fn, self.trans, self.exec, obs_norm, self.deterministic)
# denormalize plan
# if len(plan):
# if hasattr(self, "seq_normalizer"):
# plan = Normalizer.denormalize(self.seq_normalizer, plan)
# # plan (delta) to global
# plan = plan + jnp.expand_dims(obs, 1)
return act, seq
def learn_batch(self, stage_idx):
# sample batch
self.key, dataset_key, train_key = jax.random.split(self.key, 3)
if stage_idx < 0 and self.actor_dataset is not None:
# sample actor dataset
batch = _sample_dataset(dataset_key, self.actor_dataset, self.batch_size)
else:
batch = _sample_dataset(dataset_key, self.dataset, self.batch_size)
# train
metrics, self.trans, self.exec = _learn_jit(train_key, self.truncate_obs_for_actor_fn, stage_idx, self.trans, self.exec, batch)
return metrics
def load(self, dirname):
def _load_model(name, model):
with open(os.path.join(dirname, name), "rb") as f:
return model.replace(params=flax.serialization.from_bytes(model.params, f.read()))
for idx in range(len(self.trans)):
self.trans[idx] = _load_model("trans_{}".format(idx), self.trans[idx])
self.exec = _load_model("exec", self.exec)
def save(self, dirname):
def _save_model(name, model):
with open(os.path.join(dirname, name), "wb") as f:
f.write(flax.serialization.to_bytes(model.params))
for idx, t in enumerate(self.trans):
_save_model("trans_{}".format(idx), t)
_save_model("exec", self.exec)