Run this model with HF Jobs
Run Synthyra/ESMplusplus_large 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 to fine-tune https://huggingface.co/Synthyra/ESMplusplus_large. 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/Synthyra/ESMplusplus_large 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.
Instructions to use Synthyra/ESMplusplus_large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Synthyra/ESMplusplus_large with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="Synthyra/ESMplusplus_large", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForMaskedLM model = AutoModelForMaskedLM.from_pretrained("Synthyra/ESMplusplus_large", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload modeling_esm_plusplus.py with huggingface_hub
Browse files930 unmodified lines93193293393493593632 unmodified lines96997097197297397497597697797815 unmodified lines994995996997998999197 unmodified lines119711981199930 unmodified linesself.mse = nn.MSELoss()self.ce = nn.CrossEntropyLoss()self.bce = nn.BCEWithLogitsLoss()self.init_weights()def _embed(self, input_ids: torch.Tensor, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor:32 unmodified linesoutput_hidden_states=output_hidden_states)x = output.last_hidden_statecls_features = x[:, 0, :]mean_features = self.mean_pooling(x, attention_mask)# we include mean pooling features to help with early convergence, the cost of this is basically zerofeatures = torch.cat([cls_features, mean_features], dim=-1)logits = self.classifier(features)loss = Noneif labels is not None:15 unmodified linesloss = self.ce(logits.view(-1, self.num_labels), labels.view(-1))elif self.config.problem_type == "multi_label_classification":loss = self.bce(logits, labels)return ESMplusplusOutput(loss=loss,logits=logits,197 unmodified lines@propertydef special_token_ids(self):return self.all_special_ids930 unmodified lines93193293393493593693732 unmodified lines97097197297397497597615 unmodified lines992993994995996997998197 unmodified lines1196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292930 unmodified linesself.mse = nn.MSELoss()self.ce = nn.CrossEntropyLoss()self.bce = nn.BCEWithLogitsLoss()self.pooler = Pooler(['cls','mean'])self.init_weights()def _embed(self, input_ids: torch.Tensor, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor:32 unmodified linesoutput_hidden_states=output_hidden_states)x = output.last_hidden_statefeatures = self.pooler(x, attention_mask)logits = self.classifier(features)loss = Noneif labels is not None:15 unmodified linesloss = self.ce(logits.view(-1, self.num_labels), labels.view(-1))elif self.config.problem_type == "multi_label_classification":loss = self.bce(logits, labels)return ESMplusplusOutput(loss=loss,logits=logits,197 unmodified lines@propertydef special_token_ids(self):return self.all_special_idsif __name__ == "__main__":# Set device to CPU for testingdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")print(f"Using device: {device}")# Test tokenizertokenizer = EsmSequenceTokenizer()sample_sequence = "MQIFVKTLTGKTITLEVEPSDTIENVKAKIQDKEGIPPDQQRLIFAGKQLEDGRTLSDYNIQKESTLHLVLRLRGG"encoding = tokenizer(sample_sequence, return_tensors="pt")print(f"Input sequence length: {len(sample_sequence)}")print(f"Tokenized sequence: {encoding['input_ids'].shape}")# Prepare inputsinput_ids = encoding['input_ids'].to(device)attention_mask = encoding['attention_mask'].to(device)# Test base model with smaller config for quick testingprint("\n=== Testing ESMplusplus Base Model ===")base_config = ESMplusplusConfig(hidden_size=384,num_attention_heads=6,num_hidden_layers=4)base_model = ESMplusplusModel(base_config).to(device)with torch.no_grad():outputs = base_model(input_ids=input_ids, attention_mask=attention_mask)print(f"Last hidden state shape: {outputs.last_hidden_state.shape}")# Test embedding functionalityprint("\nTesting embedding functionality:")with torch.no_grad():embeddings = base_model._embed(input_ids, attention_mask)print(f"Embedding shape: {embeddings.shape}")# Test masked language modelingprint("\n=== Testing ESMplusplus For Masked LM ===")mlm_model = ESMplusplusForMaskedLM(base_config).to(device)with torch.no_grad():outputs = mlm_model(input_ids=input_ids, attention_mask=attention_mask)print(f"Last hidden state shape: {outputs.last_hidden_state.shape}")print(f"Logits shape: {outputs.logits.shape}")# Test sequence classification modelprint("\n=== Testing Sequence Classification Model ===")classification_model = ESMplusplusForSequenceClassification(base_config).to(device)with torch.no_grad():outputs = classification_model(input_ids=input_ids, attention_mask=attention_mask)print(f"Last hidden state shape: {outputs.last_hidden_state.shape}")print(f"Logits shape: {outputs.logits.shape}")# Test token classification modelprint("\n=== Testing Token Classification Model ===")token_model = ESMplusplusForTokenClassification(base_config).to(device)with torch.no_grad():outputs = token_model(input_ids=input_ids, attention_mask=attention_mask)print(f"Last hidden state shape: {outputs.last_hidden_state.shape}")print(f"Logits shape: {outputs.logits.shape}")# Test embedding dataset functionality with a mini datasetprint("\n=== Testing Embed Dataset Functionality ===")mini_dataset = [sample_sequence, sample_sequence[:50], sample_sequence[:30]]print(f"Creating embeddings for {len(mini_dataset)} sequences")# Only run this if save path doesn't exist to avoid overwritingif not os.path.exists("test_embeddings.pth"):embeddings = mlm_model.embed_dataset(sequences=mini_dataset,tokenizer=tokenizer,batch_size=2,max_len=100,full_embeddings=False,pooling_types=['mean'],save_path="test_embeddings.pth")if embeddings:print(f"Embedding dictionary size: {len(embeddings)}")for seq, emb in embeddings.items():print(f"Sequence length: {len(seq)}, Embedding shape: {emb.shape}")breakelse:print("Skipping embedding test as test_embeddings.pth already exists")print("\nAll tests completed successfully!")
modeling_esm_plusplus.py+97-4