fix: preference filter applies both length limits#1761
Conversation
Nested ternary skipped max_token_length whenever max_prompt_token_length was set. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Code Review
This pull request updates the preference dataset filter in open_instruct/dataset_processor.py to apply both max_prompt_token_length and max_token_length constraints simultaneously, rather than treating them as mutually exclusive. A review comment suggests optimizing this filtering function by returning early (short-circuiting) as soon as any length check fails, which avoids unnecessary evaluations on large datasets.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| max_prompt_token_length_ok = True | ||
| if self.config.max_prompt_token_length is not None: | ||
| max_prompt_token_length_ok = len(row[INPUT_IDS_PROMPT_KEY]) <= self.config.max_prompt_token_length | ||
|
|
||
| max_token_length_ok = True | ||
| if self.config.max_token_length is not None: | ||
| max_token_length_ok = ( | ||
| len(row[INPUT_IDS_CHOSEN_KEY]) <= self.config.max_token_length | ||
| and len(row[INPUT_IDS_REJECTED_KEY]) <= self.config.max_token_length | ||
| if self.config.max_token_length is not None | ||
| else True | ||
| ) | ||
| ) | ||
|
|
||
| return max_prompt_token_length_ok and max_token_length_ok |
There was a problem hiding this comment.
We can optimize this filtering function by returning early (short-circuiting) as soon as any length check fails. This avoids unnecessary list length evaluations and boolean operations on subsequent checks, which can improve performance when processing large datasets.
| max_prompt_token_length_ok = True | |
| if self.config.max_prompt_token_length is not None: | |
| max_prompt_token_length_ok = len(row[INPUT_IDS_PROMPT_KEY]) <= self.config.max_prompt_token_length | |
| max_token_length_ok = True | |
| if self.config.max_token_length is not None: | |
| max_token_length_ok = ( | |
| len(row[INPUT_IDS_CHOSEN_KEY]) <= self.config.max_token_length | |
| and len(row[INPUT_IDS_REJECTED_KEY]) <= self.config.max_token_length | |
| if self.config.max_token_length is not None | |
| else True | |
| ) | |
| ) | |
| return max_prompt_token_length_ok and max_token_length_ok | |
| if self.config.max_prompt_token_length is not None: | |
| if len(row[INPUT_IDS_PROMPT_KEY]) > self.config.max_prompt_token_length: | |
| return False | |
| if self.config.max_token_length is not None: | |
| if ( | |
| len(row[INPUT_IDS_CHOSEN_KEY]) > self.config.max_token_length | |
| or len(row[INPUT_IDS_REJECTED_KEY]) > self.config.max_token_length | |
| ): | |
| return False | |
| return True |
Summary
filterused a nested ternary: ifmax_prompt_token_lengthwas set,max_token_lengthwas never checked.GPU_TESTS=bypass
Test plan
Made with Cursor