Skip to content

feat: Support cos_sin_cache in all cases. - #3020

Closed
yuxianq wants to merge 6 commits into
NVIDIA:mainfrom
yuxianq:user/yuxianq/cos-sin-cache
Closed

yuxianq wants to merge 6 commits into
NVIDIA:mainfrom
yuxianq:user/yuxianq/cos-sin-cache

Conversation

@yuxianq

@yuxianq yuxianq commented Mar 24, 2025

Copy link
Copy Markdown
Collaborator

This MR contains the following updates:

  1. Handle fuse_pos_embd=True/False and create RotaryEmbedding inside attention module, so that the users don't need to handle it in the modeling files.
  2. Cache cos_sin for unfused rope implementation. If flashinfer is available, use apply_rope_with_cos_sin_cache_inplace instead of apply_rope_inplace. Otherwise, we fallback to pure pytorch implementation, which can support any rope now.
  3. We use create_rope_const_params to create and cache cos_sin_cache for all rope types, including Deepseek yarn rope.

@yuxianq
yuxianq requested review from BestJuly, QiJune, hlu1 and kaiyux March 24, 2025 09:44
@yuxianq

yuxianq commented Mar 24, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --add-multi-gpu-test

@niukuo

niukuo commented Mar 24, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #283 [ run ] triggered by Bot

@niukuo

niukuo commented Mar 24, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #283 [ run ] completed with state FAILURE
/LLM/main/L0_MergeRequest_PR pipeline #272 completed with status: 'FAILURE'

@yuxianq

yuxianq commented Mar 25, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --add-multi-gpu-test

@niukuo

niukuo commented Mar 25, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #387 [ run ] triggered by Bot

@niukuo

niukuo commented Mar 25, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #387 [ run ] completed with state FAILURE
/LLM/main/L0_MergeRequest_PR pipeline #345 completed with status: 'FAILURE'

@yuxianq

yuxianq commented Mar 25, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --add-multi-gpu-test

@niukuo

niukuo commented Mar 25, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #430 [ run ] triggered by Bot

@niukuo

niukuo commented Mar 25, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #430 [ run ] completed with state FAILURE
/LLM/main/L0_MergeRequest_PR pipeline #369 completed with status: 'FAILURE'

@yuxianq
yuxianq force-pushed the user/yuxianq/cos-sin-cache branch 2 times, most recently from 95840f8 to 54d797b Compare March 26, 2025 04:00
@yuxianq

yuxianq commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --add-multi-gpu-test

@niukuo

niukuo commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #510 [ run ] triggered by Bot

@BestJuly
BestJuly removed their request for review March 26, 2025 05:20
@BestJuly

Copy link
Copy Markdown
Collaborator

I think I am pinged by mistake, is the review request actually pointed to @litaotju ?

@niukuo

niukuo commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #510 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #437 completed with status: 'FAILURE'

@QiJune

QiJune commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

@yuxianq Can we split this PR to several small PRs? For example, the first item can be a single PR.

  1. Handle fuse_pos_embd=True/False and create RotaryEmbedding inside attention module, so that the users don't need to handle it in the modeling files.

@yuxianq
yuxianq requested a review from litaotju March 26, 2025 07:01
@yuxianq

yuxianq commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator Author

Can we split this PR to several small PRs? For example, the first item can be a single PR.

@QiJune I will have a try. Let me pass the CI first to validate that these features work correctly.

@yuxianq

yuxianq commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --add-multi-gpu-test

@yuxianq yuxianq changed the title Support cos_sin_cache in all cases. feat: Support cos_sin_cache in all cases. Mar 26, 2025
@niukuo

niukuo commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #550 [ run ] triggered by Bot

@niukuo

niukuo commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #550 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #469 completed with status: 'FAILURE'

@yuxianq

yuxianq commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --add-multi-gpu-test

@niukuo

niukuo commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #584 [ run ] triggered by Bot

@niukuo

niukuo commented Mar 26, 2025

Copy link
Copy Markdown
Collaborator

PR_Github #584 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #497 completed with status: 'FAILURE'

Comment thread examples/quantization/quantize.py Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just curious, are someone importing this file?

It should be used a cli command only.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No. I refactor it when I debug thread/process leak for multiprocessing module. It should be fine for a cli command.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks somewaht ad-hoc to me. Is it possible to not relying on the model type hard code string here in a general interface?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is a workaround for DeepSeek V3 Lite since its rope_scaling is null in config.json. DeepSeek V3/R1 should be fine without this code. I have added a comment to explain it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe Nit.

Can we say create*positions_for_yarn or something similar w/o to be too specific to DeepSeek easily?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I have renamed it to create_sinusoidal_positions_yarn. DeepSeek use a extended version of yarn with additional mscale and mscale_all_dim arguments. Since it is compatible with the original version when using default value for mscale and mscale_all_dim, we can merge the Deepseek variant with the original version.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

you should be the custom op will do rope fusion right?

Maybe renaming to something like? self.rope_fused_in_custom_op = True

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I have renamed it to enable_rope_fusion.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

"support" is a vague word.
support means which one?

  1. configuable, both fused qkv and unfused qkv can run. or
  2. requires fused qkv?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I use "support" because a backend can support both fused qkv and unfused qkv, so they are not mutually exclusive. Currently each backend only supports one case.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

when will this be true?
Can we always attach the extra_attrs in _load_model such that this won't be needed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Some tests use stub models and override _load_model (see https://github.com/NVIDIA/TensorRT-LLM/blob/7441714d66e179192ef0642a9a150f5486e5b06b/tests/unittest/_torch/test_pytorch_model_engine.py#L50). So we have to add this code to ensure the extra_attrs is attached. I have added comment to explain it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

shall we wrap the whole forward function be inside this context manager?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point, have wrapped the whole forward function.

Comment thread tensorrt_llm/llmapi/mpi_session.py Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Superjomn do you remember if we have some reason to make this "wait=False"?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It can avoid thread leak, which can further avoid memory leak.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

will not this llm object be automatically destoryed when the function return? its just a local var.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it is unnecessary. Just for debugging OOM issues. I have removed all unnecessary del llm.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same here, is there some reason that the object not deleted by python when function returns?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I have removed all unnecessary del llm.

@yuxianq

yuxianq commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --stage-list "A30-7"

@yuxianq
yuxianq force-pushed the user/yuxianq/cos-sin-cache branch from 77b5b14 to 7441714 Compare April 2, 2025 09:48
@yuxianq

yuxianq commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --stage-list "A30-7"

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1005 [ run ] triggered by Bot

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1005 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #776 (Partly Tested) completed with status: 'FAILURE'

@yuxianq

yuxianq commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --add-multi-gpu-test

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1030 [ run ] triggered by Bot

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1030 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #794 completed with status: 'FAILURE'

@yuxianq

yuxianq commented Apr 3, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --add-multi-gpu-test

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1088 [ run ] triggered by Bot

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1088 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #829 completed with status: 'FAILURE'

yuxianq added 6 commits April 3, 2025 10:40
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
@yuxianq
yuxianq force-pushed the user/yuxianq/cos-sin-cache branch from 269a813 to e774b7c Compare April 3, 2025 10:46
@yuxianq

yuxianq commented Apr 3, 2025

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --add-multi-gpu-test

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1110 [ run ] triggered by Bot

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1110 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #843 completed with status: 'FAILURE'

@yuxianq

yuxianq commented Apr 14, 2025

Copy link
Copy Markdown
Collaborator Author

This PR has been split into several PRs. Move the main part of this PR to #3517 and close this outdated PR.

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.

6 participants