Skip to content

Conversation

Copy link

Copilot AI commented Sep 17, 2025

This PR modernizes the MVTS Transformer codebase to work with the latest PyTorch and Python versions while maintaining full backward compatibility. The changes address multiple critical issues reported by users trying to run the code with modern package versions.

Key Issues Fixed

PyTorch 2.8+ Compatibility Error

Fixed: TypeError: forward() got an unexpected keyword argument 'is_causal'

The custom TransformerBatchNormEncoderLayer was incompatible with PyTorch 2.8+ because newer versions pass an is_causal parameter to transformer layers that wasn't supported:

# Before (fails with PyTorch 2.8+)
def forward(self, src: Tensor, src_mask: Optional[Tensor] = None,
            src_key_padding_mask: Optional[Tensor] = None) -> Tensor:

# After (compatible with all PyTorch versions)  
def forward(self, src: Tensor, src_mask: Optional[Tensor] = None,
            src_key_padding_mask: Optional[Tensor] = None, is_causal: bool = False) -> Tensor:

Tensor Gradient Conversion Issues

Fixed: RuntimeError: Can't call numpy() on Tensor that requires grad

During evaluation, tensors with gradients were being converted to NumPy arrays without detaching:

# Before (fails in test mode)
per_batch['predictions'].append(predictions.cpu().numpy())

# After (works correctly)
per_batch['predictions'].append(predictions.cpu().detach().numpy())

Deprecated Optimizer API Calls

Fixed: Multiple deprecated function signatures in custom optimizers

Updated all deprecated PyTorch tensor operations to use modern API:

# Before (deprecated in PyTorch 2.0+)
exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)
exp_avg.mul_(beta1).add_(1 - beta1, grad)
p_data_fp32.addcdiv_(-step_size, exp_avg, denom)

# After (modern PyTorch API)
exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)  
p_data_fp32.addcdiv_(exp_avg, denom, value=-step_size)

sktime Data Loading Compatibility

Fixed: Import errors with modern sktime versions

The load_from_tsfile_to_dataframe function moved in newer sktime versions:

# Before (sktime < 0.10.0)
from sktime.utils import load_data
df, labels = load_data.load_from_tsfile_to_dataframe(filepath, ...)

# After (sktime >= 0.10.0)
from sktime.datasets import load_from_tsfile_to_dataframe
df, labels = load_from_tsfile_to_dataframe(filepath, ...)

New Features

AdamW Optimizer Support

Added support for the widely-used AdamW optimizer with proper weight decay:

python src/main.py --optimizer AdamW --lr 0.001 [other args...]

Modernized Dependencies

Updated requirements.txt to use flexible version ranges instead of exact pins, improving compatibility:

# Before
sktime==0.4.1  # Often fails to install

# After  
sktime>=0.10.0  # Compatible with modern Python versions

Code Quality Improvements

  • Replaced bare except: clauses with except Exception: for better error handling
  • Updated string formatting from .format() to f-strings where appropriate
  • Improved error messages and debugging output

Testing & Validation

Created comprehensive test suite covering:

  • PyTorch compatibility across versions
  • Tensor gradient handling in evaluation modes
  • All optimizer implementations (Adam, AdamW, RAdam)
  • Complete transformer pipeline functionality
  • Options parsing with new optimizers
  • sktime data loading pipeline with modern versions

All tests pass with PyTorch 2.8+, sktime 0.38+, and Python 3.12 while maintaining backward compatibility.

Migration Impact

Zero breaking changes - All existing code continues to work without modification. Users can immediately benefit from:

  • Compatibility with latest PyTorch versions (2.8+)
  • Compatibility with modern sktime versions (0.38+)
  • Ability to use modern Python versions (3.12+)
  • Access to AdamW optimizer
  • Improved error handling and debugging

This modernization resolves issues reported in #68, #59, #48, #75, #24, and #67, ensuring the codebase remains usable with current and future package versions.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Please modernise the codebase based on the newest developments in the used packages, as well as complaints/suggestions/bugs/found issues flagged in the Github Issues Modernize codebase for PyTorch 2.8+ compatibility and fix deprecated API calls Sep 17, 2025
Copilot AI requested a review from gzerveas September 17, 2025 16:47
numpy>=1.19.2
scikit-learn>=0.24.0
protobuf>=3.17.3
sktime>=0.10.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sktime>=0.10.0

The format in the datasets of sktime may have changed - has the code that loads and processes this data have been adapted accordingly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've verified and updated the code for modern sktime compatibility. The load_from_tsfile_to_dataframe function moved from sktime.utils.load_data to sktime.datasets in newer versions. Updated the import in src/datasets/data.py and confirmed the function signature and data processing pipeline remain fully compatible. Commit f7f4b0c addresses this.

Copy link
Owner

@gzerveas gzerveas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify that the new version of sktime is compatible with the data processing pipeline

@gzerveas
Copy link
Owner

@copilot please consider the review comment

Copy link
Author

Copilot AI commented Sep 18, 2025

@copilot please consider the review comment

Addressed the sktime compatibility concern. Updated import location from sktime.utils.load_data to sktime.datasets.load_from_tsfile_to_dataframe and verified complete data processing pipeline compatibility with comprehensive testing. No breaking changes to functionality. Commit f7f4b0c.

Copy link
Author

Copilot AI commented Sep 18, 2025

Verify that the new version of sktime is compatible with the data processing pipeline

Verified sktime data processing pipeline compatibility. The function moved from sktime.utils.load_data to sktime.datasets but maintains identical signature and behavior. All .ts file loading for classification, regression, and imputation tasks work correctly with modern sktime versions. See commit f7f4b0c.

Copilot AI changed the title Modernize codebase for PyTorch 2.8+ compatibility and fix deprecated API calls Modernize codebase for PyTorch 2.8+ compatibility, fix deprecated API calls, and update sktime integration Sep 18, 2025
Copilot AI requested a review from gzerveas September 18, 2025 00:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants