MCPcopy Index your code
hub / github.com/Meituan-AutoML/MobileVLM

github.com/Meituan-AutoML/MobileVLM @v0.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2 ↗ · + Follow
143 symbols 536 edges 16 files 19 documented · 13%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

MobileVLM: Vision Language Model for Mobile Devices

[![hf_space](https://img.shields.io/badge/🤗-MTGV%20HuggingFace-blue.svg)](https://huggingface.co/mtgv) [![Code License](https://img.shields.io/badge/Code%20License-Apache_2.0-green.svg)](https://github.com/tatsu-lab/stanford_alpaca/blob/main/LICENSE) [![github](https://img.shields.io/badge/-Github-black?logo=github)](https://github.com/Meituan-AutoML/MobileVLM.git)[![github](https://img.shields.io/github/stars/Meituan-AutoML/MobileVLM.svg?style=social)](https://github.com/Meituan-AutoML/MobileVLM.git)
  • MobileVLM V2: Faster and Stronger Baseline for Vision Language Model

arXiv BibTex

📌 Take a quick look at our MobileVLM V2 architecture

We introduce MobileVLM V2, a family of significantly improved vision language models upon MobileVLM, which proves that a delicate orchestration of novel architectural design, an improved training scheme tailored for mobile VLMs, and rich high-quality dataset curation can substantially benefit VLMs’ performance. Specifically, MobileVLM V2 1.7B achieves better or on-par performance on standard VLM benchmarks compared with much larger VLMs at the 3B scale. Notably, our 3B model outperforms a large variety of VLMs at the 7B+ scale.

MobileVLM V2’s architecture. Xv and Xq indicate image and language instruction, respectively, and Ya refers to the text response from the language model MobileLLaMA. The diagram in the lower right corner is a detailed description of LDPv2, i.e., the lightweight downsample projector v2.

  • MobileVLM: A Fast, Strong and Open Vision Language Assistant for Mobile Devices

arXiv BibTex

📌 Take a quick look at our MobileVLM architecture

We present MobileVLM, a competent multimodal vision language model (MMVLM) targeted to run on mobile devices. It is an amalgamation of a myriad of architectural designs and techniques that are mobile-oriented, which comprises a set of language models at the scale of 1.4B and 2.7B parameters, trained from scratch, a multimodal vision model that is pre-trained in the CLIP fashion, cross-modality interaction via an efficient projector. We evaluate MobileVLM on several typical VLM benchmarks. Our models demonstrate on par performance compared with a few much larger models. More importantly, we measure the inference speed on both a Qualcomm Snapdragon 888 CPU and an NVIDIA Jeston Orin GPU, and we obtain state-of-the-art performance of 21.5 tokens and 65.3 tokens per second, respectively.

The MobileVLM architecture (right) utilizes MobileLLaMA as its language model, intakes Xv and Xq which are image and language instructions as respective inputs and gives Ya as the output language response. LDP refers to a lightweight downsample projector.

📸 Release

  • ⏳ MobileLLaMA Pre-training code.
  • ⏳ MobileVLM V2 training data and code are being sorted out.
  • Feb. 06th, 2024: 🔥🔥🔥 MobileVLM V2 is out! Paper here! The evaluation code of MobileVLM V2 is available now! Our MobileVLM V2 weights are publicly avaliable on the HuggingFace website. Enjoy them !
  • Feb. 06th, 2024: The SFT code and dataset of MobileLLaMA are released now! You can train your own chat model.
  • Jan. 23rd, 2024: 🚀🚀🚀 MobileVLM is officially supported by llama.cpp now ! Have a try !
  • Jan. 15th, 2024: Customized llama.cpp for MobileVLM and its deployment instruction on mobile devices.
  • Jan. 11st, 2024: The training and evaluation codes of MobileVLM are available now! Follow these step-by-step instructions below to easily train your own mobileVLM in 5 hours ⚡️ !
  • Dec. 31st, 2023: Our MobileVLM weights are uploaded on the HuggingFace website. We also provide inference examples for the MobileLLaMA/MobileVLM model so that anyone can enjoy them early.
  • Dec. 29th, 2023: Our MobileLLaMA weights are uploaded on the HuggingFace website. Enjoy them !
  • Dec. 28th, 2023: 🔥🔥🔥 We release MobileVLM: A Fast, Strong and Open Vision Language Assistant for Mobile Devices on arxiv. Refer to our paper for more details !

🦙 Model Zoo

MobileVLM Family

Model LLM GQA SQAI VQAT POPE MMEP MMBdev Avg.

MobileVLM-1.7B |

MobileLLaMA 1.4B | 56.1 | 57.3 | 41.5 | 84.5 | 1196.2 | 53.2 | 58.7 | | MobileVLM V2 1.7B | MobileLLaMA 1.4B | 59.3 | 66.7 | 52.1 | 84.3 | 1302.8 | 57.7 | 64.2 | | MobileVLM-3B | MobileLLaMA 2.7B | 59.0 | 61.2 | 47.5 | 84.9 | 1288.9 | 59.6 | 62.8 | | MobileVLM V2 3B | MobileLLaMA 2.7B | 61.1 | 70.0 | 57.5 | 84.7 | 1440.5 | 63.2 | 68.1 | | MobileVLM V2 7B | Vicuna-7B | 62.6 | 74.8 | 62.3 | 85.3 | 1560.7 | 69.2 | 72.1 |

MobileLLaMA Family

🔔 Usage and License Notices: This project utilizes certain datasets and checkpoints that are subject to their respective original licenses. Users must comply with all terms and conditions of these original licenses. This project is licensed permissively under the Apache 2.0 license and does not impose any additional constraints. LLaVA

🛠️ Install

  1. Clone this repository and navigate to MobileVLM folder bash git clone https://github.com/Meituan-AutoML/MobileVLM.git cd MobileVLM

  2. Install Package Shell conda create -n mobilevlm python=3.10 -y conda activate mobilevlm pip install --upgrade pip pip install -r requirements.txt

🗝️ Quick Start

Example for MobileLLaMA model inference

import torch
from transformers import LlamaTokenizer, LlamaForCausalLM

model_path = 'mtgv/MobileLLaMA-1.4B-Chat'

tokenizer = LlamaTokenizer.from_pretrained(model_path)
model = LlamaForCausalLM.from_pretrained(
    model_path, torch_dtype=torch.float16, device_map='auto',
)

prompt = 'Q: What is the largest animal?\nA:'
input_ids = tokenizer(prompt, return_tensors="pt").input_ids

generation_output = model.generate(
    input_ids=input_ids, max_new_tokens=32
)
print(tokenizer.decode(generation_output[0]))

Example for MobileVLM model inference

from scripts.inference import inference_once

model_path = "mtgv/MobileVLM-1.7B"
image_file = "assets/samples/demo.jpg"
prompt_str = "Who is the author of this book?\nAnswer the question using a single word or phrase."
# (or) What is the title of this book?
# (or) Is this book related to Education & Teaching?

args = type('Args', (), {
    "model_path": model_path,
    "image_file": image_file,
    "prompt": prompt_str,
    "conv_mode": "v1",
    "temperature": 0, 
    "top_p": None,
    "num_beams": 1,
    "max_new_tokens": 512,
    "load_8bit": False,
    "load_4bit": False,
})()

inference_once(args)

🪜 Step-by-step Tutorial

MobileVLM V2

🏃 Training code and user guidelines are coming soon.

MobileLLaMA

The SFT(supervised fine-tuning) process of MobileLLaMA: - please refer to MobileLLaMA_SFT.md for the env, dataset and training code of our MobileLLaMA SFT. - this training process takes around 3~5 hours for MobileLLaMA 1.4B/2.7B on 8x A100 (80G)

Note: You may skip MobileLLaMA training processes and directly start with MobileVLM, leveraging our pre-trained MobileLLaMA model from huggingface website (🤗 1.7B, 2.7B). .

MobileVLM

The training process of MobileVLM is divided into two stages:

  • stage I: feature alignment pretrain
  • ❄️ frozen vision encoder + 🔥 learnable LDP projector + ❄️ frozen LLM
  • this training process takes around 1~1.5 hours for MobileVLM-1.7B/3B on 8x A100 (80G) with a batch size of 256 and an average of approximately 15G/19G of GPU memory required.
  • stage II: visual instruction tuning
  • ❄️ frozen vision encoder + 🔥 learnable LDP projector + 🔥 learnable LLM
  • this training process takes around 2~3.5 hours for MobileVLM-1.7B/3B on 8x A100 (80G) with a batch size of 128 and an average of approximately 46G/52G of GPU memory required.

Note: To train on fewer GPU memory or cards, you can reduce the per_device_train_batch_size and increase the gradient_accumulation_steps accordingly. Always keep the global batch size the same: per_device_train_batch_size x gradient_accumulation_steps x num_gpus.

1️⃣ Prepare MobileLLaMA checkpoints

Download MobileLLaMA chatbot checkpoints from huggingface website (🤗 1.7B, 2.7B). Please note that this is optional (it depends on your working environment), run the training script we provide below and the model will be automatically downloaded by the transformers library.

2️⃣ Prepare data

  • For convenience, assume your working directory /path/to/project/mobilevlm as work_dir:
  • cd ${work_dir} && mkdir -p data/pretrain_data data/finetune_data data/benchmark_data
  • prepare alignment pre-training data
  • cd ${work_dir}/data/pretrain_data
  • download the LLaVA-558K from here, which is provided by LLaVA team.
  • prepare instruction tuning data
  • cd ${work_dir}/data/finetune_data
  • download the annotation of the LLaVA mixture instruction tuning data here, and download the images from constituting datasets: COCO, GQA, OCR-VQA, TextVQA, VisualGnome (Part1, Part2)
  • prepare benchmark data
  • We evaluate models on a diverse set of 6 benchmarks, i.e. GQA, MMBench, MME, POPE, SQA, TextVQA. We do not evaluate using beam search to make the inference process consistent with the chat demo of real-time outputs. You should follow these instructions to manage the datasets.

Data Download Instructions

- download some useful [data/scripts](https://github.com/Meituan-AutoML/MobileVLM/releases/download/v0.1/benchmark_data.zip) pre-collected by us.
  - `unzip benchmark_data.zip && cd benchmark_data`
  - `bmk_dir=${work_dir}/data/benchmark_data`
- gqa
  - download its image data following the official instructions [here](https://cs.stanford.edu/people/dorarad/gqa/download.html)
  - `cd ${bmk_dir}/gqa && ln -s /path/to/gqa/images images`
- mme
  - download the data following the official instructions [here](https://github.com/BradyFU/Awesome-Multimodal-Large-Language-Models/tree/Evaluation).
  - `cd ${bmk_dir}/mme && ln -s /path/to/MME/MME_Benchmark_release_version images`
- pope
  - download coco from POPE following the official instructions [here](https://github.com/AoiDragon/POPE/tree/e3e39262c85a6a83f26cf5094022a782cb0df58d/output/coco).
  - `cd ${bmk_dir}/pope && ln -s /path/to/pope/coco coco && ln -s /path/to/coco/val2014 val2014`
-

Core symbols most depended-on inside this repo

tokenizer_image_token
called by 19
mobilevlm/utils.py
get_model
called by 15
mobilevlm/model/mobilevlm.py
append_message
called by 11
mobilevlm/conversation.py
copy
called by 9
mobilevlm/conversation.py
get_prompt
called by 8
mobilevlm/conversation.py
get_vision_tower
called by 5
mobilevlm/model/mobilevlm.py
disable_torch_init
called by 4
mobilevlm/utils.py
load_pretrained_model
called by 4
mobilevlm/model/mobilevlm.py

Shape

Method 65
Function 54
Class 24

Languages

Python100%

Modules by API surface

mobilevlm/train/train.py33 symbols
mobilevlm/model/vision_projector.py19 symbols
mobilevlm/utils.py18 symbols
mobilevlm/model/vision_encoder.py13 symbols
mobilevlm/model/mobilevlm.py12 symbols
mobilevlm/train/trainer.py10 symbols
mobilevlm/conversation.py9 symbols
mobilevlm/model/mobilellama.py8 symbols
mobilevlm/eval/model_vqa_loader.py8 symbols
mobilevlm/eval/model_vqa_mmbench.py5 symbols
mobilevlm/train/llama_flash_attn.py3 symbols
mobilevlm/eval/model_vqa_science.py3 symbols

For agents

$ claude mcp add MobileVLM \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact