-
-
Notifications
You must be signed in to change notification settings - Fork 11k
[Core] Hidden State Processors via plugins #21621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -350,6 +350,7 @@ class EngineArgs: | |||||
| MultiModalConfig.mm_processor_kwargs | ||||||
| disable_mm_preprocessor_cache: bool = \ | ||||||
| MultiModalConfig.disable_mm_preprocessor_cache | ||||||
| process_hidden_states: bool = False | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this change, the default is only defined in one place
Suggested change
|
||||||
| # LoRA fields | ||||||
| enable_lora: bool = False | ||||||
| enable_lora_bias: bool = LoRAConfig.bias_enabled | ||||||
|
|
@@ -503,6 +504,8 @@ def add_cli_args(parser: FlexibleArgumentParser) -> FlexibleArgumentParser: | |||||
| **model_kwargs["enable_prompt_embeds"]) | ||||||
| model_group.add_argument("--served-model-name", | ||||||
| **model_kwargs["served_model_name"]) | ||||||
| model_group.add_argument("--process-hidden-states", | ||||||
| **model_kwargs["process_hidden_states"]) | ||||||
| # This one is a special case because it is the | ||||||
| # opposite of ModelConfig.use_async_output_proc | ||||||
| model_group.add_argument( | ||||||
|
|
@@ -910,6 +913,7 @@ def create_model_config(self) -> ModelConfig: | |||||
| enable_sleep_mode=self.enable_sleep_mode, | ||||||
| model_impl=self.model_impl, | ||||||
| override_attention_dtype=self.override_attention_dtype, | ||||||
| process_hidden_states=self.process_hidden_states, | ||||||
| ) | ||||||
|
|
||||||
| def validate_tensorizer_args(self): | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,6 +147,9 @@ class LLM: | |
| compilation_config: Either an integer or a dictionary. If it is an | ||
| integer, it is used as the level of compilation optimization. If it | ||
| is a dictionary, it can specify the full compilation configuration. | ||
| process_hidden_states: If True, it loads the hidden states processor | ||
| and to process the hiddne states for each request before returning | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: hiddne |
||
| to the user. | ||
| **kwargs: Arguments for [`EngineArgs`][vllm.EngineArgs]. | ||
|
|
||
| Note: | ||
|
|
@@ -195,6 +198,7 @@ def __init__( | |
| override_pooler_config: Optional[PoolerConfig] = None, | ||
| compilation_config: Optional[Union[int, dict[str, Any], | ||
| CompilationConfig]] = None, | ||
| process_hidden_states: bool = False, | ||
| **kwargs, | ||
| ) -> None: | ||
| """LLM constructor.""" | ||
|
|
@@ -268,6 +272,7 @@ def __init__( | |
| mm_processor_kwargs=mm_processor_kwargs, | ||
| override_pooler_config=override_pooler_config, | ||
| compilation_config=compilation_config_instance, | ||
| process_hidden_states=process_hidden_states, | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||||||||||
| # SPDX-License-Identifier: Apache-2.0 | ||||||||||||||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||||||||||||||
|
|
||||||||||||||
| import logging | ||||||||||||||
| from typing import Optional | ||||||||||||||
|
|
||||||||||||||
| import vllm.envs as envs | ||||||||||||||
| from vllm.config import VllmConfig | ||||||||||||||
| from vllm.plugins import load_plugins_by_group | ||||||||||||||
| from vllm.plugins.hidden_states_processors.interface import ( | ||||||||||||||
| HiddenStatesProcessor) | ||||||||||||||
| from vllm.utils import resolve_obj_by_qualname | ||||||||||||||
|
|
||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def identity_hidden_states_processor() -> str: | ||||||||||||||
| return ("vllm.plugins.hidden_states_processors." | ||||||||||||||
| "default.IdentityHiddenStatesProcessor") | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| default_hidden_states_processors = { | ||||||||||||||
| "identity": identity_hidden_states_processor | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def get_hidden_states_processor( | ||||||||||||||
| vllm_config: VllmConfig) -> Optional["HiddenStatesProcessor"]: | ||||||||||||||
| # hidden states processors are loaded as plugins under the | ||||||||||||||
| # 'vllm.hidden_state_processor_plugins group. Similar to platform | ||||||||||||||
| # plugins, these plugins register a function that returns the class | ||||||||||||||
| # name for the processor to install. | ||||||||||||||
| # All hidden state plugins implement the HiddenStatesProcessor class | ||||||||||||||
|
|
||||||||||||||
| hidden_states_processor_plugins = \ | ||||||||||||||
| load_plugins_by_group('vllm.hidden_states_processor_plugins') | ||||||||||||||
|
|
||||||||||||||
| available_plugins = { | ||||||||||||||
| **default_hidden_states_processors, | ||||||||||||||
| **hidden_states_processor_plugins | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| loadable_plugins = {} | ||||||||||||||
| for name, func in available_plugins.items(): | ||||||||||||||
| try: | ||||||||||||||
| assert callable(func) | ||||||||||||||
| processor_cls_qualname = func() | ||||||||||||||
| if processor_cls_qualname is not None: | ||||||||||||||
| loadable_plugins[name] = processor_cls_qualname | ||||||||||||||
| except Exception: | ||||||||||||||
| pass | ||||||||||||||
|
Comment on lines
+50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The broad
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| num_available_plugins = len(loadable_plugins.keys()) | ||||||||||||||
|
|
||||||||||||||
| # Just a sanity check to make sure we are not | ||||||||||||||
| # messing up with the available plugins | ||||||||||||||
| assert num_available_plugins > 0 | ||||||||||||||
|
|
||||||||||||||
| if num_available_plugins > 1 and envs.VLLM_USE_HIDDEN_STATES_PROCESSOR: | ||||||||||||||
| plugin_name = envs.VLLM_USE_HIDDEN_STATES_PROCESSOR | ||||||||||||||
| if plugin_name not in loadable_plugins: | ||||||||||||||
| raise ValueError( | ||||||||||||||
| f"Hidden states processor plugin '{plugin_name}' not found. " | ||||||||||||||
| f"Available plugins: {list(loadable_plugins.keys())}") | ||||||||||||||
|
|
||||||||||||||
| activated_plugin_cls = loadable_plugins[plugin_name] | ||||||||||||||
| activated_plugin_name = envs.VLLM_USE_HIDDEN_STATES_PROCESSOR | ||||||||||||||
| else: | ||||||||||||||
| activated_plugin_name = list(loadable_plugins.keys())[0] | ||||||||||||||
| activated_plugin_cls = loadable_plugins[activated_plugin_name] | ||||||||||||||
| if (num_available_plugins > 1 | ||||||||||||||
| and not envs.VLLM_USE_HIDDEN_STATES_PROCESSOR): | ||||||||||||||
| logger.info( | ||||||||||||||
| "Multiple hidden states processor plugins available " | ||||||||||||||
| "but VLLM_USE_HIDDEN_STATES_PROCESSOR is not pointing " | ||||||||||||||
| "to any specific plugins. Loading the first available one.\n" | ||||||||||||||
| "Available hidden states " | ||||||||||||||
| "processor plugins %s", str(loadable_plugins.keys())) | ||||||||||||||
|
|
||||||||||||||
| logger.info("Loaded hidden states processor plugin: %s", | ||||||||||||||
| activated_plugin_name) | ||||||||||||||
| return resolve_obj_by_qualname(activated_plugin_cls)(vllm_config) | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| from typing import Any | ||
|
|
||
| import torch | ||
|
|
||
| from vllm.plugins.hidden_states_processors.interface import ( | ||
| HiddenStatesProcessor) | ||
|
|
||
|
|
||
| class IdentityHiddenStatesProcessor(HiddenStatesProcessor): | ||
|
|
||
| def apply(self, data: torch.Tensor) -> Any: | ||
| """ | ||
| This is the default identity hidden states processor | ||
| that returns the hidden_states data as is | ||
| """ | ||
| return data |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import Any | ||
|
|
||
| import torch | ||
|
|
||
| from vllm.config import VllmConfig | ||
|
|
||
|
|
||
| class HiddenStatesProcessor(ABC): | ||
|
|
||
| def __init__(self, vllm_config: VllmConfig): | ||
| self.vllm_config = vllm_config | ||
|
|
||
| @abstractmethod | ||
| def apply(self, data: torch.Tensor) -> Any: | ||
| ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this can never be
None, we shouldn't hint it asOptional