@@ -104,19 +104,19 @@ def __init__(self, models: Dict[str, "ExecuTorchModule"], config: "PretrainedCon
104104 setattr (self , key , value )
105105
106106 self .stats = Stats ()
107-
107+
108108 # Initialize cleanup tracking
109109 self ._temp_dir = None
110110
111111 def __del__ (self ):
112112 """Clean up temporary files when the model instance is destroyed."""
113113 self ._cleanup_temp_resources ()
114-
114+
115115 def _cleanup_temp_resources (self ):
116116 """Clean up temporary directory and files."""
117- if hasattr (self , ' _temp_dir' ) and self ._temp_dir is not None :
117+ if hasattr (self , " _temp_dir" ) and self ._temp_dir is not None :
118118 try :
119- if hasattr (self ._temp_dir , ' cleanup' ):
119+ if hasattr (self ._temp_dir , " cleanup" ):
120120 # It's a TemporaryDirectory object
121121 logging .info (f"Cleaning up temporary directory: { self ._temp_dir .name } " )
122122 self ._temp_dir .cleanup ()
@@ -216,18 +216,6 @@ def _from_pretrained(
216216 subfolder = subfolder ,
217217 local_files_only = local_files_only ,
218218 )
219-
220- from torch .ao .quantization .fx ._decomposed import quantized_decomposed_lib # noqa
221- from executorch .kernels import quantized # noqa
222- from executorch .extension .pybindings .portable_lib import _get_operator_names
223- print ("----------- LOADED OPS ----------" )
224- print ('\n ' .join (_get_operator_names ()))
225- print ("---------------------------------" )
226- model = _load_for_executorch (model_cache_path )
227- logging .info (
228- f"Loaded model from { model_cache_path } ({ os .path .getsize (model_cache_path ) / (1024 * 1024 ):.2f} MB)"
229- )
230-
231219 return {default_file_name .removesuffix (_PTE_SUFFIX ): model }
232220
233221 @staticmethod
@@ -304,15 +292,6 @@ def _export(
304292 for name , _ in executorch_progs .items ():
305293 models .update (cls ._from_pretrained (save_dir_path , file_name = f"{ name } .pte" , config = config ))
306294
307- # Log temp directory info for debugging
308- logging .info (f"Created temporary directory: { save_dir_path } " )
309- for name in executorch_progs .keys ():
310- pte_file = save_dir_path / f"{ name } .pte"
311- if pte_file .exists ():
312- logging .info (f"PTE file exists at export: { pte_file } (size: { pte_file .stat ().st_size } bytes)" )
313- else :
314- logging .warning (f"PTE file missing at export: { pte_file } " )
315-
316295 return models , save_dir
317296
318297 def _save_pretrained (self , save_directory ):
@@ -399,7 +378,9 @@ def from_pretrained(
399378 ** kwargs ,
400379 )
401380 else :
402- logging .info (f"Pre-exported `.pte` artifact already exists in HuggingFace repo or provided file path for { model_id } , skipping export." )
381+ logging .info (
382+ f"Pre-exported `.pte` artifact already exists in HuggingFace repo or provided file path for { model_id } , skipping export."
383+ )
403384 models_dict = {}
404385 for pte_file in pte_files :
405386 models_dict .update (
@@ -418,12 +399,12 @@ def from_pretrained(
418399 )
419400
420401 model_instance = cls (models_dict , config )
421-
402+
422403 # Store the TemporaryDirectory reference to prevent GC
423404 if temp_dir is not None :
424405 model_instance ._temp_dir = temp_dir
425406 logging .info (f"Stored temp directory reference in model: { temp_dir .name } " )
426-
407+
427408 return model_instance
428409
429410
@@ -695,32 +676,12 @@ def forward(
695676 Returns:
696677 torch.Tensor: Logits output from the model.
697678 """
698- # Check if temp directory and PTE file still exist before forward pass
699- if hasattr (self , '_temp_dir' ) and self ._temp_dir is not None :
700- temp_path = Path (self ._temp_dir .name )
701- logging .info (f"Forward pass - temp directory exists: { temp_path .exists ()} " )
702- if temp_path .exists ():
703- pte_files = list (temp_path .glob ("*.pte" ))
704- logging .info (f"Forward pass - PTE files found: { len (pte_files )} " )
705- for pte_file in pte_files :
706- logging .info (f"Forward pass - PTE file: { pte_file } exists: { pte_file .exists ()} , size: { pte_file .stat ().st_size if pte_file .exists () else 'N/A' } " )
707- else :
708- logging .error (f"Forward pass - temp directory missing: { temp_path } " )
709- else :
710- logging .info ("Forward pass - no temp directory reference stored" )
711-
712679 self .stats .on_model_execution_start ()
713680
714681 try :
715- logging .info ("Running forward()..." )
716682 logits = self .model .forward ((input_ids , cache_position ))[0 ]
717- logging .info (f"logits from forward(): { logits } " )
718683 except Exception as e :
719684 shapes = {name : val .shape for name , val in locals ().items () if hasattr (val , "shape" )}
720- logging .error (f"Forward pass failed - temp dir exists: { hasattr (self , '_temp_dir' ) and self ._temp_dir is not None } " )
721- if hasattr (self , '_temp_dir' ) and self ._temp_dir is not None :
722- temp_path = Path (self ._temp_dir .name )
723- logging .error (f"Forward pass failed - temp directory: { temp_path } exists: { temp_path .exists ()} " )
724685 print (f"Exception: { e } .\n { self .model .method_meta ('forward' )} \n arg shapes: { shapes } " )
725686 raise
726687
0 commit comments