Skip to content

[v0.5.10] Fix mask_utils for transformers >=5.0#925

Merged
yueming-yuan merged 2 commits intobump-sglang-v0.5.10from
fix/mask-utils-transformers-v5
Apr 6, 2026
Merged

[v0.5.10] Fix mask_utils for transformers >=5.0#925
yueming-yuan merged 2 commits intobump-sglang-v0.5.10from
fix/mask-utils-transformers-v5

Conversation

@yueming-yuan
Copy link
Copy Markdown
Collaborator

Summary

  • transformers 5.x changed apply_chat_template(tokenize=True) to return BatchEncoding instead of list[int]
  • mask_utils.py used direct slicing on the result, which broke (dict has len()=2 for its keys)
  • Added _apply_chat_template_ids() wrapper that normalizes the return type

Test plan

  • test_loss_mask_qwen3_simple passes
  • test_loss_mask_qwen3_tools passes

@yueming-yuan yueming-yuan changed the title Fix mask_utils for transformers >=5.0 [v0.5.10] Fix mask_utils for transformers >=5.0 Apr 6, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a helper function _apply_chat_template_ids in miles/utils/mask_utils.py to ensure that apply_chat_template consistently returns a list of token IDs, addressing changes in transformers >= 5.0. The existing logic in get_system_message_length, gen_multi_turn_loss_mask_qwen, and gen_multi_turn_loss_mask_qwen3 has been updated to use this new helper. Feedback suggests simplifying the helper function by using the return_dict=False parameter or utilizing an existing utility function.

Comment on lines +9 to +16
def _apply_chat_template_ids(tokenizer, messages, **kwargs) -> list[int]:
"""Wrapper that always returns list[int] from apply_chat_template(tokenize=True).

transformers >=5.0 returns BatchEncoding instead of list[int]."""
result = tokenizer.apply_chat_template(messages, tokenize=True, **kwargs)
if isinstance(result, list):
return result
return result["input_ids"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The implementation of _apply_chat_template_ids can be simplified by using the return_dict=False parameter. This is the standard way in the transformers library to ensure that apply_chat_template returns a list of token IDs instead of a BatchEncoding object, making the code more idiomatic and robust across different versions. Alternatively, consider using the existing apply_chat_template utility from miles.utils.chat_template_utils.template which already handles this logic and provides additional normalization for tools and messages.

Suggested change
def _apply_chat_template_ids(tokenizer, messages, **kwargs) -> list[int]:
"""Wrapper that always returns list[int] from apply_chat_template(tokenize=True).
transformers >=5.0 returns BatchEncoding instead of list[int]."""
result = tokenizer.apply_chat_template(messages, tokenize=True, **kwargs)
if isinstance(result, list):
return result
return result["input_ids"]
def _apply_chat_template_ids(tokenizer, messages, **kwargs) -> list[int]:
"""Wrapper that always returns list[int] from apply_chat_template(tokenize=True).
transformers >=5.0 returns BatchEncoding instead of list[int] by default."""
return tokenizer.apply_chat_template(
messages, tokenize=True, return_dict=False, **kwargs
)

@yueming-yuan yueming-yuan merged commit 14a0cdb into bump-sglang-v0.5.10 Apr 6, 2026
17 of 45 checks passed
@yueming-yuan yueming-yuan deleted the fix/mask-utils-transformers-v5 branch April 6, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant