Skip to content

Support InternLM models - #266

Closed
wangruohui wants to merge 27 commits into
NVIDIA:mainfrom
wangruohui:internlm-main
Closed

wangruohui wants to merge 27 commits into
NVIDIA:mainfrom
wangruohui:internlm-main

Conversation

@wangruohui

@wangruohui wangruohui commented Nov 3, 2023 •

Copy link
Copy Markdown
Contributor

This PR support InternLM model based on main branch. It is a follow up of #123 with rebased on to main.

Supported TRTLLM build options includes:

  • FP16 / BP16
  • TP / PP
  • int4/8 weight only
  • int8 weight + int8 kv cache
  • smooth quant

Tested on both internlm-chat-7b and internlm-chat-20b.

Comment thread examples/internlm/README.md Outdated

## Support Matrix
* FP16 / BF16
<!-- * FP8 -->

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.

Please delete all comment lines in support Matrix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

Comment thread examples/internlm/README.md Outdated
--engine_dir ./internlm-chat-20b/trt_engines/smoothquant/1-gpu
```

#### FP8 Post-Training Quantization

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.

No need to mention FP8 at current stage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

Comment thread examples/internlm/README.md Outdated
--tp_size 2
```
-->
#### Groupwise quantization (AWQ/GPTQ)

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.

Remove this part.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

Comment thread examples/llama/README.md Outdated

```bash
python3 hf_llama_convert.py -i /llama-models/llama-7b-hf -o /llama/smooth_llama_7B/int8_kv_cache/ --calibrate-kv-cache -t fp16
python3 hf_llama_convert.py -i llama-7b-hf -o smooth_llama_7B/int8_kv_cache/ --calibrate-kv-cache -t fp16

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.

why we need to update llama doc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

a mistake during commit, recovered.

Comment thread docs/source/precision.md Outdated
| OPT | Y | Y | Y | . | . | . | . | . | . |
| SantaCoder | Y | Y | Y | . | . | . | . | . | . |
| StarCoder | Y | Y | Y | . | . | . | . | . | . |
| InternLM | Y | Y | Y | ? | Y | Y | Y | ? | ? |

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.

Set ? to .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@nv-guomingz

Copy link
Copy Markdown
Collaborator

Hi @wangruohui , we're going to publish lots of update to main branch this week.
Base on our internal discussion, we'll squash all features into one commit.(e.g, #188.

Of coz, we'll add your name into the co-author list and credit your work on the release notes for internllm model.

Is that ok for you?

@wangruohui

Copy link
Copy Markdown
Contributor Author

Hi @wangruohui , we're going to publish lots of update to main branch this week. Base on our internal discussion, we'll squash all features into one commit.(e.g, #188.

Of coz, we'll add your name into the co-author list and credit your work on the release notes for internllm model.

Is that ok for you?

Hi @nv-guomingz ,

It's OK for me. Please move on.

@kaiyux

kaiyux commented Nov 7, 2023

Copy link
Copy Markdown
Member

Hi @wangruohui , we just pushed an update to the main branch, and we added you as co-author, which is also mentioned in the announcement.

I'm going to close this PR, please let us know if you have any questions. Thanks again for the great contribution.

@kaiyux kaiyux closed this Nov 7, 2023
@RunningLeon

Copy link
Copy Markdown
Contributor

@nv-guomingz @kaiyux

Hi, guys. Sorry to trouble you. I want to support InternLM2 models such as internlm2-chat-7b into your amazing project. Could you guys give me some tips on how to do it more neatly? Here are some things that should be considered:

I've tried to modify examples/llama/convert_checkpoint.py and it could build and run on fp16. But how could it be done more neatly? Should I create a new examples/internlm2/convert_checkpoint.py?

Looking forward to your kind reply.
Best Regards.

@nv-guomingz

Copy link
Copy Markdown
Collaborator

Hi @RunningLeon,

Thanks for your contributing on TensorRT-LLM.

It's ok for me to create the unique or dedicated internlm2 folder at current stage but we can't guarantee that it won't not be removed (merged into internlm) later. We're actively working on new builder workflow and relateive api refactoring, the convert_checkpoint.py will be a thin wrapper in the next formal release.

Thanks,
Guoming

@ChengYouFancy

Copy link
Copy Markdown

Hi, @RunningLeon

I'm troubled by internlm2 to tensorrt, I'v tried use llama convert, but weight names are different.
Could you share your examples/internlm2/convert_checkpoint.py code with me? And I will appreciate very much!
Looking forward to your kind reply.
Thanks

@RunningLeon

Copy link
Copy Markdown
Contributor

Hi @RunningLeon,

Thanks for your contributing on TensorRT-LLM.

It's ok for me to create the unique or dedicated internlm2 folder at current stage but we can't guarantee that it won't not be removed (merged into internlm) later. We're actively working on new builder workflow and relateive api refactoring, the convert_checkpoint.py will be a thin wrapper in the next formal release.

Thanks, Guoming

@nv-guomingz hi, thanks for your kind reply. I noticed that in the new workflow for quantization, we are using quantize.py which is using ammo. Is ammo open source? how do user contribute to ammo to support new models?
Thanks.

@RunningLeon

RunningLeon commented Mar 26, 2024 •

Copy link
Copy Markdown
Contributor

Hi, @RunningLeon

I'm troubled by internlm2 to tensorrt, I'v tried use llama convert, but weight names are different. Could you share your examples/internlm2/convert_checkpoint.py code with me? And I will appreciate very much! Looking forward to your kind reply. Thanks

@ChengYouFancy hi, for internlm2, you can convert it to llama style, but there's some thing you need to change.

  1. Using the convert tools as mentioned in [Feature] 是否已经支持tensorrt-llm或者计划支持? InternLM/InternLM#714 (comment)
  2. fix the rope_scaling with dynamic as mentioned in v0.8.0 long text(100k) input, output precision worse(error) #1299 (comment)
  3. you may need to change tokenizer config to skip special token

once there's a good pipeline, we'll pull a request to tensorrt-llm.

@ChengYouFancy

Copy link
Copy Markdown

Hi, @RunningLeon
I'm troubled by internlm2 to tensorrt, I'v tried use llama convert, but weight names are different. Could you share your examples/internlm2/convert_checkpoint.py code with me? And I will appreciate very much! Looking forward to your kind reply. Thanks

@ChengYouFancy hi, for internlm2, you can convert it to llama style, but there's some thing you need to change.

  1. Using the convert tools as mentioned in [Feature] 是否已经支持tensorrt-llm或者计划支持? InternLM/InternLM#714 (comment)
  2. fix the rope_scaling with dynamic as mentioned in v0.8.0 long text(100k) input, output precision worse(error) #1299 (comment)
  3. you may need to change tokenizer config to skip special token

once there's a good pipeline, we'll pull a request to tensorrt-llm.

Thanks for your kind reply, I followed your pipeline to convert it to tensorrt successfully. It's an amazing job.
but the result of inference is incorrect, it's always "<|im_start|>" token.
Here is some situation about the model:

  1. I finetuned the internlm2-chat model
  2. I use the intern2 template correctly in finetune and inference
  3. I did not change tokenizer config to skip special token as you suggest, I'm not sure what should I change

Do you have some idea about the inference result?
Thanks for your help again!

@RunningLeon

Copy link
Copy Markdown
Contributor

@ChengYouFancy hi, did you fix the rope in cpp and rebuild trtllm?

@ChengYouFancy

Copy link
Copy Markdown

@ChengYouFancy hi, did you fix the rope in cpp and rebuild trtllm?

I followed the pipeline:

  1. use convert2llama.py in InternLm repo to convert internlm2 to llama

  2. I fixed the rope in cpp

  3. use convert_checkpoint.py in TensorRT/example/llama to convert the model of first step
    CUDA_VISIBLE_DEVICES=2 python3 convert_checkpoint.py --model_dir /workspace/model/internlm-7b-chat-all-sft-convert-llama/ \ --dtype float16 \ --output_dir /workspace/model/internlm-7b-chat-all-sft-trt_engines

  4. use trtllm-build to build trtllm
    CUDA_VISIBLE_DEVICES=2 trtllm-build --checkpoint_dir /workspace/model/internlm-7b-chat-all-sft-trt_engines \ --output_dir /workspace/model/internlm-7b-chat-all-sft-tensorrt \ --gemm_plugin float16 \ --remove_input_padding enable \ --max_batch_size 8 \ --max_output_len 8192

  5. test the inference based on run.py
    the inference is incorrect

@RunningLeon

Copy link
Copy Markdown
Contributor

@ChengYouFancy hi, you may need to check the input prompt to see if it has the correct chat template. You can add a template in here

DEFAULT_PROMPT_TEMPLATES = {

    'LlamaForCausalLM':
    "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{input_text}<|im_end|>\n<|im_start|>assistant\n",

@ChengYouFancy

Copy link
Copy Markdown

@ChengYouFancy hi, you may need to check the input prompt to see if it has the correct chat template. You can add a template in here

DEFAULT_PROMPT_TEMPLATES = {

    'LlamaForCausalLM':
    "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{input_text}<|im_end|>\n<|im_start|>assistant\n",

input text looks like is correct
`Input [Text 0]: " <|im_start|> system
You are an AI assistant whose name is InternLM (书生·浦语).

  • InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
  • InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.<|im_end|>
    <|im_start|> user
    中国的首都是哪里<|im_end|>
    <|im_start|> assistant
    "
    Output [Text 0 Beam 0]: " <|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|><|im_start|>"`

@RunningLeon

RunningLeon commented Mar 27, 2024 •

Copy link
Copy Markdown
Contributor

@ChengYouFancy Have you rebuild and reinstall trtllm ?

# Build TensorRT-LLM for Ampere.
python3 ./scripts/build_wheel.py --cuda_architectures "80-real;86-real" --trt_root /usr/local/tensorrt

@ChengYouFancy

Copy link
Copy Markdown

@ChengYouFancy Have you rebuild and reinstall trtllm ?

# Build TensorRT-LLM for Ampere.
python3 ./scripts/build_wheel.py --cuda_architectures "80-real;86-real" --trt_root /usr/local/tensorrt

I haven't, should I do it after I fixed the rope in cpp?

@RunningLeon

Copy link
Copy Markdown
Contributor

@ChengYouFancy Have you rebuild and reinstall trtllm ?

# Build TensorRT-LLM for Ampere.
python3 ./scripts/build_wheel.py --cuda_architectures "80-real;86-real" --trt_root /usr/local/tensorrt

I haven't, should I do it after I fixed the rope in cpp?

Yes. You should rebuild and reinstall after fixing cpp

@ChengYouFancy

Copy link
Copy Markdown

cpp

Thanks a lot, I will try it

@ChengYouFancy

Copy link
Copy Markdown

@ChengYouFancy Have you rebuild and reinstall trtllm ?

# Build TensorRT-LLM for Ampere.
python3 ./scripts/build_wheel.py --cuda_architectures "80-real;86-real" --trt_root /usr/local/tensorrt

I haven't, should I do it after I fixed the rope in cpp?

Yes. You should rebuild and reinstall after fixing cpp

I'v tried to rebuild tensorrtllm, but cmake failed. and I could not search any similar issue to help me
error info:
subprocess.CalledProcessError: Command 'cmake -DCMAKE_BUILD_TYPE="Release" -DBUILD_PYT="ON" -DBUILD_PYBIND="ON" -DNVTX_DISABLE="ON" "-DCMAKE_CUDA_ARCHITECTURES=80-real;86-real" -DTRT_LIB_DIR=/usr/local/tensorrt/targets/x86_64-linux-gnu/lib -DTRT_INCLUDE_DIR=/usr/local/tensorrt/include -S "/workspace/code/TensorRT-LLM/cpp"' returned non-zero exit status 1.
image

@nv-guomingz

nv-guomingz commented Mar 27, 2024 •

Copy link
Copy Markdown
Collaborator

Hi @RunningLeon,
Thanks for your contributing on TensorRT-LLM.
It's ok for me to create the unique or dedicated internlm2 folder at current stage but we can't guarantee that it won't not be removed (merged into internlm) later. We're actively working on new builder workflow and relateive api refactoring, the convert_checkpoint.py will be a thin wrapper in the next formal release.
Thanks, Guoming

@nv-guomingz hi, thanks for your kind reply. I noticed that in the new workflow for quantization, we are using quantize.py which is using ammo. Is ammo open source? how do user contribute to ammo to support new models? Thanks.

Hi @RunningLeon

AMMO is not open-sourced yet and I don't we have plan to open source it soon.

how do user contribute to ammo to support new models?

Currently, the typical workflow is we add a new model supporting on high precision and AMMO team will responsible for quantizing the model.

@RunningLeon

Copy link
Copy Markdown
Contributor

Hi @RunningLeon,
Thanks for your contributing on TensorRT-LLM.
It's ok for me to create the unique or dedicated internlm2 folder at current stage but we can't guarantee that it won't not be removed (merged into internlm) later. We're actively working on new builder workflow and relateive api refactoring, the convert_checkpoint.py will be a thin wrapper in the next formal release.
Thanks, Guoming

@nv-guomingz hi, thanks for your kind reply. I noticed that in the new workflow for quantization, we are using quantize.py which is using ammo. Is ammo open source? how do user contribute to ammo to support new models? Thanks.

Hi @RunningLeon

AMMO is not open-sourced yet and I don't we have plan to open source it soon.

how do user contribute to ammo to support new models?

Currently, the typical workflow is we add a new model supporting on high precision and AMMO team will responsible for quantizing the model.

@nv-guomingz hi, Got it. Thanks for your quick reply.

@ChengYouFancy

Copy link
Copy Markdown

@ChengYouFancy Have you rebuild and reinstall trtllm ?

# Build TensorRT-LLM for Ampere.
python3 ./scripts/build_wheel.py --cuda_architectures "80-real;86-real" --trt_root /usr/local/tensorrt

I haven't, should I do it after I fixed the rope in cpp?

Yes. You should rebuild and reinstall after fixing cpp

Thank you very much, after rebuild and reinstall trtllm, I've convert internlm2 to tensorrt_llm successfully and the inference result is correct finally
Best Regard

@RunningLeon

Copy link
Copy Markdown
Contributor

@nv-guomingz hi, could you kindly review this PR #1392 if you have time? Thanks in advance.

@nv-guomingz

Copy link
Copy Markdown
Collaborator

@nv-guomingz hi, could you kindly review this PR #1392 if you have time? Thanks in advance.

Hi @RunningLeon , thanks for your great contribution to TRT-LLM project.
We'll review your MR soon and merge your code into our mainstream once it verified.
And then we'll cite your contribution into our release notes.

cc @juney-nvidia @kaiyux @byshiue for vis

@RunningLeon RunningLeon mentioned this pull request Apr 10, 2024
@icoderzqliu

Copy link
Copy Markdown

Hi, @RunningLeon
I'm troubled by internlm2 to tensorrt, I'v tried use llama convert, but weight names are different. Could you share your examples/internlm2/convert_checkpoint.py code with me? And I will appreciate very much! Looking forward to your kind reply. Thanks

@ChengYouFancy hi, for internlm2, you can convert it to llama style, but there's some thing you need to change.

  1. Using the convert tools as mentioned in [Feature] 是否已经支持tensorrt-llm或者计划支持? InternLM/InternLM#714 (comment)
  2. fix the rope_scaling with dynamic as mentioned in v0.8.0 long text(100k) input, output precision worse(error) #1299 (comment)
  3. you may need to change tokenizer config to skip special token

once there's a good pipeline, we'll pull a request to tensorrt-llm.

Hello, I was doing inference after converting internlm2 to llama and got an error:

Loading checkpoint shards:   0%|                                                                                                                | 0/21 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "/data3/zqliu/program/LLaMA-Factory/law/infer_laws.py", line 75, in <module>
    model = AutoModelForCausalLM.from_pretrained(model_path,
  File "/home/icoderzqliu/miniconda3/envs/lf/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py", line 566, in from_pretrained
    return model_class.from_pretrained(
  File "/home/icoderzqliu/miniconda3/envs/lf/lib/python3.10/site-packages/transformers/modeling_utils.py", line 3706, in from_pretrained
    ) = cls._load_pretrained_model(
  File "/home/icoderzqliu/miniconda3/envs/lf/lib/python3.10/site-packages/transformers/modeling_utils.py", line 4091, in _load_pretrained_model
    state_dict = load_state_dict(shard_file)
  File "/home/icoderzqliu/miniconda3/envs/lf/lib/python3.10/site-packages/transformers/modeling_utils.py", line 503, in load_state_dict
    with safe_open(checkpoint_file, framework="pt") as f:
safetensors_rust.SafetensorError: Error while deserializing header: HeaderTooLarge

Can the converted model be loaded directly using AutoModelForCausalLM.from_pretrained? Or should it be directly converted into tensorrt form?

@RunningLeon

Copy link
Copy Markdown
Contributor

@icoderzqliu hi, the converted llama-style internlm2 can be loaded and run with transformers. Not met your issue. Are you using internlm2-chat-20b? BTW, you can try pr #1392, no need to convert to llama style.

@icoderzqliu

Copy link
Copy Markdown

@icoderzqliu hi, the converted llama-style internlm2 can be loaded and run with transformers. Not met your issue. Are you using internlm2-chat-20b? BTW, you can try pr #1392, no need to convert to llama style.

@RunningLeon hi, thank you for your quick reply. Yes, I use internlm2-chat-20b. I will try to use this pr #1392. It seems that the source code needs to be changed. Is there any more detailed steps that I can follow?

@ChengYouFancy

Copy link
Copy Markdown

@RunningLeon hi,we use lora finetuned the internlm2 model. Now we can convert the base model to llama, but not lora part, we tried to change the code of InternLM/tools/convert2llame.py to transfer lora to llama style, but did not work. Is there any other tools could work for lora?
tips: we can't export base model and lora to one single model, because we want to use https://github.com/NVIDIA/TensorRT-LLM/tree/main/examples/llama#run-llama-with-several-lora-checkpoints this feature. Thus, if we can transfer internlm2 lora to llama style, then we can use examples/hf_lora_convert.py to build trt-llm

@RunningLeon

Copy link
Copy Markdown
Contributor

@ChengYouFancy hi, sorry for the late reply due to the vacation. Maybe you can open an issue to https://github.com/InternLM/InternLM since the convert script is provided by them.

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