Skip to content

[Code scan] Fix Hugging Face from_pretrained path handling #32

Description

@njzjz

This issue is a result of a Codex global repository scan.

Summary

The Hugging Face model constructors resolve weight and dictionary paths and always pass them into the UniMol backend. If resolve_weight_path or resolve_dict_path returns a missing default path or stale repo-relative path, the backend does not enter its auto-download path because pretrained_model_path is not None. _has_transformers_weights also only checks local directories, so Hub model ids or saved HF checkpoints can skip super().from_pretrained() and ignore Transformers weights.

Code references

def _has_transformers_weights(pretrained_model_name_or_path):
if not isinstance(pretrained_model_name_or_path, (str, os.PathLike)):
return False
path = os.fspath(pretrained_model_name_or_path)
if not os.path.isdir(path):
return False
filenames = {
WEIGHTS_NAME,
SAFE_WEIGHTS_NAME,
"pytorch_model.bin.index.json",
"model.safetensors.index.json",
}
return any(os.path.isfile(os.path.join(path, name)) for name in filenames)

@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
config = kwargs.pop("config", None)
if config is None:
config, kwargs = cls.config_class.from_pretrained(
pretrained_model_name_or_path,
return_unused_kwargs=True,
**kwargs,
)
config.pretrained_model_path = config.resolve_weight_path(pretrained_model_name_or_path)
config.pretrained_dict_path = config.resolve_dict_path(pretrained_model_name_or_path)
if _has_transformers_weights(pretrained_model_name_or_path):
return super().from_pretrained(
pretrained_model_name_or_path,
*model_args,
config=config,
**kwargs,
)
return cls(config, *model_args)
class UnimolModel(UnimolPreTrainedModel):
def __init__(self, config: UnimolConfig):
super().__init__(config)
weight_path = config.resolve_weight_path()
dict_path = config.resolve_dict_path()
self.unimol = UniMolBackend(
output_dim=config.num_labels,
data_type=config.data_type,
remove_hs=config.remove_hs,
pretrained_model_path=weight_path,
pretrained_dict_path=dict_path,
)

weight_path = config.resolve_weight_path()
dict_path = config.resolve_dict_path()
self.unimol = UniMolBackend(
output_dim=config.num_labels,
data_type=config.data_type,
remove_hs=config.remove_hs,
pretrained_model_path=weight_path,
pretrained_dict_path=dict_path,

weight_path = config.resolve_weight_path()
dict_path = config.resolve_dict_path()
self.unimol = UniMolBackend(
output_dim=config.num_labels,
data_type=config.data_type,
remove_hs=config.remove_hs,
pretrained_model_path=weight_path,
pretrained_dict_path=dict_path,
)

def resolve_weight_path(self, pretrained_model_name_or_path=None):
if self.pretrained_model_path:
return self._resolve_path(self.pretrained_model_path, pretrained_model_name_or_path)
base = pretrained_model_name_or_path or get_weight_dir()
if os.path.isfile(os.path.join(base, self.weight_name)):
return os.path.join(base, self.weight_name)
return os.path.join(get_weight_dir(), self.weight_name)
def resolve_dict_path(self, pretrained_model_name_or_path=None):
if self.pretrained_dict_path:
return self._resolve_path(self.pretrained_dict_path, pretrained_model_name_or_path)
base = pretrained_model_name_or_path or get_weight_dir()
if os.path.isfile(os.path.join(base, self.dict_name)):
return os.path.join(base, self.dict_name)
return os.path.join(get_weight_dir(), self.dict_name)

"pretrained_dict_path": "unimol_tools/unimol_tools/weights/mol.dict.txt",
"pretrained_model_path": "unimol_tools/unimol_tools/weights/mol_pre_all_h_220816.pt",

"pretrained_dict_path": "unimol_tools/unimol_tools/weights/mol.dict.txt",
"pretrained_model_path": "unimol_tools/unimol_tools/weights/mol_pre_no_h_220816.pt",

Impact

Packaged HF entries and saved Transformers checkpoints can fail to load on clean installs or silently ignore saved HF weights, depending on whether the stale paths exist locally.

Suggested fix

Use Transformers helpers such as cached_file or has_file to detect local and remote HF weights, delegate to super().from_pretrained() for real HF checkpoints, and avoid passing missing paths into the backend. Remove stale pretrained_*_path entries from bundled configs or resolve them to local packaged files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions