Habana generation failures leave max_length mutated

Author: sdivyanshu90Created Oct 1, 2026Updated Oct 1, 2026

Summary

The Habana backend temporarily changes its max_length while delegating generation to the Hugging Face implementation. If generation raises, the saved loglikelihood limit is never restored, leaving the model instance in a mutated state for subsequent requests.

Reproduction

Use a Habana model whose loglikelihood bucket limit is 128, then simulate an exception from the delegated generation call after the backend switches to a generation limit of 2048:

python
try:
    model.generate_until(requests)
except RuntimeError:
    pass

print(model.max_length)

Observed behavior

2048

Expected behavior

128

Temporary backend state should be restored whether generation succeeds or raises.

Root cause and impact

The method saves the current limit, assigns the parent generation limit, calls the parent method, and restores the saved value only on the normal return path. The restoration is not protected by finally.

If a caller catches or retries a generation error, later likelihood work on the same instance uses the wrong context limit. This can change bucketing, memory usage, truncation, and subsequent evaluation behavior after a single failed batch.

Proposed resolution

Wrap the delegated generation call in try/finally and restore the saved limit in the finally block. Add an exception-path regression test that verifies the original value after the delegated call fails.

Source: EleutherAI/lm-evaluation-harness