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

Fix padding for int contexts #227

Merged
merged 7 commits into from
Dec 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/chronos/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def left_pad_and_stack_1D(tensors: List[torch.Tensor]) -> torch.Tensor:
size=(max_len - len(c),), fill_value=torch.nan, device=c.device
)
padded.append(torch.concat((padding, c), dim=-1))
return torch.stack(padded).to(tensors[0])
return torch.stack(padded)
2 changes: 1 addition & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: Apache-2.0
29 changes: 29 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import pytest
import torch

from chronos.utils import left_pad_and_stack_1D


@pytest.mark.parametrize(
"tensors",
[
[
torch.tensor([2.0, 3.0], dtype=dtype),
torch.tensor([4.0, 5.0, 6.0], dtype=dtype),
torch.tensor([7.0, 8.0, 9.0, 10.0], dtype=dtype),
]
for dtype in [torch.int, torch.float16, torch.float32]
],
)
def test_pad_and_stack(tensors: list):
stacked_and_padded = left_pad_and_stack_1D(tensors)

assert stacked_and_padded.dtype == torch.float32
assert stacked_and_padded.shape == (len(tensors), max(len(t) for t in tensors))

ref = torch.concat(tensors).to(dtype=stacked_and_padded.dtype)
abdulfatir marked this conversation as resolved.
Show resolved Hide resolved

assert torch.sum(torch.nan_to_num(stacked_and_padded, nan=0)) == torch.sum(ref)
2 changes: 1 addition & 1 deletion test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def validate_tensor(
assert a.shape == shape

if dtype is not None:
assert a.dtype == dtype
assert a.dtype == dtype
Loading