From 97d3d19522dcf565c1b863005faf5c8fc1c853a7 Mon Sep 17 00:00:00 2001 From: AmitMY Date: Mon, 27 Jul 2026 12:59:26 +0200 Subject: [PATCH] refactor(char-causal-lm): drop unreachable GenerationConfig construction _get_generation_params built a throwaway GenerationConfig when none was passed, then read it back through getattr with a None default. A fresh GenerationConfig has max_new_tokens = min_new_tokens = None, and getattr(None, 'max_new_tokens', None) is also None, so both paths hit the same `or 50` / `or 0` fallbacks. The object could never change the result. The existing tests already pin this down from both sides: test_none_config asserts _get_generation_params(None) == (50, 0) and test_empty_config asserts _get_generation_params(GenerationConfig()) == (50, 0). Both still pass, unchanged. Not sold as a speed fix, though it does drop an allocation from the default generate() path: 2.74 us -> 0.06 us for that call, which is nothing next to the hundreds of model forwards a generation runs. Co-Authored-By: Claude Opus 5 (1M context) --- utf8_tokenizer/char_causal_lm.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/utf8_tokenizer/char_causal_lm.py b/utf8_tokenizer/char_causal_lm.py index 50909b0..20ac92b 100644 --- a/utf8_tokenizer/char_causal_lm.py +++ b/utf8_tokenizer/char_causal_lm.py @@ -249,9 +249,10 @@ def _truncate_at_eos( @staticmethod def _get_generation_params(generation_config: GenerationConfig | None) -> tuple[int, int]: - """Extract max_new_tokens and min_new_tokens from generation config.""" - if generation_config is None: - generation_config = GenerationConfig() + """Extract max_new_tokens and min_new_tokens from generation config. + + A missing config behaves like a default one: getattr falls back to None either way. + """ max_new_tokens = getattr(generation_config, 'max_new_tokens', None) or 50 min_new_tokens = getattr(generation_config, 'min_new_tokens', None) or 0 return max_new_tokens, min_new_tokens