drlon commited on
Commit
2adceb1
·
1 Parent(s): 2dad144

with to(torch.bfloat16 & logging level

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import traceback
2
- from logging import getLogger
3
  from typing import Optional
4
  import spaces
5
  import gradio as gr
@@ -22,7 +22,10 @@ import torch
22
  from transformers import AutoModelForCausalLM
23
  from transformers import AutoProcessor
24
 
25
- logger = getLogger(__name__)
 
 
 
26
 
27
  # Define repository and local directory
28
  repo_id = "microsoft/OmniParser-v2.0" # HF repo
@@ -36,6 +39,8 @@ magam_model = AutoModelForCausalLM.from_pretrained(magma_model_id, trust_remote_
36
  magma_processor = AutoProcessor.from_pretrained(magma_model_id, trust_remote_code=True)
37
  magam_model.to("cuda")
38
 
 
 
39
  # Download the entire repository
40
  snapshot_download(repo_id=repo_id, local_dir=local_dir)
41
 
@@ -93,11 +98,12 @@ def get_som_response(instruction, image_som):
93
  add_generation_prompt=True
94
  )
95
 
96
- inputs = magma_processor(images=[image_som], texts=prompt, return_tensors="pt")
97
- inputs['pixel_values'] = inputs['pixel_values'].unsqueeze(0).to(torch.bfloat16) # Add .to(torch.bfloat16) here for explicit casting
98
- inputs['image_sizes'] = inputs['image_sizes'].unsqueeze(0)
99
- # inputs = inputs.to("cuda")
100
- inputs = inputs.to("cuda", dtype=torch.bfloat16)
 
101
 
102
  magam_model.generation_config.pad_token_id = magma_processor.tokenizer.pad_token_id
103
  with torch.inference_mode():
@@ -132,11 +138,12 @@ def get_qa_response(instruction, image):
132
  add_generation_prompt=True
133
  )
134
 
135
- inputs = magma_processor(images=[image], texts=prompt, return_tensors="pt")
136
- inputs['pixel_values'] = inputs['pixel_values'].unsqueeze(0).to(torch.bfloat16) # Add .to(torch.bfloat16) here for explicit casting
137
- inputs['image_sizes'] = inputs['image_sizes'].unsqueeze(0)
138
- # inputs = inputs.to("cuda")
139
- inputs = inputs.to("cuda", dtype=torch.bfloat16)
 
140
 
141
  magam_model.generation_config.pad_token_id = magma_processor.tokenizer.pad_token_id
142
  with torch.inference_mode():
 
1
  import traceback
2
+ import logging
3
  from typing import Optional
4
  import spaces
5
  import gradio as gr
 
22
  from transformers import AutoModelForCausalLM
23
  from transformers import AutoProcessor
24
 
25
+ logger = logging.getLogger(__name__)
26
+ logger.setLevel(logging.WARNING)
27
+ handler = logging.StreamHandler()
28
+ logger.addHandler(handler)
29
 
30
  # Define repository and local directory
31
  repo_id = "microsoft/OmniParser-v2.0" # HF repo
 
39
  magma_processor = AutoProcessor.from_pretrained(magma_model_id, trust_remote_code=True)
40
  magam_model.to("cuda")
41
 
42
+ logger.warning(f"The repository is downloading to: {local_dir}")
43
+
44
  # Download the entire repository
45
  snapshot_download(repo_id=repo_id, local_dir=local_dir)
46
 
 
98
  add_generation_prompt=True
99
  )
100
 
101
+ with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
102
+ inputs = magma_processor(images=[image_som], texts=prompt, return_tensors="pt")
103
+ inputs['pixel_values'] = inputs['pixel_values'].unsqueeze(0).to(torch.bfloat16) # Add .to(torch.bfloat16) here for explicit casting
104
+ inputs['image_sizes'] = inputs['image_sizes'].unsqueeze(0)
105
+ # inputs = inputs.to("cuda")
106
+ inputs = inputs.to("cuda", dtype=torch.bfloat16)
107
 
108
  magam_model.generation_config.pad_token_id = magma_processor.tokenizer.pad_token_id
109
  with torch.inference_mode():
 
138
  add_generation_prompt=True
139
  )
140
 
141
+ with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
142
+ inputs = magma_processor(images=[image], texts=prompt, return_tensors="pt")
143
+ inputs['pixel_values'] = inputs['pixel_values'].unsqueeze(0).to(torch.bfloat16) # Add .to(torch.bfloat16) here for explicit casting
144
+ inputs['image_sizes'] = inputs['image_sizes'].unsqueeze(0)
145
+ # inputs = inputs.to("cuda")
146
+ inputs = inputs.to("cuda", dtype=torch.bfloat16)
147
 
148
  magam_model.generation_config.pad_token_id = magma_processor.tokenizer.pad_token_id
149
  with torch.inference_mode():