Skip to content
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

add save & load support for NPU optimized model #11999

Merged
merged 2 commits into from
Sep 3, 2024

Conversation

rnwang04
Copy link
Contributor

@rnwang04 rnwang04 commented Sep 3, 2024

Description

1. Why the change?

  • fix support of NPU save & load func
  • add save & load support for NPU optimized model

2. User API changes

  • for non-optimized model
# save.py
from ipex_llm.transformers.npu_model import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True,
                                             load_in_low_bit="sym_int4",
                                             attn_implementation="eager")
model.save_low_bit("llama_low_bit_npu")
# load.py
model = AutoModelForCausalLM.load_low_bit("llama_low_bit_npu",
                                          attn_implementation="eager")
  • for optimized model
# save.py
from ipex_llm.transformers.npu_model import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
    model_path,
    torch_dtype=torch.float16,
    trust_remote_code=True,
    attn_implementation="eager",
    load_in_low_bit="sym_int4",
    optimize_model=True,
    max_output_len=args.max_output_len,
    max_prompt_len=args.max_prompt_len,
    intra_pp=args.intra_pp,
    inter_pp=args.inter_pp,
    transpose_value_cache=not args.disable_transpose_value_cache,
)

model.save_low_bit("llama_low_bit_npu")
# load.py
model = AutoModelForCausalLM.load_low_bit("llama_low_bit_npu",
                                          attn_implementation="eager",
                                          torch_dtype=torch.float16,
                                          optimize_model=True,
                                          max_output_len=args.max_output_len,
                                          max_prompt_len=args.max_prompt_len,
                                          intra_pp=args.intra_pp,
                                          inter_pp=args.inter_pp,
                                          transpose_value_cache=not args.disable_transpose_value_cache)

Remain issues:

  • for optimized model, save.py & load.py must be different scirpt, otherwise program will hang at loading stage .

4. How to test?

  • Unit test: Please manually trigger the PR Validation here by inputting the PR number (e.g., 1234). And paste your action link here once it has been successfully finished.

@rnwang04 rnwang04 requested a review from plusbang September 3, 2024 08:57
@rnwang04 rnwang04 requested a review from jason-dai September 3, 2024 08:57
Copy link
Contributor

@plusbang plusbang left a comment

Choose a reason for hiding this comment

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

LGTM, maybe we could also add related example later.

@rnwang04
Copy link
Contributor Author

rnwang04 commented Sep 3, 2024

LGTM, maybe we could also add related example later.

Yeah we can add the usage in our NPU examples in later PR.

@rnwang04 rnwang04 merged commit 9eaff5e into intel-analytics:main Sep 3, 2024
1 check passed
@rnwang04 rnwang04 deleted the npu_save_load_api branch September 3, 2024 12:53
@rnwang04
Copy link
Contributor Author

rnwang04 commented Sep 3, 2024

cyita pushed a commit to cyita/BigDL that referenced this pull request Sep 5, 2024
cranechu0131 pushed a commit to cranechu0131/ipex-llm that referenced this pull request Sep 9, 2024
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