HongyuanTao commited on
Commit
2d75de4
·
verified ·
1 Parent(s): 5118ddd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +138 -3
README.md CHANGED
@@ -1,3 +1,138 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
4
+ # mmMamba-linear Model Card
5
+
6
+ ## Introduction
7
+ We propose mmMamba, the first decoder-only multimodal state space model achieved through quadratic to linear distillation using moderate academic computing resources. Unlike existing linear-complexity encoder-based multimodal large language models (MLLMs), mmMamba eliminates the need for separate vision encoders and underperforming pre-trained RNN-based LLMs. Through our seeding strategy and three-stage progressive distillation recipe, mmMamba effectively transfers knowledge from quadratic-complexity decoder-only pre-trained MLLMs while preserving multimodal capabilities. Additionally, mmMamba introduces flexible hybrid architectures that strategically combine Transformer and Mamba layers, enabling customizable trade-offs between computational efficiency and model performance.
8
+
9
+ Distilled from the decoder-only HoVLE-2.6B, our pure Mamba-2-based mmMamba-linear achieves performance competitive with existing linear and quadratic-complexity VLMs, including those with 2x larger parameter size like EVE-7B. The hybrid variant, mmMamba-hybrid, further enhances performance across all benchmarks, approaching the capabilities of the teacher model HoVLE. In long-context scenarios with 103K tokens, mmMamba-linear demonstrates remarkable efficiency gains with a 20.6× speedup and 75.8% GPU memory reduction compared to HoVLE, while mmMamba-hybrid achieves a 13.5× speedup and 60.2% memory savings.
10
+
11
+ <div align="center">
12
+ <img src="teaser.png" />
13
+
14
+
15
+ <b>Seeding strategy and three-stage distillation pipeline of mmMamba.</b>
16
+ <img src="pipeline.png" />
17
+ </div>
18
+
19
+ ## Quick Start Guide for mmMamba Inference
20
+
21
+ We provide example code to run mmMamba inference using the Transformers library.
22
+
23
+ ### Main Dependencies for Model Inference
24
+
25
+ Below are the primary dependencies required for model inference:
26
+ - torch==2.1.0
27
+ - torchvision==0.16.0
28
+ - torchaudio==2.1.0
29
+ - transformers==4.37.2
30
+ - peft==0.10.0
31
+ - triton==3.2.0
32
+ - [mamba_ssm](https://github.com/state-spaces/mamba/releases/download/v2.2.4/mamba_ssm-2.2.4%2Bcu12torch2.1cxx11abiFALSE-cp310-cp310-linux_x86_64.whl)
33
+ - [causal_conv1d](https://github.com/Dao-AILab/causal-conv1d/releases/download/v1.5.0.post8/causal_conv1d-1.5.0.post8%2Bcu12torch2.1cxx11abiFALSE-cp310-cp310-linux_x86_64.whl)
34
+ - [flash_attn](https://github.com/Dao-AILab/flash-attention/releases/download/v2.6.0/flash_attn-2.6.0%2Bcu122torch2.1cxx11abiFALSE-cp310-cp310-linux_x86_64.whl)
35
+ (Please note that you need to select and download the corresponding .whl file based on your environment.)
36
+ - peft
37
+ - omegaconf
38
+ - rich
39
+ - accelerate
40
+ - sentencepiece
41
+ - decord
42
+ - seaborn
43
+
44
+
45
+ ### Inference with Transformers
46
+
47
+ ```python
48
+ import numpy as np
49
+ import torch
50
+ import torchvision.transforms as T
51
+ from decord import VideoReader, cpu
52
+ from PIL import Image
53
+ from torchvision.transforms.functional import InterpolationMode
54
+ from transformers import AutoModel, AutoTokenizer
55
+ IMAGENET_MEAN = (0.485, 0.456, 0.406)
56
+ IMAGENET_STD = (0.229, 0.224, 0.225)
57
+ def build_transform(input_size):
58
+ MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
59
+ transform = T.Compose([
60
+ T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
61
+ T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
62
+ T.ToTensor(),
63
+ T.Normalize(mean=MEAN, std=STD)
64
+ ])
65
+ return transform
66
+ def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
67
+ best_ratio_diff = float('inf')
68
+ best_ratio = (1, 1)
69
+ area = width * height
70
+ for ratio in target_ratios:
71
+ target_aspect_ratio = ratio[0] / ratio[1]
72
+ ratio_diff = abs(aspect_ratio - target_aspect_ratio)
73
+ if ratio_diff < best_ratio_diff:
74
+ best_ratio_diff = ratio_diff
75
+ best_ratio = ratio
76
+ elif ratio_diff == best_ratio_diff:
77
+ if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
78
+ best_ratio = ratio
79
+ return best_ratio
80
+ def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
81
+ orig_width, orig_height = image.size
82
+ aspect_ratio = orig_width / orig_height
83
+ # calculate the existing image aspect ratio
84
+ target_ratios = set(
85
+ (i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
86
+ i * j <= max_num and i * j >= min_num)
87
+ target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
88
+ # find the closest aspect ratio to the target
89
+ target_aspect_ratio = find_closest_aspect_ratio(
90
+ aspect_ratio, target_ratios, orig_width, orig_height, image_size)
91
+ # calculate the target width and height
92
+ target_width = image_size * target_aspect_ratio[0]
93
+ target_height = image_size * target_aspect_ratio[1]
94
+ blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
95
+ # resize the image
96
+ resized_img = image.resize((target_width, target_height))
97
+ processed_images = []
98
+ for i in range(blocks):
99
+ box = (
100
+ (i % (target_width // image_size)) * image_size,
101
+ (i // (target_width // image_size)) * image_size,
102
+ ((i % (target_width // image_size)) + 1) * image_size,
103
+ ((i // (target_width // image_size)) + 1) * image_size
104
+ )
105
+ # split the image
106
+ split_img = resized_img.crop(box)
107
+ processed_images.append(split_img)
108
+ assert len(processed_images) == blocks
109
+ if use_thumbnail and len(processed_images) != 1:
110
+ thumbnail_img = image.resize((image_size, image_size))
111
+ processed_images.append(thumbnail_img)
112
+ return processed_images
113
+ def load_image(image_file, input_size=448, max_num=12):
114
+ image = Image.open(image_file).convert('RGB')
115
+ transform = build_transform(input_size=input_size)
116
+ images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
117
+ pixel_values = [transform(image) for image in images]
118
+ pixel_values = torch.stack(pixel_values)
119
+ return pixel_values
120
+ path = 'hustvl/mmMamba-hybrid'
121
+ model = AutoModel.from_pretrained(
122
+ path,
123
+ torch_dtype=torch.bfloat16,
124
+ low_cpu_mem_usage=False,
125
+ trust_remote_code=True).eval().cuda()
126
+ tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
127
+ # set the max number of tiles in `max_num`
128
+ pixel_values = load_image('/path/to/image', max_num=12).to(torch.bfloat16).cuda()
129
+ generation_config = dict(max_new_tokens=1024, do_sample=True)
130
+ # pure-text conversation (纯文本对话)
131
+ question = 'Hello, who are you?'
132
+ response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
133
+ print(f'User: {question}\nAssistant: {response}')
134
+ # single-image single-round conversation (图文对话)
135
+ question = '<image>\nPlease describe the image shortly.'
136
+ response = model.chat(tokenizer, pixel_values, question, generation_config)
137
+ print(f'User: {question}\nAssistant: {response}')
138
+ ```