Fix _print_stream crash when the stream is empty - #794
Open
rootkiller6788 wants to merge 1 commit into
Open
Conversation
The streaming loop referenced the loop variable `state` after the loop, so an empty iterator (e.g. max_new_tokens=0 or a cache that is already full) raised UnboundLocalError instead of a useful error. Track the last state and raise a clear ValueError when nothing was generated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ChatSampler 开 print_stream 时,如果模型一个 token 都没生成(max_new_tokens=0,或者 cache_length 太小一上来就满了),
_print_stream会在循环结束后去用没绑定的循环变量state,直接抛 UnboundLocalError,用户根本看不出是哪的问题。改成在循环里记下最后一个 state,流是空的话抛一条明确的 ValueError,提示大概率是 max_new_tokens=0 或 cache 满了(建议加大 cache_length)。非空流的行为完全不变。
顺带把之前压着这个问题的
# pylint: disable=undefined-variable,undefined-loop-variable去掉了,因为现在这个 bug 真正被处理了。