MedSLM-124M

⚠️ Medical disclaimer: for learning and research only. This is a small experimental model. Its answers are often wrong, incomplete or made up, even when they sound confident. It is not a medical device, has not been clinically evaluated, and must not be used to diagnose, treat or make any health decision. Always consult a qualified healthcare professional. In an emergency, call your local emergency number.

Model summary

MedSLM-124M is a 124M-parameter GPT-style language model, built and trained from scratch as a learning project (based on Andrej Karpathy's nanoGPT). It was pretrained on ~2.0B tokens of public medical text, then fine-tuned to answer health questions in a simple question/answer format.

  • Developed by: adityas181
  • Model type: decoder-only transformer (GPT-2 small size)
  • Language: English
  • License: CC BY-NC-SA 4.0 (non-commercial)

Architecture

Setting Value
Parameters 124.0M
Layers / heads / embedding size 12 / 12 / 768
Context length 512 tokens
Vocabulary 50,257 tokens (GPT-2 tokenizer via tiktoken)
Details pre-LayerNorm, GELU, flash attention, tied input/output embeddings, dropout 0.0

Training data

Stage Dataset Content License
Pretraining MedRAG/pubmed (every 5th file) ~6M PubMed titles and abstracts (2023 baseline) NLM terms; some abstracts are copyrighted by their publishers
Pretraining epfl-llm/guidelines ~36K clinical practice guidelines see the dataset card
Pretraining zhengyun21/PMC-Patients ~167K patient summaries from published case reports CC BY-NC-SA 4.0
Fine-tuning keivalya/MedQuad-MedicalQnADataset ~14K NIH question/answer pairs, cleaned and cut to ~100 words CC BY 4.0 (MedQuAD)
Fine-tuning HuggingFaceTB/everyday-conversations-llama3.1-2k ~2.3K greeting exchanges (generated with Llama 3.1 70B) Apache 2.0
Fine-tuning hand-written 35 questions about the model itself (who it is, what it can't do) -

The patient summaries come from published, de-identified case reports. No private patient records were used.

Training procedure

  • Pretraining: 120,000 batches of 32 × 512 tokens (~2.0B tokens), weights updated every 8 batches. AdamW (betas 0.9/0.95, weight decay 0.1), learning rate 6e-04 with 4,000 warmup steps and cosine decay to 6e-05, bfloat16 mixed precision, gradient clipping 0.5. The checkpoint with the lowest validation loss was kept.
  • Fine-tuning: about 3 passes over the question/answer data with a constant learning rate of 5e-5 (AdamW), keeping the checkpoint with the lowest validation loss.
  • Hardware: one NVIDIA A100 GPU on Google Colab.

Results

Stage Validation loss
Pretraining (medical text) 2.443
Fine-tuning (questions and answers) 1.522

Pretraining loss curve

The model has not been evaluated on medical benchmarks such as MedQA or PubMedQA.

Example answers

Generated when this card was written (temperature 0.5, top-k 50, repetition penalty 1.2). They sound fluent, but the facts are often wrong.

Q: Hello!
A: Hello! I'm MedSLM, a small medical language model. How can I help you today?

Q: Who are you?
A: I'm MedSLM, a small language model trained from scratch on medical text. I can answer basic health questions, but I'm not a doctor.

Q: What are the symptoms of diabetes?
A: The most common signs and symptoms of diabetes are - blood pressure - weight loss - fatigue - feeling tired - confusion - dizziness, light-headedness - nausea - blurred vision - pain in one or both eyes - dry mouth - dark urine - trouble seeing - headache - diarrhea - constipation Diabetic retinopathy is the leading cause of blindness among people with type 2 diabetes.

Q: What causes high blood pressure?
A: The cause of high blood pressure is not known. The condition can be caused by a variety of factors, including - genetic or environmental - lifestyle - medications and infections (including pneumonia).

Q: What is asthma?
A: Asthma is a common, chronic disease. You can get it from asthma or another allergic reaction to an inhaled medication called a medicine called corticosteroids. The doctor may prescribe corticosteroids for people with mild asthma who do not respond well to treatment. In severe cases of asthma, the doctor will treat you with a new medication called a beta 2-adrenergic agonist compound. This drug works by blocking the action of calcium channels in airway smooth muscle cells and also prevents the symptoms of the asthma that are caused by this medication.

How to use

# pip install torch tiktoken safetensors huggingface_hub numpy tqdm
import os, sys, json, torch, tiktoken
from huggingface_hub import snapshot_download
from safetensors.torch import load_model

path = snapshot_download("adityas181/MedSLM-124M")
sys.path.append(path)
from model import GPT, GPTConfig

model = GPT(GPTConfig(**json.load(open(os.path.join(path, "config.json")))))
load_model(model, os.path.join(path, "model.safetensors"))
model.eval()

enc = tiktoken.get_encoding("gpt2")
prompt = "### Question:\nWhat is asthma?\n### Answer:\n"
idx = torch.tensor([[enc.eot_token] + enc.encode_ordinary(prompt)])
out = model.generate(idx, max_new_tokens=150, temperature=0.5, top_k=50)
print(enc.decode(out[0, idx.size(1):].tolist()).split("<|endoftext|>")[0])

Prompt format: an <|endoftext|> token, then

### Question:
<your question>
### Answer:

The model writes an answer and ends it with <|endoftext|>.

Limitations and risks

  • It is a small model (~124M parameters) trained on ~2B tokens: it often makes up facts, mixes up conditions and can repeat itself.
  • It only knows what was in its training text, which may be outdated or incomplete.
  • It can repeat biases found in the medical literature, and may reproduce text it saw in training.
  • English only. It has not been safety-tested and may give harmful advice.
  • Out of scope: any clinical use, diagnosis, treatment, triage or medication dosing, and commercial use (not allowed by the license).

License and attribution

Released under CC BY-NC-SA 4.0, because part of the training data (PMC-Patients) uses that license: you may share and adapt this model for non-commercial purposes, with attribution, under the same license. Please also respect the license of each training dataset listed above.

  • Model code based on nanoGPT by Andrej Karpathy (MIT License).
  • Tokenizer: GPT-2 BPE via tiktoken (MIT License).
  • MedQuAD: Ben Abacha and Demner-Fushman, "A question-entailment approach to question answering", BMC Bioinformatics, 2019.
  • PMC-Patients: Zhao et al., Scientific Data, 2023.
  • Clinical guidelines corpus: Chen et al., "MEDITRON-70B: Scaling Medical Pretraining for Large Language Models", 2023.
  • PubMed corpus: Xiong et al., "Benchmarking Retrieval-Augmented Generation for Medicine", 2024.
Downloads last month
-
Safetensors
Model size
0.1B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Datasets used to train adityas181/MedSLM-124M