Customizable Logit Warping Strategies for Generation #40010#40403
Customizable Logit Warping Strategies for Generation #40010#40403PamelaBha wants to merge 20 commits intohuggingface:mainfrom
Conversation
|
cc @gante |
gante
left a comment
There was a problem hiding this comment.
Very cool PR! I like where this feature is going 🔥
I've added a few comments, but I think they are simple to solve 🤗 Let me know if you have questions/counter-proposals regarding my comments.
Other global comments:
- Missing: documentation of
logits_processorinGenerationConfig. I would add a link to the example file inside the docs, your examples are quite clear! - Suggestion: in the documentation of
logits_processoringenerate(), I would add a mention about the new format (and a link to the examples) - Some classes should be directly imported from the top level, i.e. we should be able to do
from transformers import LogitProcessorRegistry. This is done by adding the classes to the outermost__init__.py. I think the only class we need to add there isLogitProcessorRegistry(we can already tofrom transformers import LogitsProcessor)
|
|
||
| if is_torch_available(): | ||
| from .logits_process import SynthIDTextWatermarkLogitsProcessor, WatermarkLogitsProcessor | ||
| from ..cache_utils import ( |
There was a problem hiding this comment.
(this diff seems related to a merge conflict, we can probably remove these added lines)
|
|
||
|
|
||
|
|
| # Special handling for logit_processors - serialize to JSON string | ||
| if "logit_processors" in output and output["logit_processors"] is not None: | ||
| if not isinstance(output["logit_processors"], str): | ||
| # Convert to JSON string | ||
| output["logit_processors"] = json.dumps(output["logit_processors"]) |
There was a problem hiding this comment.
Is this needed? if self.logit_processors is a list of dicts, then returning it directly should be fine :)
| config._original_object_hash = hash(config) # Hash to detect whether the instance was modified | ||
| return config | ||
|
|
||
| def get_logit_processors(self): |
There was a problem hiding this comment.
Can we move this function to utils.py?
Goal: let's keep classes as independent as possible -- the generation config doesn't need to know about the expected format that logit_processors takes in generate. It's mostly a data storage class.
|
|
||
| # Enhanced LogitsProcessorList | ||
| class ConfigurableLogitsProcessorList(LogitsProcessorList): | ||
| """Extended LogitsProcessorList that supports configuration-based construction.""" |
There was a problem hiding this comment.
missing: an example in the docstring
|
|
||
| # Add any directly passed processors last | ||
| if logits_processor is not None: | ||
| processors.extend(logits_processor) |
There was a problem hiding this comment.
merging with the custom logits_processor already done in self._merge_criteria_processor_list call a few lines above
| if configured_processors is not None: | ||
| # Check for duplicates and warn | ||
| existing_types = {type(p).__name__ for p in processors} | ||
| config_types = {type(p).__name__ for p in configured_processors} | ||
| duplicates = existing_types & config_types | ||
|
|
||
| if duplicates: | ||
| warnings.warn( | ||
| f"Duplicate LogitProcessors detected: {duplicates}. " | ||
| f"Configured processors will be added after standard ones." | ||
| ) | ||
|
|
||
| processors.extend(configured_processors) |
There was a problem hiding this comment.
Perhaps we can reuse the logic in _merge_criteria_processor_list ?
| self.assertTrue(delay_pattern_processor.active_batches.all()) | ||
| self.assertTrue((delay_pattern_processor.delay_pattern == torch.tensor(delay_pattern) - 1).all()) | ||
|
|
||
| class TestLogitProcessorRegistry(unittest.TestCase): |
There was a problem hiding this comment.
Let's have a single class for everything related to the configurable logits processors 🤗
| from transformers.generation.logits_process import LogitsProcessor, LogitProcessorRegistry | ||
| import torch |
There was a problem hiding this comment.
all the imports in the example should be:
1 - at the top of the file
2 - transformers imports should be top-level imports, i.e. from transformers import LogitsProcessor, LogitProcessorRegistry. See the global PR comment I added about this 🤗
thanks a lot for reviewing. I will address these soon and send a new PR |
…into Issue40010
…into Issue40010
…into Issue40010
|
@gante this test is failing in the latest build but nothing to do with my change I believe: FAILED tests/models/gpt_oss/test_modeling_gpt_oss.py::GptOssModelTest::test_assisted_decoding_matches_greedy_search_1_same - AssertionError: False is not true any pointers for me? |
|
@PamelaBha don't worry about tests that you believe are unrelated to your changes. That may be temporary issues in our codebase, and I can help you with them when we're ready to merge 🤗 A request: the diff in the PR has many style-related changes, likely the outcome of an automated tool (like the one I paste below). Make sure your environment is up-to-date (
Ping me when the PR is ready for a re-review |
b3d158f to
49239b1
Compare
49239b1 to
98289c5
Compare
|
@gante can you help reopen my PR? I was trying to undo all the styling bot changes and somehow accidentally closed the PR. I am hoping you have a reopen permission |
|
haha done 👍 |
779f006 to
7bea595
Compare
|
@gante please take a look at the latest PR |
|
@gante ping on this |
|
Hi @gante - can you please help review and complete the PR? |
|
@PamelaBha I will take a look tomorrow, sorry for a delay. With the major v5 release, we might have missed this PR. In the meanwhile, review would be easier if you can revert style changes which aren't related. I see the configuration file has no changes expect for style and shorter line length It'll help me spot the actual files to review |

What does this PR do?
Feature request
Improve the generate() API by supporting custom, declarative logit warping strategies. Make it easier for users to plug in standard and custom LogitProcessors via configuration or arguments without needing to subclass or dive into internals.
Motivation
The generation module already supports rich logit manipulation through LogitProcessorList, but:
It is undocumented and hard to use for casual users
Requires advanced subclassing to customize behaviors (e.g., word bans, domain constraints)
Doesn’t support JSON- or dict-style configuration like many other parts of Transformers
Making logit warping more accessible enables:
Prompt engineers and power users to fine-tune generation behavior
Safer generation via blacklists or probability shifting
Dynamic controls like repetition penalties or temperature annealing
Fixes # (issue): 40010
Before submitting
Pull Request section?
to it if that's the case. Customizable Logit Warping Strategies for Generation #40010
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.