Text Generation
Transformers
Safetensors
English
plasmid_lm
biology
genomics
plasmid
dna
causal-lm
synthetic-biology
custom_code
Run this model with HF Jobs
Run McClain/PlasmidLM on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/McClain/PlasmidLM. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/McClain/PlasmidLM on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/McClain/PlasmidLM. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/McClain/PlasmidLM. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use McClain/PlasmidLM with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use McClain/PlasmidLM with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="McClain/PlasmidLM", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("McClain/PlasmidLM", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use McClain/PlasmidLM with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "McClain/PlasmidLM" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "McClain/PlasmidLM", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/McClain/PlasmidLM
- SGLang
How to use McClain/PlasmidLM with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "McClain/PlasmidLM" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "McClain/PlasmidLM", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "McClain/PlasmidLM" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "McClain/PlasmidLM", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use McClain/PlasmidLM with Docker Model Runner:
docker model run hf.co/McClain/PlasmidLM
Upload folder using huggingface_hub
Browse filesFiles changed (6)
- README.md+48-73
- config.json+4-1
- configuration_plasmid_lm.py+20
- modeling_plasmid_lm.py+106-32
- moe.pyAdded+99
- tokenization_plasmid_lm.py+16
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273743 unmodified lines78798081828384858687888990912 unmodified lines949596979899100101102103104105106107108109110111112113114115116117118119120121122123124---language:- enlicense: apache-2.0library_name: transformerstags:- biology- genomics- dna- plasmid- synthetic-biology- causal-lm- protein-engineeringdatasets:- custompipeline_tag: text-generationmodel-index:- name: PlasmidLMresults:- task:type: text-generationname: Plasmid DNA Generationmetrics:- name: Eval Losstype: lossvalue: 0.093- name: Token Accuracytype: accuracyvalue: 0.961---# PlasmidLMA 17M-parameter transformer language model for conditional generation of synthetic plasmid DNA sequences.## Model DescriptionPlasmidLM generates plasmid DNA sequences conditioned on functional component specifications. Given a prompt specifying desired elements (antibiotic resistance genes, origins of replication, promoters, reporters, etc.), it autoregressively generates a complete DNA sequence containing those elements.**Architecture**: LLaMA-style transformer decoder with RoPE, RMSNorm, and GELU activations.| Parameter | Value ||-----------|-------|| Parameters | 17M || Hidden size | 384 || Layers | 10 || Attention heads | 8 || Context length | 16,384 tokens || Vocabulary | 120 tokens |The vocabulary consists of 5 DNA bases (A, T, C, G, N), control tokens (BOS, EOS, SEP, PAD, UNK), and ~100 categorical tokens representing functional plasmid components (e.g., `<AMR_KANAMYCIN>`, `<ORI_COLE1>`, `<PROM_T7>`).## TrainingPretrained with causal language modeling on ~108K plasmid sequences derived from the [Addgene](https://www.addgene.org/) repository, annotated with functional components via [pLannotate](https://github.com/barricklab/pLannotate).- **Steps**: 15,000- **Epochs**: ~2.3- **Eval loss**: 0.093- **Token accuracy**: 96.1%- **Optimizer**: AdamW- **Precision**: bf16## Intended UseThis is a **base pretrained model**. It has learned the statistical patterns of plasmid DNA sequences and their relationship to categorical component tokens. It can be used for:- **Direct generation**: Prompt with component tokens to generate plasmid sequences- **Fine-tuning**: Post-train with reinforcement learning (GRPO/PPO) to improve motif placement accuracy- **Embeddings**: Use hidden states as learned representations of plasmid sequences- **Research**: Study the learned structure of synthetic DNA## Usage3 unmodified linesmodel = AutoModelForCausalLM.from_pretrained("McClain/PlasmidLM", trust_remote_code=True)tokenizer = AutoTokenizer.from_pretrained("McClain/PlasmidLM", trust_remote_code=True)# Generate a plasmid with kanamycin resistance and ColE1 originprompt = "<BOS><AMR_KANAMYCIN><ORI_COLE1><SEP>"inputs = tokenizer(prompt, return_tensors="pt")outputs = model.generate(**inputs, max_new_tokens=4096, temperature=0.8, do_sample=True)sequence = tokenizer.decode(outputs[0], skip_special_tokens=False)print(sequence)```## Input Format```2 unmodified linesThe model generates DNA bases (A/T/C/G) after the `<SEP>` token until it produces `<EOS>` or hits the maximum length.## Component Categories| Category | Examples | Count ||----------|----------|-------|| Antibiotic Resistance (AMR) | Kanamycin, Ampicillin, Chloramphenicol, ... | 11 || Origin of Replication (ORI) | ColE1, F1, P15A, pSC101, SV40, ... | 7 || Promoter (PROM) | CMV, T7, U6, EF1a, CAG, ... | 11 || Reporter | EGFP, mCherry, YFP, NanoLuc, ... | 6 || Vector Type (VEC) | Lentiviral, CRISPR, Bacterial, AAV, ... | 10 || Other | Tags, elements, species, backbones | ~55 |## Limitations- This is a **pretrained base model** -- it learns sequence statistics but has not been optimized for motif placement accuracy. Post-training with RL significantly improves functional element fidelity.- Generated sequences are **not experimentally validated**. Always verify computationally (e.g., with pLannotate) and experimentally before synthesis.- The model was trained on Addgene plasmids, which are biased toward commonly deposited vectors (mammalian expression, bacterial cloning, CRISPR).- Maximum context of 16K tokens (~16 kbp), which covers most but not all plasmids.## Citation```bibtex@misc{thiel2026plasmidlm,title={PlasmidLM: Language Models for Conditional Plasmid DNA Generation},author={Thiel, McClain},year={2026},url={https://huggingface.co/McClain/PlasmidLM}}```1234567891011121314151617181920212223242526272829303132333435363738394041423 unmodified lines4647484950515253545556575859602 unmodified lines63646566676869707172737475767778798081828384858687888990919293949596979899---library_name: transformerslicense: apache-2.0tags:- biology- genomics- plasmid- dna- causal-lm- synthetic-biologylanguage:- enpipeline_tag: text-generation---# PlasmidLMA 17.7M parameter autoregressive language model for **plasmid DNA sequence generation**, trained on ~108K plasmid sequences from Addgene.## Model Details| Property | Value ||---|---|| Parameters | 17.7M || Architecture | Transformer decoder (dense MLP), LLaMA-style || Hidden size | 384 || Layers | 10 || Attention heads | 8 || Intermediate size | 1,536 || Max sequence length | 16,384 tokens || Tokenizer | Character-level (single DNA bases) || Vocab size | 120 |### Training- **Data**: ~108K plasmid sequences from Addgene, annotated with functional components via pLannotate- **Steps**: 15,000- **Eval loss**: 0.093- **Token accuracy**: 96.1%## Usage3 unmodified linesmodel = AutoModelForCausalLM.from_pretrained("McClain/PlasmidLM", trust_remote_code=True)tokenizer = AutoTokenizer.from_pretrained("McClain/PlasmidLM", trust_remote_code=True)# Condition on antibiotic resistance + origin of replicationprompt = "<BOS><AMR_KANAMYCIN><ORI_COLE1><SEP>"inputs = tokenizer(prompt, return_tensors="pt")outputs = model.generate(**inputs, max_new_tokens=4096, temperature=0.8, do_sample=True, top_p=0.95)print(tokenizer.decode(outputs[0].tolist()))```The model generates plasmid DNA sequences conditioned on functional annotations (antibiotic resistance markers, origins of replication, promoters, reporters, etc.) provided as special tokens in the prompt.## Input Format```2 unmodified linesThe model generates DNA bases (A/T/C/G) after the `<SEP>` token until it produces `<EOS>` or hits the maximum length.## Special Tokens| Token | Purpose ||---|---|| `<BOS>` | Beginning of sequence || `<EOS>` | End of sequence || `<SEP>` | Separator between prompt annotations and DNA sequence || `<PAD>` | Padding || `<AMR_*>` | Antibiotic resistance markers (e.g., `<AMR_KANAMYCIN>`, `<AMR_AMPICILLIN>`) || `<ORI_*>` | Origins of replication (e.g., `<ORI_COLE1>`, `<ORI_P15A>`) || `<PROM_*>` | Promoters (e.g., `<PROM_CMV>`, `<PROM_T7>`) || `<REP_*>` | Reporters (e.g., `<REP_EGFP>`, `<REP_MCHERRY>`) |## Related Models- [McClain/PlasmidLM-kmer6](https://huggingface.co/McClain/PlasmidLM-kmer6) — kmer6 tokenizer, 19.3M params, dense- [McClain/PlasmidLM-kmer6-MoE](https://huggingface.co/McClain/PlasmidLM-kmer6-MoE) — kmer6 tokenizer, 78.3M total params, Mixture-of-Experts## Limitations- This is a **pretrained base model** -- generated sequences are not optimized for functional element placement. Post-training with RL improves fidelity.- Generated sequences are **not experimentally validated**. Always verify computationally and experimentally before synthesis.- Trained on Addgene plasmids, which are biased toward commonly deposited vectors.- Maximum context of 16K tokens (~16 kbp).## Citation```bibtex@misc{thiel2026plasmidlm,title={PlasmidLM: Language Models for Plasmid DNA Generation},author={Thiel, McClain},year={2026}}```
24 unmodified lines252627282924 unmodified lines"tokenization_plasmid_lm.PlasmidLMTokenizer",null]}}24 unmodified lines252627282930313224 unmodified lines"tokenization_plasmid_lm.PlasmidLMTokenizer",null]},"use_moe": false,"tie_word_embeddings": true,"use_cache": false}
17 unmodified lines1819202122234 unmodified lines28293031323317 unmodified linesmax_position_embeddings: int = 16384,rope_theta: float = 10000.0,tie_word_embeddings: bool = True,**kwargs,):self.hidden_size = hidden_size4 unmodified linesself.rms_norm_eps = rms_norm_epsself.max_position_embeddings = max_position_embeddingsself.rope_theta = rope_thetasuper().__init__(vocab_size=vocab_size,tie_word_embeddings=tie_word_embeddings,17 unmodified lines181920212223242526272829303132334 unmodified lines3839404142434445464748495051525317 unmodified linesmax_position_embeddings: int = 16384,rope_theta: float = 10000.0,tie_word_embeddings: bool = True,# MoEuse_moe: bool = False,num_experts: int = 6,num_experts_per_tok: int = 2,moe_intermediate_size: int | None = None,aux_loss_coef: float = 0.01,# Tokenizer metadata (informational, saved in checkpoint)tokenizer_type: str = "char",kmer_k: int | None = None,kmer_stride: int | None = None,**kwargs,):self.hidden_size = hidden_size4 unmodified linesself.rms_norm_eps = rms_norm_epsself.max_position_embeddings = max_position_embeddingsself.rope_theta = rope_theta# MoEself.use_moe = use_moeself.num_experts = num_expertsself.num_experts_per_tok = num_experts_per_tokself.moe_intermediate_size = moe_intermediate_size or intermediate_sizeself.aux_loss_coef = aux_loss_coef# Tokenizer metadataself.tokenizer_type = tokenizer_typeself.kmer_k = kmer_kself.kmer_stride = kmer_stridesuper().__init__(vocab_size=vocab_size,tie_word_embeddings=tie_word_embeddings,
13 unmodified lines14151617181928 unmodified lines4849505152539 unmodified lines636465666768697015 unmodified lines868788899091922 unmodified lines95969798991001011021031041051061071081091 unmodified line1111121131141151161171181191201211221231241251261278 unmodified lines13613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218336 unmodified lines22022122222322422531 unmodified lines2572582592602612622635 unmodified lines26927027127227327414 unmodified lines2892902912922932942952968 unmodified lines30530630730830931031113 unmodified linesfrom transformers.modeling_outputs import CausalLMOutputWithPastfrom .configuration_plasmid_lm import PlasmidLMConfigdef _rope_freqs(dim: int, max_len: int, base: float) -> Tuple[torch.Tensor, torch.Tensor]:28 unmodified linesrope_sin: torch.Tensor,past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,position_offset: int = 0,) -> Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:B, S, _ = hidden_states.shapeq = self.q_proj(hidden_states).view(B, S, self.num_heads, self.head_dim).transpose(1, 2)9 unmodified linesv = torch.cat([past_key_value[1], v], dim=2)new_kv = (k, v)use_causal = past_key_value is Noneattn = F.scaled_dot_product_attention(q, k, v, is_causal=use_causal)out = attn.transpose(1, 2).reshape(B, S, -1)return self.o_proj(out), new_kv15 unmodified linesself.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)self.self_attn = PlasmidLMAttention(config)self.post_attention_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)self.mlp = PlasmidLMMLP(config)def forward(self,2 unmodified linesrope_sin: torch.Tensor,past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,position_offset: int = 0,) -> Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:residual = hidden_stateshidden_states = self.input_layernorm(hidden_states)attn_out, new_kv = self.self_attn(hidden_states, rope_cos, rope_sin, past_key_value, position_offset)hidden_states = residual + attn_outresidual = hidden_stateshidden_states = residual + self.mlp(self.post_attention_layernorm(hidden_states))return hidden_states, new_kvclass PlasmidLMPreTrainedModel(PreTrainedModel):1 unmodified linebase_model_prefix = "model"supports_gradient_checkpointing = Truedef _init_weights(self, module):if isinstance(module, PlasmidLMModel):# Recompute RoPE buffers — they are non-persistent so not saved in# safetensors. from_pretrained's fast-init path zeros them out.head_dim = self.config.hidden_size // self.config.num_attention_headscos, sin = _rope_freqs(head_dim, self.config.max_position_embeddings, self.config.rope_theta)module.rope_cos = cosmodule.rope_sin = sindef _set_gradient_checkpointing(self, module, value=False):if isinstance(module, PlasmidLMModel):module.gradient_checkpointing = value8 unmodified linesself.layers = nn.ModuleList([PlasmidLMDecoderLayer(config) for _ in range(config.num_hidden_layers)])self.norm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)head_dim = config.hidden_size // config.num_attention_headscos, sin = _rope_freqs(head_dim, config.max_position_embeddings, config.rope_theta)self.register_buffer("rope_cos", cos, persistent=False)self.register_buffer("rope_sin", sin, persistent=False)self.gradient_checkpointing = Falseself.post_init()def get_input_embeddings(self):return self.embed_tokensdef set_input_embeddings(self, value):self.embed_tokens = valuedef forward(self,input_ids: torch.Tensor,past_key_values: Optional[list] = None,position_offset: int = 0,**kwargs,) -> Tuple[torch.Tensor, list]:hidden_states = self.embed_tokens(input_ids)new_kv_caches = []for i, layer in enumerate(self.layers):past_kv = past_key_values[i] if past_key_values else Noneif self.gradient_checkpointing and self.training:# Gradient checkpointing recomputes activations on backward — no past_kv during trainingdef make_ckpt_fn(l):def fn(h, cos, sin):out, kv = l(h, cos, sin, None, 0)return out, kv[0], kv[1]return fnhidden_states, k, v = torch.utils.checkpoint.checkpoint(make_ckpt_fn(layer), hidden_states, self.rope_cos, self.rope_sin,use_reentrant=False,)new_kv = (k, v)else:hidden_states, new_kv = layer(hidden_states, self.rope_cos, self.rope_sin, past_kv, position_offset)new_kv_caches.append(new_kv)hidden_states = self.norm(hidden_states)return hidden_states, new_kv_cachesclass PlasmidLMForCausalLM(PlasmidLMPreTrainedModel, GenerationMixin):36 unmodified linesreturn {"input_ids": input_ids,"past_key_values": past_key_values,"use_cache": True,}31 unmodified linesif kv_list is not None:position_offset = kv_list[0][0].shape[2]hidden_states, new_kv_list = self.model(input_ids, kv_list, position_offset)logits = self.lm_head(hidden_states)loss = None5 unmodified linesshift_labels.view(-1),ignore_index=-100,)new_cache = Noneif use_cache:14 unmodified linestop_k: int = 50,) -> torch.Tensor:"""Simple autoregressive generation with KV cache."""# Prefillhidden_states, kv_caches = self.model(input_ids)logits = self.lm_head(hidden_states[:, -1:, :]).squeeze(1)cur_len = input_ids.shape[1]8 unmodified linesnext_token = torch.multinomial(probs, 1)input_ids = torch.cat([input_ids, next_token], dim=1)hidden_states, kv_caches = self.model(next_token, kv_caches, cur_len)logits = self.lm_head(hidden_states).squeeze(1)cur_len += 113 unmodified lines1415161718192028 unmodified lines495051525354559 unmodified lines656667686970717273747515 unmodified lines9192939495969798991001012 unmodified lines1041051061071081091101111121131141151161171181191201211221231241 unmodified line1261271281291301318 unmodified lines14014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225336 unmodified lines29029129229329429529631 unmodified lines3283293303313323333343353365 unmodified lines34234334434534634734814 unmodified lines3633643653663673683693708 unmodified lines37938038138238338438513 unmodified linesfrom transformers.modeling_outputs import CausalLMOutputWithPastfrom .configuration_plasmid_lm import PlasmidLMConfigfrom .moe import PlasmidLMSparseMoEdef _rope_freqs(dim: int, max_len: int, base: float) -> Tuple[torch.Tensor, torch.Tensor]:28 unmodified linesrope_sin: torch.Tensor,past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,position_offset: int = 0,attention_mask: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:B, S, _ = hidden_states.shapeq = self.q_proj(hidden_states).view(B, S, self.num_heads, self.head_dim).transpose(1, 2)9 unmodified linesv = torch.cat([past_key_value[1], v], dim=2)new_kv = (k, v)if attention_mask is not None:attn = F.scaled_dot_product_attention(q, k, v, attn_mask=attention_mask)else:use_causal = past_key_value is Noneattn = F.scaled_dot_product_attention(q, k, v, is_causal=use_causal)out = attn.transpose(1, 2).reshape(B, S, -1)return self.o_proj(out), new_kv15 unmodified linesself.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)self.self_attn = PlasmidLMAttention(config)self.post_attention_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)self.use_moe = config.use_moeif self.use_moe:self.moe = PlasmidLMSparseMoE(config)else:self.mlp = PlasmidLMMLP(config)def forward(self,2 unmodified linesrope_sin: torch.Tensor,past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,position_offset: int = 0,attention_mask: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor], torch.Tensor]:residual = hidden_stateshidden_states = self.input_layernorm(hidden_states)attn_out, new_kv = self.self_attn(hidden_states, rope_cos, rope_sin, past_key_value, position_offset, attention_mask)hidden_states = residual + attn_outresidual = hidden_statesif self.use_moe:moe_out, aux_loss = self.moe(self.post_attention_layernorm(hidden_states))hidden_states = residual + moe_outelse:hidden_states = residual + self.mlp(self.post_attention_layernorm(hidden_states))aux_loss = torch.tensor(0.0, device=hidden_states.device)return hidden_states, new_kv, aux_lossclass PlasmidLMPreTrainedModel(PreTrainedModel):1 unmodified linebase_model_prefix = "model"supports_gradient_checkpointing = Truedef _set_gradient_checkpointing(self, module, value=False):if isinstance(module, PlasmidLMModel):module.gradient_checkpointing = value8 unmodified linesself.layers = nn.ModuleList([PlasmidLMDecoderLayer(config) for _ in range(config.num_hidden_layers)])self.norm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)# Lazy RoPE: computed on first forward call to ensure correct device# placement after from_pretrained (which uses meta device tensors).self.register_buffer("rope_cos", None, persistent=False)self.register_buffer("rope_sin", None, persistent=False)self.gradient_checkpointing = Falseself.post_init()def _init_rope(self, device: torch.device) -> None:"""Compute and cache RoPE cos/sin on the given device."""head_dim = self.config.hidden_size // self.config.num_attention_headscos, sin = _rope_freqs(head_dim, self.config.max_position_embeddings, self.config.rope_theta)self.register_buffer("rope_cos", cos.to(device), persistent=False)self.register_buffer("rope_sin", sin.to(device), persistent=False)def get_input_embeddings(self):return self.embed_tokensdef set_input_embeddings(self, value):self.embed_tokens = valuedef _build_4d_attention_mask(self,attention_mask: Optional[torch.Tensor],seq_len: int,past_seq_len: int,device: torch.device,dtype: torch.dtype,) -> Optional[torch.Tensor]:"""Build a 4D causal+padding mask for SDPA.Returns (B, 1, S, S+past) float mask with 0 for attend and -inf for ignore,or None if no masking is needed (no padding, no past KV)."""if attention_mask is None and past_seq_len == 0:# No padding and no cache — SDPA's is_causal=True handles thisreturn Nonetotal_len = past_seq_len + seq_len# Causal mask: each query position can attend to itself and all prior positionscausal = torch.triu(torch.full((seq_len, total_len), float("-inf"), device=device, dtype=dtype),diagonal=past_seq_len + 1,) # (S, S+past)mask_4d = causal.unsqueeze(0).unsqueeze(0) # (1, 1, S, S+past)if attention_mask is not None:# attention_mask is (B, total_len) with 1=attend, 0=ignore# Use a large finite negative instead of -inf for padding mask.# With left-padding, the first padding positions can only attend# to other padding positions (causal blocks future). If we use# -inf, ALL keys are blocked → softmax([-inf,...]) = NaN.# Using min_dtype keeps at least the self-attention score finite,# so softmax produces a valid (though meaningless) output.min_dtype = torch.finfo(dtype).minpad_mask = torch.where(attention_mask[:, None, None, :].bool(),torch.zeros(1, device=device, dtype=dtype),torch.tensor(min_dtype, device=device, dtype=dtype),) # (B, 1, 1, total_len)mask_4d = mask_4d + pad_maskreturn mask_4ddef forward(self,input_ids: torch.Tensor,past_key_values: Optional[list] = None,position_offset: int = 0,attention_mask: Optional[torch.Tensor] = None,**kwargs,) -> Tuple[torch.Tensor, list, torch.Tensor]:# Lazy RoPE init: compute on first forward for correct device placementif self.rope_cos is None:self._init_rope(input_ids.device)hidden_states = self.embed_tokens(input_ids)past_seq_len = past_key_values[0][0].shape[2] if past_key_values else 0mask_4d = self._build_4d_attention_mask(attention_mask, input_ids.shape[1], past_seq_len,input_ids.device, hidden_states.dtype,)new_kv_caches = []total_aux_loss = torch.tensor(0.0, device=input_ids.device)for i, layer in enumerate(self.layers):past_kv = past_key_values[i] if past_key_values else Noneif self.gradient_checkpointing and self.training:# Gradient checkpointing recomputes activations on backward — no past_kv during trainingdef make_ckpt_fn(l):def fn(h, cos, sin):out, kv, aux = l(h, cos, sin, None, 0)return out, kv[0], kv[1], auxreturn fnhidden_states, k, v, layer_aux = torch.utils.checkpoint.checkpoint(make_ckpt_fn(layer), hidden_states, self.rope_cos, self.rope_sin,use_reentrant=False,)new_kv = (k, v)else:hidden_states, new_kv, layer_aux = layer(hidden_states, self.rope_cos, self.rope_sin, past_kv, position_offset, mask_4d)new_kv_caches.append(new_kv)total_aux_loss = total_aux_loss + layer_auxhidden_states = self.norm(hidden_states)return hidden_states, new_kv_caches, total_aux_lossclass PlasmidLMForCausalLM(PlasmidLMPreTrainedModel, GenerationMixin):36 unmodified linesreturn {"input_ids": input_ids,"past_key_values": past_key_values,"attention_mask": attention_mask,"use_cache": True,}31 unmodified linesif kv_list is not None:position_offset = kv_list[0][0].shape[2]hidden_states, new_kv_list, aux_loss = self.model(input_ids, kv_list, position_offset, attention_mask=attention_mask)logits = self.lm_head(hidden_states)loss = None5 unmodified linesshift_labels.view(-1),ignore_index=-100,)loss = loss + self.config.aux_loss_coef * aux_lossnew_cache = Noneif use_cache:14 unmodified linestop_k: int = 50,) -> torch.Tensor:"""Simple autoregressive generation with KV cache."""# Prefill (aux_loss ignored during generation)hidden_states, kv_caches, _ = self.model(input_ids)logits = self.lm_head(hidden_states[:, -1:, :]).squeeze(1)cur_len = input_ids.shape[1]8 unmodified linesnext_token = torch.multinomial(probs, 1)input_ids = torch.cat([input_ids, next_token], dim=1)hidden_states, kv_caches, _ = self.model(next_token, kv_caches, cur_len)logits = self.lm_head(hidden_states).squeeze(1)cur_len += 1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"""Mixture of Experts (Mixtral-style) for PlasmidLM."""
from __future__ import annotations
import torchimport torch.nn as nnimport torch.nn.functional as F
from .configuration_plasmid_lm import PlasmidLMConfig
class PlasmidLMExpertMLP(nn.Module): """Single expert MLP — same architecture as PlasmidLMMLP."""
def __init__(self, hidden_size: int, intermediate_size: int): super().__init__() self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False) self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False) self.act = nn.GELU()
def forward(self, x: torch.Tensor) -> torch.Tensor: return self.down_proj(self.act(self.up_proj(x)))
class PlasmidLMSparseMoE(nn.Module): """Sparse Mixture of Experts with top-k routing and load balancing loss.
Implements Mixtral-style routing: softmax over all experts, then select top-k. The output is a weighted sum of the selected experts' outputs.
Load balancing auxiliary loss: L_aux = N * sum(f_i * P_i) where f_i = fraction of tokens routed to expert i, P_i = mean routing probability for expert i. """
def __init__(self, config: PlasmidLMConfig): super().__init__() self.num_experts = config.num_experts self.top_k = config.num_experts_per_tok intermediate = config.moe_intermediate_size
self.router = nn.Linear(config.hidden_size, self.num_experts, bias=False) self.experts = nn.ModuleList( [PlasmidLMExpertMLP(config.hidden_size, intermediate) for _ in range(self.num_experts)] )
def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: """ Args: hidden_states: (batch, seq_len, hidden_size)
Returns: output: (batch, seq_len, hidden_size) aux_loss: scalar load balancing loss """ batch, seq_len, hidden = hidden_states.shape flat = hidden_states.view(-1, hidden) # (B*S, H) num_tokens = flat.shape[0]
# Router: compute probabilities over experts router_logits = self.router(flat) # (B*S, num_experts) router_probs = F.softmax(router_logits, dim=-1)
# Top-k selection top_weights, top_indices = torch.topk(router_probs, self.top_k, dim=-1) # (B*S, top_k) # Normalize selected weights to sum to 1 top_weights = top_weights / top_weights.sum(dim=-1, keepdim=True)
# Dispatch: loop over experts, gather tokens, compute, scatter back output = torch.zeros_like(flat) for i, expert in enumerate(self.experts): # Mask for tokens where expert i is in the top-k mask = (top_indices == i).any(dim=-1) # (B*S,) if not mask.any(): continue expert_input = flat[mask] # (n_tokens, H) expert_output = expert(expert_input) # (n_tokens, H) # Weight for this expert for selected tokens # Find which top-k slot(s) matched and get corresponding weight match_positions = (top_indices[mask] == i) # (n_tokens, top_k) weights = (top_weights[mask] * match_positions.float()).sum(dim=-1, keepdim=True) # (n_tokens, 1) output[mask] += weights * expert_output
output = output.view(batch, seq_len, hidden)
# Load balancing auxiliary loss # f_i: fraction of tokens dispatched to expert i # P_i: mean routing probability assigned to expert i with torch.no_grad(): # Count tokens per expert (based on top-k assignments) expert_counts = torch.zeros(self.num_experts, device=flat.device) for k in range(self.top_k): expert_counts.scatter_add_(0, top_indices[:, k], torch.ones(num_tokens, device=flat.device)) f = expert_counts / (num_tokens * self.top_k) # fraction
P = router_probs.mean(dim=0) # (num_experts,) aux_loss = self.num_experts * (f * P).sum()
return output, aux_loss61 unmodified lines62636465666761 unmodified linesdef vocab_size(self) -> int:return len(self._vocab)def get_vocab(self) -> dict:return dict(self._vocab)61 unmodified lines6263646566676869707172737475767778798081828361 unmodified linesdef vocab_size(self) -> int:return len(self._vocab)@propertydef pad_token_id(self) -> int:return self._vocab.get("<PAD>", 0)@propertydef bos_token_id(self) -> int:return self._vocab.get("<BOS>", 1)@propertydef eos_token_id(self) -> int:return self._vocab.get("<EOS>", 2)@propertydef sep_token_id(self) -> int:return self._vocab.get("<SEP>", 3)def get_vocab(self) -> dict:return dict(self._vocab)
README.md+48-73
config.json+4-1
configuration_plasmid_lm.py+20
modeling_plasmid_lm.py+106-32
moe.pyAdded+99
tokenization_plasmid_lm.py+16