Skip to content

Conversation

@zephyr-sh
Copy link
Contributor

✨ Summary

This PR introduces a polynomial LR scheduler with warmup and enhances the warmup scheduler infrastructure with better modularity, validation, and test coverage.

✅ Major Changes

1. Refactored PolynomialLRWarmup

  • Migrated to inherit from torch.optim.lr_scheduler.LRScheduler (modern PyTorch API).

  • Rewritten with:

    • Explicit closed-form logic via get_closed_form()
    • Strict input validation for warmup_iters, total_iters, and power
    • Support for both legacy API (step(epoch)) and modern chainable API
  • Behavior:

    • Linearly increases LR from 0 → base_lr over warmup_iters
    • Then polynomial decay to 0 across remaining total_iters - warmup_iters
    • Fixed to clamp after total_iters

2. Introduced WrappedLRScheduler

  • A general-purpose warmup wrapper:

    • Handles linear warmup with configurable multiplier
    • Delegates to any other scheduler (after_scheduler) after warmup
  • Works with any LRScheduler (e.g. ReduceLROnPlateau, MultiStepLR, etc.)

  • Compatible with step() API (including metric-based schedulers)

  • Includes strong argument checks and lifecycle safety (e.g. base LR re-init once)

3. Added MultiStepLRWarmUp factory function

  • Combines:

    • Linear warmup via WrappedLRScheduler
    • Followed by decay via MultiStepLR
  • Clean and reusable for config-based instantiation

4. Registry Integration

  • Registered all schedulers (PolynomialLRWarmup, WrappedLRScheduler, MultiStepLRWarmUp) to OPTIMIZERS registry
  • Updated __init__.py to reflect the new module inclusion

🧪 Tests

tests/base/optim/test_polynomial_lr_warmup.py

  • Validates:

    • Input validation
    • Correct LR output in warmup and decay phases
    • Behavior after total_iters
    • Compatibility with .step() chainable API

tests/base/optim/test_warm_up.py

  • Covers:

    • WrappedLRScheduler warmup-only behavior
    • Warmup w/ multiplier > 1
    • Correct delegation to after_scheduler (e.g. MultiStepLR)
    • Correct final LR holding behavior
    • Factory behavior and edge cases for MultiStepLRWarmUp

All tests pass.


🧩 Motivation

  • PyTorch is progressively deprecating _LRScheduler in favor of LRScheduler.
  • Polynomial warmup+decay is a common pattern not yet fully supported natively.
  • Reusable warmup wrappers simplify composition and config loading.
  • Provides a reliable warmup scheduling foundation for future models.

🔄 API Summary

# Polynomial warmup + decay
PolynomialLRWarmup(optimizer, warmup_iters=5, total_iters=30, power=1.0)

# General warmup wrapper
WrappedLRScheduler(optimizer, milestone=5, multiplier=1.0, after_scheduler=...)

# Factory: warmup + MultiStep
MultiStepLRWarmUp(optimizer, milestones=[10, 20], warmup_milestone=3, gamma=0.1)

📎 Miscellaneous

  • Ensures compatibility with both old and new scheduler API
  • Fully documented with docstrings and param comments

✅ Checklist

  • Feature implemented with modular, typed design
  • Integrated with registry
  • Unit tests added
  • Passes all CI checks
  • Compatible with both legacy and chainable .step() API

@zephyr-sh zephyr-sh requested a review from kunkunlin1221 July 8, 2025 02:16
@zephyr-sh zephyr-sh self-assigned this Jul 8, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jul 8, 2025

Coverage

Coverage Report
FileStmtsMissCoverMissing
chameleon
   __init__.py60100% 
chameleon/base
   __init__.py70100% 
   power_module.py4022 95%
   utils.py4644 91%
chameleon/base/blocks
   __init__.py10100% 
   conv_block.py4622 96%
   mamba_block.py222 0%
   vit_block.py222 0%
chameleon/base/components
   __init__.py50100% 
   activation.py3644 89%
   dropout.py50100% 
   loss.py823434 59%
   norm.py190100% 
   pooling.py200100% 
chameleon/base/layers
   __init__.py50100% 
   aspp.py210100% 
   grl.py220100% 
   selayer.py180100% 
   vae.py230100% 
   weighted_sum.py230100% 
chameleon/base/ops
   __init__.py10100% 
   positional_encoding.py140100% 
chameleon/base/optim
   __init__.py90100% 
   polynomial_lr_warmup.py270100% 
   warm_up.py3911 97%
chameleon/metrics
   __init__.py10100% 
   normalized_levenshtein_similarity.py512929 43%
chameleon/modules
   __init__.py20100% 
chameleon/modules/backbones
   __init__.py30100% 
   gpunet.py5422 96%
   timm.py70100% 
chameleon/modules/necks
   __init__.py20100% 
   bifpn.py8722 98%
   fpn.py5822 97%
   pafpn.py00100% 
chameleon/registry
   __init__.py20100% 
   registry.py921212 87%
   root.py2655 81%
chameleon/tools
   __init__.py10100% 
chameleon/tools/calflops
   __init__.py20100% 
   calculate_pipline.py1825858 68%
   flops_counter.py521616 69%
   pytorch_ops.py336153153 54%
   utils.py1127676 32%
TOTAL158940674% 

Tests Skipped Failures Errors Time
104 0 💤 0 ❌ 0 🔥 5.342s ⏱️

Copy link
Collaborator

@kunkunlin1221 kunkunlin1221 left a comment

Choose a reason for hiding this comment

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

LGTM

@zephyr-sh zephyr-sh merged commit ccd6266 into main Jul 8, 2025
1 check passed
@zephyr-sh zephyr-sh deleted the bugfix/fix_warmup_scheduler_setting_error branch July 8, 2025 04:02
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.

3 participants