Skip to content

Commit 5a09289

Browse files
committed
[BugFix] Fix SliceSampler for torch.compile compatibility
Convert seq_length to tensor before torch.where operations to avoid torch.compile inductor C++ codegen bugs with mixed scalar/tensor int64 in blendv operations. ghstack-source-id: 3ee8327 Pull-Request: #3298
1 parent 4507f16 commit 5a09289

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

torchrl/data/replay_buffers/samplers.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,14 +1556,30 @@ def _get_index(
15561556
out_of_traj = relative_starts < 0
15571557
if out_of_traj.any():
15581558
# a negative start means sampling fewer elements
1559+
# Convert seq_length to tensor to avoid torch.compile inductor C++ codegen
1560+
# bug with mixed scalar/tensor int64 in blendv operations (see PyTorch #xyz)
1561+
seq_length_t = torch.as_tensor(
1562+
seq_length,
1563+
dtype=relative_starts.dtype,
1564+
device=relative_starts.device,
1565+
)
15591566
seq_length = torch.where(
1560-
~out_of_traj, seq_length, seq_length + relative_starts
1567+
~out_of_traj, seq_length_t, seq_length_t + relative_starts
1568+
)
1569+
relative_starts = torch.where(
1570+
~out_of_traj, relative_starts, torch.zeros_like(relative_starts)
15611571
)
1562-
relative_starts = torch.where(~out_of_traj, relative_starts, 0)
15631572
if self.span[1]:
15641573
out_of_traj = relative_starts + seq_length > lengths[traj_idx]
15651574
if out_of_traj.any():
15661575
# a negative start means sampling fewer elements
1576+
# Convert seq_length to tensor if it's still a scalar
1577+
if not isinstance(seq_length, torch.Tensor):
1578+
seq_length = torch.as_tensor(
1579+
seq_length,
1580+
dtype=relative_starts.dtype,
1581+
device=relative_starts.device,
1582+
)
15671583
seq_length = torch.minimum(
15681584
seq_length, lengths[traj_idx] - relative_starts
15691585
)

0 commit comments

Comments
 (0)