Multimodality support - #2301
Conversation
Adds a vision encoder + multimodal projector pipeline for VLMs: - litgpt/vision.py: VisionEncoder (HF backbone or conv fallback), MultiModalProjector (linear/mlp2x), merge_input_embeds, and ImagePreprocessor. - Config gains optional vision_* fields and an is_multimodal property. - GPT.forward accepts pixel_values and merges projected image patch embeddings into the token embedding sequence at <image> placeholder positions. Same wiring is added to the LoRA/Adapter/AdapterV2 GPT subclasses, which construct their own state rather than calling GPT.__init__. - litgpt.generate.base and litgpt.chat.base thread pixel_values through generation; LLM.generate() accepts an `image` path/PIL input. - convert_hf_checkpoint: load Gemma3 vision tower / mm-projector weights only when the target config explicitly declares a matching vision architecture, to avoid corrupting the state_dict when it doesn't match Gemma3's SigLIP tower shapes.
GPU test results (Modal, A10G)Ran this PR's branch ( Test suite
Total: 1193 passed, 0 failed across the vision, LoRA, Adapter, AdapterV2, base model, chat, and generate suites, confirming the Backward-compatibility checkNone of the existing suites explicitly call config = Config.from_name("pythia-14m") # no vision_* fields
model = GPT(config)
...
out_no_kwarg = model(idx) # old call signature
out_none_kwarg = model(idx, pixel_values=None) # new call signature
assert torch.equal(out_no_kwarg, out_none_kwarg)Result: |
|
@bhimrazy could you provide a first review of this when you have some time to spare? |
closes #2173
Supersedes #2232 (which had gone stale against
mainand was hard to review after 4 months / 14 commits). This is the same feature rebased onto currentmainas a single commit, plus a fix for a bug found while chasing down a CI failure on the old PR: the LoRA/Adapter/AdapterV2GPTsubclasses construct their own state instead of callingGPT.__init__, so they never received the newvision_encoder/mm_projectorattributes — passingpixel_valuesthrough those model types raisedAttributeError.Summary
litgpt/vision.py:VisionEncoder(HF backbone or conv-patch fallback),MultiModalProjector(linear/mlp2x),merge_input_embeds,ImagePreprocessor.Configgains optionalvision_*fields and anis_multimodalproperty.GPT.forwardacceptspixel_valuesand merges projected image-patch embeddings into the token embedding sequence at<image>placeholder positions. Same wiring added to LoRA/Adapter/AdapterV2GPTsubclasses.litgpt.generate.base/litgpt.chat.basethreadpixel_valuesthrough generation;LLM.generate()accepts animagepath/PIL input.convert_hf_checkpoint: only load Gemma3 vision tower / mm-projector weights when the target config explicitly declares a matching vision architecture, to avoid corrupting the state_dict when shapes don't match Gemma3's SigLIP tower.Test plan
tests/test_vision.py(new, 23 tests) coversVisionEncoder,MultiModalProjector,merge_input_embeds,ImagePreprocessor, and GPT integration.tests/test_lora.py,tests/test_adapter.py,tests/test_adapter_v2.py,tests/test_model.pylocally on CPU against latestmain— all pass.ruff checkpasses on all changed files.AI Usage Disclaimer