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
23 changes: 20 additions & 3 deletions paddleformers/transformers/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2978,7 +2978,12 @@ def from_pretrained(cls, pretrained_model_name_or_path, *args, **kwargs):
with ContextManagers(init_contexts):
model = cls(config, *init_args, **model_kwargs)

if hasattr(cls, "_gen_aoa_config") and load_checkpoint_format == "flex_checkpoint":
if load_checkpoint_format == "flex_checkpoint":
if not hasattr(cls, "_gen_aoa_config"):
raise RuntimeError(
"When using flex_checkpoint to load Hugging Face open-source weights, "
"the model must implement the _gen_aoa_config function to provide checkpoint conversion rules."
)
aoa_config = cls._gen_aoa_config(config)
sharded_state_dict = model.sharded_state_dict()
dist.load_state_dict(
Expand Down Expand Up @@ -3216,8 +3221,20 @@ def save_pretrained(
# Only save the model in distributed training setup
model_to_save = unwrap_model(self)

if hasattr(self.__class__, "_gen_inv_aoa_config") and save_checkpoint_format == "flex_checkpoint":
aoa_config = self.__class__._gen_inv_aoa_config(model_to_save.config)
if save_checkpoint_format == "flex_checkpoint":
if not hasattr(self.__class__, "_gen_inv_aoa_config"):
if hasattr(self.__class__, "_gen_aoa_config"):
aoa_config = self.__class__._gen_aoa_config(model_to_save.config)
aoa_config["aoa_config_reverse"] = True
logger.warning("There is no _gen_inv_aoa_config, so we auto-derived it from _gen_aoa_config.")
else:
raise RuntimeError(
"When using flex_checkpoint to save Hugging Face weights, "
"the model must implement either the _gen_inv_aoa_config function "
"or the _gen_aoa_config function (which will be automatically used to derive _gen_inv_aoa_config)."
)
else:
aoa_config = self.__class__._gen_inv_aoa_config(model_to_save.config)

clean_unrelated_safetensors(save_dir)

Expand Down
Loading