Skip to content

fix: resolve TypeError when max_tokens=None in generate() and CLI#296

Open
voidborne-d wants to merge 1 commit intonari-labs:mainfrom
voidborne-d:fix/max-tokens-none-typeerror
Open

fix: resolve TypeError when max_tokens=None in generate() and CLI#296
voidborne-d wants to merge 1 commit intonari-labs:mainfrom
voidborne-d:fix/max-tokens-none-typeerror

Conversation

@voidborne-d
Copy link
Copy Markdown

Summary

Fixes #281.

When cli.py omits --max-tokens (default None), it passes max_tokens=None to Dia.generate(), which overrides the hardcoded default of 3072. The generation loop then compares dec_step < None, raising:

TypeError: '<' not supported between instances of 'int' and 'NoneType'

Root Cause

generate() declared max_tokens: int = 3072, but callers (including cli.py) pass explicit None which overrides the default. Two comparisons depend on max_tokens being an integer:

while dec_step < max_tokens:       # line 696
is_max_len = current_step_idx >= max_tokens - max_delay_pattern  # line 721

Note that _prepare_generation() already handles None correctly via max_generation_length or config.decoder_config.max_position_embeddings, so only the generate() function itself crashes.

Changes

dia/model.py

  • Change generate() signature: max_tokens: int = 3072max_tokens: int | None = None
  • Add resolution at function entry: if max_tokens is None: max_tokens = self.config.decoder_config.max_position_embeddings
  • This is consistent with how _prepare_generation already handles None
  • Existing callers passing explicit int values are unaffected

cli.py

  • Only include max_tokens in generate() kwargs when user explicitly sets --max-tokens, avoiding the None override entirely
  • Import and use DEFAULT_SAMPLE_RATE from dia.model instead of hardcoded 44100 to keep the sample rate in sync with the model constant

tests/test_generate_max_tokens.py

  • 15 regression tests organized into 6 test classes:
    • TestGenerateSignature (2): signature accepts None, default is None
    • TestNoneResolution (4): guard exists, resolves to config value, happens before loop, preserves explicit int
    • TestCLI (4): default is None, guards against passing None, imports DEFAULT_SAMPLE_RATE, no hardcoded 44100
    • TestPrepareGeneration (1): accepts None default
    • TestConstants (2): DEFAULT_SAMPLE_RATE defined, save_audio() uses it
    • TestIssue281Reproduction (2): confirms int < None raises TypeError, verifies no unguarded comparisons
  • Tests parse source code directly (no torch/torchaudio required) so they run in lightweight CI environments

Testing

$ python3 -m pytest tests/test_generate_max_tokens.py -v
15 passed in 0.08s

Fixes nari-labs#281.

Root cause: when CLI omits --max-tokens (default None), it passes
max_tokens=None to Dia.generate(), which overrides the hardcoded
default of 3072. The generation loop then compares dec_step < None,
raising TypeError: '<' not supported between instances of 'int'
and 'NoneType'.

Changes:
- dia/model.py: change generate() signature from max_tokens: int = 3072
  to max_tokens: int | None = None; resolve None to
  config.decoder_config.max_position_embeddings at function entry,
  before any comparison.  This is consistent with how
  _prepare_generation already handles None via its own or-fallback.
- cli.py: only pass max_tokens when user explicitly sets --max-tokens;
  import and use DEFAULT_SAMPLE_RATE from dia.model instead of
  hardcoded 44100 to keep sample rate in sync with the model constant.
- tests/test_generate_max_tokens.py: 15 regression tests covering
  signature, None resolution order, CLI integration, constant
  consistency, and exact reproduction of the issue nari-labs#281 error path.
  Tests parse source code directly to avoid requiring torch/torchaudio
  in the test environment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error during audio generation or saving: '<' not supported between instances of 'int' and 'NoneType'

1 participant