Support cuda and cpu
#51
by master103525 - opened
- modeling_GOT.py +14 -12
modeling_GOT.py
CHANGED
|
@@ -19,6 +19,8 @@ DEFAULT_IMAGE_PATCH_TOKEN = '<imgpad>'
|
|
| 19 |
DEFAULT_IM_START_TOKEN = '<img>'
|
| 20 |
DEFAULT_IM_END_TOKEN = '</img>'
|
| 21 |
|
|
|
|
|
|
|
| 22 |
from enum import auto, Enum
|
| 23 |
class SeparatorStyle(Enum):
|
| 24 |
"""Different separator style."""
|
|
@@ -164,7 +166,7 @@ class GOTQwenModel(Qwen2Model):
|
|
| 164 |
use_im_start_end=False,
|
| 165 |
vision_select_layer=-1,
|
| 166 |
dtype=torch.float16,
|
| 167 |
-
device=
|
| 168 |
):
|
| 169 |
|
| 170 |
|
|
@@ -453,7 +455,7 @@ class GOTQwenForCausalLM(Qwen2ForCausalLM):
|
|
| 453 |
tokenizer,
|
| 454 |
freeze_lm_model=False,
|
| 455 |
pretrained_stage1_model=None,
|
| 456 |
-
device=
|
| 457 |
):
|
| 458 |
config = self.get_model().config
|
| 459 |
|
|
@@ -558,7 +560,7 @@ class GOTQwenForCausalLM(Qwen2ForCausalLM):
|
|
| 558 |
|
| 559 |
image_tensor_1 = image_processor_high(image)
|
| 560 |
|
| 561 |
-
input_ids = torch.as_tensor(inputs.input_ids).
|
| 562 |
|
| 563 |
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
|
| 564 |
keywords = [stop_str]
|
|
@@ -566,10 +568,10 @@ class GOTQwenForCausalLM(Qwen2ForCausalLM):
|
|
| 566 |
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 567 |
|
| 568 |
if stream_flag:
|
| 569 |
-
with torch.autocast(
|
| 570 |
output_ids = self.generate(
|
| 571 |
input_ids,
|
| 572 |
-
images=[image_tensor_1.unsqueeze(0).half().
|
| 573 |
do_sample=False,
|
| 574 |
num_beams = 1,
|
| 575 |
no_repeat_ngram_size = 20,
|
|
@@ -578,10 +580,10 @@ class GOTQwenForCausalLM(Qwen2ForCausalLM):
|
|
| 578 |
stopping_criteria=[stopping_criteria]
|
| 579 |
)
|
| 580 |
else:
|
| 581 |
-
with torch.autocast(
|
| 582 |
output_ids = self.generate(
|
| 583 |
input_ids,
|
| 584 |
-
images=[image_tensor_1.unsqueeze(0).half().
|
| 585 |
do_sample=False,
|
| 586 |
num_beams = 1,
|
| 587 |
no_repeat_ngram_size = 20,
|
|
@@ -812,7 +814,7 @@ class GOTQwenForCausalLM(Qwen2ForCausalLM):
|
|
| 812 |
|
| 813 |
inputs = tokenizer([prompt])
|
| 814 |
|
| 815 |
-
input_ids = torch.as_tensor(inputs.input_ids).
|
| 816 |
|
| 817 |
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
|
| 818 |
keywords = [stop_str]
|
|
@@ -820,10 +822,10 @@ class GOTQwenForCausalLM(Qwen2ForCausalLM):
|
|
| 820 |
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 821 |
|
| 822 |
if stream_flag:
|
| 823 |
-
with torch.autocast(
|
| 824 |
output_ids = self.generate(
|
| 825 |
input_ids,
|
| 826 |
-
images=[image_list.half().
|
| 827 |
do_sample=False,
|
| 828 |
num_beams = 1,
|
| 829 |
# no_repeat_ngram_size = 20,
|
|
@@ -832,10 +834,10 @@ class GOTQwenForCausalLM(Qwen2ForCausalLM):
|
|
| 832 |
stopping_criteria=[stopping_criteria]
|
| 833 |
)
|
| 834 |
else:
|
| 835 |
-
with torch.autocast(
|
| 836 |
output_ids = self.generate(
|
| 837 |
input_ids,
|
| 838 |
-
images=[image_list.half().
|
| 839 |
do_sample=False,
|
| 840 |
num_beams = 1,
|
| 841 |
# no_repeat_ngram_size = 20,
|
|
|
|
| 19 |
DEFAULT_IM_START_TOKEN = '<img>'
|
| 20 |
DEFAULT_IM_END_TOKEN = '</img>'
|
| 21 |
|
| 22 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
|
| 24 |
from enum import auto, Enum
|
| 25 |
class SeparatorStyle(Enum):
|
| 26 |
"""Different separator style."""
|
|
|
|
| 166 |
use_im_start_end=False,
|
| 167 |
vision_select_layer=-1,
|
| 168 |
dtype=torch.float16,
|
| 169 |
+
device=DEVICE
|
| 170 |
):
|
| 171 |
|
| 172 |
|
|
|
|
| 455 |
tokenizer,
|
| 456 |
freeze_lm_model=False,
|
| 457 |
pretrained_stage1_model=None,
|
| 458 |
+
device=DEVICE
|
| 459 |
):
|
| 460 |
config = self.get_model().config
|
| 461 |
|
|
|
|
| 560 |
|
| 561 |
image_tensor_1 = image_processor_high(image)
|
| 562 |
|
| 563 |
+
input_ids = torch.as_tensor(inputs.input_ids).to(DEVICE)
|
| 564 |
|
| 565 |
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
|
| 566 |
keywords = [stop_str]
|
|
|
|
| 568 |
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 569 |
|
| 570 |
if stream_flag:
|
| 571 |
+
with torch.autocast(DEVICE, dtype=torch.bfloat16):
|
| 572 |
output_ids = self.generate(
|
| 573 |
input_ids,
|
| 574 |
+
images=[image_tensor_1.unsqueeze(0).half().to(DEVICE)],
|
| 575 |
do_sample=False,
|
| 576 |
num_beams = 1,
|
| 577 |
no_repeat_ngram_size = 20,
|
|
|
|
| 580 |
stopping_criteria=[stopping_criteria]
|
| 581 |
)
|
| 582 |
else:
|
| 583 |
+
with torch.autocast(DEVICE, dtype=torch.bfloat16):
|
| 584 |
output_ids = self.generate(
|
| 585 |
input_ids,
|
| 586 |
+
images=[image_tensor_1.unsqueeze(0).half().to(DEVICE)],
|
| 587 |
do_sample=False,
|
| 588 |
num_beams = 1,
|
| 589 |
no_repeat_ngram_size = 20,
|
|
|
|
| 814 |
|
| 815 |
inputs = tokenizer([prompt])
|
| 816 |
|
| 817 |
+
input_ids = torch.as_tensor(inputs.input_ids).to(DEVICE)
|
| 818 |
|
| 819 |
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
|
| 820 |
keywords = [stop_str]
|
|
|
|
| 822 |
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 823 |
|
| 824 |
if stream_flag:
|
| 825 |
+
with torch.autocast(DEVICE, dtype=torch.bfloat16):
|
| 826 |
output_ids = self.generate(
|
| 827 |
input_ids,
|
| 828 |
+
images=[image_list.half().to(DEVICE)],
|
| 829 |
do_sample=False,
|
| 830 |
num_beams = 1,
|
| 831 |
# no_repeat_ngram_size = 20,
|
|
|
|
| 834 |
stopping_criteria=[stopping_criteria]
|
| 835 |
)
|
| 836 |
else:
|
| 837 |
+
with torch.autocast(DEVICE, dtype=torch.bfloat16):
|
| 838 |
output_ids = self.generate(
|
| 839 |
input_ids,
|
| 840 |
+
images=[image_list.half().to(DEVICE)],
|
| 841 |
do_sample=False,
|
| 842 |
num_beams = 1,
|
| 843 |
# no_repeat_ngram_size = 20,
|