Add support for LFM2-VL (lfm2_vl) model for image-text-to-text task#1695
Closed
openvino-agent wants to merge 1 commit intohuggingface:mainfrom
Closed
Add support for LFM2-VL (lfm2_vl) model for image-text-to-text task#1695openvino-agent wants to merge 1 commit intohuggingface:mainfrom
openvino-agent wants to merge 1 commit intohuggingface:mainfrom
Conversation
- Add Lfm2VLOpenVINOConfig and DummyLfm2VlVisionInputGenerator in model_configs.py - Add Lfm2VlImageEmbeddingsModelPatcher and Lfm2VlLMModelPatcher in model_patcher.py with dynamic-shape-compatible positional embedding resize using grid_sample - Add lfm2_vl to MULTI_MODAL_TEXT_GENERATION_MODELS in utils.py - Add _OVLfm2VLCache, _OVLfm2VLLanguageModel, and _OVLfm2VLForCausalLM in modeling_visual_language.py for non-stateful Mamba-style cache handling - Update docs to include LFM2-VL model - Add test entries for lfm2_vl in test_export.py, test_quantization.py, utils_tests.py
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds OpenVINO export and inference support for the LFM2-VL vision-language model family (
model_type=lfm2_vl) for theimage-text-to-texttask.Model Architecture
LFM2-VL (
LiquidAI/LFM2-VL-450M) is a vision-language model with:(batch, N_patches, 768)+spatial_shapesmetadataChanges
Export (
model_configs.py,model_patcher.py,utils.py)DummyLfm2VlVisionInputGenerator— generates flat NaFlex-format dummy vision inputs (16×16 = 256 patches)Lfm2VLOpenVINOConfig— registered forlfm2_vl/image-text-to-text, exports 3 sub-models: vision embeddings, language model, text embeddingsLfm2VlImageEmbeddingsModelPatcher:Siglip2VisionEmbeddings.resize_positional_embeddingsto usetorch.linspace + F.grid_sampleinstead ofF.interpolate— making it compatible with dynamic spatial shapes in OpenVINOpixel_valuesto valid patches before vision tower (pixel_values[:, :h*w, :])Lfm2VlLMModelPatcher— standard LM export patcherlfm2_vltoMULTI_MODAL_TEXT_GENERATION_MODELSInference (
modeling_visual_language.py)Since LFM2-VL uses a non-stateful Mamba-style cache (explicit conv states + KV tensors as model I/O), the standard
OVModelWithEmbedForCausalLMclass (designed for stateful transformer LMs) cannot be used. Instead:_OVLfm2VLCache— simple dataclass holding conv_states, key_cache, value_cache numpy arrays_OVLfm2VLLanguageModel— custom LM wrapper that:InferRequestAPI for OV inferencepast_key_values=None/non-Nonesentinel interface compatible with the VLM generation loopembed_tokens,clear_requests,compile,tomethods_OVLfm2VLForCausalLM— full VLM class with:forward— passesspatial_shapesto vision processingprepare_inputs_for_generation— passesspatial_shapesin generation loopget_vision_embeddings/merge_vision_text_embeddings/preprocess_inputs"lfm2_vl": _OVLfm2VLForCausalLMinMODEL_TYPE_TO_CLS_MAPPINGTests and Docs
lfm2_vlentries totest_export.py,test_quantization.py,utils_tests.pydocs/source/openvino/models.mdxwith LFM2-VL entryValidation
Export:
optimum-cli export openvino --model LiquidAI/LFM2-VL-450M --task image-text-to-text output_dir✅ Successfully exports 3 sub-models with dynamic shapes.
Inference:
✅ End-to-end generation produces correct output.