@@ -85,12 +85,21 @@ def __init__(self, regular_linear_layer, lora_scale=1, network_alpha=None, rank=
8585
8686 self .lora_scale = lora_scale
8787
88+ # overwrite PyTorch's `state_dict` to be sure that only the 'regular_linear_layer' weights are saved
89+ # when saving the whole text encoder model and when LoRA is unloaded or fused
90+ def state_dict (self , * args , destination = None , prefix = "" , keep_vars = False ):
91+ if self .lora_linear_layer is None :
92+ return self .regular_linear_layer .state_dict (
93+ * args , destination = destination , prefix = prefix , keep_vars = keep_vars
94+ )
95+
96+ return super ().state_dict (* args , destination = destination , prefix = prefix , keep_vars = keep_vars )
97+
8898 def _fuse_lora (self ):
8999 if self .lora_linear_layer is None :
90100 return
91101
92102 dtype , device = self .regular_linear_layer .weight .data .dtype , self .regular_linear_layer .weight .data .device
93- logger .info (f"Fusing LoRA weights for { self .__class__ } " )
94103
95104 w_orig = self .regular_linear_layer .weight .data .float ()
96105 w_up = self .lora_linear_layer .up .weight .data .float ()
@@ -112,14 +121,14 @@ def _fuse_lora(self):
112121 def _unfuse_lora (self ):
113122 if not (hasattr (self , "w_up" ) and hasattr (self , "w_down" )):
114123 return
115- logger .info (f"Unfusing LoRA weights for { self .__class__ } " )
116124
117125 fused_weight = self .regular_linear_layer .weight .data
118126 dtype , device = fused_weight .dtype , fused_weight .device
119127
120- self .w_up = self .w_up .to (device = device , dtype = dtype )
121- self .w_down = self .w_down .to (device , dtype = dtype )
122- unfused_weight = fused_weight - torch .bmm (self .w_up [None , :], self .w_down [None , :])[0 ]
128+ w_up = self .w_up .to (device = device ).float ()
129+ w_down = self .w_down .to (device ).float ()
130+
131+ unfused_weight = fused_weight .float () - torch .bmm (w_up [None , :], w_down [None , :])[0 ]
123132 self .regular_linear_layer .weight .data = unfused_weight .to (device = device , dtype = dtype )
124133
125134 self .w_up = None
@@ -1405,15 +1414,15 @@ def _remove_text_encoder_monkey_patch(self):
14051414 def _remove_text_encoder_monkey_patch_classmethod (cls , text_encoder ):
14061415 for _ , attn_module in text_encoder_attn_modules (text_encoder ):
14071416 if isinstance (attn_module .q_proj , PatchedLoraProjection ):
1408- attn_module .q_proj = attn_module . q_proj . regular_linear_layer
1409- attn_module .k_proj = attn_module . k_proj . regular_linear_layer
1410- attn_module .v_proj = attn_module . v_proj . regular_linear_layer
1411- attn_module .out_proj = attn_module . out_proj . regular_linear_layer
1417+ attn_module .q_proj . lora_linear_layer = None
1418+ attn_module .k_proj . lora_linear_layer = None
1419+ attn_module .v_proj . lora_linear_layer = None
1420+ attn_module .out_proj . lora_linear_layer = None
14121421
14131422 for _ , mlp_module in text_encoder_mlp_modules (text_encoder ):
14141423 if isinstance (mlp_module .fc1 , PatchedLoraProjection ):
1415- mlp_module .fc1 = mlp_module . fc1 . regular_linear_layer
1416- mlp_module .fc2 = mlp_module . fc2 . regular_linear_layer
1424+ mlp_module .fc1 . lora_linear_layer = None
1425+ mlp_module .fc2 . lora_linear_layer = None
14171426
14181427 @classmethod
14191428 def _modify_text_encoder (
@@ -1447,23 +1456,43 @@ def _modify_text_encoder(
14471456 else :
14481457 current_rank = rank
14491458
1459+ q_linear_layer = (
1460+ attn_module .q_proj .regular_linear_layer
1461+ if isinstance (attn_module .q_proj , PatchedLoraProjection )
1462+ else attn_module .q_proj
1463+ )
14501464 attn_module .q_proj = PatchedLoraProjection (
1451- attn_module . q_proj , lora_scale , network_alpha = query_alpha , rank = current_rank , dtype = dtype
1465+ q_linear_layer , lora_scale , network_alpha = query_alpha , rank = current_rank , dtype = dtype
14521466 )
14531467 lora_parameters .extend (attn_module .q_proj .lora_linear_layer .parameters ())
14541468
1469+ k_linear_layer = (
1470+ attn_module .k_proj .regular_linear_layer
1471+ if isinstance (attn_module .k_proj , PatchedLoraProjection )
1472+ else attn_module .k_proj
1473+ )
14551474 attn_module .k_proj = PatchedLoraProjection (
1456- attn_module . k_proj , lora_scale , network_alpha = key_alpha , rank = current_rank , dtype = dtype
1475+ k_linear_layer , lora_scale , network_alpha = key_alpha , rank = current_rank , dtype = dtype
14571476 )
14581477 lora_parameters .extend (attn_module .k_proj .lora_linear_layer .parameters ())
14591478
1479+ v_linear_layer = (
1480+ attn_module .v_proj .regular_linear_layer
1481+ if isinstance (attn_module .v_proj , PatchedLoraProjection )
1482+ else attn_module .v_proj
1483+ )
14601484 attn_module .v_proj = PatchedLoraProjection (
1461- attn_module . v_proj , lora_scale , network_alpha = value_alpha , rank = current_rank , dtype = dtype
1485+ v_linear_layer , lora_scale , network_alpha = value_alpha , rank = current_rank , dtype = dtype
14621486 )
14631487 lora_parameters .extend (attn_module .v_proj .lora_linear_layer .parameters ())
14641488
1489+ out_linear_layer = (
1490+ attn_module .out_proj .regular_linear_layer
1491+ if isinstance (attn_module .out_proj , PatchedLoraProjection )
1492+ else attn_module .out_proj
1493+ )
14651494 attn_module .out_proj = PatchedLoraProjection (
1466- attn_module . out_proj , lora_scale , network_alpha = out_alpha , rank = current_rank , dtype = dtype
1495+ out_linear_layer , lora_scale , network_alpha = out_alpha , rank = current_rank , dtype = dtype
14671496 )
14681497 lora_parameters .extend (attn_module .out_proj .lora_linear_layer .parameters ())
14691498
@@ -1475,13 +1504,23 @@ def _modify_text_encoder(
14751504 current_rank_fc1 = rank .pop (f"{ name } .fc1.lora_linear_layer.up.weight" )
14761505 current_rank_fc2 = rank .pop (f"{ name } .fc2.lora_linear_layer.up.weight" )
14771506
1507+ fc1_linear_layer = (
1508+ mlp_module .fc1 .regular_linear_layer
1509+ if isinstance (mlp_module .fc1 , PatchedLoraProjection )
1510+ else mlp_module .fc1
1511+ )
14781512 mlp_module .fc1 = PatchedLoraProjection (
1479- mlp_module . fc1 , lora_scale , network_alpha = fc1_alpha , rank = current_rank_fc1 , dtype = dtype
1513+ fc1_linear_layer , lora_scale , network_alpha = fc1_alpha , rank = current_rank_fc1 , dtype = dtype
14801514 )
14811515 lora_parameters .extend (mlp_module .fc1 .lora_linear_layer .parameters ())
14821516
1517+ fc2_linear_layer = (
1518+ mlp_module .fc2 .regular_linear_layer
1519+ if isinstance (mlp_module .fc2 , PatchedLoraProjection )
1520+ else mlp_module .fc2
1521+ )
14831522 mlp_module .fc2 = PatchedLoraProjection (
1484- mlp_module . fc2 , lora_scale , network_alpha = fc2_alpha , rank = current_rank_fc2 , dtype = dtype
1523+ fc2_linear_layer , lora_scale , network_alpha = fc2_alpha , rank = current_rank_fc2 , dtype = dtype
14851524 )
14861525 lora_parameters .extend (mlp_module .fc2 .lora_linear_layer .parameters ())
14871526
0 commit comments