Upload MedSLM: model weights, optimizer, tokenizer, config, and model card
Browse files- .gitattributes +1 -0
- README.md +133 -0
- config.json +18 -0
- loss_curves.png +3 -0
- model.safetensors +3 -0
- optimizer.pt +3 -0
- tokenizer/tokenizer.json +0 -0
- tokenizer/tokenizer_config.json +12 -0
- training_config.json +104 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
loss_curves.png filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
tags:
|
| 6 |
+
- medical
|
| 7 |
+
- language-model
|
| 8 |
+
- transformer
|
| 9 |
+
- pytorch
|
| 10 |
+
- pretraining
|
| 11 |
+
- slm
|
| 12 |
+
- rope
|
| 13 |
+
- swiglu
|
| 14 |
+
- gqa
|
| 15 |
+
- rmsnorm
|
| 16 |
+
datasets:
|
| 17 |
+
- Saminx22/medical_data_for_slm
|
| 18 |
+
pipeline_tag: text-generation
|
| 19 |
+
model-index:
|
| 20 |
+
- name: MedSLM
|
| 21 |
+
results: []
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# MedSLM — Medical Small Language Model (~381M Parameters)
|
| 25 |
+
|
| 26 |
+
A **381M parameter** transformer language model pre-trained on curated medical text from PubMed abstracts, PMC full-text articles, and clinical guidelines.
|
| 27 |
+
|
| 28 |
+
## Architecture
|
| 29 |
+
|
| 30 |
+
MedSLM uses a modern GPT-style transformer with several architectural improvements over the standard GPT-2 design:
|
| 31 |
+
|
| 32 |
+
| Component | Detail |
|
| 33 |
+
|---|---|
|
| 34 |
+
| **Normalization** | RMSNorm (faster than LayerNorm, used in LLaMA/Mistral) |
|
| 35 |
+
| **Positional Encoding** | Rotary Positional Embeddings (RoPE) — better length generalization |
|
| 36 |
+
| **Feed-Forward** | SwiGLU activation (gated FFN, outperforms GELU) |
|
| 37 |
+
| **Attention** | Grouped-Query Attention (GQA) — shared KV heads for efficiency |
|
| 38 |
+
| **Layers** | 24 transformer blocks |
|
| 39 |
+
| **Attention Heads** | 16 query heads, 8 KV heads |
|
| 40 |
+
| **Embedding Dim** | 1024 |
|
| 41 |
+
| **Context Length** | 1024 tokens |
|
| 42 |
+
| **Vocab Size** | 50,257 (GPT-2 BPE tokenizer) |
|
| 43 |
+
| **Parameters** | 381,373,440 (~381M) |
|
| 44 |
+
|
| 45 |
+
## Training
|
| 46 |
+
|
| 47 |
+
- **Dataset**: [`Saminx22/medical_data_for_slm`](https://huggingface.co/datasets/Saminx22/medical_data_for_slm) (~44M tokens)
|
| 48 |
+
- **Sources**: PubMed abstracts, PMC Open Access full-text, Clinical Guidelines
|
| 49 |
+
- **Tokenizer**: GPT-2 BPE tokenizer (50,257 vocab)
|
| 50 |
+
- **Optimizer**: AdamW (betas=0.9/0.95, weight_decay=0.1)
|
| 51 |
+
- **LR Schedule**: Linear warmup (1000 steps) + Cosine decay
|
| 52 |
+
- **Peak LR**: 0.0003
|
| 53 |
+
- **Precision**: bfloat16
|
| 54 |
+
- **Effective Batch Size**: 256
|
| 55 |
+
- **Max Steps**: 20,000
|
| 56 |
+
- **Best Val Loss**: 3.2198 (at step 19500)
|
| 57 |
+
|
| 58 |
+
## Usage
|
| 59 |
+
|
| 60 |
+
### Loading the Model
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
import torch
|
| 64 |
+
import json
|
| 65 |
+
from safetensors.torch import load_file
|
| 66 |
+
from transformers import AutoTokenizer
|
| 67 |
+
|
| 68 |
+
# Load config
|
| 69 |
+
with open("config.json") as f:
|
| 70 |
+
config_dict = json.load(f)
|
| 71 |
+
|
| 72 |
+
# Reconstruct model (requires the MedSLM class definition)
|
| 73 |
+
config = MedSLMConfig(**{k: v for k, v in config_dict.items()
|
| 74 |
+
if k in MedSLMConfig.__dataclass_fields__})
|
| 75 |
+
model = MedSLM(config)
|
| 76 |
+
|
| 77 |
+
# Load weights
|
| 78 |
+
state_dict = load_file("model.safetensors")
|
| 79 |
+
model.load_state_dict(state_dict)
|
| 80 |
+
model.eval()
|
| 81 |
+
|
| 82 |
+
# Load tokenizer
|
| 83 |
+
tokenizer = AutoTokenizer.from_pretrained("tokenizer/")
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
### Generating Text
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
prompt = "The patient presented with acute myocardial infarction"
|
| 90 |
+
input_ids = torch.tensor(tokenizer.encode(prompt)).unsqueeze(0)
|
| 91 |
+
|
| 92 |
+
output = model.generate(input_ids, max_new_tokens=200, temperature=0.8, top_k=50, top_p=0.9)
|
| 93 |
+
print(tokenizer.decode(output.squeeze().tolist()))
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
### Resuming Training
|
| 97 |
+
|
| 98 |
+
```python
|
| 99 |
+
# Load optimizer state
|
| 100 |
+
optimizer_state = torch.load("optimizer.pt")
|
| 101 |
+
optimizer.load_state_dict(optimizer_state)
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
## Files
|
| 105 |
+
|
| 106 |
+
| File | Description |
|
| 107 |
+
|---|---|
|
| 108 |
+
| `model.safetensors` | Model weights (safetensors format) |
|
| 109 |
+
| `optimizer.pt` | Optimizer state dict for resuming training |
|
| 110 |
+
| `config.json` | Model architecture configuration |
|
| 111 |
+
| `training_config.json` | Training hyperparameters and loss history |
|
| 112 |
+
| `tokenizer/` | GPT-2 tokenizer files |
|
| 113 |
+
| `loss_curves.png` | Training/validation loss plot |
|
| 114 |
+
|
| 115 |
+
## Intended Use
|
| 116 |
+
|
| 117 |
+
This model is intended for **research purposes** in medical NLP. It can be used as:
|
| 118 |
+
- A foundation model for downstream medical NLP tasks (NER, classification, QA)
|
| 119 |
+
- A starting point for medical instruction tuning
|
| 120 |
+
- A baseline for comparing medical language model architectures
|
| 121 |
+
|
| 122 |
+
## Limitations
|
| 123 |
+
|
| 124 |
+
- **Not for clinical use**: This model should NOT be used for clinical decision-making
|
| 125 |
+
- **Small scale**: ~381M parameters is relatively small; larger models will perform better
|
| 126 |
+
- **Limited data**: Trained on ~44M tokens (production models use trillions)
|
| 127 |
+
- **No alignment**: This is a base model without instruction tuning or RLHF
|
| 128 |
+
- **English only**: Trained exclusively on English medical text
|
| 129 |
+
- **Potential biases**: May reflect biases present in the medical literature
|
| 130 |
+
|
| 131 |
+
## License
|
| 132 |
+
|
| 133 |
+
Apache 2.0
|
config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"block_size": 1024,
|
| 3 |
+
"vocab_size": 50257,
|
| 4 |
+
"n_layer": 24,
|
| 5 |
+
"n_head": 16,
|
| 6 |
+
"n_kv_head": 8,
|
| 7 |
+
"n_embd": 1024,
|
| 8 |
+
"dropout": 0.1,
|
| 9 |
+
"rope_theta": 10000.0,
|
| 10 |
+
"architecture": "MedSLM",
|
| 11 |
+
"model_type": "med-slm",
|
| 12 |
+
"improvements": [
|
| 13 |
+
"RMSNorm (replaces LayerNorm)",
|
| 14 |
+
"Rotary Positional Embeddings / RoPE (replaces learned absolute)",
|
| 15 |
+
"SwiGLU activation (replaces GELU)",
|
| 16 |
+
"Grouped-Query Attention / GQA (replaces standard MHA)"
|
| 17 |
+
]
|
| 18 |
+
}
|
loss_curves.png
ADDED
|
Git LFS Details
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0e79b77f38368e09a9d0ca5ae3ed78b8cc4d05a5b9de345adbce10de2d5c524a
|
| 3 |
+
size 1319662784
|
optimizer.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:272e4a8326b9a8f0169a5e7b3f924a09f11f19d0f9769e2e1ff3c1c8cdc70836
|
| 3 |
+
size 2639460235
|
tokenizer/tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer/tokenizer_config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": "<|endoftext|>",
|
| 5 |
+
"eos_token": "<|endoftext|>",
|
| 6 |
+
"errors": "replace",
|
| 7 |
+
"is_local": false,
|
| 8 |
+
"model_max_length": 1024,
|
| 9 |
+
"pad_token": null,
|
| 10 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 11 |
+
"unk_token": "<|endoftext|>"
|
| 12 |
+
}
|
training_config.json
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"best_step": 19500,
|
| 3 |
+
"best_val_loss": 3.219841718673706,
|
| 4 |
+
"learning_rate": 0.0003,
|
| 5 |
+
"min_lr": 3e-05,
|
| 6 |
+
"max_iters": 20000,
|
| 7 |
+
"warmup_steps": 1000,
|
| 8 |
+
"batch_size": 8,
|
| 9 |
+
"gradient_accumulation_steps": 32,
|
| 10 |
+
"effective_batch_size": 256,
|
| 11 |
+
"block_size": 1024,
|
| 12 |
+
"precision": "bfloat16",
|
| 13 |
+
"optimizer": "AdamW",
|
| 14 |
+
"betas": [
|
| 15 |
+
0.9,
|
| 16 |
+
0.95
|
| 17 |
+
],
|
| 18 |
+
"weight_decay": 0.1,
|
| 19 |
+
"grad_clip": 1.0,
|
| 20 |
+
"dataset": "Saminx22/medical_data_for_slm",
|
| 21 |
+
"tokenizer": "gpt2",
|
| 22 |
+
"train_losses": [
|
| 23 |
+
7.879132270812988,
|
| 24 |
+
6.511569023132324,
|
| 25 |
+
6.0676093101501465,
|
| 26 |
+
5.704098701477051,
|
| 27 |
+
5.454875946044922,
|
| 28 |
+
5.183218002319336,
|
| 29 |
+
4.928450107574463,
|
| 30 |
+
4.7383880615234375,
|
| 31 |
+
4.58985710144043,
|
| 32 |
+
4.431442737579346,
|
| 33 |
+
4.286258697509766,
|
| 34 |
+
4.190935134887695,
|
| 35 |
+
4.071569442749023,
|
| 36 |
+
3.9881348609924316,
|
| 37 |
+
3.9011902809143066,
|
| 38 |
+
3.81817626953125,
|
| 39 |
+
3.7602646350860596,
|
| 40 |
+
3.687962532043457,
|
| 41 |
+
3.623549461364746,
|
| 42 |
+
3.596832275390625,
|
| 43 |
+
3.516059160232544,
|
| 44 |
+
3.483616352081299,
|
| 45 |
+
3.4555230140686035,
|
| 46 |
+
3.4156558513641357,
|
| 47 |
+
3.3746585845947266,
|
| 48 |
+
3.366776704788208,
|
| 49 |
+
3.336205244064331,
|
| 50 |
+
3.320267915725708,
|
| 51 |
+
3.2959280014038086,
|
| 52 |
+
3.2804367542266846,
|
| 53 |
+
3.2574355602264404,
|
| 54 |
+
3.247173547744751,
|
| 55 |
+
3.244846820831299,
|
| 56 |
+
3.2060155868530273,
|
| 57 |
+
3.192638635635376,
|
| 58 |
+
3.1810803413391113,
|
| 59 |
+
3.1632189750671387,
|
| 60 |
+
3.161890983581543,
|
| 61 |
+
3.1812844276428223
|
| 62 |
+
],
|
| 63 |
+
"val_losses": [
|
| 64 |
+
7.889859199523926,
|
| 65 |
+
6.505797863006592,
|
| 66 |
+
6.062633037567139,
|
| 67 |
+
5.6944580078125,
|
| 68 |
+
5.421933174133301,
|
| 69 |
+
5.1644487380981445,
|
| 70 |
+
4.942782402038574,
|
| 71 |
+
4.735868453979492,
|
| 72 |
+
4.573709487915039,
|
| 73 |
+
4.4302897453308105,
|
| 74 |
+
4.306708812713623,
|
| 75 |
+
4.180722713470459,
|
| 76 |
+
4.104051113128662,
|
| 77 |
+
4.005853652954102,
|
| 78 |
+
3.9109673500061035,
|
| 79 |
+
3.827249050140381,
|
| 80 |
+
3.7792670726776123,
|
| 81 |
+
3.678201198577881,
|
| 82 |
+
3.652942419052124,
|
| 83 |
+
3.6105289459228516,
|
| 84 |
+
3.561580181121826,
|
| 85 |
+
3.52152156829834,
|
| 86 |
+
3.4833824634552,
|
| 87 |
+
3.45076847076416,
|
| 88 |
+
3.4399120807647705,
|
| 89 |
+
3.411548376083374,
|
| 90 |
+
3.3855507373809814,
|
| 91 |
+
3.3449840545654297,
|
| 92 |
+
3.337197780609131,
|
| 93 |
+
3.3391780853271484,
|
| 94 |
+
3.301971673965454,
|
| 95 |
+
3.291358709335327,
|
| 96 |
+
3.2936108112335205,
|
| 97 |
+
3.2686355113983154,
|
| 98 |
+
3.2617692947387695,
|
| 99 |
+
3.258305311203003,
|
| 100 |
+
3.2488183975219727,
|
| 101 |
+
3.233189105987549,
|
| 102 |
+
3.219841718673706
|
| 103 |
+
]
|
| 104 |
+
}
|