@@ -91,7 +91,7 @@ class TransformerEncoderWrapper(torch.nn.Module):
9191 def __init__ (self , model ):
9292 super ().__init__ ()
9393 self .model = model
94- self .accepts_token_type_ids = getattr (model , ' accepts_token_type_ids' , True )
94+ self .accepts_token_type_ids = getattr (model , " accepts_token_type_ids" , True )
9595
9696 def forward (self , input_ids , attention_mask , token_type_ids = None ):
9797 """Run transformer encoder to get emissions.
@@ -130,7 +130,7 @@ def export_crf_params(model, output_path: str):
130130 # Handle different CRF implementations
131131 # Standard Contract: crf_params.json MUST contain "transitions" in [from_tag][to_tag] orientation.
132132 # Java Code (CRFDecoder.java) iterates as transitions[prevTag][currentTag].
133-
133+
134134 if hasattr (crf , "crf" ):
135135 # Using pytorch-crf wrapper (standard CRF class)
136136 # pytorch-crf stores transitions as [from_tag, to_tag]
@@ -254,15 +254,18 @@ def export_classification_config(model_config, output_path: str):
254254 print (f"Exported classification config to { output_path } " )
255255
256256
257-
258257def export_class_labels (model_config , output_path : str ):
259258 """
260259 Export class labels for text classification model.
261260 """
262261 labels = {
263262 "labels" : model_config .list_classes ,
264- "labelToIndex" : {label : idx for idx , label in enumerate (model_config .list_classes )},
265- "indexToLabel" : {idx : label for idx , label in enumerate (model_config .list_classes )},
263+ "labelToIndex" : {
264+ label : idx for idx , label in enumerate (model_config .list_classes )
265+ },
266+ "indexToLabel" : {
267+ idx : label for idx , label in enumerate (model_config .list_classes )
268+ },
266269 }
267270
268271 with open (output_path , "w" ) as f :
@@ -288,13 +291,15 @@ def export_tokenizer(tokenizer, output_dir: str):
288291 os .makedirs (output_dir , exist_ok = True )
289292 tokenizer .save_pretrained (output_dir )
290293 print (f"Exported tokenizer to { output_dir } " )
291-
294+
292295 # List exported files
293296 for f in os .listdir (output_dir ):
294297 print (f" - { f } " )
295298
296299
297- def export_transformer_config (model_config , preprocessor , accepts_token_type_ids : bool , output_path : str ):
300+ def export_transformer_config (
301+ model_config , preprocessor , accepts_token_type_ids : bool , output_path : str
302+ ):
298303 """
299304 Export transformer model configuration for Java runtime.
300305
@@ -309,15 +314,16 @@ def export_transformer_config(model_config, preprocessor, accepts_token_type_ids
309314 "architecture" : model_config .architecture ,
310315 "transformerName" : model_config .transformer_name ,
311316 "maxSequenceLength" : model_config .max_sequence_length ,
312- "useCRF" : "CRF" in model_config .architecture or "ChainCRF" in model_config .architecture ,
317+ "useCRF" : "CRF" in model_config .architecture
318+ or "ChainCRF" in model_config .architecture ,
313319 "useChainCRF" : "ChainCRF" in model_config .architecture ,
314320 "useFeatures" : "FEATURES" in model_config .architecture ,
315321 "useChar" : "CHAR" in model_config .architecture ,
316322 "acceptsTokenTypeIds" : accepts_token_type_ids ,
317323 }
318324
319325 # Add label mappings
320- if hasattr (preprocessor , ' vocab_tag' ):
326+ if hasattr (preprocessor , " vocab_tag" ):
321327 config ["labelVocab" ] = preprocessor .vocab_tag
322328 config ["labelIndex" ] = {str (k ): v for k , v in preprocessor .indice_tag .items ()}
323329 config ["numLabels" ] = len (preprocessor .vocab_tag )
@@ -723,7 +729,7 @@ def export_transformer_to_onnx(
723729 seq_len = max_seq_length
724730 dummy_input_ids = torch .zeros (batch_size , seq_len , dtype = torch .long )
725731 dummy_attention_mask = torch .ones (batch_size , seq_len , dtype = torch .long )
726-
732+
727733 # Prepare inputs and names based on model requirements
728734 if accepts_token_type_ids :
729735 dummy_token_type_ids = torch .zeros (batch_size , seq_len , dtype = torch .long )
@@ -768,23 +774,27 @@ def export_transformer_to_onnx(
768774 print ("ONNX transformer model exported successfully" )
769775
770776 # Export CRF params if applicable
771- if hasattr (model , ' crf' ):
777+ if hasattr (model , " crf" ):
772778 crf_path = os .path .join (output_dir , "crf_params.json" )
773779 export_crf_params (model , crf_path )
774780 else :
775781 print ("No CRF layer found (softmax output model)" )
776782
777783 # Export tokenizer
778784 tokenizer_dir = os .path .join (output_dir , "tokenizer" )
779- if hasattr (preprocessor , ' tokenizer' ) and preprocessor .tokenizer is not None :
785+ if hasattr (preprocessor , " tokenizer" ) and preprocessor .tokenizer is not None :
780786 export_tokenizer (preprocessor .tokenizer , tokenizer_dir )
781787 else :
782788 print ("Warning: No tokenizer found in preprocessor, skipping tokenizer export" )
783- print (" You may need to load the tokenizer separately using the transformer name" )
789+ print (
790+ " You may need to load the tokenizer separately using the transformer name"
791+ )
784792
785793 # Export config
786794 config_path = os .path .join (output_dir , "config.json" )
787- export_transformer_config (model_config , preprocessor , accepts_token_type_ids , config_path )
795+ export_transformer_config (
796+ model_config , preprocessor , accepts_token_type_ids , config_path
797+ )
788798
789799 # Verify ONNX model
790800 try :
@@ -807,7 +817,7 @@ def export_transformer_to_onnx(
807817 print (f" - { sub_item } " )
808818 else :
809819 # Show file size for ONNX file
810- if item .endswith (' .onnx' ):
820+ if item .endswith (" .onnx" ):
811821 size_mb = os .path .getsize (item_path ) / (1024 * 1024 )
812822 print (f" - { item } ({ size_mb :.1f} MB)" )
813823 else :
@@ -887,7 +897,9 @@ def export_transformer_to_onnx(
887897
888898def main ():
889899 parser = argparse .ArgumentParser (description = "Export DeLFT model to ONNX format" )
890- parser .add_argument ("model" , help = "Name of the model (e.g., header, date, dataseer-binary)" )
900+ parser .add_argument (
901+ "model" , help = "Name of the model (e.g., header, date, dataseer-binary)"
902+ )
891903 parser .add_argument (
892904 "--architecture" ,
893905 required = True ,
0 commit comments