1818
1919from litert_torch import backend
2020from litert_torch import model as model_lib
21+ from litert_torch import progress
2122from litert_torch ._convert import signature
2223from litert_torch .backend import inline_consts as inline_consts_lib
2324from litert_torch .quantize import quant_config as qcfg
@@ -75,16 +76,20 @@ def to_file(self, path: str):
7576 f .write (self .content )
7677 return
7778
78- try :
79- # TODO b/478909085 - Remove the try-except once converter_api_ext is
80- # stable in OSS.
81- converter_api_ext .export_flatbuffer_to_file (self ._module_op , path )
82- except TypeError :
83- converter_api_ext .export_flatbuffer_to_file (self .module , path )
79+ with progress .task (f"Write Model to { path } " ):
80+ try :
81+ # TODO b/478909085 - Remove the try-except once converter_api_ext is
82+ # stable in OSS.
83+ converter_api_ext .export_flatbuffer_to_file (self ._module_op , path )
84+ except TypeError :
85+ converter_api_ext .export_flatbuffer_to_file (self .module , path )
8486
8587 def to_bytes (self ) -> bytes :
8688 """Returns the flatbuffer bytes of the module."""
87- if self .content is None :
89+ if self .content is not None :
90+ return self .content
91+
92+ with progress .task ("Write Model to Bytes" ):
8893 try :
8994 # TODO b/478909085 - Remove the try-except once converter_api_ext is
9095 # stable in OSS.
@@ -93,8 +98,8 @@ def to_bytes(self) -> bytes:
9398 )
9499 except TypeError :
95100 self .content = converter_api_ext .export_flatbuffer_to_bytes (self .module )
96- self .module = None
97101
102+ self .module = None
98103 return self .content
99104
100105
@@ -114,19 +119,19 @@ def exported_programs_to_flatbuffer(
114119 )
115120
116121 ir_context = backend .export_utils .create_ir_context ()
117-
118122 cross_program_inline_consts_ctx = inline_consts_lib .InlineConstsContext (
119123 enable_lazy_constants = lightweight_conversion ,
120124 )
121125
122126 lowered_programs = []
123127 for exported_program , sig in zip (exported_programs , signatures ):
124128 # Convert ExportedProgram to Mlir Module.
125- lowered = backend .export .exported_program_to_mlir (
126- exported_program ,
127- ir_context = ir_context ,
128- lowering_context_plugins = [cross_program_inline_consts_ctx ],
129- )
129+ with progress .task (f"Lower to MLIR: { sig .name } " ):
130+ lowered = backend .export .exported_program_to_mlir (
131+ exported_program ,
132+ ir_context = ir_context ,
133+ lowering_context_plugins = [cross_program_inline_consts_ctx ],
134+ )
130135
131136 # Set signature.
132137 sig_name = sig .name
@@ -141,9 +146,10 @@ def exported_programs_to_flatbuffer(
141146 lowered_programs .append (lowered )
142147
143148 # Merge all lowered modules into one module.
144- merged_module = converter_api_ext .merge_modules (
145- [lowered .module for lowered in lowered_programs ]
146- )
149+ with progress .task ("Merge MLIR Modules" ):
150+ merged_module = converter_api_ext .merge_modules (
151+ [lowered .module for lowered in lowered_programs ]
152+ )
147153
148154 # Prepare ai-edge-quantizer recipe.
149155 translated_recipe = None
@@ -170,7 +176,7 @@ def exported_programs_to_flatbuffer(
170176 config .canonicalizing_inf_as_min_max_float = False
171177
172178 # Run LiteRT converter passes.
173- with ir_context :
179+ with ir_context , progress . task ( "Run LiteRT Converter Passes" ) :
174180 pass_manager = passmanager .PassManager ()
175181 converter_api_ext .run_convert_to_tfl_passes (
176182 merged_module , pass_manager , config
@@ -181,9 +187,10 @@ def exported_programs_to_flatbuffer(
181187
182188 # Quantize the model if needed.
183189 if translated_recipe :
184- model_bytes = translate_recipe .quantize_model (
185- exporter .to_bytes (), translated_recipe
186- )
187- exporter = LazyModelExporter (content = model_bytes )
190+ with progress .task ("Quantize Model" ):
191+ model_bytes = translate_recipe .quantize_model (
192+ exporter .to_bytes (), translated_recipe
193+ )
194+ exporter = LazyModelExporter (content = model_bytes )
188195
189196 return exporter
0 commit comments