-
Notifications
You must be signed in to change notification settings - Fork 11
refactor(sunjx): refactor loss-filter implementation #17
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
Open
Jiaxuan-Sun
wants to merge
10
commits into
opendilab:main
Choose a base branch
from
Jiaxuan-Sun:refactor/loss-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0ebcdbf
refactor(sunjx): refactor loss-filter for sample filtering and loss w…
Jiaxuan-Sun 11e81ac
refactor(sunjx): refactor loss-filter implementation
Jiaxuan-Sun 008c90a
refactor(sunjx): Unify the comment style
Jiaxuan-Sun ab61fef
Merge remote-tracking branch 'opendilab/main' into refactor/loss-filter
Jiaxuan-Sun 4d04e1d
refactor(sunjx): fix format/fcheck bugs
Jiaxuan-Sun a43ae21
feature(sunjx): fix dynamic_sampling bugs
Jiaxuan-Sun d0346d0
Merge branch 'main' into refactor/loss-filter
Jiaxuan-Sun 7d8dea4
refactor(sunjx): pass formt and fcheck
Jiaxuan-Sun a659c00
refactor(sunjx): pass format and fcheck check
Jiaxuan-Sun 97f8a92
refactor(sunjx): Organize the code
Jiaxuan-Sun 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
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,169 @@ | ||
| """ | ||
| Filter and Weight Module | ||
|
|
||
| Unified interface for sample filtering and loss weighting in RLHF. | ||
|
|
||
| This module provides a three-layer architecture for managing sample filtering | ||
| and loss weighting: | ||
|
|
||
| 1. **Metrics Layer**: Compute sample-level metrics (entropy, difficulty, staleness, etc.) | ||
| 2. **Filter Layer**: Filter samples based on metrics (keep/discard decisions) | ||
| 3. **Weight Layer**: Compute per-sample loss weights based on metrics | ||
|
|
||
| The FilterWeightManager provides a high-level API to orchestrate these components. | ||
|
|
||
| Example usage: | ||
| ```python | ||
| from lightrft.trainer.filter_weight import ( | ||
| FilterWeightManager, | ||
| ResponseLengthFilter, | ||
| DifficultyWeighting, | ||
| ) | ||
|
|
||
| # Create manager | ||
| manager = FilterWeightManager( | ||
| filters=[ResponseLengthFilter(max_length=1024)], | ||
| weights=[(DifficultyWeighting(mode="prioritized"), 1.0)], | ||
| enable_metrics={"difficulty": True} | ||
| ) | ||
|
|
||
| # Compute metrics | ||
| metrics = manager.compute_metrics(outputs) | ||
|
|
||
| # Apply to experiences | ||
| experiences, weights = manager.apply_to_experiences(experiences, metrics) | ||
| ``` | ||
|
|
||
| Author: LightRLHF Team | ||
|
Jiaxuan-Sun marked this conversation as resolved.
Outdated
|
||
| """ | ||
|
|
||
| # Metrics | ||
| from .metrics import ( | ||
| SampleMetrics, | ||
| MetricsComputer, | ||
| ) | ||
|
|
||
| # Filters | ||
| from .filters import ( | ||
| SampleFilter, | ||
| ResponseLengthFilter, | ||
| RewardValueFilter, | ||
| EntropyFilter, | ||
| DifficultyFilter, | ||
| CompositeFilter, | ||
| PercentileFilter, | ||
| ) | ||
|
|
||
| # Weights | ||
| from .weights import ( | ||
| LossWeighting, | ||
| ResponseLengthWeighting, | ||
| EntropyWeighting, | ||
| DifficultyWeighting, | ||
| StalenessWeighting, | ||
| RewardMagnitudeWeighting, | ||
| CompositeWeighting, | ||
| UniformWeighting, | ||
| ) | ||
|
|
||
| # Manager | ||
| from .manager import ( | ||
| FilterWeightManager, | ||
| FilterWeightManagerBuilder, | ||
| ) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| # ========== Metrics ========== | ||
| "SampleMetrics", | ||
| "MetricsComputer", | ||
|
|
||
| # ========== Filters ========== | ||
| "SampleFilter", | ||
| "ResponseLengthFilter", | ||
| "RewardValueFilter", | ||
| "EntropyFilter", | ||
| "DifficultyFilter", | ||
| "CompositeFilter", | ||
| "PercentileFilter", | ||
|
|
||
| # ========== Weights ========== | ||
| "LossWeighting", | ||
| "ResponseLengthWeighting", | ||
| "EntropyWeighting", | ||
| "DifficultyWeighting", | ||
| "StalenessWeighting", | ||
| "RewardMagnitudeWeighting", | ||
| "CompositeWeighting", | ||
| "UniformWeighting", | ||
|
|
||
| # ========== Manager ========== | ||
| "FilterWeightManager", | ||
| "FilterWeightManagerBuilder", | ||
| ] | ||
|
|
||
|
|
||
| # Version info | ||
| __version__ = "1.0.0" | ||
| __author__ = "LightRLHF Team" | ||
|
Jiaxuan-Sun marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def get_version(): | ||
| """Get the version of the filter_weight module.""" | ||
| return __version__ | ||
|
|
||
|
|
||
| # Quick access functions for common use cases | ||
| def create_length_filter(max_length: int = 1024, **kwargs): | ||
| """ | ||
| Quick function to create a response length filter. | ||
|
|
||
| Args: | ||
|
Jiaxuan-Sun marked this conversation as resolved.
Outdated
|
||
| max_length: Maximum response length | ||
| **kwargs: Additional arguments for ResponseLengthFilter | ||
|
|
||
| Returns: | ||
| ResponseLengthFilter instance | ||
| """ | ||
| return ResponseLengthFilter(max_length=max_length, **kwargs) | ||
|
|
||
|
|
||
| def create_difficulty_weighting(mode: str = "prioritized", alpha: float = 0.6, **kwargs): | ||
| """ | ||
| Quick function to create difficulty weighting. | ||
|
|
||
| Args: | ||
| mode: Weighting mode ("prioritized" or "curriculum") | ||
| alpha: Prioritization exponent | ||
| **kwargs: Additional arguments for DifficultyWeighting | ||
|
|
||
| Returns: | ||
| DifficultyWeighting instance | ||
| """ | ||
| return DifficultyWeighting(mode=mode, alpha=alpha, **kwargs) | ||
|
|
||
|
|
||
| def create_manager_from_args(args, packing_samples: bool = False): | ||
| """ | ||
| Quick function to create FilterWeightManager from training arguments. | ||
|
|
||
| Args: | ||
| args: Training arguments | ||
| packing_samples: Whether samples are packed | ||
|
|
||
| Returns: | ||
| FilterWeightManager instance | ||
| """ | ||
| return FilterWeightManagerBuilder.from_args(args, packing_samples) | ||
|
|
||
|
|
||
| # Add convenience imports at module level | ||
| __all__.extend([ | ||
| "get_version", | ||
| "create_length_filter", | ||
| "create_difficulty_weighting", | ||
| "create_manager_from_args", | ||
| ]) | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
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.