@@ -511,8 +511,9 @@ def store_pos_in_caller(self, pos: StorePos,
511511 raise NotImplementedError
512512
513513 def merge_call (self , state : 'State' , stack_objs : list [Any ]) -> None :
514- print ("to merge graph" , state .fx_graph .result_graph )
515- print ("to merge frameid" , state .frame_id , self .frame_id )
514+ if config .get_config ('debug' ):
515+ print ("to merge graph" , state .fx_graph .result_graph )
516+ print ("to merge frameid" , state .frame_id , self .frame_id )
516517 # self.written = True
517518 # self.defer_restart = None
518519 replacement_mapping : dict [torch .fx .Node , torch .fx .Node ] = {}
@@ -562,9 +563,9 @@ def merge_call_guard() -> None:
562563 ExtractFromMethod , ExtractFromFunction )):
563564 self_pos = self .store_pos_in_caller (pos , idx )
564565 if self_pos is None :
565- print (
566- "\033 [34m[warning] cannot find store pos in caller, skip guard check\033 [0m" ,
567- type (var ), var .extract_code_at_start )
566+ # print(
567+ # "\033[34m[warning] cannot find store pos in caller, skip guard check\033[0m",
568+ # type(var), var.extract_code_at_start)
568569 new_var .need_guard_check = False
569570 else :
570571 new_var .extract_code_at_start .append (self_pos )
@@ -1169,8 +1170,9 @@ def commit(self) -> None:
11691170 end_pc = self .code .get_orig_pc (lasti )
11701171 if end_pc == - 1 :
11711172 end_pc = self .code .get_next_orig_pc (lasti )
1172- print ("commiting" , self .frame_id , self .state .start_pc , end_pc ,
1173- self .code .original_insts [end_pc ], lasti )
1173+ if config .get_config ('debug' ):
1174+ print ("commiting" , self .frame_id , self .state .start_pc , end_pc ,
1175+ self .code .original_insts [end_pc ], lasti )
11741176 # TODO: can be optimized by only reproduce the modified variables
11751177 if self .state .defer_restart is not None :
11761178 stack_objs = self .state .defer_restart .stack_objs
@@ -1180,14 +1182,16 @@ def commit(self) -> None:
11801182
11811183 if self .state .start_pc == 0 and self .code .original_insts [
11821184 end_pc ].opname == "RETURN_VALUE" and self .caller is not None :
1183- print ("callee is full graph, merge to caller" )
1185+ if config .get_config ('debug' ):
1186+ print ("callee is full graph, merge to caller" )
11841187 assert len (stack_objs ) == 1
11851188 caller = self .caller
11861189 assert caller is not None
11871190 caller .state .merge_call (self .state ,
11881191 [get_value_stack_from_top (self .frame , 0 )])
11891192 elif self .cf_info is not None and self .num_breaks == 1 and self .cf_info .end_pc == end_pc :
1190- print ("reach end of nested tracker, merge to caller" )
1193+ if config .get_config ('debug' ):
1194+ print ("reach end of nested tracker, merge to caller" )
11911195 self .rewrite_loop_graph ()
11921196 stack_objs = get_all_objects_in_stack (self .frame )
11931197 nest_caller = self .caller
@@ -1257,18 +1261,19 @@ def commit(self) -> None:
12571261
12581262 self .state .fx_graph .set_output_nodes (
12591263 graph_codegen .get_graph_outputs ())
1260- print ("graph input" , [
1261- (name , x ) for x , name in self .state .fx_graph .example_inputs
1262- ])
1263- print ("graph" , self .state .fx_graph .result_graph )
1264- from .control_flow import CondModule
1265- for node in self .state .fx_graph .result_graph .nodes :
1266- if node .op == 'call_module' and '.' not in node .target :
1267- mod = getattr (self .state .root , node .target )
1268- if isinstance (mod , CondModule ):
1269- print ("CondModule:" , node .target )
1270- print ("true_body:" , mod .true_body .graph )
1271- print ("false_body:" , mod .false_body .graph )
1264+ if config .get_config ('debug' ):
1265+ print ("graph input" ,
1266+ [(name , x )
1267+ for x , name in self .state .fx_graph .example_inputs ])
1268+ print ("graph" , self .state .fx_graph .result_graph )
1269+ from .control_flow import CondModule
1270+ for node in self .state .fx_graph .result_graph .nodes :
1271+ if node .op == 'call_module' and '.' not in node .target :
1272+ mod = getattr (self .state .root , node .target )
1273+ if isinstance (mod , CondModule ):
1274+ print ("CondModule:" , node .target )
1275+ print ("true_body:" , mod .true_body .graph )
1276+ print ("false_body:" , mod .false_body .graph )
12721277
12731278 graph_code = graph_codegen .get_code ()
12741279 compiled_graph = self .state .fx_graph .compile ()
@@ -1278,16 +1283,19 @@ def commit(self) -> None:
12781283{ guard_code }
12791284 """
12801285 out : Dict [str , Any ] = dict ()
1281- print ("RUNNING PY CODE" )
1282- print (py_code )
1286+ if config .get_config ('debug' ):
1287+ print ("RUNNING PY CODE" )
1288+ print (py_code )
12831289 exec (py_code , self .frame .f_globals , out )
12841290 guard_fn = out ["___make_guard_fn" ](* guard_codegen .objs .values ())
12851291 graph_fn = out ["___make_graph_fn" ](compiled_graph ,
12861292 * graph_codegen .objs .values ())
12871293
1288- print ("guard_fn:" , guard_fn )
1289- print ("pc:" , self .state .start_pc , end_pc )
1290- print ("stack:" , self .state .start_stack_size , len (stack_objs ))
1294+ if config .get_config ('debug' ):
1295+ print ("guard_fn:" , guard_fn )
1296+ print ("pc:" , self .state .start_pc , end_pc )
1297+ print ("stack:" , self .state .start_stack_size ,
1298+ len (stack_objs ))
12911299
12921300 get_frame_cache (self .frame_id ).add (
12931301 CachedGraph (
@@ -1526,7 +1534,8 @@ def make_sub_var(value: Any, fx_node: torch.fx.Node) -> None:
15261534 self .state .defer_restart = None
15271535
15281536 def restart (self , restart_reason : str , restart_caller : bool = True ) -> None :
1529- print (f"restart: { restart_reason } " )
1537+ if config .get_config ('debug' ):
1538+ print (f"restart: { restart_reason } " )
15301539 self .have_error = True
15311540 self .num_breaks += 1
15321541 self .commit ()
@@ -1585,7 +1594,7 @@ def is_builtin_func(self, func: Callable[..., Any]) -> bool:
15851594 str .split , sorted )
15861595
15871596 def is_numpy_constant_func (self , func : Callable [..., Any ]) -> bool :
1588- print (dir (func ))
1597+ # print(dir(func))
15891598 if (hasattr (func , '__module__' ) and 'numpy' in func .__module__ and
15901599 'random' not in func .__module__ ):
15911600 return True
@@ -1741,7 +1750,8 @@ def call_function(
17411750 ]
17421751 })
17431752 return
1744- print ("run into user defined function" , func )
1753+ if config .get_config ('debug' ):
1754+ print ("run into user defined function" , func )
17451755 stack_objs = get_all_objects_in_stack (self .frame )
17461756 self .state .mark_calling_func (func )
17471757 self .state .mark_defer_restart (
@@ -1825,7 +1835,8 @@ def set_if_inplace_return() -> None:
18251835 elif hasattr (func , "__self__" ) and isinstance (
18261836 func .__self__ , torch .autograd .profiler .record_function ):
18271837 return
1828- print ("record function in graph" , func )
1838+ if config .get_config ("debug" ):
1839+ print ("record function in graph" , func )
18291840 self .state .record_function (
18301841 func ,
18311842 args ,
@@ -2772,15 +2783,17 @@ def push_tracker(frame: FrameType,
27722783 caller = None
27732784 new_tracker = GuardTracker (frame , frame_id , caller , read_stack , cf_info )
27742785 trackers .append (new_tracker )
2775- print ("push tracker" , frame_id , "frame" , hex (id (frame )),
2776- "frame_id" , frame_id , "read_stack" , read_stack , "cf_info" ,
2777- type (cf_info ), "all" , [t .frame_id for t in trackers ])
2786+ if config .get_config ('debug' ):
2787+ print ("push tracker" , frame_id , "frame" , hex (id (frame )),
2788+ "frame_id" , frame_id , "read_stack" , read_stack , "cf_info" ,
2789+ type (cf_info ), "all" , [t .frame_id for t in trackers ])
27782790 return new_tracker
27792791
27802792
27812793def pop_tracker (frame_id : int ) -> None :
2782- print ("before pop_tracker" , [t .frame_id for t in trackers ], "frame_id" ,
2783- frame_id )
2794+ if config .get_config ('debug' ):
2795+ print ("before pop_tracker" , [t .frame_id for t in trackers ], "frame_id" ,
2796+ frame_id )
27842797 to_pop = trackers .pop ()
27852798 if not get_config ("enable_fallback" ):
27862799 assert to_pop .frame_id == frame_id
@@ -2790,7 +2803,7 @@ def pop_tracker(frame_id: int) -> None:
27902803def record (frame : FrameType , frame_id : int ) -> None :
27912804 if id (frame ) != id (trackers [- 1 ].frame ):
27922805 if trackers [- 1 ].state .calling_func is not None :
2793- print ("push tracker due to record" )
2806+ # print("push tracker due to record")
27942807 push_tracker (frame , frame_id )
27952808 trackers [- 1 ].record (frame , frame_id )
27962809
0 commit comments