Skip to content

to support 2D input for tokenizer of genai reranker #2949

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

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions notebooks/llm-rag-langchain/llm-rag-langchain-genai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -880,19 +880,49 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"id": "d0bab20b",
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoTokenizer\n",
"from openvino_tokenizers import convert_tokenizer\n",
"import openvino as ov\n",
"\n",
"export_command_base = \"optimum-cli export openvino --model {} --task text-classification\".format(rerank_model_configuration[\"model_id\"])\n",
"export_command = export_command_base + \" \" + str(rerank_model_id.value)\n",
"\n",
"display(Markdown(\"**Export command:**\"))\n",
"display(Markdown(f\"`{export_command}`\"))\n",
"\n",
"if not Path(rerank_model_id.value).exists():\n",
" optimum_cli(rerank_model_configuration[\"model_id\"], str(rerank_model_id.value), show_command=False, additional_args={\"task\": \"text-classificaton\"})"
" optimum_cli(rerank_model_configuration[\"model_id\"], str(rerank_model_id.value), show_command=False, additional_args={\"task\": \"text-classificaton\"})\n",
" hf_tokenizer = AutoTokenizer.from_pretrained(rerank_model_configuration[\"model_id\"], trust_remote_code=True)\n",
" ov_tokenizer, ov_detokenizer = convert_tokenizer(hf_tokenizer, with_detokenizer=True, number_of_inputs=2)\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

@openvino-dev-samples , I've asked advice @pavel-esir and @apaniukov regarding how to better resolve second input addition in tokenizer. There is no need to complete tokenizer conversion: detokenizer remains the same, for converted tokenizer you can apply helper function from openvino tokenizers library https://github.com/openvinotoolkit/openvino_tokenizers/blob/master/python/openvino_tokenizers/tokenizer_transformations.py#L281

Copy link
Collaborator Author

@openvino-dev-samples openvino-dev-samples May 19, 2025

Choose a reason for hiding this comment

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

My method is provided by @pavel-esir, could you suggest an official solution on this case ? thanks

" ov.save_model(ov_tokenizer, f\"{rerank_model_id.value}/openvino_tokenizer.xml\")\n",
" ov.save_model(ov_detokenizer, f\"{rerank_model_id.value}/openvino_detokenizer.xml\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36c5c855",
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoTokenizer\n",
"from openvino_tokenizers import convert_tokenizer\n",
"import openvino as ov\n",
"\n",
"export_command_base = \"optimum-cli export openvino --model {} --task text-classification\".format(rerank_model_configuration[\"model_id\"])\n",
"export_command = export_command_base + \" \" + str(rerank_model_id.value)\n",
"\n",
"if not Path(rerank_model_id.value).exists():\n",
" ! $export_command\n",
" hf_tokenizer = AutoTokenizer.from_pretrained(rerank_model_configuration[\"model_id\"], trust_remote_code=True)\n",
" ov_tokenizer, ov_detokenizer = convert_tokenizer(hf_tokenizer, with_detokenizer=True, number_of_inputs=2)\n",
" ov.save_model(ov_tokenizer, f\"{rerank_model_id.value}/openvino_tokenizer.xml\")\n",
" ov.save_model(ov_detokenizer, f\"{rerank_model_id.value}/openvino_detokenizer.xml\")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions notebooks/llm-rag-langchain/ov_langchain_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ def rerank(self, request: Any) -> Any:
query = request.query
passages = request.passages
# # openvino tokenizer can only support 1D list
query_passage_pairs = [query + "</s></s> " + passage["text"] for passage in passages]
# query_passage_pairs = [[query, passage["text"]] for passage in passages]
# query_passage_pairs = [query + "</s></s> " + passage["text"] for passage in passages]
query_passage_pairs = [[query, passage["text"]] for passage in passages]
length = self.ov_model.inputs[0].get_partial_shape()[1]
if length.is_dynamic:
features = self.tokenizer.encode(query_passage_pairs)
Expand Down
6 changes: 4 additions & 2 deletions notebooks/qwen2.5-omni-chatbot/qwen2.5-omni-chatbot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "0116846d-da6f-4e81-b6be-0a882a3eb872",
"metadata": {},
"outputs": [],
"source": [
"%pip install -q \"git+https://github.com/huggingface/transformers\" \\\n",
"%pip uninstall transformers\n",
"\n",
"%pip install -q \"git+https://github.com/huggingface/[email protected]\" \\\n",
"\"torchvision\" \"accelerate\" \"qwen-omni-utils[decord]\" \"gradio>=4.19\" --no-cache-dir --extra-index-url https://download.pytorch.org/whl/cpu\n",
"%pip install -q \"openvino==2025.1.0\" \"nncf>=2.16.0\""
]
Expand Down