@@ -185,9 +185,17 @@ def _make_exporter_export(
185185
186186 if exporter == "export-strict" :
187187 try :
188- exported = torch .export .export (
189- model , inputs , dynamic_shapes = dynamic_shapes , strict = True
190- )
188+ if verbose >= 2 :
189+ exported = torch .export .export (
190+ model , inputs , dynamic_shapes = dynamic_shapes , strict = True
191+ )
192+ else :
193+ with contextlib .redirect_stdout (io .StringIO ()), contextlib .redirect_stderr (
194+ io .StringIO ()
195+ ):
196+ exported = torch .export .export (
197+ model , inputs , dynamic_shapes = dynamic_shapes , strict = True
198+ )
191199 except Exception as e :
192200 if not quiet :
193201 raise
@@ -198,17 +206,33 @@ def _make_exporter_export(
198206 return exported .module ()
199207 if exporter in ("export-strict-dec" , "export-strict-decall" ):
200208 try :
201- exported = torch .export .export (
202- model , inputs , dynamic_shapes = dynamic_shapes , strict = True
203- )
204- if verbose >= 9 :
205- print ("-- graph before decomposition" )
206- print (exported .graph )
207- exported = (
208- exported .run_decompositions ()
209- if "decall" in exporter
210- else exported .run_decompositions ({})
211- )
209+ if verbose >= 2 :
210+ exported = torch .export .export (
211+ model , inputs , dynamic_shapes = dynamic_shapes , strict = True
212+ )
213+ if verbose >= 9 :
214+ print ("-- graph before decomposition" )
215+ print (exported .graph )
216+ exported = (
217+ exported .run_decompositions ()
218+ if "decall" in exporter
219+ else exported .run_decompositions ({})
220+ )
221+ else :
222+ with contextlib .redirect_stdout (io .StringIO ()), contextlib .redirect_stderr (
223+ io .StringIO ()
224+ ):
225+ exported = torch .export .export (
226+ model , inputs , dynamic_shapes = dynamic_shapes , strict = True
227+ )
228+ if verbose >= 9 :
229+ print ("-- graph before decomposition" )
230+ print (exported .graph )
231+ exported = (
232+ exported .run_decompositions ()
233+ if "decall" in exporter
234+ else exported .run_decompositions ({})
235+ )
212236 except Exception as e :
213237 if not quiet :
214238 raise
@@ -219,9 +243,17 @@ def _make_exporter_export(
219243 return exported .module ()
220244 if exporter == "export-nostrict" :
221245 try :
222- exported = torch .export .export (
223- model , inputs , dynamic_shapes = dynamic_shapes , strict = False
224- )
246+ if verbose >= 2 :
247+ exported = torch .export .export (
248+ model , inputs , dynamic_shapes = dynamic_shapes , strict = False
249+ )
250+ else :
251+ with contextlib .redirect_stdout (io .StringIO ()), contextlib .redirect_stderr (
252+ io .StringIO ()
253+ ):
254+ exported = torch .export .export (
255+ model , inputs , dynamic_shapes = dynamic_shapes , strict = False
256+ )
225257 except Exception as e :
226258 if not quiet :
227259 raise
@@ -232,17 +264,33 @@ def _make_exporter_export(
232264 return exported .module ()
233265 if exporter in ("export-nostrict-dec" , "export-nostrict-decall" ):
234266 try :
235- exported = torch .export .export (
236- model , inputs , dynamic_shapes = dynamic_shapes , strict = False
237- )
238- if verbose >= 9 :
239- print ("-- graph before decomposition" )
240- print (exported .graph )
241- exported = (
242- exported .run_decompositions ()
243- if "decall" in exporter
244- else exported .run_decompositions ({})
245- )
267+ if verbose >= 2 :
268+ exported = torch .export .export (
269+ model , inputs , dynamic_shapes = dynamic_shapes , strict = False
270+ )
271+ if verbose >= 9 :
272+ print ("-- graph before decomposition" )
273+ print (exported .graph )
274+ exported = (
275+ exported .run_decompositions ()
276+ if "decall" in exporter
277+ else exported .run_decompositions ({})
278+ )
279+ else :
280+ with contextlib .redirect_stdout (io .StringIO ()), contextlib .redirect_stderr (
281+ io .StringIO ()
282+ ):
283+ exported = torch .export .export (
284+ model , inputs , dynamic_shapes = dynamic_shapes , strict = False
285+ )
286+ if verbose >= 9 :
287+ print ("-- graph before decomposition" )
288+ print (exported .graph )
289+ exported = (
290+ exported .run_decompositions ()
291+ if "decall" in exporter
292+ else exported .run_decompositions ({})
293+ )
246294 except Exception as e :
247295 if not quiet :
248296 raise
@@ -255,8 +303,15 @@ def _make_exporter_export(
255303 from experimental_experiment .torch_interpreter .tracing import CustomTracer
256304
257305 try :
258- graph = CustomTracer ().trace (model )
259- mod = torch .fx .GraphModule (model , graph )
306+ if verbose >= 2 :
307+ graph = CustomTracer ().trace (model )
308+ mod = torch .fx .GraphModule (model , graph )
309+ else :
310+ with contextlib .redirect_stdout (io .StringIO ()), contextlib .redirect_stderr (
311+ io .StringIO ()
312+ ):
313+ graph = CustomTracer ().trace (model )
314+ mod = torch .fx .GraphModule (model , graph )
260315 except Exception as e :
261316 if not quiet :
262317 raise
@@ -289,13 +344,25 @@ def _make_exporter_onnx(
289344 if "-dec" in exporter :
290345 opts ["decomposition_table" ] = "all" if "-decall" in exporter else "default"
291346 try :
292- onx , builder = to_onnx (
293- model ,
294- inputs ,
295- dynamic_shapes = dynamic_shapes ,
296- export_options = ExportOptions (** opts ),
297- return_builder = True ,
298- )
347+ if verbose >= 2 :
348+ onx , builder = to_onnx (
349+ model ,
350+ inputs ,
351+ dynamic_shapes = dynamic_shapes ,
352+ export_options = ExportOptions (** opts ),
353+ return_builder = True ,
354+ )
355+ else :
356+ with contextlib .redirect_stdout (io .StringIO ()), contextlib .redirect_stderr (
357+ io .StringIO ()
358+ ):
359+ onx , builder = to_onnx (
360+ model ,
361+ inputs ,
362+ dynamic_shapes = dynamic_shapes ,
363+ export_options = ExportOptions (** opts ),
364+ return_builder = True ,
365+ )
299366 except Exception as e :
300367 if not quiet :
301368 raise RuntimeError (
@@ -306,6 +373,7 @@ def _make_exporter_onnx(
306373 ) from e
307374 return dict (error = str (e ), success = 0 , error_step = "export" )
308375 return onx , builder
376+
309377 if exporter == "dynamo" :
310378 import torch
311379
@@ -338,6 +406,7 @@ def _make_exporter_onnx(
338406 ) from e
339407 return dict (error = str (e ), success = 0 , error_step = "export" )
340408 return onx , None
409+
341410 if exporter == "dynamo-ir" :
342411 import torch
343412
0 commit comments