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

Qwen2vl add fused rope #1638

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/image-to-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Models that have been validated:
- [meta-llama/Llama-3.2-90B-Vision-Instruct](https://huggingface.co/meta-llama/Llama-3.2-90B-Vision-Instruct)
- [tiiuae/falcon-11B-vlm](https://huggingface.co/tiiuae/falcon-11B-vlm)
- [google/paligemma-3b-mix-224](https://huggingface.co/google/paligemma-3b-mix-224)
- [Qwen/Qwen2-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct)

### Inference with BF16

Expand Down Expand Up @@ -122,6 +123,26 @@ python3 run_pipeline.py \
--sdp_on_bf16
```

To run Qwen/Qwen2-VL-2B-Instruct inference, use the following command:

```bash
python3 run_pipeline.py \
--model_name_or_path Qwen/Qwen2-VL-2B-Instruct \
--use_hpu_graphs \
--bf16 \
--use_flash_attention
```

To run Qwen/Qwen2-VL-7B-Instruct inference, use the following command:

```bash
python3 run_pipeline.py \
--model_name_or_path Qwen/Qwen2-VL-7B-Instruct \
--use_hpu_graphs \
--bf16 \
--use_flash_attention
```

### Inference with FP8
Inference for Llava-1.5-7b, Llava-1.5-13b, Llava-v1.6-mistral-7b and Llava-v1.6-vicuna-13b in FP8 precision are enabled using [Intel Neural Compressor (INC)](https://docs.habana.ai/en/latest/PyTorch/Inference_on_PyTorch/Inference_Using_FP8.html), which provides model measurement and quantization capabilities in PyTorch.

Expand Down
15 changes: 12 additions & 3 deletions examples/image-to-text/run_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def main():

config = AutoConfig.from_pretrained(args.model_name_or_path)
model_type = config.model_type
if args.image_path is None and model_type in ["llava", "idefics2", "mllama"]:

if args.image_path is None and model_type in ["llava", "idefics2", "mllama", "qwen2_vl"]:
args.image_path = ["https://llava-vl.github.io/static/images/view.jpg"]
elif args.image_path is None and model_type == "paligemma":
args.image_path = [
Expand All @@ -210,7 +211,7 @@ def main():
"https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true"
]

if model_type in ["llava", "idefics2", "llava_next", "mllama", "paligemma"]:
if model_type in ["llava", "idefics2", "llava_next", "mllama", "paligemma", "qwen2_vl"]:
processor = AutoProcessor.from_pretrained(args.model_name_or_path)
if args.prompt is None:
if processor.chat_template is not None:
Expand Down Expand Up @@ -289,6 +290,9 @@ def main():
generator = pipeline(
"image-to-text",
model=args.model_name_or_path,
config=args.model_name_or_path,
tokenizer=args.model_name_or_path,
image_processor=args.model_name_or_path,
torch_dtype=model_dtype,
device="hpu",
)
Expand Down Expand Up @@ -316,12 +320,17 @@ def main():
if args.use_kv_cache:
generate_kwargs["use_cache"] = args.use_kv_cache

if model_type == "qwen2_vl":
generate_kwargs["use_cache"] = True
generate_kwargs["cache_implementation"] = "static"
generate_kwargs["static_shapes"] = True

if args.quant_config:
generator.model = setup_quantization(generator.model, args)
htcore.hpu_initialize(generator.model)

# delete once pipeline integrate AutoProcessor as preprocess engine
if model_type in ["idefics2", "mllama", "paligemma"]:
if model_type in ["idefics2", "mllama", "paligemma", "qwen2_vl"]:
from transformers.image_utils import load_image

def preprocess(self, image, prompt=None, timeout=None):
Expand Down
20 changes: 20 additions & 0 deletions optimum/habana/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@
GaudiQwen2MoeForCausalLM,
GaudiQwen2MoeMLP,
GaudiQwen2MoeModel,
GaudiQwen2VisionSdpaAttention,
GaudiQwen2VisionTransformerPretrainedModel,
GaudiQwen2VLDecoderLayer,
GaudiQwen2VLForConditionalGeneration,
GaudiQwen2VLModel,
GaudiQwen2VLSdpaAttention,
GaudiQwen2VLVisionBlock,
GaudiStableLmAttention,
GaudiStableLmDecoderLayer,
GaudiStableLmForCausalLM,
Expand Down Expand Up @@ -644,6 +651,19 @@ def adapt_transformers_to_gaudi():
gaudi_qwen2moe_block_sparse_moe_forward
)

# Optimization for qwen2-vl Gaudi
transformers.models.qwen2_vl.modeling_qwen2_vl.VisionSdpaAttention = GaudiQwen2VisionSdpaAttention
transformers.models.qwen2_vl.modeling_qwen2_vl.Qwen2VLVisionBlock = GaudiQwen2VLVisionBlock
transformers.models.qwen2_vl.modeling_qwen2_vl.Qwen2VisionTransformerPretrainedModel = (
GaudiQwen2VisionTransformerPretrainedModel
)
transformers.models.qwen2_vl.modeling_qwen2_vl.Qwen2VLSdpaAttention = GaudiQwen2VLSdpaAttention
transformers.models.qwen2_vl.modeling_qwen2_vl.Qwen2VLDecoderLayer = GaudiQwen2VLDecoderLayer
transformers.models.qwen2_vl.modeling_qwen2_vl.Qwen2VLModel = GaudiQwen2VLModel
transformers.models.qwen2_vl.modeling_qwen2_vl.Qwen2VLForConditionalGeneration = (
GaudiQwen2VLForConditionalGeneration
)

# Optimization for stablelm on Gaudi
transformers.models.stablelm.modeling_stablelm.StableLmAttention = GaudiStableLmAttention
transformers.models.stablelm.modeling_stablelm.StableLmDecoderLayer = GaudiStableLmDecoderLayer
Expand Down
9 changes: 9 additions & 0 deletions optimum/habana/transformers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@
gaudi_qwen2moe_block_sparse_moe_forward,
gaudi_qwen2moe_rmsnorm_forward,
)
from .qwen2_vl import (
GaudiQwen2VisionSdpaAttention,
GaudiQwen2VisionTransformerPretrainedModel,
GaudiQwen2VLDecoderLayer,
GaudiQwen2VLForConditionalGeneration,
GaudiQwen2VLModel,
GaudiQwen2VLSdpaAttention,
GaudiQwen2VLVisionBlock,
)
from .seamless_m4t import (
gaudi_SeamlessM4TAttention_forward,
gaudi_SeamlessM4TCodeHifiGan_get_output_hifigan_lengths,
Expand Down
9 changes: 9 additions & 0 deletions optimum/habana/transformers/models/qwen2_vl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .modeling_qwen2_vl import (
GaudiQwen2VisionSdpaAttention,
GaudiQwen2VisionTransformerPretrainedModel,
GaudiQwen2VLDecoderLayer,
GaudiQwen2VLForConditionalGeneration,
GaudiQwen2VLModel,
GaudiQwen2VLSdpaAttention,
GaudiQwen2VLVisionBlock,
)
Loading