Skip to content

Commit 99386b9

Browse files
czxttklfacebook-github-bot
authored andcommitted
quick fix (#634)
Summary: Pull Request resolved: #634 When KeyedJaggedTensor doesn't have weights, `.weights()` will throw an assertion error. We should use `.weights_or_none()` to check if a KJT has weights. Reviewed By: BerenLuthien Differential Revision: D36005910 fbshipit-source-id: dbef99a6aad7ecf5d9bb85ec51899053a18e70eb
1 parent cc5091e commit 99386b9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

reagent/core/torch_utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def reorder_data_kjt(x: KeyedJaggedTensor, indices: torch.Tensor):
224224
[torch.cat([x[y] for y in indices.tolist()]) for x in splitted_vals_per_key]
225225
)
226226
reordered_lengths = torch.cat([x[indices] for x in val_lens_per_key])
227-
if x.weights() is not None:
227+
if x.weights_or_none() is not None:
228228
weights_per_key = torch.tensor_split(x.weights(), acc_lengths_per_key)[:-1]
229229
splitted_weights_per_key = [
230230
torch.tensor_split(x, torch.cumsum(y, dim=0))[:-1]
@@ -282,7 +282,7 @@ def shift_kjt_by_one(x: KeyedJaggedTensor):
282282
shifted_lengths = torch.cat(
283283
[torch.cat([x[1:], torch.tensor([0])]) for x in val_lens_per_key]
284284
)
285-
if x.weights() is not None:
285+
if x.weights_or_none() is not None:
286286
weights_per_key = torch.tensor_split(x.weights(), acc_lengths_per_key)[:-1]
287287
shifted_weights = torch.cat(
288288
[x[y[0] :] for x, y in zip(weights_per_key, val_lens_per_key)]

reagent/test/base/test_utils.py

+22
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def test_reorder_data_kjt(self) -> None:
120120
weights = values / 10.0
121121
lengths = torch.tensor([2, 0, 1, 1, 1, 2])
122122

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

135+
# Without weights
136+
x = KeyedJaggedTensor(keys=keys, values=values, lengths=lengths)
137+
y = reorder_data_kjt(x, torch.tensor([2, 1, 0]))
138+
self.assertEqual(y.keys(), keys)
139+
assert torch.allclose(
140+
y.values(), torch.tensor([2.0, 0.0, 1.0, 5.0, 6.0, 4.0, 3.0])
141+
)
142+
assert torch.allclose(y.lengths(), torch.tensor([1, 0, 2, 2, 1, 1]))
143+
134144
def test_shift_kjt_by_one(self) -> None:
135145
"""Test the example in the docstring of shift_kjt_by_one"""
136146
keys = ["Key0", "Key1"]
137147
values = torch.arange(7).float()
138148
weights = values / 10.0
139149
lengths = torch.tensor([2, 0, 1, 1, 1, 2])
140150

151+
# With weights
141152
x = KeyedJaggedTensor(
142153
keys=keys, values=values, lengths=lengths, weights=weights
143154
)
@@ -146,3 +157,14 @@ def test_shift_kjt_by_one(self) -> None:
146157
assert torch.allclose(y.values(), torch.tensor([2.0, 4.0, 5.0, 6.0]))
147158
assert torch.allclose(y.lengths(), torch.tensor([0, 1, 0, 1, 2, 0]))
148159
assert torch.allclose(y.weights(), y.values() / 10.0)
160+
161+
# Without weights
162+
x = KeyedJaggedTensor(
163+
keys=keys,
164+
values=values,
165+
lengths=lengths,
166+
)
167+
y = shift_kjt_by_one(x)
168+
self.assertEqual(y.keys(), keys)
169+
assert torch.allclose(y.values(), torch.tensor([2.0, 4.0, 5.0, 6.0]))
170+
assert torch.allclose(y.lengths(), torch.tensor([0, 1, 0, 1, 2, 0]))

0 commit comments

Comments
 (0)