Repository navigation
fix(model): preserve Transformer batch dimension for singleton inputs - #2385
Open
bushishasshasha wants to merge 1 commit into
Open
bushishasshasha wants to merge 1 commit into
bushishasshasha wants to merge 1 commit into
Conversation
Author
|
@microsoft-github-policy-service agree |
|
The The same pattern is still on
So the "last batch has one sample" failure should reproduce for those models too. Would it make sense to widen this PR to them (same one-line change, and the parametrized |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Both Transformer implementations use
output.squeeze()on a regression output of shape(batch_size, 1). When a prediction batch contains one sample, this returns a scalar, andTransformerModel.predict()fails when concatenating the resulting NumPy arrays.Use
squeeze(-1)in the DatasetH and TSDatasetH implementations to preserve the batch dimension. Multi-sample output shapes and model parameters are unchanged.Motivation and Context
Prediction should retain every input sample, including a singleton dataset and a final batch with one remaining sample. This was reproduced on current
mainwithout market data. There is no associated issue.How Has This Been Tested?
pytest tests/test_all_pipeline.pyfrom the repository root): not run. It requires external market data and broader pipeline dependencies; the official dataset is currently marked unavailable in the README. The offline tests below exercise the real prediction wrappers with Qlib datasets as a focused substitute.python -m pytest tests/model/test_transformer_batch.py -q— 12 passed. Before the fix, the same tests produced 6 failed and 6 passed.Coverage includes both Transformer implementations, batch sizes 1/2/5, finite backward gradients, DatasetH/TSDatasetH prediction for 1/3/4 samples with batch size 2, exact output-index preservation, and agreement across inference batch partitions.
Formatting and lint:
All three checks passed locally. Tests ran on Windows with Python 3.12.6, PyTorch 2.14.1+cpu, NumPy 2.5.4, and pandas 3.1.0. Python modules were imported from this source checkout; the unchanged Cython extensions were supplied by the official pyqlib 0.9.7 Windows wheel. Full training, backtesting, and the upstream CI matrix have not been run. Existing PyTorch/pandas warnings are recorded in the test logs.
Screenshots of Test Results (if appropriate)
12 passed, 19 warnings.Types of changes
AI assistance was used to prepare the patch and regression tests.