models¶
Concrete model implementations that subclass the core abstractions. Each model wraps an external library (ESM, ProteinMPNN) into ProteinGen's unified interface.
Tokenization landscape¶
Three tokenizer ecosystems coexist in the library — cross-tokenizer mapping is handled by GuidanceProjection (see guide):
| Tokenizer | Vocab size | Special tokens | Used by |
|---|---|---|---|
ESM (EsmSequenceTokenizer) |
33 | <cls>=0, <pad>=1, <eos>=2, <unk>=3, <mask>=32 |
ESMC, ESM3 |
MPNN (MPNNTokenizer) |
21 (or 22 with mask) | UNK(X)=20, optional <mask>=21 |
StabilityPMPNN |
| Simple 20-AA | 21 | pad=20 |
Custom predictors |
API Reference¶
proteingen.models
¶
Backward-compatible model namespace.
New code should prefer proteingen.modeling.
This module keeps old import styles working, including:
from proteingen.models import ESMCfrom proteingen.models import esmc(module)from proteingen.models.esm import ESMC
DPLM2
¶
Bases: GenerativeModelWithEmbedding
DPLM-2 discrete diffusion protein language model.
Wraps ByteDance's DPLM-2 (multimodal diffusion protein LM) as a GenerativeModelWithEmbedding for use with proteingen's sampling, guidance, and probe infrastructure.
Currently supports sequence-only mode: input is [
Available checkpoints
"airkingbd/dplm2_150m"— 150M params, 640d, 30 layers"airkingbd/dplm2_650m"— 650M params, 1280d, 33 layers (default)"airkingbd/dplm2_3b"— 3B params, 2560d, 36 layers
Tensor index legend
S: batch index P: position index T: token/vocab dimension (OUTPUT_DIM = vocab_size = 8229) D: embedding dimension (EMB_DIM = hidden_size)
Example::
model = DPLM2("airkingbd/dplm2_650m")
log_probs = model.get_log_probs_from_string(["ACDEF"])
Source code in src/proteingen/modeling/models/dplm2/__init__.py
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
differentiable_embedding
¶
OHE (or soft distribution) → deep embeddings through the transformer.
Replicates the forward path through EsmEmbeddings + EsmEncoder: 1. Soft word embedding lookup via matmul 2. Token dropout: zero mask positions, then rescale (ESM's mask-ratio compensation, runs in both train and eval mode) 3. Attention mask application 4. Transformer encoder with rotary attention
For soft distributions (TAG guidance), mask tokens should not appear in the input — the zeroing step uses argmax to identify mask positions, which is non-differentiable at those positions.
Source code in src/proteingen/modeling/models/dplm2/__init__.py
embedding_to_outputs
¶
Deep embeddings → logits via the LM head.
format_raw_to_logits
¶
Extract logits from MaskedLMOutput and apply logit formatting.
Source code in src/proteingen/modeling/models/dplm2/__init__.py
DPLM2Tokenizer
¶
Tokenizer for DPLM2's extended vocabulary (AA + structure tokens).
Wraps HuggingFace's EsmTokenizer with DPLM2-specific special token assignments. The DPLM2 vocabulary has 3 regions: - Tokens 0-32: amino acid tokens + AA special tokens - Tokens 33-8228: structure tokens + struct special tokens - Token IDs >= vocab_size: generic HF special tokens (excluded)
Key special tokens
- 0:
(BOS for amino acids) - 1:
- 2:
(EOS for amino acids) - 32:
(mask for amino acid diffusion)
Source code in src/proteingen/modeling/models/dplm2/__init__.py
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 | |
encode
¶
Encode an amino acid sequence to token IDs.
Source code in src/proteingen/modeling/models/dplm2/__init__.py
decode
¶
Decode token IDs back to an amino acid sequence (skipping special tokens).
Source code in src/proteingen/modeling/models/dplm2/__init__.py
ESM3
¶
Bases: GenerativeModelWithEmbedding
ESM3 masked language model as a GenerativeModelWithEmbedding.
Non-sequence tracks (structure, ss8, sasa, function, residue) default to padding values. Only the sequence embedding is differentiable.
Structure conditioning: pass atom37 coordinates via set_condition_()
or conditioned_on(). The structure VQ-VAE encodes them once; the
resulting structure tokens and coordinates are used in both the
differentiable embedding path and the full forward path.
Example::
model = ESM3()
coords_RAX, wt_seq = pdb_to_atom37_and_seq("1abc.pdb")
with model.conditioned_on({"coords_RAX": coords_RAX}):
log_probs = model.get_log_probs(seq_SP)
Tensor Index Legend
S: sequence index in batch P: position index in sequence T: token/vocab dimension (OUTPUT_DIM = 64) D: embedding dimension (EMB_DIM = 1536)
Source code in src/proteingen/modeling/models/esm/esm3.py
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
preprocess_observations
¶
Encode structure once via VQ-VAE (expensive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observations
|
dict
|
{"coords_RAX": (L, 37, 3) tensor or np.array} |
required |
Returns:
| Type | Description |
|---|---|
dict
|
{"structure_tokens": (L+2,), "coordinates": (L+2, 37, 3)} with BOS/EOS padding. |
Source code in src/proteingen/modeling/models/esm/esm3.py
collate_observations
¶
Tile cached structure to match batch size.
Source code in src/proteingen/modeling/models/esm/esm3.py
ESM3IF
¶
Bases: ESM3
Deprecated: use ESM3 with set_condition_() instead.
This thin subclass exists only for backwards compatibility. It issues a
deprecation warning on construction and delegates everything to ESM3.
Source code in src/proteingen/modeling/models/esm/esm3if.py
ESMC
¶
Bases: GenerativeModelWithEmbedding
ESM-C masked language model as a GenerativeModelWithEmbedding.
Tensor Index Legend
S: sequence index in batch P: position index in sequence T: token/vocab dimension (OUTPUT_DIM = 64) D: embedding dimension (EMB_DIM = 960 for 300m, 1152 for 600m)
Source code in src/proteingen/modeling/models/esm/esmc.py
ESMForgeAPI
¶
Bases: GenerativeModel
ESM model accessed via the EvolutionaryScale Forge API.
Wraps a Forge inference client to provide the same get_log_probs
interface as the local ESM wrappers. No local weights needed — inference
happens remotely.
Automatically selects the right client (ESM3 vs ESMC) based on the model name. Structure conditioning is supported for ESM3 models only.
Limitations vs local models
- No gradient access (no
embed, no TAG guidance) - No LoRA / fine-tuning
- No checkpointing
- Batched inference loops sequentially over the API
Example::
import os
model = ESMForgeAPI("esmc-6b-2024-12", token=os.environ["FORGE_TOKEN"])
log_probs = model.get_log_probs_from_string(["ACDEF"])
Tensor Index Legend
S: sequence index in batch P: position index in sequence T: token/vocab dimension (OUTPUT_DIM = 64)
Source code in src/proteingen/modeling/models/esm/esm_api.py
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 194 195 196 197 198 199 200 | |
forward
¶
Forward pass via the Forge API.
Loops over the batch dimension, stripping padding from each sequence before sending to the API, then re-pads results to the max length.
Returns:
| Type | Description |
|---|---|
FloatTensor
|
Logits tensor of shape (S, P, OUTPUT_DIM). |
Source code in src/proteingen/modeling/models/esm/esm_api.py
preprocess_observations
¶
Encode structure via the Forge API (remote VQ-VAE).
Only supported for ESM3 models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observations
|
dict
|
{"coords_RAX": (L, 37, 3) tensor or np.array} |
required |
Returns:
| Type | Description |
|---|---|
dict
|
{"structure_tokens": (L+2,), "coordinates": (L+2, 37, 3)} with BOS/EOS. |
Source code in src/proteingen/modeling/models/esm/esm_api.py
Frame2seq
¶
Bases: GenerativeModelWithEmbedding
Frame2seq structure-conditioned inverse folding model.
Loads Frame2seq's bundled checkpoint ensemble and exposes it through proteingen's GenerativeModelWithEmbedding interface.
Conditioning is required and must be set with:
model.set_condition_({"pdb_path": "1abc.pdb", "chain_id": "A"})
Tensor Index Legend
B: batch index
P: residue position index
A: atom index (N, CA, C, CB, O)
U: Frame2seq sequence dim (21 = 20 AAs + X)
T: proteingen tokenizer dim (22 = U +
Source code in src/proteingen/modeling/models/frame2seq/frame2seq.py
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |