22import os
33from typing import Dict , List , Optional , Tuple , Union
44import math
5+ import gc
56
67import numpy as np
78import torch
@@ -296,28 +297,26 @@ def wrapper(module, target_op, *args, **kwargs):
296297 model ._openvino_patch_orig_forward = model .forward
297298 model .forward = partial (ov_wrapper , model )
298299
299-
300- def patch_stateful_model (model ):
300+ def patch_stateful_model (model , factory ):
301301 print ('TRANSFORMING OPTIMUM-INTEL MODEL TO vLLM COMPATIBLE FORM' )
302302 from openvino .runtime .passes import Manager , MatcherPass , WrapType , Matcher , AnyInput , Or
303303 from openvino .runtime import opset13
304- from openvino .runtime .utils .node_factory import NodeFactory
305304 from openvino .runtime .utils import replace_node
306- factory = NodeFactory ()
307- factory .add_extension ("libuser_ov_extensions.so" )
308305
309306 #model.remove_parameter(model.input('beam_idx').get_node())
310- max_context_len = opset13 .parameter (shape = [], dtype = np .int32 , name = 'max_context_len' ) # max_context_len
307+ max_context_len = opset13 .parameter (shape = [], dtype = np .int64 , name = 'max_context_len' ) # max_context_len
311308 model_remaining_params = [
312309 opset13 .parameter (shape = [], dtype = bool , name = 'is_prompt' ), # is_prompt
313310 opset13 .parameter (shape = [- 1 , - 1 ], dtype = np .int64 , name = 'slot_mapping' ), # slot mapping
314311 max_context_len ,
315- opset13 .parameter (shape = [- 1 ], dtype = np .int32 , name = 'context_lens' ), # context_lens
312+ opset13 .parameter (shape = [- 1 ], dtype = np .int64 , name = 'context_lens' ), # context_lens
316313 opset13 .parameter (shape = [- 1 , - 1 ], dtype = np .int32 , name = 'block_tables' ), # block_tables
317314 ]
315+ for parameter in model_remaining_params :
316+ parameter .get_output_tensor (0 ).set_names ({parameter .get_friendly_name ()})
318317 paged_attention_remaining_args = [
319- opset13 .constant ([] ), # alibi_slopes
320- opset13 .constant (0 ), # sliding_window
318+ opset13 .constant (np . array ([], np . float32 ) ), # alibi_slopes
319+ opset13 .constant (np . array ( 0 , np . int32 ) ), # sliding_window
321320 ]
322321
323322 kv_parameters = []
@@ -468,6 +467,7 @@ def callback(m: Matcher) -> bool:
468467 position_ids_parameter .append (opset13 .parameter (shape = [- 1 , - 1 ], dtype = np .int64 , name = "position_ids" ))
469468 print ('CREATED A NEW position_ids PARAMETER' )
470469 replace_node (mapping [position_ids ].get_node (), position_ids_parameter [0 ])
470+ position_ids_parameter [0 ].get_output_tensor (0 ).set_names ({'position_ids' })
471471 print ('APPLIED position_ids PARAMETER INSTEAD OF attention_mask-BASED SUB-GRAPH' )
472472 return True
473473
@@ -548,8 +548,13 @@ def load_model(self) -> None:
548548 if is_openvino_optimum_intel :
549549 import openvino as ov
550550 from optimum .intel import OVModelForCausalLM
551- self .model = OVModelForCausalLM .from_pretrained (self .model_config .model , export = True , compile = False , load_in_8bit = False ) # need stateful because it also enables SDPA
552- patch_stateful_model (self .model .model )
551+ self .model = OVModelForCausalLM .from_pretrained (self .model_config .model , export = True , compile = False , load_in_8bit = False , trust_remote_code = True ) # need stateful because it also enables SDPA
552+ if not hasattr (self .model , 'ov_node_factory' ):
553+ from openvino .runtime .utils .node_factory import NodeFactory
554+ # Keep factory to destroy it in a particular moment when all other objects referencing custom nodes are destoyed
555+ self .model .ov_node_factory = NodeFactory ()
556+ self .model .ov_node_factory .add_extension ('libuser_ov_extensions.so' )
557+ patch_stateful_model (self .model .model , self .model .ov_node_factory )
553558 #ov.serialize(self.model.model, 'vllm_openvino_model.xml')
554559 core = ov .Core ()
555560 ov_compiled = core .compile_model (self .model .model , "CPU" )
@@ -568,6 +573,15 @@ def load_model(self) -> None:
568573 else :
569574 self .model = get_model (self .model_config )
570575
576+ def __del__ (self ):
577+ # Order is important
578+ if hasattr (self .model , 'ov_node_factory' ):
579+ del self .model .ov_request
580+ del self .model .model
581+ if gc : # when app is being destroyed the module may not be available
582+ gc .collect ()
583+ del self .model .ov_node_factory
584+
571585 def set_block_size (self , block_size : int ) -> None :
572586 self .block_size = block_size
573587
0 commit comments