Skip to content

fix bugs #3962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 23, 2025
Merged
Changes from 1 commit
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
Next Next commit
fix multimodal max_length
  • Loading branch information
Jintao-Huang committed Apr 23, 2025
commit c224e0ba350c7d2370cdade1773b7c91d6ef22b0
11 changes: 6 additions & 5 deletions swift/llm/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ def encode(self,
encoded.pop(key)
if return_template_inputs:
encoded['template_inputs'] = inputs

if self.max_length is not None and self.truncation_strategy == 'raise':
length = len(encoded.get('input_ids') or encoded.get('labels') or [])
if length > self.max_length:
raise MaxLengthError(f'Current length of row({length}) is larger'
f' than the max_length({self.max_length}).')
if self.use_megatron:
encoded['labels'] = encoded['labels'][1:] + [-100]
encoded['position_ids'] = list(range(len(encoded['labels'])))
Expand Down Expand Up @@ -1023,10 +1027,7 @@ def _encode(self, inputs: StdTemplateInputs) -> Dict[str, Any]:
encoded['tokenizer_kwargs'] = tokenizer_kwargs

if self.max_length is not None:
if self.truncation_strategy == 'raise' and len(input_ids) > self.max_length:
raise MaxLengthError(f'Current length of row({len(input_ids)}) is larger'
f' than the max_length({self.max_length}).')
elif self.truncation_strategy == 'right':
if self.truncation_strategy == 'right':
input_ids = input_ids[:self.max_length]
if labels is not None:
labels = labels[:self.max_length]
Expand Down