LLM on HPC

Here we provide an example of how one can run a Large-language model (LLM) on NYU Greene cluster

Prepare environment

pip install transformers

Prepare script

Create a python script using the following code from sections 1-9 and save it in a file called huggingface.py


import torch
import numpy as np
from transformers import AutoTokenizer, AutoModel

texts = ["How do I get a replacement Medicare card?",
        "What is the monthly premium for Medicare Part B?",
        "How do I terminate my Medicare Part B (medical insurance)?",
        "How do I sign up for Medicare?",
        "Can I sign up for Medicare Part B if I am working and have health insurance through an employer?",
        "How do I sign up for Medicare Part B if I already have Part A?"]

model_name = 'cardiffnlp/twitter-roberta-base-sentiment'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name, output_hidden_states=True)


ids = tokenizer(texts, padding=True, return_tensors="pt")

device = 'cuda' if torch.cuda.is_available() else 'cpu'model.to(device)ids = ids.to(device)
model.eval()

with torch.no_grad():
  out = model(**ids)

last_hidden_states = out.last_hidden_state
sentence_embedding = last_hidden_states[:, 0, :]
print("Shape of the batch embedding: {}".format(sentence_embedding.shape))

Prepare Sbatch file

After saving the above code in a script called huggingface.py, create a file called run.SBATCH with the the following code:

#!/bin/bash#SBATCH --nodes=1#SBATCH --ntasks-per-node=1#SBATCH --cpus-per-task=1#SBATCH --time=00:10:00#SBATCH --mem=64GB#SBATCH --gres=gpu#SBATCH --job-name=huggingface#SBATCH --output=huggingface.out
module purge
if [ -e /dev/nvidia0 ]; then nv="--nv"; fi
singularity exec $nv \  --overlay /scratch/netID/my_env/overlay-50G-10M.ext3:rw \  /scratch/work/public/singularity/cuda11.2.2-cudnn8-devel-ubuntu20.04.sif \  /bin/bash -c "source /ext3/env.sh; python /scratch/netID/path-to-script/huggingface.py"

Run the run.SBATCH file

sbatch run.SBATCH

The output can be found in huggingface.out

Acknowledgements

Instructions are developed and provided by Laiba Mehnaz, a member of AIfSR