@@ -1163,14 +1163,8 @@ def __init__(self, optimizer, shard_fn=None, gradient_accumulation_steps=1):
11631163 self ._mp_group = None
11641164 self .do_tensor_fusion_once = True
11651165 self ._strategy = Strategy ()
1166- self .enable_tensor_fusion = os .getenv ("FLAGS_enable_tensor_fusion" ) in [
1167- "True" ,
1168- "true" ,
1169- "1" ,
1170- ]
1171- self .enable_sharding_overlap = os .getenv (
1172- "FLAGS_enable_sharding_overlap"
1173- ) in ["True" , "true" , "1" ]
1166+ self .enable_tensor_fusion = False
1167+ self .enable_sharding_overlap = False
11741168
11751169 def _set_and_check_sharding_prop_from_param (self ):
11761170 global_mesh = fleet .auto .get_mesh ()
@@ -1507,14 +1501,14 @@ def get_mesh(pp_idx=0):
15071501 self .param_storage [idx ].is_sync = False
15081502
15091503 def _enable_tensor_fusion (self ):
1510- # TODO: enable after clear FLAGS_enable_tensor_fusion
1511- # self.enable_tensor_fusion = True
1512- pass
1504+ os . environ [ "FLAGS_enable_tensor_fusion" ] = "1"
1505+ self .enable_tensor_fusion = True
1506+ self . _shard_fn . _enable_tensor_fusion ()
15131507
15141508 def _enable_sharding_overlap (self , layers ):
15151509 if hasattr (layers , 'config' ) and layers .config .get ("to_static" , False ):
15161510 return
1517- # self.enable_sharding_overlap = True
1511+ self .enable_sharding_overlap = True
15181512 if not isinstance (layers , paddle .nn .Layer ):
15191513 raise RuntimeError (
15201514 f"`layers` must be `paddle.nn.Layer` but got { type (layers )} "
@@ -1971,15 +1965,19 @@ def __init__(self, mesh, sharding_mesh_dim):
19711965 self ._mesh = mesh
19721966 self ._sharding_axis = 0
19731967 self ._sharding_mesh_dim = sharding_mesh_dim
1968+ self .enable_tensor_fusion = False
19741969
19751970 def _set_sharding_axis (self , sharding_axis ):
19761971 self ._sharding_axis = sharding_axis
19771972
1973+ def _enable_tensor_fusion (self ):
1974+ self .enable_tensor_fusion = True
1975+
19781976 def shard_master_weight (
19791977 self , param : Tensor , master_weight : Tensor
19801978 ) -> Tensor :
19811979 if param .is_dist ():
1982- if os . getenv ( "FLAGS_enable_tensor_fusion" ) in [ "True" , "true" , "1" ] :
1980+ if self . enable_tensor_fusion :
19831981 placements = param .placements
19841982 else :
19851983 placements = get_placement_with_sharding (
@@ -2115,10 +2113,7 @@ def __call__(self, key: str, param: Tensor, tensor: Tensor) -> Tensor:
21152113 return tensor
21162114
21172115 # Only deal with momentum in optimizer, beta should be replicated cross param's mesh
2118- if (
2119- os .getenv ("FLAGS_enable_tensor_fusion" ) not in ["True" , "true" , "1" ]
2120- and 'beta' not in key
2121- ):
2116+ if not self .enable_tensor_fusion and 'beta' not in key :
21222117 placements = get_placement_with_sharding (param , self ._sharding_axis )
21232118 else :
21242119 placements = [
0 commit comments