model-inference
Load local SaProt checkpoints, tokenize structure-aware sequences, extract embeddings, score mutations, and run inverse folding.
- License 许可
- MIT
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- model-inference
- license
- MIT
- metadata
- { "disco-role": "operating" }
- description
- Load local SaProt checkpoints, tokenize structure-aware sequences, extract embeddings, score mutations, and run inverse folding.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/api-reference.md5.5 KB
- 参考文档references/model-assets.md3.9 KB
- 参考文档references/troubleshooting.md4.7 KB
- 脚本scripts/check_model_assets.py8.5 KB
03 SKILL.md 正文
SaProt Model Inference
Use this sub-skill when an agent needs to run SaProt inference from local model assets: Hugging Face model directories, ESM-style .pt checkpoints, mutation-effect helpers, embedding extraction, or inverse folding. It assumes the caller already has a valid amino-acid plus 3Di structure-aware sequence when structure context is required.
Route First
- For PDB/mmCIF to AA+3Di sequence conversion, route to
structure-sequencesbefore using this sub-skill. - For LMDB schemas, YAML task configs, or dataset placement, route to
datasets-configs. - For training, fine-tuning, benchmark evaluation, or ClinVar/ProteinGym scripts, route to
training-evaluation.
Local Asset Check
Validate local assets before writing inference code:
python scripts/check_model_assets.py /path/to/SaProt_650M_AF2
python scripts/check_model_assets.py /path/to/SaProt_650M_AF2.pt --kind esm-pt
python scripts/check_model_assets.py /path/to/SaProt_650M_AF2 --try-tokenizer
The repository does not bundle model weights. Hugging Face loading expects a local model directory; utils.esm_loader.load_esm_saprot expects a local .pt checkpoint file.
Hugging Face Masked-LM Loading
Use this path for standard tokenizer/model inference from a local SaProt Hugging Face directory:
import torch
from transformers import EsmForMaskedLM, EsmTokenizer
model_dir = "/path/to/local/SaProt_650M_AF2"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = EsmTokenizer.from_pretrained(model_dir)
model = EsmForMaskedLM.from_pretrained(model_dir).to(device).eval()
seq = "M#EvVpQpL#VyQdYaKv"
inputs = tokenizer(seq, return_tensors="pt")
inputs = {name: tensor.to(device) for name, tensor in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
print(outputs.logits.shape)
Structure-aware SaProt tokens are two-character AA+3Di units such as Ev, Qp, and L#. Do not insert spaces unless a specific SaProt helper has already tokenized and rejoined tokens for masked mutation scoring.
Embeddings With SaprotBaseModel
Use SaprotBaseModel(task="base", config_path=model_dir, load_pretrained=True) to get last-layer hidden states. get_hidden_states(inputs, reduction="mean") returns one tensor per input sequence; with reduction=None, each tensor preserves per-token hidden states excluding special tokens.
import torch
from transformers import EsmTokenizer
from model.saprot.base import SaprotBaseModel
model_dir = "/path/to/local/SaProt_650M_AF2"
device = "cuda" if torch.cuda.is_available() else "cpu"
model = SaprotBaseModel(task="base", config_path=model_dir, load_pretrained=True)
model = model.to(device).eval()
tokenizer = EsmTokenizer.from_pretrained(model_dir)
seq = "M#EvVpQpL#VyQdYaKv"
inputs = tokenizer(seq, return_tensors="pt")
inputs = {name: tensor.to(device) for name, tensor in inputs.items()}
with torch.no_grad():
embeddings = model.get_hidden_states(inputs, reduction="mean")
print(embeddings[0].shape)
For the 35M and 650M SaProt checkpoints, prefer SA-token input for frozen embeddings. AA-only input works syntactically but is not the intended frozen-embedding mode for those checkpoints. The 1.3B SaProt variants are documented as better for AA-only usage.
Mutation Effects
Use SaprotFoldseekMutationModel with a local Hugging Face model directory. foldseek_path can be None for the direct helper methods below when you already provide a combined AA+3Di sequence.
import torch
from model.saprot.saprot_foldseek_mutation_model import SaprotFoldseekMutationModel
model_dir = "/path/to/local/SaProt_650M_AF2"
device = "cuda" if torch.cuda.is_available() else "cpu"
model = SaprotFoldseekMutationModel(
foldseek_path=None,
config_path=model_dir,
load_pretrained=True,
)
model = model.to(device).eval()
seq = "M#EvVpQpL#VyQdYaKv"
score = model.predict_mut(seq, "V3A:Q4M")
substitution_scores = model.predict_pos_mut(seq, 3)
substitution_probs = model.predict_pos_prob(seq, 3)
Mutation strings are 1-indexed and use the amino-acid position, for example V3A. Multiple substitutions use colon syntax such as V3A:Q4M. predict_pos_mut(seq, pos) returns log-ratio mutation effects for all 20 amino acids at that 1-indexed position; predict_pos_prob(seq, pos) returns amino-acid probabilities at the masked position.
ESM-Style .pt Loading
Use load_esm_saprot only for the SaProt .pt checkpoint format:
from utils.esm_loader import load_esm_saprot
pt_path = "/path/to/local/SaProt_650M_AF2.pt"
model, alphabet = load_esm_saprot(pt_path)
model.eval()
This path requires torch and fair-esm. It builds the SaProt alphabet from the AA vocabulary plus Foldseek 3Di vocabulary and then loads the checkpoint model and config entries.
Inverse Folding
Use SaProtIFModel with a local inverse-folding Hugging Face model directory. The amino-acid string and Foldseek structure string must have equal length. Mask amino acids to predict with #.
import torch
from model.saprot.saprot_if_model import SaProtIFModel
model_dir = "/path/to/local/SaProt_650M_AF2_inverse_folding"
device = "cuda" if torch.cuda.is_available() else "cpu"
model = SaProtIFModel(config_path=model_dir, load_pretrained=True)
model = model.to(device).eval()
predicted_sequences = model.predict("##########", "dddddddddd", method="argmax")
print(predicted_sequences[0])
method must be "argmax" or "multinomial". num_samples must be 1 for argmax; use method="multinomial" for multiple sampled designs. The method returns a list of predicted amino-acid sequences, even for deterministic argmax.
References
references/api-reference.mdfor signatures, return shapes, and method distinctions.references/model-assets.mdfor checkpoint catalog, local asset layout, and AA-only caveats.references/troubleshooting.mdfor dependency, path, tokenization, CUDA/CPU, mutation, and inverse-folding failures.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).