Skip to content

Commit 4679d0a

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: 8e546ee Pull-Request: #3298
1 parent d7ef78b commit 4679d0a

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
@@ -2090,14 +2090,30 @@ def _get_index(
20902090
out_of_traj = relative_starts < 0
20912091
if out_of_traj.any():
20922092
# a negative start means sampling fewer elements
2093+
# Convert seq_length to tensor to avoid torch.compile inductor C++ codegen
2094+
# bug with mixed scalar/tensor int64 in blendv operations (see PyTorch #xyz)
2095+
seq_length_t = torch.as_tensor(
2096+
seq_length,
2097+
dtype=relative_starts.dtype,
2098+
device=relative_starts.device,
2099+
)
20932100
seq_length = torch.where(
2094-
~out_of_traj, seq_length, seq_length + relative_starts
2101+
~out_of_traj, seq_length_t, seq_length_t + relative_starts
2102+
)
2103+
relative_starts = torch.where(
2104+
~out_of_traj, relative_starts, torch.zeros_like(relative_starts)
20952105
)
2096-
relative_starts = torch.where(~out_of_traj, relative_starts, 0)
20972106
if self.span[1]:
20982107
out_of_traj = relative_starts + seq_length > lengths[traj_idx]
20992108
if out_of_traj.any():
21002109
# a negative start means sampling fewer elements
2110+
# Convert seq_length to tensor if it's still a scalar
2111+
if not isinstance(seq_length, torch.Tensor):
2112+
seq_length = torch.as_tensor(
2113+
seq_length,
2114+
dtype=relative_starts.dtype,
2115+
device=relative_starts.device,
2116+
)
21012117
seq_length = torch.minimum(
21022118
seq_length, lengths[traj_idx] - relative_starts
21032119
)

0 commit comments

Comments
 (0)