Datasets:
Download scripts/predict.py from zhilong777/mattext-syn: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/datasets/zhilong777/mattext-syn/resolve/main/scripts/predict.py
- Command line
-
hf download hf://datasets/zhilong777/mattext-syn/scripts/predict.py
-
curl -L -o predict.py https://huggingface.co/datasets/zhilong777/mattext-syn/resolve/main/scripts/predict.py
2.57 kB
| """Score structures with saved fine-tuned weights. | |
| python predict.py --weights DIR/<rep>_lr<lr>_s<seed> --rep REP --data reps.parquet --out OUT.csv --xtal2txt SRC | |
| The parquet needs columns id and <rep>; label and set are copied through when present.""" | |
| import os, sys, types, argparse | |
| import numpy as np, pandas as pd, torch | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--weights", required=True); ap.add_argument("--rep", required=True) | |
| ap.add_argument("--data", required=True); ap.add_argument("--out", required=True); ap.add_argument("--xtal2txt", required=True) | |
| a = ap.parse_args() | |
| CTX = {"composition": 32, "slices": 512, "cif_symmetrized": 1024, "cif_p1": 1024, "zmatrix": 512, | |
| "atom_sequences": 32, "atom_sequences_plusplus": 32} | |
| ctx = CTX[a.rep] | |
| pkg = types.ModuleType("xtal2txt"); pkg.__path__ = [os.path.join(a.xtal2txt, "src", "xtal2txt")]; sys.modules["xtal2txt"] = pkg | |
| from xtal2txt.tokenizer import SliceTokenizer, CompositionTokenizer, CifTokenizer, CrysllmTokenizer | |
| TOK = {"slices": SliceTokenizer, "composition": CompositionTokenizer, "cif_symmetrized": CifTokenizer, "cif_p1": CifTokenizer, | |
| "zmatrix": CrysllmTokenizer, "atom_sequences": CompositionTokenizer, "atom_sequences_plusplus": CompositionTokenizer} | |
| SPECIAL = {"unk_token": "[UNK]", "pad_token": "[PAD]", "cls_token": "[CLS]", "sep_token": "[SEP]", "mask_token": "[MASK]", | |
| "eos_token": "[EOS]", "bos_token": "[BOS]"} | |
| tok = TOK[a.rep](special_num_token=False, special_tokens=SPECIAL, model_max_length=512, truncation=False, padding=False) | |
| import pyarrow.parquet as pq | |
| have = pq.read_schema(a.data).names | |
| df = pd.read_parquet(a.data, columns=[c for c in ("id", "label", "set", a.rep) if c in have]) | |
| df = df[df[a.rep].notna()].reset_index(drop=True) | |
| enc = tok(df[a.rep].tolist(), truncation=True, padding="max_length", max_length=ctx) | |
| ids, mask = torch.tensor(enc["input_ids"]), torch.tensor(enc["attention_mask"]) | |
| from transformers import AutoModelForSequenceClassification | |
| model = AutoModelForSequenceClassification.from_pretrained(a.weights).to("cuda").eval() | |
| out = [] | |
| with torch.no_grad(): | |
| for i in range(0, len(df), 256): | |
| x, m = ids[i:i+256], mask[i:i+256]; L = int(m.sum(1).max()); x, m = x[:, :L], m[:, :L] | |
| with torch.autocast("cuda", dtype=torch.bfloat16): | |
| lg = model(input_ids=x.to("cuda"), attention_mask=m.to("cuda")).logits | |
| out.append(torch.softmax(lg.float(), -1)[:, 1].cpu()) | |
| df["prob"] = torch.cat(out).numpy() | |
| df.drop(columns=[a.rep]).to_csv(a.out, index=False) | |
| print("DONE", a.weights, len(df), flush=True) | |