Text Generation
Transformers
PyTorch
gpt2
chemistry
molecule
drug
custom_code
text-generation-inference
Instructions to use entropy/roberta_zinc_decoder with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use entropy/roberta_zinc_decoder with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="entropy/roberta_zinc_decoder", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("entropy/roberta_zinc_decoder", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("entropy/roberta_zinc_decoder", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use entropy/roberta_zinc_decoder with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "entropy/roberta_zinc_decoder" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "entropy/roberta_zinc_decoder", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/entropy/roberta_zinc_decoder
- SGLang
How to use entropy/roberta_zinc_decoder 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 "entropy/roberta_zinc_decoder" \ --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": "entropy/roberta_zinc_decoder", "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 "entropy/roberta_zinc_decoder" \ --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": "entropy/roberta_zinc_decoder", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use entropy/roberta_zinc_decoder with Docker Model Runner:
docker model run hf.co/entropy/roberta_zinc_decoder
| import pandas as pd | |
| import os | |
| import torch | |
| from transformers import RobertaTokenizerFast, RobertaForMaskedLM, DataCollatorWithPadding | |
| import datasets | |
| from datasets import disable_caching | |
| disable_caching() | |
| DEVICE = 'cuda:0' # model device | |
| ENCODER_MODEL_NAME = "entropy/roberta_zinc_480m" # encoder name | |
| ENCODER_BATCH_SIZE = 1024 # batch size for computing embeddings | |
| TOKENIZER_MAX_LEN = 256 # max_length param on tokenizer | |
| TOKENIZATION_NUM_PROC = 32 # number of processes for tokenization | |
| ''' | |
| Data source is expected to be a CSV file with a column of SMILES strings | |
| denoted by `SMILES_COLUMN`. The CSV is processed in chunks of size `PROCESS_CHUNKSIZE`. | |
| Processed chunks are saved to `SAVE_PATH` with the format `SAVE_PATH/processed_shard_{i}.hf` | |
| ''' | |
| DATASET_CSV_FILENAME = None # path to data csv | |
| PROCESS_CHUNKSIZE = 1000000 # how many rows to process/save for each dataset shard | |
| SMILES_COLUMN = 'smiles' # csv column holding smiles strings | |
| MAX_CHUNKS = None # total number of chunks to process (if None, all chunks are processed) | |
| MAX_SMILES_LENGTH = 90 # max smiles string length (exclusive) | |
| MIN_SMILES_LENGTH = 5 # min smiles string length (exclusive) | |
| FILTER_NUM_PROC = 32 # number of processes for filtering | |
| SAVE_PATH = None # directory to save data shards to | |
| assert DATASET_CSV_FILENAME is not None, "must specify dataset filename" | |
| assert SAVE_PATH is not None, "must specify save path" | |
| def tokenization(example): | |
| return tokenizer(example[SMILES_COLUMN], add_special_tokens=True, | |
| truncation=True, max_length=TOKENIZER_MAX_LEN) | |
| def embed(inputs): | |
| inputs = {k:inputs[k] for k in ['input_ids', 'attention_mask']} | |
| inputs = collator(inputs) | |
| inputs = {k:v.to(DEVICE) for k,v in inputs.items()} | |
| with torch.no_grad(): | |
| outputs = model(**inputs, output_hidden_states=True) | |
| full_embeddings = outputs[-1][-1] | |
| mask = inputs['attention_mask'] | |
| mean_embeddings = ((full_embeddings * mask.unsqueeze(-1)).sum(1) / mask.sum(-1).unsqueeze(-1)) | |
| return {'encoder_hidden_states' : mean_embeddings} | |
| def length_filter_smiles(example): | |
| min_check = (len(example[SMILES_COLUMN])>MIN_SMILES_LENGTH) if (MIN_SMILES_LENGTH is not None) else True | |
| max_check = (len(example[SMILES_COLUMN])<MAX_SMILES_LENGTH) if (MIN_SMILES_LENGTH is not None) else True | |
| type_check = type(example[SMILES_COLUMN])==str | |
| filter_pass = all([min_check, max_check, type_check]) | |
| return filter_pass | |
| tokenizer = RobertaTokenizerFast.from_pretrained(ENCODER_MODEL_NAME, max_len=TOKENIZER_MAX_LEN) | |
| collator = DataCollatorWithPadding(tokenizer, padding=True, return_tensors='pt') | |
| model = RobertaForMaskedLM.from_pretrained(ENCODER_MODEL_NAME) | |
| model.to(DEVICE) | |
| model.eval() | |
| df_iter = pd.read_csv(DATASET_CSV_FILENAME, chunksize=PROCESS_CHUNKSIZE, usecols=[SMILES_COLUMN]) | |
| for i, df in enumerate(df_iter): | |
| print(f'processing dataset chunk {i}') | |
| dataset = datasets.Dataset.from_pandas(df) | |
| dataset = dataset.filter(lambda example: length_filter_smiles(example), num_proc=FILTER_NUM_PROC) | |
| dataset = dataset.map(tokenization, batched=True, num_proc=TOKENIZATION_NUM_PROC) | |
| dataset = dataset.map(embed, batched=True, batch_size=ENCODER_BATCH_SIZE) | |
| dataset.save_to_disk(f'{SAVE_PATH}/processed_shard_{i}.hf') | |
| if (MAX_CHUNKS is not None) and (i >= MAX_CHUNKS-1): | |
| break | |
| print('finished data processing') | |