Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quick fix #634

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions reagent/core/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def reorder_data_kjt(x: KeyedJaggedTensor, indices: torch.Tensor):
[torch.cat([x[y] for y in indices.tolist()]) for x in splitted_vals_per_key]
)
reordered_lengths = torch.cat([x[indices] for x in val_lens_per_key])
if x.weights() is not None:
if x.weights_or_none() is not None:
weights_per_key = torch.tensor_split(x.weights(), acc_lengths_per_key)[:-1]
splitted_weights_per_key = [
torch.tensor_split(x, torch.cumsum(y, dim=0))[:-1]
Expand Down Expand Up @@ -282,7 +282,7 @@ def shift_kjt_by_one(x: KeyedJaggedTensor):
shifted_lengths = torch.cat(
[torch.cat([x[1:], torch.tensor([0])]) for x in val_lens_per_key]
)
if x.weights() is not None:
if x.weights_or_none() is not None:
weights_per_key = torch.tensor_split(x.weights(), acc_lengths_per_key)[:-1]
shifted_weights = torch.cat(
[x[y[0] :] for x, y in zip(weights_per_key, val_lens_per_key)]
Expand Down
22 changes: 22 additions & 0 deletions reagent/test/base/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def test_reorder_data_kjt(self) -> None:
weights = values / 10.0
lengths = torch.tensor([2, 0, 1, 1, 1, 2])

# With weights
x = KeyedJaggedTensor(
keys=keys, values=values, lengths=lengths, weights=weights
)
Expand All @@ -131,13 +132,23 @@ def test_reorder_data_kjt(self) -> None:
assert torch.allclose(y.lengths(), torch.tensor([1, 0, 2, 2, 1, 1]))
assert torch.allclose(y.weights(), y.values() / 10.0)

# Without weights
x = KeyedJaggedTensor(keys=keys, values=values, lengths=lengths)
y = reorder_data_kjt(x, torch.tensor([2, 1, 0]))
self.assertEqual(y.keys(), keys)
assert torch.allclose(
y.values(), torch.tensor([2.0, 0.0, 1.0, 5.0, 6.0, 4.0, 3.0])
)
assert torch.allclose(y.lengths(), torch.tensor([1, 0, 2, 2, 1, 1]))

def test_shift_kjt_by_one(self) -> None:
"""Test the example in the docstring of shift_kjt_by_one"""
keys = ["Key0", "Key1"]
values = torch.arange(7).float()
weights = values / 10.0
lengths = torch.tensor([2, 0, 1, 1, 1, 2])

# With weights
x = KeyedJaggedTensor(
keys=keys, values=values, lengths=lengths, weights=weights
)
Expand All @@ -146,3 +157,14 @@ def test_shift_kjt_by_one(self) -> None:
assert torch.allclose(y.values(), torch.tensor([2.0, 4.0, 5.0, 6.0]))
assert torch.allclose(y.lengths(), torch.tensor([0, 1, 0, 1, 2, 0]))
assert torch.allclose(y.weights(), y.values() / 10.0)

# Without weights
x = KeyedJaggedTensor(
keys=keys,
values=values,
lengths=lengths,
)
y = shift_kjt_by_one(x)
self.assertEqual(y.keys(), keys)
assert torch.allclose(y.values(), torch.tensor([2.0, 4.0, 5.0, 6.0]))
assert torch.allclose(y.lengths(), torch.tensor([0, 1, 0, 1, 2, 0]))