generated from AzulGarza/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 56
fix: change Chronos default dtype from bfloat16 to float32 #309
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
Merged
+120
−4
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1899b17
fix: change Chronos default dtype from bfloat16 to float32
ce3eb38
fix: update TimeSeriesDataset default dtype and add tests
9f323c1
refactor: add dtype attribute to Chronos class
f4c0b25
style: fix ruff formatting
43d4799
fix: keep TimeSeriesDataset default dtype as bfloat16
d032e93
test: reorganize tests and add forecast dtype verification
c4664b7
style: fix import ordering
705b33f
fix: add correct name test
AzulGarza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import torch | ||
|
|
||
|
|
||
| def test_chronos_default_dtype_is_float32(): | ||
| """Ensure Chronos defaults to float32 dtype.""" | ||
| from timecopilot.models.foundation.chronos import Chronos | ||
|
|
||
| model = Chronos(repo_id="amazon/chronos-t5-tiny") | ||
| assert model.dtype == torch.float32 | ||
|
|
||
|
|
||
| def test_chronos_model_uses_configured_dtype(mocker): | ||
| """Ensure Chronos loads models with the configured dtype.""" | ||
| mock_pipeline = mocker.patch( | ||
| "timecopilot.models.foundation.chronos.BaseChronosPipeline.from_pretrained" | ||
| ) | ||
| mocker.patch("torch.cuda.is_available", return_value=False) | ||
|
|
||
| from timecopilot.models.foundation.chronos import Chronos | ||
|
|
||
| # Test default (float32) | ||
| model = Chronos(repo_id="amazon/chronos-t5-tiny") | ||
| with model._get_model(): | ||
| pass | ||
| call_kwargs = mock_pipeline.call_args[1] | ||
| assert call_kwargs["torch_dtype"] == torch.float32 | ||
|
|
||
| # Test custom dtype (bfloat16) | ||
| mock_pipeline.reset_mock() | ||
| model_bf16 = Chronos(repo_id="amazon/chronos-t5-tiny", dtype=torch.bfloat16) | ||
| with model_bf16._get_model(): | ||
| pass | ||
| call_kwargs = mock_pipeline.call_args[1] | ||
| assert call_kwargs["torch_dtype"] == torch.bfloat16 | ||
|
|
||
|
|
||
| def test_chronos_forecast_uses_configured_dtype(mocker): | ||
| """Ensure Chronos.forecast uses the configured dtype for dataset creation.""" | ||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| from timecopilot.models.foundation.chronos import Chronos | ||
|
|
||
| # Patch dataset creation to capture dtype argument | ||
| mock_from_df = mocker.patch( | ||
| "timecopilot.models.foundation.chronos.TimeSeriesDataset.from_df" | ||
| ) | ||
|
|
||
| # Avoid real model loading and CUDA branching | ||
| mocker.patch( | ||
| "timecopilot.models.foundation.chronos.BaseChronosPipeline.from_pretrained" | ||
| ) | ||
| mocker.patch("torch.cuda.is_available", return_value=False) | ||
|
|
||
| model_dtype = torch.bfloat16 | ||
| model = Chronos(repo_id="amazon/chronos-t5-tiny", dtype=model_dtype) | ||
|
|
||
| df = pd.DataFrame( | ||
| { | ||
| "unique_id": ["A"] * 10, | ||
| "ds": pd.date_range("2020-01-01", periods=10), | ||
| "y": range(10), | ||
| } | ||
| ) | ||
|
|
||
| def _from_df_side_effect(*args, **kwargs): | ||
| # Assert that Chronos.forecast passes the configured dtype through | ||
| assert kwargs.get("dtype") == model_dtype | ||
| # Short-circuit the rest of the forecast call | ||
| raise RuntimeError("stop after dtype check") | ||
|
|
||
| mock_from_df.side_effect = _from_df_side_effect | ||
|
|
||
| with pytest.raises(RuntimeError, match="stop after dtype check"): | ||
| model.forecast(df=df, h=2) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import torch | ||
|
|
||
| from timecopilot.models.foundation.utils import TimeSeriesDataset | ||
|
|
||
|
|
||
| def test_timeseries_dataset_class_default_dtype_is_bfloat16(): | ||
| """Ensure TimeSeriesDataset defaults to bfloat16 for backward compatibility.""" | ||
| import pandas as pd | ||
|
|
||
| df = pd.DataFrame( | ||
| { | ||
| "unique_id": ["A"] * 10, | ||
| "ds": pd.date_range("2020-01-01", periods=10), | ||
| "y": range(10), | ||
| } | ||
| ) | ||
| dataset = TimeSeriesDataset.from_df(df, batch_size=10) | ||
| assert dataset.data[0].dtype == torch.bfloat16 | ||
|
|
||
|
|
||
| def test_timeseries_dataset_respects_custom_dtype(): | ||
| """Ensure TimeSeriesDataset respects custom dtype parameter.""" | ||
| import pandas as pd | ||
|
|
||
| df = pd.DataFrame( | ||
| { | ||
| "unique_id": ["A"] * 10, | ||
| "ds": pd.date_range("2020-01-01", periods=10), | ||
| "y": range(10), | ||
| } | ||
| ) | ||
| dataset = TimeSeriesDataset.from_df(df, batch_size=10, dtype=torch.float32) | ||
| assert dataset.data[0].dtype == torch.float32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.