11import math
22import re
3+
34from importlib .metadata import version
45from typing import Any , Callable , List , Optional , Tuple , Type
56
67import packaging
78import torch
9+
810from torch import nn
911
1012from megatron .core import ModelParallelConfig , parallel_state
1820
1921try :
2022 from megatron .core .extensions .transformer_engine import (
23+ TEColumnParallelGroupedLinear ,
2124 TEColumnParallelLinear ,
2225 TELayerNormColumnParallelLinear ,
23- TEColumnParallelGroupedLinear ,
24- TERowParallelLinear ,
25- TERowParallelGroupedLinear ,
2626 TELinear ,
27+ TERowParallelGroupedLinear ,
28+ TERowParallelLinear ,
2729 )
30+
2831 HAVE_TE = True
2932except ImportError :
3033 HAVE_TE = False
3437
3538
3639def match_module (m , name , prefix , target_modules ):
37- """
38- """
40+ """ """
3941 full_name = f"{ prefix } .{ name } " if prefix else name
4042 for pattern in target_modules :
4143 if name == pattern or wildcard_match (pattern , full_name ):
@@ -117,7 +119,13 @@ def get_adapter_attributes_from_linear(m: nn.Module):
117119 else :
118120 raise NotImplementedError (f"Layer type is unrecognized for LoRA: { type (m )} " )
119121
120- return input_is_parallel , in_features , out_features , disable_sequence_parallel_comm , base_linear_is_parallel
122+ return (
123+ input_is_parallel ,
124+ in_features ,
125+ out_features ,
126+ disable_sequence_parallel_comm ,
127+ base_linear_is_parallel ,
128+ )
121129
122130
123131def is_expert_linear (fqn ):
@@ -188,7 +196,9 @@ def __init__(
188196 if model_parallel_config is None :
189197 model_parallel_config = ModelParallelConfig ()
190198 _sequence_parallel = model_parallel_config .sequence_parallel
191- model_parallel_config .sequence_parallel = False # SP is irrelevant for the lora linear layer
199+ model_parallel_config .sequence_parallel = (
200+ False # SP is irrelevant for the lora linear layer
201+ )
192202 self .config = model_parallel_config
193203
194204 if input_is_parallel :
@@ -215,7 +225,7 @@ def __init__(
215225 symmetric_ar_type = model_parallel_config .symmetric_ar_type ,
216226 skip_weight_param_allocation = False ,
217227 )
218-
228+
219229 if input_is_parallel :
220230 self .linear_out = TELinear (
221231 input_size = dim ,
@@ -246,7 +256,7 @@ def __init__(
246256 self .dropout = torch .nn .Dropout (p = dropout )
247257 else :
248258 self .dropout = None
249-
259+
250260 # revert config change in case it is read elsewhere
251261 model_parallel_config .sequence_parallel = _sequence_parallel
252262 self .disable_sequence_parallel_comm = disable_sequence_parallel_comm
@@ -271,26 +281,34 @@ def forward(self, x):
271281 if self .dropout is not None and self .dropout_position == 'pre' :
272282 x = self .dropout (x )
273283
274- if not self .disable_sequence_parallel_comm and not self .input_is_parallel and not self .is_expert :
284+ if (
285+ not self .disable_sequence_parallel_comm
286+ and not self .input_is_parallel
287+ and not self .is_expert
288+ ):
275289 # for attention_qkv and linear_fc1
276290 # layernorm before lora is impacted by sequence parallel,
277291 # hence seq dim need to be gathered right before lora linear layers
278292 # this function also handles the backward pass correctly
279293 x = gather_from_sequence_parallel_region (x )
280-
281- x , _ = self .linear_in (x )
282- x , _ = self .linear_out (x )
283294
284- if not self .disable_sequence_parallel_comm and self .input_is_parallel and not self .is_expert :
295+ x , _ = self .linear_in (x )
296+ x , _ = self .linear_out (x )
297+
298+ if (
299+ not self .disable_sequence_parallel_comm
300+ and self .input_is_parallel
301+ and not self .is_expert
302+ ):
285303 # for attention_dense and linear_fc2
286304 # layernorm after lora is impacted by sequence parallel,
287305 # hence seq dim need to be scattered right after lora linear layers
288306 # this function also handles the backward pass correctly
289307 x = scatter_to_sequence_parallel_region (x )
290-
308+
291309 if self .dropout is not None and self .dropout_position == 'post' :
292310 x = self .dropout (x )
293-
311+
294312 x = x * (self .alpha / self .dim )
295313
296314 return x
@@ -303,17 +321,21 @@ def sharded_state_dict(
303321 since TP is sharded separately for the two logical matrices (gate and up)
304322 """
305323 sharded_state_dict = {}
306- linear_in_sd = self .linear_in .sharded_state_dict (f"{ prefix } linear_in." , sharded_offsets , metadata )
307- linear_out_sd = self .linear_out .sharded_state_dict (f"{ prefix } linear_out." , sharded_offsets , metadata )
324+ linear_in_sd = self .linear_in .sharded_state_dict (
325+ f"{ prefix } linear_in." , sharded_offsets , metadata
326+ )
327+ linear_out_sd = self .linear_out .sharded_state_dict (
328+ f"{ prefix } linear_out." , sharded_offsets , metadata
329+ )
308330
309331 sharded_state_dict .update (linear_in_sd )
310332 sharded_state_dict .update (linear_out_sd )
311333 return sharded_state_dict
312-
334+
313335 def __repr__ (self ):
314336 return (
315337 f"{ type (self ).__name__ } (linear_in: in_features={ self .linear_in .in_features } , "
316338 f"out_features={ self .linear_in .out_features } , bias={ self .linear_in .use_bias } , TP={ self .linear_in .tp_size } ), "
317339 f"(linear_out: in_features={ self .linear_out .in_features } , "
318340 f"out_features={ self .linear_out .out_features } , bias={ self .linear_out .use_bias } , TP={ self .linear_out .tp_size } )"
319- )
341+ )
0 commit comments