CoolFace
Modelpublic

Synthyra/ESM2-8M

sourceHugging Facemitupdated 1d agoView on Hugging Face
4likes560downloads
modeling_fastplms.py194 linesDownload Raw Back to root
1"""Generated bridge to the embedded FastPLMs runtime sources."""2 3import base644import hashlib5import importlib6import importlib.util7import sys8import tempfile9from io import BytesIO10from pathlib import Path11from typing import ClassVar12from zipfile import ZIP_DEFLATED, ZipFile13 14from .fastplms_bundle import RUNTIME_DATA, RUNTIME_HASH15 16if RUNTIME_HASH != "0e257efaa72f739cd17377c961647c63b9a74f0654ef5f8570e299d938e2cacb":17    raise RuntimeError("FastPLMs runtime identity differs from the bridge.")18 19_RUNTIME_TEMPORARIES = []20 21def _archive_runtime_hashes(payload):22    result = {}23    with ZipFile(BytesIO(payload)) as archive:24        for member in archive.infolist():25            name = member.filename26            parts = Path(name).parts27            if (28                member.is_dir()29                or "\\" in name30                or not parts31                or parts[0] != "fastplms"32                or len(parts) < 233                or any(part in {"", ".", ".."} for part in parts)34                or Path(name).suffix in {".pyc", ".pyo"}35                or member.flag_bits & 0x136                or member.compress_type != ZIP_DEFLATED37                or member.external_attr >> 16 != 0o10064438            ):39                raise RuntimeError("Embedded FastPLMs archive has an unsafe path.")40            relative = Path(*parts[1:]).as_posix()41            if relative in result:42                raise RuntimeError("Embedded FastPLMs archive repeats a path.")43            result[relative] = hashlib.sha256(archive.read(member)).hexdigest()44    return result45 46def _ensure_runtime():47    payload = base64.b85decode("".join(RUNTIME_DATA))48    if hashlib.sha256(payload).hexdigest() != RUNTIME_HASH:49        raise RuntimeError("Embedded FastPLMs runtime hash mismatch.")50    expected = _archive_runtime_hashes(payload)51    temporary = tempfile.TemporaryDirectory(prefix="fastplms-artifact-runtime-")52    try:53        runtime_root = Path(temporary.name)54        with ZipFile(BytesIO(payload)) as archive:55            for member in archive.infolist():56                target = runtime_root.joinpath(*Path(member.filename).parts)57                target.parent.mkdir(parents=True, exist_ok=True)58                with target.open("xb") as handle:59                    handle.write(archive.read(member))60        package_root = runtime_root / "fastplms"61        if _runtime_file_hashes(package_root) != expected:62            raise RuntimeError(63                "Private FastPLMs runtime differs from the embedded archive."64            )65    except BaseException:66        temporary.cleanup()67        raise68    _RUNTIME_TEMPORARIES.append(temporary)69    return package_root70 71def _runtime_file_hashes(package_root):72    result = {}73    for path in sorted(package_root.rglob("*")):74        relative = path.relative_to(package_root)75        if path.is_symlink():76            raise RuntimeError("Private FastPLMs runtime contains a symlink.")77        if path.is_dir():78            continue79        if path.suffix in {".pyc", ".pyo"}:80            raise RuntimeError("Private FastPLMs runtime contains bytecode.")81        if not path.is_file():82            raise RuntimeError("Private FastPLMs runtime contains a non-file entry.")83        result[relative.as_posix()] = hashlib.sha256(path.read_bytes()).hexdigest()84    return result85 86def _extend_loaded_package_paths(package_root):87    for name, module in list(sys.modules.items()):88        if name != "fastplms" and not name.startswith("fastplms."):89            continue90        paths = getattr(module, "__path__", None)91        if paths is None:92            continue93        relative = name.split(".")[1:]94        candidate = package_root.joinpath(*relative)95        candidate_text = str(candidate)96        if candidate.is_dir() and candidate_text not in paths:97            paths.append(candidate_text)98 99def _merge_runtime(package, package_root):100    incoming = _runtime_file_hashes(package_root)101    known = getattr(package, "__fastplms_artifact_runtime_files__", None)102    if not isinstance(known, dict):103        raise RuntimeError(104            "A non-artifact fastplms module is already loaded. Load the Hub artifact "105            "in a separate Python process."106        )107    conflicts = sorted(108        relative109        for relative, digest in incoming.items()110        if relative in known and known[relative] != digest111    )112    if conflicts:113        raise RuntimeError(114            "FastPLMs artifacts contain incompatible runtime sources at "115            + ", ".join(repr(path) for path in conflicts[:5])116            + ". Load incompatible releases in separate Python processes."117        )118    known = dict(known)119    known.update(incoming)120    package.__fastplms_artifact_runtime_files__ = known121    roots = list(getattr(package, "__fastplms_artifact_runtime_roots__", ()))122    if str(package_root) not in roots:123        roots.append(str(package_root))124    package.__fastplms_artifact_runtime_roots__ = tuple(roots)125    temporaries = list(126        getattr(package, "__fastplms_artifact_runtime_temporaries__", ())127    )128    for temporary in _RUNTIME_TEMPORARIES:129        if temporary not in temporaries:130            temporaries.append(temporary)131    package.__fastplms_artifact_runtime_temporaries__ = tuple(temporaries)132    hashes = set(getattr(package, "__fastplms_artifact_runtime_hashes__", ()))133    hashes.add(RUNTIME_HASH)134    package.__fastplms_artifact_runtime_hashes__ = frozenset(hashes)135    _extend_loaded_package_paths(package_root)136    return package137 138def _import_without_bytecode(module_name):139    previous = sys.dont_write_bytecode140    sys.dont_write_bytecode = True141    try:142        return importlib.import_module(module_name)143    finally:144        sys.dont_write_bytecode = previous145 146def _install_runtime():147    package = sys.modules.get("fastplms")148    hashes = getattr(package, "__fastplms_artifact_runtime_hashes__", ())149    if RUNTIME_HASH in hashes:150        return package151    package_root = _ensure_runtime()152    if package is not None:153        return _merge_runtime(package, package_root)154    spec = importlib.util.spec_from_file_location(155        "fastplms",156        package_root / "__init__.py",157        submodule_search_locations=[str(package_root)],158    )159    if spec is None or spec.loader is None:160        raise ImportError("Unable to load the embedded FastPLMs runtime.")161    package = importlib.util.module_from_spec(spec)162    package.__fastplms_artifact_runtime_hash__ = RUNTIME_HASH163    package.__fastplms_artifact_runtime_hashes__ = frozenset({RUNTIME_HASH})164    package.__fastplms_artifact_runtime_files__ = _runtime_file_hashes(package_root)165    package.__fastplms_artifact_runtime_roots__ = (str(package_root),)166    package.__fastplms_artifact_runtime_temporaries__ = tuple(167        _RUNTIME_TEMPORARIES168    )169    sys.modules["fastplms"] = package170    previous = sys.dont_write_bytecode171    sys.dont_write_bytecode = True172    try:173        try:174            spec.loader.exec_module(package)175        except BaseException:176            sys.modules.pop("fastplms", None)177            raise178    finally:179        sys.dont_write_bytecode = previous180    return package181 182_install_runtime()183_module_182 = _import_without_bytecode("fastplms.models.esm2.modeling_fastesm")184FastEsmConfig = _module_182.FastEsmConfig185FastEsmConfig.__module__ = __name__186FastEsmForMaskedLM = _module_182.FastEsmForMaskedLM187FastEsmForMaskedLM.__module__ = __name__188FastEsmForSequenceClassification = _module_182.FastEsmForSequenceClassification189FastEsmForSequenceClassification.__module__ = __name__190FastEsmForTokenClassification = _module_182.FastEsmForTokenClassification191FastEsmForTokenClassification.__module__ = __name__192FastEsmModel = _module_182.FastEsmModel193FastEsmModel.__module__ = __name__194