AustingDong
commited on
Commit
·
92fce16
1
Parent(s):
f73c268
Update model_utils.py
Browse files- demo/model_utils.py +9 -4
demo/model_utils.py
CHANGED
|
@@ -7,11 +7,13 @@ from transformers import CLIPProcessor, CLIPModel
|
|
| 7 |
from janus.models import MultiModalityCausalLM, VLChatProcessor
|
| 8 |
|
| 9 |
@spaces.GPU(duration=120)
|
| 10 |
-
def set_dtype_device(model, precision=16):
|
| 11 |
dtype = (torch.bfloat16 if torch.cuda.is_available() else torch.float16) if precision==16 else (torch.bfloat32 if torch.cuda.is_available() else torch.float32)
|
| 12 |
cuda_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 13 |
if torch.cuda.is_available():
|
| 14 |
-
model = model.to(dtype)
|
|
|
|
|
|
|
| 15 |
else:
|
| 16 |
torch.set_default_device("cpu")
|
| 17 |
model = model.to(dtype)
|
|
@@ -125,6 +127,7 @@ class LLaVA_Utils(Model_Utils):
|
|
| 125 |
self.vl_gpt = LlavaForConditionalGeneration.from_pretrained(model_path,
|
| 126 |
low_cpu_mem_usage=True,
|
| 127 |
attn_implementation = 'eager',
|
|
|
|
| 128 |
output_attentions=True
|
| 129 |
)
|
| 130 |
self.vl_gpt, self.dtype, self.cuda_device = set_dtype_device(self.vl_gpt)
|
|
@@ -137,11 +140,13 @@ class LLaVA_Utils(Model_Utils):
|
|
| 137 |
self.processor = AutoProcessor.from_pretrained(model_path)
|
| 138 |
|
| 139 |
self.vl_gpt = LlavaOnevisionForConditionalGeneration.from_pretrained(model_path,
|
| 140 |
-
torch_dtype=torch.float16,
|
|
|
|
| 141 |
low_cpu_mem_usage=True,
|
| 142 |
attn_implementation = 'eager',
|
| 143 |
output_attentions=True)
|
| 144 |
-
self.vl_gpt, self.dtype, self.cuda_device = set_dtype_device(self.vl_gpt)
|
|
|
|
| 145 |
self.tokenizer = self.processor.tokenizer
|
| 146 |
|
| 147 |
return self.vl_gpt, self.tokenizer
|
|
|
|
| 7 |
from janus.models import MultiModalityCausalLM, VLChatProcessor
|
| 8 |
|
| 9 |
@spaces.GPU(duration=120)
|
| 10 |
+
def set_dtype_device(model, precision=16, device_map=None):
|
| 11 |
dtype = (torch.bfloat16 if torch.cuda.is_available() else torch.float16) if precision==16 else (torch.bfloat32 if torch.cuda.is_available() else torch.float32)
|
| 12 |
cuda_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 13 |
if torch.cuda.is_available():
|
| 14 |
+
model = model.to(dtype)
|
| 15 |
+
if not device_map:
|
| 16 |
+
model.cuda()
|
| 17 |
else:
|
| 18 |
torch.set_default_device("cpu")
|
| 19 |
model = model.to(dtype)
|
|
|
|
| 127 |
self.vl_gpt = LlavaForConditionalGeneration.from_pretrained(model_path,
|
| 128 |
low_cpu_mem_usage=True,
|
| 129 |
attn_implementation = 'eager',
|
| 130 |
+
device_map="auto",
|
| 131 |
output_attentions=True
|
| 132 |
)
|
| 133 |
self.vl_gpt, self.dtype, self.cuda_device = set_dtype_device(self.vl_gpt)
|
|
|
|
| 140 |
self.processor = AutoProcessor.from_pretrained(model_path)
|
| 141 |
|
| 142 |
self.vl_gpt = LlavaOnevisionForConditionalGeneration.from_pretrained(model_path,
|
| 143 |
+
torch_dtype=torch.float16,
|
| 144 |
+
device_map="auto",
|
| 145 |
low_cpu_mem_usage=True,
|
| 146 |
attn_implementation = 'eager',
|
| 147 |
output_attentions=True)
|
| 148 |
+
self.vl_gpt, self.dtype, self.cuda_device = set_dtype_device(self.vl_gpt, device_map="auto")
|
| 149 |
+
|
| 150 |
self.tokenizer = self.processor.tokenizer
|
| 151 |
|
| 152 |
return self.vl_gpt, self.tokenizer
|