HongyuanTao commited on
Commit
1bde94d
·
verified ·
1 Parent(s): 5032dc0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +139 -1
README.md CHANGED
@@ -1,6 +1,8 @@
1
  ---
2
  license: mit
3
  ---
 
 
4
  ## Introduction
5
  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.
6
 
@@ -12,4 +14,140 @@ Distilled from the decoder-only HoVLE-2.6B, our pure Mamba-2-based mmMamba-linea
12
 
13
  <b>Seeding strategy and three-stage distillation pipeline of mmMamba.</b>
14
  <img src="assets/pipeline.png" />
15
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
14
 
15
  <b>Seeding strategy and three-stage distillation pipeline of mmMamba.</b>
16
  <img src="assets/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
+
56
+ IMAGENET_MEAN = (0.485, 0.456, 0.406)
57
+ IMAGENET_STD = (0.229, 0.224, 0.225)
58
+
59
+ def build_transform(input_size):
60
+ MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
61
+ transform = T.Compose([
62
+ T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
63
+ T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
64
+ T.ToTensor(),
65
+ T.Normalize(mean=MEAN, std=STD)
66
+ ])
67
+ return transform
68
+
69
+ def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
70
+ best_ratio_diff = float('inf')
71
+ best_ratio = (1, 1)
72
+ area = width * height
73
+ for ratio in target_ratios:
74
+ target_aspect_ratio = ratio[0] / ratio[1]
75
+ ratio_diff = abs(aspect_ratio - target_aspect_ratio)
76
+ if ratio_diff < best_ratio_diff:
77
+ best_ratio_diff = ratio_diff
78
+ best_ratio = ratio
79
+ elif ratio_diff == best_ratio_diff:
80
+ if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
81
+ best_ratio = ratio
82
+ return best_ratio
83
+
84
+ def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
85
+ orig_width, orig_height = image.size
86
+ aspect_ratio = orig_width / orig_height
87
+
88
+ # calculate the existing image aspect ratio
89
+ target_ratios = set(
90
+ (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
91
+ i * j <= max_num and i * j >= min_num)
92
+ target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
93
+
94
+ # find the closest aspect ratio to the target
95
+ target_aspect_ratio = find_closest_aspect_ratio(
96
+ aspect_ratio, target_ratios, orig_width, orig_height, image_size)
97
+
98
+ # calculate the target width and height
99
+ target_width = image_size * target_aspect_ratio[0]
100
+ target_height = image_size * target_aspect_ratio[1]
101
+ blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
102
+
103
+ # resize the image
104
+ resized_img = image.resize((target_width, target_height))
105
+ processed_images = []
106
+ for i in range(blocks):
107
+ box = (
108
+ (i % (target_width // image_size)) * image_size,
109
+ (i // (target_width // image_size)) * image_size,
110
+ ((i % (target_width // image_size)) + 1) * image_size,
111
+ ((i // (target_width // image_size)) + 1) * image_size
112
+ )
113
+ # split the image
114
+ split_img = resized_img.crop(box)
115
+ processed_images.append(split_img)
116
+ assert len(processed_images) == blocks
117
+ if use_thumbnail and len(processed_images) != 1:
118
+ thumbnail_img = image.resize((image_size, image_size))
119
+ processed_images.append(thumbnail_img)
120
+ return processed_images
121
+
122
+ def load_image(image_file, input_size=448, max_num=12):
123
+ image = Image.open(image_file).convert('RGB')
124
+ transform = build_transform(input_size=input_size)
125
+ images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
126
+ pixel_values = [transform(image) for image in images]
127
+ pixel_values = torch.stack(pixel_values)
128
+ return pixel_values
129
+
130
+
131
+ path = 'hustvl/mmMamba-linear'
132
+ model = AutoModel.from_pretrained(
133
+ path,
134
+ torch_dtype=torch.bfloat16,
135
+ low_cpu_mem_usage=False,
136
+ trust_remote_code=True).eval().cuda()
137
+ tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
138
+
139
+ # set the max number of tiles in `max_num`
140
+ pixel_values = load_image('/path/to/image', max_num=12).to(torch.bfloat16).cuda()
141
+ generation_config = dict(max_new_tokens=1024, do_sample=True)
142
+
143
+ # pure-text conversation (纯文本对话)
144
+ question = 'Hello, who are you?'
145
+ response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
146
+ print(f'User: {question}\nAssistant: {response}')
147
+
148
+
149
+ # single-image single-round conversation (图文对话)
150
+ question = '<image>\nPlease describe the image shortly.'
151
+ response = model.chat(tokenizer, pixel_values, question, generation_config)
152
+ print(f'User: {question}\nAssistant: {response}')
153
+ ```