Models¶
CrystaLLM-pi supports two conditioning mechanisms for crystal structure generation: prefix conditioning and residual conditioning. The tokenizer and datasets used to train these models are documented on the Data page.
Each conditional model family includes a configuration, an encoder that maps the conditioning inputs to the representation expected by the transformer, and a GPT-2 model that applies the conditioning.
Tensor shapes and forward() behaviour are documented in the source docstrings and are available through the linked source or help().
The earlier PKV and Slider classes used in the paper are retained for loading released checkpoints. They implement earlier versions of the same conditioning mechanisms and are not part of the current model API, so they are not included below.
Prefix¶
Prefix conditioning represents scalar properties as prefix key-values for GPT-2.
PrefixGPT2Config
¶
Bases: GPT2Config
Configuration for prefix-conditioned GPT-2.
n_input_vector specifies the number of scalar properties, n_prefix_tokens the number of
virtual tokens emitted per layer, and n_hidden_cond the conditioning encoder width.
Cross-attention is disabled because conditioning enters through cached key-values.
Source code in _models/Prefix_model.py
PrefixEncoder
¶
Bases: Module
Project a scalar conditioning vector into per-layer prefix key-value tensors.
The MLP emits one flat tensor per batch item, which is later reshaped into layers, heads, and
prefix tokens. This encoder uses GELU, whereas the legacy PKV_model uses ReLU, so the two
checkpoint families are not interchangeable.
Source code in _models/Prefix_model.py
PrefixGPT
¶
Bases: GPT2LMHeadModel
GPT-2 conditioned on scalar properties through learned prefix key-values.
The encoder output occupies config.n_prefix_tokens cached positions before the text sequence.
Generation must therefore account for the prefix when computing the effective text length and
position embeddings.
Source code in _models/Prefix_model.py
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 | |
Residual¶
Residual conditioning adds the conditioning signal to the attention blocks as a residual contribution.
ResidualGPT2Config
¶
Bases: GPT2Config
Configuration for GPT-2 with residual scalar conditioning.
slider_on enables the conditioning path. slider_n_variables specifies the number of scalar
properties, slider_n_hidden the encoder MLP width, and slider_n_heads_sharing_slider the
number of attention heads sharing each conditioning projection. The sharing count must divide
the number of attention heads.
Source code in _models/Residual_model.py
ResidualEncoder
¶
Bases: Module
Encode scalar conditioning values into per-head key-value tensors.
Each variable has its own encode, upscaling, and downscaling projections. A projection is shared
across each group of slider_n_heads_sharing_slider attention heads. The learned mixing weight
is stored as attention_factor.
Source code in _models/Residual_model.py
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 | |
ResidualAttention
¶
Bases: GPT2Attention
GPT-2 attention with optional residual conditioning contributions.
Source code in _models/Residual_model.py
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 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 | |
ResidualGPT2Block
¶
Bases: GPT2Block
GPT-2 block with residual scalar conditioning.
Source code in _models/Residual_model.py
ResidualGPT2Model
¶
Bases: ResidualGPT2PreTrainedModel
Transformer body for GPT-2 with residual scalar conditioning.
Source code in _models/Residual_model.py
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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
ResidualGPT
¶
Bases: ResidualGPT2PreTrainedModel, GenerationMixin
GPT-2 with scalar conditioning injected into each attention block.
Conditioning is mixed into attention per layer rather than represented as prefix tokens, so it does not change the text sequence length. Missing properties are handled through the encoder's condition mask.
Source code in _models/Residual_model.py
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | |
Loading and building¶
Model selection is registry-based. activate_conditionality selects the requested model family from MODEL_REGISTRY. An unknown family raises an error rather than falling back to a different model.
load_pretrained_model
¶
Load a checkpoint into the model class selected by activate_conditionality.
The model class is resolved through MODEL_REGISTRY. Unknown conditionality values raise an
error. Only the intended resize surface, consisting of position embeddings, token embeddings,
and the tied head, may have checkpoint shape mismatches. Other mismatches raise.
When the target context differs, positions are resized. Converting a non-prefix checkpoint to a
Prefix family shifts pretrained position rows by n_prefix_tokens so text tokens retain their
original positional embeddings.
Source code in _utils/model.py
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 | |
build_model
¶
Build a fresh model using the class selected by activate_conditionality.
Model selection follows MODEL_REGISTRY, and unknown conditionality values raise an error.