Skip to content

feat: modify kwai template#9117

Open
LeonDiao0427 wants to merge 1 commit into
modelscope:mainfrom
LeonDiao0427:feature/update-kwai-template
Open

feat: modify kwai template#9117
LeonDiao0427 wants to merge 1 commit into
modelscope:mainfrom
LeonDiao0427:feature/update-kwai-template

Conversation

@LeonDiao0427
Copy link
Copy Markdown

PR type

  • Bug Fix

PR information

Fix the KeyeVLTemplate implementation with the following changes:

  • Add RoPE position_ids computation via model.get_rope_index_slowfast()
  • Remove incorrect do_resize=False in image preprocessing

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 refactors the Kwai template to support slow-fast multimodal inputs and modularizes visual embedding logic. It also integrates RoPE index calculation into the post-encoding phase. Review feedback indicates that the early return in _post_encode may incorrectly bypass necessary embedding and position ID logic during inference. A suggestion was also made to use a more robust method for calculating dimension products to avoid floating-point conversions.

Comment on lines 154 to 155
if not self.is_training:
return inputs
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.

high

The early return if not self.is_training prevents the computation of inputs_embeds and the new position_ids (via get_rope_index_slowfast) during inference when using the transformers backend. For multimodal models like KeyeVL, replacing token embeddings with visual embeddings and using the correct RoPE indices is essential for correct inference results. Since this PR aims to fix the RoPE implementation, this logic should also be applied during inference.

cu_seqlens = [0]
for idx, thw_tuple in enumerate(grid_hws):
numel = int(np.prod(thw_tuple))
media_position_ids = torch.arange(numel, device=device) % int(np.prod(thw_tuple[1:]))
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

Using np.prod on a slice of a tuple returns a float in many NumPy versions. While it is cast to int here, using math.prod (available in Python 3.8+) is generally more idiomatic for calculating the product of dimensions in a shape tuple and avoids floating-point conversions.

Suggested change
media_position_ids = torch.arange(numel, device=device) % int(np.prod(thw_tuple[1:]))
media_position_ids = torch.arange(numel, device=device) % int(torch.prod(torch.tensor(thw_tuple[1:])))

@Jintao-Huang
Copy link
Copy Markdown
Collaborator

thanks!

please pass lint test

pip install pre-commit
pre-commit run --all-files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants