[3/N] Support and implement merged input processor for LLaVA model (#10676)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk> Co-authored-by: Roger Wang <ywang@roblox.com>
This commit is contained in:
co-authored by
Roger Wang
parent
acf092d348
commit
955fa9533a
@@ -15,7 +15,7 @@ from .audio import AudioPlugin
|
||||
from .base import MultiModalInputMapper, MultiModalPlugin, MultiModalTokensCalc
|
||||
from .image import ImagePlugin
|
||||
from .inputs import MultiModalDataDict, MultiModalKwargs, NestedTensors
|
||||
from .processing import MultiModalProcessor
|
||||
from .processing import MultiModalProcessingMetadata, MultiModalProcessor
|
||||
from .video import VideoPlugin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -200,6 +200,27 @@ class MultiModalRegistry:
|
||||
"""
|
||||
return self.register_max_multimodal_tokens("image", max_mm_tokens)
|
||||
|
||||
def get_max_tokens_by_modality(
|
||||
self,
|
||||
model_config: "ModelConfig",
|
||||
) -> Mapping[str, int]:
|
||||
"""
|
||||
Get the maximum number of tokens from each modality
|
||||
for profiling the memory usage of a model.
|
||||
|
||||
See :meth:`MultiModalPlugin.get_max_multimodal_tokens` for more details.
|
||||
|
||||
Note:
|
||||
This should be called after :meth:`init_mm_limits_per_prompt`.
|
||||
"""
|
||||
limits_per_plugin = self._limits_by_model[model_config]
|
||||
|
||||
return {
|
||||
key: (limits_per_plugin[key] *
|
||||
plugin.get_max_multimodal_tokens(model_config))
|
||||
for key, plugin in self._plugins.items()
|
||||
}
|
||||
|
||||
def get_max_multimodal_tokens(self, model_config: "ModelConfig") -> int:
|
||||
"""
|
||||
Get the maximum number of multi-modal tokens
|
||||
@@ -210,11 +231,7 @@ class MultiModalRegistry:
|
||||
Note:
|
||||
This should be called after :meth:`init_mm_limits_per_prompt`.
|
||||
"""
|
||||
limits_per_plugin = self._limits_by_model[model_config]
|
||||
|
||||
return sum((limits_per_plugin[key] *
|
||||
plugin.get_max_multimodal_tokens(model_config))
|
||||
for key, plugin in self._plugins.items())
|
||||
return sum(self.get_max_tokens_by_modality(model_config).values())
|
||||
|
||||
def init_mm_limits_per_prompt(
|
||||
self,
|
||||
@@ -270,7 +287,8 @@ class MultiModalRegistry:
|
||||
factory: MultiModalProcessorFactory,
|
||||
):
|
||||
"""
|
||||
Register a multi-modal processor to a model class.
|
||||
Register a multi-modal processor to a model class. The processor
|
||||
is constructed lazily, hence a factory method should be passed.
|
||||
|
||||
When the model receives multi-modal data, the provided function is
|
||||
invoked to transform the data into a dictionary of model inputs.
|
||||
@@ -293,6 +311,41 @@ class MultiModalRegistry:
|
||||
|
||||
return wrapper
|
||||
|
||||
def register_processor_by_metadata(
|
||||
self,
|
||||
metadata_factory: Callable[[InputProcessingContext],
|
||||
MultiModalProcessingMetadata],
|
||||
get_dummy_mm_kwargs: Callable[
|
||||
[InputProcessingContext, Mapping[str, int]], MultiModalKwargs],
|
||||
):
|
||||
"""
|
||||
Convenience method to register a multi-modal processor to a model class
|
||||
according to a function that constructs its metadata.
|
||||
|
||||
When the model receives multi-modal data, the provided function is
|
||||
invoked to transform the data into a dictionary of model inputs.
|
||||
|
||||
See also:
|
||||
- :ref:`input_processing_pipeline`
|
||||
- :ref:`enabling_multimodal_inputs`
|
||||
"""
|
||||
|
||||
class ConcreteMultiModalProcessor(MultiModalProcessor):
|
||||
|
||||
def _get_dummy_mm_kwargs(
|
||||
self,
|
||||
mm_counts: Mapping[str, int],
|
||||
) -> MultiModalKwargs:
|
||||
return get_dummy_mm_kwargs(self.ctx, mm_counts)
|
||||
|
||||
def factory(ctx: InputProcessingContext):
|
||||
return ConcreteMultiModalProcessor(
|
||||
ctx=ctx,
|
||||
metadata=metadata_factory(ctx),
|
||||
)
|
||||
|
||||
return self.register_processor(factory)
|
||||
|
||||
def has_processor(self, model_config: "ModelConfig") -> bool:
|
||||
"""
|
||||
Test whether a multi-modal processor is defined for a specific model.
|
||||
|
||||
Reference in New Issue
Block a user