Skip to content

Commit 489236b

Browse files
committed
action/log-only state_mask use F pad
1 parent 4902da5 commit 489236b

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

amago/agent.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def forward(
446446
K_a = self.num_actions_for_value_in_actor_loss if not self.discrete else 1
447447
# note that the last timestep does not have an action.
448448
# we give it a fake one to make shape math work.
449-
a_buffer = torch.cat((a, a[:, -1, ...].clone().unsqueeze(1)), axis=1)
449+
a_buffer = F.pad(a, (0, 0, 0, 1), "replicate")
450450
a_buffer = repeat(a_buffer, f"b l a -> b l {G} a")
451451
C = len(self.critics)
452452
# arrays used by critic update end up in a (B, L, C, G, dim) format
@@ -458,7 +458,7 @@ def forward(
458458
D_emb = self.traj_encoder.emb_dim
459459
# 1.0 where loss at this index should count, 0.0 where is should be ignored
460460
state_mask = (~((batch.rl2s == self.pad_val).all(-1, keepdim=True))).float()[:, 1:, ...]
461-
actor_mask = torch.cat((state_mask, torch.zeros(B, 1, 1, device=d.device)), dim=1)
461+
actor_mask = F.pad(state_mask, (0, 0, 0, 1), "constant", 0.0)
462462
actor_mask = repeat(actor_mask, f"b l 1 -> b l {G} 1")
463463
critic_mask = repeat(state_mask, f"b l 1 -> b l {C} {G} 1")
464464

@@ -787,7 +787,7 @@ def forward(self, batch: Batch, log_step: bool):
787787
assert _L == L - 1
788788
G = len(self.gammas)
789789
K_c = self.num_actions_for_value_in_critic_loss
790-
a_buffer = torch.cat((a, a[:, -1, ...].clone().unsqueeze(1)), axis=1)
790+
a_buffer = F.pad(a, (0, 0, 0, 1), "replicate")
791791
a_buffer = repeat(a_buffer, f"b l a -> b l {G} a")
792792
C = len(self.critics)
793793
assert batch.rews.shape == (B, L - 1, 1)
@@ -797,9 +797,10 @@ def forward(self, batch: Batch, log_step: bool):
797797
gamma = self.gammas.to(r.device).unsqueeze(-1)
798798
D_emb = self.traj_encoder.emb_dim
799799
Bins = self.critics.num_bins
800-
state_mask = (~((batch.rl2s == self.pad_val).all(-1, keepdim=True))).float()
801-
actor_mask = repeat(state_mask, f"b l 1 -> b l {G} 1")
802-
critic_mask = repeat(state_mask[:, 1:, ...], f"b l 1 -> b l {C} {G} 1")
800+
state_mask = (~((batch.rl2s == self.pad_val).all(-1, keepdim=True))).float()[:, 1:, ...]
801+
actor_mask = F.pad(state_mask, (0, 0, 0, 1), "constant", 0.0)
802+
actor_mask = repeat(actor_mask, f"b l 1 -> b l {G} 1")
803+
critic_mask = repeat(state_mask, f"b l 1 -> b l {C} {G} 1")
803804

804805
########################
805806
## Sequence Embedding ##

0 commit comments

Comments
 (0)