File size: 7,647 Bytes
443b6bc
b593054
 
 
 
 
 
 
 
 
 
 
 
 
443b6bc
b593054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443b6bc
b593054
443b6bc
 
 
 
 
 
b593054
 
 
 
 
 
 
 
 
 
 
443b6bc
b593054
443b6bc
 
b593054
 
443b6bc
b593054
443b6bc
b593054
 
 
 
443b6bc
 
b593054
443b6bc
b593054
443b6bc
b593054
 
 
 
 
 
 
 
 
 
443b6bc
 
b593054
443b6bc
b593054
443b6bc
 
b593054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443b6bc
 
b593054
443b6bc
b593054
443b6bc
b593054
443b6bc
b593054
443b6bc
b593054
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Generated bridge to the embedded FastPLMs runtime sources."""

import base64
import hashlib
import importlib
import importlib.util
import sys
import tempfile
from io import BytesIO
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile

from .fastplms_bundle import RUNTIME_DATA, RUNTIME_HASH

if RUNTIME_HASH != "a8dda20d6a4a55e6808c7c2f2c9477c113edaa7d6ec6ef57f4a898cd47b73d48":
    raise RuntimeError("FastPLMs runtime identity differs from the bridge.")

_RUNTIME_TEMPORARIES = []

def _archive_runtime_hashes(payload):
    result = {}
    with ZipFile(BytesIO(payload)) as archive:
        for member in archive.infolist():
            name = member.filename
            parts = Path(name).parts
            if (
                member.is_dir()
                or "\\" in name
                or not parts
                or parts[0] != "fastplms"
                or len(parts) < 2
                or any(part in {"", ".", ".."} for part in parts)
                or Path(name).suffix in {".pyc", ".pyo"}
                or member.flag_bits & 0x1
                or member.compress_type != ZIP_DEFLATED
                or member.external_attr >> 16 != 0o100644
            ):
                raise RuntimeError("Embedded FastPLMs archive has an unsafe path.")
            relative = Path(*parts[1:]).as_posix()
            if relative in result:
                raise RuntimeError("Embedded FastPLMs archive repeats a path.")
            result[relative] = hashlib.sha256(archive.read(member)).hexdigest()
    return result

def _ensure_runtime():
    payload = base64.b85decode("".join(RUNTIME_DATA))
    if hashlib.sha256(payload).hexdigest() != RUNTIME_HASH:
        raise RuntimeError("Embedded FastPLMs runtime hash mismatch.")
    expected = _archive_runtime_hashes(payload)
    temporary = tempfile.TemporaryDirectory(prefix="fastplms-artifact-runtime-")
    try:
        runtime_root = Path(temporary.name)
        with ZipFile(BytesIO(payload)) as archive:
            for member in archive.infolist():
                target = runtime_root.joinpath(*Path(member.filename).parts)
                target.parent.mkdir(parents=True, exist_ok=True)
                with target.open("xb") as handle:
                    handle.write(archive.read(member))
        package_root = runtime_root / "fastplms"
        if _runtime_file_hashes(package_root) != expected:
            raise RuntimeError(
                "Private FastPLMs runtime differs from the embedded archive."
            )
    except BaseException:
        temporary.cleanup()
        raise
    _RUNTIME_TEMPORARIES.append(temporary)
    return package_root

def _runtime_file_hashes(package_root):
    result = {}
    for path in sorted(package_root.rglob("*")):
        relative = path.relative_to(package_root)
        if path.is_symlink():
            raise RuntimeError("Private FastPLMs runtime contains a symlink.")
        if path.is_dir():
            continue
        if path.suffix in {".pyc", ".pyo"}:
            raise RuntimeError("Private FastPLMs runtime contains bytecode.")
        if not path.is_file():
            raise RuntimeError("Private FastPLMs runtime contains a non-file entry.")
        result[relative.as_posix()] = hashlib.sha256(path.read_bytes()).hexdigest()
    return result

def _extend_loaded_package_paths(package_root):
    for name, module in list(sys.modules.items()):
        if name != "fastplms" and not name.startswith("fastplms."):
            continue
        paths = getattr(module, "__path__", None)
        if paths is None:
            continue
        relative = name.split(".")[1:]
        candidate = package_root.joinpath(*relative)
        candidate_text = str(candidate)
        if candidate.is_dir() and candidate_text not in paths:
            paths.append(candidate_text)

def _merge_runtime(package, package_root):
    incoming = _runtime_file_hashes(package_root)
    known = getattr(package, "__fastplms_artifact_runtime_files__", None)
    if not isinstance(known, dict):
        raise RuntimeError(
            "A non-artifact fastplms module is already loaded. Load the Hub artifact "
            "in a separate Python process."
        )
    conflicts = sorted(
        relative
        for relative, digest in incoming.items()
        if relative in known and known[relative] != digest
    )
    if conflicts:
        raise RuntimeError(
            "FastPLMs artifacts contain incompatible runtime sources at "
            + ", ".join(repr(path) for path in conflicts[:5])
            + ". Load incompatible releases in separate Python processes."
        )
    known = dict(known)
    known.update(incoming)
    package.__fastplms_artifact_runtime_files__ = known
    roots = list(getattr(package, "__fastplms_artifact_runtime_roots__", ()))
    if str(package_root) not in roots:
        roots.append(str(package_root))
    package.__fastplms_artifact_runtime_roots__ = tuple(roots)
    temporaries = list(
        getattr(package, "__fastplms_artifact_runtime_temporaries__", ())
    )
    for temporary in _RUNTIME_TEMPORARIES:
        if temporary not in temporaries:
            temporaries.append(temporary)
    package.__fastplms_artifact_runtime_temporaries__ = tuple(temporaries)
    hashes = set(getattr(package, "__fastplms_artifact_runtime_hashes__", ()))
    hashes.add(RUNTIME_HASH)
    package.__fastplms_artifact_runtime_hashes__ = frozenset(hashes)
    _extend_loaded_package_paths(package_root)
    return package

def _import_without_bytecode(module_name):
    previous = sys.dont_write_bytecode
    sys.dont_write_bytecode = True
    try:
        return importlib.import_module(module_name)
    finally:
        sys.dont_write_bytecode = previous

def _install_runtime():
    package = sys.modules.get("fastplms")
    hashes = getattr(package, "__fastplms_artifact_runtime_hashes__", ())
    if RUNTIME_HASH in hashes:
        return package
    package_root = _ensure_runtime()
    if package is not None:
        return _merge_runtime(package, package_root)
    spec = importlib.util.spec_from_file_location(
        "fastplms",
        package_root / "__init__.py",
        submodule_search_locations=[str(package_root)],
    )
    if spec is None or spec.loader is None:
        raise ImportError("Unable to load the embedded FastPLMs runtime.")
    package = importlib.util.module_from_spec(spec)
    package.__fastplms_artifact_runtime_hash__ = RUNTIME_HASH
    package.__fastplms_artifact_runtime_hashes__ = frozenset({RUNTIME_HASH})
    package.__fastplms_artifact_runtime_files__ = _runtime_file_hashes(package_root)
    package.__fastplms_artifact_runtime_roots__ = (str(package_root),)
    package.__fastplms_artifact_runtime_temporaries__ = tuple(
        _RUNTIME_TEMPORARIES
    )
    sys.modules["fastplms"] = package
    previous = sys.dont_write_bytecode
    sys.dont_write_bytecode = True
    try:
        try:
            spec.loader.exec_module(package)
        except BaseException:
            sys.modules.pop("fastplms", None)
            raise
    finally:
        sys.dont_write_bytecode = previous
    return package

_install_runtime()
_module_181 = _import_without_bytecode("fastplms.models.e1.modeling_e1")
E1Config = _module_181.E1Config
E1Config.__module__ = __name__
E1ForMaskedLM = _module_181.E1ForMaskedLM
E1ForMaskedLM.__module__ = __name__
E1ForSequenceClassification = _module_181.E1ForSequenceClassification
E1ForSequenceClassification.__module__ = __name__
E1ForTokenClassification = _module_181.E1ForTokenClassification
E1ForTokenClassification.__module__ = __name__
E1Model = _module_181.E1Model
E1Model.__module__ = __name__