geno_lewm.encoder¶
encoder
¶
State-encoder input preparation and Carbon wrapper helpers.
The pure-Python windowing, pooling, and cache helpers import without the
ML runtime. CarbonStateEncoder loads the optional Transformers stack
only when callers construct it without injected model/tokenizer objects.
CacheLookupResult
dataclass
¶
One cached embedding together with its selected physical provenance.
CacheProvenance
dataclass
¶
CacheProvenance(cache_schema_version: str, physical_encoding: str, shard_path: Path, row_offset: int)
Physical source selected for one logical cache-key lookup.
CacheReindexReport
dataclass
¶
Summary of a SQLite index rebuild.
CacheRepairReport
dataclass
¶
Summary of a repair pass over Parquet shards.
CacheShardInspection
dataclass
¶
CacheShardInspection(path: Path, records: tuple[WindowCacheRecord, ...], sha256: str, size_bytes: int)
Fully decoded immutable shard bytes and their exact file identity.
WindowCacheKey
dataclass
¶
WindowCacheKey(window_hash: bytes, encoder_hash: bytes, state_layer: int, pool_type: str, pool_radius: int, center_token: int | None, dtype: str)
Content-addressed key for a cached embedding row.
WindowCacheRecord
dataclass
¶
WindowCacheRecord(chrom: str, start_bp: int, end_bp: int, window_hash: bytes, encoder_hash: bytes, state_layer: int, pool_type: str, pool_radius: int, center_token: int | None, dtype: str, embedding: tuple[float, ...], untargeted: bool, created_at: int = 0, schema_version: str = CACHE_SCHEMA_VERSION)
One raw pooled row in the window-embedding cache schema.
Normalization is a consumer-side view and is intentionally absent from the cache key. Cache producers must never persist normalized states.
with_created_at
¶
Fill created_at with current UTC nanoseconds when absent.
Source code in geno_lewm/encoder/cache.py
CacheBuildReport
dataclass
¶
Completed evidence bundle and its JSON-native report payload.
to_dict
¶
CarbonStateEncoder
¶
CarbonStateEncoder(model_id: str, revision: str, *, dtype: str = 'bf16', state_layer: int = -1, pool_type: str = POOL_CENTERED_MEAN, pool_radius: int = DEFAULT_POOL_RADIUS_TOKENS, normalize: bool = True, lora_config: object | None = None, model: object | None = None, tokenizer: object | None = None, encoder_hash: bytes | str | None = None, local_files_only: bool = True, trust_remote_code: bool = False, device: str | None = None)
Encode DNA windows with Carbon hidden states plus deterministic pooling.
Source code in geno_lewm/encoder/carbon.py
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 | |
parameter_count
property
¶
Return the number of parameters exposed by the encoder module.
trainable_parameter_count
property
¶
Return zero after the frozen-encoder contract is enforced.
encode
¶
encode_batch
¶
encode_batch(windows: Sequence[str], edit_loci: Sequence[int | None]) -> tuple[tuple[float, ...], ...]
Encode and pool a batch of DNA windows.
Source code in geno_lewm/encoder/carbon.py
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 | |
pooling_identity
¶
Resolve the exact cache pooling identity from Carbon token IDs.
Source code in geno_lewm/encoder/carbon.py
PoolingResult
dataclass
¶
PoolingResult(vector: tuple[float, ...], pool_type: Literal['centered_mean', 'global_mean'], pool_radius: int, untargeted: bool, center_token: int | None, token_count: int)
Pooled state vector plus cache-key metadata.
as_cache_fields
¶
Return fields shared with the window-cache schema.
EncoderRuntimeIdentity
dataclass
¶
EncoderRuntimeIdentity(model_id: str, revision: str, state_contract_version: str, runtime_hash: str, weights_hash: str | None = None, schema_version: str = ENCODER_RUNTIME_IDENTITY_SCHEMA_VERSION)
Pinned model and byte identities required for cache construction.
cache_identity_hash
property
¶
Return the identity committed into cache keys for this state contract.
to_dict
¶
Return the canonical JSON-native contract payload.
Source code in geno_lewm/encoder/runtime_identity.py
ExtractedWindow
dataclass
¶
ExtractedWindow(sequence: str, start_bp: int, end_bp: int, window_bp: int, edit_locus: int | None = None, relative_edit_locus: int | None = None, pad_right_bp: int = 0)
A fixed-size DNA window plus its source-coordinate metadata.
start_bp and end_bp are 0-based half-open coordinates in
the caller's source coordinate system. end_bp - start_bp always
equals window_bp even when the sequence had to be right-padded
past the available source bases; pad_right_bp records how many
trailing A bases were introduced.
as_tokenizer_input
¶
default_cache_dir
¶
inspect_cache_shard
¶
Decode and hash one shard through one no-follow file descriptor.
The caller may pass a cache-relative path or an absolute path inside the cache root. Every physical row and embedding is decoded and validated. The digest and size describe the same held inode, so resume checks do not rely on a path-following time-of-check/time-of-use sequence.
Source code in geno_lewm/encoder/cache.py
read_cache_entries
¶
read_cache_entries(cache_dir: Path | str, keys: Sequence[WindowCacheKey], *, policy: CacheReadPolicy = 'require_v3') -> tuple[CacheLookupResult | None, ...]
Return cache entries in request order under an explicit provenance policy.
Source code in geno_lewm/encoder/cache.py
read_cache_entry
¶
read_cache_entry(cache_dir: Path | str, key: WindowCacheKey, *, policy: CacheReadPolicy = 'require_v3') -> CacheLookupResult | None
Return one embedding and its selected cache provenance.
Source code in geno_lewm/encoder/cache.py
read_embedding
¶
read_embedding(cache_dir: Path | str, key: WindowCacheKey, *, policy: CacheReadPolicy = 'require_v3') -> tuple[float, ...] | None
Return a raw pooled embedding by content key, or None on cache miss.
Source code in geno_lewm/encoder/cache.py
read_embeddings
¶
read_embeddings(cache_dir: Path | str, keys: Sequence[WindowCacheKey], *, policy: CacheReadPolicy = 'require_v3') -> tuple[tuple[float, ...] | None, ...]
Return raw embeddings for keys in order, grouping reads by shard.
Duplicate keys and misses are preserved in the returned tuple. Only the Parquet row groups containing requested rows are read.
Source code in geno_lewm/encoder/cache.py
reindex_cache
¶
Rebuild index.sqlite from every readable Parquet shard.
Source code in geno_lewm/encoder/cache.py
repair_cache
¶
Quarantine unreadable Parquet shards and rebuild the SQLite index.
Source code in geno_lewm/encoder/cache.py
shard_path_for
¶
shard_path_for(cache_dir: Path | str, *, encoder_id: str, state_layer: int, pool_type: str, pool_radius: int, contig: str, stride_block: int, encoder_hash: bytes | None = None, dtype: str | None = None) -> Path
Return the canonical Parquet shard path for a cache block.
Supplying encoder_hash and dtype selects the collision-safe v3
namespace. Omitting both preserves the legacy v2 path contract for
callers that need to locate existing artifacts.
Source code in geno_lewm/encoder/cache.py
write_shard
¶
write_shard(cache_dir: Path | str, *, encoder_id: str, contig: str, stride_block: int, records: Sequence[WindowCacheRecord]) -> Path
Write one immutable Parquet shard and index its rows.
If the shard already exists with the same rows, this is a no-op. If it exists and new or conflicting rows are supplied, the function raises instead of rewriting in place (INV-DATA-3 / INV-DATA-10).
Source code in geno_lewm/encoder/cache.py
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 | |
build_window_cache
¶
build_window_cache(*, requests_jsonl: Path | str | bytes, cache_dir: Path | str, evidence_dir: Path | str, encoder: object, encoder_id: str, batch_size: int, rows_per_shard: int, created_at_ns: int, hardware: str, resolved_config: Mapping[str, object], encoder_runtime_identity: Mapping[str, object], input_artifacts: Mapping[str, Path | str | bytes] | None = None, logger: GenoLeWMLogger | None = None) -> CacheBuildReport
Build or resume the cache for one exact request JSONL artifact.
encoder must expose the raw-state CarbonStateEncoder contract:
pooling_identity, encode_batch, an exact 32-byte encoder_hash,
and normalize is False. Existing planned shards are decoded, hashed,
compared row-for-row with the plan, and re-indexed before any missing shard
is encoded.
Source code in geno_lewm/encoder/cache_build.py
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 649 650 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 833 834 835 836 837 838 | |
centered_mean
¶
centered_mean(hidden_states: Sequence[Sequence[float]], *, center_token: int, pool_radius: int = DEFAULT_POOL_RADIUS_TOKENS) -> tuple[float, ...]
Mean-pool the inclusive token span center_token ± pool_radius.
Source code in geno_lewm/encoder/pooling.py
global_mean
¶
Mean-pool every token vector in hidden_states.
pool_hidden_states
¶
pool_hidden_states(hidden_states: Sequence[Sequence[float]], *, edit_locus: int | None = None, center_token: int | None = None, content_token_bounds: tuple[int, int] | None = None, pool_type: Literal['centered_mean', 'global_mean'] = POOL_CENTERED_MEAN, pool_radius: int = DEFAULT_POOL_RADIUS_TOKENS) -> PoolingResult
Pool token-level hidden states into a state vector.
center_token is the actual hidden-state index resolved from the
tokenizer's DNA/control-token layout. edit_locus only records whether
the state is targeted; this function deliberately does not approximate a
token index from base-pair arithmetic. When the locus is absent, the
encoder contract requires a global-mean fallback tagged as untargeted.
Source code in geno_lewm/encoder/pooling.py
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 | |
parse_encoder_runtime_identity_bytes
¶
Parse one duplicate-key-free, closed runtime identity JSON object.
Source code in geno_lewm/encoder/runtime_identity.py
canonicalize_dna
¶
Return uppercase DNA after validating the supported alphabet.
The cache hash invariant is based on uppercased window content, so
callers can hash raw source slices and already-canonical windows
interchangeably. N is accepted because reference FASTA and
edited windows may contain masked bases.
Source code in geno_lewm/encoder/windowing.py
extract_window
¶
extract_window(source_sequence: str, *, edit_locus: int | None = None, window_bp: int = DEFAULT_WINDOW_BP, assume_canonical: bool = False) -> ExtractedWindow
Extract a supported-width DNA window from source_sequence.
edit_locus is a 0-based offset in source_sequence. When it
is supplied the window is centered on that locus unless clamped by
source boundaries. When omitted, the source midpoint is used. If
the source is shorter than the requested window or the selected
interval extends past the right edge, trailing A bases are
appended per Carbon's tokenizer convention.
Set assume_canonical when source_sequence is already uppercase,
validated DNA (e.g. a contig from a loaded reference FASTA) to skip the
O(len) re-validation. Re-validating a whole chromosome once per variant
otherwise dominates VCF scoring wall-clock.
Source code in geno_lewm/encoder/windowing.py
pad_for_carbon_tokenizer
¶
Right-pad canonical DNA to Carbon's token multiple.
Source code in geno_lewm/encoder/windowing.py
window_sha256
¶
Return SHA-256 bytes for the canonicalized DNA sequence.
wrap_dna_for_tokenizer
¶
Return <dna>...</dna> input with Carbon-compatible padding.