fix: gradient checkpointing compat for transformers 4.40.2 (OOM fix)
Browse files
root_gainlora/src/t5_gainlora_inflora.py
CHANGED
|
@@ -1006,9 +1006,11 @@ class T5PreTrainedModel(PreTrainedModel):
|
|
| 1006 |
if module.has_relative_attention_bias:
|
| 1007 |
module.relative_attention_bias.weight.data.normal_(mean=0.0, std=factor * ((d_model) ** -0.5))
|
| 1008 |
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
|
|
|
|
|
|
| 1012 |
|
| 1013 |
def _shift_right(self, input_ids):
|
| 1014 |
decoder_start_token_id = self.config.decoder_start_token_id
|
|
@@ -1415,18 +1417,34 @@ class T5Stack(T5PreTrainedModel):
|
|
| 1415 |
|
| 1416 |
return custom_forward
|
| 1417 |
|
| 1418 |
-
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
-
|
| 1422 |
-
|
| 1423 |
-
|
| 1424 |
-
|
| 1425 |
-
|
| 1426 |
-
|
| 1427 |
-
|
| 1428 |
-
|
| 1429 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1430 |
else:
|
| 1431 |
layer_outputs = layer_module(
|
| 1432 |
hidden_states,
|
|
|
|
| 1006 |
if module.has_relative_attention_bias:
|
| 1007 |
module.relative_attention_bias.weight.data.normal_(mean=0.0, std=factor * ((d_model) ** -0.5))
|
| 1008 |
|
| 1009 |
+
# NOTE: _set_gradient_checkpointing removed intentionally.
|
| 1010 |
+
# The old format (with 'value' param) causes transformers to silently ignore
|
| 1011 |
+
# gradient_checkpointing_kwargs (including use_reentrant=False).
|
| 1012 |
+
# Without this method, transformers uses the new format which properly
|
| 1013 |
+
# passes the checkpointing function with use_reentrant=False.
|
| 1014 |
|
| 1015 |
def _shift_right(self, input_ids):
|
| 1016 |
decoder_start_token_id = self.config.decoder_start_token_id
|
|
|
|
| 1417 |
|
| 1418 |
return custom_forward
|
| 1419 |
|
| 1420 |
+
gc_fn = getattr(self, '_gradient_checkpointing_func', None)
|
| 1421 |
+
if gc_fn is not None:
|
| 1422 |
+
layer_outputs = gc_fn(
|
| 1423 |
+
create_custom_forward(layer_module),
|
| 1424 |
+
hidden_states,
|
| 1425 |
+
extended_attention_mask,
|
| 1426 |
+
position_bias,
|
| 1427 |
+
encoder_hidden_states,
|
| 1428 |
+
encoder_extended_attention_mask,
|
| 1429 |
+
encoder_decoder_position_bias,
|
| 1430 |
+
layer_head_mask,
|
| 1431 |
+
cross_attn_layer_head_mask,
|
| 1432 |
+
None, # past_key_value is always None with gradient checkpointing
|
| 1433 |
+
)
|
| 1434 |
+
else:
|
| 1435 |
+
layer_outputs = checkpoint(
|
| 1436 |
+
create_custom_forward(layer_module),
|
| 1437 |
+
hidden_states,
|
| 1438 |
+
extended_attention_mask,
|
| 1439 |
+
position_bias,
|
| 1440 |
+
encoder_hidden_states,
|
| 1441 |
+
encoder_extended_attention_mask,
|
| 1442 |
+
encoder_decoder_position_bias,
|
| 1443 |
+
layer_head_mask,
|
| 1444 |
+
cross_attn_layer_head_mask,
|
| 1445 |
+
None,
|
| 1446 |
+
use_reentrant=False,
|
| 1447 |
+
)
|
| 1448 |
else:
|
| 1449 |
layer_outputs = layer_module(
|
| 1450 |
hidden_states,
|