Skip to content

Commit 1eb2a56

Browse files
authored
Reduce copying example batch allocations (#55)
1 parent 1843568 commit 1eb2a56

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

‎examples/copying_problem.py‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,12 @@ def copy_data(batch_size):
138138
seq = torch.randint(
139139
1, alphabet_size + 1, (batch_size, S), dtype=torch.long, device=device
140140
)
141-
zeros1 = torch.zeros((batch_size, L), dtype=torch.long, device=device)
142-
zeros2 = torch.zeros((batch_size, S - 1), dtype=torch.long, device=device)
143-
zeros3 = torch.zeros((batch_size, S + L), dtype=torch.long, device=device)
144-
marker = torch.full(
145-
(batch_size, 1), alphabet_size + 1, dtype=torch.long, device=device
146-
)
147141

148-
x = torch.cat([seq, zeros1, marker, zeros2], dim=1)
149-
y = torch.cat([zeros3, seq], dim=1)
142+
x = torch.zeros((batch_size, L + 2 * S), dtype=torch.long, device=device)
143+
y = torch.zeros_like(x)
144+
x[:, :S] = seq
145+
x[:, S + L] = alphabet_size + 1
146+
y[:, S + L :] = seq
150147

151148
return x, y
152149

0 commit comments

Comments
 (0)