Skip to content

feat:[AutoDeploy] Add support for Phi3/4 Model Family - #3079

Closed
Fridah-nv wants to merge 9 commits into
NVIDIA:mainfrom
nv-auto-deploy:fridah/phi3
Closed

Fridah-nv wants to merge 9 commits into
NVIDIA:mainfrom
nv-auto-deploy:fridah/phi3

Conversation

@Fridah-nv

@Fridah-nv Fridah-nv commented Mar 25, 2025 •

Copy link
Copy Markdown
Collaborator

Overview

  1. new transformation to unfuse qkv_gemm and gate_up_proj to standardize exported graph and apply later transformations
  2. Patch Phi-3 Model init issue with HF AutoModelForCausalLM

Testing

  1. unit test for unfuse_weights transformation
  2. integration tests for Phi models + quantized model

ACC difference:

gsm8k mmlu
baseline 79.68 69.93
autodeploy 77.27 70.56

Future Work:

  1. Support Phi3LongRoPEScaledRotaryEmbedding for Phi-3.5 and other long context models

Support Matrix

Model compile_backend / runtime / atten_backend world_size
microsoft/phi-4 {torch-simple,torch-opt}, {trtllm,demollm}, {TritonWithFlattenedInputs,FlashInfer} 1,2,4
microsoft/Phi-3-mini-4k-instruct {torch-simple,torch-opt}, {trtllm,demollm}, {TritonWithFlattenedInputs} 1,2,4
microsoft/Phi-3-medium-4k-instruct {torch-simple,torch-opt}, {trtllm,demollm}, {TritonWithFlattenedInputs} 1,2,4

@Fridah-nv
Fridah-nv force-pushed the fridah/phi3 branch 2 times, most recently from 58b275a to 1317bcc Compare March 25, 2025 23:23

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.

what's wrong with

Suggested change
get_param_node = gm.graph.create_node("get_attr", full_new_param_name)
get_param_node = gm.get_attr(full_new_param_name, torch.Tensor)

?

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.

The two versions look identical to me. I probably changed it for debugging and to keep it same as adding quant scale buffers.
Both approaches creates a new get_attr node in the graph, I don't see much difference between them. Do you think one approach is better than the other?

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.

let's use gm.get_attr. If there is a utility by torch.fx supporting this functionality, why not use it :)

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.

what's wrong with adding everything to the root module? Is this the clean-up issue that canonicalize_graph won't remove the weight parameters unless the entire submodule is unused?

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.

Or let me ask differently. Do we want unique submodule names for this to ensure the clean-up works? Then we should hard-code that into the utility

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.

There are two concerns:

  1. New params registered as gm's attribute won't get handled by gm.delete_all_unused_submodules(), because they are not in submodules
  2. Submodules are only deleted when all of the content is unused, so we should keep unique submodule names

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 also updated unfuse_weight to have unique submodule for every unfused weight.

Now it looks something like:
'model.layers.0.self_attn.qkv_proj_unfused_slice_6.weight'

And later they get fused into:
model.layers.0.self_attn.fused__qkv_proj_unfused_slice_6__qkv_proj_unfused_slice_7__qkv_proj_unfused_slice_8.weight

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.

Looks great. Now let's document why we put each fused/unfused weight into separate submodules

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.

And one more reason is that this avoids conflicts with quantization formats.
Currently quantization transformation assumes each "leaf" submodule contains a weight attribute and add input and weight scales to the submodule.

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.

now that we are assigning fused weights to distinct submodules we don't need key_fused anymore or the idx argument from which it is created. We can remove both

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.

Thanks for the catch, need to update quant scale buffer registration as well. Now buffers are in submodules as well. Will do an e2e test with quant models

Comment on lines 99 to 101

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.

Since we are already algorithmically generating the weight_key and new_module_path, I suggest we make it more verbose. Then it can help with debugging as well.

Here is one suggestion: https://chatgpt.com/share/67ec5bb7-3af4-8009-be61-ba3ef69b65d3

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.

Thank you, I added the function

Comment on lines 78 to 89

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.

Do we know how this interacts with quantization? Supposedly quantization would operate on the original weights, right?

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.

That's a good point. I wasn't considering quantization before.
I think the simpler way to support unfusing quantized models is to apply quantize first on the original weights, then update unfuse_weights to handle simple.linear as well as quant.linear, separate the scales together with params

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.

I agree. Can we move unfuse_weights after quantize then to reflect this hierarchy?

and maybe add a TODO into the unfuse_weights transformation that we need to add support for quantization here

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.

why not just use ad_logger instead of making it an argument?

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.

add type hints

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.

Done

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.

This function is called add_new_parameter_to_submodule but then we allow setting arbitrary attributes in the submodule.

Let's be consistent. If we just wanna allow setting parameters then let's do submodule.register_parameter(...). Otherwise, let's change the name of the function indicating any type attribute can be set.

I vote for the latter

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.

Updated for the latter case

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.

Let's add a tiny version of Phi to tests/unittest/_torch/auto_deploy/unit/singlegpu/test_ad_build_small_single.py as well. This architecture is somewhat different than others and so we should include in our pre-merge pipeline as well

@Fridah-nv Fridah-nv self-assigned this Apr 2, 2025
Signed-off-by: Ubuntu <[email protected]>
Signed-off-by: Ubuntu <[email protected]>
Signed-off-by: Frida Hou <[email protected]>
…nfuse_weight to register new param in new submodule for each weight; add small build test for phi

Signed-off-by: Frida Hou <[email protected]>
@Fridah-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1038 [ run ] triggered by Bot

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@Fridah-nv
Fridah-nv requested a review from meenchen April 2, 2025 19:57
Signed-off-by: Frida Hou <[email protected]>
@Fridah-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1040 [ run ] triggered by Bot

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1042 [ kill ] triggered by Bot

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1040 [ run ] completed with state ABORTED

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #1042 [ kill ] completed with state SUCCESS
Successfully killed previous jobs for commit e702a3a

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.

let's use gm.get_attr. If there is a utility by torch.fx supporting this functionality, why not use it :)

Comment on lines 78 to 89

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.

I agree. Can we move unfuse_weights after quantize then to reflect this hierarchy?

and maybe add a TODO into the unfuse_weights transformation that we need to add support for quantization here

Comment on lines +120 to +128
# Clean up deleted modules to save GPU memory
gm.graph.eliminate_dead_code()
gm.delete_all_unused_submodules()

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.

Can you delete the old unused weight parameter directly?

Otherwise, we get at O(n^2) since these utilities also iterate over all nodes, where n = # nodes

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.

Done


"""
token_lists = [key.split(".") for key in keys]

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.

should we assert that

assert len(set(tokens[-1] for tokens in token_lists))==1

?

@@ -110,6 +136,31 @@ def split_output(tensor: torch.Tensor) -> Tuple[torch.Tensor, ...]:
gm.delete_all_unused_submodules()

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.

Can you delete the old unused weight parameter directly?

Otherwise, we get at O(n^2 since these utilities also iterate over all nodes, where n = # nodes

(same as other comment)

Comment on lines +280 to +282
def add_new_parameter_to_submodule(
gm: GraphModule, new_submodule_name: str, new_param_name: str, new_param: torch.Tensor
) -> str:

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.

Let's use this function to keep it more consistent with other nn.Module functions:

Suggested change
def add_new_parameter_to_submodule(
gm: GraphModule, new_submodule_name: str, new_param_name: str, new_param: torch.Tensor
) -> str:
def register_parameter(mod: nn.Module, target: str, param: Optional[nn.Parameter]) -> str:

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.

Sorry, I don't get this. Do you suggest to change add_new_parameter_to_submodule's signature into def register_parameter(mod: nn.Module, target: str, param: Optional[nn.Parameter]) -> str:?

Or changing submodule.register_parameter(new_param_name, new_param) into sth like register_parameter(submodule, new_param_name, new_param)?

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.

Looks great. Now let's document why we put each fused/unfused weight into separate submodules

@lucaslie lucaslie added the AutoDeploy <NV> AutoDeploy Backend label May 14, 2025
@Fridah-nv

Copy link
Copy Markdown
Collaborator Author

Main part merged in nv-auto-deploy#17
Related backlog: #4586

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

Labels

AutoDeploy <NV> AutoDeploy Backend

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants