[Model] Add Moondream3 model support(only query and caption skills) (#32325)
Signed-off-by: Dong Wang <dongw2019@gmail.com>
This commit is contained in:
+31
-8
@@ -371,6 +371,8 @@ class HfRunner:
|
||||
is_cross_encoder: bool = False,
|
||||
skip_tokenizer_init: bool = False,
|
||||
auto_cls: type[_BaseAutoModelClass] = AutoModelForCausalLM,
|
||||
tokenizer_name: str | None = None,
|
||||
processor: Any | None = None,
|
||||
# Set this to avoid hanging issue
|
||||
default_torch_num_threads: int | None = None,
|
||||
) -> None:
|
||||
@@ -391,6 +393,8 @@ class HfRunner:
|
||||
is_cross_encoder=is_cross_encoder,
|
||||
skip_tokenizer_init=skip_tokenizer_init,
|
||||
auto_cls=auto_cls,
|
||||
tokenizer_name=tokenizer_name,
|
||||
processor=processor,
|
||||
)
|
||||
|
||||
def _init(
|
||||
@@ -405,6 +409,8 @@ class HfRunner:
|
||||
is_cross_encoder: bool = False,
|
||||
skip_tokenizer_init: bool = False,
|
||||
auto_cls: type[_BaseAutoModelClass] = AutoModelForCausalLM,
|
||||
tokenizer_name: str | None = None,
|
||||
processor: Any | None = None,
|
||||
) -> None:
|
||||
model_name = maybe_model_redirect(model_name)
|
||||
self.model_name = model_name
|
||||
@@ -484,20 +490,27 @@ class HfRunner:
|
||||
if not skip_tokenizer_init:
|
||||
self.tokenizer: "PreTrainedTokenizer | PreTrainedTokenizerFast" = (
|
||||
AutoTokenizer.from_pretrained(
|
||||
model_name,
|
||||
tokenizer_name or model_name,
|
||||
trust_remote_code=trust_remote_code,
|
||||
)
|
||||
)
|
||||
|
||||
# don't put this import at the top level
|
||||
# it will call torch.accelerator.device_count()
|
||||
from transformers import AutoProcessor
|
||||
if processor is not None:
|
||||
self.processor = processor
|
||||
else:
|
||||
# don't put this import at the top level
|
||||
# it will call torch.accelerator.device_count()
|
||||
from transformers import AutoProcessor
|
||||
|
||||
self.processor = AutoProcessor.from_pretrained(
|
||||
model_name,
|
||||
trust_remote_code=trust_remote_code,
|
||||
)
|
||||
self.processor = AutoProcessor.from_pretrained(
|
||||
model_name,
|
||||
trust_remote_code=trust_remote_code,
|
||||
)
|
||||
if skip_tokenizer_init:
|
||||
if self.processor is None:
|
||||
raise ValueError(
|
||||
"skip_tokenizer_init=True requires processor initialization."
|
||||
)
|
||||
self.tokenizer = self.processor.tokenizer
|
||||
|
||||
def get_inputs(
|
||||
@@ -520,6 +533,12 @@ class HfRunner:
|
||||
all_inputs: list[BatchFeature | BatchEncoding | dict[str, torch.Tensor]] = []
|
||||
for i, prompt in enumerate(prompts):
|
||||
if isinstance(prompt, str):
|
||||
if self.processor is None:
|
||||
raise RuntimeError(
|
||||
"HfRunner.processor is not initialized. "
|
||||
"Pass processor=... to HfRunner or set "
|
||||
"hf_model.processor before generation."
|
||||
)
|
||||
# Create a copy to avoid modifying the original dict
|
||||
processor_kwargs = (
|
||||
tokenization_kwargs.copy()
|
||||
@@ -617,6 +636,10 @@ class HfRunner:
|
||||
use_cache=True,
|
||||
**kwargs,
|
||||
)
|
||||
if self.processor is None:
|
||||
raise RuntimeError(
|
||||
"HfRunner.processor is not initialized; cannot decode output."
|
||||
)
|
||||
output_str = self.processor.batch_decode(
|
||||
output_ids,
|
||||
skip_special_tokens=True,
|
||||
|
||||
Reference in New Issue
Block a user