Skip to content

Commit 1b99ff6

Browse files
committed
fix after rebase
1 parent 7c0da29 commit 1b99ff6

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

optimum/exporters/openvino/utils.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,6 @@ def save_preprocessors(
319319
maybe_save_preprocessors(model_name_or_path, output, trust_remote_code=trust_remote_code)
320320

321321

322-
<<<<<<< HEAD
323-
<<<<<<< HEAD
324322
COMPLEX_CHAT_TEMPLATES = {
325323
# minicpm3
326324
"{%- macro json_to_python_type(param_name, json_spec) %}\n{%- set basic_type_map = {\n 'string': 'str',\n 'number': 'float',\n 'integer': 'int',\n 'boolean': 'bool',\n 'null': 'None'\n} %}\n\n{%- if json_spec.enum %}\n {{- param_name|title }}\n{%- elif basic_type_map[json_spec.type] is defined %}\n {{- basic_type_map[json_spec.type] }}\n{%- elif json_spec.type == 'array' %}\n {{- 'List[' + json_to_python_type(param_name, json_spec['items']) + ']' }}\n{%- elif json_spec.type == 'object' %}\n {{- 'Dict[str, ' + json_to_python_type(param_name, json_spec.additionalProperties if json_spec.additionalProperties else 'Any') + ']' if not json_spec.properties else param_name|title }}\n{%- elif json_spec.type is iterable %}\n {{- 'Union[' }}\n {%- for t in json_spec.type %}\n {{- json_to_python_type(param_name, {'type': t}) }}\n {{- ', ' if not loop.last }}\n {%- endfor %}\n {{- ']' }}\n{%- else %}\n {{- 'Any' }}\n{%- endif %}\n{%- endmacro %}\n\n{%- macro object_to_fields(json_spec, field_indent) %}\n {%- set o_ns = namespace(f = caller()) %}\n {%- for param_name, param_fields in json_spec.properties|items %}\n {%- if param_fields.enum %}\n {{- '\\n\\nclass ' + param_name|title + '(Enum):\\n' }}\n {%- for enum_option in param_fields.enum %}\n {{- ' enum_' + loop.index0|string + ' = ' + enum_option|tojson + '\\n' }}\n {%- endfor %}\n {%- elif param_fields.type == 'object' and param_fields.properties %}\n {%- call object_to_fields(param_fields, ' ') %}\n {{- '\\n\\nclass ' + param_name|title + '(BaseModel):\\n' }}\n {%- endcall %}\n {%- elif param_fields.type == 'array' and param_fields['items'] and param_fields['items'].type == 'object' and param_fields['items'].properties %}\n {%- call object_to_fields(param_fields['items'], ' ') %}\n {{- '\\n\\nclass ' + param_name|title + '(BaseModel):\\n' }}\n {%- endcall %}\n {%- endif %}\n {%- set param_default = param_fields.default|tojson if param_fields.default is string else param_fields.default|string if param_fields.default is defined else 'None' %}\n {%- set o_ns.f = o_ns.f + field_indent + param_name + ': ' %}\n {%- set o_ns.f = o_ns.f + ('Optional[' + json_to_python_type(param_name, param_fields) + ']' if param_name not in json_spec.required else json_to_python_type(param_name, param_fields)) %}\n {%- if not param_fields.title and not param_fields.description and not param_fields.pattern %}\n {%- set o_ns.f = o_ns.f + (' = ' + param_default if param_name not in json_spec.required else '') %}\n {%- else %}\n {%- set o_ns.f = o_ns.f + (' = Field(...' if param_name in json_spec.required else ' = Field(' + param_default) %}\n {%- set o_ns.f = o_ns.f + (', description=' + param_fields.description|tojson if param_fields.description else '') %}\n {%- set o_ns.f = o_ns.f + (', regex=' + param_fields.pattern|tojson if param_fields.pattern else '') %}\n {%- set o_ns.f = o_ns.f + (', title=' + param_fields.title|tojson if param_fields.title else '') %}\n {%- set o_ns.f = o_ns.f + ')' %}\n {%- endif %}\n {%- set o_ns.f = o_ns.f + '\\n' %}\n {%- endfor %}\n {{- o_ns.f }}\n{%- endmacro %}\n\n{%- macro tool_parser(tools) %}\n{%- for tool in tools %}\n {%- if tool.type is not defined or tool.type == 'function' %}\n {%- if tool.function is defined %}\n {%- set tool = tool.function %}\n {%- endif %}\n {%- set tool_params = tool.parameters if tool.parameters is defined else none %}\n {%- call object_to_fields(tool_params, ' ') %}\n {{- '\\n\\ndef ' + tool.name + '(' }}\n {%- if tool_params %}\n {%- for param_name, param_fields in tool_params.properties|items %}\n {%- set param_default = param_fields.default|tojson if param_fields.default is string else param_fields.default|string if param_fields.default is defined else 'None' %}\n {{- ', ' if loop.index0 != 0 }}\n {{- param_name }}\n {{- '=' + param_default if param_name not in tool_params.required }}\n {%- endfor %}\n {%- endif %}\n {{- '):\\n \"\"\"' }}\n {{- tool.description }}\n {{- '\\n\\n Args:\\n' if tool_params else '\\n' }}\n {%- endcall %}\n {{- ' \"\"\"\\n' }}\n {%- endif %}\n{%- endfor %}\n{%- endmacro %}\n\n{%- if messages[0]['role'] == 'system' %}\n {%- set loop_messages = messages[1:] %}\n {%- set system_message = messages[0]['content'] %}\n{%- else %}\n {%- set loop_messages = messages %}\n {%- set system_message = '' %}\n{%- endif %}\n{{- '<|im_start|>system\\n' + system_message if system_message or tools }}\n{%- if tools %}\n {{- '\\n# Functions\\nHere is a list of functions that you can invoke:\\n```python\\nfrom enum import Enum\\nfrom typing import List, Dict, Optional\\nfrom pydantic import BaseModel, Field\\n\\n' }}\n {{- tool_parser(tools) }}\n {{- \"\\n```\\n\\n# Function Call Rule and Output Format\\n- If the user's question can be answered without calling any function, please answer the user's question directly. In this situation, you should return your thought and answer the user's question directly.\\n- If the user cannot be answered without calling any function, and the user does not provide enough information to call functions, please ask the user for more information. In this situation, you should return your thought and ask the user for more information.\\n- If the user's question cannot be answered without calling any function, and the user has provided enough information to call functions to solve it, you should call the functions. In this situation, the assistant should return your thought and call the functions.\\n- Use default parameters unless the user has specified otherwise.\\n- You should answer in the following format:\\n\\n<|thought_start|>\\n{explain why the user's question can be answered without calling a function or why you should ask the user for more information or why you should call one or more functions and your plan to solve the user's question.}\\n<|thought_end|>\\n<|tool_call_start|>\\n```python\\nfunc1(params_name=params_value, params_name2=params_value2...)\\nfunc2(params)\\n```\\n<|tool_call_end|>\\n{answer the user's question directly or ask the user for more information}\" }}\n{%- endif %}\n{{- '<|im_end|>\\n' if system_message or tools }}\n{%- for message in loop_messages %}\n {%- set content = message.content %}\n {%- if message.role == 'assistant' and message.tool_calls %}\n {{- '<|im_start|>' + message.role + '\\n' }}\n {{- '<|thought_start|>\\n' + message.thought + '\\n<|thought_end|>\\n' if message.thought }}\n {{- '<|tool_call_start|>\\n```python\\n' }}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- tool_call.name + '(' }}\n {%- if tool_call.arguments is defined and tool_call.arguments|length > 0 %}\n {%- for param_name, param_value in tool_call.arguments|items %}\n {{- param_name + '=' + param_value|tojson }}\n {{- ',' if not loop.last }}\n {%- endfor %}\n {%- endif %}\n {{- ')\\n' }}\n {%- endfor %}\n {{- '```\\n<|tool_call_end|>\\n' }}\n {{- content if content and not content.startswith('<|tool_call_start|>') }}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == 'assistant' and message.thought %}\n {{- '<|im_start|>' + message.role + '\\n' + '<|thought_start|>\\n' + message.thought + '\\n<|thought_end|>\\n' + content + '<|im_end|>\\n' }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endfor %}\n\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n{%- endif %}": "{% for message in messages %}{% if message['role'] == 'system' %}{{ '<|im_start|>system\\n' + message['content'] + '<|im_end|>\\n' }}{% elif message['role'] == 'user' %}{{ '<|im_start|>user\\n' + message['content'] + '<|im_end|>\\n' }}{% elif message['role'] == 'assistant' %}{{ '<|im_start|>assistant\\n ' + message['content'] + '<|im_end|>\\n' }}{% endif %}{% if loop.last and add_generation_prompt %}{{ '<|im_start|>assistant\\n' }}{% endif %}{% endfor %}",
@@ -367,14 +365,13 @@ def allow_skip_tracing_check(library_name, model_type):
367365
return model_type in SKIP_CHECK_TRACE_MODELS
368366

369367

370-
371368
def load_preprocessors(
372369
src_name_or_path: Union[str, Path], subfolder: str = "", trust_remote_code: bool = False, model_type: str = None
373370
):
374371
preprocessors = maybe_load_preprocessors(
375372
src_name_or_path, subfolder=subfolder, trust_remote_code=trust_remote_code
376373
)
377-
if model_type == "janus":
374+
if model_type.replace("_", "-") == "multi-modality":
378375
if importlib.util.find_spec("janus") is not None:
379376
from janus.models import VLChatProcessor
380377

optimum/intel/openvino/modeling_visual_language.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
PretrainedConfig,
2525
PreTrainedTokenizer,
2626
)
27-
from transformers.modeling_outputs import BaseModelOutputWithPooling, BaseModelOutputWithPast
27+
from transformers.modeling_outputs import BaseModelOutputWithPast, BaseModelOutputWithPooling
2828
from transformers.utils import ModelOutput
2929

3030
from ...exporters.openvino import main_export
@@ -125,7 +125,6 @@ def _compile_lm_head(self):
125125
self.lm_head_model, self._device, self.ov_config, self.model_save_dir
126126
)
127127

128-
129128
def clear_requests(self):
130129
if self._compile_only:
131130
raise ValueError(
@@ -235,11 +234,13 @@ def forward(
235234
if self.lm_head_request is not None:
236235
last_hidden_state = self.request.get_tensor("last_hidden_state").data
237236
if include_head:
238-
logits = self.lm_head_request(logits)[0]
237+
logits = self.lm_head_request(last_hidden_state)[0]
239238
else:
240-
return BaseModelOutputWithPast(torch.from_numpy(last_hidden_state).to(self.device), past_key_values=past_key_values)
239+
return BaseModelOutputWithPast(
240+
torch.from_numpy(last_hidden_state).to(self.device), past_key_values=past_key_values
241+
)
241242
else:
242-
logits = self.request.get_tensor("logits" if self.lm_head_request is None else "last_hidden_state").data
243+
logits = self.request.get_tensor("logits").data
243244
logits = torch.from_numpy(logits).to(self.device)
244245

245246
return CausalLMOutputWithPast(logits=logits, past_key_values=past_key_values)
@@ -417,7 +418,7 @@ def __init__(
417418
)
418419
self.vision_embeddings = OVVisionEmbedding(self.vision_embeddings_model, self)
419420
for part in self.additional_parts:
420-
if model_part == "lm_head":
421+
if part == "lm_head":
421422
continue
422423
model_part = getattr(self, f"{part}_model", None)
423424
if model_part is not None:
@@ -909,7 +910,7 @@ def prepare_inputs_for_generation(
909910
"video_grid_thw": kwargs.get("video_grid_thw"),
910911
"token_type_ids": kwargs.get("token_type_ids"),
911912
"images_seq_mask": kwargs.get("images_seq_mask"),
912-
"images_emb_mask": kwargs.get("images_emb_mask")
913+
"images_emb_mask": kwargs.get("images_emb_mask"),
913914
}
914915
)
915916
return model_inputs
@@ -3309,7 +3310,6 @@ def preprocess_inputs(
33093310
return processed_inputs
33103311

33113312

3312-
33133313
class _OVJanusForCausalLM(OVModelForVisualCausalLM):
33143314
additional_parts = ["vision_gen_embeddings", "vision_gen_head", "vision_gen_decoder", "lm_head"]
33153315

@@ -3364,6 +3364,7 @@ def generate_image(
33643364
img_size: int = 384,
33653365
patch_size: int = 16,
33663366
generator=None,
3367+
show_progress=True,
33673368
):
33683369
from PIL import Image
33693370

@@ -3483,5 +3484,5 @@ def preprocess_inputs(
34833484
"qwen2_5_vl": _OVQwen2_5_VLForCausalLM,
34843485
"got_ocr2": _OVGotOCR2ForCausalLM,
34853486
"gemma3": _OVGemma3ForCausalLM,
3486-
"multi_modality": _OVJanusForCausalLM
3487+
"multi_modality": _OVJanusForCausalLM,
34873488
}

0 commit comments

Comments
 (0)