Spaces:
Running on Zero
Running on Zero
Regenerate knapsack_cache.zip for instanovo 1.2.2 format
Browse files- knapsack_cache.zip +2 -2
- scripts/regenerate_knapsack.py +78 -0
knapsack_cache.zip
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba45f03fb19b29c7c3a8f47aa7535bbd1f8f1407c4aa0bf2e3799453a63eb437
|
| 3 |
+
size 62161027
|
scripts/regenerate_knapsack.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Regenerate knapsack_cache/ and knapsack_cache.zip against the currently
|
| 2 |
+
installed instanovo version.
|
| 3 |
+
|
| 4 |
+
When InstaNovo bumps its Knapsack on-disk format (e.g. v1.1 -> v1.2.2 added a
|
| 5 |
+
field to parameters.pkl, giving the "not enough values to unpack" error on
|
| 6 |
+
load), the bundled cache becomes unreadable and the runtime falls back to
|
| 7 |
+
multi-minute regeneration on every container cold-start. This script does
|
| 8 |
+
that regeneration once on a workstation so the committed zip stays in sync.
|
| 9 |
+
|
| 10 |
+
Run with:
|
| 11 |
+
.venv/bin/python scripts/regenerate_knapsack.py
|
| 12 |
+
|
| 13 |
+
Expect a few minutes on CPU. Output:
|
| 14 |
+
./knapsack_cache/{parameters.pkl,masses.npy,chart.npy}
|
| 15 |
+
./knapsack_cache.zip (the three files above, repacked for HF Spaces)
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
import shutil
|
| 21 |
+
import time
|
| 22 |
+
import zipfile
|
| 23 |
+
from pathlib import Path
|
| 24 |
+
|
| 25 |
+
from instanovo.constants import MASS_SCALE, MAX_MASS
|
| 26 |
+
from instanovo.inference import Knapsack
|
| 27 |
+
from instanovo.transformer.model import InstaNovo
|
| 28 |
+
|
| 29 |
+
TRANSFORMER_MODEL_ID = "instanovo-v1.2.0"
|
| 30 |
+
KNAPSACK_DIR = Path(__file__).resolve().parent.parent / "knapsack_cache"
|
| 31 |
+
KNAPSACK_ZIP = KNAPSACK_DIR.with_suffix(".zip")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def main() -> None:
|
| 35 |
+
print(f"Loading {TRANSFORMER_MODEL_ID} for residue set...")
|
| 36 |
+
model, _ = InstaNovo.from_pretrained(TRANSFORMER_MODEL_ID)
|
| 37 |
+
residue_set = model.residue_set
|
| 38 |
+
|
| 39 |
+
residue_masses = dict(residue_set.residue_masses)
|
| 40 |
+
excluded = set(residue_set.special_tokens) | {
|
| 41 |
+
k for k, v in residue_masses.items() if v <= 0
|
| 42 |
+
}
|
| 43 |
+
for res in excluded:
|
| 44 |
+
residue_masses.pop(res, None)
|
| 45 |
+
if not residue_masses:
|
| 46 |
+
raise SystemExit("No valid residues with positive mass; cannot regenerate.")
|
| 47 |
+
print(f"Excluding {sorted(excluded)} from knapsack; {len(residue_masses)} residues remain.")
|
| 48 |
+
|
| 49 |
+
if KNAPSACK_DIR.exists():
|
| 50 |
+
print(f"Clearing existing {KNAPSACK_DIR}...")
|
| 51 |
+
shutil.rmtree(KNAPSACK_DIR)
|
| 52 |
+
KNAPSACK_DIR.parent.mkdir(parents=True, exist_ok=True)
|
| 53 |
+
# Knapsack.save() creates KNAPSACK_DIR itself and refuses if it already exists.
|
| 54 |
+
|
| 55 |
+
start = time.time()
|
| 56 |
+
print("Generating knapsack (this can take several minutes)...")
|
| 57 |
+
knapsack = Knapsack.construct_knapsack(
|
| 58 |
+
residue_masses=residue_masses,
|
| 59 |
+
residue_indices=residue_set.residue_to_index,
|
| 60 |
+
max_mass=MAX_MASS,
|
| 61 |
+
mass_scale=MASS_SCALE,
|
| 62 |
+
)
|
| 63 |
+
knapsack.save(str(KNAPSACK_DIR))
|
| 64 |
+
print(f"Saved knapsack to {KNAPSACK_DIR} ({time.time() - start:.1f}s).")
|
| 65 |
+
|
| 66 |
+
print(f"Packing {KNAPSACK_ZIP}...")
|
| 67 |
+
if KNAPSACK_ZIP.exists():
|
| 68 |
+
KNAPSACK_ZIP.unlink()
|
| 69 |
+
with zipfile.ZipFile(KNAPSACK_ZIP, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 70 |
+
for f in sorted(KNAPSACK_DIR.iterdir()):
|
| 71 |
+
zf.write(f, arcname=f.name)
|
| 72 |
+
print(f"Wrote {KNAPSACK_ZIP} ({KNAPSACK_ZIP.stat().st_size / 1e6:.1f} MB).")
|
| 73 |
+
|
| 74 |
+
print("Done. Commit the new zip with git (LFS-tracked) and push to the Space.")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
main()
|