generated from fastai/nbdev_template
    
        
        - 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.3k
feat(trainer): add PAPOTrainer for preference-based optimization #4334
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
      
      
            SolarWindRider
  wants to merge
  20
  commits into
  huggingface:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
SolarWindRider:feat/trainer-papo
  
      
      
   
  
    
  
  
  
 
  
      
    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.
          
          
      
        
          +492
        
        
          −0
        
        
          
        
      
    
  
  
     Open
                    Changes from 7 commits
      Commits
    
    
            Show all changes
          
          
            20 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      727a783
              
                feat(trainer): add PAPOTrainer for preference-based optimization
              
              
                 c2c4c56
              
                Merge branch 'main' into feat/trainer-papo
              
              
                SolarWindRider c32a23c
              
                Merge branch 'main' into feat/trainer-papo
              
              
                SolarWindRider e4af368
              
                Merge branch 'main' into feat/trainer-papo
              
              
                SolarWindRider 1b1fb4c
              
                	new file:   trl/experimental/papo/__init__.py
              
              
                 a3751e2
              
                move papo to exp and add paper index
              
              
                 4f2b250
              
                clean trainer __init__.py
              
              
                 75b231d
              
                precommit
              
              
                qgallouedec 59e7222
              
                Merge branch 'huggingface:main' into feat/trainer-papo
              
              
                SolarWindRider 04b986b
              
                paper index info And a class method for the citation
              
              
                 25c3595
              
                Merge branch 'feat/trainer-papo' of https://github.com/SolarWindRider…
              
              
                 6d25b0a
              
                Merge branch 'main' into feat/trainer-papo
              
              
                SolarWindRider 6422795
              
                fix conversational inputs bugs
              
              
                SolarWindRider 4ccfd28
              
                Merge branch 'main' into feat/trainer-papo
              
              
                SolarWindRider bfaee35
              
                Merge branch 'main' into feat/trainer-papo
              
              
                SolarWindRider f85a53d
              
                Merge branch 'main' into feat/trainer-papo
              
              
                SolarWindRider 57fbcd8
              
                perception clip
              
              
                SolarWindRider 63670e6
              
                Merge branch 'main' into feat/trainer-papo
              
              
                qgallouedec c8511d0
              
                Style and add section to doc
              
              
                qgallouedec b0ed5c0
              
                fix link
              
              
                qgallouedec 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,17 @@ | ||
| # Copyright 2020-2025 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|  | ||
|  | ||
| from .papo_config import PAPOConfig | ||
| from .papo_trainer import PAPOTrainer | 
  
    
      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,78 @@ | ||
| # Copyright 2020-2025 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|  | ||
| from dataclasses import dataclass | ||
| from typing import Literal | ||
|  | ||
| from trl.trainer.grpo_config import GRPOConfig | ||
|  | ||
|  | ||
| @dataclass | ||
| class PAPOConfig(GRPOConfig): | ||
| """ | ||
| Configuration class for PAPOTrainer. | ||
|  | ||
| PAPO (Perception-Aware Policy Optimization) extends GRPO/DAPO for multimodal reasoning | ||
| by adding an implicit perception loss and double entropy regularization. | ||
|  | ||
| Args: | ||
| perception_loss_weight (`float`, *optional*, defaults to `0.1`): | ||
| gamma | ||
| Weight coefficient for the perception loss term. This encourages the model to | ||
| be sensitive to visual changes. | ||
|  | ||
| mask_ratio (`float`, *optional*, defaults to `0.3`): | ||
| Ratio of the image to mask when computing perception loss. | ||
|  | ||
| mask_type (`Literal["random", "patch", "grid"]`, *optional*, defaults to `"random"`): | ||
| Type of masking strategy to use. | ||
|  | ||
| der_loss_weight1 (`float`, *optional*, defaults to `0.03`): | ||
| eta1 | ||
| Weight coefficient for the Double Entropy Regularization (DER) term. This term | ||
| encourages confident predictions with original images (low entropy) and | ||
| uncertain predictions with masked images (high entropy). | ||
|  | ||
| der_loss_weight2 (`float`, *optional*, defaults to `0.03`): | ||
| eta2 | ||
| Weight coefficient for the Double Entropy Regularization (DER) term. This term | ||
| encourages confident predictions with original images (low entropy) and | ||
| uncertain predictions with masked images (high entropy). | ||
|  | ||
| loss_type (`Literal["grpo", "dapo"]`, inherited from GRPOConfig): | ||
| Base loss type to use. Set to "grpo" for PAPO-G or "dapo" for PAPO-D. | ||
| """ | ||
|  | ||
| perception_loss_weight: float = 0.1 | ||
| mask_ratio: float = 0.3 | ||
| mask_type: Literal["random", "patch", "grid"] = "random" | ||
|  | ||
| # Added for Double Entropy Regularization | ||
| der_loss_weight1: float = 0.03 | ||
| der_loss_weight2: float = 0.03 | ||
|  | ||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|  | ||
| # Validation | ||
| if not 0.0 <= self.mask_ratio <= 1.0: | ||
| raise ValueError(f"mask_ratio must be between 0 and 1, got {self.mask_ratio}") | ||
|  | ||
| if self.der_loss_weight1 < 0 or self.der_loss_weight2 < 0: | ||
| raise ValueError( | ||
| f"der_loss_weight1 and der_loss_weight2 must be non-negative, got {self.der_loss_weight1} and {self.der_loss_weight2}" | ||
| ) | ||
|  | ||
| if self.mask_type not in ["random", "patch", "grid"]: | ||
| raise ValueError(f"mask_type must be one of ['random', 'patch', 'grid'], got {self.mask_type}") | 
      
      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.