File size: 10,097 Bytes
c99d198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# coding=utf-8
# Copyright 2024 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Soft-Actor-Critic agent. From XIRL by Zakka et al. [3]

This is a cleanup of [1]. For the original algorithm, see [2].

References:
    [1]: https://github.com/denisyarats/pytorch_sac
    [2]: https://arxiv.org/abs/1801.01290
    [3]: https://github.com/google-research/google-research/tree/master/xirl
"""

import typing

import ml_collections
import numpy as np
from .replay_buffer import ReplayBuffer
import torch
from torch import distributions as pyd
from torch import nn
import torch.nn.functional as F
from torch.nn.utils import clip_grad_norm_

TensorType = torch.Tensor
InfoType = typing.Dict[str, TensorType]
TrainableType = typing.Union[nn.Parameter, nn.Module]


def orthogonal_init(m):
  """Orthogonal init for Conv2D and Linear layers."""
  if isinstance(m, nn.Linear):
    nn.init.orthogonal_(m.weight.data)
    if hasattr(m.bias, "data"):
      m.bias.data.fill_(0.0)


def mlp(
    input_dim,
    hidden_dim,
    output_dim,
    hidden_depth,
    output_mod = None,
):
  """Construct an MLP module."""
  if hidden_depth == 0:
    mods = [nn.Linear(input_dim, output_dim)]
  else:
    mods = [nn.Linear(input_dim, hidden_dim), nn.ReLU(inplace=True)]
    for _ in range(hidden_depth - 1):
      mods += [nn.Linear(hidden_dim, hidden_dim), nn.ReLU(inplace=True)]
    mods += [nn.Linear(hidden_dim, output_dim)]
  if output_mod is not None:
    mods += [output_mod]
  trunk = nn.Sequential(*mods)
  return trunk


class Critic(nn.Module):
  """Critic module."""

  def __init__(
      self,
      obs_dim,
      action_dim,
      hidden_dim,
      hidden_depth,
  ):
    super().__init__()

    # self.norm = nn.LayerNorm(obs_dim)
    self.norm1 = nn.LayerNorm(2048)
    self.norm2 = nn.LayerNorm(2048)
    self.model = mlp(obs_dim + action_dim, hidden_dim, 1, hidden_depth)
    self.apply(orthogonal_init)

  def forward(self, obs, action):
    assert obs.size(0) == action.size(0)
    obs1 = self.norm1(obs[:, :2048])
    obs2 = self.norm2(obs[:, 2048:])
    obs = torch.cat((obs1, obs2), dim=1)
    obs_action = torch.cat([obs, action], dim=-1)
    return self.model(obs_action)


class DoubleCritic(nn.Module):
  """DocubleCritic module."""

  def __init__(
      self,
      obs_dim,
      action_dim,
      hidden_dim,
      hidden_depth,
  ):
    super().__init__()

    self.critic1 = Critic(obs_dim, action_dim, hidden_dim, hidden_depth)
    self.critic2 = Critic(obs_dim, action_dim, hidden_dim, hidden_depth)

  def forward(self, *args):
    return self.critic1(*args), self.critic2(*args)


class SquashedNormal(pyd.transformed_distribution.TransformedDistribution):
  """A tanh-squashed Normal distribution."""

  def __init__(self, loc, scale):
    self.loc = loc
    self.scale = scale

    self.base_dist = pyd.Normal(loc, scale)
    transforms = [pyd.TanhTransform(cache_size=1)]
    super().__init__(self.base_dist, transforms)

  @property
  def mean(self):
    mu = self.loc
    for tr in self.transforms:
      mu = tr(mu)
    return mu


class DiagGaussianActor(nn.Module):
  """A torch.distributions implementation of a diagonal Gaussian policy."""

  def __init__(
      self,
      obs_dim,
      action_dim,
      hidden_dim,
      hidden_depth,
      log_std_bounds,
  ):
    super().__init__()

    self.log_std_bounds = log_std_bounds
    self.norm1 = nn.LayerNorm(2048)
    self.norm2 = nn.LayerNorm(2048)
    self.trunk = mlp(obs_dim, hidden_dim, 2 * action_dim, hidden_depth)

    self.apply(orthogonal_init)

  def forward(self, obs):
    obs1 = self.norm1(obs[:, :2048])
    obs2 = self.norm2(obs[:, 2048:])
    obs = torch.cat((obs1, obs2), dim=1)
    mu, log_std = self.trunk(obs).chunk(2, dim=-1)

    # Constrain log_std inside [log_std_min, log_std_max].
    log_std = torch.tanh(log_std)
    log_std_min, log_std_max = self.log_std_bounds
    log_std_range = log_std_max - log_std_min
    log_std = log_std_min + 0.5 * log_std_range * (log_std + 1)

    std = log_std.exp()
    return SquashedNormal(mu, std)


def soft_update_params(
    net,
    target_net,
    tau,
):
  for param, target_param in zip(net.parameters(), target_net.parameters()):
    val = tau * param.data + (1 - tau) * target_param.data
    target_param.data.copy_(val)


class SAC(nn.Module):
  """Soft-Actor-Critic."""

  def __init__(
      self,
      device,
      config,
  ):
    super().__init__()

    self.device = device
    self.config = config

    self.action_range = config.action_range
    self.discount = config.discount
    self.critic_tau = config.critic_tau
    self.actor_update_frequency = config.actor_update_frequency
    self.critic_target_update_frequency = (
        config.critic_target_update_frequency)
    self.batch_size = config.batch_size
    self.learnable_temperature = config.learnable_temperature

    self.critic = DoubleCritic(
        config.critic.obs_dim,
        config.critic.action_dim,
        config.critic.hidden_dim,
        config.critic.hidden_depth,
    ).to(self.device)
    self.critic_target = DoubleCritic(
        config.critic.obs_dim,
        config.critic.action_dim,
        config.critic.hidden_dim,
        config.critic.hidden_depth,
    ).to(self.device)
    self.critic_target.load_state_dict(self.critic.state_dict())

    self.actor = DiagGaussianActor(
        config.actor.obs_dim,
        config.actor.action_dim,
        config.actor.hidden_dim,
        config.actor.hidden_depth,
        config.actor.log_std_bounds,
    ).to(self.device)

    print(f"critic.action_dim is {config.critic.action_dim}, actor.action_dim is {config.actor.action_dim}")

    self.log_alpha = nn.Parameter(
        torch.as_tensor(np.log(config.init_temperature), device=self.device),
        requires_grad=True,
    )

    # Set target entropy to -|A|.
    self.target_entropy = -config.critic.action_dim

    # Optimizers.
    self.actor_optimizer = torch.optim.Adam(
        self.actor.parameters(),
        lr=config.actor_lr,
        betas=config.actor_betas,
    )
    self.critic_optimizer = torch.optim.Adam(
        self.critic.parameters(),
        lr=config.critic_lr,
        betas=config.critic_betas,
    )
    self.log_alpha_optimizer = torch.optim.Adam(
        [self.log_alpha],
        lr=config.alpha_lr,
        betas=config.alpha_betas,
    )

    self.train()
    self.critic_target.train()

  def train(self, training = True):
    self.training = training
    self.actor.train(training)
    self.critic.train(training)

  @property
  def alpha(self):
    return self.log_alpha.exp()

  @torch.no_grad()
  def act(self, obs, sample = False):
    obs = torch.as_tensor(obs, device=self.device)
    dist = self.actor(obs.unsqueeze(0))
    action = dist.sample() if sample else dist.mean
    action = action.clamp(*self.action_range)
    return action.cpu().numpy()[0]

  def update_critic(
      self,
      obs,
      action,
      reward,
      next_obs,
      mask,
  ):
    with torch.no_grad():
      dist = self.actor(next_obs)
      next_action = dist.rsample()
      log_prob = dist.log_prob(next_action).sum(-1, keepdim=True)
      target_q1, target_q2 = self.critic_target(next_obs, next_action)
      target_v = (
          torch.min(target_q1, target_q2) - self.alpha.detach() * log_prob)
      target_q = reward + (mask * self.discount * target_v)

    # Get current Q estimates.
    current_q1, current_q2 = self.critic(obs, action)
    critic_loss = F.mse_loss(current_q1, target_q) + F.mse_loss(
        current_q2, target_q)

    # Optimize the critic.
    self.critic_optimizer.zero_grad()
    critic_loss.backward()
    clip_grad_norm_(self.critic.parameters(), 10.0)
    self.critic_optimizer.step()

    return {"critic_loss": critic_loss}

  def update_actor_and_alpha(
      self,
      obs,
  ):
    dist = self.actor(obs)
    action = dist.rsample()
    log_prob = dist.log_prob(action).sum(-1, keepdim=True)
    actor_q1, actor_q2 = self.critic(obs, action)

    actor_q = torch.min(actor_q1, actor_q2)
    actor_loss = (self.alpha.detach() * log_prob - actor_q).mean()
    actor_info = {
        "actor_loss": actor_loss,
        "entropy": -log_prob.mean(),
    }

    # Optimize the actor.
    self.actor_optimizer.zero_grad()
    actor_loss.backward()
    clip_grad_norm_(self.actor.parameters(), 10.0)
    self.actor_optimizer.step()

    # Optimize the temperature.
    alpha_info = {}
    if self.learnable_temperature:
      self.log_alpha_optimizer.zero_grad()
      alpha_loss = (self.alpha *
                    (-log_prob - self.target_entropy).detach()).mean()
      alpha_loss.backward()
      self.log_alpha_optimizer.step()
      alpha_info["temperature_loss"] = alpha_loss
      alpha_info["temperature"] = self.alpha

    return actor_info, alpha_info

  def update(
      self,
      replay_buffer,
      step,
  ):
    obs, action, reward, next_obs, mask, subgoal = replay_buffer.sample(self.batch_size)
    batch_info = {"batch_reward": reward.mean()}

    critic_info = self.update_critic(obs, action, reward, next_obs, mask)

    if step % self.actor_update_frequency == 0:
      actor_info, alpha_info = self.update_actor_and_alpha(obs)

    if step % self.critic_target_update_frequency == 0:
      soft_update_params(self.critic, self.critic_target, self.critic_tau)

    return {**batch_info, **critic_info, **actor_info, **alpha_info}

  def optim_dict(self):
    return {
        "actor_optimizer": self.actor_optimizer,
        "log_alpha_optimizer": self.log_alpha_optimizer,
        "critic_optimizer": self.critic_optimizer,
    }