Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/transformers/generation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,20 @@ def _prepare_generation_config(
generation_config.update(**self.generation_config.to_dict(), defaults_only=True)
generation_config.update(**global_defaults, defaults_only=True)

# Due to some values being boolean and not `None`, we need additional logic to overwrite
# them explicitly (`defaults_only=False`) on the condition that it's only a previous
# default value
default_generation_config = GenerationConfig()
generation_config.update(
**{
k: v
for k, v in self.generation_config.to_dict().items()
if isinstance(v, bool)
and hasattr(default_generation_config, k)
and getattr(generation_config, k, None) == getattr(default_generation_config, k)
}
)
Comment on lines +1802 to +1810

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for late review. I think we're back to the original issue where users pass do_sample=False in their custom config and the model has do_sample=True saved on the hub. And since False is the default value, we won't update it here and keep True

In that case we'd need to set the booleans to None by default as well, I dont' see a cleaner way than defining None as non-set


# Finally, if there are any kwargs, update config with it -> highest priority at the end
model_kwargs = generation_config.update(**kwargs)

Expand Down
19 changes: 19 additions & 0 deletions tests/generation/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4802,6 +4802,25 @@ def test_hub_gen_strategies(self, custom_generate, extra_kwargs):
output = model.generate(**generation_kwargs, **model_inputs)
self.assertEqual(output.sequences.shape, (1, 9))

def test_model_generation_config_can_override_defaults(self):
"""Sanity check that the model samples, not ignoring the model's generation config"""
torch.manual_seed(42) # make it deterministic

model = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-LlamaForCausalLM").eval()
model = model.to(torch_device)

tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-LlamaForCausalLM")
model_inputs = tokenizer(["Write a poem about the market crashing in summer"], return_tensors="pt")
model_inputs = model_inputs.to(model.device)

# Overwrite default value or sampling
model.generation_config.do_sample = True
output = model.generate(**model_inputs, max_new_tokens=32)

EXPECTED_TEXT = 'Write a poem about the market crashing in summersong contradictionPr aucitated realiz Comicsflutterąc inventминцій Glad:` Raymond moreover KulturMillteger мартаTEXT CFщая Русбе Świ Sendlink heuresListener Luigiaceae' # fmt: skip
output = tokenizer.decode(output[0], skip_special_tokens=True)
self.assertTrue(output == EXPECTED_TEXT)


@require_torch
class TokenHealingTestCase(unittest.TestCase):
Expand Down