positions_from_is_init#
- class torchrl.modules.positions_from_is_init(is_init: Tensor)[source]#
Compute per-token positions within each episode segment of a window.
Positions restart at
0on everyis_initflag. The first step of the window is always treated as position0, so callers must pass episode-aligned windows:TransformerModulevalidates that every row of a training window starts withis_init=True.- Parameters:
is_init (torch.Tensor) – a boolean tensor of shape
[*batch, T]marking the first step of each episode.- Returns:
A
torch.longtensor of shape[*batch, T]holding the position of each step within its episode segment.
Examples
>>> is_init = torch.tensor([[True, False, True, False]]) >>> positions_from_is_init(is_init) tensor([[0, 1, 0, 1]])