Skip to content

[Feature] support deepseek-vl2 for pytorch engine - #3149

Merged
lvhan028 merged 20 commits into
InternLM:mainfrom
CUHKSZzxy:zxy/support_deepseek_vl2
Feb 24, 2025
Merged

[Feature] support deepseek-vl2 for pytorch engine#3149
lvhan028 merged 20 commits into
InternLM:mainfrom
CUHKSZzxy:zxy/support_deepseek_vl2

Conversation

@CUHKSZzxy

@CUHKSZzxy CUHKSZzxy commented Feb 17, 2025

Copy link
Copy Markdown
Collaborator

Motivation

Issue 3042 support DeepSeek-VL2 for the PyTorch engine.

Modification

  1. Add chat template, builders...
  2. Add vision model preprocessing, model forward related file etc.
  3. Modify several configuration check conditions, since deepseek-vl2 has different huggingface configuration formats, which leads to errors in the original implementations.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

Single image inference

import os
from lmdeploy import pipeline, PytorchEngineConfig, GenerationConfig
from lmdeploy.vl import load_image

os.environ['CUDA_VISIBLE_DEVICES'] = '0'


model_path = '/mnt/141/deepseek-vl2'

pt_backend_config = PytorchEngineConfig(
                    tp=1,
                    max_batch_size=32,
                    cache_max_entry_count=0.8,
                    session_len=8192,
                )

gen_config = GenerationConfig(
                    max_new_tokens=512,
                    do_sample=False,
                )


def main():
    pipe = pipeline(
                    model_path,
                    backend_config=pt_backend_config,
                    )

    image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')

    response = pipe(('describe this image.', image), gen_config=gen_config)
    print(response)

if __name__ == '__main__':
    main()

Expected outputs:
expected_outputs

Multi-round conversations

import os
from lmdeploy import pipeline, PytorchEngineConfig, GenerationConfig
from lmdeploy.vl import load_image

os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'


model_path = '/mnt/141/deepseek-vl2'

pt_backend_config = PytorchEngineConfig(
                    tp=2,
                    max_batch_size=32,
                    cache_max_entry_count=0.8,
                    session_len=8192,
                )

gen_config = GenerationConfig(
                    max_new_tokens=512,
                    do_sample=False,
                )


def main():
    pipe = pipeline(
                    model_path,
                    backend_config=pt_backend_config,
                    )

    image_woman = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/demo/resources/human-pose.jpg')
    image_tiger = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')

    sess = pipe.chat(('<IMAGE_TOKEN>Describe this image', image_woman))
    print(sess.response.text)
    print('-'*50)
    sess = pipe.chat('What is the woman doing?', session=sess)
    print(sess.response.text)
    print('-'*50)
    sess = pipe.chat(('<IMAGE_TOKEN>Can you describe this image as well?', image_tiger), session=sess)
    print(sess.response.text)
    print('-'*50)
    sess = pipe.chat(('Tell me a story using those images.'), session=sess)
    print(sess.response.text)
    print('-'*50)


if __name__ == '__main__':
    main()

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness.
  3. If the modification has a dependency on downstream projects of a newer version, this PR should be tested with all supported versions of downstream projects.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@lvhan028
lvhan028 requested a review from RunningLeon February 17, 2025 13:13
@lvhan028 lvhan028 added the enhancement New feature or request label Feb 17, 2025
@lvhan028
lvhan028 self-requested a review February 17, 2025 13:14
@lvhan028

Copy link
Copy Markdown
Collaborator

may update supported models in RADME and supported_models.md

Comment thread lmdeploy/vl/model/deepseek_vl2.py Outdated
Comment thread lmdeploy/vl/model/builder.py Outdated
Comment thread lmdeploy/vl/model/deepseek_vl2_processors.py Outdated
Comment thread lmdeploy/model.py
Comment thread lmdeploy/pytorch/configurations/cogvlm.py Outdated
Comment thread lmdeploy/archs.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2_config.py Outdated
Comment thread lmdeploy/vl/model/glm_4v.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py
Comment thread lmdeploy/pytorch/configurations/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/configurations/deepseek_vl2.py
Comment thread docs/zh_cn/supported_models/supported_models.md
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread docs/en/multi_modal/deepseek_vl2.md
Comment thread docs/en/multi_modal/deepseek_vl2.md
Comment thread docs/en/multi_modal/deepseek_vl2.md Outdated
Comment thread docs/en/multi_modal/deepseek_vl2.md Outdated
Comment thread docs/en/multi_modal/deepseek_vl2.md Outdated
except ImportError:
raise ImportError('Please install timm') from ImportError

model = timm.create_model(

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.

instead of using timm, we may need to add vision transformers using kernels of lmdeploy just like

class EVA2CLIPAttention(nn.Module):

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 tried to implement the VisionTransformer part using the upstream repository code, replacing the operators inside with LMDeploy kernels. While upstream code depends on the xformers memory_efficient_attention here, which I replaced with torch scaled_dot_product_attention but yields different tensor outputs.

Therefore, I refer to vllm deepseek-vl2 vision part for simplicity.

I may spare more time and try to accomplish previous attempts to reuse LMDeploy kernels instead of timm.

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.

using pure timm without xformers is show and if it can be repalced with sdpa, it can be replaced by kernels in lmdeploy as well.

@lvhan028 lvhan028 Feb 24, 2025

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.

Let's make another PR to replace timm.

Comment thread docs/en/multi_modal/deepseek_vl2.md Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated
Comment thread lmdeploy/pytorch/models/deepseek_vl2.py Outdated

@RunningLeon RunningLeon left a comment

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.

LGTM

@lvhan028
lvhan028 merged commit d97dc76 into InternLM:main Feb 24, 2025
tastelikefeet added a commit to tastelikefeet/lmdeploy that referenced this pull request Feb 25, 2025
…oad_state_dict

* commit 'f6f7a5d707e3ccbc69af10babf1c9afcaf72a402':
  fix deepseekv2 has no attribute use_mla error (InternLM#3188)
  fix blocked fp8 moe (InternLM#3181)
  [Feature] support deepseek-vl2 for pytorch engine (InternLM#3149)
  make turbomind support gpu embedding inputs (InternLM#3177)
  fix temperature=0 (InternLM#3176)
  Update qwen2.py (InternLM#3174)
  Fix tool call prompt for InternLM and Qwen (InternLM#3156)
  Use pad_token_id as image_token_id for vl models (InternLM#3158)
  fix default temperature value (InternLM#3166)
  fix min length penalty (InternLM#3150)
  update cuda runtime package dependencies (InternLM#3142)
  fix typing (InternLM#3153)
  support deepseekv2 for maca backend. (InternLM#2918)
  fix the issue that stop_token may be less than defined in model.py (InternLM#3148)
  [fix] fix vl gradio, use pipeline api and remove interactive chat (InternLM#3136)
  [feature] add dlinfer w8a8 support. (InternLM#2988)
  Use aiohttp inside proxy server && add --disable-cache-status argument (InternLM#3020)
  support eos_token list in turbomind (InternLM#3044)
@CUHKSZzxy
CUHKSZzxy deleted the zxy/support_deepseek_vl2 branch April 24, 2025 03:39
oliveagle pushed a commit to oliveagle/lmdeploy that referenced this pull request May 22, 2026
* support deepseek-vl2 for pytorch engine

* cleanup the codes

* fix, update doc and UT

* fix config check rule

* resolve review suggestions, fix bugs

* fix typo

* fix bug, update doc

* update doc, add trans version check

* update doc

* update doc

* cleanup

* supress tokenizer parallelism warnings

* update

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants