Skip to content
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

Adding hidden_states to generator state to enable embedding scenarios #1203

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

vortex-captain
Copy link
Contributor

@vortex-captain vortex-captain commented Jan 24, 2025

Description

This PR enables users to get hidden_states output as text embedding from language models by calling GenAI C# interface. (See #1127 for creating models with hidden_states output).

If "hidden_states" is specified as a non-empty string in genai_config["model"]["decoder"]["outputs"], an extra output will be added to the generator state via Generators::Logits. Otherwise The behavior of Logits remains unchanged.

Test

Convert a model with hidden_states output:

python -m onnxruntime_genai.models.builder -m Qwen/Qwen2.5-0.5B-Instruct -c <model-cache-dir> -e cpu -p int4 -o ./qwen_w_embedding --extra_options include_hidden_states=true

Item hidden_states is added to genai_config["model"]["decoder"]["outputs"]:

"outputs": {
    "hidden_states": "hidden_states",
    "logits": "logits",
    "present_key_names": "present.%d.key",
    "present_value_names": "present.%d.value"
},

Then, in C# code:

Generator generator = ...;
generator.GenerateNextToken();
generator.GetOutput("hidden_states");

@vortex-captain vortex-captain changed the title Add hidden_states to genai_config, and add hidden_states to state out… Adding hidden_states to generator state to enable embedding scenarios Jan 24, 2025
@vortex-captain vortex-captain marked this pull request as ready for review January 24, 2025 08:39
@@ -326,7 +326,8 @@ def make_genai_config(self, model_name_or_path, extra_kwargs, out_dir):
})
if "hidden_states" in outputs:
# Remove 'hidden_states' from 'outputs' entry in config since ORT GenAI doesn't use it
del outputs["hidden_states"]
# del outputs["hidden_states"]
pass
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need an extra bool flag in genai_config to indicate whether to remove hidden_states from outputs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant