Skip to content
Merged
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
6 changes: 5 additions & 1 deletion swift/llm/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,11 @@ def _swift_encode(self, inputs: StdTemplateInputs):
if isinstance(stop_word, str))
# self.is_training needed because we may want to continue generation from
# the current response
if (self.is_training or self.task_type != 'causal_lm') and not sep_token and not endswith_stop_words:
add_eos = inputs.extra_kwargs.get('add_eos')
if add_eos is None:
add_eos = (self.is_training
or self.task_type != 'causal_lm') and not sep_token and not endswith_stop_words
if add_eos:
extra_context_list = template_meta.suffix
extra_context_type = ContextType.SUFFIX
Comment on lines +1143 to 1148
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve readability and limit variable scope, it's better to declare and use the add_eos logic entirely within this block, as it's the only place it's used. Using a local variable like add_eos_flag makes it clear that its scope is confined here.

With this change, you can remove the declaration add_eos = inputs.extra_kwargs.get('add_eos') from line 1102.

Suggested change
if add_eos is None:
add_eos = (self.is_training
or self.task_type != 'causal_lm') and not sep_token and not endswith_stop_words
if add_eos:
extra_context_list = template_meta.suffix
extra_context_type = ContextType.SUFFIX
add_eos_flag = inputs.extra_kwargs.get('add_eos')
if add_eos_flag is None:
add_eos_flag = (self.is_training
or self.task_type != 'causal_lm') and not sep_token and not endswith_stop_words
if add_eos_flag:
extra_context_list = template_meta.suffix
extra_context_type = ContextType.SUFFIX

elif template_meta.response_prefix:
Expand Down
Loading