mirror of
https://github.com/p-e-w/heretic.git
synced 2026-12-03 18:42:57 +07:00
[GH-ISSUE #70] Bug +fix: padding_side not set for tokenizers with existing pad_token, causing empty responses #34
Labels
No labels
bug
enhancement
enhancement
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
p-e-w/heretic#34
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @accemlcc on GitHub (Dec 4, 2025).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/70
Bug: padding_side not set for tokenizers with existing pad_token, causing empty responses
Problem
When a tokenizer already has a
pad_tokendefined (e.g.,allenai/Olmo-3-7B-Instruct),padding_sideis not set to"left", causing empty responses during batched generation.Decoder-only models require left-padding for generation. With right-padding (the default), models see
[PROMPT] [PAD] [PAD]and stop generating immediately, returning empty strings.Impact
is_refusal()(no markers found → success)batch_size > 1(no padding needed for single prompts)batch_size=1shows correct results, larger batches show near-zero refusalsReproduction
Test with any model that has its own
pad_token(doesn't use EOS fallback):Tested with
allenai/Olmo-3-7B-Instructandsqu11z1/Hypnos-i1-8B.Current Code (model.py:50-52)
Suggested Fix
Optional: Handle empty responses in evaluation
Additionally,
is_refusal()inevaluator.pycould treat empty/very short responses as failures rather than successes:This provides defense-in-depth against other potential generation failures.
@red40maxxer commented on GitHub (Dec 5, 2025):
Yep, I noticed this last night as well. #53 will allow much more flexible refusal detection logic but I will add that check to the default implementation too
@accemlcc commented on GitHub (Dec 5, 2025):
@red40maxxer Thanks! Looking forward to the plugin system (#53), that will definitely make the detection logic much more robust (e.g. penalizing empty responses).
However, regarding the tokenizer fix itself:
Since the incorrect padding side actually causes the empty generation in the first place (on decoder-only models), we should probably apply this fix globally and immediately, independent of how we evaluate the result later.
If you are touching
model.pyin your PR anyway, feel free to include thepadding_side="left"line there! Otherwise, a quick hotfix commit to master might be safer to unblock users right now.@p-e-w commented on GitHub (Dec 5, 2025):
No, something else is going on here. Almost all tokenizers define a
pad_token(random example). In fact, I only introduced this code in0bae27f359after noticing that Falcon-E-3B doesn't provide a pad token, which is very uncommon. If you just remove thatifclause entirely, things will still work fine with almost all models.Right-padding is not the default. The default depends on the
padding_sideclass attribute (see Transformers docs). I just spent 10 minutes in the bowels of Transformers, but I couldn't find where exactlyGPT2Tokenizer(the class used by allenai/Olmo-3-7B-Instruct) sets itspadding_side.Your suggested fix is probably good, but we should make sure that we fully understand what is going on here. Empty responses should probably raise an exception, because they almost always indicate that something fundamental is going wrong. Just flagging the response as a refusal is not sufficient.
@accemlcc commented on GitHub (Dec 5, 2025):
hey @p-e-w :-)
Here are the references from the HuggingFace documentation and source code to back this up:
1. Regarding the Default Value:
The
padding_sidedefaults to'right'in the base classPreTrainedTokenizerBase. SinceGPT2Tokenizer(used by OLMo) does not explicitly override this in its__init__, it inherits'right'.Source Code Link (See the
__init__arguments)2. Regarding Generation Failure:
HuggingFace explicitly warns against right-padding for decoder-only generation in their LLM tutorial:
Since Heretic uses batched generation (
batch_size > 1), right-padding puts[PAD]tokens after the prompt. The model attends to these pads as the "most recent context" and usually outputs EOS or empty strings immediately.Forcing
self.tokenizer.padding_side = 'left'ensures safety across all decoder models, regardless of whether they come with a pad token or not.3. Why it sometimes appears to work (The confusion):
You might have observed cases where generation works despite
padding_side='right'. This happens when:In these cases, no
[PAD]tokens are appended, so the model doesn't see the "end of sequence" signal prematurely. The failure strictly triggers only when actual padding tokens are inserted to align lengths in a batch.(4. Deterministic Failure:
It is important to remember that LLMs are deterministic functions regarding their stop conditions. If a model consistently outputs an empty string, it is not "refusing" in a semantic sense (deciding not to answer). It is encountering a structural stop-signal (like right-padding tokens interpreted as end-of-sequence) immediately.
Therefore, an empty response is always an indicator of a broken inference configuration, not a model behavior trait.)
@p-e-w commented on GitHub (Dec 6, 2025):
Thanks for the explanation. I know how padding works and why inputs should be right-aligned in the batch frame, but I didn't see why right-padding would be used by
GPT2Tokenizeruntil you pointed it out. The Transformers docs are quite misleading there, stating (emphasis added):What they mean is that the default is picked from the class attribute of the base class (unless overridden), so in practice, the default is "right" as you described.
I have implemented your suggested fixes, naming you as co-author. I changed my mind about raising an exception for empty responses, because such responses might legitimately come from a model damaged by abliteration, and this could throw off the optimization process if one of the parameter configurations happens to generate such a model.