Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/transformers/models/longt5/modeling_longt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,36 @@ def dummy_inputs(self):
}
return dummy_inputs

def _try_load_missing_tied_module(self, key):
module = self
if key.endswith(".weight"):
key = key[: -len(".weight")]
for sub_key in key.split("."):
if not hasattr(module, sub_key):
return
module = getattr(module, sub_key)

self._tie_or_clone_weights(module, self.shared)

@classmethod
def from_pretrained(self, *args, **kwargs):
requested_loading_info = kwargs.get("output_loading_info", False)
kwargs["output_loading_info"] = True
model, loading_info = super().from_pretrained(*args, **kwargs)
missing_keys = loading_info.get("missing_keys", [])

if hasattr(model, "shared") and hasattr(model, "_tied_weights_keys"):
for missing_key in missing_keys:
logger.warning(
f"Recovering a missing tied weight {missing_key} from a legacy LongT5 checkpoint. "
f"Consider saving {missing_key} in your checkpoint or updating the config (tie_word_embeddings=true)."
)
model._try_load_missing_tied_module(missing_key)

if requested_loading_info:
return model, loading_info
return model

def _init_weights(self, module):
"""Initialize the weights"""
factor = self.config.initializer_factor # Used for testing weights initialization
Expand Down