mjbuehler commited on
Commit
f5e7ae9
·
verified ·
1 Parent(s): 5a36fca

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +155 -130
README.md CHANGED
@@ -1,199 +1,224 @@
1
  ---
 
 
 
2
  library_name: transformers
3
- tags: []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  ---
 
5
 
6
- # Model Card for Model ID
7
 
8
- <!-- Provide a quick summary of what the model is/does. -->
9
 
 
10
 
 
11
 
12
- ## Model Details
13
 
14
- ### Model Description
15
 
16
- <!-- Provide a longer summary of what this model is. -->
17
 
18
- This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
 
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
 
28
- ### Model Sources [optional]
 
 
 
29
 
30
- <!-- Provide the basic links for the model. -->
31
 
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
 
36
- ## Uses
 
 
 
 
 
 
37
 
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
 
 
39
 
40
- ### Direct Use
41
 
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
 
44
- [More Information Needed]
 
 
 
 
 
45
 
46
- ### Downstream Use [optional]
 
 
 
 
 
47
 
48
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
 
50
- [More Information Needed]
 
 
 
 
51
 
52
- ### Out-of-Scope Use
53
 
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
 
 
 
 
 
55
 
56
- [More Information Needed]
 
57
 
58
- ## Bias, Risks, and Limitations
 
 
59
 
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
 
61
 
62
- [More Information Needed]
63
 
64
- ### Recommendations
65
 
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
 
68
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
 
 
 
 
69
 
70
- ## How to Get Started with the Model
71
 
72
- Use the code below to get started with the model.
 
 
73
 
74
- [More Information Needed]
75
 
76
- ## Training Details
77
 
78
- ### Training Data
 
 
 
 
79
 
80
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
 
82
- [More Information Needed]
83
 
84
- ### Training Procedure
85
 
86
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
 
88
- #### Preprocessing [optional]
 
 
89
 
90
- [More Information Needed]
91
 
 
92
 
93
- #### Training Hyperparameters
94
 
95
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
 
97
- #### Speeds, Sizes, Times [optional]
 
 
 
 
 
 
 
98
 
99
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
 
101
- [More Information Needed]
 
 
102
 
103
- ## Evaluation
 
 
104
 
105
- <!-- This section describes the evaluation protocols and provides the results. -->
 
106
 
107
- ### Testing Data, Factors & Metrics
 
108
 
109
- #### Testing Data
110
 
111
- <!-- This should link to a Dataset Card if possible. -->
112
 
113
- [More Information Needed]
 
114
 
115
- #### Factors
116
 
117
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
 
119
- [More Information Needed]
120
 
121
- #### Metrics
122
 
123
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
 
125
- [More Information Needed]
126
 
127
- ### Results
128
 
129
- [More Information Needed]
130
 
131
- #### Summary
132
 
 
133
 
 
134
 
135
- ## Model Examination [optional]
136
 
137
- <!-- Relevant interpretability work for the model goes here -->
138
 
139
- [More Information Needed]
140
-
141
- ## Environmental Impact
142
-
143
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
-
145
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
-
147
- - **Hardware Type:** [More Information Needed]
148
- - **Hours used:** [More Information Needed]
149
- - **Cloud Provider:** [More Information Needed]
150
- - **Compute Region:** [More Information Needed]
151
- - **Carbon Emitted:** [More Information Needed]
152
-
153
- ## Technical Specifications [optional]
154
-
155
- ### Model Architecture and Objective
156
-
157
- [More Information Needed]
158
-
159
- ### Compute Infrastructure
160
-
161
- [More Information Needed]
162
-
163
- #### Hardware
164
-
165
- [More Information Needed]
166
-
167
- #### Software
168
-
169
- [More Information Needed]
170
-
171
- ## Citation [optional]
172
-
173
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
-
175
- **BibTeX:**
176
-
177
- [More Information Needed]
178
-
179
- **APA:**
180
-
181
- [More Information Needed]
182
-
183
- ## Glossary [optional]
184
-
185
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
-
187
- [More Information Needed]
188
-
189
- ## More Information [optional]
190
-
191
- [More Information Needed]
192
-
193
- ## Model Card Authors [optional]
194
-
195
- [More Information Needed]
196
-
197
- ## Model Card Contact
198
-
199
- [More Information Needed]
 
1
  ---
2
+ language:
3
+ - multilingual
4
+ license: apache-2.0
5
  library_name: transformers
6
+ datasets:
7
+ - lamm-mit/Cephalo-Bioinspired-Mechanics-Materials
8
+ - lamm-mit/Cephalo-Wikipedia-Materials
9
+ tags:
10
+ - nlp
11
+ - code
12
+ - vision
13
+ - chemistry
14
+ - engineering
15
+ - biology
16
+ - bio-inspired
17
+ - text-generation-inference
18
+ - materials science
19
+ pipeline_tag: image-text-to-text
20
+ inference:
21
+ parameters:
22
+ temperature: 0.3
23
+ widget:
24
+ - messages:
25
+ - role: user
26
+ content: <|image_1|>Can you describe what you see in the image?
27
  ---
28
+ ## Model Summary
29
 
30
+ Cephalo is a series of multimodal materials science focused vision large language models (V-LLMs) designed to integrate visual and linguistic data for advanced understanding and interaction in human-AI or multi-agent AI frameworks.
31
 
32
+ A novel aspect of Cephalo's development is the innovative dataset generation method. The extraction process employs advanced algorithms to accurately detect and separate images and their corresponding textual descriptions from complex PDF documents. It involves extracting images and captions from PDFs to create well-reasoned image-text pairs, utilizing large language models (LLMs) for natural language processing. These image-text pairs are then refined and validated through LLM-based NLP processing, ensuring high-quality and contextually relevant data for training.
33
 
34
+ Cephalo can interpret complex visual scenes and generating contextually accurate language descriptions and answer queries.
35
 
36
+ The model is developed to process diverse inputs, including images and text, facilitating a broad range of applications such as image captioning, visual question answering, and multimodal content generation. The architecture combines a vision encoder model and an autoregressive transformer to process complex natural language understanding.
37
 
38
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/kl5GWBP9WS0D4uwd1t3S7.png)
39
 
40
+ Cephalo provides a robust framework for multimodal interaction and understanding, including the development of complex generative pipelines to create 2D and 3D renderings of material microstructures as input for additive manufacturing methods.
41
 
42
+ This version of Cephalo, lamm-mit/Cephalo-Phi-3-MoE-vision-128k-3x4b-beta, is a Mixture-of-Expert model based on the Phi-3-Vision-128K-Instruct model.
43
 
44
+ ###
45
 
46
+ ```python
47
+ from transformers import AutoModelForCausalLM, AutoProcessor,AutoConfig
 
 
 
 
 
48
 
49
+ def count_parameters(model):
50
+ total_params = sum(p.numel() for p in model.parameters())
51
+ trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
52
+ return total_params/1e9, trainable_params/1e9
53
 
54
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
55
 
56
+ model_name_moe = "lamm-mit/Cephalo-Phi-3-MoE-vision-128k-3x4b-beta"
 
 
57
 
58
+ processor = AutoProcessor.from_pretrained(model_name_moe, trust_remote_code=True)
59
+ moe_model = AutoModelForCausalLM.from_pretrained(
60
+ model_name_moe,
61
+ trust_remote_code=True, torch_dtype=torch.bfloat16,
62
+ ).to(device)
63
+ count_parameters(moe_model)
64
+ ```
65
 
66
+ ## Make a Phi-3-V-MoE model from several pre-trained models
67
+ ```python
68
+ from Phi_3V_MoE.moe_phi3_v import Phi3VForCausalLMMoE, Phi3VForCausalLMMoEConfig
69
 
 
70
 
71
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
72
 
73
+ model_name_1 = f"lamm-mit/Cephalo-Phi-3-vision-128k-4b-beta"
74
+ model_1 = AutoModelForCausalLM.from_pretrained(
75
+ model_name_1,
76
+ trust_remote_code=True, torch_dtype=torch.bfloat16,
77
+
78
+ ).to(device)
79
 
80
+ model_name_2 = f"microsoft/Phi-3-vision-128k-instruct"
81
+ model_2 = AutoModelForCausalLM.from_pretrained(
82
+ model_name_2,
83
+ trust_remote_code=True, torch_dtype=torch.bfloat16,
84
+
85
+ ).to(device)
86
 
87
+ model_name_3 = f"lamm-mit/Cephalo-Phi-3-vision-128k-4b-alpha"
88
 
89
+ model_3 = AutoModelForCausalLM.from_pretrained(
90
+ model_name_3,
91
+ trust_remote_code=True, torch_dtype=torch.bfloat16,
92
+
93
+ ).to(device)
94
 
95
+ dtype = torch.bfloat16 # Desired dtype for new layers
96
 
97
+ # Initialize the models
98
+ base_model = copy.deepcopy(model_2) # Your base model
99
+ expert_models = [model_1, model_2, model_3 ] # List of expert models
100
+
101
+ # Load a processor (e.g. from base model)
102
+ processor = AutoProcessor.from_pretrained(model_name_2, trust_remote_code=True)
103
 
104
+ # Create the config
105
+ config = AutoConfig.from_pretrained(model_name_2, trust_remote_code=True)
106
 
107
+ # Create the MoE model
108
+ moe_config = Phi3VForCausalLMMoEConfig(config=config, k=1, num_expert_models=len (expert_models))
109
+ moe_model = Phi3VForCausalLMMoE(moe_config, base_model, expert_models, layer_dtype = dtype).to(device)
110
 
111
+ count_parameters(expert_models[0]),count_parameters(moe_model)
112
+ ```
113
 
114
+ ####################
115
 
 
116
 
117
+ ### Chat Format
118
 
119
+ Given the nature of the training data, the Cephalo-Phi-3-vision-128k-4b-beta model is best suited for a single image input wih prompts using the chat format as follows.
120
+ You can provide the prompt as a single image with a generic template as follow:
121
+ ```markdown
122
+ <|user|>\n<|image_1|>\n{prompt}<|end|>\n<|assistant|>\n
123
+ ```
124
 
125
+ where the model generates the text after `<|assistant|>` . For multi-turn conversations, the prompt should be formatted as follows:
126
 
127
+ ```markdown
128
+ <|user|>\n<|image_1|>\n{prompt_1}<|end|>\n<|assistant|>\n{response_1}<|end|>\n<|user|>\n{prompt_2}<|end|>\n<|assistant|>\n
129
+ ```
130
 
131
+ ### Sample inference code
132
 
133
+ This code snippets show how to get quickly started on a GPU:
134
 
135
+ ```python
136
+ from PIL import Image
137
+ import requests
138
+ from transformers import AutoModelForCausalLM
139
+ from transformers import AutoProcessor
140
 
141
+ model_id = "lamm-mit/Cephalo-Phi-3-vision-128k-4b-beta"
142
 
143
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda", trust_remote_code=True, torch_dtype="auto")
144
 
145
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
146
 
147
+ question = "What is shown in this image, and what is the relevance for materials design? Include a discussion of multi-agent AI."
148
 
149
+ messages = [
150
+ {"role": "user", "content": f"<|image_1|>\n{question}"},
151
+ ]
152
 
153
+ url = "https://d2r55xnwy6nx47.cloudfront.net/uploads/2018/02/Ants_Lede1300.jpg"
154
 
155
+ image = Image.open(requests.get(url, stream=True).raw)
156
 
157
+ prompt = processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
158
 
159
+ inputs = processor(prompt, [image], return_tensors="pt").to("cuda:0")
160
 
161
+ generation_args = {
162
+ "max_new_tokens": 512,
163
+ "temperature": 0.1,
164
+ "do_sample": True,
165
+ "stop_strings": ['<|end|>',
166
+ '<|endoftext|>'],
167
+ "tokenizer": processor.tokenizer,
168
+ }
169
 
170
+ generate_ids = model.generate(**inputs, eos_token_id=processor.tokenizer.eos_token_id, **generation_args)
171
 
172
+ # remove input tokens
173
+ generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
174
+ response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
175
 
176
+ print(response)
177
+ ```
178
+ Sample output:
179
 
180
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/5n6oRNHrfwHkBX0QertZp.png)
181
+ <small>Image by [Vaishakh Manohar](https://www.quantamagazine.org/the-simple-algorithm-that-ants-use-to-build-bridges-20180226/)</small>
182
 
183
+ <pre style="white-space: pre-wrap;">
184
+ The image shows a group of red ants (Solenopsis invicta) climbing over a vertical wooden post. The ants are using their long legs and antennae to navigate the rough surface of the wood, demonstrating their ability to adapt to different materials and environments. This behavior is relevant for materials design because it highlights the importance of considering the interactions between materials and living organisms, such as ants, when designing new materials.
185
 
186
+ Multi-agent AI (Artificial Intelligence) is a field of study that focuses on the development of AI systems that can work together with other AI systems to achieve a common goal. In the context of this image, multi-agent AI could be used to design materials that are more compatible with the natural behaviors of living organisms, such as ants, and that can adapt to different environments and conditions.
187
 
188
+ By studying the behavior of ants and other living organisms, researchers can gain insights into how materials can be designed to better interact with these organisms and to better mimic their natural behaviors. This can lead to the development of new materials that are more sustainable, efficient, and effective in a variety of applications.
189
 
190
+ In summary, the image of red ants climbing over a wooden post highlights the importance of considering the interactions between materials and living organisms when designing new materials, and the potential of multi-agent AI to help achieve this goal.
191
+ </pre>
192
 
 
193
 
194
+ ## Dataset generation
195
 
196
+ The schematic below shows a visualization of the approach to generate datasets for training the vision model. The extraction process employs advanced algorithms to accurately detect and separate images and their corresponding textual descriptions from complex PDF documents. It involves extracting images and captions from PDFs to create well-reasoned image-text pairs, utilizing large language models (LLMs) for natural language processing. These image-text pairs are then refined and validated through LLM-based NLP processing, ensuring high-quality and contextually relevant data for training.
197
 
198
+ The image below shows reproductions of two representative pages of the scientific article (here, Spivak, Buehler, et al., 2011), and how they are used to extract visual scientific data for training the Cephalo model.
199
 
200
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/qHURSBRWEDgHy4o56escN.png)
201
 
 
202
 
203
+ ## Example applications
204
 
205
+ The paper provides detailed examples and use cases. Here is a visual of a pipeline that consists of 1) analysis of an image provided to Cephalo-Phi-3-vision-128k-4b-beta, 2) generation of an image generation fromt, and 3) generation of a new image using Stable Diffusion XL Turbo.
206
 
207
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/3VvHK_c9eJolQvfOrhiBw.png)
208
 
209
+ A similar mechanism can be employed to generate 3D models:
210
 
211
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/6ZsvCZ3x3TGvugly44MMI.png)
212
 
213
+ ## Citation
214
 
215
+ Please cite as:
216
 
217
+ ```bibtex
218
+ @article{Buehler_Cephalo_2024,
219
+ title={Cephalo: Multi-Modal Vision-Language Models for Bio-Inspired Materials Analysis and Design},
220
+ author={Markus J. Buehler},
221
+ journal={arXiv preprint arXiv:2405.19076},
222
+ year={2024}
223
+ }
224
+ ```